1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

rename aes_ctr_counter_inc to aes_ctr_cbuf_inc and move it to aes_modes.c

This commit is contained in:
Pavol Rusnak 2014-06-07 14:13:05 +02:00
parent 22b0dd2e62
commit b16e36f10e
3 changed files with 17 additions and 15 deletions

2
aes.h
View File

@ -196,6 +196,8 @@ typedef void cbuf_inc(unsigned char *cbuf);
AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
void aes_ctr_cbuf_inc(unsigned char *cbuf);
#endif
#if defined(__cplusplus)

View File

@ -941,6 +941,16 @@ AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
return EXIT_SUCCESS;
}
void aes_ctr_cbuf_inc(unsigned char *cbuf)
{
int i = 15;
while (i >= 0) {
cbuf[i]++;
if (cbuf[i]) return; // if there was no overflow
i--;
}
}
#if defined(__cplusplus)
}
#endif

20
tests.c
View File

@ -437,22 +437,12 @@ START_TEST(test_verify_speed)
}
END_TEST
void aes_ctr_counter_inc(uint8_t *ctr)
{
int i = 15;
while (i >= 0) {
ctr[i]++;
if (ctr[i]) return; // if there was no overflow
i--;
}
}
// test vectors from http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors
START_TEST(test_aes)
{
aes_encrypt_ctx ctxe;
aes_decrypt_ctx ctxd;
uint8_t ibuf[16], obuf[16], iv[16], cntr[16];
uint8_t ibuf[16], obuf[16], iv[16], cbuf[16];
const char **ivp, **plainp, **cipherp;
// ECB
@ -574,22 +564,22 @@ START_TEST(test_aes)
// encrypt
plainp = ctr_vector;
cipherp = ctr_vector + 1;
memcpy(cntr, fromhex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), 16);
memcpy(cbuf, fromhex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), 16);
aes_encrypt_key256(fromhex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), &ctxe);
while (*plainp && *cipherp) {
memcpy(ibuf, fromhex(*plainp), 16);
aes_ctr_encrypt(ibuf, obuf, 16, cntr, aes_ctr_counter_inc, &ctxe);
aes_ctr_encrypt(ibuf, obuf, 16, cbuf, aes_ctr_cbuf_inc, &ctxe);
ck_assert_mem_eq(obuf, fromhex(*cipherp), 16);
plainp += 2; cipherp += 2;
}
// decrypt (uses encryption)
plainp = ctr_vector;
cipherp = ctr_vector + 1;
memcpy(cntr, fromhex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), 16);
memcpy(cbuf, fromhex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), 16);
aes_encrypt_key256(fromhex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"), &ctxe);
while (*plainp && *cipherp) {
memcpy(ibuf, fromhex(*cipherp), 16);
aes_ctr_decrypt(ibuf, obuf, 16, cntr, aes_ctr_counter_inc, &ctxe);
aes_ctr_decrypt(ibuf, obuf, 16, cbuf, aes_ctr_cbuf_inc, &ctxe);
ck_assert_mem_eq(obuf, fromhex(*plainp), 16);
plainp += 2; cipherp += 2;
}