]> git.saurik.com Git - apple/security.git/blob - AppleCSP/MiscCSPAlgs/miscAlgFactory.cpp
2e7e848bbe6f899a52e554eb206313c870898b0c
[apple/security.git] / AppleCSP / MiscCSPAlgs / miscAlgFactory.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // miscAlgFactory.h - miscellaneous algorithm factory
21 // Written by Doug Mitchell 3/28/2001
22 //
23
24 #include "miscAlgFactory.h"
25 #include <AES/aescspi.h>
26 #include "desContext.h"
27 #include "rc2Context.h"
28 #include "rc4Context.h"
29 #include "rc5Context.h"
30 #include "MacContext.h"
31 #include "DigestContext.h"
32 #include "SHA1_MD5_Object.h" /* raw digest */
33 #include "MD2Object.h"
34 #include <Security/cssmapple.h>
35
36 /*
37 * normally CSSM_ALGID_SHA1HMAC_LEGACY maps to a MacLegacyContext if
38 * CRYPTKIT_CSP_ENABLE is true. For quick testing, we also map
39 * CSSM_ALGID_SHA1HMAC to MacLegacyContext.
40 */
41 #define HMAC_BOGUS_ENABLE 0
42
43 /*
44 * These #defines are mainly to facilitate measuring the performance of our own
45 * implementation vs. the ones in BSafe. This factory gets called first; if
46 * we disable e.g. DES here the BSAFE version will be used.
47 */
48 #ifdef BSAFE_CSP_ENABLE
49
50 #define MAF_DES_ENABLE 0
51 #define MAF_DES3_ENABLE 0
52 #define MAF_RC2_ENABLE 0
53 #define MAF_RC4_ENABLE 0
54 #define MAF_RC5_ENABLE 0
55 #define MAF_MAC_ENABLE 0
56
57 #else /* !BSAFE_CSP_ENABLE, normal case */
58
59 #define MAF_DES_ENABLE 1
60 #define MAF_DES3_ENABLE 1
61 #define MAF_RC2_ENABLE 1
62 #define MAF_RC4_ENABLE 1
63 #define MAF_RC5_ENABLE 1
64 #define MAF_MAC_ENABLE 1
65
66 #endif /* BSAFE_CSP_ENABLE */
67
68 #if (!MAF_DES_ENABLE || !MAF_DES3_ENABLE || !MAF_RC2_ENABLE || !MAF_RC4_ENABLE || \
69 !MAF_RC5_ENABLE || !MAF_MAC_ENABLE)
70 #warning Internal DES/RC2/RC4/RC5/Mac implementation disabled!
71 #endif
72
73 bool MiscAlgFactory::setup(
74 AppleCSPSession &session,
75 CSPFullPluginSession::CSPContext * &cspCtx,
76 const Context &context)
77 {
78 CSSM_CONTEXT_TYPE ctype = context.type();
79 CSSM_ALGORITHMS alg = context.algorithm();
80
81 switch(ctype) {
82 case CSSM_ALGCLASS_SYMMETRIC:
83 switch(alg) {
84 case CSSM_ALGID_AES:
85 if(cspCtx == NULL) {
86 cspCtx = new AESContext(session);
87 }
88 return true;
89
90 #if MAF_DES_ENABLE
91 case CSSM_ALGID_DES:
92 if(cspCtx == NULL) {
93 cspCtx = new DESContext(session);
94 }
95 return true;
96 #endif /* MAF_DES_ENABLE */
97
98 #if MAF_DES3_ENABLE
99 /*
100 * TripleDES: for some reason, cssmtype.h defines different symbols
101 * for CSSM_ALGID_3DES_3KEY (key gen) and CSSM_ALGID_3DES_3KEY_EDE
102 * (an encrypt alg with mode), but they define to the same value.
103 */
104 case CSSM_ALGID_3DES_3KEY_EDE:
105 if(cspCtx == NULL) {
106 cspCtx = new DES3Context(session);
107 }
108 return true;
109 #endif
110
111 #if MAF_RC2_ENABLE
112 case CSSM_ALGID_RC2:
113 if(cspCtx == NULL) {
114 cspCtx = new RC2Context(session);
115 }
116 return true;
117 #endif
118
119 #if MAF_RC4_ENABLE
120 case CSSM_ALGID_RC4:
121 if(cspCtx == NULL) {
122 cspCtx = new RC4Context(session);
123 }
124 return true;
125 #endif
126
127 #if MAF_RC5_ENABLE
128 case CSSM_ALGID_RC5:
129 if(cspCtx == NULL) {
130 cspCtx = new RC5Context(session);
131 }
132 return true;
133 #endif
134
135 default:
136 break; // not our symmetric alg
137 } // switch alg for symmetric
138 break; // from case CSSM_ALGCLASS_SYMMETRIC
139
140 /* digest algorithms always enabled here */
141 case CSSM_ALGCLASS_DIGEST:
142 switch(alg) {
143 case CSSM_ALGID_SHA1:
144 if(cspCtx == NULL) {
145 /* reuse is OK */
146 cspCtx = new DigestContext(session,
147 *(new SHA1Object));
148 }
149 return true;
150 case CSSM_ALGID_MD5:
151 if(cspCtx == NULL) {
152 /* reuse is OK */
153 cspCtx = new DigestContext(session,
154 *(new MD5Object));
155 }
156 return true;
157 case CSSM_ALGID_MD2:
158 if(cspCtx == NULL) {
159 /* reuse is OK */
160 cspCtx = new DigestContext(session,
161 *(new MD2Object));
162 }
163 return true;
164 default:
165 break; // not our digest alg
166 } // switch digest alg
167 break; // from case CSSM_ALGCLASS_DIGEST
168
169 case CSSM_ALGCLASS_KEYGEN:
170 switch(alg) {
171 case CSSM_ALGID_AES:
172 if(cspCtx == NULL) {
173 cspCtx = new AESKeyGenContext(session);
174 }
175 return true;
176
177 #if MAF_DES_ENABLE
178 case CSSM_ALGID_DES:
179 if(cspCtx == NULL) {
180 cspCtx = new AppleSymmKeyGenerator(session,
181 DES_KEY_SIZE_BITS_EXTERNAL,
182 DES_KEY_SIZE_BITS_EXTERNAL,
183 true); // must be byte size
184 }
185 return true;
186 #endif /* MAF_DES_ENABLE */
187
188 #if MAF_DES3_ENABLE
189 case CSSM_ALGID_3DES_3KEY_EDE:
190 if(cspCtx == NULL) {
191 cspCtx = new AppleSymmKeyGenerator(session,
192 DES3_KEY_SIZE_BYTES * 8,
193 DES3_KEY_SIZE_BYTES * 8,
194 true); // must be byte size
195 }
196 return true;
197 #endif
198
199 #if MAF_RC2_ENABLE
200 case CSSM_ALGID_RC2:
201 if(cspCtx == NULL) {
202 cspCtx = new AppleSymmKeyGenerator(session,
203 RC2_MIN_KEY_SIZE_BYTES * 8,
204 RC2_MAX_KEY_SIZE_BYTES * 8,
205 true); // must be byte size
206 }
207 return true;
208 #endif
209
210 #if MAF_RC4_ENABLE
211 case CSSM_ALGID_RC4:
212 if(cspCtx == NULL) {
213 cspCtx = new AppleSymmKeyGenerator(session,
214 RC4_MIN_KEY_SIZE_BYTES * 8,
215 RC4_MAX_KEY_SIZE_BYTES * 8,
216 true); // must be byte size
217 }
218 return true;
219 #endif
220
221 #if MAF_RC5_ENABLE
222 case CSSM_ALGID_RC5:
223 if(cspCtx == NULL) {
224 cspCtx = new AppleSymmKeyGenerator(session,
225 RC5_MIN_KEY_SIZE_BYTES * 8,
226 RC5_MAX_KEY_SIZE_BYTES * 8,
227 true); // must be byte size
228 }
229 return true;
230 #endif
231
232 #if MAF_MAC_ENABLE
233 case CSSM_ALGID_SHA1HMAC:
234 if(cspCtx == NULL) {
235 cspCtx = new AppleSymmKeyGenerator(session,
236 HMAC_MIN_KEY_SIZE * 8,
237 HMAC_MAX_KEY_SIZE * 8,
238 true); // must be byte size
239 }
240 return true;
241 #endif
242
243 default:
244 break; // not our keygen alg
245 } // switch alg for keygen
246 break; // from case CSSM_ALGCLASS_KEYGEN
247
248 case CSSM_ALGCLASS_MAC:
249 switch(alg) {
250 #if MAF_MAC_ENABLE
251 case CSSM_ALGID_SHA1HMAC:
252 if(cspCtx == NULL) {
253 #if HMAC_BOGUS_ENABLE
254 /* quick hack for Keychain Access testing */
255 cspCtx = new MacLegacyContext(session);
256 #else
257 cspCtx = new MacContext(session);
258 #endif
259 }
260 return true;
261 #endif
262 #if CRYPTKIT_CSP_ENABLE
263 case CSSM_ALGID_SHA1HMAC_LEGACY:
264 if(cspCtx == NULL) {
265 cspCtx = new MacLegacyContext(session);
266 }
267 return true;
268 #endif
269 default:
270 /* not our mac alg */
271 break;
272 }
273 break;
274
275 default:
276 break; // not our context type
277 } // switch context type
278
279 /* not ours */
280 return false;
281 }