Skip to content

Commit

Permalink
Import |EVP_aes_128/192/256_cfb1/8| APIs. (#595)
Browse files Browse the repository at this point in the history
* Import |EVP_aes_128/192/256_cfb1/8| APIs.

* Add EVP_CIPH_FLAG_LENGTH_BITS.

* Add explicit cast.
  • Loading branch information
bryce-shang authored Aug 30, 2022
1 parent 98cac48 commit c2dba34
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 32 deletions.
107 changes: 105 additions & 2 deletions crypto/decrepit/cfb/cfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,80 @@
#include "../../internal.h"
#include "../../fipsmodule/cipher/internal.h"

// MAXBITCHUNK is used in |aes_cfb1_cipher| to avoid overflow because
// |AES_cfb1_encrypt| operates data on bit level.
#define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))

typedef struct {
AES_KEY ks;
} EVP_CFB_CTX;

static int aes_cfb_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
const uint8_t *iv, int enc) {
if (key) {
EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data;
AES_set_encrypt_key(key, ctx->key_len * 8, &cfb_ctx->ks);
}

return 1;
}

static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
const uint8_t *in, size_t len) {
if (!out || !in) {
return 0;
}

EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data;
if (ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) {
int num = ctx->num;
AES_cfb1_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num,
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
ctx->num = num;
return 1;
}

while (len >= MAXBITCHUNK) {
int num = ctx->num;
AES_cfb1_encrypt(in, out, MAXBITCHUNK * 8, &cfb_ctx->ks, ctx->iv, &num,
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
ctx->num = num;
len -= MAXBITCHUNK;
out += MAXBITCHUNK;
in += MAXBITCHUNK;
}
if (len) {
int num = ctx->num;
AES_cfb1_encrypt(in, out, len * 8, &cfb_ctx->ks, ctx->iv, &num,
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
ctx->num = num;
}

return 1;
}

static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
const uint8_t *in, size_t len) {
if (!out || !in) {
return 0;
}

EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data;
int num = ctx->num;
AES_cfb8_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num,
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
ctx->num = num;

return 1;
}

static int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
const uint8_t *in, size_t len) {
if (!out || !in) {
return 0;
}

EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
EVP_CFB_CTX *cfb_ctx = (EVP_CFB_CTX *)ctx->cipher_data;
int num = ctx->num;
AES_cfb128_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num,
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
Expand All @@ -51,30 +104,80 @@ static int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
return 1;
}

static const EVP_CIPHER aes_128_cfb1 = {
NID_aes_128_cfb1, 1 /* block_size */, 16 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb1_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_128_cfb8 = {
NID_aes_128_cfb8, 1 /* block_size */, 16 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb8_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_128_cfb128 = {
NID_aes_128_cfb128, 1 /* block_size */, 16 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_192_cfb1 = {
NID_aes_192_cfb1, 1 /* block_size */, 24 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb1_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_192_cfb8 = {
NID_aes_192_cfb8, 1 /* block_size */, 24 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb8_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_192_cfb128 = {
NID_aes_192_cfb128, 1 /* block_size */, 24 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_256_cfb1 = {
NID_aes_256_cfb1, 1 /* block_size */, 32 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb1_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_256_cfb8 = {
NID_aes_256_cfb8, 1 /* block_size */, 32 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb8_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

static const EVP_CIPHER aes_256_cfb128 = {
NID_aes_256_cfb128, 1 /* block_size */, 32 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};

const EVP_CIPHER *EVP_aes_128_cfb1(void) { return &aes_128_cfb1; }
const EVP_CIPHER *EVP_aes_128_cfb8(void) { return &aes_128_cfb8; }
const EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; }
const EVP_CIPHER *EVP_aes_128_cfb(void) { return &aes_128_cfb128; }

const EVP_CIPHER *EVP_aes_192_cfb1(void) { return &aes_192_cfb1; }
const EVP_CIPHER *EVP_aes_192_cfb8(void) { return &aes_192_cfb8; }
const EVP_CIPHER *EVP_aes_192_cfb128(void) { return &aes_192_cfb128; }
const EVP_CIPHER *EVP_aes_192_cfb(void) { return &aes_192_cfb128; }

const EVP_CIPHER *EVP_aes_256_cfb1(void) { return &aes_256_cfb1; }
const EVP_CIPHER *EVP_aes_256_cfb8(void) { return &aes_256_cfb8; }
const EVP_CIPHER *EVP_aes_256_cfb128(void) { return &aes_256_cfb128; }
const EVP_CIPHER *EVP_aes_256_cfb(void) { return &aes_256_cfb128; }
108 changes: 78 additions & 30 deletions crypto/decrepit/cfb/cfb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,86 @@
#include "../../test/test_util.h"

struct CFBTestCase {
size_t key_len;
const EVP_CIPHER *evp_cipher;
uint8_t key[32];
uint8_t iv[16];
uint8_t plaintext[16*4];
uint8_t ciphertext[16*4];
std::vector<uint8_t> plaintext;
std::vector<uint8_t> ciphertext;
};

static const CFBTestCase kCFBTestCases[] = {
// CFB1
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.1, for CFB1-AES128
EVP_aes_128_cfb1(),
{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1},
{0x68, 0xb3},
},
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.3, CFB1-AES192
EVP_aes_192_cfb1(),
{0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1},
{0x93, 0x59},
},
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.5, CFB1-AES256
EVP_aes_256_cfb1(),
{0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1},
{0x90, 0x29},
},
// CFB8
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.7, for CFB8-AES128
EVP_aes_128_cfb8(),
{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d},
{0x3b, 0x79, 0x42, 0x4c, 0x9c, 0x0d, 0xd4, 0x36, 0xba, 0xce, 0x9e, 0x0e, 0xd4, 0x58, 0x6a, 0x4f, 0x32, 0xb9},
},
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.9, CFB8-AES192
EVP_aes_192_cfb8(),
{0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d},
{0xcd, 0xa2, 0x52, 0x1e, 0xf0, 0xa9, 0x05, 0xca, 0x44, 0xcd, 0x05, 0x7c, 0xbf, 0x0d, 0x47, 0xa0, 0x67, 0x8a},
},
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.11, CFB8-AES256
EVP_aes_256_cfb8(),
{0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d},
{0xdc, 0x1f, 0x1a, 0x85, 0x20, 0xa6, 0x4d, 0xb5, 0x5f, 0xcc, 0x8a, 0xc5, 0x54, 0x84, 0x4e, 0x88, 0x97, 0x00},
},
// CFB/CFB128
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.13, for CFB128-AES128
16,
EVP_aes_128_cfb128(),
{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
Expand All @@ -48,7 +115,7 @@ static const CFBTestCase kCFBTestCases[] = {
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.15, CFB128-AES192
24,
EVP_aes_192_cfb128(),
{0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
Expand All @@ -65,7 +132,7 @@ static const CFBTestCase kCFBTestCases[] = {
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.17, CFB128-AES256
32,
EVP_aes_256_cfb128(),
{0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
Expand All @@ -86,22 +153,13 @@ TEST(CFBTest, TestVectors) {
test_num++;
SCOPED_TRACE(test_num);

const size_t input_len = sizeof(test.plaintext);
const size_t input_len = test.plaintext.size();
const EVP_CIPHER *evp_cipher = test.evp_cipher;
std::unique_ptr<uint8_t[]> out(new uint8_t[input_len]);

for (size_t stride = 1; stride <= input_len; stride++) {
bssl::ScopedEVP_CIPHER_CTX ctx;
if (test.key_len == 16) {
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cfb128(), nullptr,
test.key, test.iv));
} else if (test.key_len == 24) {
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_aes_192_cfb128(), nullptr,
test.key, test.iv));
} else {
assert(test.key_len == 32);
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cfb128(), nullptr,
test.key, test.iv));
}
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), evp_cipher, nullptr, test.key, test.iv));

