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
)) {
68 // If all the other methods have failed, use the default method from the RngProtocol
69 Status
= RngProtocol
->GetRNG (RngProtocol
, NULL
, BufferSize
, Buffer
);
70 DEBUG ((DEBUG_INFO
, "%a: GetRNG algorithm Hash-256 - Status = %r\n", __FUNCTION__
, Status
));
71 if (!EFI_ERROR (Status
)) {
75 // If we get to this point, we have failed
76 DEBUG ((DEBUG_ERROR
, "%a: GetRNG() failed, staus = %r\n", __FUNCTION__
, Status
));
79 }// GenerateRandomNumberViaNist800Algorithm()
82 Generates a 16-bit random number.
84 if Rand is NULL, return FALSE.
86 @param[out] Rand Buffer pointer to store the 16-bit random value.
88 @retval TRUE Random number generated successfully.
89 @retval FALSE Failed to generate the random number.
104 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, sizeof (UINT16
));
105 if (EFI_ERROR (Status
)) {
113 Generates a 32-bit random number.
115 if Rand is NULL, return FALSE.
117 @param[out] Rand Buffer pointer to store the 32-bit random value.
119 @retval TRUE Random number generated successfully.
120 @retval FALSE Failed to generate the random number.
135 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, sizeof (UINT32
));
136 if (EFI_ERROR (Status
)) {
144 Generates a 64-bit random number.
146 if Rand is NULL, return FALSE.
148 @param[out] Rand Buffer pointer to store the 64-bit random value.
150 @retval TRUE Random number generated successfully.
151 @retval FALSE Failed to generate the random number.
166 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, sizeof (UINT64
));
167 if (EFI_ERROR (Status
)) {
175 Generates a 128-bit random number.
177 if Rand is NULL, return FALSE.
179 @param[out] Rand Buffer pointer to store the 128-bit random value.
181 @retval TRUE Random number generated successfully.
182 @retval FALSE Failed to generate the random number.
197 Status
= GenerateRandomNumberViaNist800Algorithm ((UINT8
*)Rand
, 2 * sizeof (UINT64
));
198 if (EFI_ERROR (Status
)) {