diff --git a/aes.c b/aes.c index 1d94297..cf6bd10 100644 --- a/aes.c +++ b/aes.c @@ -21,21 +21,20 @@ void aes128_init(const uint8_t *key) { return; } -void aes128_ecb(uint8_t *dest, uint8_t const *const in) { +uint8_t *aes128_ecb(uint8_t const *const in) { #ifdef HOST_BUILD AES_KEY key; AES_set_encrypt_key(g_ecbdata.key, 128, &key); AES_encrypt(in, g_ecbdata.out, &key); #else - memmove(g_ecbdata.in, in, 16); + memcpy(g_ecbdata.in, in, 16); NRF_ECB->TASKS_STARTECB = 1; while (!NRF_ECB->EVENTS_ENDECB) { ; } NRF_ECB->EVENTS_ENDECB = 0; #endif /* HOST_BUILD */ - memmove(dest, g_ecbdata.out, 16); - return; + return g_ecbdata.out; } #ifdef HOST_BUILD diff --git a/aes.h b/aes.h index 3582b02..952f696 100644 --- a/aes.h +++ b/aes.h @@ -11,5 +11,5 @@ typedef struct { } ecbdata_t; void aes128_init(const uint8_t *); -void aes128_ecb(uint8_t *, uint8_t const *const); +uint8_t *aes128_ecb(uint8_t const *const); void aes_dump_state(void); diff --git a/block.c b/block.c index 44dcce7..9e19cf4 100644 --- a/block.c +++ b/block.c @@ -31,7 +31,7 @@ void block_print_bytes(char const *const label, void const *const v, } #endif -void block_xor(uint8_t *dest, uint8_t *const a, uint8_t *const b) { +void block_xor(uint8_t *dest, uint8_t const *const a, uint8_t const *const b) { for (uint_fast8_t i = 0; i < 4; i++) { *((uint32_t *)dest + i) = *((uint32_t *)a + i) ^ *((uint32_t *)b + i); } diff --git a/block.h b/block.h index 6ca99b7..64bc195 100644 --- a/block.h +++ b/block.h @@ -12,4 +12,4 @@ void block_print(char const *const, void const *const); void block_print_bytes(char const *const, void const *const, uint32_t); void block_shiftl(uint8_t *, uint8_t *const, uint_fast8_t); void block_shiftr(uint8_t *, uint8_t *const, uint_fast8_t); -void block_xor(uint8_t *, uint8_t *const, uint8_t *const); +void block_xor(uint8_t *, uint8_t const *const, uint8_t const *const); diff --git a/cmac.c b/cmac.c index 01adb6e..3982661 100644 --- a/cmac.c +++ b/cmac.c @@ -33,10 +33,9 @@ void cmac_aes128_init(const uint8_t *key) { void cmac_aes128_expand_key(uint8_t const *const key, uint8_t *k1, uint8_t *k2) { /* Generate two required subkeys according to NIST 800-38B */ - uint8_t l[16] = {0}, - Rb[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87}; + uint8_t Rb[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87}; - aes128_ecb(l, zeros); + uint8_t *l = aes128_ecb(zeros); if ((l[0] >> 7) == 0) { block_shiftl(k1, l, 1); } else { @@ -70,7 +69,7 @@ void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, uint8_t alt_msg[num_blocks * 16], *last_block = &alt_msg[(num_blocks - 1) * 16]; memset(alt_msg, 0, num_blocks * 16); - memmove(alt_msg, msg, msg_len); + memcpy(alt_msg, msg, msg_len); if (!last_block_complete) { /* Padding is single 1 bit, run out on 0s.. find the next byte, @@ -81,12 +80,12 @@ void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, block_xor(last_block, last_block, g_k1); } - uint8_t x[16] = {0}, y[16] = {0}; + uint8_t *x = (uint8_t *)zeros, y[16] = {0}; for (uint32_t i = 0; i < num_blocks; i++) { uint8_t *block = &alt_msg[i * 16]; block_xor(y, x, block); - aes128_ecb(x, y); + x = aes128_ecb(y); } cmac_truncate(dest, x, tag_len); return; diff --git a/ctr.c b/ctr.c index 040eab2..32026f2 100644 --- a/ctr.c +++ b/ctr.c @@ -20,15 +20,15 @@ void aes128_ctr_evolve_counter(void) { } void aes128_ctr(uint8_t *dest, uint8_t *msg, uint32_t msg_len) { - uint8_t buffer[16]; + uint8_t *buffer; uint32_t num_blocks = msg_len / 16; for (uint32_t i = 0; i < num_blocks; i++) { - aes128_ecb(buffer, g_counter); + buffer = aes128_ecb(g_counter); aes128_ctr_evolve_counter(); block_xor(dest + (i * 16), msg + (i * 16), buffer); } if (msg_len % 16) { - aes128_ecb(buffer, g_counter); + buffer = aes128_ecb(g_counter); for (uint8_t i = 0; i < msg_len % 16; i++) { dest[num_blocks * 16 + i] = msg[num_blocks * 16 + i] ^ buffer[i]; }