]> git.saurik.com Git - apple/security.git/blob - libsecurity_apple_csp/lib/miscAlgFactory.cpp
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_apple_csp / lib / 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 <aescspi.h>
26 #include <gladmanContext.h>
27 #include "desContext.h"
28 #include "rc2Context.h"
29 #include "rc4Context.h"
30 #include "rc5Context.h"
31 #include "MacContext.h"
32 #include "DigestContext.h"
33 #include "SHA1_MD5_Object.h" /* raw digest */
34 #include "SHA2_Object.h"
35 #include "MD2Object.h"
36 #include "NullCryptor.h"
37 #include "bfContext.h"
38 #include "castContext.h"
39 #include <Security/cssmapple.h>
40
41 /*
42 * These #defines are mainly to facilitate measuring the performance of our own
43 * implementation vs. the ones in BSafe. This factory gets called first; if
44 * we disable e.g. DES here the BSAFE version will be used.
45 */
46 #ifdef BSAFE_CSP_ENABLE
47
48 #define MAF_DES_ENABLE 0
49 #define MAF_DES3_ENABLE 0
50 #define MAF_RC2_ENABLE 0
51 #define MAF_RC4_ENABLE 0
52 #define MAF_RC5_ENABLE 0
53 #define MAF_MAC_ENABLE 0
54
55 #else /* !BSAFE_CSP_ENABLE, normal case */
56
57 #define MAF_DES_ENABLE 1
58 #define MAF_DES3_ENABLE 1
59 #define MAF_RC2_ENABLE 1
60 #define MAF_RC4_ENABLE 1
61 #define MAF_RC5_ENABLE 1
62 #define MAF_MAC_ENABLE 1
63
64 #endif /* BSAFE_CSP_ENABLE */
65
66 #if (!MAF_DES_ENABLE || !MAF_DES3_ENABLE || !MAF_RC2_ENABLE || !MAF_RC4_ENABLE || \
67 !MAF_RC5_ENABLE || !MAF_MAC_ENABLE)
68 #warning Internal DES/RC2/RC4/RC5/Mac implementation disabled!
69 #endif
70
71 bool MiscAlgFactory::setup(
72 AppleCSPSession &session,
73 CSPFullPluginSession::CSPContext * &cspCtx,
74 const Context &context)
75 {
76 CSSM_CONTEXT_TYPE ctype = context.type();
77 CSSM_ALGORITHMS alg = context.algorithm();
78
79 switch(ctype) {
80 case CSSM_ALGCLASS_SYMMETRIC:
81 switch(alg) {
82 case CSSM_ALGID_AES:
83 if(cspCtx == NULL) {
84 /*
85 * Get optional block size to determine correct implementation
86 */
87 uint32 blockSize = context.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE);
88 if(blockSize == 0) {
89 blockSize = GLADMAN_BLOCK_SIZE_BYTES;
90 }
91 if(GLADMAN_AES_128_ENABLE &&
92 (blockSize == GLADMAN_BLOCK_SIZE_BYTES)) {
93 cspCtx = new GAESContext(session);
94 }
95 else {
96 cspCtx = new AESContext(session);
97 }
98 }
99 return true;
100
101 #if MAF_DES_ENABLE
102 case CSSM_ALGID_DES:
103 if(cspCtx == NULL) {
104 cspCtx = new DESContext(session);
105 }
106 return true;
107 #endif /* MAF_DES_ENABLE */
108
109 #if MAF_DES3_ENABLE
110 /*
111 * TripleDES: for some reason, cssmtype.h defines different symbols
112 * for CSSM_ALGID_3DES_3KEY (key gen) and CSSM_ALGID_3DES_3KEY_EDE
113 * (an encrypt alg with mode), but they define to the same value.
114 */
115 case CSSM_ALGID_3DES_3KEY_EDE:
116 if(cspCtx == NULL) {
117 cspCtx = new DES3Context(session);
118 }
119 return true;
120 #endif
121
122 #if MAF_RC2_ENABLE
123 case CSSM_ALGID_RC2:
124 if(cspCtx == NULL) {
125 cspCtx = new RC2Context(session);
126 }
127 return true;
128 #endif
129
130 #if MAF_RC4_ENABLE
131 case CSSM_ALGID_RC4:
132 if(cspCtx == NULL) {
133 cspCtx = new RC4Context(session);
134 }
135 return true;
136 #endif
137
138 #if MAF_RC5_ENABLE
139 case CSSM_ALGID_RC5:
140 if(cspCtx == NULL) {
141 cspCtx = new RC5Context(session);
142 }
143 return true;
144 #endif
145
146 case CSSM_ALGID_BLOWFISH:
147 if(cspCtx == NULL) {
148 cspCtx = new BlowfishContext(session);
149 }
150 return true;
151
152 case CSSM_ALGID_CAST:
153 case CSSM_ALGID_CAST5:
154 if(cspCtx == NULL) {
155 cspCtx = new CastContext(session);
156 }
157 return true;
158
159 #if NULL_CRYPT_ENABLE
160 case CSSM_ALGID_NONE:
161 if(cspCtx == NULL) {
162 cspCtx = new NullCryptor(session);
163 }
164 return true;
165 #endif /* NULL_CRYPT_ENABLE */
166
167 default:
168 break; // not our symmetric alg
169 } // switch alg for symmetric
170 break; // from case CSSM_ALGCLASS_SYMMETRIC
171
172 /* digest algorithms always enabled here */
173 case CSSM_ALGCLASS_DIGEST:
174 switch(alg) {
175 case CSSM_ALGID_SHA1:
176 if(cspCtx == NULL) {
177 /* reuse is OK */
178 cspCtx = new DigestContext(session,
179 *(new SHA1Object));
180 }
181 return true;
182 case CSSM_ALGID_MD5:
183 if(cspCtx == NULL) {
184 /* reuse is OK */
185 cspCtx = new DigestContext(session,
186 *(new MD5Object));
187 }
188 return true;
189 case CSSM_ALGID_MD2:
190 if(cspCtx == NULL) {
191 /* reuse is OK */
192 cspCtx = new DigestContext(session,
193 *(new MD2Object));
194 }
195 return true;
196 case CSSM_ALGID_SHA224:
197 if(cspCtx == NULL) {
198 /* reuse is OK */
199 cspCtx = new DigestContext(session,
200 *(new SHA224Object));
201 }
202 return true;
203 case CSSM_ALGID_SHA256:
204 if(cspCtx == NULL) {
205 /* reuse is OK */
206 cspCtx = new DigestContext(session,
207 *(new SHA256Object));
208 }
209 return true;
210 case CSSM_ALGID_SHA384:
211 if(cspCtx == NULL) {
212 /* reuse is OK */
213 cspCtx = new DigestContext(session,
214 *(new SHA384Object));
215 }
216 return true;
217 case CSSM_ALGID_SHA512:
218 if(cspCtx == NULL) {
219 /* reuse is OK */
220 cspCtx = new DigestContext(session,
221 *(new SHA512Object));
222 }
223 return true;
224 default:
225 break; // not our digest alg
226 } // switch digest alg
227 break; // from case CSSM_ALGCLASS_DIGEST
228
229 case CSSM_ALGCLASS_KEYGEN:
230 switch(alg) {
231 case CSSM_ALGID_AES:
232 if(cspCtx == NULL) {
233 cspCtx = new AESKeyGenContext(session);
234 }
235 return true;
236
237 #if MAF_DES_ENABLE
238 case CSSM_ALGID_DES:
239 if(cspCtx == NULL) {
240 cspCtx = new AppleSymmKeyGenerator(session,
241 DES_KEY_SIZE_BITS_EXTERNAL,
242 DES_KEY_SIZE_BITS_EXTERNAL,
243 true); // must be byte size
244 }
245 return true;
246 #endif /* MAF_DES_ENABLE */
247
248 #if MAF_DES3_ENABLE
249 case CSSM_ALGID_3DES_3KEY_EDE:
250 if(cspCtx == NULL) {
251 cspCtx = new AppleSymmKeyGenerator(session,
252 DES3_KEY_SIZE_BYTES * 8,
253 DES3_KEY_SIZE_BYTES * 8,
254 true); // must be byte size
255 }
256 return true;
257 #endif
258
259 #if MAF_RC2_ENABLE
260 case CSSM_ALGID_RC2:
261 if(cspCtx == NULL) {
262 cspCtx = new AppleSymmKeyGenerator(session,
263 RC2_MIN_KEY_SIZE_BYTES * 8,
264 RC2_MAX_KEY_SIZE_BYTES * 8,
265 true); // must be byte size
266 }
267 return true;
268 #endif
269
270 #if MAF_RC4_ENABLE
271 case CSSM_ALGID_RC4:
272 if(cspCtx == NULL) {
273 cspCtx = new AppleSymmKeyGenerator(session,
274 kCCKeySizeMinRC4 * 8,
275 kCCKeySizeMaxRC4 * 8,
276 true); // must be byte size
277 }
278 return true;
279 #endif
280
281 #if MAF_RC5_ENABLE
282 case CSSM_ALGID_RC5:
283 if(cspCtx == NULL) {
284 cspCtx = new AppleSymmKeyGenerator(session,
285 RC5_MIN_KEY_SIZE_BYTES * 8,
286 RC5_MAX_KEY_SIZE_BYTES * 8,
287 true); // must be byte size
288 }
289 return true;
290 #endif
291
292 case CSSM_ALGID_BLOWFISH:
293 if(cspCtx == NULL) {
294 cspCtx = new AppleSymmKeyGenerator(session,
295 BF_MIN_KEY_SIZE_BYTES * 8,
296 BF_MAX_KEY_SIZE_BYTES * 8,
297 true); // must be byte size
298 }
299 return true;
300
301 /* Note we require keys to be ALGID_CAST, not ALGID_CAST5 */
302 case CSSM_ALGID_CAST:
303 if(cspCtx == NULL) {
304 cspCtx = new AppleSymmKeyGenerator(session,
305 kCCKeySizeMinCAST * 8,
306 kCCKeySizeMaxCAST * 8,
307 true); // must be byte size
308 }
309 return true;
310
311 #if MAF_MAC_ENABLE
312 case CSSM_ALGID_SHA1HMAC:
313 if(cspCtx == NULL) {
314 cspCtx = new AppleSymmKeyGenerator(session,
315 HMAC_SHA_MIN_KEY_SIZE * 8,
316 HMAC_MAX_KEY_SIZE * 8,
317 true); // must be byte size
318 }
319 return true;
320 case CSSM_ALGID_MD5HMAC:
321 if(cspCtx == NULL) {
322 cspCtx = new AppleSymmKeyGenerator(session,
323 HMAC_MD5_MIN_KEY_SIZE * 8,
324 HMAC_MAX_KEY_SIZE * 8,
325 true); // must be byte size
326 }
327 return true;
328 #endif
329
330 #if NULL_CRYPT_ENABLE
331 case CSSM_ALGID_NONE:
332 if(cspCtx == NULL) {
333 cspCtx = new AppleSymmKeyGenerator(session,
334 NULL_CRYPT_BLOCK_SIZE * 8,
335 NULL_CRYPT_BLOCK_SIZE * 8,
336 true); // must be byte size
337 }
338 return true;
339 #endif /* NULL_CRYPT_ENABLE */
340
341 default:
342 break; // not our keygen alg
343 } // switch alg for keygen
344 break; // from case CSSM_ALGCLASS_KEYGEN
345
346 case CSSM_ALGCLASS_MAC:
347 switch(alg) {
348 #if MAF_MAC_ENABLE
349 case CSSM_ALGID_SHA1HMAC:
350 case CSSM_ALGID_MD5HMAC:
351 if(cspCtx == NULL) {
352 cspCtx = new MacContext(session, alg);
353 }
354 return true;
355 #endif
356 #if CRYPTKIT_CSP_ENABLE
357 case CSSM_ALGID_SHA1HMAC_LEGACY:
358 if(cspCtx == NULL) {
359 cspCtx = new MacLegacyContext(session, alg);
360 }
361 return true;
362 #endif
363 default:
364 /* not our mac alg */
365 break;
366 }
367 break;
368
369 default:
370 break; // not our context type
371 } // switch context type
372
373 /* not ours */
374 return false;
375 }