miscAlgFactory.cpp [plain text]
#include "miscAlgFactory.h"
#include <aescspi.h>
#include <gladmanContext.h>
#include "desContext.h"
#include "rc2Context.h"
#include "rc4Context.h"
#include "rc5Context.h"
#include "MacContext.h"
#include "DigestContext.h"
#include "SHA1_MD5_Object.h"
#include "SHA2_Object.h"
#include "MD2Object.h"
#include "NullCryptor.h"
#include "bfContext.h"
#include "castContext.h"
#include <Security/cssmapple.h>
#define MAF_DES_ENABLE 1
#define MAF_DES3_ENABLE 1
#define MAF_RC2_ENABLE 1
#define MAF_RC4_ENABLE 1
#define MAF_RC5_ENABLE 1
#define MAF_MAC_ENABLE 1
#if (!MAF_DES_ENABLE || !MAF_DES3_ENABLE || !MAF_RC2_ENABLE || !MAF_RC4_ENABLE || \
!MAF_RC5_ENABLE || !MAF_MAC_ENABLE)
#warning Internal DES/RC2/RC4/RC5/Mac implementation disabled!
#endif
bool MiscAlgFactory::setup(
AppleCSPSession &session,
CSPFullPluginSession::CSPContext * &cspCtx,
const Context &context)
{
CSSM_CONTEXT_TYPE ctype = context.type();
CSSM_ALGORITHMS alg = context.algorithm();
switch(ctype) {
case CSSM_ALGCLASS_SYMMETRIC:
switch(alg) {
case CSSM_ALGID_AES:
if(cspCtx == NULL) {
uint32 blockSize = context.getInt(CSSM_ATTRIBUTE_BLOCK_SIZE);
if(blockSize == 0) {
blockSize = GLADMAN_BLOCK_SIZE_BYTES;
}
if(GLADMAN_AES_128_ENABLE &&
(blockSize == GLADMAN_BLOCK_SIZE_BYTES)) {
cspCtx = new GAESContext(session);
}
else {
cspCtx = new AESContext(session);
}
}
return true;
#if MAF_DES_ENABLE
case CSSM_ALGID_DES:
if(cspCtx == NULL) {
cspCtx = new DESContext(session);
}
return true;
#endif
#if MAF_DES3_ENABLE
case CSSM_ALGID_3DES_3KEY_EDE:
if(cspCtx == NULL) {
cspCtx = new DES3Context(session);
}
return true;
#endif
#if MAF_RC2_ENABLE
case CSSM_ALGID_RC2:
if(cspCtx == NULL) {
cspCtx = new RC2Context(session);
}
return true;
#endif
#if MAF_RC4_ENABLE
case CSSM_ALGID_RC4:
if(cspCtx == NULL) {
cspCtx = new RC4Context(session);
}
return true;
#endif
#if MAF_RC5_ENABLE
case CSSM_ALGID_RC5:
if(cspCtx == NULL) {
cspCtx = new RC5Context(session);
}
return true;
#endif
case CSSM_ALGID_BLOWFISH:
if(cspCtx == NULL) {
cspCtx = new BlowfishContext(session);
}
return true;
case CSSM_ALGID_CAST:
case CSSM_ALGID_CAST5:
if(cspCtx == NULL) {
cspCtx = new CastContext(session);
}
return true;
#if NULL_CRYPT_ENABLE
case CSSM_ALGID_NONE:
if(cspCtx == NULL) {
cspCtx = new NullCryptor(session);
}
return true;
#endif
default:
break; } break;
case CSSM_ALGCLASS_DIGEST:
switch(alg) {
case CSSM_ALGID_SHA1:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new SHA1Object));
}
return true;
case CSSM_ALGID_MD5:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new MD5Object));
}
return true;
case CSSM_ALGID_MD2:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new MD2Object));
}
return true;
case CSSM_ALGID_SHA224:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new SHA224Object));
}
return true;
case CSSM_ALGID_SHA256:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new SHA256Object));
}
return true;
case CSSM_ALGID_SHA384:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new SHA384Object));
}
return true;
case CSSM_ALGID_SHA512:
if(cspCtx == NULL) {
cspCtx = new DigestContext(session,
*(new SHA512Object));
}
return true;
default:
break; } break;
case CSSM_ALGCLASS_KEYGEN:
switch(alg) {
case CSSM_ALGID_AES:
if(cspCtx == NULL) {
cspCtx = new AESKeyGenContext(session);
}
return true;
#if MAF_DES_ENABLE
case CSSM_ALGID_DES:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
DES_KEY_SIZE_BITS_EXTERNAL,
DES_KEY_SIZE_BITS_EXTERNAL,
true); }
return true;
#endif
#if MAF_DES3_ENABLE
case CSSM_ALGID_3DES_3KEY_EDE:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
DES3_KEY_SIZE_BYTES * 8,
DES3_KEY_SIZE_BYTES * 8,
true); }
return true;
#endif
#if MAF_RC2_ENABLE
case CSSM_ALGID_RC2:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
RC2_MIN_KEY_SIZE_BYTES * 8,
RC2_MAX_KEY_SIZE_BYTES * 8,
true); }
return true;
#endif
#if MAF_RC4_ENABLE
case CSSM_ALGID_RC4:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
kCCKeySizeMinRC4 * 8,
kCCKeySizeMaxRC4 * 8,
true); }
return true;
#endif
#if MAF_RC5_ENABLE
case CSSM_ALGID_RC5:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
RC5_MIN_KEY_SIZE_BYTES * 8,
RC5_MAX_KEY_SIZE_BYTES * 8,
true); }
return true;
#endif
case CSSM_ALGID_BLOWFISH:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
BF_MIN_KEY_SIZE_BYTES * 8,
BF_MAX_KEY_SIZE_BYTES * 8,
true); }
return true;
case CSSM_ALGID_CAST:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
kCCKeySizeMinCAST * 8,
kCCKeySizeMaxCAST * 8,
true); }
return true;
#if MAF_MAC_ENABLE
case CSSM_ALGID_SHA1HMAC:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
HMAC_SHA_MIN_KEY_SIZE * 8,
HMAC_MAX_KEY_SIZE * 8,
true); }
return true;
case CSSM_ALGID_MD5HMAC:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
HMAC_MD5_MIN_KEY_SIZE * 8,
HMAC_MAX_KEY_SIZE * 8,
true); }
return true;
#endif
#if NULL_CRYPT_ENABLE
case CSSM_ALGID_NONE:
if(cspCtx == NULL) {
cspCtx = new AppleSymmKeyGenerator(session,
NULL_CRYPT_BLOCK_SIZE * 8,
NULL_CRYPT_BLOCK_SIZE * 8,
true); }
return true;
#endif
default:
break; } break;
case CSSM_ALGCLASS_MAC:
switch(alg) {
#if MAF_MAC_ENABLE
case CSSM_ALGID_SHA1HMAC:
case CSSM_ALGID_MD5HMAC:
if(cspCtx == NULL) {
cspCtx = new MacContext(session, alg);
}
return true;
#endif
#if CRYPTKIT_CSP_ENABLE
case CSSM_ALGID_SHA1HMAC_LEGACY:
if(cspCtx == NULL) {
cspCtx = new MacLegacyContext(session, alg);
}
return true;
#endif
default:
break;
}
break;
default:
break; }
return false;
}