blob: c1355b96572429cbfb66111ba1b41085b85cd851 [file] [log] [blame]
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +02001/*
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)
Manuel Pégourié-Gonnardcdee2d92015-08-07 09:40:51 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020021 */
22
23/*
24 * WARNING: this is a temporary hack!
25 * 1. Currently does not provide strong entropy, should be replaced to use the
26 * on-board hardware RNG (see IOTSSL-303)
27 * 2. This should be in a separete yotta module which would be a target
28 * dependency of mbedtls (see IOTSSL-313)
29 */
30
31#if defined(TARGET_LIKE_CORTEX_M4)
32
33#include "MK64F12.h"
34#include "core_cm4.h"
35#include <string.h>
36
37unsigned long hardclock( void )
38{
39 static int dwt_started = 0;
40
41 if( dwt_started == 0 )
42 {
43 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
44 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
45 }
46
47 return( DWT->CYCCNT );
48}
49
50int mbedtls_hardware_poll( void *data,
51 unsigned char *output, size_t len, size_t *olen )
52{
53 unsigned long timer = hardclock();
54 ((void) data);
55 *olen = 0;
56
57 if( len < sizeof(unsigned long) )
58 return( 0 );
59
60 memcpy( output, &timer, sizeof(unsigned long) );
61 *olen = sizeof(unsigned long);
62
63 return( 0 );
64}
65
66#endif