diff options
Diffstat (limited to 'src/cc20rng')
-rw-r--r-- | src/cc20rng/.clang-format | 4 | ||||
-rw-r--r-- | src/cc20rng/Makefile | 4 | ||||
-rw-r--r-- | src/cc20rng/cc20_prng.c | 87 | ||||
-rw-r--r-- | src/cc20rng/cc20_prng.h | 9 | ||||
-rw-r--r-- | src/cc20rng/main.c | 118 |
5 files changed, 111 insertions, 111 deletions
diff --git a/src/cc20rng/.clang-format b/src/cc20rng/.clang-format new file mode 100644 index 0000000..8367454 --- /dev/null +++ b/src/cc20rng/.clang-format @@ -0,0 +1,4 @@ +IndentWidth: 4 +BreakBeforeBraces: Custom +BraceWrapping: + AfterFunction: true diff --git a/src/cc20rng/Makefile b/src/cc20rng/Makefile index 2873505..6c274be 100644 --- a/src/cc20rng/Makefile +++ b/src/cc20rng/Makefile @@ -29,6 +29,10 @@ $(PROJ_NAME).elf: $(SRCS) $(OBJDUMP) -St $(PROJ_NAME).elf >$(PROJ_NAME).lst $(SIZE) $(PROJ_NAME).elf +c-format: + clang-format-4.0 -i cc20_prng.[ch] + clang-format-4.0 -style="{IndentWidth: 2}" -i main.c + clean: find ./ -name '*~' | xargs rm -f rm -f *.o diff --git a/src/cc20rng/cc20_prng.c b/src/cc20rng/cc20_prng.c index 3d52fc6..50739bd 100644 --- a/src/cc20rng/cc20_prng.c +++ b/src/cc20rng/cc20_prng.c @@ -44,12 +44,13 @@ void _dump(struct cc20_state *cc, char *str); #endif -inline uint32_t rotl32 (uint32_t x, uint32_t n) +inline uint32_t rotl32(uint32_t x, uint32_t n) { return (x << n) | (x >> (32 - n)); } -inline void _qr(struct cc20_state *cc, uint32_t a, uint32_t b, uint32_t c, uint32_t d) +inline void _qr(struct cc20_state *cc, uint32_t a, uint32_t b, uint32_t c, + uint32_t d) { cc->i[a] += cc->i[b]; cc->i[d] ^= cc->i[a]; @@ -72,11 +73,12 @@ void chacha20_prng_reseed(struct cc20_state *cc, uint32_t *entropy) { uint32_t i = 256 / 8 / 4; while (i--) { - cc->i[i] = entropy[i]; + cc->i[i] = entropy[i]; } } -void chacha20_prng_block(struct cc20_state *cc, uint32_t block_counter, struct cc20_state *out) +void chacha20_prng_block(struct cc20_state *cc, uint32_t block_counter, + struct cc20_state *out) { uint32_t i; @@ -88,22 +90,22 @@ void chacha20_prng_block(struct cc20_state *cc, uint32_t block_counter, struct c cc->i[12] = block_counter; for (i = 4; i < CHACHA20_NUM_WORDS; i++) { - out->i[i] = cc->i[i]; + out->i[i] = cc->i[i]; } for (i = 10; i; i--) { - _qr(out, 0, 4, 8,12); - _qr(out, 1, 5, 9,13); - _qr(out, 2, 6,10,14); - _qr(out, 3, 7,11,15); - _qr(out, 0, 5,10,15); - _qr(out, 1, 6,11,12); - _qr(out, 2, 7, 8,13); - _qr(out, 3, 4, 9,14); + _qr(out, 0, 4, 8, 12); + _qr(out, 1, 5, 9, 13); + _qr(out, 2, 6, 10, 14); + _qr(out, 3, 7, 11, 15); + _qr(out, 0, 5, 10, 15); + _qr(out, 1, 6, 11, 12); + _qr(out, 2, 7, 8, 13); + _qr(out, 3, 4, 9, 14); } for (i = 0; i < CHACHA20_NUM_WORDS; i++) { - out->i[i] += cc->i[i]; + out->i[i] += cc->i[i]; } } @@ -119,11 +121,11 @@ int chacha20_prng_self_test1() 0x00000001, 0x09000000, 0x4a000000, 0x00000000, }}; struct cc20_state expected = { - {0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, - 0xc7f4d1c7, 0x0368c033, 0x9aaa2204, 0x4e6cd4c3, - 0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9, - 0xd19c12b5, 0xb94e16de, 0xe883d0cb, 0x4e3c50a2, - }}; + {0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, + 0xc7f4d1c7, 0x0368c033, 0x9aaa2204, 0x4e6cd4c3, + 0x466482d2, 0x09aa9f07, 0x05d7c214, 0xa2028bd9, + 0xd19c12b5, 0xb94e16de, 0xe883d0cb, 0x4e3c50a2, + }}; uint32_t i; struct cc20_state out; @@ -137,7 +139,8 @@ int chacha20_prng_self_test1() #endif for (i = 0; i < CHACHA20_NUM_WORDS; i++) { - if (out.i[i] != expected.i[i]) return 0; + if (out.i[i] != expected.i[i]) + return 0; } return 1; @@ -149,23 +152,23 @@ int chacha20_prng_self_test2() * https://tools.ietf.org/html/rfc7539#section-2.4.2 */ struct cc20_state test = { - {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574, - 0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, - 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c, - 0x00000001, 0x00000000, 0x4a000000, 0x00000000, - }}; + {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574, + 0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, + 0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c, + 0x00000001, 0x00000000, 0x4a000000, 0x00000000, + }}; struct cc20_state expected1 = { - {0xf3514f22, 0xe1d91b40, 0x6f27de2f, 0xed1d63b8, - 0x821f138c, 0xe2062c3d, 0xecca4f7e, 0x78cff39e, - 0xa30a3b8a, 0x920a6072, 0xcd7479b5, 0x34932bed, - 0x40ba4c79, 0xcd343ec6, 0x4c2c21ea, 0xb7417df0, - }}; + {0xf3514f22, 0xe1d91b40, 0x6f27de2f, 0xed1d63b8, + 0x821f138c, 0xe2062c3d, 0xecca4f7e, 0x78cff39e, + 0xa30a3b8a, 0x920a6072, 0xcd7479b5, 0x34932bed, + 0x40ba4c79, 0xcd343ec6, 0x4c2c21ea, 0xb7417df0, + }}; struct cc20_state expected2 = { - {0x9f74a669, 0x410f633f, 0x28feca22, 0x7ec44dec, - 0x6d34d426, 0x738cb970, 0x3ac5e9f3, 0x45590cc4, - 0xda6e8b39, 0x892c831a, 0xcdea67c1, 0x2b7e1d90, - 0x037463f3, 0xa11a2073, 0xe8bcfb88, 0xedc49139, - }}; + {0x9f74a669, 0x410f633f, 0x28feca22, 0x7ec44dec, + 0x6d34d426, 0x738cb970, 0x3ac5e9f3, 0x45590cc4, + 0xda6e8b39, 0x892c831a, 0xcdea67c1, 0x2b7e1d90, + 0x037463f3, 0xa11a2073, 0xe8bcfb88, 0xedc49139, + }}; struct cc20_state out; uint32_t i; @@ -178,7 +181,8 @@ int chacha20_prng_self_test2() _dump(&out, "First block"); #endif for (i = 0; i < CHACHA20_NUM_WORDS; i++) { - if (out.i[i] != expected1.i[i]) return 0; + if (out.i[i] != expected1.i[i]) + return 0; } chacha20_prng_block(&test, 2, &out); @@ -186,7 +190,8 @@ int chacha20_prng_self_test2() _dump(&out, "Second block"); #endif for (i = 0; i < CHACHA20_NUM_WORDS; i++) { - if (out.i[i] != expected2.i[i]) return 0; + if (out.i[i] != expected2.i[i]) + return 0; } return 1; @@ -201,8 +206,8 @@ void _dump(struct cc20_state *cc, char *str) printf("%s\n", str); for (i = 0; i < 4; i++) { - printf("%02i %08x %08x %08x %08x\n", i * 4, cc->i[i * 4 + 0], - cc->i[i * 4 + 1], cc->i[i * 4 + 2], cc->i[i * 4 + 3]); + printf("%02i %08x %08x %08x %08x\n", i * 4, cc->i[i * 4 + 0], + cc->i[i * 4 + 1], cc->i[i * 4 + 2], cc->i[i * 4 + 3]); } printf("\n"); } @@ -213,7 +218,5 @@ void _dump(struct cc20_state *cc, char *str) */ int chacha20_prng_self_test() { - return \ - chacha20_prng_self_test1() && \ - chacha20_prng_self_test2(); + return chacha20_prng_self_test1() && chacha20_prng_self_test2(); } diff --git a/src/cc20rng/cc20_prng.h b/src/cc20rng/cc20_prng.h index 6940f53..7b597d0 100644 --- a/src/cc20rng/cc20_prng.h +++ b/src/cc20rng/cc20_prng.h @@ -3,16 +3,17 @@ #include <stdint.h> -#define CHACHA20_MAX_BLOCK_COUNTER 0xffffffff -#define CHACHA20_NUM_WORDS 16 -#define CHACHA20_BLOCK_SIZE (CHACHA20_NUM_WORDS * 4) +#define CHACHA20_MAX_BLOCK_COUNTER 0xffffffff +#define CHACHA20_NUM_WORDS 16 +#define CHACHA20_BLOCK_SIZE (CHACHA20_NUM_WORDS * 4) struct cc20_state { uint32_t i[CHACHA20_NUM_WORDS]; }; extern void chacha20_prng_reseed(struct cc20_state *cc, uint32_t *entropy); -extern void chacha20_prng_block(struct cc20_state *cc, uint32_t block_counter, struct cc20_state *out); +extern void chacha20_prng_block(struct cc20_state *cc, uint32_t block_counter, + struct cc20_state *out); extern int chacha20_prng_self_test(); #endif /* __STM32_CHACHA20_H */ diff --git a/src/cc20rng/main.c b/src/cc20rng/main.c index e478409..88b5144 100644 --- a/src/cc20rng/main.c +++ b/src/cc20rng/main.c @@ -31,12 +31,12 @@ */ #include <string.h> +#include "cc20_prng.h" #include "main.h" #include "stm_init.h" -#include "cc20_prng.h" -#define UART_RANDOM_BYTES_PER_CHUNK 8 -#define RESEED_BLOCKS CHACHA20_MAX_BLOCK_COUNTER +#define UART_RANDOM_BYTES_PER_CHUNK 8 +#define RESEED_BLOCKS CHACHA20_MAX_BLOCK_COUNTER extern DMA_HandleTypeDef hdma_tim; @@ -44,19 +44,22 @@ static UART_HandleTypeDef *huart; static __IO ITStatus UartReady = RESET; static union { - uint8_t rnd[257]; /* 256 bytes + 1 for use in the POST */ + uint8_t rnd[257]; /* 256 bytes + 1 for use in the POST */ uint32_t rnd32[64]; } buf; -/* First DMA value (DMA_counters[0]) is unreliable, leftover in DMA FIFO perhaps? */ -#define FIRST_DMA_IDX_USED 3 +/* First DMA value (DMA_counters[0]) is unreliable, leftover in DMA FIFO + * perhaps? */ +#define FIRST_DMA_IDX_USED 3 /* * Number of counters used to produce 8 bits of entropy is: - * 8 * 4 - four flanks are used to produce two (hopefully) uncorrelated bits (a and b) + * 8 * 4 - four flanks are used to produce two (hopefully) uncorrelated bits + * (a and b) * * 2 - von Neumann will on average discard 1/2 of the bits 'a' and 'b' */ -#define DMA_COUNTERS_NUM ((UART_RANDOM_BYTES_PER_CHUNK * 8 * 4 * 2) + FIRST_DMA_IDX_USED + 1) +#define DMA_COUNTERS_NUM \ + ((UART_RANDOM_BYTES_PER_CHUNK * 8 * 4 * 2) + FIRST_DMA_IDX_USED + 1) struct DMA_params { volatile uint32_t buf0[DMA_COUNTERS_NUM]; volatile uint32_t buf1[DMA_COUNTERS_NUM]; @@ -64,28 +67,23 @@ struct DMA_params { }; static struct DMA_params DMA = { - {}, - {}, - 0, + {}, {}, 0, }; - /* The main work horse functions */ static void get_entropy32(uint32_t num_bytes, uint32_t buf_idx); /* Various support functions */ static inline uint32_t get_one_bit(void) __attribute__((__always_inline__)); static volatile uint32_t *restart_DMA(void); static inline volatile uint32_t *get_DMA_read_buf(void); -static inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, const uint32_t dmabuf_idx); +static inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, + const uint32_t dmabuf_idx); static void check_uart_rx(UART_HandleTypeDef *this); static void cc_reseed(struct cc20_state *cc); void Error_Handler(void); - -int -main() -{ +int main() { uint32_t i, timeout, block_counter = 0; struct cc20_state cc, out; HAL_StatusTypeDef res; @@ -97,10 +95,10 @@ main() DMA.buf1[i] = 0xffff0100 + i; } - stm_init((uint32_t *) &DMA.buf0, DMA_COUNTERS_NUM); + stm_init((uint32_t *)&DMA.buf0, DMA_COUNTERS_NUM); - if (! chacha20_prng_self_test()) { - Error_Handler(); + if (!chacha20_prng_self_test()) { + Error_Handler(); } /* Ensure there is actual Timer IC counters in both DMA buffers. */ @@ -110,29 +108,25 @@ main() huart = &huart1; /* Toggle GREEN LED to show we've initialized */ - { - for (i = 0; i < 10; i++) { - HAL_GPIO_TogglePin(LED_PORT, LED_GREEN); - HAL_Delay(125); - } + for (i = 0; i < 10; i++) { + HAL_GPIO_TogglePin(LED_PORT, LED_GREEN); + HAL_Delay(125); } /* Generate initial block of random data directly into buf */ cc_reseed(&cc); block_counter = RESEED_BLOCKS; - chacha20_prng_block(&cc, block_counter--, (struct cc20_state *) buf.rnd32); + chacha20_prng_block(&cc, block_counter--, (struct cc20_state *)buf.rnd32); - /* - * Main loop - */ + /* Main loop */ while (1) { - if (! (block_counter % 1000)) { + if (!(block_counter % 1000)) { HAL_GPIO_TogglePin(LED_PORT, LED_YELLOW); } - if (! block_counter) { - cc_reseed(&cc); - block_counter = RESEED_BLOCKS; + if (!block_counter) { + cc_reseed(&cc); + block_counter = RESEED_BLOCKS; } /* Send buf on UART (non blocking interrupt driven send). */ @@ -143,12 +137,14 @@ main() chacha20_prng_block(&cc, block_counter--, &out); /* Copying using a loop is faster than memcpy on STM32 */ for (i = 0; i < CHACHA20_NUM_WORDS; i++) { - buf.rnd32[i] = out.i[i]; + buf.rnd32[i] = out.i[i]; } if (res == HAL_OK) { timeout = 0xffff; - while (UartReady != SET && timeout) { timeout--; } + while (UartReady != SET && timeout) { + timeout--; + } } if (UartReady != SET) { @@ -164,22 +160,19 @@ main() } } - /** * @brief Reseed chacha20 state with hardware generated entropy. * @param cc: ChaCha20 state * @retval None */ -static void -cc_reseed(struct cc20_state *cc) -{ - HAL_GPIO_WritePin(LED_PORT, LED_BLUE, GPIO_PIN_SET); +static void cc_reseed(struct cc20_state *cc) { + HAL_GPIO_WritePin(LED_PORT, LED_BLUE, GPIO_PIN_SET); - get_entropy32(CHACHA20_BLOCK_SIZE / 4, 0); - restart_DMA(); - chacha20_prng_reseed(cc, (uint32_t *) &buf); + get_entropy32(CHACHA20_BLOCK_SIZE / 4, 0); + restart_DMA(); + chacha20_prng_reseed(cc, (uint32_t *)&buf); - HAL_GPIO_WritePin(LED_PORT, LED_BLUE, GPIO_PIN_RESET); + HAL_GPIO_WritePin(LED_PORT, LED_BLUE, GPIO_PIN_RESET); } /** @@ -188,16 +181,14 @@ cc_reseed(struct cc20_state *cc) * @param start: Start index value into buf.rnd32. * @retval None */ -static inline void get_entropy32(uint32_t count, const uint32_t start) -{ +static inline void get_entropy32(uint32_t count, const uint32_t start) { uint32_t i, bits, buf_idx; buf_idx = start; do { bits = 0; - /* Get 32 bits of entropy. - */ + /* Get 32 bits of entropy. */ for (i = 32; i; i--) { bits <<= 1; bits += get_one_bit(); @@ -213,8 +204,7 @@ static inline void get_entropy32(uint32_t count, const uint32_t start) * @param None * @retval One bit, in the LSB of an uint32_t since this is a 32 bit MCU. */ -static inline uint32_t get_one_bit() -{ +static inline uint32_t get_one_bit() { register uint32_t a, b, temp; /* Start at end of buffer so restart_DMA() is called. */ static uint32_t dmabuf_idx = DMA_COUNTERS_NUM - 1; @@ -264,8 +254,7 @@ static inline uint32_t get_one_bit() * @param None * @retval Pointer to buffer currently being read from. */ -static inline volatile uint32_t *get_DMA_read_buf(void) -{ +static inline volatile uint32_t *get_DMA_read_buf(void) { return DMA.write_buf ? DMA.buf0 : DMA.buf1; } @@ -274,8 +263,7 @@ static inline volatile uint32_t *get_DMA_read_buf(void) * @param None * @retval Pointer to buffer currently being written to. */ -static inline volatile uint32_t *get_DMA_write_buf(void) -{ +static inline volatile uint32_t *get_DMA_write_buf(void) { return DMA.write_buf ? DMA.buf1 : DMA.buf0; } @@ -284,16 +272,17 @@ static inline volatile uint32_t *get_DMA_write_buf(void) * @param None * @retval Pointer to buffer full of Timer IC values ready to be consumed. */ -static volatile uint32_t *restart_DMA(void) -{ +static volatile uint32_t *restart_DMA(void) { /* Wait for transfer complete flag to become SET. Trying to change the * M0AR register while the DMA is running is a no-no. */ - while(__HAL_DMA_GET_FLAG(&hdma_tim, __HAL_DMA_GET_TC_FLAG_INDEX(&hdma_tim)) == RESET) { ; } + while (__HAL_DMA_GET_FLAG(&hdma_tim, + __HAL_DMA_GET_TC_FLAG_INDEX(&hdma_tim)) == RESET) + ; /* Switch buffer being written to */ DMA.write_buf ^= 1; - hdma_tim.Instance->M0AR = (uint32_t) get_DMA_write_buf(); + hdma_tim.Instance->M0AR = (uint32_t)get_DMA_write_buf(); /* Start at 0 to help manual inspection */ TIM2->CNT = 0; @@ -311,7 +300,8 @@ static volatile uint32_t *restart_DMA(void) * @param dmabuf_idx: Word index into `dmabuf'. * @retval One Timer IC counter value. */ -static inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, const uint32_t dmabuf_idx) { +static inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, + const uint32_t dmabuf_idx) { register uint32_t a; /* Prevent re-use of values. DMA stored values are <= 0xffff. */ do { @@ -322,8 +312,7 @@ static inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, const uint32_ } /* UART transmit complete callback */ -void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UH) -{ +void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UH) { if ((UH->Instance == USART1 && huart->Instance == USART1) || (UH->Instance == USART2 && huart->Instance == USART2)) { /* Signal UART transmit complete to the code in the main loop. */ @@ -350,10 +339,9 @@ static void check_uart_rx(UART_HandleTypeDef *this) { * @param None * @retval None */ - -void Error_Handler(void) -{ +void Error_Handler(void) { /* Turn on RED LED and then loop indefinitely */ HAL_GPIO_WritePin(LED_PORT, LED_RED, GPIO_PIN_SET); - while(1) { ; } + while (1) + ; } |