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

@@ -119,3 +119,26 @@ AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
}
}
}
void
AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
unsigned long size, const AES_KEY *key,
unsigned char *iv, int forward_encrypt)
{
int i;
for (i = 0; i < size; i++) {
unsigned char tmp[AES_BLOCK_SIZE + 1];
memcpy(tmp, iv, AES_BLOCK_SIZE);
AES_encrypt(iv, iv, key);
if (!forward_encrypt) {
tmp[AES_BLOCK_SIZE] = in[i];
}
out[i] = in[i] ^ iv[0];
if (forward_encrypt) {
tmp[AES_BLOCK_SIZE] = out[i];
}
memcpy(iv, &tmp[1], AES_BLOCK_SIZE);
}
}

View File

@@ -42,6 +42,7 @@
#define AES_encrypt hc_AES_encrypt
#define AES_decrypt hc_AES_decrypt
#define AES_cbc_encrypt hc_AES_cbc_encrypt
#define AES_cfb8_encrypt hc_AES_cfb8_encrypt
/*
*
@@ -71,6 +72,10 @@ void AES_decrypt(const unsigned char *, unsigned char *, const AES_KEY *);
void AES_cbc_encrypt(const unsigned char *, unsigned char *,
unsigned long, const AES_KEY *,
unsigned char *, int);
void AES_cfb8_encrypt(const unsigned char *, unsigned char *,
unsigned long, const AES_KEY *,
unsigned char *, int);
#ifdef __cplusplus
}

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);
}
/**

View File

@@ -47,6 +47,9 @@
#define EVP_cc_aes_128_cbc hc_EVP_cc_aes_128_cbc
#define EVP_cc_aes_192_cbc hc_EVP_cc_aes_192_cbc
#define EVP_cc_aes_256_cbc hc_EVP_cc_aes_256_cbc
#define EVP_cc_aes_cfb_128_cbc hc_EVP_cc_aes_128_cfb8
#define EVP_cc_aes_cfb_192_cbc hc_EVP_cc_aes_192_cfb8
#define EVP_cc_aes_cfb_256_cbc hc_EVP_cc_aes_256_cfb8
#define EVP_cc_rc4 hc_EVP_cc_rc4
#define EVP_cc_rc4_40 hc_EVP_cc_rc4_40
#define EVP_cc_rc2_40_cbc hc_EVP_cc_rc2_40_cbc
@@ -82,6 +85,10 @@ const EVP_CIPHER * EVP_cc_aes_128_cbc(void);
const EVP_CIPHER * EVP_cc_aes_192_cbc(void);
const EVP_CIPHER * EVP_cc_aes_256_cbc(void);
const EVP_CIPHER * EVP_cc_aes_128_cfb8(void);
const EVP_CIPHER * EVP_cc_aes_192_cfb8(void);
const EVP_CIPHER * EVP_cc_aes_256_cfb8(void);
const EVP_CIPHER * EVP_cc_camellia_128_cbc(void);
const EVP_CIPHER * EVP_cc_camellia_192_cbc(void);
const EVP_CIPHER * EVP_cc_camellia_256_cbc(void);

View File

@@ -83,7 +83,10 @@ aes_do_cipher(EVP_CIPHER_CTX *ctx,
unsigned int size)
{
AES_KEY *k = ctx->cipher_data;
AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
if (ctx->flags & EVP_CIPH_CFB8_MODE)
AES_cfb8_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
else
AES_cbc_encrypt(in, out, size, k, ctx->iv, ctx->encrypt);
return 1;
}
@@ -175,6 +178,94 @@ EVP_hcrypto_aes_256_cbc(void)
return &aes_256_cbc;
}
/**
* The AES-128 CFB8 cipher type (hcrypto)
*
* @return the AES-128 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_hcrypto_aes_128_cfb8(void)
{
static const EVP_CIPHER aes_128_cfb8 = {
0,
1,
16,
16,
EVP_CIPH_CFB8_MODE,
aes_init,
aes_do_cipher,
NULL,
sizeof(AES_KEY),
NULL,
NULL,
NULL,
NULL
};
return &aes_128_cfb8;
}
/**
* The AES-192 CFB8 cipher type (hcrypto)
*
* @return the AES-192 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_hcrypto_aes_192_cfb8(void)
{
static const EVP_CIPHER aes_192_cfb8 = {
0,
1,
24,
16,
EVP_CIPH_CFB8_MODE,
aes_init,
aes_do_cipher,
NULL,
sizeof(AES_KEY),
NULL,
NULL,
NULL,
NULL
};
return &aes_192_cfb8;
}
/**
* The AES-256 CFB8 cipher type (hcrypto)
*
* @return the AES-256 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_hcrypto_aes_256_cfb8(void)
{
static const EVP_CIPHER aes_256_cfb8 = {
0,
1,
32,
16,
EVP_CIPH_CFB8_MODE,
aes_init,
aes_do_cipher,
NULL,
sizeof(AES_KEY),
NULL,
NULL,
NULL,
NULL
};
return &aes_256_cfb8;
}
/**
* The message digest SHA256 - hcrypto
*

View File

@@ -47,6 +47,9 @@
#define EVP_hcrypto_aes_128_cbc hc_EVP_hcrypto_aes_128_cbc
#define EVP_hcrypto_aes_192_cbc hc_EVP_hcrypto_aes_192_cbc
#define EVP_hcrypto_aes_256_cbc hc_EVP_hcrypto_aes_256_cbc
#define EVP_hcrypto_aes_128_cfb8 hc_EVP_hcrypto_aes_128_cfb8
#define EVP_hcrypto_aes_192_cfb8 hc_EVP_hcrypto_aes_192_cfb8
#define EVP_hcrypto_aes_256_cfb8 hc_EVP_hcrypto_aes_256_cfb8
#define EVP_hcrypto_rc4 hc_EVP_hcrypto_rc4
#define EVP_hcrypto_rc4_40 hc_EVP_hcrypto_rc4_40
#define EVP_hcrypto_rc2_40_cbc hc_EVP_hcrypto_rc2_40_cbc
@@ -82,6 +85,10 @@ const EVP_CIPHER * EVP_hcrypto_aes_128_cbc(void);
const EVP_CIPHER * EVP_hcrypto_aes_192_cbc(void);
const EVP_CIPHER * EVP_hcrypto_aes_256_cbc(void);
const EVP_CIPHER * EVP_hcrypto_aes_128_cfb8(void);
const EVP_CIPHER * EVP_hcrypto_aes_192_cfb8(void);
const EVP_CIPHER * EVP_hcrypto_aes_256_cfb8(void);
const EVP_CIPHER * EVP_hcrypto_camellia_128_cbc(void);
const EVP_CIPHER * EVP_hcrypto_camellia_192_cbc(void);
const EVP_CIPHER * EVP_hcrypto_camellia_256_cbc(void);

View File

@@ -768,6 +768,10 @@ EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
case EVP_CIPH_STREAM_CIPHER:
break;
case EVP_CIPH_CFB8_MODE:
if (iv)
memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
break;
default:
return 0;
@@ -1125,6 +1129,48 @@ EVP_aes_256_cbc(void)
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
}
/**
* The AES-128 cipher type
*
* @return the AES-128 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_128_cfb8(void)
{
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
}
/**
* The AES-192 cipher type
*
* @return the AES-192 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_192_cfb8(void)
{
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
}
/**
* The AES-256 cipher type
*
* @return the AES-256 EVP_CIPHER pointer.
*
* @ingroup hcrypto_evp
*/
const EVP_CIPHER *
EVP_aes_256_cfb8(void)
{
return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
}
/**
* The Camellia-128 cipher type
*
@@ -1179,6 +1225,9 @@ static const struct cipher_name {
{ "aes-128-cbc", EVP_aes_128_cbc },
{ "aes-192-cbc", EVP_aes_192_cbc },
{ "aes-256-cbc", EVP_aes_256_cbc },
{ "aes-128-cfb8", EVP_aes_128_cfb8 },
{ "aes-192-cfb8", EVP_aes_192_cfb8 },
{ "aes-256-cfb8", EVP_aes_256_cfb8 },
{ "camellia-128-cbc", EVP_camellia_128_cbc },
{ "camellia-192-cbc", EVP_camellia_192_cbc },
{ "camellia-256-cbc", EVP_camellia_256_cbc }

View File

@@ -74,6 +74,9 @@
#define EVP_aes_128_cbc hc_EVP_aes_128_cbc
#define EVP_aes_192_cbc hc_EVP_aes_192_cbc
#define EVP_aes_256_cbc hc_EVP_aes_256_cbc
#define EVP_aes_128_cfb8 hc_EVP_aes_128_cfb8
#define EVP_aes_192_cfb8 hc_EVP_aes_192_cfb8
#define EVP_aes_256_cfb8 hc_EVP_aes_256_cfb8
#define EVP_des_cbc hc_EVP_des_cbc
#define EVP_des_ede3_cbc hc_EVP_des_ede3_cbc
@@ -129,6 +132,7 @@ struct hc_CIPHER {
*/
#define EVP_CIPH_STREAM_CIPHER 0
#define EVP_CIPH_CBC_MODE 2
#define EVP_CIPH_CFB8_MODE 4
#define EVP_CIPH_MODE 0x7
#define EVP_CIPH_VARIABLE_LENGTH 0x008 /* variable key length */
@@ -224,6 +228,9 @@ const EVP_MD *EVP_sha256(void);
const EVP_CIPHER * EVP_aes_128_cbc(void);
const EVP_CIPHER * EVP_aes_192_cbc(void);
const EVP_CIPHER * EVP_aes_256_cbc(void);
const EVP_CIPHER * EVP_aes_128_cfb8(void);
const EVP_CIPHER * EVP_aes_192_cfb8(void);
const EVP_CIPHER * EVP_aes_256_cfb8(void);
HC_DEPRECATED_CRYPTO const EVP_CIPHER * EVP_des_cbc(void);
const EVP_CIPHER * EVP_des_ede3_cbc(void);
const EVP_CIPHER * EVP_enc_null(void);

