Skip to content

Commit f450786

Browse files
committed
Add entropy collector for K64F to mbed HAL for use in mbed TLS
mbed TLS requires an entropy source, and this provides support for one through the K64F RNG. The macro MBEDTLS_ENTROPY_HARDWARE_ALT also added to target.json to enable use of the entropy collector by mbed TLS.
1 parent 02bb0df commit f450786

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

hal/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@
566566
"supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
567567
"extra_labels": ["Freescale", "KSDK2_MCUS", "FRDM", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F"],
568568
"is_disk_virtual": true,
569-
"macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED"],
569+
"macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED", "MBEDTLS_ENTROPY_HARDWARE_ALT"],
570570
"inherits": ["Target"],
571571
"progen": {"target": "frdm-k64f"},
572572
"detect_code": ["0240"],
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Hardware entropy collector for the K64F, using Freescale's RNGA
3+
*
4+
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
18+
*
19+
*/
20+
21+
/*
22+
* Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34
23+
*/
24+
25+
#include <stdlib.h>
26+
#include "cmsis.h"
27+
#include "fsl_common.h"
28+
#include "fsl_clock.h"
29+
30+
/*
31+
* Get one byte of entropy from the RNG, assuming it is up and running.
32+
* As recommended (34.1.1), get only one bit of each output.
33+
*/
34+
static void rng_get_byte( unsigned char *byte )
35+
{
36+
size_t bit;
37+
38+
/* 34.5 Steps 3-4-5: poll SR and read from OR when ready */
39+
for( bit = 0; bit < 8; bit++ )
40+
{
41+
while( ( RNG->SR & RNG_SR_OREG_LVL_MASK ) == 0 );
42+
*byte |= ( RNG->OR & 1 ) << bit;
43+
}
44+
}
45+
46+
/*
47+
* Get len bytes of entropy from the hardware RNG.
48+
*/
49+
int mbedtls_hardware_poll( void *data,
50+
unsigned char *output, size_t len, size_t *olen )
51+
{
52+
size_t i;
53+
int ret;
54+
((void) data);
55+
56+
CLOCK_EnableClock( kCLOCK_Rnga0 );
57+
CLOCK_DisableClock( kCLOCK_Rnga0 );
58+
CLOCK_EnableClock( kCLOCK_Rnga0 );
59+
60+
/* Set "Interrupt Mask", "High Assurance" and "Go",
61+
* unset "Clear interrupt" and "Sleep" */
62+
RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
63+
64+
for( i = 0; i < len; i++ )
65+
rng_get_byte( output + i );
66+
67+
/* Just be extra sure that we didn't do it wrong */
68+
if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 )
69+
{
70+
ret = -1;
71+
goto cleanup;
72+
}
73+
74+
*olen = len;
75+
ret = 0;
76+
77+
cleanup:
78+
/* Disable clock to save power - assume we're the only users of RNG */
79+
CLOCK_DisableClock( kCLOCK_Rnga0 );
80+
81+
return( ret );
82+
}
83+

0 commit comments

Comments
 (0)