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

33
aes.c
View File

@ -12,32 +12,33 @@
static ecbdata_t g_ecbdata; static ecbdata_t g_ecbdata;
void aes128_init(uint8_t *key) { void aes128_init(uint8_t *key) {
if (key != NULL) { if (key != NULL) {
memmove(g_ecbdata.key, key, 16); memmove(g_ecbdata.key, key, 16);
#ifndef HOST_BUILD #ifndef HOST_BUILD
NRF_ECB->ECBDATAPTR = (uint32_t)&g_ecbdata; NRF_ECB->ECBDATAPTR = (uint32_t)&g_ecbdata;
#endif /* HOST_BUILD */ #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 #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);
AES_encrypt(in, g_ecbdata.out, &key); AES_encrypt(in, g_ecbdata.out, &key);
#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);
return; return;
} }
#ifdef HOST_BUILD #ifdef HOST_BUILD
void aes_dump_state(void) { void aes_dump_state(void) {
block_print("Key", g_ecbdata.key); block_print("Key", g_ecbdata.key);
} }
#endif /* HOST_BUILD */ #endif /* HOST_BUILD */

9
aes.h
View File

@ -5,12 +5,11 @@
#include <stdint.h> #include <stdint.h>
typedef struct { typedef struct {
uint8_t key[16]; uint8_t key[16];
uint8_t in[16]; uint8_t in[16];
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);

135
block.c
View File

@ -9,92 +9,91 @@
#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++) {
printf("%.2x", b[i]);
if (!((i+1) % 4)) {
printf(" ");
} }
} for (int i = 0; i < 16; i++) {
printf("\n"); printf("%.2x", b[i]);
return; if (!((i + 1) % 4)) {
printf(" ");
}
}
printf("\n");
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(" ");
}
} }
} printf("\n");
printf("\n");
} }
#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) { block_shiftr(dest, dest, num - n);
block_shiftr(dest, dest, num - n); }
} 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) {
/* No overflow, changes propagate no further */ /* No overflow, changes propagate no further */
break; break;
}
} }
} return;
return;
} }
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) {
/* No overflow, changes propagate no further */ /* No overflow, changes propagate no further */
break; break;
}
} }
} return;
return;
} }

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

122
cmac.c
View File

