/** 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 )
{
    int ret;
    ((void) data);

    /* RNG Peripheral clock enable */
    __HAL_RCC_RNG_CLK_ENABLE();

    /* Initialize RNG instance */
    RngHandle.Instance = RNG;
    HAL_RNG_Init(&RngHandle);

    /* Get Random byte */
    for( uint32_t i = 0; i < len; i++ ){
        rng_get_byte( output + i );

    }
    *olen = len;
    /* Just be extra sure that we didn't do it wrong */
    if( ( __HAL_RNG_GET_FLAG(&RngHandle, (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 );
}
Exemple #2
0
/* RNG init function */
void MX_RNG_Init(void)
{

  hrng.Instance = RNG;
  HAL_RNG_Init(&hrng);

}
Exemple #3
0
/**
  * @brief  Paints exit button
  * @param  hObj: button handle
  * @retval None
  */
static void _BuildFileName(void) {

  hRNG.Instance = RNG;
  __HAL_RCC_RNG_CLK_ENABLE();
  HAL_RNG_Init(&hRNG);
  sprintf(FileName, "record-%04lu.wav", HAL_RNG_GetRandomNumber(&hRNG) % 10000);
}
Exemple #4
0
void stm32_rng_init(void)
{
    RNG_HandleTypeDef rng_handle = { 0 };

    __HAL_RCC_RNG_CLK_ENABLE();

    rng_handle.Instance = RNG;
    HAL_StatusTypeDef status = HAL_RNG_Init(&rng_handle);
    if (status != HAL_OK) {
        panic("error initializing random number hardware\n");
    }

    /* seed the pseudo random number generator with this */
#if STM32_SEED_RAND_FROM_HWRNG
    uint32_t r;

    /* discard he first result */
    status = HAL_RNG_GenerateRandomNumber(&rng_handle, &r);
    if (status != HAL_OK) {
        panic("error getting random number from hardware\n");
    }

    status = HAL_RNG_GenerateRandomNumber(&rng_handle, &r);
    if (status != HAL_OK) {
        panic("error getting random number from hardware\n");
    }

    srand(r);
#endif
}
Exemple #5
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  uint32_t counter = 0;
 
  /* STM32L0xx HAL library initialization:
       - Configure the Flash prefetch, Flash preread and Buffer caches
       - Systick timer is configured by default as source of time base, but user 
             can eventually implement his proper time base source (a general purpose 
             timer for example or other time source), keeping in mind that Time base 
             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
             handled in milliseconds basis.
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure LED2 and Key Button */
  BSP_LED_Init(LED2);
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);   
  
  /* Configure the system clock to 2 Mhz (Up to 32MHZ possible) */
  SystemClock_Config();
  
  /*##-1- Configure the RNG peripheral #######################################*/
  RngHandle.Instance = RNG;
  
  if(HAL_RNG_Init(&RngHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  while (1)
  {
    /*##-2- Wait until Key button is pressed #################################*/
    while(BSP_PB_GetState(BUTTON_KEY) != RESET)
    {
    }
    
    /*##-3- Loop while Key button is maintained pressed ######################*/
    while(BSP_PB_GetState(BUTTON_KEY) == RESET)
    {
    }
    
    /*##-4- Generate 8 Random 32bit Numbers ##################################*/
    for(counter = 0; counter < 8; counter++)
    {
      aRandom32bit[counter] = HAL_RNG_GetRandomNumber(&RngHandle);
    }
  }
}
Exemple #6
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  uint32_t counter = 0;
 
  /* STM32F2xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
   
  /* Configure the system clock to 168 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);
  
  /* Configure Key Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
  
  /*##-1- Configure the RNG peripheral #######################################*/
  RngHandle.Instance = RNG;
  
  if(HAL_RNG_Init(&RngHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /* Infinite loop */  
  while (1)
  {
    /*##-2- Wait until Key button is pressed #################################*/
    while(BSP_PB_GetState(BUTTON_KEY) != RESET)
    {
    }
    
    /*##-3- Loop while Key button is maintained pressed ######################*/
    while(BSP_PB_GetState(BUTTON_KEY) == RESET)
    {
    }
    
    /*##-4- Generate 8 Random 32bit Numbers ##################################*/
    for(counter = 0; counter < 8; counter++)
    {
      aRandom32bit[counter] = HAL_RNG_GetRandomNumber(&RngHandle);
    }
  }
}
Exemple #7
0
/**
  * @brief  Initializes the STM32756G-EVAL's LCD and LEDs resources.
  * @param  None
  * @retval None
  */
static void BSP_Config(void)
{
  /* Initialize RNG peripheral */
  RngHandle.Instance = RNG;
  HAL_RNG_Init(&RngHandle);

  /* UART configuration */
  UartHandle.Instance          = USART1;
  UartHandle.Init.BaudRate     = 115200;
  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits     = UART_STOPBITS_1;
  UartHandle.Init.Parity       = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    
  /* Initialize UART peripheral */
  HAL_UART_Init(&UartHandle);
  
  /* Configures COM1 port */
  BSP_COM_Init(COM1, &UartHandle);
  
  /* Configure LED1, LED2, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
  /* Init IO Expander */
  BSP_IO_Init();
  
  /* Enable IO Expander interrupt for ETH MII pin */
  BSP_IO_ConfigPin(MII_INT_PIN, IO_MODE_IT_FALLING_EDGE);
  
#ifdef USE_LCD

  /* Initialize the LCD */
  BSP_LCD_Init();
  
  /* Initialize the LCD Layers */
  BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS);
  
  /* Set LCD Foreground Layer  */
  BSP_LCD_SelectLayer(1);
  
  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  
  /* Clear the LCD */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Display message on the LCD*/
  BSP_LCD_DisplayStringAtLine(0, (uint8_t*)"        STM32F756xx          ");
  BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"      STM32F-7 Series        ");
  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)"      SSL Server demo        ");
  BSP_LCD_DisplayStringAtLine(3, (uint8_t*)"      using HW Crypto        ");

#endif
}
Exemple #8
0
/**
  * @brief  BSP configuration.
  * @param  None
  * @retval None
  */
static void BSP_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Enable PB14 to IT mode: Ethernet Link interrupt */ 
  __GPIOB_CLK_ENABLE(); 
  GPIO_InitStructure.Pin = GPIO_PIN_14;
  GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStructure.Pull = GPIO_NOPULL ;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Enable EXTI Line interrupt */
  HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0xF, 0);
  HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  
  /* Initialize RNG peripheral */
  HAL_RNG_Init(&RngHandle);
  
  /* UART configuration */
  UartHandle.Instance        = USART1;
  UartHandle.Init.BaudRate   = 9600;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX;
  
  /* Initialize UART peripheral */
  HAL_UART_Init(&UartHandle);
  
  /* Configures COM1 port */
  BSP_COM_Init(COM1, &UartHandle);
  
  /* Initialize STM324x9I-EVAL's LEDs */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  
#ifdef USE_LCD
  
  /* Initialize the LCD */
  BSP_LCD_Init();
  
  BSP_LCD_SetFont(&Font20);
  
  /* Clear the LCD */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Display message on the LCD*/
  BSP_LCD_DisplayStringAtLine(0, (uint8_t*)"     STM32F417xx    ");
  BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"   STM32F-4 Series  ");
  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)"   SSL Server demo  ");
  BSP_LCD_DisplayStringAtLine(3, (uint8_t*)"   using HW Crypto  ");
  
#endif
}
Exemple #9
0
/**
  * @brief  Initializes the STM32479I-EVAL's LCD and LEDs resources.
  * @param  None
  * @retval None
  */
