Skip to content

Commit b67e1f7

Browse files
authored
Merge pull request #2765 from 0xc0170/dev_rng_hal
RNG HAL addition
2 parents 4820c87 + 1b95c67 commit b67e1f7

File tree

19 files changed

+357
-241
lines changed

19 files changed

+357
-241
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if defined(DEVICE_TRNG)
18+
19+
#include "hal/trng_api.h"
20+
21+
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) {
22+
trng_t trng_obj;
23+
trng_init(&trng_obj);
24+
int ret = trng_get_bytes(&trng_obj, output, len, olen);
25+
trng_free(&trng_obj);
26+
return ret;
27+
}
28+
29+
#endif

hal/hal/trng_api.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_TRNG_API_H
17+
#define MBED_TRNG_API_H
18+
19+
#include <stddef.h>
20+
#include "device.h"
21+
22+
#if DEVICE_TRNG
23+
24+
/** TRNG HAL structure. trng_s is declared in the target's HAL
25+
*/
26+
typedef struct trng_s trng_t;
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/**
33+
* \defgroup hal_trng TRNG hal functions
34+
* @{
35+
*/
36+
37+
/** Initialize the TRNG peripheral
38+
*
39+
* @param obj The TRNG object
40+
*/
41+
void trng_init(trng_t *obj);
42+
43+
/** Deinitialize the TRNG peripheral
44+
*
45+
* @param obj The TRNG object
46+
*/
47+
void trng_free(trng_t *obj);
48+
49+
/** Get random data from TRNG peripheral
50+
*
51+
* @param obj The TRNG object
52+
* @param output The pointer to an output array
53+
* @param length The size of output data, to avoid buffer overwrite
54+
* @param output_length The length of generated data
55+
* @return 0 success, -1 fail
56+
*/
57+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length);
58+
59+
/**@}*/
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif
64+
65+
#endif
66+
67+
#endif

hal/targets.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@
565565
"inherits": ["Target"],
566566
"progen": {"target": "frdm-k64f"},
567567
"detect_code": ["0240"],
568-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE"],
568+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE", "TRNG"],
569569
"features": ["IPV4", "STORAGE"],
570570
"release_versions": ["2", "5"]
571571
},
@@ -603,7 +603,7 @@
603603
"inherits": ["Target"],
604604
"progen": {"target": "frdm-k66f"},
605605
"detect_code": ["0311"],
606-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
606+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
607607
"release_versions": ["2", "5"]
608608
},
609609
"NUCLEO_F030R8": {
@@ -795,7 +795,7 @@
795795
"progen": {"target": "nucleo-f410rb"},
796796
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT", "TRANSACTION_QUEUE_SIZE_SPI=2"],
797797
"detect_code": ["0740"],
798-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
798+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
799799
"release_versions": ["2", "5"]
800800
},
801801
"NUCLEO_F411RE": {
@@ -833,7 +833,7 @@
833833
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
834834
"progen": {"target": "nucleo-f429zi"},
835835
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT", "DEVICE_RTC_LSI=1", "TRANSACTION_QUEUE_SIZE_SPI=2"],
836-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
836+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "RTC_LSI", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
837837
"detect_code": ["0796"],
838838
"features": ["IPV4"],
839839
"release_versions": ["2", "5"]
@@ -892,7 +892,7 @@
892892
},
893893
"supported_form_factors": ["ARDUINO"],
894894
"detect_code": ["0816"],
895-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
895+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
896896
"features": ["IPV4"],
897897
"release_versions": ["2", "5"]
898898
},
@@ -905,7 +905,7 @@
905905
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
906906
"progen": {"target": "nucleo-f767zi"},
907907
"detect_code": ["0818"],
908-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
908+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
909909
"features": ["IPV4"],
910910
"release_versions": ["2", "5"]
911911
},
@@ -1077,7 +1077,7 @@
10771077
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","DEVICE_RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2"],
10781078
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10791079
"progen": {"target": "disco-f429zi"},
1080-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
1080+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
10811081
"release_versions": ["2", "5"]
10821082
},
10831083
"DISCO_F469NI": {
@@ -1090,7 +1090,7 @@
10901090
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","TRANSACTION_QUEUE_SIZE_SPI=2"],
10911091
"progen": {"target": "disco-f469ni"},
10921092
"detect_code": ["0788"],
1093-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
1093+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
10941094
"release_versions": ["2", "5"]
10951095
},
10961096
"DISCO_L053C8": {
@@ -1113,7 +1113,7 @@
11131113
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
11141114
"progen": {"target": "disco-f746ng"},
11151115
"detect_code": ["0815"],
1116-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
1116+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
11171117
"features": ["IPV4"],
11181118
"release_versions": ["2", "5"]
11191119
},
@@ -2088,7 +2088,7 @@
20882088
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
20892089
"inherits": ["Target"],
20902090
"progen": {"target": "numaker-pfm-nuc472"},
2091-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"],
2091+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG"],
20922092
"features": ["IPV4"],
20932093
"release_versions": ["2", "5"]
20942094
},

hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/entropy_hardware_poll.c renamed to hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K66F/trng_api.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,27 @@
2626
#include "cmsis.h"
2727
#include "fsl_common.h"
2828
#include "fsl_clock.h"
29+
#include "trng_api.h"
30+
31+
void trng_init(trng_t *obj)
32+
{
33+
(void)obj;
34+
CLOCK_EnableClock(kCLOCK_Rnga0);
35+
CLOCK_DisableClock(kCLOCK_Rnga0);
36+
CLOCK_EnableClock(kCLOCK_Rnga0);
37+
}
38+
39+
void trng_free(trng_t *obj)
40+
{
41+
(void)obj;
42+
CLOCK_DisableClock(kCLOCK_Rnga0);
43+
}
2944

3045
/*
3146
* Get one byte of entropy from the RNG, assuming it is up and running.
3247
* As recommended (34.1.1), get only one bit of each output.
3348
*/
34-
static void rng_get_byte( unsigned char *byte )
49+
static void trng_get_byte(unsigned char *byte)
3550
{
3651
size_t bit;
3752

@@ -43,41 +58,26 @@ static void rng_get_byte( unsigned char *byte )
4358
}
4459
}
4560

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 )
61+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
5162
{
63+
(void)obj;
5264
size_t i;
5365
int ret;
54-
((void) data);
55-
56-
CLOCK_EnableClock( kCLOCK_Rnga0 );
57-
CLOCK_DisableClock( kCLOCK_Rnga0 );
58-
CLOCK_EnableClock( kCLOCK_Rnga0 );
5966

6067
/* Set "Interrupt Mask", "High Assurance" and "Go",
6168
* unset "Clear interrupt" and "Sleep" */
6269
RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
6370

64-
for( i = 0; i < len; i++ )
65-
rng_get_byte( output + i );
71+
for (i = 0; i < length; i++) {
72+
trng_get_byte(output + i);
73+
}
6674

6775
/* 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;
76+
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
77+
return -1;
7278
}
7379

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+
*output_length = length;
8081

81-
return( ret );
82+
return 0;
8283
}
83-
Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,66 @@
2222
* Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34
2323
*/
2424

25+
#if defined(DEVICE_TRNG)
26+
2527
#include <stdlib.h>
2628
#include "cmsis.h"
2729
#include "fsl_common.h"
2830
#include "fsl_clock.h"
31+
#include "trng_api.h"
32+
33+
void trng_init(trng_t *obj)
34+
{
35+
(void)obj;
36+
CLOCK_EnableClock(kCLOCK_Rnga0);
37+
CLOCK_DisableClock(kCLOCK_Rnga0);
38+
CLOCK_EnableClock(kCLOCK_Rnga0);
39+
}
40+
41+
void trng_free(trng_t *obj)
42+
{
43+
(void)obj;
44+
CLOCK_DisableClock(kCLOCK_Rnga0);
45+
}
2946

3047
/*
3148
* Get one byte of entropy from the RNG, assuming it is up and running.
3249
* As recommended (34.1.1), get only one bit of each output.
3350
*/
34-
static void rng_get_byte( unsigned char *byte )
51+
static void trng_get_byte(unsigned char *byte)
3552
{
3653
size_t bit;
3754

3855
/* 34.5 Steps 3-4-5: poll SR and read from OR when ready */
3956
for( bit = 0; bit < 8; bit++ )
4057
{
41-
while( ( RNG->SR & RNG_SR_OREG_LVL_MASK ) == 0 );
42-
*byte |= ( RNG->OR & 1 ) << bit;
58+
while((RNG->SR & RNG_SR_OREG_LVL_MASK) == 0 );
59+
*byte |= (RNG->OR & 1) << bit;
4360
}
4461
}
4562

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 )
63+
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
5164
{
65+
(void)obj;
5266
size_t i;
5367
int ret;
54-
((void) data);
55-
56-
CLOCK_EnableClock( kCLOCK_Rnga0 );
57-
CLOCK_DisableClock( kCLOCK_Rnga0 );
58-
CLOCK_EnableClock( kCLOCK_Rnga0 );
5968

6069
/* Set "Interrupt Mask", "High Assurance" and "Go",
6170
* unset "Clear interrupt" and "Sleep" */
6271
RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
6372

64-
for( i = 0; i < len; i++ )
65-
rng_get_byte( output + i );
73+
for (i = 0; i < length; i++) {
74+
trng_get_byte(output + i);
75+
}
6676

6777
/* 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;
78+
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
79+
return -1;
7280
}
7381

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 );
82+
*output_length = length;
8083

81-
return( ret );
84+
return 0;
8285
}
8386

87+
#endif

hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/api/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ struct dac_s {
6161
DACName dac;
6262
};
6363

64+
struct trng_s {
65+
uint8_t dummy;
66+
};
67+
6468
#include "gpio_object.h"
6569

6670
#ifdef __cplusplus

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ struct sleep_s {
124124
int powerdown;
125125
};
126126

127+
struct trng_s {
128+
uint8_t dummy;
129+
};
130+
127131
#ifdef __cplusplus
128132
}
129133
#endif

0 commit comments

Comments
 (0)