sandbox / mbedtls

Fork of mbedtls by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers entropy_hardware_poll.c Source File

entropy_hardware_poll.c

00001 /*
00002  *  Temporary "entropy" collector for Cortex-M4
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 /*
00023  * WARNING: this is a temporary hack!
00024  * 1. Currently does not provide strong entropy, should be replaced to use the
00025  * on-board hardware RNG (see IOTSSL-303)
00026  * 2. This should be in a separete yotta module which would be a target
00027  * dependency of mbedtls (see IOTSSL-313)
00028  */
00029 
00030 #if defined(TARGET_LIKE_CORTEX_M4)
00031 
00032 #include "MK64F12.h"
00033 #include "core_cm4.h"
00034 #include <string.h>
00035 
00036 unsigned long hardclock( void )
00037 {
00038     static int dwt_started = 0;
00039 
00040     if( dwt_started == 0 )
00041     {
00042         CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
00043         DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
00044     }
00045 
00046     return( DWT->CYCCNT );
00047 }
00048 
00049 int mbedtls_hardware_poll( void *data,
00050                     unsigned char *output, size_t len, size_t *olen )
00051 {
00052     unsigned long timer = hardclock();
00053     ((void) data);
00054     *olen = 0;
00055 
00056     if( len < sizeof(unsigned long) )
00057         return( 0 );
00058 
00059     memcpy( output, &timer, sizeof(unsigned long) );
00060     *olen = sizeof(unsigned long);
00061 
00062     return( 0 );
00063 }
00064 
00065 #endif