mirror of https://github.com/ARMmbed/mbed-os.git
commit
b67e1f7564
|
@ -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
|
|
@ -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
|
||||
|
||||
/** 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);
|
||||
|
||||
/**@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -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"]
|
||||
},
|
||||
|
@ -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": {
|
||||
|
@ -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": {
|
||||
|
@ -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"]
|
||||
|
@ -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"]
|
||||
},
|
||||
|
@ -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"]
|
||||
},
|
||||
|
@ -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": {
|
||||
|
@ -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": {
|
||||
|
@ -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"]
|
||||
},
|
||||
|
@ -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"]
|
||||
},
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -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 );
|
||||
|
||||
/* Just be extra sure that we didn't do it wrong */
|
||||
if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 )
|
||||
{
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
for (i = 0; i < length; i++) {
|
||||
trng_get_byte(output + i);
|
||||
}
|
||||
|
||||
*olen = len;
|
||||
ret = 0;
|
||||
/* Just be extra sure that we didn't do it wrong */
|
||||
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -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 );
|
||||
|
||||
/* Just be extra sure that we didn't do it wrong */
|
||||
if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 )
|
||||
{
|
||||
ret = -1;
|
||||
goto cleanup;
|
||||
for (i = 0; i < length; i++) {
|
||||
trng_get_byte(output + i);
|
||||
}
|
||||
|
||||
*olen = len;
|
||||
ret = 0;
|
||||
/* Just be extra sure that we didn't do it wrong */
|
||||
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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
|
|
@ -61,6 +61,10 @@ struct dac_s {
|
|||
DACName dac;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
uint8_t dummy;
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -124,6 +124,10 @@ struct sleep_s {
|
|||
int powerdown;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
uint8_t dummy;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,116 +0,0 @@
|
|||
/*
|
||||
* Hardware entropy collector for NUC472's RNGA
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "cmsis.h"
|
||||
#include "NUC472_442.h"
|
||||
#include "us_ticker_api.h"
|
||||
|
||||
/*
|
||||
* Get Random number generator.
|
||||
*/
|
||||
static volatile int g_PRNG_done;
|
||||
|
||||
void CRYPTO_IRQHandler()
|
||||
{
|
||||
if (PRNG_GET_INT_FLAG()) {
|
||||
g_PRNG_done = 1;
|
||||
PRNG_CLR_INT_FLAG();
|
||||
}
|
||||
}
|
||||
|
||||
static void rng_get(unsigned char *pConversionData)
|
||||
{
|
||||
uint32_t *p32ConversionData;
|
||||
|
||||
p32ConversionData = (uint32_t *)pConversionData;
|
||||
|
||||
/* Unlock protected registers */
|
||||
SYS_UnlockReg();
|
||||
/* Enable IP clock */
|
||||
CLK_EnableModuleClock(CRPT_MODULE);
|
||||
|
||||
/* Lock protected registers */
|
||||
SYS_LockReg();
|
||||
|
||||
NVIC_EnableIRQ(CRPT_IRQn);
|
||||
PRNG_ENABLE_INT();
|
||||
|
||||
// PRNG_Open(PRNG_KEY_SIZE_64, 0, 0);
|
||||
PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read());
|
||||
PRNG_Start();
|
||||
while (!g_PRNG_done);
|
||||
|
||||
|
||||
PRNG_Read(p32ConversionData);
|
||||
|
||||
// printf(" 0x%08x 0x%08x 0x%08x 0x%08x\n\r", *p32ConversionData, *(p32ConversionData+1), *(p32ConversionData+2), *(p32ConversionData+3));
|
||||
// printf(" 0x%08x 0x%08x 0x%08x 0x%08x\n\r", *(p32ConversionData+4), *(p32ConversionData+5), *(p32ConversionData+6), *(p32ConversionData+7));
|
||||
|
||||
PRNG_DISABLE_INT();
|
||||
NVIC_DisableIRQ(CRPT_IRQn);
|
||||
// CLK_DisableModuleClock(CRPT_MODULE);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Get len bytes of entropy from the hardware RNG.
|
||||
*/
|
||||
|
||||
int mbedtls_hardware_poll( void *data,
|
||||
unsigned char *output, size_t len, size_t *olen )
|
||||
{
|
||||
#if 0
|
||||
unsigned long timer = us_ticker_read();
|
||||
((void) data);
|
||||
*olen = 0;
|
||||
|
||||
if( len < sizeof(unsigned long) )
|
||||
return( 0 );
|
||||
|
||||
memcpy( output, &timer, sizeof(unsigned long) );
|
||||
*olen = sizeof(unsigned long);
|
||||
#else
|
||||
*olen = 0;
|
||||
if( len < 32 )
|
||||
{
|
||||
unsigned char tmpBuff[32];
|
||||
rng_get(tmpBuff);
|
||||
memcpy( output, &tmpBuff, len );
|
||||
*olen = len;
|
||||
return( 0 );
|
||||
}
|
||||
for( int i = 0; i < (len/32) ; i++)
|
||||
{
|
||||
rng_get(output);
|
||||
*olen += 32;
|
||||
// printf("Output result of len[%d][%d]: 0x%08x 0x%08x\n\r", len, *olen, *((int32_t *)output), *(((int32_t *)output)+1));
|
||||
output += 32;
|
||||
}
|
||||
#endif
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Hardware entropy collector for NUC472's RNGA
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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 DEVICE_TRNG
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "cmsis.h"
|
||||
#include "NUC472_442.h"
|
||||
#include "us_ticker_api.h"
|
||||
#include "trng_api.h"
|
||||
|
||||
/*
|
||||
* Get Random number generator.
|
||||
*/
|
||||
static volatile int g_PRNG_done;
|
||||
|
||||
void CRYPTO_IRQHandler()
|
||||
{
|
||||
if (PRNG_GET_INT_FLAG()) {
|
||||
g_PRNG_done = 1;
|
||||
PRNG_CLR_INT_FLAG();
|
||||
}
|
||||
}
|
||||
|
||||
static void trng_get(unsigned char *pConversionData)
|
||||
{
|
||||
uint32_t *p32ConversionData;
|
||||
|
||||
p32ConversionData = (uint32_t *)pConversionData;
|
||||
|
||||
PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read());
|
||||
PRNG_Start();
|
||||
while (!g_PRNG_done);
|
||||
|
||||
PRNG_Read(p32ConversionData);
|
||||
}
|
||||
|
||||
void trng_init(trng_t *obj)
|
||||
{
|
||||
(void)obj;
|
||||
/* Unlock protected registers */
|
||||
SYS_UnlockReg();
|
||||
/* Enable IP clock */
|
||||
CLK_EnableModuleClock(CRPT_MODULE);
|
||||
|
||||
/* Lock protected registers */
|
||||
SYS_LockReg();
|
||||
|
||||
NVIC_EnableIRQ(CRPT_IRQn);
|
||||
PRNG_ENABLE_INT();
|
||||
}
|
||||
|
||||
void trng_free(trng_t *obj)
|
||||
{
|
||||
(void)obj;
|
||||
PRNG_DISABLE_INT();
|
||||
NVIC_DisableIRQ(CRPT_IRQn);
|
||||
}
|
||||
|
||||
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
|
||||
{
|
||||
(void)obj;
|
||||
|
||||
*output_length = 0;
|
||||
if (length < 32) {
|
||||
unsigned char tmpBuff[32];
|
||||
trng_get(tmpBuff);
|
||||
memcpy(output, &tmpBuff, length);
|
||||
*output_length = length;
|
||||
} else {
|
||||
for (int i = 0; i < (length/32); i++) {
|
||||
trng_get(output);
|
||||
*output_length += 32;
|
||||
output += 32;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -70,6 +70,10 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -70,6 +70,10 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -65,6 +65,10 @@ struct dac_s {
|
|||
uint8_t channel;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -70,6 +70,10 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
#include "common_objects.h"
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#if defined(DEVICE_TRNG)
|
||||
|
||||
#if defined(TARGET_STM32F405xx) || defined(TARGET_STM32F415xx) || defined(TARGET_STM32F407xx) || defined(TARGET_STM32F417xx) ||\
|
||||
defined(TARGET_STM32F427xx) || defined(TARGET_STM32F437xx) || defined(TARGET_STM32F429xx) || defined(TARGET_STM32F439xx) ||\
|
||||
|
@ -25,61 +26,57 @@
|
|||
defined(TARGET_STM32F479xx)
|
||||
#include <stdlib.h>
|
||||
#include "cmsis.h"
|
||||
#include "trng_api.h"
|
||||
|
||||
/* RNG handler declaration */
|
||||
RNG_HandleTypeDef RngHandle;
|
||||
|
||||
|
||||
/** rng_get_byte
|
||||
/** trng_get_byte
|
||||
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
|
||||
* @param obj TRNG obj
|
||||
* @param pointer to the hardware generated random byte.
|
||||
*/
|
||||
static void rng_get_byte( unsigned char *byte )
|
||||
static void trng_get_byte(trng_t *obj, unsigned char *byte )
|
||||
{
|
||||
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
|
||||
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle);
|
||||
}
|
||||
|
||||
|
||||
/** mbedtls_hardware_poll
|
||||
* @brief Get len bytes of entropy from the hardware RNG.
|
||||
* @param data pointer will be NULL
|
||||
* @param output pointer to the random generated bytes buffer
|
||||
* @param len input is the requested length of bytes to be generated
|
||||
* @param olen is the pointer to the length of bytes effectively generated
|
||||
* @returns 0 if the generation went well. -1 in case of error
|
||||
*/
|
||||
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
|
||||
void trng_init(trng_t *obj)
|
||||
{
|
||||
int ret;
|
||||
((void) data);
|
||||
|
||||
/* RNG Peripheral clock enable */
|
||||
__HAL_RCC_RNG_CLK_ENABLE();
|
||||
|
||||
/* Initialize RNG instance */
|
||||
RngHandle.Instance = RNG;
|
||||
HAL_RNG_Init(&RngHandle);
|
||||
obj->handle.Instance = RNG;
|
||||
HAL_RNG_Init(&obj->handle);
|
||||
|
||||
}
|
||||
|
||||
void trng_free(trng_t *obj)
|
||||
{
|
||||
/*Disable the RNG peripheral */
|
||||
HAL_RNG_DeInit(&obj->handle);
|
||||
/* RNG Peripheral clock disable - assume we're the only users of RNG */
|
||||
__HAL_RCC_RNG_CLK_DISABLE();
|
||||
}
|
||||
|
||||
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Get Random byte */
|
||||
for( uint32_t i = 0; i < len; i++ ){
|
||||
rng_get_byte( output + i );
|
||||
|
||||
for( uint32_t i = 0; i < length; i++ ){
|
||||
trng_get_byte(obj, output + i );
|
||||
}
|
||||
*olen = len;
|
||||
|
||||
*output_length = length;
|
||||
/* Just be extra sure that we didn't do it wrong */
|
||||
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
|
||||
if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) {
|
||||
ret = -1;
|
||||
} else {
|
||||
ret = 0;
|
||||
}
|
||||
/*Disable the RNG peripheral */
|
||||
HAL_RNG_DeInit(&RngHandle);
|
||||
/* RNG Peripheral clock disable - assume we're the only users of RNG */
|
||||
__HAL_RCC_RNG_CLK_DISABLE();
|
||||
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
|
||||
STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx */
|
||||
|
||||
#endif
|
|
@ -90,6 +90,10 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -90,6 +90,10 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
#include "common_objects.h"
|
||||
|
||||
|
|
|
@ -90,6 +90,10 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "common_objects.h"
|
||||
#include "gpio_object.h"
|
||||
|
||||
|
|
|
@ -90,6 +90,9 @@ struct can_s {
|
|||
int index;
|
||||
};
|
||||
|
||||
struct trng_s {
|
||||
RNG_HandleTypeDef handle;
|
||||
};
|
||||
|
||||
#include "gpio_object.h"
|
||||
#include "common_objects.h"
|
||||
|
|
|
@ -18,62 +18,58 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#if defined(DEVICE_TRNG)
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "cmsis.h"
|
||||
#include "trng_api.h"
|
||||
|
||||
/* RNG handler declaration */
|
||||
RNG_HandleTypeDef RngHandle;
|
||||
|
||||
|
||||
/** rng_get_byte
|
||||
/** trng_get_byte
|
||||
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
|
||||
* @param pointer to the hardware generated random byte.
|
||||
*/
|
||||
static void rng_get_byte( unsigned char *byte )
|
||||
static void trng_get_byte(trng_t *obj, unsigned char *byte )
|
||||
{
|
||||
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
|
||||
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle);
|
||||
}
|
||||
|
||||
|
||||
/** mbedtls_hardware_poll
|
||||
* @brief Get len bytes of entropy from the hardware RNG.
|
||||
* @param data pointer will be NULL
|
||||
* @param output pointer to the random generated bytes buffer
|
||||
* @param len input is the requested length of bytes to be generated
|
||||
* @param olen is the pointer to the length of bytes effectively generated
|
||||
* @returns 0 if the generation went well. -1 in case of error
|
||||
*/
|
||||
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
|
||||
void trng_init(trng_t *obj)
|
||||
{
|
||||
int ret;
|
||||
((void) data);
|
||||
|
||||
/* RNG Peripheral clock enable */
|
||||
__HAL_RCC_RNG_CLK_ENABLE();
|
||||
|
||||
/* Initialize RNG instance */
|
||||
RngHandle.Instance = RNG;
|
||||
HAL_RNG_Init(&RngHandle);
|
||||
obj->handle.Instance = RNG;
|
||||
HAL_RNG_Init(&obj->handle);
|
||||
|
||||
}
|
||||
|
||||
void trng_free(trng_t *obj)
|
||||
{
|
||||
/*Disable the RNG peripheral */
|
||||
HAL_RNG_DeInit(&obj->handle);
|
||||
/* RNG Peripheral clock disable - assume we're the only users of RNG */
|
||||
__HAL_RCC_RNG_CLK_DISABLE();
|
||||
}
|
||||
|
||||
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Get Random byte */
|
||||
for( uint32_t i = 0; i < len; i++ ){
|
||||
rng_get_byte( output + i );
|
||||
|
||||
for( uint32_t i = 0; i < length; i++ ){
|
||||
trng_get_byte(obj, output + i );
|
||||
}
|
||||
*olen = len;
|
||||
|
||||
*output_length = length;
|
||||
/* Just be extra sure that we didn't do it wrong */
|
||||
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
|
||||
if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) {
|
||||
ret = -1;
|
||||
} else {
|
||||
ret = 0;
|
||||
}
|
||||
/*Disable the RNG peripheral */
|
||||
HAL_RNG_DeInit(&RngHandle);
|
||||
/* RNG Peripheral clock disable - assume we're the only users of RNG */
|
||||
__HAL_RCC_RNG_CLK_DISABLE();
|
||||
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue