]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Application/RngTest/RngTest.c
OvmfPkg: PlatformDxe: utility functions for saving / loading configuration
[mirror_edk2.git] / SecurityPkg / Application / RngTest / RngTest.c
CommitLineData
3aa8dc6c
LQ
1/** @file\r
2 UEFI RNG (Random Number Generator) Protocol test application.\r
3\r
4Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/ \r
14\r
15#include <Uefi.h>\r
16#include <Library/UefiLib.h>\r
17#include <Library/UefiApplicationEntryPoint.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Protocol/Rng.h>\r
22\r
23/**\r
24 The user Entry Point for Application. The user code starts with this function\r
25 as the real entry point for the application.\r
26\r
27 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
28 @param[in] SystemTable A pointer to the EFI System Table.\r
29 \r
30 @retval EFI_SUCCESS The entry point is executed successfully.\r
31 @retval other Some error occurs when executing this entry point.\r
32\r
33**/\r
34EFI_STATUS\r
35EFIAPI\r
36UefiMain (\r
37 IN EFI_HANDLE ImageHandle,\r
38 IN EFI_SYSTEM_TABLE *SystemTable\r
39 )\r
40{\r
41 EFI_STATUS Status;\r
42 EFI_RNG_PROTOCOL *Rng;\r
43 UINTN RngAlgListSize;\r
44 EFI_RNG_ALGORITHM RngAlgList[10];\r
45 EFI_RNG_ALGORITHM *PtrRngAlg;\r
46 UINTN RngAlgCount;\r
47 UINT8 *Rand;\r
48 UINTN RandSize;\r
49 UINTN Index;\r
50 UINTN Index2;\r
51\r
52 Status = EFI_SUCCESS;\r
53 PtrRngAlg = NULL;\r
54 Rand = NULL;\r
55 \r
56 Print (L"UEFI RNG Protocol Testing :\n");\r
57 Print (L"----------------------------\n");\r
58\r
59 //-----------------------------------------\r
60 // Basic UEFI RNG Protocol Test\r
61 //-----------------------------------------\r
62 Print (L" -- Locate UEFI RNG Protocol : ");\r
63 Status = gBS->LocateProtocol (&gEfiRngProtocolGuid, NULL, (VOID **)&Rng);\r
64 if (EFI_ERROR (Status)) {\r
65 Print (L"[Fail - Status = %r]\n", Status);\r
66 goto Exit;\r
67 } else {\r
68 Print (L"[Pass]\n");\r
69 }\r
70\r
71 //-----------------------------------------\r
72 // Rng->GetInfo() interface test.\r
73 //-----------------------------------------\r
74 \r
75 Print (L" -- Call RNG->GetInfo() interface : ");\r
76 RngAlgListSize = 0;\r
77 Status = Rng->GetInfo (Rng, &RngAlgListSize, NULL);\r
78 if (Status != EFI_BUFFER_TOO_SMALL) {\r
79 Print (L"[Fail - Status = %r]\n", Status);\r
80 }\r
81 //\r
82 // Print out the supported RNG algorithm GUIDs\r
83 //\r
84 RngAlgCount = RngAlgListSize / sizeof (EFI_RNG_ALGORITHM);\r
85 Print (L"\n >> Supported RNG Algorithm (Count = %d) : ", RngAlgCount);\r
86 Status = Rng->GetInfo (Rng, &RngAlgListSize, RngAlgList);\r
87 for (Index = 0; Index < RngAlgCount; Index++) {\r
88 PtrRngAlg = (EFI_RNG_ALGORITHM *)(&RngAlgList[Index]);\r
89 Print (L"\n %d) ", Index);\r
90 Print (L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", PtrRngAlg->Data1,\r
91 PtrRngAlg->Data2, PtrRngAlg->Data3, PtrRngAlg->Data4[0], PtrRngAlg->Data4[1],\r
92 PtrRngAlg->Data4[2], PtrRngAlg->Data4[3], PtrRngAlg->Data4[4],\r
93 PtrRngAlg->Data4[5], PtrRngAlg->Data4[6], PtrRngAlg->Data4[7]); \r
94 }\r
95\r
96 //-----------------------------------------\r
97 // Rng->GetRNG() interface test.\r
98 //-----------------------------------------\r
99 Print (L"\n -- Call RNG->GetRNG() interface : ");\r
100\r
101 //\r
102 // Allocate one buffer to store random data.\r
103 //\r
104 RandSize = 32;\r
105 Rand = AllocatePool (RandSize);\r
b6023fb1
LQ
106 if (Rand == NULL) {\r
107 goto Exit;\r
108 }\r
3aa8dc6c
LQ
109 \r
110 //\r
111 // RNG with default algorithm\r
112 //\r
113 Print (L"\n >> RNG with default algorithm : ");\r
114 Status = Rng->GetRNG (Rng, NULL, RandSize, Rand);\r
115 if (EFI_ERROR (Status)) {\r
116 Print (L"[Fail - Status = %r]", Status);\r
117 } else {\r
118 Print (L"[Pass]");\r
119 }\r
120 \r
121 //\r
122 // RNG with SP800-90-HMAC-256\r
123 //\r
124 Print (L"\n >> RNG with SP800-90-HMAC-256 : ");\r
125 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmSp80090Hmac256Guid, RandSize, Rand);\r
126 if (EFI_ERROR (Status)) {\r
127 Print (L"[Fail - Status = %r]", Status);\r
128 } else {\r
129 Print (L"[Pass]");\r
130 }\r
131\r
132 //\r
133 // RNG with SP800-90-HASH-256\r
134 //\r
135 Print (L"\n >> RNG with SP800-90-Hash-256 : ");\r
136 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmSp80090Hash256Guid, RandSize, Rand);\r
137 if (EFI_ERROR (Status)) {\r
138 Print (L"[Fail - Status = %r]", Status);\r
139 } else {\r
140 Print (L"[Pass]");\r
141 }\r
142\r
143 //\r
144 // RNG with SP800-90-CTR-256\r
145 //\r
146 Print (L"\n >> RNG with SP800-90-CTR-256 : ");\r
147 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmSp80090Ctr256Guid, RandSize, Rand);\r
148 if (EFI_ERROR (Status)) {\r
149 Print (L"[Fail - Status = %r]", Status);\r
150 } else {\r
151 Print (L"[Pass]");\r
152 }\r
153\r
154 //\r
155 // RNG with X9.31-3DES\r
156 //\r
157 Print (L"\n >> RNG with X9.31-3DES : ");\r
158 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmX9313DesGuid, RandSize, Rand);\r
159 if (EFI_ERROR (Status)) {\r
160 Print (L"[Fail - Status = %r]", Status);\r
161 } else {\r
162 Print (L"[Pass]");\r
163 }\r
164\r
165 //\r
166 // RNG with X9.31-AES\r
167 //\r
168 Print (L"\n >> RNG with X9.31-AES : ");\r
169 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmX931AesGuid, RandSize, Rand);\r
170 if (EFI_ERROR (Status)) {\r
171 Print (L"[Fail - Status = %r]", Status);\r
172 } else {\r
173 Print (L"[Pass]");\r
174 }\r
175\r
176 //\r
177 // RNG with RAW Entropy\r
178 //\r
179 Print (L"\n >> RNG with RAW Entropy : ");\r
180 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmRaw, RandSize, Rand);\r
181 if (EFI_ERROR (Status)) {\r
182 Print (L"[Fail - Status = %r]", Status);\r
183 } else {\r
184 Print (L"[Pass]");\r
185 }\r
186\r
187 //-----------------------------------------\r
188 // Random Number Generator test.\r
189 //-----------------------------------------\r
190 Print (L"\n -- Random Number Generation Test with default RNG Algorithm (20 Rounds): ");\r
191\r
192 RandSize = 1;\r
193 for (Index = 0; Index < 20; Index++) {\r
194 Status = Rng->GetRNG (Rng, NULL, RandSize, Rand);\r
195 if (EFI_ERROR (Status)) {\r
196 Print (L"[Fail - Status = %r]", Status);\r
197 break;\r
198 } else {\r
199 Print (L"\n %02d) - ", Index + 1);\r
200 for (Index2 = 0; Index2 < RandSize; Index2++) {\r
201 Print (L"%02x", Rand[Index2]);\r
202 }\r
203 }\r
204\r
205 RandSize +=1;\r
206 }\r
207\r
208 //-----------------------------------------\r
209 // Random Number Generator test.\r
210 //-----------------------------------------\r
211 Print (L"\n -- RAW Entropy Generation Test (20 Rounds) : ");\r
212\r
213 RandSize = 32;\r
214 for (Index = 0; Index < 20; Index++) {\r
215 Status = Rng->GetRNG (Rng, &gEfiRngAlgorithmRaw, RandSize, Rand);\r
216 if (EFI_ERROR (Status)) {\r
217 Print (L"[Fail - Status = %r]", Status);\r
218 break;\r
219 } else {\r
220 Print (L"\n %02d) - ", Index + 1);\r
221 for (Index2 = 0; Index2 < RandSize; Index2++) {\r
222 Print (L"%02x", Rand[Index2]);\r
223 }\r
224 }\r
225 }\r
226\r
227 Print (L"\n -- Exit UEFI RNG Protocol Test (Status = %r).\n", Status);\r
228 \r
229Exit:\r
230 if (Rand != NULL) {\r
231 FreePool (Rand);\r
232 }\r
233 return Status;\r
234}\r