add cfb8-aes modes

This commit is contained in:
Love Hornquist Astrand
2010-06-03 17:20:40 -07:00
parent efa5a314a8
commit 91020dd11d
10 changed files with 360 additions and 9 deletions

View File

@@ -83,6 +83,44 @@ cc_do_cipher(EVP_CIPHER_CTX *ctx,
return 1;
}
static int
cc_do_cfb8_cipher(EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int size)
{
struct cc_key *cc = ctx->cipher_data;
CCCryptorStatus ret;
size_t moved;
unsigned int i;
for (i = 0; i < size; i++) {
unsigned char oiv[EVP_MAX_IV_LENGTH + 1];
assert(ctx->cipher->iv_len + 1 <= sizeof(oiv));
memcpy(oiv, ctx->iv, ctx->cipher->iv_len);
ret = CCCryptorUpdate(cc->href, ctx->iv, ctx->cipher->iv_len,
ctx->iv, ctx->cipher->iv_len, &moved);
if (ret)
return 0;
if (moved != ctx->cipher->iv_len)
return 0;
if (!ctx->encrypt)
oiv[ctx->cipher->iv_len] = in[i];
out[i] = in[i] ^ ctx->iv[0];
if (ctx->encrypt)
oiv[ctx->cipher->iv_len] = out[i];
memcpy(ctx->iv, &oiv[1], ctx->cipher->iv_len);
}
return 1;
}
static int
cc_cleanup(EVP_CIPHER_CTX *ctx)
{
@@ -93,7 +131,7 @@ cc_cleanup(EVP_CIPHER_CTX *ctx)
}
static int
init_cc_key(int encp, CCAlgorithm alg, const void *key,
init_cc_key(int encp, CCAlgorithm alg, CCOptions opts, const void *key,
size_t keylen, const void *iv, CCCryptorRef *ref)
{
CCOperation op = encp ? kCCEncrypt : kCCDecrypt;
@@ -107,7 +145,7 @@ init_cc_key(int encp, CCAlgorithm alg, const void *key,
CCCryptorRelease(*ref);
}
ret = CCCryptorCreate(op, alg, 0, key, keylen, iv, ref);
ret = CCCryptorCreate(op, alg, opts, key, keylen, iv, ref);
if (ret)
return 0;
return 1;
@@ -120,7 +158,7 @@ cc_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx,
int encp)
{
struct cc_key *cc = ctx->cipher_data;
return init_cc_key(encp, kCCAlgorithm3DES, key, kCCKeySize3DES, iv, &cc->href);
return init_cc_key(encp, kCCAlgorithm3DES, 0, key, kCCKeySize3DES, iv, &cc->href);
}
/**
@@ -163,7 +201,7 @@ cc_des_cbc_init(EVP_CIPHER_CTX *ctx,
int encp)
{
struct cc_key *cc = ctx->cipher_data;
return init_cc_key(encp, kCCAlgorithmDES, key, kCCBlockSizeDES, iv, &cc->href);
return init_cc_key(encp, kCCAlgorithmDES, 0, key, kCCBlockSizeDES, iv, &cc->href);
}
/**
@@ -206,7 +244,7 @@ cc_aes_cbc_init(EVP_CIPHER_CTX *ctx,
int encp)
{
struct cc_key *cc = ctx->cipher_data;
return init_cc_key(encp, kCCAlgorithmAES128, key, ctx->cipher->key_len, iv, &cc->href);
return init_cc_key(encp, kCCAlgorithmAES128, 0, key, ctx->cipher->key_len, iv, &cc->href);
}
/**
@@ -296,6 +334,104 @@ EVP_cc_aes_256_cbc(void)
return &c;
}
static int
cc_aes_cfb8_init(EVP_CIPHER_CTX *ctx,
const unsigned char * key,
const unsigned char * iv,
int encp)
{
struct cc_key *cc = ctx->cipher_data;
return init_cc_key(1, kCCAlgorithmAES128, kCCOptionECBMode,
key, ctx->cipher->key_len, NULL, &cc->href);
}
/**
* The AES-128 CFB8 cipher type (Apple CommonCrypto provider)
*
* @return the AES-128-CFB8 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_cc_aes_128_cfb8(void)
{
static const EVP_CIPHER c = {
0,
1,
kCCKeySizeAES128,
kCCBlockSizeAES128,
EVP_CIPH_CFB8_MODE,
cc_aes_cfb8_init,
cc_do_cfb8_cipher,
cc_cleanup,
sizeof(struct cc_key),
NULL,
NULL,
NULL,
NULL
};
return &c;
}
/**
* The AES-192 CFB8 cipher type (Apple CommonCrypto provider)
*
* @return the AES-192-CFB8 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_cc_aes_192_cfb8(void)
{
static const EVP_CIPHER c = {
0,
1,
kCCKeySizeAES192,
kCCBlockSizeAES128,
EVP_CIPH_CFB8_MODE,
cc_aes_cfb8_init,
cc_do_cfb8_cipher,
cc_cleanup,
sizeof(struct cc_key),
NULL,
NULL,
NULL,
NULL
};
return &c;
}
/**
* The AES-256 CFB8 cipher type (Apple CommonCrypto provider)
*
* @return the AES-256-CFB8 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_cc_aes_256_cfb8(void)
{
static const EVP_CIPHER c = {
0,
1,
kCCKeySizeAES256,
kCCBlockSizeAES128,
EVP_CIPH_CFB8_MODE,
cc_aes_cfb8_init,
cc_do_cfb8_cipher,
cc_cleanup,
sizeof(struct cc_key),
NULL,
NULL,
NULL,
NULL
};
return &c;
}
/*
*
*/
@@ -308,7 +444,7 @@ cc_rc2_cbc_init(EVP_CIPHER_CTX *ctx,
int encp)
{
struct cc_key *cc = ctx->cipher_data;
return init_cc_key(encp, kCCAlgorithmRC2, key, ctx->cipher->key_len, iv, &cc->href);
return init_cc_key(encp, kCCAlgorithmRC2, 0, key, ctx->cipher->key_len, iv, &cc->href);
}
#endif
@@ -593,7 +729,7 @@ cc_rc4_init(EVP_CIPHER_CTX *ctx,
int encp)
{
struct cc_key *cc = ctx->cipher_data;
return init_cc_key(encp, kCCAlgorithmRC4, key, ctx->key_len, iv, &cc->href);
return init_cc_key(encp, kCCAlgorithmRC4, 0, key, ctx->key_len, iv, &cc->href);
}
/**