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