@ -11,85 +11,83 @@
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 */
void cmac_aes128_init(uint8_t *key) { void cmac_aes128_init(uint8_t *key) {
/* Initialize AES engine and cache subkeys */ /* Initialize AES engine and cache subkeys */
if (key != NULL) { if (key != NULL) {
aes128_init(key); aes128_init(key);
cmac_aes128_expand_key(key, g_k1, g_k2); 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) { void cmac_aes128_expand_key(uint8_t const *const key, uint8_t *k1,
/* Generate two required subkeys according to NIST 800-38B */ uint8_t *k2) {
uint8_t l[16] = {0}, /* Generate two required subkeys according to NIST 800-38B */
Rb[16] = {0, 0, 0, 0, 0, 0, 0, 0, uint8_t l[16] = {0},
0, 0, 0, 0, 0, 0, 0, 0x87}; Rb[16] = {0, 0, 0, 0, 0, 0, 0, 0, 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) {
block_shiftl(k1, l, 1); block_shiftl(k1, l, 1);
} else { } else {
uint8_t tmp[16]; uint8_t tmp[16];
block_shiftl(tmp, l, 1); block_shiftl(tmp, l, 1);
block_xor(k1, tmp, Rb); block_xor(k1, tmp, Rb);
} }
if (!(k1[0] >> 7)) { if (!(k1[0] >> 7)) {
block_shiftl(k2, k1, 1); block_shiftl(k2, k1, 1);
} else { } else {
uint8_t tmp[16]; uint8_t tmp[16];
block_shiftl(tmp, k1, 1); block_shiftl(tmp, k1, 1);
block_xor(k2, tmp, Rb); block_xor(k2, tmp, Rb);
} }
} }
static void cmac_truncate(uint8_t *dest, uint8_t *tag, uint_fast8_t tag_len) { 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,
/* Simulate ceiling integer division by adding a block if remainder */ uint_fast8_t tag_len) {
size_t num_blocks = msg_len / 16 + (msg_len % 16 ? 1 : 0); /* Simulate ceiling integer division by adding a block if remainder */
bool last_block_complete = !(msg_len % 16 ? 1 : 0); size_t num_blocks = msg_len / 16 + (msg_len % 16 ? 1 : 0);
if (msg_len == 0) { bool last_block_complete = !(msg_len % 16 ? 1 : 0);
num_blocks = 1; if (msg_len == 0) {
last_block_complete = false; num_blocks = 1;
} 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) {
/* Padding is single 1 bit, run out on 0s.. find the next byte, /* Padding is single 1 bit, run out on 0s.. find the next byte,
set it to 0b1000000 */ set it to 0b1000000 */
alt_msg[msg_len] = 0x80; alt_msg[msg_len] = 0x80;
block_xor(last_block, last_block, g_k2); block_xor(last_block, last_block, g_k2);
} else { } else {
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 *);

36
ctr.c
View File

@ -6,29 +6,29 @@
static uint8_t g_counter[16]; static uint8_t g_counter[16];
void aes128_ctr_init(uint8_t *key, uint8_t *counter) { void aes128_ctr_init(uint8_t *key, uint8_t *counter) {
if (counter != NULL) { if (counter != NULL) {
memcpy(g_counter, counter, 16); memcpy(g_counter, counter, 16);
} }
aes128_init(key); aes128_init(key);
} }
void aes128_ctr_evolve_counter(void) { void aes128_ctr_evolve_counter(void) {
block_incr(g_counter); block_incr(g_counter);
return; return;
} }
void aes128_ctr(uint8_t *dest, uint8_t *msg, uint32_t msg_len) { void aes128_ctr(uint8_t *dest, uint8_t *msg, uint32_t msg_len) {
uint8_t buffer[16]; uint8_t buffer[16];
uint32_t num_blocks = msg_len / 16; uint32_t num_blocks = msg_len / 16;
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 *);

62
eax.c
View File

@ -8,42 +8,40 @@
static uint8_t g_nonce[16] = {0}; static uint8_t g_nonce[16] = {0};
void aes128_eax_init(uint8_t *key, uint8_t *nonce) { void aes128_eax_init(uint8_t *key, uint8_t *nonce) {
if (nonce != NULL) { if (nonce != NULL) {
memcpy(g_nonce, nonce, 16); memcpy(g_nonce, nonce, 16);
} }
cmac_aes128_init(key); cmac_aes128_init(key);
aes128_ctr_init(key, nonce); aes128_ctr_init(key, 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];
buf[15] = (uint8_t)t; memset(buf, 0, msg_len + 16);
buf[14] = (uint8_t)t >> 8; buf[15] = (uint8_t)t;
buf[13] = (uint8_t)t >> 16; buf[14] = (uint8_t)t >> 8;
buf[12] = (uint8_t)t >> 24; buf[13] = (uint8_t)t >> 16;
memcpy(buf+16, msg, msg_len); buf[12] = (uint8_t)t >> 24;
cmac_aes128(dest, buf, msg_len+16, 16); memcpy(buf + 16, msg, msg_len);
return; cmac_aes128(dest, buf, msg_len + 16, 16);
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[msg_len];
ciphertext_cmac[16],
ciphertext[msg_len];
eax_omac(nonce_cmac, g_nonce, 16, 0); eax_omac(nonce_cmac, g_nonce, 16, 0);
eax_omac(header_cmac, header, header_len, 1); eax_omac(header_cmac, header, header_len, 1);
aes128_ctr_init(NULL, nonce_cmac); aes128_ctr_init(NULL, nonce_cmac);
aes128_ctr(ciphertext, msg, msg_len); aes128_ctr(ciphertext, msg, msg_len);
eax_omac(ciphertext_cmac, ciphertext, msg_len, 2); eax_omac(ciphertext_cmac, ciphertext, msg_len, 2);
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);

127
main.c
View File

@ -12,76 +12,83 @@
#if (ERROR_LED_ACTIVE == LOW) #if (ERROR_LED_ACTIVE == LOW)
#define ERROR_LED_OFF nrf_gpio_pin_set(ERROR_LED_PIN) #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 #else
#define ERROR_LED_OFF nrf_gpio_pin_clear(ERROR_LED_PIN) #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 */ #endif /* ERROR_LET_ACTIVE */
void test_cmac(void) { void test_cmac(void) {
/* NIST Examples */ /* NIST Examples */
uint8_t k[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, uint8_t k[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
cmac_aes128_init(k); cmac_aes128_init(k);
uint8_t tag[16]; uint8_t tag[16];
/* Test Null Message, NIST Example */ /* Test Null Message, NIST Example */
uint8_t *msg1 = NULL; uint8_t *msg1 = NULL;
uint8_t case1[] = {0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, uint8_t case1[] = {0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46}; 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46};
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 */
uint8_t msg2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, uint8_t msg2[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a}; 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
uint8_t case2[] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, uint8_t case2[] = {0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c}; 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c};
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 */
uint8_t msg3[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, uint8_t msg3[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11}; 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11};
uint8_t case3[] = {0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30, uint8_t case3[] = {0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27}; 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27};
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); ;
} }
}
} }
int main(void) { int main(void) {
nrf_gpio_cfg_output(ERROR_LED_PIN); nrf_gpio_cfg_output(ERROR_LED_PIN);
ERROR_LED_OFF; ERROR_LED_OFF;
for (;;) { for (;;) {
test_cmac(); test_cmac();
} }
} }

33
rng.c
View File

@ -3,23 +3,24 @@
#include "nrf.h" #include "nrf.h"
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;
NRF_RNG->TASKS_STOP = 1; NRF_RNG->TASKS_STOP = 1;
NRF_RNG->EVENTS_VALRDY = 0; NRF_RNG->EVENTS_VALRDY = 0;
return val; return val;
} }
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; ;
NRF_RNG->EVENTS_VALRDY = 0; out[i] = (uint8_t)NRF_RNG->VALUE;
} NRF_RNG->EVENTS_VALRDY = 0;
NRF_RNG->TASKS_STOP = 1; }
return; NRF_RNG->TASKS_STOP = 1;
return;
} }

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