ArmTrngLib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /** @file
  2. Arm Firmware TRNG interface library.
  3. Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. @par Reference(s):
  6. - [1] Arm True Random Number Generator Firmware, Interface 1.0,
  7. Platform Design Document.
  8. (https://developer.arm.com/documentation/den0098/latest/)
  9. - [2] NIST Special Publication 800-90B, Recommendation for the Entropy
  10. Sources Used for Random Bit Generation.
  11. (https://csrc.nist.gov/publications/detail/sp/800-90b/final)
  12. @par Glossary:
  13. - TRNG - True Random Number Generator
  14. - FID - Function ID
  15. **/
  16. #include <Base.h>
  17. #include <Library/ArmLib.h>
  18. #include <Library/ArmMonitorLib.h>
  19. #include <Library/BaseMemoryLib.h>
  20. #include <Library/DebugLib.h>
  21. #include "ArmTrngDefs.h"
  22. /** Convert TRNG status codes to RETURN status codes.
  23. @param [in] TrngStatus TRNG status code.
  24. @retval RETURN_SUCCESS Success.
  25. @retval RETURN_UNSUPPORTED Function not implemented or
  26. negative return code.
  27. @retval RETURN_INVALID_PARAMETER A parameter is invalid.
  28. @retval RETURN_NOT_READY No Entropy available.
  29. **/
  30. STATIC
  31. RETURN_STATUS
  32. TrngStatusToReturnStatus (
  33. IN INT32 TrngStatus
  34. )
  35. {
  36. switch (TrngStatus) {
  37. case TRNG_STATUS_NOT_SUPPORTED:
  38. return RETURN_UNSUPPORTED;
  39. case TRNG_STATUS_INVALID_PARAMETER:
  40. return RETURN_INVALID_PARAMETER;
  41. case TRNG_STATUS_NO_ENTROPY:
  42. return RETURN_NOT_READY;
  43. case TRNG_STATUS_SUCCESS:
  44. return RETURN_SUCCESS;
  45. default:
  46. if (TrngStatus < 0) {
  47. return RETURN_UNSUPPORTED;
  48. }
  49. return RETURN_SUCCESS;
  50. }
  51. }
  52. /** Get the version of the Arm TRNG backend.
  53. A TRNG may be implemented by the system firmware, in which case this
  54. function shall return the version of the Arm TRNG backend.
  55. The implementation must return NOT_SUPPORTED if a Back end is not present.
  56. @param [out] MajorRevision Major revision.
  57. @param [out] MinorRevision Minor revision.
  58. @retval RETURN_SUCCESS The function completed successfully.
  59. @retval RETURN_INVALID_PARAMETER Invalid parameter.
  60. @retval RETURN_UNSUPPORTED Backend not present.
  61. **/
  62. RETURN_STATUS
  63. EFIAPI
  64. GetArmTrngVersion (
  65. OUT UINT16 *MajorRevision,
  66. OUT UINT16 *MinorRevision
  67. )
  68. {
  69. RETURN_STATUS Status;
  70. ARM_MONITOR_ARGS Parameters;
  71. INT32 Revision;
  72. if ((MajorRevision == NULL) || (MinorRevision == NULL)) {
  73. return RETURN_INVALID_PARAMETER;
  74. }
  75. ZeroMem (&Parameters, sizeof (Parameters));
  76. Parameters.Arg0 = ARM_SMC_ID_TRNG_VERSION;
  77. ArmMonitorCall (&Parameters);
  78. Revision = (INT32)Parameters.Arg0;
  79. Status = TrngStatusToReturnStatus (Revision);
  80. if (RETURN_ERROR (Status)) {
  81. return Status;
  82. }
  83. *MinorRevision = (Revision & TRNG_REV_MINOR_MASK);
  84. *MajorRevision = ((Revision >> TRNG_REV_MAJOR_SHIFT) & TRNG_REV_MAJOR_MASK);
  85. return RETURN_SUCCESS;
  86. }
  87. /** Get the features supported by the Arm TRNG backend.
  88. The caller can determine if functions defined in the Arm TRNG ABI are
  89. present in the ABI implementation.
  90. @param [in] FunctionId Function Id.
  91. @param [out] Capability Function specific capability if present.
  92. @retval RETURN_SUCCESS The function completed successfully.
  93. @retval RETURN_INVALID_PARAMETER Invalid parameter.
  94. @retval RETURN_UNSUPPORTED Function not implemented.
  95. **/
  96. STATIC
  97. RETURN_STATUS
  98. EFIAPI
  99. GetArmTrngFeatures (
  100. IN CONST UINT32 FunctionId,
  101. OUT UINT32 *Capability OPTIONAL
  102. )
  103. {
  104. ARM_MONITOR_ARGS Parameters;
  105. RETURN_STATUS Status;
  106. ZeroMem (&Parameters, sizeof (Parameters));
  107. Parameters.Arg0 = ARM_SMC_ID_TRNG_FEATURES;
  108. Parameters.Arg1 = FunctionId;
  109. ArmMonitorCall (&Parameters);
  110. Status = TrngStatusToReturnStatus (Parameters.Arg0);
  111. if (RETURN_ERROR (Status)) {
  112. return Status;
  113. }
  114. if (Capability != NULL) {
  115. *Capability = (UINT32)Parameters.Arg0;
  116. }
  117. return RETURN_SUCCESS;
  118. }
  119. /** Get the UUID of the Arm TRNG backend.
  120. A TRNG may be implemented by the system firmware, in which case this
  121. function shall return the UUID of the TRNG backend.
  122. Returning the Arm TRNG UUID is optional and if not implemented,
  123. RETURN_UNSUPPORTED shall be returned.
  124. Note: The caller must not rely on the returned UUID as a trustworthy Arm TRNG
  125. Back end identity
  126. @param [out] Guid UUID of the Arm TRNG backend.
  127. @retval RETURN_SUCCESS The function completed successfully.
  128. @retval RETURN_INVALID_PARAMETER Invalid parameter.
  129. @retval RETURN_UNSUPPORTED Function not implemented.
  130. **/
  131. RETURN_STATUS
  132. EFIAPI
  133. GetArmTrngUuid (
  134. OUT GUID *Guid
  135. )
  136. {
  137. ARM_MONITOR_ARGS Parameters;
  138. if (Guid == NULL) {
  139. return RETURN_INVALID_PARAMETER;
  140. }
  141. ZeroMem (&Parameters, sizeof (Parameters));
  142. Parameters.Arg0 = ARM_SMC_ID_TRNG_GET_UUID;
  143. ArmMonitorCall (&Parameters);
  144. // Only invalid value is TRNG_STATUS_NOT_SUPPORTED (-1).
  145. if ((INT32)Parameters.Arg0 == TRNG_STATUS_NOT_SUPPORTED) {
  146. return TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
  147. }
  148. Guid->Data1 = (Parameters.Arg0 & MAX_UINT32);
  149. Guid->Data2 = (Parameters.Arg1 & MAX_UINT16);
  150. Guid->Data3 = ((Parameters.Arg1 >> 16) & MAX_UINT16);
  151. Guid->Data4[0] = (Parameters.Arg2 & MAX_UINT8);
  152. Guid->Data4[1] = ((Parameters.Arg2 >> 8) & MAX_UINT8);
  153. Guid->Data4[2] = ((Parameters.Arg2 >> 16) & MAX_UINT8);
  154. Guid->Data4[3] = ((Parameters.Arg2 >> 24) & MAX_UINT8);
  155. Guid->Data4[4] = (Parameters.Arg3 & MAX_UINT8);
  156. Guid->Data4[5] = ((Parameters.Arg3 >> 8) & MAX_UINT8);
  157. Guid->Data4[6] = ((Parameters.Arg3 >> 16) & MAX_UINT8);
  158. Guid->Data4[7] = ((Parameters.Arg3 >> 24) & MAX_UINT8);
  159. DEBUG ((DEBUG_INFO, "FW-TRNG: UUID %g\n", Guid));
  160. return RETURN_SUCCESS;
  161. }
  162. /** Returns maximum number of entropy bits that can be returned in a single
  163. call.
  164. @return Returns the maximum number of Entropy bits that can be returned
  165. in a single call to GetArmTrngEntropy().
  166. **/
  167. UINTN
  168. EFIAPI
  169. GetArmTrngMaxSupportedEntropyBits (
  170. VOID
  171. )
  172. {
  173. return MAX_ENTROPY_BITS;
  174. }
  175. /** Returns N bits of conditioned entropy.
  176. See [2] Section 2.3.1 GetEntropy: An Interface to the Entropy Source
  177. GetEntropy
  178. Input:
  179. bits_of_entropy: the requested amount of entropy
  180. Output:
  181. entropy_bitstring: The string that provides the requested entropy.
  182. status: A Boolean value that is TRUE if the request has been satisfied,
  183. and is FALSE otherwise.
  184. @param [in] EntropyBits Number of entropy bits requested.
  185. @param [in] BufferSize Size of the Buffer in bytes.
  186. @param [out] Buffer Buffer to return the entropy bits.
  187. @retval RETURN_SUCCESS The function completed successfully.
  188. @retval RETURN_INVALID_PARAMETER Invalid parameter.
  189. @retval RETURN_UNSUPPORTED Function not implemented.
  190. @retval RETURN_BAD_BUFFER_SIZE Buffer size is too small.
  191. @retval RETURN_NOT_READY No Entropy available.
  192. **/
  193. RETURN_STATUS
  194. EFIAPI
  195. GetArmTrngEntropy (
  196. IN UINTN EntropyBits,
  197. IN UINTN BufferSize,
  198. OUT UINT8 *Buffer
  199. )
  200. {
  201. RETURN_STATUS Status;
  202. ARM_MONITOR_ARGS Parameters;
  203. UINTN EntropyBytes;
  204. UINTN LastValidBits;
  205. UINTN BytesToClear;
  206. UINTN EntropyData[3];
  207. if ((EntropyBits == 0) ||
  208. (EntropyBits > MAX_ENTROPY_BITS) ||
  209. (Buffer == NULL))
  210. {
  211. return RETURN_INVALID_PARAMETER;
  212. }
  213. EntropyBytes = (EntropyBits + 7) >> 3;
  214. if (EntropyBytes > BufferSize) {
  215. return RETURN_BAD_BUFFER_SIZE;
  216. }
  217. ZeroMem (Buffer, BufferSize);
  218. ZeroMem (&Parameters, sizeof (Parameters));
  219. Parameters.Arg0 = ARM_SMC_ID_TRNG_RND;
  220. Parameters.Arg1 = EntropyBits;
  221. ArmMonitorCall (&Parameters);
  222. Status = TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
  223. if (RETURN_ERROR (Status)) {
  224. return Status;
  225. }
  226. // The entropy data is returned in the Parameters.Arg<3..1>
  227. // With the lower order bytes in Parameters.Arg3 and the higher
  228. // order bytes being stored in Parameters.Arg1.
  229. EntropyData[0] = Parameters.Arg3;
  230. EntropyData[1] = Parameters.Arg2;
  231. EntropyData[2] = Parameters.Arg1;
  232. CopyMem (Buffer, EntropyData, EntropyBytes);
  233. // Mask off any unused top bytes, in accordance with specification.
  234. BytesToClear = BufferSize - EntropyBytes;
  235. if (BytesToClear != 0) {
  236. ZeroMem (&Buffer[EntropyBytes], BytesToClear);
  237. }
  238. // Clear the unused MSB bits of the last byte.
  239. LastValidBits = EntropyBits & 0x7;
  240. if (LastValidBits != 0) {
  241. Buffer[EntropyBytes - 1] &= (0xFF >> (8 - LastValidBits));
  242. }
  243. return Status;
  244. }
  245. /** The constructor checks that the FW-TRNG interface is supported
  246. by the host firmware.
  247. It will ASSERT() if FW-TRNG is not supported.
  248. It will always return RETURN_SUCCESS.
  249. @retval RETURN_SUCCESS The constructor always returns RETURN_SUCCESS.
  250. **/
  251. RETURN_STATUS
  252. EFIAPI
  253. ArmTrngLibConstructor (
  254. VOID
  255. )
  256. {
  257. ARM_MONITOR_ARGS Parameters;
  258. RETURN_STATUS Status;
  259. UINT16 MajorRev;
  260. UINT16 MinorRev;
  261. GUID Guid;
  262. ZeroMem (&Parameters, sizeof (Parameters));
  263. Parameters.Arg0 = SMCCC_VERSION;
  264. ArmMonitorCall (&Parameters);
  265. Status = TrngStatusToReturnStatus ((INT32)Parameters.Arg0);
  266. if (RETURN_ERROR (Status)) {
  267. ASSERT_RETURN_ERROR (Status);
  268. goto ErrorHandler;
  269. }
  270. // Cf [1] s2.1.3 'Caller responsibilities',
  271. // SMCCC version must be greater or equal than 1.1
  272. if ((INT32)Parameters.Arg0 < 0x10001) {
  273. ASSERT_RETURN_ERROR (RETURN_UNSUPPORTED);
  274. goto ErrorHandler;
  275. }
  276. Status = GetArmTrngVersion (&MajorRev, &MinorRev);
  277. if (RETURN_ERROR (Status)) {
  278. ASSERT_RETURN_ERROR (Status);
  279. goto ErrorHandler;
  280. }
  281. // Check that the required features are present.
  282. Status = GetArmTrngFeatures (ARM_SMC_ID_TRNG_RND, NULL);
  283. if (RETURN_ERROR (Status)) {
  284. ASSERT_RETURN_ERROR (Status);
  285. goto ErrorHandler;
  286. }
  287. // Check if TRNG UUID is supported and if so trace the GUID.
  288. Status = GetArmTrngFeatures (ARM_SMC_ID_TRNG_GET_UUID, NULL);
  289. if (RETURN_ERROR (Status)) {
  290. ASSERT_RETURN_ERROR (Status);
  291. goto ErrorHandler;
  292. }
  293. DEBUG_CODE_BEGIN ();
  294. Status = GetArmTrngUuid (&Guid);
  295. if (RETURN_ERROR (Status)) {
  296. ASSERT_RETURN_ERROR (Status);
  297. goto ErrorHandler;
  298. }
  299. DEBUG ((
  300. DEBUG_INFO,
  301. "FW-TRNG: Version %d.%d, GUID {%g}\n",
  302. MajorRev,
  303. MinorRev,
  304. &Guid
  305. ));
  306. DEBUG_CODE_END ();
  307. return RETURN_SUCCESS;
  308. ErrorHandler:
  309. DEBUG ((DEBUG_ERROR, "ArmTrngLib could not be correctly initialized.\n"));
  310. return RETURN_SUCCESS;
  311. }