ArmRngDxe.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /** @file
  2. RNG Driver to produce the UEFI Random Number Generator protocol.
  3. The driver can use RNDR instruction (through the RngLib and if FEAT_RNG is
  4. present) to produce random numbers. It also uses the Arm FW-TRNG interface
  5. to implement EFI_RNG_ALGORITHM_RAW.
  6. RNG Algorithms defined in UEFI 2.4:
  7. - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
  8. - EFI_RNG_ALGORITHM_RAW
  9. - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
  10. - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
  11. - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported
  12. - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported
  13. Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
  14. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
  15. (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
  16. Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
  17. SPDX-License-Identifier: BSD-2-Clause-Patent
  18. **/
  19. #include <Library/BaseLib.h>
  20. #include <Library/BaseMemoryLib.h>
  21. #include <Library/DebugLib.h>
  22. #include <Library/MemoryAllocationLib.h>
  23. #include <Library/UefiBootServicesTableLib.h>
  24. #include <Library/RngLib.h>
  25. #include <Protocol/Rng.h>
  26. #include "RngDxeInternals.h"
  27. /** Free mAvailableAlgoArray.
  28. **/
  29. VOID
  30. EFIAPI
  31. FreeAvailableAlgorithms (
  32. VOID
  33. )
  34. {
  35. FreePool (mAvailableAlgoArray);
  36. return;
  37. }
  38. /**
  39. Produces and returns an RNG value using either the default or specified RNG algorithm.
  40. @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
  41. @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
  42. algorithm to use. May be NULL in which case the function will
  43. use its default RNG algorithm.
  44. @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
  45. RNGValue. The driver shall return exactly this numbers of bytes.
  46. @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
  47. resulting RNG value.
  48. @retval EFI_SUCCESS The RNG value was returned successfully.
  49. @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
  50. this driver.
  51. @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
  52. firmware error.
  53. @retval EFI_NOT_READY There is not enough random data available to satisfy the length
  54. requested by RNGValueLength.
  55. @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
  56. **/
  57. EFI_STATUS
  58. EFIAPI
  59. RngGetRNG (
  60. IN EFI_RNG_PROTOCOL *This,
  61. IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
  62. IN UINTN RNGValueLength,
  63. OUT UINT8 *RNGValue
  64. )
  65. {
  66. EFI_STATUS Status;
  67. UINTN Index;
  68. if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
  69. return EFI_INVALID_PARAMETER;
  70. }
  71. if (RNGAlgorithm == NULL) {
  72. //
  73. // Use the default RNG algorithm if RNGAlgorithm is NULL.
  74. //
  75. for (Index = 0; Index < mAvailableAlgoArrayCount; Index++) {
  76. if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {
  77. RNGAlgorithm = &mAvailableAlgoArray[Index];
  78. goto FoundAlgo;
  79. }
  80. }
  81. if (Index == mAvailableAlgoArrayCount) {
  82. // No algorithm available.
  83. ASSERT (Index != mAvailableAlgoArrayCount);
  84. return EFI_DEVICE_ERROR;
  85. }
  86. }
  87. FoundAlgo:
  88. if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
  89. Status = RngGetBytes (RNGValueLength, RNGValue);
  90. return Status;
  91. }
  92. // Raw algorithm (Trng)
  93. if (CompareGuid (RNGAlgorithm, &gEfiRngAlgorithmRaw)) {
  94. return GenerateEntropy (RNGValueLength, RNGValue);
  95. }
  96. //
  97. // Other algorithms are unsupported by this driver.
  98. //
  99. return EFI_UNSUPPORTED;
  100. }
  101. /**
  102. Returns information about the random number generation implementation.
  103. @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
  104. @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
  105. On output with a return code of EFI_SUCCESS, the size
  106. in bytes of the data returned in RNGAlgorithmList. On output
  107. with a return code of EFI_BUFFER_TOO_SMALL,
  108. the size of RNGAlgorithmList required to obtain the list.
  109. @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
  110. with one EFI_RNG_ALGORITHM element for each supported
  111. RNG algorithm. The list must not change across multiple
  112. calls to the same driver. The first algorithm in the list
  113. is the default algorithm for the driver.
  114. @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
  115. @retval EFI_UNSUPPORTED The services is not supported by this driver.
  116. @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
  117. hardware or firmware error.
  118. @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
  119. @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
  120. **/
  121. EFI_STATUS
  122. EFIAPI
  123. RngGetInfo (
  124. IN EFI_RNG_PROTOCOL *This,
  125. IN OUT UINTN *RNGAlgorithmListSize,
  126. OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
  127. )
  128. {
  129. UINTN RequiredSize;
  130. if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
  131. return EFI_INVALID_PARAMETER;
  132. }
  133. RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);
  134. if (RequiredSize == 0) {
  135. // No supported algorithms found.
  136. return EFI_UNSUPPORTED;
  137. }
  138. if (*RNGAlgorithmListSize < RequiredSize) {
  139. *RNGAlgorithmListSize = RequiredSize;
  140. return EFI_BUFFER_TOO_SMALL;
  141. }
  142. if (RNGAlgorithmList == NULL) {
  143. return EFI_INVALID_PARAMETER;
  144. }
  145. // There is no gap in the array, so copy the block.
  146. CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);
  147. *RNGAlgorithmListSize = RequiredSize;
  148. return EFI_SUCCESS;
  149. }