size_t done = 0;
while (done < input_len) {
Expand All @@ -112,7 +170,7 @@ TEST(CFBTest, TestVectors) {

int out_bytes;
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out.get() + done, &out_bytes,
test.plaintext + done, todo));
test.plaintext.data() + done, todo));
ASSERT_EQ(static_cast<size_t>(out_bytes), todo);

done += todo;
Expand All @@ -122,17 +180,7 @@ TEST(CFBTest, TestVectors) {
}

bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
if (test.key_len == 16) {
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_aes_128_cfb128(),
nullptr, test.key, test.iv));
} else if (test.key_len == 24) {
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_aes_192_cfb128(),
nullptr, test.key, test.iv));
} else {
assert(test.key_len == 32);
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_aes_256_cfb128(),
nullptr, test.key, test.iv));
}
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), evp_cipher, nullptr, test.key, test.iv));

std::unique_ptr<uint8_t[]> plaintext(new uint8_t[input_len]);
int num_bytes;
Expand Down
17 changes: 17 additions & 0 deletions crypto/fipsmodule/aes/mode_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

#include "../aes/internal.h"
#include "../modes/internal.h"
#include "../cipher/internal.h"

void AES_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
const AES_KEY *key, uint8_t ivec[AES_BLOCK_SIZE],
Expand Down Expand Up @@ -113,6 +114,22 @@ void AES_ofb128_encrypt(const uint8_t *in, uint8_t *out, size_t length,
*num = (int)num_u;
}

void AES_cfb1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
const AES_KEY *key, uint8_t *ivec, int *num,
int enc) {
unsigned num_u = (unsigned)(*num);
CRYPTO_cfb128_1_encrypt(in, out, bits, key, ivec, &num_u, enc, AES_encrypt);
*num = (int)num_u;
}

void AES_cfb8_encrypt(const uint8_t *in, uint8_t *out, size_t length,
const AES_KEY *key, uint8_t *ivec, int *num,
int enc) {
unsigned num_u = (unsigned)(*num);
CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, &num_u, enc, AES_encrypt);
*num = (int)num_u;
}

void AES_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t length,
const AES_KEY *key, uint8_t *ivec, int *num,
int enc) {
Expand Down
10 changes: 10 additions & 0 deletions crypto/fipsmodule/cipher/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key,
block128_f *out_block, const uint8_t *key,
size_t key_bytes);

// AES_cfb1_encrypt calls |CRYPTO_cfb128_1_encrypt| using the block |AES_encrypt|.
void AES_cfb1_encrypt(const uint8_t *in, uint8_t *out,
size_t bits, const AES_KEY *key,
uint8_t *ivec, int *num, int enc);

// AES_cfb8_encrypt calls |CRYPTO_cfb128_8_encrypt| using the block |AES_encrypt|.
void AES_cfb8_encrypt(const uint8_t *in, uint8_t *out,
size_t len, const AES_KEY *key,
uint8_t *ivec, int *num, int enc);

#if defined(__cplusplus)
} // extern C
#endif
Expand Down
Loading

0 comments on commit c2dba34

Please sign in to comment.