]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_apple_csp/lib/miscAlgFactory.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / libsecurity_apple_csp / lib / miscAlgFactory.cpp
1 /*
2 * Copyright (c) 2000-2001,2011,2014 Apple 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 //
22
23 #include "miscAlgFactory.h"
24 #include <aescspi.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>
39
40 /*
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.
44 */
45 #ifdef BSAFE_CSP_ENABLE
46
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
53
54 #else /* !BSAFE_CSP_ENABLE, normal case */
55
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
62
63 #endif /* BSAFE_CSP_ENABLE */
64
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!
68 #endif
69
70 bool MiscAlgFactory::setup(
71 AppleCSPSession &session,
72 CSPFullPluginSession::CSPContext * &cspCtx,
73 const Context &context)
74 {
75 CSSM_CONTEXT_TYPE ctype = context.type();
76 CSSM_ALGORITHMS alg = context.algorithm();
77
78 switch(ctype) {
79 case CSSM_ALGCLASS_SYMMETRIC:
80 switch(alg) {
81 case CSSM_ALGID_AES:
82 if(cspCtx == NULL) {
83 /*
84 * Get optional block size to determine correct implementation
85 */
86 uint32 blockSize = context.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE);
87 if(blockSize == 0) {
88 blockSize = GLADMAN_BLOCK_SIZE_BYTES;
89 }
90 if(GLADMAN_AES_128_ENABLE &&
91 (blockSize == GLADMAN_BLOCK_SIZE_BYTES)) {
92 cspCtx = new GAESContext(session);
93 }
94 else {
95 cspCtx = new AESContext(session);
96 }
97 }
98 return true;
99
100 #if MAF_DES_ENABLE
101 case CSSM_ALGID_DES:
102 if(cspCtx == NULL) {
103 cspCtx = new DESContext(session);
104 }
105 return true;
106 #endif /* MAF_DES_ENABLE */
107
108 #if MAF_DES3_ENABLE
109 /*
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.
113 */
114 case CSSM_ALGID_3DES_3KEY_EDE:
115 if(cspCtx == NULL) {
116 cspCtx = new DES3Context(session);
117 }
118 return true;
119 #endif
120
121 #if MAF_RC2_ENABLE
122 case CSSM_ALGID_RC2:
123 if(cspCtx == NULL) {
124 cspCtx = new RC2Context(session);
125 }
126 return true;
127 #endif
128
129 #if MAF_RC4_ENABLE
130 case CSSM_ALGID_RC4:
131 if(cspCtx == NULL) {
132 cspCtx = new RC4Context(session);
133 }
134 return true;
135 #endif
136
137 #if MAF_RC5_ENABLE
138 case CSSM_ALGID_RC5:
139 if(cspCtx == NULL) {
140 cspCtx = new RC5Context(session);
141 }
142 return true;
143 #endif
144
145 case CSSM_ALGID_BLOWFISH:
146 if(cspCtx == NULL) {
147 cspCtx = new BlowfishContext(session);
148 }
149 return true;
150
151 case CSSM_ALGID_CAST:
152 case CSSM_ALGID_CAST5:
153 if(cspCtx == NULL) {
154 cspCtx = new CastContext(session);
155 }
156 return true;
157
158 #if NULL_CRYPT_ENABLE
159 case CSSM_ALGID_NONE:
160 if(cspCtx == NULL) {
161 cspCtx = new NullCryptor(session);
162 }
163 return true;
164 #endif /* NULL_CRYPT_ENABLE */
165
166 default:
167 break; // not our symmetric alg
168 } // switch alg for symmetric
169 break; // from case CSSM_ALGCLASS_SYMMETRIC
170
171 /* digest algorithms always enabled here */
172 case CSSM_ALGCLASS_DIGEST:
173 switch(alg) {
174 case CSSM_ALGID_SHA1:
175 if(cspCtx == NULL) {
176 /* reuse is OK */
177 cspCtx = new DigestContext(session,
178 *(new SHA1Object));
179 }
180 return true;
181 case CSSM_ALGID_MD5:
182 if(cspCtx == NULL) {
183 /* reuse is OK */
184 cspCtx = new DigestContext(session,
185 *(new MD5Object));
186 }
187 return true;
188 case CSSM_ALGID_MD2:
189 if(cspCtx == NULL) {
190 /* reuse is OK */
191 cspCtx = new DigestContext(session,
192 *(new MD2Object));
193 }
194 return true;
195 case CSSM_ALGID_SHA224:
196 if(cspCtx == NULL) {
197 /* reuse is OK */
198 cspCtx = new DigestContext(session,
199 *(new SHA224Object));
200 }
201 return true;
202 case CSSM_ALGID_SHA256:
203 if(cspCtx == NULL) {
204 /* reuse is OK */
205 cspCtx = new DigestContext(session,
206 *(new SHA256Object));
207 }
208 return true;
209 case CSSM_ALGID_SHA384:
210 if(cspCtx == NULL) {
211 /* reuse is OK */
212 cspCtx = new DigestContext(session,
213 *(new SHA384Object));
214 }
215 return true;
216 case CSSM_ALGID_SHA512:
217 if(cspCtx == NULL) {
218 /* reuse is OK */
219 cspCtx = new DigestContext(session,
220 *(new SHA512Object));
221 }
222 return true;
223 default:
224 break; // not our digest alg
225 } // switch digest alg
226 break; // from case CSSM_ALGCLASS_DIGEST
227
228 case CSSM_ALGCLASS_KEYGEN:
229 switch(alg) {
230 case CSSM_ALGID_AES:
231 if(cspCtx == NULL) {
232 cspCtx = new AESKeyGenContext(session);
233 }
234 return true;
235
236 #if MAF_DES_ENABLE
237 case CSSM_ALGID_DES:
238 if(cspCtx == NULL) {
239 cspCtx = new AppleSymmKeyGenerator(session,
240 DES_KEY_SIZE_BITS_EXTERNAL,
241 DES_KEY_SIZE_BITS_EXTERNAL,
242 true); // must be byte size
243 }
244 return true;
245 #endif /* MAF_DES_ENABLE */
246
247 #if MAF_DES3_ENABLE
248 case CSSM_ALGID_3DES_3KEY_EDE:
249 if(cspCtx == NULL) {
250 cspCtx = new AppleSymmKeyGenerator(session,
251 DES3_KEY_SIZE_BYTES * 8,
252 DES3_KEY_SIZE_BYTES * 8,
253 true); // must be byte size
254 }
255 return true;
256 #endif
257
258 #if MAF_RC2_ENABLE
259 case CSSM_ALGID_RC2:
260 if(cspCtx == NULL) {
261 cspCtx = new AppleSymmKeyGenerator(session,
262 RC2_MIN_KEY_SIZE_BYTES * 8,
263 RC2_MAX_KEY_SIZE_BYTES * 8,
264 true); // must be byte size
265 }
266 return true;
267 #endif
268
269 #if MAF_RC4_ENABLE
270 case CSSM_ALGID_RC4:
271 if(cspCtx == NULL) {
272 cspCtx = new AppleSymmKeyGenerator(session,
273 kCCKeySizeMinRC4 * 8,
274 kCCKeySizeMaxRC4 * 8,
275 true); // must be byte size
276 }
277 return true;
278 #endif
279
280 #if MAF_RC5_ENABLE
281 case CSSM_ALGID_RC5:
282 if(cspCtx == NULL) {
283 cspCtx = new AppleSymmKeyGenerator(session,
284 RC5_MIN_KEY_SIZE_BYTES * 8,
285 RC5_MAX_KEY_SIZE_BYTES * 8,
286 true); // must be byte size
287 }
288 return true;
289 #endif
290
291 case CSSM_ALGID_BLOWFISH:
292 if(cspCtx == NULL) {
293 cspCtx = new AppleSymmKeyGenerator(session,
294 BF_MIN_KEY_SIZE_BYTES * 8,
295 BF_MAX_KEY_SIZE_BYTES * 8,
296 true); // must be byte size
297 }
298 return true;
299
300 /* Note we require keys to be ALGID_CAST, not ALGID_CAST5 */
301 case CSSM_ALGID_CAST:
302 if(cspCtx == NULL) {
303 cspCtx = new AppleSymmKeyGenerator(session,
304 kCCKeySizeMinCAST * 8,
305 kCCKeySizeMaxCAST * 8,
306 true); // must be byte size
307 }
308 return true;
309
310 #if MAF_MAC_ENABLE
311 case CSSM_ALGID_SHA1HMAC:
312 if(cspCtx == NULL) {
313 cspCtx = new AppleSymmKeyGenerator(session,
314 HMAC_SHA_MIN_KEY_SIZE * 8,
315 HMAC_MAX_KEY_SIZE * 8,
316 true); // must be byte size
317 }
318 return true;
319 case CSSM_ALGID_MD5HMAC:
320 if(cspCtx == NULL) {
321 cspCtx = new AppleSymmKeyGenerator(session,
322 HMAC_MD5_MIN_KEY_SIZE * 8,
323 HMAC_MAX_KEY_SIZE * 8,
324 true); // must be byte size
325 }
326 return true;
327 #endif
328
329 #if NULL_CRYPT_ENABLE
330 case CSSM_ALGID_NONE:
331 if(cspCtx == NULL) {
332 cspCtx = new AppleSymmKeyGenerator(session,
333 NULL_CRYPT_BLOCK_SIZE * 8,
334 NULL_CRYPT_BLOCK_SIZE * 8,
335 true); // must be byte size
336 }
337 return true;
338 #endif /* NULL_CRYPT_ENABLE */
339
340 default:
341 break; // not our keygen alg
342 } // switch alg for keygen
343 break; // from case CSSM_ALGCLASS_KEYGEN
344
345 case CSSM_ALGCLASS_MAC:
346 switch(alg) {
347 #if MAF_MAC_ENABLE
348 case CSSM_ALGID_SHA1HMAC:
349 case CSSM_ALGID_MD5HMAC:
350 if(cspCtx == NULL) {
351 cspCtx = new MacContext(session, alg);
352 }
353 return true;
354 #endif
355 #if CRYPTKIT_CSP_ENABLE
356 case CSSM_ALGID_SHA1HMAC_LEGACY:
357 if(cspCtx == NULL) {
358 cspCtx = new MacLegacyContext(session, alg);
359 }
360 return true;
361 #endif
362 default:
363 /* not our mac alg */
364 break;
365 }
366 break;
367
368 default:
369 break; // not our context type
370 } // switch context type
371
372 /* not ours */
373 return false;
374 }