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 "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>
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.
41 #define HMAC_BOGUS_ENABLE 0
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.
48 #ifdef BSAFE_CSP_ENABLE
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
57 #else /* !BSAFE_CSP_ENABLE, normal case */
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
66 #endif /* BSAFE_CSP_ENABLE */
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!
73 bool MiscAlgFactory::setup(
74 AppleCSPSession
&session
,
75 CSPFullPluginSession::CSPContext
* &cspCtx
,
76 const Context
&context
)
78 CSSM_CONTEXT_TYPE ctype
= context
.type();
79 CSSM_ALGORITHMS alg
= context
.algorithm();
82 case CSSM_ALGCLASS_SYMMETRIC
:
86 cspCtx
= new AESContext(session
);
93 cspCtx
= new DESContext(session
);
96 #endif /* MAF_DES_ENABLE */
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.
104 case CSSM_ALGID_3DES_3KEY_EDE
:
106 cspCtx
= new DES3Context(session
);
114 cspCtx
= new RC2Context(session
);
122 cspCtx
= new RC4Context(session
);
130 cspCtx
= new RC5Context(session
);
136 break; // not our symmetric alg
137 } // switch alg for symmetric
138 break; // from case CSSM_ALGCLASS_SYMMETRIC
140 /* digest algorithms always enabled here */
141 case CSSM_ALGCLASS_DIGEST
:
143 case CSSM_ALGID_SHA1
:
146 cspCtx
= new DigestContext(session
,
153 cspCtx
= new DigestContext(session
,
160 cspCtx
= new DigestContext(session
,
165 break; // not our digest alg
166 } // switch digest alg
167 break; // from case CSSM_ALGCLASS_DIGEST
169 case CSSM_ALGCLASS_KEYGEN
:
173 cspCtx
= new AESKeyGenContext(session
);
180 cspCtx
= new AppleSymmKeyGenerator(session
,
181 DES_KEY_SIZE_BITS_EXTERNAL
,
182 DES_KEY_SIZE_BITS_EXTERNAL
,
183 true); // must be byte size
186 #endif /* MAF_DES_ENABLE */
189 case CSSM_ALGID_3DES_3KEY_EDE
:
191 cspCtx
= new AppleSymmKeyGenerator(session
,
192 DES3_KEY_SIZE_BYTES
* 8,
193 DES3_KEY_SIZE_BYTES
* 8,
194 true); // must be byte size
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
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
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
233 case CSSM_ALGID_SHA1HMAC
:
235 cspCtx
= new AppleSymmKeyGenerator(session
,
236 HMAC_MIN_KEY_SIZE
* 8,
237 HMAC_MAX_KEY_SIZE
* 8,
238 true); // must be byte size
244 break; // not our keygen alg
245 } // switch alg for keygen
246 break; // from case CSSM_ALGCLASS_KEYGEN
248 case CSSM_ALGCLASS_MAC
:
251 case CSSM_ALGID_SHA1HMAC
:
253 #if HMAC_BOGUS_ENABLE
254 /* quick hack for Keychain Access testing */
255 cspCtx
= new MacLegacyContext(session
);
257 cspCtx
= new MacContext(session
);
262 #if CRYPTKIT_CSP_ENABLE
263 case CSSM_ALGID_SHA1HMAC_LEGACY
:
265 cspCtx
= new MacLegacyContext(session
);
270 /* not our mac alg */
276 break; // not our context type
277 } // switch context type