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"
25 #include <AES/aescspi.h>
26 #include <AES/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 "MD2Object.h"
35 #include "NullCryptor.h"
36 #include "bfContext.h"
37 #include "castContext.h"
38 #include <Security/cssmapple.h>
41 * These #defines are mainly to facilitate measuring the performance of our own
42 * implementation vs. the ones in BSafe. This factory gets called first; if
43 * we disable e.g. DES here the BSAFE version will be used.
45 #ifdef BSAFE_CSP_ENABLE
47 #define MAF_DES_ENABLE 0
48 #define MAF_DES3_ENABLE 0
49 #define MAF_RC2_ENABLE 0
50 #define MAF_RC4_ENABLE 0
51 #define MAF_RC5_ENABLE 0
52 #define MAF_MAC_ENABLE 0
54 #else /* !BSAFE_CSP_ENABLE, normal case */
56 #define MAF_DES_ENABLE 1
57 #define MAF_DES3_ENABLE 1
58 #define MAF_RC2_ENABLE 1
59 #define MAF_RC4_ENABLE 1
60 #define MAF_RC5_ENABLE 1
61 #define MAF_MAC_ENABLE 1
63 #endif /* BSAFE_CSP_ENABLE */
65 #if (!MAF_DES_ENABLE || !MAF_DES3_ENABLE || !MAF_RC2_ENABLE || !MAF_RC4_ENABLE || \
66 !MAF_RC5_ENABLE || !MAF_MAC_ENABLE)
67 #warning Internal DES/RC2/RC4/RC5/Mac implementation disabled!
70 bool MiscAlgFactory::setup(
71 AppleCSPSession
&session
,
72 CSPFullPluginSession::CSPContext
* &cspCtx
,
73 const Context
&context
)
75 CSSM_CONTEXT_TYPE ctype
= context
.type();
76 CSSM_ALGORITHMS alg
= context
.algorithm();
79 case CSSM_ALGCLASS_SYMMETRIC
:
84 * Get optional block size to determine correct implementation
86 uint32 blockSize
= context
.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE
);
88 blockSize
= GLADMAN_BLOCK_SIZE_BYTES
;
90 if(GLADMAN_AES_128_ENABLE
&&
91 (blockSize
== GLADMAN_BLOCK_SIZE_BYTES
)) {
92 cspCtx
= new GAESContext(session
);
95 cspCtx
= new AESContext(session
);
103 cspCtx
= new DESContext(session
);
106 #endif /* MAF_DES_ENABLE */
110 * TripleDES: for some reason, cssmtype.h defines different symbols
111 * for CSSM_ALGID_3DES_3KEY (key gen) and CSSM_ALGID_3DES_3KEY_EDE
112 * (an encrypt alg with mode), but they define to the same value.
114 case CSSM_ALGID_3DES_3KEY_EDE
:
116 cspCtx
= new DES3Context(session
);
124 cspCtx
= new RC2Context(session
);
132 cspCtx
= new RC4Context(session
);
140 cspCtx
= new RC5Context(session
);
145 case CSSM_ALGID_BLOWFISH
:
147 cspCtx
= new BlowfishContext(session
);
151 case CSSM_ALGID_CAST
:
152 case CSSM_ALGID_CAST5
:
154 cspCtx
= new CastContext(session
);
158 #if NULL_CRYPT_ENABLE
159 case CSSM_ALGID_NONE
:
161 cspCtx
= new NullCryptor(session
);
164 #endif /* NULL_CRYPT_ENABLE */
167 break; // not our symmetric alg
168 } // switch alg for symmetric
169 break; // from case CSSM_ALGCLASS_SYMMETRIC
171 /* digest algorithms always enabled here */
172 case CSSM_ALGCLASS_DIGEST
:
174 case CSSM_ALGID_SHA1
:
177 cspCtx
= new DigestContext(session
,
184 cspCtx
= new DigestContext(session
,
191 cspCtx
= new DigestContext(session
,
196 break; // not our digest alg
197 } // switch digest alg
198 break; // from case CSSM_ALGCLASS_DIGEST
200 case CSSM_ALGCLASS_KEYGEN
:
204 cspCtx
= new AESKeyGenContext(session
);
211 cspCtx
= new AppleSymmKeyGenerator(session
,
212 DES_KEY_SIZE_BITS_EXTERNAL
,
213 DES_KEY_SIZE_BITS_EXTERNAL
,
214 true); // must be byte size
217 #endif /* MAF_DES_ENABLE */
220 case CSSM_ALGID_3DES_3KEY_EDE
:
222 cspCtx
= new AppleSymmKeyGenerator(session
,
223 DES3_KEY_SIZE_BYTES
* 8,
224 DES3_KEY_SIZE_BYTES
* 8,
225 true); // must be byte size
233 cspCtx
= new AppleSymmKeyGenerator(session
,
234 RC2_MIN_KEY_SIZE_BYTES
* 8,
235 RC2_MAX_KEY_SIZE_BYTES
* 8,
236 true); // must be byte size
244 cspCtx
= new AppleSymmKeyGenerator(session
,
245 RC4_MIN_KEY_SIZE_BYTES
* 8,
246 RC4_MAX_KEY_SIZE_BYTES
* 8,
247 true); // must be byte size
255 cspCtx
= new AppleSymmKeyGenerator(session
,
256 RC5_MIN_KEY_SIZE_BYTES
* 8,
257 RC5_MAX_KEY_SIZE_BYTES
* 8,
258 true); // must be byte size
263 case CSSM_ALGID_BLOWFISH
:
265 cspCtx
= new AppleSymmKeyGenerator(session
,
266 BF_MIN_KEY_SIZE_BYTES
* 8,
267 BF_MAX_KEY_SIZE_BYTES
* 8,
268 true); // must be byte size
272 /* Note we require keys to be ALGID_CAST, not ALGID_CAST5 */
273 case CSSM_ALGID_CAST
:
275 cspCtx
= new AppleSymmKeyGenerator(session
,
276 CAST_MIN_KEY_LENGTH
* 8,
278 true); // must be byte size
283 case CSSM_ALGID_SHA1HMAC
:
285 cspCtx
= new AppleSymmKeyGenerator(session
,
286 HMAC_SHA_MIN_KEY_SIZE
* 8,
287 HMAC_MAX_KEY_SIZE
* 8,
288 true); // must be byte size
291 case CSSM_ALGID_MD5HMAC
:
293 cspCtx
= new AppleSymmKeyGenerator(session
,
294 HMAC_MD5_MIN_KEY_SIZE
* 8,
295 HMAC_MAX_KEY_SIZE
* 8,
296 true); // must be byte size
301 #if NULL_CRYPT_ENABLE
302 case CSSM_ALGID_NONE
:
304 cspCtx
= new AppleSymmKeyGenerator(session
,
305 NULL_CRYPT_BLOCK_SIZE
* 8,
306 NULL_CRYPT_BLOCK_SIZE
* 8,
307 true); // must be byte size
310 #endif /* NULL_CRYPT_ENABLE */
313 break; // not our keygen alg
314 } // switch alg for keygen
315 break; // from case CSSM_ALGCLASS_KEYGEN
317 case CSSM_ALGCLASS_MAC
:
320 case CSSM_ALGID_SHA1HMAC
:
321 case CSSM_ALGID_MD5HMAC
:
323 cspCtx
= new MacContext(session
, alg
);
327 #if CRYPTKIT_CSP_ENABLE
328 case CSSM_ALGID_SHA1HMAC_LEGACY
:
330 cspCtx
= new MacLegacyContext(session
, alg
);
335 /* not our mac alg */
341 break; // not our context type
342 } // switch context type