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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * WARNING: this is a temporary hack! |
| 24 | * 1. Currently does not provide strong entropy, should be replaced to use the |
| 25 | * on-board hardware RNG (see IOTSSL-303) |
| 26 | * 2. This should be in a separete yotta module which would be a target |
| 27 | * dependency of mbedtls (see IOTSSL-313) |
| 28 | */ |
| 29 | |
| 30 | #if defined(TARGET_LIKE_CORTEX_M4) |
| 31 | |
| 32 | #include "MK64F12.h" |
| 33 | #include "core_cm4.h" |
| 34 | #include <string.h> |
| 35 | |
| 36 | unsigned long hardclock( void ) |
| 37 | { |
| 38 | static int dwt_started = 0; |
| 39 | |
| 40 | if( dwt_started == 0 ) |
| 41 | { |
| 42 | CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
| 43 | DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; |
| 44 | } |
| 45 | |
| 46 | return( DWT->CYCCNT ); |
| 47 | } |
| 48 | |
| 49 | int mbedtls_hardware_poll( void *data, |
| 50 | unsigned char *output, size_t len, size_t *olen ) |
| 51 | { |
| 52 | unsigned long timer = hardclock(); |
| 53 | ((void) data); |
| 54 | *olen = 0; |
| 55 | |
| 56 | if( len < sizeof(unsigned long) ) |
| 57 | return( 0 ); |
| 58 | |
| 59 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 60 | *olen = sizeof(unsigned long); |
| 61 | |
| 62 | return( 0 ); |
| 63 | } |
| 64 | |
| 65 | #endif |