1
0
Fork 0

Auto formatted and tidy'd

This commit is contained in:
Shawn Nock 2016-06-07 12:51:42 -04:00
parent 18a25e958b
commit 5ce8d1b447
15 changed files with 312 additions and 299 deletions

View File

@ -12,6 +12,7 @@ SDK_PATH = $(HOME)/devel/nrf-sdk/10.0/
TEMPLATE_PATH = ./template/ TEMPLATE_PATH = ./template/
HOST_CC = gcc # Used when running tests on host
CFLAGS = -Os -Wall -Werror -flto -g CFLAGS = -Os -Wall -Werror -flto -g
LDFLAGS = -Os -flto -g LDFLAGS = -Os -flto -g

5
aes.c
View File

@ -21,7 +21,7 @@ void aes128_init(uint8_t *key) {
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 #ifdef HOST_BUILD
AES_KEY key; AES_KEY key;
AES_set_encrypt_key(g_ecbdata.key, 128, &key); AES_set_encrypt_key(g_ecbdata.key, 128, &key);
@ -29,7 +29,8 @@ void aes128_ecb(uint8_t *dest, uint8_t const * const in) {
#else #else
memmove(g_ecbdata.in, in, 16); memmove(g_ecbdata.in, in, 16);
NRF_ECB->TASKS_STARTECB = 1; NRF_ECB->TASKS_STARTECB = 1;
while (!NRF_ECB->EVENTS_ENDECB); while (!NRF_ECB->EVENTS_ENDECB)
;
NRF_ECB->EVENTS_ENDECB = 0; NRF_ECB->EVENTS_ENDECB = 0;
#endif /* HOST_BUILD */ #endif /* HOST_BUILD */
memmove(dest, g_ecbdata.out, 16); memmove(dest, g_ecbdata.out, 16);

3
aes.h
View File

@ -10,7 +10,6 @@ typedef struct {
uint8_t out[16]; uint8_t out[16];
} ecbdata_t; } ecbdata_t;
void aes128_init(uint8_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); void aes_dump_state(void);

33
block.c
View File

@ -9,16 +9,15 @@
#include <string.h> #include <string.h>
#ifdef HOST_BUILD #ifdef HOST_BUILD
void block_print(char const * const label, void block_print(char const *const label, uint8_t const *const b) {
uint8_t const * const b) {
if (label != NULL) { if (label != NULL) {
printf("%s: ", label); printf("%s: ", label);
} else { } else {
printf("\n"); printf("\n");
} }
for(int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
printf("%.2x", b[i]); printf("%.2x", b[i]);
if (!((i+1) % 4)) { if (!((i + 1) % 4)) {
printf(" "); printf(" ");
} }
} }
@ -26,12 +25,12 @@ void block_print(char const * const label,
return; return;
} }
void block_print_bytes(char const * const label, void block_print_bytes(char const *const label, uint8_t const *const b,
uint8_t const * const b, uint32_t num_bytes) { uint32_t num_bytes) {
printf("%s: ", label); printf("%s: ", label);
for(int i = 0; i < num_bytes; i++) { for (int i = 0; i < num_bytes; i++) {
printf("%.2x", b[i]); printf("%.2x", b[i]);
if (!((i+1) % 4)) { if (!((i + 1) % 4)) {
printf(" "); printf(" ");
} }
} }
@ -39,20 +38,20 @@ void block_print_bytes(char const * const label,
} }
#endif /* HOST_BUILD */ #endif /* HOST_BUILD */
void block_xor(uint8_t *dest, uint8_t * const a, uint8_t * const b) { void block_xor(uint8_t *dest, uint8_t *const a, uint8_t *const b) {
for (uint_fast8_t i = 0; i < 4; i++) { for (uint_fast8_t i = 0; i < 4; i++) {
*((uint32_t *)dest+i) = *((uint32_t *)a+i) ^ *((uint32_t *)b+i); *((uint32_t *)dest + i) = *((uint32_t *)a + i) ^ *((uint32_t *)b + i);
} }
return; return;
} }
void block_shiftr(uint8_t *dest, uint8_t * const a, uint_fast8_t num) { void block_shiftr(uint8_t *dest, uint8_t *const a, uint_fast8_t num) {
uint_fast8_t n = (num <= 8 ? num : 8); uint_fast8_t n = (num <= 8 ? num : 8);
for (int_fast8_t i = 15; i >= 0; i--) { for (int_fast8_t i = 15; i >= 0; i--) {
/* Shift right from LSB to MSB */ /* Shift right from LSB to MSB */
dest[i] = a[i] >> n; dest[i] = a[i] >> n;
if (i != 0) { if (i != 0) {
dest[i] |= (a[i-1] << (8 - n)); dest[i] |= (a[i - 1] << (8 - n));
} }
} }
if (num - n != 0) { if (num - n != 0) {
@ -61,22 +60,22 @@ void block_shiftr(uint8_t *dest, uint8_t * const a, uint_fast8_t num) {
return; return;
} }
void block_shiftl(uint8_t *dest, uint8_t * const a, uint_fast8_t num) { void block_shiftl(uint8_t *dest, uint8_t *const a, uint_fast8_t num) {
uint_fast8_t n = (num <= 8 ? num : 8); uint_fast8_t n = (num <= 8 ? num : 8);
for (int_fast8_t i = 0; i < 16; i++) { for (int_fast8_t i = 0; i < 16; i++) {
dest[i] = a[i] << n; dest[i] = a[i] << n;
if (i != 15) { if (i != 15) {
dest[i] |= (a[i+1] >> (8 - n)); dest[i] |= (a[i + 1] >> (8 - n));
} }
} }
if (num - n != 0) { if (num - n != 0) {
block_shiftl(dest, dest, num - n); block_shiftl(dest, dest, num - n);
} }
return; return;
} }
void block_incr(uint8_t *in) { void block_incr(uint8_t *in) {
for (int_fast8_t i = 15; i > 0 ; i--) { for (int_fast8_t i = 15; i > 0; i--) {
uint8_t prev = in[i]; uint8_t prev = in[i];
in[i] += 1; in[i] += 1;
if (in[i] > prev) { if (in[i] > prev) {
@ -88,7 +87,7 @@ void block_incr(uint8_t *in) {
} }
void block_decr(uint8_t *in) { void block_decr(uint8_t *in) {
for (int_fast8_t i = 15; i > 0 ; i--) { for (int_fast8_t i = 15; i > 0; i--) {
uint8_t prev = in[i]; uint8_t prev = in[i];
in[i] -= 1; in[i] -= 1;
if (in[i] < prev) { if (in[i] < prev) {

11
block.h
View File

@ -8,9 +8,8 @@
void block_decr(uint8_t *); void block_decr(uint8_t *);
void block_incr(uint8_t *); void block_incr(uint8_t *);
void block_print(char const * const, uint8_t const * 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_print_bytes(char const *const, uint8_t const *const, uint32_t);
void block_shiftl(uint8_t *, uint8_t * const, uint_fast8_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_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, uint8_t *const);

26
cmac.c
View File

@ -11,14 +11,13 @@
static const uint8_t zeros[16] = {0}; static const uint8_t zeros[16] = {0};
static uint8_t g_k1[16], static uint8_t g_k1[16], g_k2[16];
g_k2[16];
#ifdef HOST_BUILD #ifdef HOST_BUILD
void cmac_get_subkeys(uint8_t *dest) { void cmac_get_subkeys(uint8_t *dest) {
/* Testing stub to get subkeys for algo check */ /* Testing stub to get subkeys for algo check */
memcpy(dest, g_k1, 16); memcpy(dest, g_k1, 16);
memcpy(dest+16, g_k2, 16); memcpy(dest + 16, g_k2, 16);
return; return;
} }
#endif /* HOST_BUILD */ #endif /* HOST_BUILD */
@ -31,11 +30,11 @@ void cmac_aes128_init(uint8_t *key) {
} }
} }
void cmac_aes128_expand_key(uint8_t const * const key, uint8_t *k1, uint8_t *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 */ /* Generate two required subkeys according to NIST 800-38B */
uint8_t l[16] = {0}, uint8_t l[16] = {0},
Rb[16] = {0, 0, 0, 0, 0, 0, 0, 0, Rb[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87};
0, 0, 0, 0, 0, 0, 0, 0x87};
aes128_ecb(l, zeros); aes128_ecb(l, zeros);
if ((l[0] >> 7) == 0) { if ((l[0] >> 7) == 0) {
@ -58,7 +57,8 @@ 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) { 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 */ /* Simulate ceiling integer division by adding a block if remainder */
size_t num_blocks = msg_len / 16 + (msg_len % 16 ? 1 : 0); size_t num_blocks = msg_len / 16 + (msg_len % 16 ? 1 : 0);
bool last_block_complete = !(msg_len % 16 ? 1 : 0); bool last_block_complete = !(msg_len % 16 ? 1 : 0);
@ -67,9 +67,9 @@ void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, uint_fast8_t tag_l
last_block_complete = false; last_block_complete = false;
} }
uint8_t alt_msg[num_blocks*16], uint8_t alt_msg[num_blocks * 16],
*last_block = &alt_msg[(num_blocks-1)*16]; *last_block = &alt_msg[(num_blocks - 1) * 16];
memset(alt_msg, 0, num_blocks*16); memset(alt_msg, 0, num_blocks * 16);
memmove(alt_msg, msg, msg_len); memmove(alt_msg, msg, msg_len);
if (!last_block_complete) { if (!last_block_complete) {
@ -81,15 +81,13 @@ void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, uint_fast8_t tag_l
block_xor(last_block, last_block, g_k1); block_xor(last_block, last_block, g_k1);
} }
uint8_t x[16] = {0}, uint8_t x[16] = {0}, y[16] = {0};
y[16] = {0};
for (uint32_t i = 0; i < num_blocks; i++) { for (uint32_t i = 0; i < num_blocks; i++) {
uint8_t *block = &alt_msg[i*16]; uint8_t *block = &alt_msg[i * 16];
block_xor(y, x, block); block_xor(y, x, block);
aes128_ecb(x, y); aes128_ecb(x, y);
} }
cmac_truncate(dest, x, tag_len); cmac_truncate(dest, x, tag_len);
return; return;
} }

2
cmac.h
View File

@ -6,6 +6,6 @@
#include "block.h" #include "block.h"
void cmac_aes128(uint8_t *, uint8_t *, size_t, uint_fast8_t); 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_aes128_init(uint8_t *);
void cmac_get_subkeys(uint8_t *); void cmac_get_subkeys(uint8_t *);

4
ctr.c
View File

@ -23,12 +23,12 @@ void aes128_ctr(uint8_t *dest, uint8_t *msg, uint32_t msg_len) {
for (uint32_t i = 0; i < num_blocks; i++) { for (uint32_t i = 0; i < num_blocks; i++) {
aes128_ecb(buffer, g_counter); aes128_ecb(buffer, g_counter);
aes128_ctr_evolve_counter(); aes128_ctr_evolve_counter();
block_xor(dest+(i*16), msg+(i*16), buffer); block_xor(dest + (i * 16), msg + (i * 16), buffer);
} }
if (msg_len % 16) { if (msg_len % 16) {
aes128_ecb(buffer, g_counter); aes128_ecb(buffer, g_counter);
for (uint8_t i = 0; i < msg_len % 16; i++) { for (uint8_t i = 0; i < msg_len % 16; i++) {
dest[num_blocks*16+i] = msg[num_blocks*16+i] ^ buffer[i]; dest[num_blocks * 16 + i] = msg[num_blocks * 16 + i] ^ buffer[i];
} }
} }
} }

1
ctr.h
View File

@ -4,4 +4,3 @@
void aes128_ctr(uint8_t *, uint8_t *, uint32_t); void aes128_ctr(uint8_t *, uint8_t *, uint32_t);
void aes128_ctr_init(uint8_t *, uint8_t *); void aes128_ctr_init(uint8_t *, uint8_t *);

18
eax.c
View File

@ -16,24 +16,22 @@ void aes128_eax_init(uint8_t *key, uint8_t *nonce) {
return; return;
} }
static void eax_omac(uint8_t *dest, uint8_t *msg, uint32_t msg_len, uint32_t t) { static void eax_omac(uint8_t *dest, uint8_t *msg, uint32_t msg_len,
uint8_t buf[msg_len+16]; uint32_t t) {
memset(buf, 0, msg_len+16); uint8_t buf[msg_len + 16];
memset(buf, 0, msg_len + 16);
buf[15] = (uint8_t)t; buf[15] = (uint8_t)t;
buf[14] = (uint8_t)t >> 8; buf[14] = (uint8_t)t >> 8;
buf[13] = (uint8_t)t >> 16; buf[13] = (uint8_t)t >> 16;
buf[12] = (uint8_t)t >> 24; buf[12] = (uint8_t)t >> 24;
memcpy(buf+16, msg, msg_len); memcpy(buf + 16, msg, msg_len);
cmac_aes128(dest, buf, msg_len+16, 16); cmac_aes128(dest, buf, msg_len + 16, 16);
return; return;
} }
void aes128_eax(uint8_t *dest, uint8_t *header, uint32_t header_len, 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 *msg, uint32_t msg_len, uint_fast8_t tag_len) {
uint8_t nonce_cmac[16], uint8_t nonce_cmac[16], header_cmac[16], ciphertext_cmac[16],
header_cmac[16],
ciphertext_cmac[16],
ciphertext[msg_len]; ciphertext[msg_len];
eax_omac(nonce_cmac, g_nonce, 16, 0); eax_omac(nonce_cmac, g_nonce, 16, 0);
@ -44,6 +42,6 @@ void aes128_eax(uint8_t *dest, uint8_t *header, uint32_t header_len,
block_xor(nonce_cmac, nonce_cmac, header_cmac); block_xor(nonce_cmac, nonce_cmac, header_cmac);
block_xor(nonce_cmac, nonce_cmac, ciphertext_cmac); block_xor(nonce_cmac, nonce_cmac, ciphertext_cmac);
memcpy(dest, ciphertext, msg_len); memcpy(dest, ciphertext, msg_len);
memcpy(dest+msg_len, nonce_cmac, tag_len); memcpy(dest + msg_len, nonce_cmac, tag_len);
return; return;
} }

3
eax.h
View File

@ -1,5 +1,6 @@
#pragma once #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 aes128_eax_init(uint8_t *, uint8_t *);
void eax_dump_state(void); void eax_dump_state(void);

31
main.c
View File

@ -32,7 +32,9 @@ void test_cmac(void) {
cmac_aes128(tag, msg1, 0, 16); cmac_aes128(tag, msg1, 0, 16);
if (!block_eq(tag, case1)) { if (!block_eq(tag, case1)) {
ERROR_LED_ON; ERROR_LED_ON;
while(1); while (1) {
;
}
} }
/* 16b example */ /* 16b example */
@ -43,7 +45,9 @@ void test_cmac(void) {
cmac_aes128(tag, msg2, sizeof(msg2), 16); cmac_aes128(tag, msg2, sizeof(msg2), 16);
if (!block_eq(tag, case2)) { if (!block_eq(tag, case2)) {
ERROR_LED_ON; ERROR_LED_ON;
while(1); while (1) {
;
}
} }
/* 40b example */ /* 40b example */
@ -57,24 +61,27 @@ void test_cmac(void) {
cmac_aes128(tag, msg3, sizeof(msg3), 16); cmac_aes128(tag, msg3, sizeof(msg3), 16);
if (!block_eq(tag, case3)) { if (!block_eq(tag, case3)) {
ERROR_LED_ON; ERROR_LED_ON;
while(1); while (1) {
;
}
} }
/* 64b example */ /* 64b example */
uint8_t msg4[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, uint8_t msg4[] = {
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03,
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19,
0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, 0x1a, 0x0a, 0x52, 0xef, 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
uint8_t case4[] = {0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92, uint8_t case4[] = {0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe}; 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe};
cmac_aes128(tag, msg4, sizeof(msg4), 16); cmac_aes128(tag, msg4, sizeof(msg4), 16);
if (!block_eq(tag, case4)) { if (!block_eq(tag, case4)) {
ERROR_LED_ON; ERROR_LED_ON;
while(1); while (1) {
;
}
} }
} }

5
rng.c
View File

@ -4,7 +4,7 @@
uint8_t rng_byte(void) { uint8_t rng_byte(void) {
NRF_RNG->TASKS_START = 1; NRF_RNG->TASKS_START = 1;
while(!NRF_RNG->EVENTS_VALRDY) { while (!NRF_RNG->EVENTS_VALRDY) {
/* Waiting for hardware */ /* Waiting for hardware */
} }
uint8_t val = (uint8_t)NRF_RNG->VALUE; uint8_t val = (uint8_t)NRF_RNG->VALUE;
@ -16,7 +16,8 @@ uint8_t rng_byte(void) {
void rng_bytes(uint8_t *out, uint32_t num_bytes) { void rng_bytes(uint8_t *out, uint32_t num_bytes) {
NRF_RNG->TASKS_START = 1; NRF_RNG->TASKS_START = 1;
for (uint32_t i = 0; i < num_bytes; i++) { 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; out[i] = (uint8_t)NRF_RNG->VALUE;
NRF_RNG->EVENTS_VALRDY = 0; NRF_RNG->EVENTS_VALRDY = 0;
} }

10
scripts/cleanup-format.sh Executable file
View File

@ -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}

View File

@ -78,12 +78,12 @@ debug-gdbinit:
.PHONY: flash flash-softdevice erase-all startdebug .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: 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 ./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 ./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 ./tests/test_eax