2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // miscAlgFactory.h - miscellaneous algorithm factory
21 // Written by Doug Mitchell 3/28/2001
24 #include "miscAlgFactory.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>
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.
46 #ifdef BSAFE_CSP_ENABLE
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
55 #else /* !BSAFE_CSP_ENABLE, normal case */
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
64 #endif /* BSAFE_CSP_ENABLE */
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!
71 bool MiscAlgFactory::setup(
72 AppleCSPSession
&session
,
73 CSPFullPluginSession::CSPContext
* &cspCtx
,
74 const Context
&context
)
76 CSSM_CONTEXT_TYPE ctype
= context
.type();
77 CSSM_ALGORITHMS alg
= context
.algorithm();
80 case CSSM_ALGCLASS_SYMMETRIC
:
85 * Get optional block size to determine correct implementation
87 uint32 blockSize
= context
.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE
);
89 blockSize
= GLADMAN_BLOCK_SIZE_BYTES
;
91 if(GLADMAN_AES_128_ENABLE
&&
92 (blockSize
== GLADMAN_BLOCK_SIZE_BYTES
)) {
93 cspCtx
= new GAESContext(session
);
96 cspCtx
= new AESContext(session
);
104 cspCtx
= new DESContext(session
);
107 #endif /* MAF_DES_ENABLE */
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.
115 case CSSM_ALGID_3DES_3KEY_EDE
:
117 cspCtx
= new DES3Context(session
);
125 cspCtx
= new RC2Context(session
);
133 cspCtx
= new RC4Context(session
);
141 cspCtx
= new RC5Context(session
);
146 case CSSM_ALGID_BLOWFISH
:
148 cspCtx
= new BlowfishContext(session
);
152 case CSSM_ALGID_CAST
:
153 case CSSM_ALGID_CAST5
:
155 cspCtx
= new CastContext(session
);
159 #if NULL_CRYPT_ENABLE
160 case CSSM_ALGID_NONE
:
162 cspCtx
= new NullCryptor(session
);
165 #endif /* NULL_CRYPT_ENABLE */
168 break; // not our symmetric alg
169 } // switch alg for symmetric
170 break; // from case CSSM_ALGCLASS_SYMMETRIC
172 /* digest algorithms always enabled here */
173 case CSSM_ALGCLASS_DIGEST
:
175 case CSSM_ALGID_SHA1
:
178 cspCtx
= new DigestContext(session
,
185 cspCtx
= new DigestContext(session
,
192 cspCtx
= new DigestContext(session
,
196 case CSSM_ALGID_SHA224
:
199 cspCtx
= new DigestContext(session
,
200 *(new SHA224Object
));
203 case CSSM_ALGID_SHA256
:
206 cspCtx
= new DigestContext(session
,
207 *(new SHA256Object
));
210 case CSSM_ALGID_SHA384
:
213 cspCtx
= new DigestContext(session
,
214 *(new SHA384Object
));
217 case CSSM_ALGID_SHA512
:
220 cspCtx
= new DigestContext(session
,
221 *(new SHA512Object
));
225 break; // not our digest alg
226 } // switch digest alg
227 break; // from case CSSM_ALGCLASS_DIGEST
229 case CSSM_ALGCLASS_KEYGEN
:
233 cspCtx
= new AESKeyGenContext(session
);
240 cspCtx
= new AppleSymmKeyGenerator(session
,
241 DES_KEY_SIZE_BITS_EXTERNAL
,
242 DES_KEY_SIZE_BITS_EXTERNAL
,
243 true); // must be byte size
246 #endif /* MAF_DES_ENABLE */
249 case CSSM_ALGID_3DES_3KEY_EDE
:
251 cspCtx
= new AppleSymmKeyGenerator(session
,
252 DES3_KEY_SIZE_BYTES
* 8,
253 DES3_KEY_SIZE_BYTES
* 8,
254 true); // must be byte size
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
273 cspCtx
= new AppleSymmKeyGenerator(session
,
274 kCCKeySizeMinRC4
* 8,
275 kCCKeySizeMaxRC4
* 8,
276 true); // must be byte size
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
292 case CSSM_ALGID_BLOWFISH
:
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
301 /* Note we require keys to be ALGID_CAST, not ALGID_CAST5 */
302 case CSSM_ALGID_CAST
:
304 cspCtx
= new AppleSymmKeyGenerator(session
,
305 kCCKeySizeMinCAST
* 8,
306 kCCKeySizeMaxCAST
* 8,
307 true); // must be byte size
312 case CSSM_ALGID_SHA1HMAC
:
314 cspCtx
= new AppleSymmKeyGenerator(session
,
315 HMAC_SHA_MIN_KEY_SIZE
* 8,
316 HMAC_MAX_KEY_SIZE
* 8,
317 true); // must be byte size
320 case CSSM_ALGID_MD5HMAC
:
322 cspCtx
= new AppleSymmKeyGenerator(session
,
323 HMAC_MD5_MIN_KEY_SIZE
* 8,
324 HMAC_MAX_KEY_SIZE
* 8,
325 true); // must be byte size
330 #if NULL_CRYPT_ENABLE
331 case CSSM_ALGID_NONE
:
333 cspCtx
= new AppleSymmKeyGenerator(session
,
334 NULL_CRYPT_BLOCK_SIZE
* 8,
335 NULL_CRYPT_BLOCK_SIZE
* 8,
336 true); // must be byte size
339 #endif /* NULL_CRYPT_ENABLE */
342 break; // not our keygen alg
343 } // switch alg for keygen
344 break; // from case CSSM_ALGCLASS_KEYGEN
346 case CSSM_ALGCLASS_MAC
:
349 case CSSM_ALGID_SHA1HMAC
:
350 case CSSM_ALGID_MD5HMAC
:
352 cspCtx
= new MacContext(session
, alg
);
356 #if CRYPTKIT_CSP_ENABLE
357 case CSSM_ALGID_SHA1HMAC_LEGACY
:
359 cspCtx
= new MacLegacyContext(session
, alg
);
364 /* not our mac alg */
370 break; // not our context type
371 } // switch context type