From 5ce8d1b447f4cfbb16940ba4740d7afa529df833 Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Tue, 7 Jun 2016 12:51:42 -0400 Subject: [PATCH] Auto formatted and tidy'd --- Makefile | 1 + aes.c | 33 +++++----- aes.h | 9 ++- block.c | 135 +++++++++++++++++++------------------- block.h | 11 ++-- cmac.c | 130 ++++++++++++++++++------------------ cmac.h | 2 +- ctr.c | 36 +++++----- ctr.h | 1 - eax.c | 64 +++++++++--------- eax.h | 3 +- main.c | 135 ++++++++++++++++++++------------------ rng.c | 33 +++++----- scripts/cleanup-format.sh | 10 +++ template/Makefile.posix | 8 +-- 15 files changed, 312 insertions(+), 299 deletions(-) create mode 100755 scripts/cleanup-format.sh diff --git a/Makefile b/Makefile index d1e4c03..37828ee 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ SDK_PATH = $(HOME)/devel/nrf-sdk/10.0/ TEMPLATE_PATH = ./template/ +HOST_CC = gcc # Used when running tests on host CFLAGS = -Os -Wall -Werror -flto -g LDFLAGS = -Os -flto -g diff --git a/aes.c b/aes.c index 6b0eb17..11d8d99 100644 --- a/aes.c +++ b/aes.c @@ -12,32 +12,33 @@ static ecbdata_t g_ecbdata; void aes128_init(uint8_t *key) { - if (key != NULL) { - memmove(g_ecbdata.key, key, 16); + if (key != NULL) { + memmove(g_ecbdata.key, key, 16); #ifndef HOST_BUILD - NRF_ECB->ECBDATAPTR = (uint32_t)&g_ecbdata; + NRF_ECB->ECBDATAPTR = (uint32_t)&g_ecbdata; #endif /* HOST_BUILD */ - } - return; + } + return; } -void aes128_ecb(uint8_t *dest, uint8_t const * const in) { +void aes128_ecb(uint8_t *dest, 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); + 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); - NRF_ECB->TASKS_STARTECB = 1; - while (!NRF_ECB->EVENTS_ENDECB); - NRF_ECB->EVENTS_ENDECB = 0; + memmove(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; + memmove(dest, g_ecbdata.out, 16); + return; } #ifdef HOST_BUILD void aes_dump_state(void) { - block_print("Key", g_ecbdata.key); + block_print("Key", g_ecbdata.key); } #endif /* HOST_BUILD */ diff --git a/aes.h b/aes.h index 23a21f6..30fcf56 100644 --- a/aes.h +++ b/aes.h @@ -5,12 +5,11 @@ #include typedef struct { - uint8_t key[16]; - uint8_t in[16]; - uint8_t out[16]; + uint8_t key[16]; + uint8_t in[16]; + uint8_t out[16]; } ecbdata_t; - void aes128_init(uint8_t *); -void aes128_ecb(uint8_t *, uint8_t const * const); +void aes128_ecb(uint8_t *, uint8_t const *const); void aes_dump_state(void); diff --git a/block.c b/block.c index c401015..c9b1af1 100644 --- a/block.c +++ b/block.c @@ -9,92 +9,91 @@ #include #ifdef HOST_BUILD -void block_print(char const * const label, - uint8_t const * const b) { - if (label != NULL) { - printf("%s: ", label); - } else { - printf("\n"); - } - for(int i = 0; i < 16; i++) { - printf("%.2x", b[i]); - if (!((i+1) % 4)) { - printf(" "); +void block_print(char const *const label, uint8_t const *const b) { + if (label != NULL) { + printf("%s: ", label); + } else { + printf("\n"); } - } - printf("\n"); - return; + for (int i = 0; i < 16; i++) { + printf("%.2x", b[i]); + if (!((i + 1) % 4)) { + printf(" "); + } + } + printf("\n"); + return; } -void block_print_bytes(char const * const label, - uint8_t const * const b, uint32_t num_bytes) { - printf("%s: ", label); - for(int i = 0; i < num_bytes; i++) { - printf("%.2x", b[i]); - if (!((i+1) % 4)) { - printf(" "); +void block_print_bytes(char const *const label, uint8_t const *const b, + uint32_t num_bytes) { + printf("%s: ", label); + for (int i = 0; i < num_bytes; i++) { + printf("%.2x", b[i]); + if (!((i + 1) % 4)) { + printf(" "); + } } - } - printf("\n"); + printf("\n"); } #endif /* HOST_BUILD */ -void block_xor(uint8_t *dest, uint8_t * const a, uint8_t * const b) { - for (uint_fast8_t i = 0; i < 4; i++) { - *((uint32_t *)dest+i) = *((uint32_t *)a+i) ^ *((uint32_t *)b+i); - } - return; +void block_xor(uint8_t *dest, uint8_t *const a, uint8_t *const b) { + for (uint_fast8_t i = 0; i < 4; i++) { + *((uint32_t *)dest + i) = *((uint32_t *)a + i) ^ *((uint32_t *)b + i); + } + return; } -void block_shiftr(uint8_t *dest, uint8_t * const a, uint_fast8_t num) { - uint_fast8_t n = (num <= 8 ? num : 8); - for (int_fast8_t i = 15; i >= 0; i--) { - /* Shift right from LSB to MSB */ - dest[i] = a[i] >> n; - if (i != 0) { - dest[i] |= (a[i-1] << (8 - n)); +void block_shiftr(uint8_t *dest, uint8_t *const a, uint_fast8_t num) { + uint_fast8_t n = (num <= 8 ? num : 8); + for (int_fast8_t i = 15; i >= 0; i--) { + /* Shift right from LSB to MSB */ + dest[i] = a[i] >> n; + if (i != 0) { + dest[i] |= (a[i - 1] << (8 - n)); + } } - } - if (num - n != 0) { - block_shiftr(dest, dest, num - n); - } - return; + if (num - n != 0) { + block_shiftr(dest, dest, num - n); + } + return; } -void block_shiftl(uint8_t *dest, uint8_t * const a, uint_fast8_t num) { - uint_fast8_t n = (num <= 8 ? num : 8); - for (int_fast8_t i = 0; i < 16; i++) { - dest[i] = a[i] << n; - if (i != 15) { - dest[i] |= (a[i+1] >> (8 - n)); +void block_shiftl(uint8_t *dest, uint8_t *const a, uint_fast8_t num) { + uint_fast8_t n = (num <= 8 ? num : 8); + for (int_fast8_t i = 0; i < 16; i++) { + dest[i] = a[i] << n; + if (i != 15) { + dest[i] |= (a[i + 1] >> (8 - n)); + } } - } - if (num - n != 0) { - block_shiftl(dest, dest, num - n); - } - return; - } + if (num - n != 0) { + block_shiftl(dest, dest, num - n); + } + return; +} void block_incr(uint8_t *in) { - for (int_fast8_t i = 15; i > 0 ; i--) { - uint8_t prev = in[i]; - in[i] += 1; - if (in[i] > prev) { - /* No overflow, changes propagate no further */ - break; + for (int_fast8_t i = 15; i > 0; i--) { + uint8_t prev = in[i]; + in[i] += 1; + if (in[i] > prev) { + /* No overflow, changes propagate no further */ + break; + } } - } - return; + return; } void block_decr(uint8_t *in) { - for (int_fast8_t i = 15; i > 0 ; i--) { - uint8_t prev = in[i]; - in[i] -= 1; - if (in[i] < prev) { - /* No overflow, changes propagate no further */ - break; + for (int_fast8_t i = 15; i > 0; i--) { + uint8_t prev = in[i]; + in[i] -= 1; + if (in[i] < prev) { + /* No overflow, changes propagate no further */ + break; + } } - } - return; + return; } diff --git a/block.h b/block.h index 02819f2..810ad2a 100644 --- a/block.h +++ b/block.h @@ -8,9 +8,8 @@ void block_decr(uint8_t *); void block_incr(uint8_t *); -void block_print(char const * const, uint8_t const * const); -void block_print_bytes(char const * const, uint8_t 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_print(char const *const, uint8_t const *const); +void block_print_bytes(char const *const, uint8_t 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); diff --git a/cmac.c b/cmac.c index 24b5496..c77f22f 100644 --- a/cmac.c +++ b/cmac.c @@ -11,85 +11,83 @@ static const uint8_t zeros[16] = {0}; -static uint8_t g_k1[16], - g_k2[16]; +static uint8_t g_k1[16], g_k2[16]; #ifdef HOST_BUILD void cmac_get_subkeys(uint8_t *dest) { - /* Testing stub to get subkeys for algo check */ - memcpy(dest, g_k1, 16); - memcpy(dest+16, g_k2, 16); - return; + /* Testing stub to get subkeys for algo check */ + memcpy(dest, g_k1, 16); + memcpy(dest + 16, g_k2, 16); + return; } #endif /* HOST_BUILD */ void cmac_aes128_init(uint8_t *key) { - /* Initialize AES engine and cache subkeys */ - if (key != NULL) { - aes128_init(key); - cmac_aes128_expand_key(key, g_k1, g_k2); - } -} + /* Initialize AES engine and cache subkeys */ + if (key != NULL) { + aes128_init(key); + cmac_aes128_expand_key(key, g_k1, g_k2); + } +} -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}; - - aes128_ecb(l, zeros); - if ((l[0] >> 7) == 0) { - block_shiftl(k1, l, 1); - } else { - uint8_t tmp[16]; - block_shiftl(tmp, l, 1); - block_xor(k1, tmp, Rb); - } - if (!(k1[0] >> 7)) { - block_shiftl(k2, k1, 1); - } else { - uint8_t tmp[16]; - block_shiftl(tmp, k1, 1); - block_xor(k2, tmp, Rb); - } +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}; + + aes128_ecb(l, zeros); + if ((l[0] >> 7) == 0) { + block_shiftl(k1, l, 1); + } else { + uint8_t tmp[16]; + block_shiftl(tmp, l, 1); + block_xor(k1, tmp, Rb); + } + if (!(k1[0] >> 7)) { + block_shiftl(k2, k1, 1); + } else { + uint8_t tmp[16]; + block_shiftl(tmp, k1, 1); + block_xor(k2, tmp, Rb); + } } static void cmac_truncate(uint8_t *dest, uint8_t *tag, uint_fast8_t tag_len) { - memmove(dest, tag, tag_len); + memmove(dest, tag, tag_len); } -void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, uint_fast8_t tag_len) { - /* Simulate ceiling integer division by adding a block if remainder */ - size_t num_blocks = msg_len / 16 + (msg_len % 16 ? 1 : 0); - bool last_block_complete = !(msg_len % 16 ? 1 : 0); - if (msg_len == 0) { - num_blocks = 1; - last_block_complete = false; - } - - 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); +void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, + uint_fast8_t tag_len) { + /* Simulate ceiling integer division by adding a block if remainder */ + size_t num_blocks = msg_len / 16 + (msg_len % 16 ? 1 : 0); + bool last_block_complete = !(msg_len % 16 ? 1 : 0); + if (msg_len == 0) { + num_blocks = 1; + last_block_complete = false; + } - if (!last_block_complete) { - /* Padding is single 1 bit, run out on 0s.. find the next byte, - set it to 0b1000000 */ - alt_msg[msg_len] = 0x80; - block_xor(last_block, last_block, g_k2); - } else { - block_xor(last_block, last_block, g_k1); - } - - uint8_t x[16] = {0}, - y[16] = {0}; + 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); - 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); - } - cmac_truncate(dest, x, tag_len); - return; + if (!last_block_complete) { + /* Padding is single 1 bit, run out on 0s.. find the next byte, + set it to 0b1000000 */ + alt_msg[msg_len] = 0x80; + block_xor(last_block, last_block, g_k2); + } else { + block_xor(last_block, last_block, g_k1); + } + + uint8_t x[16] = {0}, 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); + } + cmac_truncate(dest, x, tag_len); + return; } - diff --git a/cmac.h b/cmac.h index f7f7a25..0d247fc 100644 --- a/cmac.h +++ b/cmac.h @@ -6,6 +6,6 @@ #include "block.h" void cmac_aes128(uint8_t *, uint8_t *, size_t, uint_fast8_t); -void cmac_aes128_expand_key(uint8_t const * const, uint8_t *, uint8_t *); +void cmac_aes128_expand_key(uint8_t const *const, uint8_t *, uint8_t *); void cmac_aes128_init(uint8_t *); void cmac_get_subkeys(uint8_t *); diff --git a/ctr.c b/ctr.c index ac92aee..1594890 100644 --- a/ctr.c +++ b/ctr.c @@ -6,29 +6,29 @@ static uint8_t g_counter[16]; void aes128_ctr_init(uint8_t *key, uint8_t *counter) { - if (counter != NULL) { - memcpy(g_counter, counter, 16); - } - aes128_init(key); + if (counter != NULL) { + memcpy(g_counter, counter, 16); + } + aes128_init(key); } void aes128_ctr_evolve_counter(void) { - block_incr(g_counter); - return; + block_incr(g_counter); + return; } void aes128_ctr(uint8_t *dest, uint8_t *msg, uint32_t msg_len) { - uint8_t buffer[16]; - uint32_t num_blocks = msg_len / 16; - for (uint32_t i = 0; i < num_blocks; i++) { - aes128_ecb(buffer, g_counter); - aes128_ctr_evolve_counter(); - block_xor(dest+(i*16), msg+(i*16), buffer); - } - if (msg_len % 16) { - aes128_ecb(buffer, g_counter); - for (uint8_t i = 0; i < msg_len % 16; i++) { - dest[num_blocks*16+i] = msg[num_blocks*16+i] ^ buffer[i]; + uint8_t buffer[16]; + uint32_t num_blocks = msg_len / 16; + for (uint32_t i = 0; i < num_blocks; i++) { + aes128_ecb(buffer, g_counter); + aes128_ctr_evolve_counter(); + block_xor(dest + (i * 16), msg + (i * 16), buffer); + } + if (msg_len % 16) { + aes128_ecb(buffer, g_counter); + for (uint8_t i = 0; i < msg_len % 16; i++) { + dest[num_blocks * 16 + i] = msg[num_blocks * 16 + i] ^ buffer[i]; + } } - } } diff --git a/ctr.h b/ctr.h index 564410a..ab7d716 100644 --- a/ctr.h +++ b/ctr.h @@ -4,4 +4,3 @@ void aes128_ctr(uint8_t *, uint8_t *, uint32_t); void aes128_ctr_init(uint8_t *, uint8_t *); - diff --git a/eax.c b/eax.c index 59766cc..13673d1 100644 --- a/eax.c +++ b/eax.c @@ -8,42 +8,40 @@ static uint8_t g_nonce[16] = {0}; void aes128_eax_init(uint8_t *key, uint8_t *nonce) { - if (nonce != NULL) { - memcpy(g_nonce, nonce, 16); - } - cmac_aes128_init(key); - aes128_ctr_init(key, nonce); - return; + if (nonce != NULL) { + memcpy(g_nonce, nonce, 16); + } + cmac_aes128_init(key); + aes128_ctr_init(key, nonce); + return; } -static void eax_omac(uint8_t *dest, uint8_t *msg, uint32_t msg_len, uint32_t t) { - uint8_t buf[msg_len+16]; - memset(buf, 0, msg_len+16); - buf[15] = (uint8_t)t; - buf[14] = (uint8_t)t >> 8; - buf[13] = (uint8_t)t >> 16; - buf[12] = (uint8_t)t >> 24; - memcpy(buf+16, msg, msg_len); - cmac_aes128(dest, buf, msg_len+16, 16); - return; +static void eax_omac(uint8_t *dest, uint8_t *msg, uint32_t msg_len, + uint32_t t) { + uint8_t buf[msg_len + 16]; + memset(buf, 0, msg_len + 16); + buf[15] = (uint8_t)t; + buf[14] = (uint8_t)t >> 8; + buf[13] = (uint8_t)t >> 16; + buf[12] = (uint8_t)t >> 24; + memcpy(buf + 16, msg, msg_len); + cmac_aes128(dest, buf, msg_len + 16, 16); + return; } - void aes128_eax(uint8_t *dest, uint8_t *header, uint32_t header_len, - uint8_t *msg, uint32_t msg_len, uint_fast8_t tag_len) { - uint8_t nonce_cmac[16], - header_cmac[16], - ciphertext_cmac[16], - ciphertext[msg_len]; - - eax_omac(nonce_cmac, g_nonce, 16, 0); - eax_omac(header_cmac, header, header_len, 1); - aes128_ctr_init(NULL, nonce_cmac); - aes128_ctr(ciphertext, msg, msg_len); - eax_omac(ciphertext_cmac, ciphertext, msg_len, 2); - block_xor(nonce_cmac, nonce_cmac, header_cmac); - block_xor(nonce_cmac, nonce_cmac, ciphertext_cmac); - memcpy(dest, ciphertext, msg_len); - memcpy(dest+msg_len, nonce_cmac, tag_len); - return; + uint8_t *msg, uint32_t msg_len, uint_fast8_t tag_len) { + uint8_t nonce_cmac[16], header_cmac[16], ciphertext_cmac[16], + ciphertext[msg_len]; + + eax_omac(nonce_cmac, g_nonce, 16, 0); + eax_omac(header_cmac, header, header_len, 1); + aes128_ctr_init(NULL, nonce_cmac); + aes128_ctr(ciphertext, msg, msg_len); + eax_omac(ciphertext_cmac, ciphertext, msg_len, 2); + block_xor(nonce_cmac, nonce_cmac, header_cmac); + block_xor(nonce_cmac, nonce_cmac, ciphertext_cmac); + memcpy(dest, ciphertext, msg_len); + memcpy(dest + msg_len, nonce_cmac, tag_len); + return; } diff --git a/eax.h b/eax.h index c9e46eb..13f53f0 100644 --- a/eax.h +++ b/eax.h @@ -1,5 +1,6 @@ #pragma once -void aes128_eax(uint8_t *, uint8_t *, uint32_t, uint8_t *, uint32_t, uint_fast8_t); +void aes128_eax(uint8_t *, uint8_t *, uint32_t, uint8_t *, uint32_t, + uint_fast8_t); void aes128_eax_init(uint8_t *, uint8_t *); void eax_dump_state(void); diff --git a/main.c b/main.c index 3bd809f..2ea4721 100644 --- a/main.c +++ b/main.c @@ -12,76 +12,83 @@ #if (ERROR_LED_ACTIVE == LOW) #define ERROR_LED_OFF nrf_gpio_pin_set(ERROR_LED_PIN) -#define ERROR_LED_ON nrf_gpio_pin_clear(ERROR_LED_PIN) +#define ERROR_LED_ON nrf_gpio_pin_clear(ERROR_LED_PIN) #else #define ERROR_LED_OFF nrf_gpio_pin_clear(ERROR_LED_PIN) -#define ERROR_LED_ON nrf_gpio_pin_set(ERROR_LED_PIN) +#define ERROR_LED_ON nrf_gpio_pin_set(ERROR_LED_PIN) #endif /* ERROR_LET_ACTIVE */ void test_cmac(void) { - /* NIST Examples */ - uint8_t k[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; - cmac_aes128_init(k); - uint8_t tag[16]; - - /* Test Null Message, NIST Example */ - uint8_t *msg1 = NULL; - uint8_t case1[] = {0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, - 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46}; - cmac_aes128(tag, msg1, 0, 16); - if (!block_eq(tag, case1)) { - ERROR_LED_ON; - while(1); - } - - /* 16b example */ - uint8_t msg2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, - 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; - uint8_t case2[] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, - 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c}; - cmac_aes128(tag, msg2, sizeof(msg2), 16); - if (!block_eq(tag, case2)) { - ERROR_LED_ON; - while(1); - } - - /* 40b example */ - uint8_t msg3[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, - 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, - 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, - 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, - 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11}; - uint8_t case3[] = {0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30, - 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27}; - cmac_aes128(tag, msg3, sizeof(msg3), 16); - if (!block_eq(tag, case3)) { - ERROR_LED_ON; - while(1); - } - - /* 64b example */ - uint8_t msg4[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, - 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, - 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, - 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, - 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, - 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, - 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, - 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10}; - uint8_t case4[] = {0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, - 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe}; - cmac_aes128(tag, msg4, sizeof(msg4), 16); - if (!block_eq(tag, case4)) { - ERROR_LED_ON; - while(1); - } + /* NIST Examples */ + uint8_t k[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; + cmac_aes128_init(k); + uint8_t tag[16]; + + /* Test Null Message, NIST Example */ + uint8_t *msg1 = NULL; + uint8_t case1[] = {0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, + 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46}; + cmac_aes128(tag, msg1, 0, 16); + if (!block_eq(tag, case1)) { + ERROR_LED_ON; + while (1) { + ; + } + } + + /* 16b example */ + uint8_t msg2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; + uint8_t case2[] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, + 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c}; + cmac_aes128(tag, msg2, sizeof(msg2), 16); + if (!block_eq(tag, case2)) { + ERROR_LED_ON; + while (1) { + ; + } + } + + /* 40b example */ + uint8_t msg3[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, + 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, + 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11}; + uint8_t case3[] = {0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30, + 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27}; + cmac_aes128(tag, msg3, sizeof(msg3), 16); + if (!block_eq(tag, case3)) { + ERROR_LED_ON; + while (1) { + ; + } + } + + /* 64b example */ + uint8_t msg4[] = { + 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, + 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, + 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, + 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, + 0x1a, 0x0a, 0x52, 0xef, 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, + 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10}; + uint8_t case4[] = {0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, + 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe}; + cmac_aes128(tag, msg4, sizeof(msg4), 16); + if (!block_eq(tag, case4)) { + ERROR_LED_ON; + while (1) { + ; + } + } } int main(void) { - nrf_gpio_cfg_output(ERROR_LED_PIN); - ERROR_LED_OFF; - for (;;) { - test_cmac(); - } + nrf_gpio_cfg_output(ERROR_LED_PIN); + ERROR_LED_OFF; + for (;;) { + test_cmac(); + } } diff --git a/rng.c b/rng.c index f33377f..0f2da45 100644 --- a/rng.c +++ b/rng.c @@ -3,23 +3,24 @@ #include "nrf.h" uint8_t rng_byte(void) { - NRF_RNG->TASKS_START = 1; - while(!NRF_RNG->EVENTS_VALRDY) { - /* Waiting for hardware */ - } - uint8_t val = (uint8_t)NRF_RNG->VALUE; - NRF_RNG->TASKS_STOP = 1; - NRF_RNG->EVENTS_VALRDY = 0; - return val; + NRF_RNG->TASKS_START = 1; + while (!NRF_RNG->EVENTS_VALRDY) { + /* Waiting for hardware */ + } + uint8_t val = (uint8_t)NRF_RNG->VALUE; + NRF_RNG->TASKS_STOP = 1; + NRF_RNG->EVENTS_VALRDY = 0; + return val; } void rng_bytes(uint8_t *out, uint32_t num_bytes) { - NRF_RNG->TASKS_START = 1; - for (uint32_t i = 0; i < num_bytes; i++) { - while(!NRF_RNG->EVENTS_VALRDY); - out[i] = (uint8_t)NRF_RNG->VALUE; - NRF_RNG->EVENTS_VALRDY = 0; - } - NRF_RNG->TASKS_STOP = 1; - return; + NRF_RNG->TASKS_START = 1; + for (uint32_t i = 0; i < num_bytes; i++) { + while (!NRF_RNG->EVENTS_VALRDY) + ; + out[i] = (uint8_t)NRF_RNG->VALUE; + NRF_RNG->EVENTS_VALRDY = 0; + } + NRF_RNG->TASKS_STOP = 1; + return; } diff --git a/scripts/cleanup-format.sh b/scripts/cleanup-format.sh new file mode 100755 index 0000000..ccec5b4 --- /dev/null +++ b/scripts/cleanup-format.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +clang-tidy \ + -fix \ + -fix-errors \ + -header-filter=.* \ + --checks=readability-braces-around-statements,misc-macro-parentheses \ + *.c \ + -- -I. +clang-format -style="{BasedOnStyle: llvm, IndentWidth: 4, AllowShortFunctionsOnASingleLine: None, KeepEmptyLinesAtTheStartOfBlocks: false}" -i *.{h,c} + diff --git a/template/Makefile.posix b/template/Makefile.posix index 6ab5ae3..e5fc1e8 100644 --- a/template/Makefile.posix +++ b/template/Makefile.posix @@ -78,12 +78,12 @@ debug-gdbinit: .PHONY: flash flash-softdevice erase-all startdebug -TEST_CFLAGS=-std=gnu99 -g -Wall -Werror -I.. -DHOST_BUILD +TEST_CFLAGS=-std=gnu99 -g -Wall -Werror -I.. -DHOST_BUILD -fsanitize=address test: - gcc $(TEST_CFLAGS) -lcrypto block.c aes.c cmac.c tests/test_cmac.c -o tests/test_cmac + $(HOST_CC) $(TEST_CFLAGS) -lcrypto block.c aes.c cmac.c tests/test_cmac.c -o tests/test_cmac ./tests/test_cmac - gcc $(TEST_CFLAGS) -lcrypto block.c aes.c ctr.c tests/test_ctr.c -o tests/test_ctr + $(HOST_CC) $(TEST_CFLAGS) -lcrypto block.c aes.c ctr.c tests/test_ctr.c -o tests/test_ctr ./tests/test_ctr - gcc $(TEST_CFLAGS) -lcrypto block.c aes.c ctr.c cmac.c eax.c tests/test_eax.c -o tests/test_eax + $(HOST_CC) $(TEST_CFLAGS) -lcrypto block.c aes.c ctr.c cmac.c eax.c tests/test_eax.c -o tests/test_eax ./tests/test_eax