]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeRngLib/DxeRngLib.c
9c3d67b5a62d2e8099fef948fd306610e8f7739c
[mirror_edk2.git] / MdePkg / Library / DxeRngLib / DxeRngLib.c
1 /** @file
2 Provides an implementation of the library class RngLib that uses the Rng protocol.
3
4 Copyright (c) Microsoft Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8 #include <Uefi.h>
9 #include <Library/UefiBootServicesTableLib.h>
10 #include <Library/DebugLib.h>
11 #include <Library/RngLib.h>
12 #include <Protocol/Rng.h>
13
14 /**
15 Routine Description:
16
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
20 for more information.
21
22 @param[out] Buffer Buffer to receive the random number.
23 @param[in] BufferSize Number of bytes in Buffer.
24
25 @retval EFI_SUCCESS or underlying failure code.
26 **/
27 STATIC
28 EFI_STATUS
29 GenerateRandomNumberViaNist800Algorithm (
30 OUT UINT8 *Buffer,
31 IN UINTN BufferSize
32 )
33 {
34 EFI_STATUS Status;
35 EFI_RNG_PROTOCOL *RngProtocol;
36
37 RngProtocol = NULL;
38
39 if (Buffer == NULL) {
40 DEBUG((DEBUG_ERROR, "%a: Buffer == NULL.\n", __FUNCTION__));
41 return EFI_INVALID_PARAMETER;
42 }
43
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));
47 return Status;
48 }
49
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)) {
53 return Status;
54 }
55
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)) {
59 return Status;
60 }
61
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)) {
65 return Status;
66 }
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)) {
71 return Status;
72 }
73 // If we get to this point, we have failed
74 DEBUG((DEBUG_ERROR, "%a: GetRNG() failed, staus = %r\n", __FUNCTION__, Status));
75
76 return Status;
77 }// GenerateRandomNumberViaNist800Algorithm()
78
79
80 /**
81 Generates a 16-bit random number.
82
83 if Rand is NULL, return FALSE.
84
85 @param[out] Rand Buffer pointer to store the 16-bit random value.
86
87 @retval TRUE Random number generated successfully.
88 @retval FALSE Failed to generate the random number.
89
90 **/
91 BOOLEAN
92 EFIAPI
93 GetRandomNumber16 (
94 OUT UINT16 *Rand
95 )
96 {
97 EFI_STATUS Status;
98
99 if (Rand == NULL)
100 {
101 return FALSE;
102 }
103
104 Status = GenerateRandomNumberViaNist800Algorithm ((UINT8 *)Rand, sizeof(UINT16));
105 if (EFI_ERROR (Status)) {
106 return FALSE;
107 }
108 return TRUE;
109 }
110
111 /**
112 Generates a 32-bit random number.
113
114 if Rand is NULL, return FALSE.
115
116 @param[out] Rand Buffer pointer to store the 32-bit random value.
117
118 @retval TRUE Random number generated successfully.
119 @retval FALSE Failed to generate the random number.
120
121 **/
122 BOOLEAN
123 EFIAPI
124 GetRandomNumber32 (
125 OUT UINT32 *Rand
126 )
127 {
128 EFI_STATUS Status;
129
130 if (Rand == NULL) {
131 return FALSE;
132 }
133
134 Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, sizeof(UINT32));
135 if (EFI_ERROR (Status)) {
136 return FALSE;
137 }
138 return TRUE;
139 }
140
141 /**
142 Generates a 64-bit random number.
143
144 if Rand is NULL, return FALSE.
145
146 @param[out] Rand Buffer pointer to store the 64-bit random value.
147
148 @retval TRUE Random number generated successfully.
149 @retval FALSE Failed to generate the random number.
150
151 **/
152 BOOLEAN
153 EFIAPI
154 GetRandomNumber64 (
155 OUT UINT64 *Rand
156 )
157 {
158 EFI_STATUS Status;
159
160 if (Rand == NULL) {
161 return FALSE;
162 }
163
164 Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, sizeof(UINT64));
165 if (EFI_ERROR (Status)) {
166 return FALSE;
167 }
168 return TRUE;
169 }
170
171 /**
172 Generates a 128-bit random number.
173
174 if Rand is NULL, return FALSE.
175
176 @param[out] Rand Buffer pointer to store the 128-bit random value.
177
178 @retval TRUE Random number generated successfully.
179 @retval FALSE Failed to generate the random number.
180
181 **/
182 BOOLEAN
183 EFIAPI
184 GetRandomNumber128 (
185 OUT UINT64 *Rand
186 )
187 {
188 EFI_STATUS Status;
189
190 if (Rand == NULL) {
191 return FALSE;
192 }
193
194 Status = GenerateRandomNumberViaNist800Algorithm ((UINT8*)Rand, 2 * sizeof(UINT64));
195 if (EFI_ERROR (Status)) {
196 return FALSE;
197 }
198 return TRUE;
199 }