View File

@@ -72,6 +72,17 @@ struct tests aes_tests[] = {
}
};
struct tests aes_cfb_tests[] = {
{ "aes-cfb8-128",
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
16,
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
16,
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
"\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b\x88\x4c\xfa\x59\xca\x34\x2b\x2e"
}
};
struct tests rc2_40_tests[] = {
{ "rc2-40",
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
@@ -320,6 +331,9 @@ main(int argc, char **argv)
/* hcrypto */
for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++)
ret += test_cipher(i, EVP_hcrypto_aes_256_cbc(), &aes_tests[i]);
for (i = 0; i < sizeof(aes_cfb_tests)/sizeof(aes_cfb_tests[0]); i++)
ret += test_cipher(i, EVP_hcrypto_aes_128_cfb8(), &aes_cfb_tests[i]);
for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++)
ret += test_cipher(i, EVP_hcrypto_rc2_40_cbc(), &rc2_40_tests[i]);
for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++)
@@ -334,6 +348,10 @@ main(int argc, char **argv)
#ifdef __APPLE__
for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++)
ret += test_cipher(i, EVP_cc_aes_256_cbc(), &aes_tests[i]);
#if 0
for (i = 0; i < sizeof(aes_cfb_tests)/sizeof(aes_cfb_tests[0]); i++)
ret += test_cipher(i, EVP_cc_aes_128_cfb8(), &aes_cfb_tests[i]);
#endif
for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++)
ret += test_cipher(i, EVP_cc_rc2_40_cbc(), &rc2_40_tests[i]);
for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++)

