2 * Copyright (c) 2000-2001,2011,2014 Apple 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
23 #include "miscAlgFactory.h"
25 #include <gladmanContext.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 "SHA2_Object.h"
34 #include "MD2Object.h"
35 #include "NullCryptor.h"
36 #include "bfContext.h"
37 #include "castContext.h"
38 #include <Security/cssmapple.h>
40 #define MAF_DES_ENABLE 1
41 #define MAF_DES3_ENABLE 1
42 #define MAF_RC2_ENABLE 1
43 #define MAF_RC4_ENABLE 1
44 #define MAF_RC5_ENABLE 1
45 #define MAF_MAC_ENABLE 1
47 #if (!MAF_DES_ENABLE || !MAF_DES3_ENABLE || !MAF_RC2_ENABLE || !MAF_RC4_ENABLE || \
48 !MAF_RC5_ENABLE || !MAF_MAC_ENABLE)
49 #warning Internal DES/RC2/RC4/RC5/Mac implementation disabled!
52 bool MiscAlgFactory::setup(
53 AppleCSPSession
&session
,
54 CSPFullPluginSession::CSPContext
* &cspCtx
,
55 const Context
&context
)
57 CSSM_CONTEXT_TYPE ctype
= context
.type();
58 CSSM_ALGORITHMS alg
= context
.algorithm();
61 case CSSM_ALGCLASS_SYMMETRIC
:
66 * Get optional block size to determine correct implementation
68 uint32 blockSize
= context
.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE
);
70 blockSize
= GLADMAN_BLOCK_SIZE_BYTES
;
72 if(GLADMAN_AES_128_ENABLE
&&
73 (blockSize
== GLADMAN_BLOCK_SIZE_BYTES
)) {
74 cspCtx
= new GAESContext(session
);
77 cspCtx
= new AESContext(session
);
85 cspCtx
= new DESContext(session
);
88 #endif /* MAF_DES_ENABLE */
92 * TripleDES: for some reason, cssmtype.h defines different symbols
93 * for CSSM_ALGID_3DES_3KEY (key gen) and CSSM_ALGID_3DES_3KEY_EDE
94 * (an encrypt alg with mode), but they define to the same value.
96 case CSSM_ALGID_3DES_3KEY_EDE
:
98 cspCtx
= new DES3Context(session
);
106 cspCtx
= new RC2Context(session
);
114 cspCtx
= new RC4Context(session
);
122 cspCtx
= new RC5Context(session
);
127 case CSSM_ALGID_BLOWFISH
:
129 cspCtx
= new BlowfishContext(session
);
133 case CSSM_ALGID_CAST
:
134 case CSSM_ALGID_CAST5
:
136 cspCtx
= new CastContext(session
);
140 #if NULL_CRYPT_ENABLE
141 case CSSM_ALGID_NONE
:
143 cspCtx
= new NullCryptor(session
);
146 #endif /* NULL_CRYPT_ENABLE */
149 break; // not our symmetric alg
150 } // switch alg for symmetric
151 break; // from case CSSM_ALGCLASS_SYMMETRIC
153 /* digest algorithms always enabled here */
154 case CSSM_ALGCLASS_DIGEST
:
156 case CSSM_ALGID_SHA1
:
159 cspCtx
= new DigestContext(session
,
166 cspCtx
= new DigestContext(session
,
173 cspCtx
= new DigestContext(session
,
177 case CSSM_ALGID_SHA224
:
180 cspCtx
= new DigestContext(session
,
181 *(new SHA224Object
));
184 case CSSM_ALGID_SHA256
:
187 cspCtx
= new DigestContext(session
,
188 *(new SHA256Object
));
191 case CSSM_ALGID_SHA384
:
194 cspCtx
= new DigestContext(session
,
195 *(new SHA384Object
));
198 case CSSM_ALGID_SHA512
:
201 cspCtx
= new DigestContext(session
,
202 *(new SHA512Object
));
206 break; // not our digest alg
207 } // switch digest alg
208 break; // from case CSSM_ALGCLASS_DIGEST
210 case CSSM_ALGCLASS_KEYGEN
:
214 cspCtx
= new AESKeyGenContext(session
);
221 cspCtx
= new AppleSymmKeyGenerator(session
,
222 DES_KEY_SIZE_BITS_EXTERNAL
,
223 DES_KEY_SIZE_BITS_EXTERNAL
,
224 true); // must be byte size
227 #endif /* MAF_DES_ENABLE */
230 case CSSM_ALGID_3DES_3KEY_EDE
:
232 cspCtx
= new AppleSymmKeyGenerator(session
,
233 DES3_KEY_SIZE_BYTES
* 8,
234 DES3_KEY_SIZE_BYTES
* 8,
235 true); // must be byte size
243 cspCtx
= new AppleSymmKeyGenerator(session
,
244 RC2_MIN_KEY_SIZE_BYTES
* 8,
245 RC2_MAX_KEY_SIZE_BYTES
* 8,
246 true); // must be byte size
254 cspCtx
= new AppleSymmKeyGenerator(session
,
255 kCCKeySizeMinRC4
* 8,
256 kCCKeySizeMaxRC4
* 8,
257 true); // must be byte size
265 cspCtx
= new AppleSymmKeyGenerator(session
,
266 RC5_MIN_KEY_SIZE_BYTES
* 8,
267 RC5_MAX_KEY_SIZE_BYTES
* 8,
268 true); // must be byte size
273 case CSSM_ALGID_BLOWFISH
:
275 cspCtx
= new AppleSymmKeyGenerator(session
,
276 BF_MIN_KEY_SIZE_BYTES
* 8,
277 BF_MAX_KEY_SIZE_BYTES
* 8,
278 true); // must be byte size
282 /* Note we require keys to be ALGID_CAST, not ALGID_CAST5 */
283 case CSSM_ALGID_CAST
:
285 cspCtx
= new AppleSymmKeyGenerator(session
,
286 kCCKeySizeMinCAST
* 8,
287 kCCKeySizeMaxCAST
* 8,
288 true); // must be byte size
293 case CSSM_ALGID_SHA1HMAC
:
295 cspCtx
= new AppleSymmKeyGenerator(session
,
296 HMAC_SHA_MIN_KEY_SIZE
* 8,
297 HMAC_MAX_KEY_SIZE
* 8,
298 true); // must be byte size
301 case CSSM_ALGID_MD5HMAC
:
303 cspCtx
= new AppleSymmKeyGenerator(session
,
304 HMAC_MD5_MIN_KEY_SIZE
* 8,
305 HMAC_MAX_KEY_SIZE
* 8,
306 true); // must be byte size
311 #if NULL_CRYPT_ENABLE
312 case CSSM_ALGID_NONE
:
314 cspCtx
= new AppleSymmKeyGenerator(session
,
315 NULL_CRYPT_BLOCK_SIZE
* 8,
316 NULL_CRYPT_BLOCK_SIZE
* 8,
317 true); // must be byte size
320 #endif /* NULL_CRYPT_ENABLE */
323 break; // not our keygen alg
324 } // switch alg for keygen
325 break; // from case CSSM_ALGCLASS_KEYGEN
327 case CSSM_ALGCLASS_MAC
:
330 case CSSM_ALGID_SHA1HMAC
:
331 case CSSM_ALGID_MD5HMAC
:
333 cspCtx
= new MacContext(session
, alg
);
337 #if CRYPTKIT_CSP_ENABLE
338 case CSSM_ALGID_SHA1HMAC_LEGACY
:
340 cspCtx
= new MacLegacyContext(session
, alg
);
345 /* not our mac alg */
351 break; // not our context type
352 } // switch context type