Skip to content

RNG HAL addition #2765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions features/mbedtls/platform/src/mbed_trng.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

#if defined(DEVICE_TRNG)

#include "hal/trng_api.h"

int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) {
trng_t trng_obj;
trng_init(&trng_obj);
int ret = trng_get_bytes(&trng_obj, output, len, olen);
trng_free(&trng_obj);
return ret;
}

#endif
67 changes: 67 additions & 0 deletions hal/hal/trng_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#ifndef MBED_TRNG_API_H
#define MBED_TRNG_API_H

#include <stddef.h>
#include "device.h"

#if DEVICE_TRNG

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might just be a cosmetic change, but perhaps this should be #if defined(DEVICE_TRNG)?

Copy link
Contributor Author

@0xc0170 0xc0170 Sep 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to change it this morning then realized this is how it is within HAL (if we go that path, should be fixed everywhere). lets keep it as it is


/** TRNG HAL structure. trng_s is declared in the target's HAL
*/
typedef struct trng_s trng_t;

#ifdef __cplusplus
extern "C" {
#endif

/**
* \defgroup hal_trng TRNG hal functions
* @{
*/

/** Initialize the TRNG peripheral
*
* @param obj The TRNG object
*/
void trng_init(trng_t *obj);

/** Deinitialize the TRNG peripheral
*
* @param obj The TRNG object
*/
void trng_free(trng_t *obj);

/** Get random data from TRNG peripheral
*
* @param obj The TRNG object
* @param output The pointer to an output array
* @param length The size of output data, to avoid buffer overwrite
* @param output_length The length of generated data
* @return 0 success, -1 fail
*/
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length);

/**@}*/

Copy link
Contributor

@yanesca yanesca Sep 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also would need a int trng_get_min_entropy() function that returns an estimate on the min entropy of the TRNG. The amount of entropy returned by each call of trng_get_bytes MUST be at least this much. This is usually a static value, a characteristic of the hardware and the driver. (The amount of entropy in a single returned byte can be anything between 0 and 8)

mbed TLS will call this function automatically and use this value to calculate how many times we have to poll to achieve the configured level of cryptographic strength.

Copy link
Contributor

@yanesca yanesca Sep 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to put a reference in a comment on how to estimate the entropy of a source. Section 6 in the standard: http://csrc.nist.gov/publications/drafts/800-90/sp800-90b_second_draft.pdf describes it in great detail with easy to follow implementation instructions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably discussed this with the others, but just to be sure I write it here too: The trng_get_min_entropy function is not necessary for this release. It is postponed and it will be redesigned before implementation.

#ifdef __cplusplus
}
#endif

#endif

#endif
20 changes: 10 additions & 10 deletions hal/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@
"inherits": ["Target"],
"progen": {"target": "frdm-k64f"},
"detect_code": ["0240"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE"],
"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"],
"features": ["IPV4", "STORAGE"],
"release_versions": ["2", "5"]
},
Expand Down Expand Up @@ -603,7 +603,7 @@
"inherits": ["Target"],
"progen": {"target": "frdm-k66f"},
"detect_code": ["0311"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
"release_versions": ["2", "5"]
},
"NUCLEO_F030R8": {
Expand Down Expand Up @@ -795,7 +795,7 @@
"progen": {"target": "nucleo-f410rb"},
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT", "TRANSACTION_QUEUE_SIZE_SPI=2"],
"detect_code": ["0740"],
"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"],
"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"],
"release_versions": ["2", "5"]
},
"NUCLEO_F411RE": {
Expand Down Expand Up @@ -833,7 +833,7 @@
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
"progen": {"target": "nucleo-f429zi"},
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT", "DEVICE_RTC_LSI=1", "TRANSACTION_QUEUE_SIZE_SPI=2"],
"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"],
"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"],
"detect_code": ["0796"],
"features": ["IPV4"],
"release_versions": ["2", "5"]
Expand Down Expand Up @@ -892,7 +892,7 @@
},
"supported_form_factors": ["ARDUINO"],
"detect_code": ["0816"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
"features": ["IPV4"],
"release_versions": ["2", "5"]
},
Expand All @@ -905,7 +905,7 @@
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
"progen": {"target": "nucleo-f767zi"},
"detect_code": ["0818"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
"features": ["IPV4"],
"release_versions": ["2", "5"]
},
Expand Down Expand Up @@ -1077,7 +1077,7 @@
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","DEVICE_RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2"],
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
"progen": {"target": "disco-f429zi"},
"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"],
"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"],
"release_versions": ["2", "5"]
},
"DISCO_F469NI": {
Expand All @@ -1090,7 +1090,7 @@
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","TRANSACTION_QUEUE_SIZE_SPI=2"],
"progen": {"target": "disco-f469ni"},
"detect_code": ["0788"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
"release_versions": ["2", "5"]
},
"DISCO_L053C8": {
Expand All @@ -1113,7 +1113,7 @@
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT"],
"progen": {"target": "disco-f746ng"},
"detect_code": ["0815"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "TRNG"],
"features": ["IPV4"],
"release_versions": ["2", "5"]
},
Expand Down Expand Up @@ -2088,7 +2088,7 @@
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
"inherits": ["Target"],
"progen": {"target": "numaker-pfm-nuc472"},
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"],
"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"],
"features": ["IPV4"],
"release_versions": ["2", "5"]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,27 @@
#include "cmsis.h"
#include "fsl_common.h"
#include "fsl_clock.h"
#include "trng_api.h"

