Merge pull request #2300 from hierophect/stm32-urandom

STM32: implement OS urandom
crypto-aes
Dan Halbert 3 years ago committed by GitHub
commit 41aa214589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      ports/stm32f4/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h
  2. 2
      ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h
  3. 2
      ports/stm32f4/boards/stm32f412zg_discovery/stm32f4xx_hal_conf.h
  4. 35
      ports/stm32f4/common-hal/os/__init__.c
  5. 3
      ports/stm32f4/peripherals/stm32f4/periph.h

@ -55,7 +55,7 @@
#define HAL_I2S_MODULE_ENABLED
/* #define HAL_IWDG_MODULE_ENABLED */
/* #define HAL_LTDC_MODULE_ENABLED */
/* #define HAL_RNG_MODULE_ENABLED */
#define HAL_RNG_MODULE_ENABLED
/* #define HAL_RTC_MODULE_ENABLED */
/* #define HAL_SAI_MODULE_ENABLED */
/* #define HAL_SD_MODULE_ENABLED */

@ -55,7 +55,7 @@
#define HAL_I2S_MODULE_ENABLED
/* #define HAL_IWDG_MODULE_ENABLED */
/* #define HAL_LTDC_MODULE_ENABLED */
/* #define HAL_RNG_MODULE_ENABLED */
#define HAL_RNG_MODULE_ENABLED
/* #define HAL_RTC_MODULE_ENABLED */
/* #define HAL_SAI_MODULE_ENABLED */
/* #define HAL_SD_MODULE_ENABLED */

@ -55,7 +55,7 @@
#define HAL_I2S_MODULE_ENABLED
/* #define HAL_IWDG_MODULE_ENABLED */
/* #define HAL_LTDC_MODULE_ENABLED */
/* #define HAL_RNG_MODULE_ENABLED */
#define HAL_RNG_MODULE_ENABLED
/* #define HAL_RTC_MODULE_ENABLED */
/* #define HAL_SAI_MODULE_ENABLED */
#define HAL_SD_MODULE_ENABLED

@ -30,6 +30,11 @@
#include "py/objstr.h"
#include "py/objtuple.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "stm32f4xx_hal.h"
#include "stm32f4/periph.h"
STATIC const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename,
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
@ -56,11 +61,35 @@ mp_obj_t common_hal_os_uname(void) {
return (mp_obj_t)&os_uname_info_obj;
}
#define RNG_TIMEOUT 5
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
#if (HAS_TRNG)
//init the RNG
__HAL_RCC_RNG_CLK_ENABLE();
RNG_HandleTypeDef handle;
handle.Instance = RNG;
if (HAL_RNG_Init(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG Init Error"));
for (uint32_t i = 0; i < length; i++) {
buffer[i] = 4; //read in my coffee dregs; the truest random number, chosen by Nrthalotep
//todo: spurn the gods, replace with actual code.
//Assign bytes
for (uint i = 0; i < length; i++) {
uint32_t temp;
uint32_t start = HAL_GetTick();
//the HAL function has a timeout, but it isn't long enough, and isn't adjustable
while(!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT));
//
if (HAL_RNG_GenerateRandomNumber(&handle, &temp) != HAL_OK) {
mp_raise_ValueError(translate("Random number generation error"));
}
*buffer = (uint8_t)temp;
}
//shut down the peripheral
if (HAL_RNG_DeInit(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG DeInit Error"));
__HAL_RCC_RNG_CLK_DISABLE();
return true;
#else
return false;
#endif
}

@ -142,11 +142,13 @@ typedef struct {
#ifdef STM32F411xE
#define HAS_DAC 0
#define HAS_TRNG 0
#include "stm32f411xe/periph.h"
#endif
#ifdef STM32F412Zx
#define HAS_DAC 0
#define HAS_TRNG 1
#include "stm32f412zx/periph.h"
#endif
@ -154,6 +156,7 @@ typedef struct {
#ifdef STM32F405xx
#define HAS_DAC 1
#define HAS_TRNG 1
#include "stm32f405xx/periph.h"
#endif

Loading…
Cancel
Save