trng_api.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Hardware entropy collector for the STM32 families
  3. *
  4. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. */
  20. #if defined(DEVICE_TRNG)
  21. #include <stdlib.h>
  22. #include "cmsis.h"
  23. #include "trng_api.h"
  24. #include "mbed_error.h"
  25. #include "mbed_critical.h"
  26. static uint8_t users = 0;
  27. void trng_init(trng_t *obj)
  28. {
  29. uint32_t dummy;
  30. /* We're only supporting a single user of RNG */
  31. if (core_util_atomic_incr_u8(&users, 1) > 1) {
  32. error("Only 1 RNG instance supported\r\n");
  33. }
  34. #if defined(TARGET_STM32L4)
  35. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  36. /*Select PLLQ output as RNG clock source */
  37. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RNG;
  38. PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_PLL;
  39. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  40. #endif
  41. /* RNG Peripheral clock enable */
  42. __HAL_RCC_RNG_CLK_ENABLE();
  43. /* Initialize RNG instance */
  44. obj->handle.Instance = RNG;
  45. obj->handle.State = HAL_RNG_STATE_RESET;
  46. obj->handle.Lock = HAL_UNLOCKED;
  47. HAL_RNG_Init(&obj->handle);
  48. /* first random number generated after setting the RNGEN bit should not be used */
  49. HAL_RNG_GenerateRandomNumber(&obj->handle, &dummy);
  50. }
  51. void trng_free(trng_t *obj)
  52. {
  53. /*Disable the RNG peripheral */
  54. HAL_RNG_DeInit(&obj->handle);
  55. /* RNG Peripheral clock disable - assume we're the only users of RNG */
  56. __HAL_RCC_RNG_CLK_DISABLE();
  57. users = 0;
  58. }
  59. int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
  60. {
  61. int ret = 0;
  62. volatile uint8_t random[4];
  63. *output_length = 0;
  64. /* Get Random byte */
  65. while ((*output_length < length) && (ret == 0)) {
  66. if (HAL_RNG_GenerateRandomNumber(&obj->handle, (uint32_t *)random) != HAL_OK) {
  67. ret = -1;
  68. } else {
  69. for (uint8_t i = 0; (i < 4) && (*output_length < length) ; i++) {
  70. *output++ = random[i];
  71. *output_length += 1;
  72. random[i] = 0;
  73. }
  74. }
  75. }
  76. /* Just be extra sure that we didn't do it wrong */
  77. if ((__HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS))) != 0) {
  78. ret = -1;
  79. }
  80. return (ret);
  81. }
  82. #endif