2015-07-28 14:17:48 +02:00
|
|
|
/*
|
2015-08-18 13:33:14 +02:00
|
|
|
* Hardware entropy collector for the K64F, using Freescale's RNGA
|
2015-07-28 14:17:48 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-07-28 14:17:48 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-08-07 09:40:51 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-07 09:40:51 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-08-07 09:40:51 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2015-07-28 14:17:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2015-08-18 13:33:14 +02:00
|
|
|
* WARNING: this is temporary!
|
|
|
|
* This should be in a separate yotta module which would be a target
|
2015-07-28 14:17:48 +02:00
|
|
|
* dependency of mbedtls (see IOTSSL-313)
|
|
|
|
*/
|
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
#if defined(TARGET_LIKE_K64F)
|
2015-07-28 14:17:48 +02:00
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
/*
|
|
|
|
* Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34
|
|
|
|
*/
|
2015-07-28 14:17:48 +02:00
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
#include "fsl_clock_manager.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get one byte of entropy from the RNG, assuming it is up and running.
|
|
|
|
* As recommended (34.1.1), get only one bit of each output.
|
|
|
|
*/
|
|
|
|
static void rng_get_byte( unsigned char *byte )
|
2015-07-28 14:17:48 +02:00
|
|
|
{
|
2015-08-18 13:33:14 +02:00
|
|
|
size_t bit;
|
2015-07-28 14:17:48 +02:00
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
/* 34.5 Steps 3-4-5: poll SR and read from OR when ready */
|
|
|
|
for( bit = 0; bit < 8; bit++ )
|
2015-07-28 14:17:48 +02:00
|
|
|
{
|
2015-08-18 13:33:14 +02:00
|
|
|
while( ( RNG->SR & RNG_SR_OREG_LVL_MASK ) == 0 );
|
|
|
|
*byte |= ( RNG->OR & 1 ) << bit;
|
2015-07-28 14:17:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
/*
|
|
|
|
* Get len bytes of entropy from the hardware RNG.
|
|
|
|
*/
|
2015-07-28 14:17:48 +02:00
|
|
|
int mbedtls_hardware_poll( void *data,
|
|
|
|
unsigned char *output, size_t len, size_t *olen )
|
|
|
|
{
|
2015-08-18 13:33:14 +02:00
|
|
|
size_t i;
|
|
|
|
int ret;
|
2015-07-28 14:17:48 +02:00
|
|
|
((void) data);
|
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
CLOCK_SYS_EnableRngaClock( 0 );
|
|
|
|
|
|
|
|
/* Set "Interrupt Mask", "High Assurance" and "Go",
|
|
|
|
* unset "Clear interrupt" and "Sleep" */
|
|
|
|
RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
rng_get_byte( output + i );
|
|
|
|
|
|
|
|
/* Just be extra sure that we didn't do it wrong */
|
|
|
|
if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 )
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
*olen = len;
|
|
|
|
ret = 0;
|
2015-07-28 14:17:48 +02:00
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
cleanup:
|
|
|
|
/* Disable clock to save power - assume we're the only users of RNG */
|
|
|
|
CLOCK_SYS_DisableRngaClock( 0 );
|
2015-07-28 14:17:48 +02:00
|
|
|
|
2015-08-18 13:33:14 +02:00
|
|
|
return( ret );
|
2015-07-28 14:17:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|