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