diff --git a/aes.c b/aes.c index 7606ae2..1d94297 100644 --- a/aes.c +++ b/aes.c @@ -29,8 +29,9 @@ void aes128_ecb(uint8_t *dest, uint8_t const *const in) { #else memmove(g_ecbdata.in, in, 16); NRF_ECB->TASKS_STARTECB = 1; - while (!NRF_ECB->EVENTS_ENDECB) + while (!NRF_ECB->EVENTS_ENDECB) { ; + } NRF_ECB->EVENTS_ENDECB = 0; #endif /* HOST_BUILD */ memmove(dest, g_ecbdata.out, 16); diff --git a/block.c b/block.c index 2591f5e..44dcce7 100644 --- a/block.c +++ b/block.c @@ -1,8 +1,6 @@ /* Utility functions for manipulating block_t structures; 128bit blocks of data for AES & CMAC */ - - #include #include #include @@ -13,16 +11,16 @@ #endif #include "block.h" -#if defined (HOST_BUILD) || defined (DEBUG_UART) +#if defined(HOST_BUILD) || defined(DEBUG_UART) void block_print(char const *const label, void const *const b) { - block_print_bytes(label, b, 16); - return; + block_print_bytes(label, b, 16); + return; } void block_print_bytes(char const *const label, void const *const v, uint32_t num_bytes) { - const uint8_t *b = v; - printf("%s: ", label); + const uint8_t *b = v; + printf("%s: ", label); for (int i = 0; i < num_bytes; i++) { printf("%.2x", b[i]); if (!((i + 1) % 4)) { diff --git a/ckdf.c b/ckdf.c index c8fc3ae..1093120 100644 --- a/ckdf.c +++ b/ckdf.c @@ -13,41 +13,41 @@ #include "cmac.h" void ckdf_extract(uint8_t *prk, const uint8_t *salt, const uint8_t *ikm, - uint32_t ikm_len) { - uint8_t zeros[16] = {0}; - if (salt == NULL) { - cmac_aes128_init(zeros); - } else { - cmac_aes128_init(salt); - } - cmac_aes128(prk, (uint8_t*) ikm, ikm_len, 16); + uint32_t ikm_len) { + uint8_t zeros[16] = {0}; + if (salt == NULL) { + cmac_aes128_init(zeros); + } else { + cmac_aes128_init(salt); + } + cmac_aes128(prk, (uint8_t *)ikm, ikm_len, 16); } -void ckdf_expand(uint8_t *okm, uint8_t *prk, uint8_t *info, - uint32_t info_len, uint32_t okm_len) { - uint32_t num_blocks = okm_len / 16; - /* Simulate ceiling division instead of C std floor */ - uint8_t remainder = okm_len % 16; - if (remainder) { - num_blocks++; - } - cmac_aes128_init(prk); - uint8_t t[16+info_len+1]; - uint8_t *initial = &t[16]; - memset(t, 0, 16); - for (uint32_t i = 0; i < num_blocks; i++) { - *(initial+info_len) = i+1; - if (i == 0) { - memcpy(initial, info, info_len); - cmac_aes128(t, initial, info_len+1, 16); - } else { - cmac_aes128(t, t, sizeof(t), 16); +void ckdf_expand(uint8_t *okm, uint8_t *prk, uint8_t *info, uint32_t info_len, + uint32_t okm_len) { + uint32_t num_blocks = okm_len / 16; + /* Simulate ceiling division instead of C std floor */ + uint8_t remainder = okm_len % 16; + if (remainder) { + num_blocks++; } - if (i == num_blocks - 1 && remainder) { - memcpy(okm+16*i, t, remainder); - } else { - memcpy(okm+16*i, t, 16); + cmac_aes128_init(prk); + uint8_t t[16 + info_len + 1]; + uint8_t *initial = &t[16]; + memset(t, 0, 16); + for (uint32_t i = 0; i < num_blocks; i++) { + *(initial + info_len) = i + 1; + if (i == 0) { + memcpy(initial, info, info_len); + cmac_aes128(t, initial, info_len + 1, 16); + } else { + cmac_aes128(t, t, sizeof(t), 16); + } + if (i == num_blocks - 1 && remainder) { + memcpy(okm + 16 * i, t, remainder); + } else { + memcpy(okm + 16 * i, t, 16); + } } - } - return; + return; } diff --git a/ckdf.h b/ckdf.h index 9e7ffc5..b713e1a 100644 --- a/ckdf.h +++ b/ckdf.h @@ -9,4 +9,3 @@ void ckdf_extract(uint8_t *, const uint8_t *, const uint8_t *, uint32_t); void ckdf_expand(uint8_t *, uint8_t *, uint8_t *, uint32_t, uint32_t); - diff --git a/ctr.c b/ctr.c index 2b71b86..040eab2 100644 --- a/ctr.c +++ b/ctr.c @@ -10,7 +10,7 @@ void aes128_ctr_init(uint8_t *key, uint8_t *counter) { memcpy(g_counter, counter, 16); } if (key != NULL) { - aes128_init(key); + aes128_init(key); } } diff --git a/eax.c b/eax.c index ab50223..4529aa0 100644 --- a/eax.c +++ b/eax.c @@ -12,7 +12,7 @@ void aes128_eax_init(const uint8_t *key, uint8_t *nonce) { memcpy(g_nonce, nonce, 16); } if (key != NULL) { - cmac_aes128_init(key); + cmac_aes128_init(key); } aes128_ctr_init(key, nonce); return; diff --git a/rng.c b/rng.c index 0f2da45..d8cdfe4 100644 --- a/rng.c +++ b/rng.c @@ -16,8 +16,9 @@ uint8_t rng_byte(void) { 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) + while (!NRF_RNG->EVENTS_VALRDY) { ; + } out[i] = (uint8_t)NRF_RNG->VALUE; NRF_RNG->EVENTS_VALRDY = 0; } diff --git a/scripts/cleanup-format.sh b/scripts/cleanup-format.sh index ccec5b4..0539e39 100755 --- a/scripts/cleanup-format.sh +++ b/scripts/cleanup-format.sh @@ -5,6 +5,5 @@ clang-tidy \ -header-filter=.* \ --checks=readability-braces-around-statements,misc-macro-parentheses \ *.c \ - -- -I. + -- -DNRF51 -I. -I ../../../nrf-sdk/6.1/nrf51822/Include -I ../../../nrf-sdk/6.1/nrf51822/Include/gcc clang-format -style="{BasedOnStyle: llvm, IndentWidth: 4, AllowShortFunctionsOnASingleLine: None, KeepEmptyLinesAtTheStartOfBlocks: false}" -i *.{h,c} -