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