View File

@@ -144,6 +144,9 @@ HEIMDAL_CRYPTO_1.0 {
hc_EVP_aes_128_cbc;
hc_EVP_aes_192_cbc;
hc_EVP_aes_256_cbc;
hc_EVP_aes_128_cfb8;
hc_EVP_aes_192_cfb8;
hc_EVP_aes_256_cfb8;
hc_EVP_des_cbc;
hc_EVP_des_ede3_cbc;
hc_EVP_camellia_128_cbc;
@@ -173,6 +176,9 @@ HEIMDAL_CRYPTO_1.0 {
hc_EVP_cc_aes_128_cbc;
hc_EVP_cc_aes_192_cbc;
hc_EVP_cc_aes_256_cbc;
hc_EVP_cc_aes_128_cfb8;
hc_EVP_cc_aes_192_cfb8;
hc_EVP_cc_aes_256_cfb8;
hc_EVP_hcrypto_md2;
hc_EVP_hcrypto_md4;
@@ -183,13 +189,15 @@ HEIMDAL_CRYPTO_1.0 {
hc_EVP_hcrypto_aes_128_cbc;
hc_EVP_hcrypto_aes_192_cbc;
hc_EVP_hcrypto_aes_256_cbc;
hc_EVP_hcrypto_aes_128_cfb8;
hc_EVP_hcrypto_aes_192_cfb8;
hc_EVP_hcrypto_aes_256_cfb8;
hc_EVP_hcrypto_rc4;
hc_EVP_hcrypto_rc4_40;
hc_EVP_hcrypto_aes_128_cts;
hc_EVP_hcrypto_aes_192_cts;
hc_EVP_hcrypto_aes_256_cts;
hc_HMAC;
hc_HMAC_CTX_cleanup;
hc_HMAC_CTX_init;