2 Provides an implementation of the library class RngLib that uses the Rng protocol.
4 Copyright (c) Microsoft Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/UefiBootServicesTableLib.h>
10 #include <Library/DebugLib.h>
11 #include <Library/RngLib.h>
12 #include <Protocol/Rng.h>
17 Generates a random number via the NIST
18 800-9A algorithm. Refer to
19 http://csrc.nist.gov/groups/STM/cavp/documents/drbg/DRBGVS.pdf
22 @param[out] Buffer Buffer to receive the random number.
23 @param[in] BufferSize Number of bytes in Buffer.
25 @retval EFI_SUCCESS or underlying failure code.
29 GenerateRandomNumberViaNist800Algorithm (
35 EFI_RNG_PROTOCOL
*RngProtocol
;
40 DEBUG((DEBUG_ERROR
, "%a: Buffer == NULL.\n", __FUNCTION__
));
41 return EFI_INVALID_PARAMETER
;
44 Status
= gBS
->LocateProtocol (&gEfiRngProtocolGuid
, NULL
, (VOID
**)&RngProtocol
);
45 if (EFI_ERROR (Status
) || RngProtocol
== NULL
) {
46 DEBUG((DEBUG_ERROR
, "%a: Could not locate RNG prototocol, Status = %r\n", __FUNCTION__
, Status
));
50 Status
= RngProtocol
->GetRNG (RngProtocol
, &gEfiRngAlgorithmSp80090Ctr256Guid
, BufferSize
, Buffer
);
51 DEBUG((DEBUG_INFO
, "%a: GetRNG algorithm CTR-256 - Status = %r\n", __FUNCTION__
, Status
));
52 if (!EFI_ERROR (Status
)) {
56 Status
= RngProtocol
->GetRNG (RngProtocol
, &gEfiRngAlgorithmSp80090Hmac256Guid
, BufferSize
, Buffer
);
57 DEBUG((DEBUG_INFO
, "%a: GetRNG algorithm HMAC-256 - Status = %r\n", __FUNCTION__
, Status
));
58 if (!EFI_ERROR (Status
)) {
62 Status
= RngProtocol
->GetRNG (RngProtocol
, &gEfiRngAlgorithmSp80090Hash256Guid
, BufferSize
, Buffer
);
63 DEBUG((DEBUG_INFO
, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__
, Status
));
64 if (!EFI_ERROR (Status
)) {
67 // If all the other methods have failed, use the default method from the RngProtocol
68 Status
= RngProtocol
->GetRNG (RngProtocol
, NULL
, BufferSize
, Buffer
);
69 DEBUG((DEBUG_INFO
, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__
, Status
));
70 if (!EFI_ERROR (Status
)) {
73 // If we get to this point, we have failed
74 DEBUG((DEBUG_ERROR
, "%a: GetRNG() failed, staus = %r\n", __FUNCTION__
, Status
));
77 }// GenerateRandomNumberViaNist800Algorithm()
81 Generates a 16-bit random number.
83 if Rand is NULL, return FALSE.
85 @param[out] Rand Buffer pointer to store the 16-bit random value.
87 @retval TRUE Random number generated successfully.
88 @retval FALSE Failed to generate the random number.
104 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, sizeof(UINT16
));
105 if (EFI_ERROR (Status
)) {
112 Generates a 32-bit random number.
114 if Rand is NULL, return FALSE.
116 @param[out] Rand Buffer pointer to store the 32-bit random value.
118 @retval TRUE Random number generated successfully.
119 @retval FALSE Failed to generate the random number.
134 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, sizeof(UINT32
));
135 if (EFI_ERROR (Status
)) {
142 Generates a 64-bit random number.
144 if Rand is NULL, return FALSE.
146 @param[out] Rand Buffer pointer to store the 64-bit random value.
148 @retval TRUE Random number generated successfully.
149 @retval FALSE Failed to generate the random number.
164 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, sizeof(UINT64
));
165 if (EFI_ERROR (Status
)) {
172 Generates a 128-bit random number.
174 if Rand is NULL, return FALSE.
176 @param[out] Rand Buffer pointer to store the 128-bit random value.
178 @retval TRUE Random number generated successfully.
179 @retval FALSE Failed to generate the random number.
194 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, 2 * sizeof(UINT64
));
195 if (EFI_ERROR (Status
)) {