コード例 #1
0
/** 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 );
}
コード例 #2
0
ファイル: main.c プロジェクト: pierreroth64/STM32Cube_FW_F4
/**
  * @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();      
      }
    }
   
  }
}