Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Temporary "entropy" collector for Cortex-M4 |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
| 5 | * |
| 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * WARNING: this is a temporary hack! |
| 11 | * 1. Currently does not provide strong entropy, should be replaced to use the |
| 12 | * on-board hardware RNG (see IOTSSL-303) |
| 13 | * 2. This should be in a separete yotta module which would be a target |
| 14 | * dependency of mbedtls (see IOTSSL-313) |
| 15 | */ |
| 16 | |
| 17 | #if defined(TARGET_LIKE_CORTEX_M4) |
| 18 | |
| 19 | #include "MK64F12.h" |
| 20 | #include "core_cm4.h" |
| 21 | #include <string.h> |
| 22 | |
| 23 | unsigned long hardclock( void ) |
| 24 | { |
| 25 | static int dwt_started = 0; |
| 26 | |
| 27 | if( dwt_started == 0 ) |
| 28 | { |
| 29 | CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
| 30 | DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; |
| 31 | } |
| 32 | |
| 33 | return( DWT->CYCCNT ); |
| 34 | } |
| 35 | |
| 36 | int mbedtls_hardware_poll( void *data, |
| 37 | unsigned char *output, size_t len, size_t *olen ) |
| 38 | { |
| 39 | unsigned long timer = hardclock(); |
| 40 | ((void) data); |
| 41 | *olen = 0; |
| 42 | |
| 43 | if( len < sizeof(unsigned long) ) |
| 44 | return( 0 ); |
| 45 | |
| 46 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 47 | *olen = sizeof(unsigned long); |
| 48 | |
| 49 | return( 0 ); |
| 50 | } |
| 51 | |
| 52 | #endif |