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

3
aes.c
View File

@ -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);

1
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);

View File

@ -9,8 +9,7 @@
#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 {
@ -26,8 +25,8 @@ 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]);

View File

@ -13,4 +13,3 @@ 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);

16
cmac.c
View File

@ -11,8 +11,7 @@
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) {
@ -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);
@ -81,8 +81,7 @@ 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];
@ -92,4 +91,3 @@ void cmac_aes128(uint8_t *dest, uint8_t *msg, size_t msg_len, uint_fast8_t tag_l
cmac_truncate(dest, x, tag_len); cmac_truncate(dest, x, tag_len);
return; return;
} }

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 *);

8
eax.c
View File

@ -16,7 +16,8 @@ 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,
uint32_t t) {
uint8_t buf[msg_len + 16]; uint8_t buf[msg_len + 16];
memset(buf, 0, msg_len + 16); memset(buf, 0, msg_len + 16);
buf[15] = (uint8_t)t; buf[15] = (uint8_t)t;
@ -28,12 +29,9 @@ static void eax_omac(uint8_t *dest, uint8_t *msg, uint32_t msg_len, uint32_t t)
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);

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) {
;
}
} }
} }

3
rng.c
View File

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