static void BSP_Config(void)
{
#ifdef USE_LCD
  uint8_t lcd_status = LCD_OK;
  uint8_t  sdram_status = SDRAM_OK;
#endif /* USE_LCD */

  /* Initialize RNG peripheral */
  HAL_RNG_Init(&RngHandle);

  /* UART configuration */
  UartHandle.Instance          = USART1;
  UartHandle.Init.BaudRate     = 9600;
  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits     = UART_STOPBITS_1;
  UartHandle.Init.Parity       = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    
  /* Initialize UART peripheral */
  HAL_UART_Init(&UartHandle);
  
  /* Configures COM1 port */
  BSP_COM_Init(COM1, &UartHandle);

  /* Configure LED1, LED2, LED3 and LED4 */
  BSP_LED_Init(LED1);
  BSP_LED_Init(LED2);
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);

  /* Set Systick Interrupt to the highest priority */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0xF, 0x0);

  /* Init IO Expander (MFX) */
  BSP_IO_Init();

  /* Enable IO Expander (MFX) interrupt for ETH MII pin */
  BSP_IO_ConfigPin(MII_INT_PIN, IO_MODE_IT_FALLING_EDGE);

#ifdef USE_LCD

  /* Initialize the SDRAM */
  sdram_status = BSP_SDRAM_Init();
  if(sdram_status != SDRAM_OK)
  {
	  Error_Handler();
  }

  /* Initialize LCD in landscape mode in DSI mode video burst */
  /* Initialize and start the LCD display in mode 'lcd_mode'
   *  Using LCD_FB_START_ADDRESS as frame buffer displayed contents.
   *  This buffer is modified by the BSP (draw fonts, objects depending on BSP calls).
   */

  /* Set Portrait orientation if needed, by default orientation is set to
     Landscape */
  
  /* Initialize DSI LCD */
  //  BSP_LCD_InitEx(LCD_ORIENTATION_PORTRAIT); /* uncomment if Portrait orientation is needed */
  BSP_LCD_Init(); /* Uncomment if default config (landscape orientation) is needed */
  while(lcd_status != LCD_OK);

  BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS);   

  /* Set LCD Foreground Layer as active one */
  BSP_LCD_SelectLayer(1);

  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);

  /* Clear the LCD */
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  
  /* Set the LCD Back Color */
  BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
  
  /* Set the LCD Text Color */
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
  
  /* Display message on the LCD*/
  BSP_LCD_DisplayStringAtLine(0, (uint8_t*)"                STM32F479xx          ");
  BSP_LCD_DisplayStringAtLine(1, (uint8_t*)"              STM32F-4 Series        ");
  BSP_LCD_DisplayStringAtLine(2, (uint8_t*)"              SSL Client demo        ");
  BSP_LCD_DisplayStringAtLine(3, (uint8_t*)"              using HW Crypto        ");
  BSP_LCD_DisplayStringAtLine(8, (uint8_t*)"  Ethernet Initialization ..."); 

#endif /* USE_LCD */
}
Exemple #10
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  uint32_t counter = 0;
  
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /*## Configure peripherals #################################################*/
  
  /* Initialize LEDs on board */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED1);

  /* Configure Tamper push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_EXTI);
  
    /*## Configure the RNG peripheral #######################################*/
  RngHandle.Instance = RNG;
  
  /* DeInitialize the RNG peripheral */
  if (HAL_RNG_DeInit(&RngHandle) != HAL_OK)
  {
    /* DeInitialization Error */
    Error_Handler();
  }    

  /* Initialize the RNG peripheral */
  if (HAL_RNG_Init(&RngHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while (1)
  {

    /* Wait for event on push button to perform following actions */
    while ((ubUserButtonClickEvent) == RESET)
    {
      __NOP();
    }
    /* Reset variable for next loop iteration */
    ubUserButtonClickEvent = RESET;
    
 
     /*## Generate eight 32-bit long random numbers ##################################*/
    for (counter = 0; counter < 8; counter++)
    {
      if (HAL_RNG_GenerateRandomNumber(&RngHandle, &aRandom32bit[counter]) != HAL_OK)
      {
        /* Random number generation error */
        Error_Handler();      
      }
    }
   
  }
}
Exemple #11
0
void rng_init(void) {
    __RNG_CLK_ENABLE();
    HAL_RNG_Init(&RNGHandle);
}