Jan Korycan / WNCInterface

Dependencies:   WncControllerK64F

Fork of WNCInterface by Jan Korycan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers entropy_hardware_poll.c Source File

entropy_hardware_poll.c

00001 /*
00002  *  Hardware entropy collector for the K64F, using Freescale's RNGA
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 temporary!
00024  * This should be in a separate yotta module which would be a target
00025  * dependency of mbedtls (see IOTSSL-313)
00026  */
00027 
00028 #if defined(TARGET_LIKE_K64F)
00029 
00030 /*
00031  * Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34
00032  */
00033 
00034 #include "fsl_clock_manager.h"
00035 
00036 /*
00037  * Get one byte of entropy from the RNG, assuming it is up and running.
00038  * As recommended (34.1.1), get only one bit of each output.
00039  */
00040 static void rng_get_byte( unsigned char *byte )
00041 {
00042     size_t bit;
00043 
00044     /* 34.5 Steps 3-4-5: poll SR and read from OR when ready */
00045     for( bit = 0; bit < 8; bit++ )
00046     {
00047         while( ( RNG->SR & RNG_SR_OREG_LVL_MASK ) == 0 );
00048         *byte |= ( RNG->OR & 1 ) << bit;
00049     }
00050 }
00051 
00052 /*
00053  * Get len bytes of entropy from the hardware RNG.
00054  */
00055 int mbedtls_hardware_poll( void *data,
00056                     unsigned char *output, size_t len, size_t *olen )
00057 {
00058     size_t i;
00059     int ret;
00060     ((void) data);
00061 
00062     CLOCK_SYS_EnableRngaClock( 0 );
00063 
00064     /* Set "Interrupt Mask", "High Assurance" and "Go",
00065      * unset "Clear interrupt" and "Sleep" */
00066     RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
00067 
00068     for( i = 0; i < len; i++ )
00069         rng_get_byte( output + i );
00070 
00071     /* Just be extra sure that we didn't do it wrong */
00072     if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 )
00073     {
00074         ret = -1;
00075         goto cleanup;
00076     }
00077 
00078     *olen = len;
00079     ret = 0;
00080 
00081 cleanup:
00082     /* Disable clock to save power - assume we're the only users of RNG */
00083     CLOCK_SYS_DisableRngaClock( 0 );
00084 
00085     return( ret );
00086 }
00087 
00088 #endif