void trng_init(trng_t *obj)
{
(void)obj;
CLOCK_EnableClock(kCLOCK_Rnga0);
CLOCK_DisableClock(kCLOCK_Rnga0);
CLOCK_EnableClock(kCLOCK_Rnga0);
}

void trng_free(trng_t *obj)
{
(void)obj;
CLOCK_DisableClock(kCLOCK_Rnga0);
}

/*
* 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 )
static void trng_get_byte(unsigned char *byte)
{
size_t bit;

Expand All @@ -43,41 +58,26 @@ static void rng_get_byte( unsigned char *byte )
}
}

/*
* Get len bytes of entropy from the hardware RNG.
*/
int mbedtls_hardware_poll( void *data,
unsigned char *output, size_t len, size_t *olen )
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
{
(void)obj;
size_t i;
int ret;
((void) data);

CLOCK_EnableClock( kCLOCK_Rnga0 );
CLOCK_DisableClock( kCLOCK_Rnga0 );
CLOCK_EnableClock( kCLOCK_Rnga0 );

/* 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 );
for (i = 0; i < length; i++) {
trng_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;
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
return -1;
}

*olen = len;
ret = 0;

cleanup:
/* Disable clock to save power - assume we're the only users of RNG */
CLOCK_DisableClock( kCLOCK_Rnga0 );
*output_length = length;

return( ret );
return 0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,66 @@
* Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34
*/

#if defined(DEVICE_TRNG)

#include <stdlib.h>
#include "cmsis.h"
#include "fsl_common.h"
#include "fsl_clock.h"
#include "trng_api.h"

void trng_init(trng_t *obj)
{
(void)obj;
CLOCK_EnableClock(kCLOCK_Rnga0);
CLOCK_DisableClock(kCLOCK_Rnga0);
CLOCK_EnableClock(kCLOCK_Rnga0);
}

void trng_free(trng_t *obj)
{
(void)obj;
CLOCK_DisableClock(kCLOCK_Rnga0);
}

/*
* 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 )
static void trng_get_byte(unsigned char *byte)
{
size_t bit;

/* 34.5 Steps 3-4-5: poll SR and read from OR when ready */
for( bit = 0; bit < 8; bit++ )
{
while( ( RNG->SR & RNG_SR_OREG_LVL_MASK ) == 0 );
*byte |= ( RNG->OR & 1 ) << bit;
while((RNG->SR & RNG_SR_OREG_LVL_MASK) == 0 );
*byte |= (RNG->OR & 1) << bit;
}
}

/*
* Get len bytes of entropy from the hardware RNG.
*/
int mbedtls_hardware_poll( void *data,
unsigned char *output, size_t len, size_t *olen )
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
{
(void)obj;
size_t i;
int ret;
((void) data);

CLOCK_EnableClock( kCLOCK_Rnga0 );
CLOCK_DisableClock( kCLOCK_Rnga0 );
CLOCK_EnableClock( kCLOCK_Rnga0 );

/* 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 );
for (i = 0; i < length; i++) {
trng_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;
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
return -1;
}

*olen = len;
ret = 0;

cleanup:
/* Disable clock to save power - assume we're the only users of RNG */
CLOCK_DisableClock( kCLOCK_Rnga0 );
*output_length = length;

return( ret );
return 0;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ struct dac_s {
DACName dac;
};

struct trng_s {
uint8_t dummy;
};

#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ struct sleep_s {
int powerdown;
};

struct trng_s {
uint8_t dummy;
};

#ifdef __cplusplus
}
#endif
Expand Down
Loading