2 UEFI RNG (Random Number Generator) Protocol test application.
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
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.
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>
24 The user Entry Point for Application. The user code starts with this function
25 as the real entry point for the application.
27 @param[in] ImageHandle The firmware allocated handle for the EFI image.
28 @param[in] SystemTable A pointer to the EFI System Table.
30 @retval EFI_SUCCESS The entry point is executed successfully.
31 @retval other Some error occurs when executing this entry point.
37 IN EFI_HANDLE ImageHandle
,
38 IN EFI_SYSTEM_TABLE
*SystemTable
42 EFI_RNG_PROTOCOL
*Rng
;
44 EFI_RNG_ALGORITHM RngAlgList
[10];
45 EFI_RNG_ALGORITHM
*PtrRngAlg
;
56 Print (L
"UEFI RNG Protocol Testing :\n");
57 Print (L
"----------------------------\n");
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
);
71 //-----------------------------------------
72 // Rng->GetInfo() interface test.
73 //-----------------------------------------
75 Print (L
" -- Call RNG->GetInfo() interface : ");
77 Status
= Rng
->GetInfo (Rng
, &RngAlgListSize
, NULL
);
78 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
79 Print (L
"[Fail - Status = %r]\n", Status
);
82 // Print out the supported RNG algorithm GUIDs
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]);
96 //-----------------------------------------
97 // Rng->GetRNG() interface test.
98 //-----------------------------------------
99 Print (L
"\n -- Call RNG->GetRNG() interface : ");
102 // Allocate one buffer to store random data.
105 Rand
= AllocatePool (RandSize
);
108 // RNG with default algorithm
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
);
119 // RNG with SP800-90-HMAC-256
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
);
130 // RNG with SP800-90-HASH-256
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
);
141 // RNG with SP800-90-CTR-256
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
);
152 // RNG with X9.31-3DES
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
);
163 // RNG with X9.31-AES
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
);
174 // RNG with RAW Entropy
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
);
184 //-----------------------------------------
185 // Random Number Generator test.
186 //-----------------------------------------
187 Print (L
"\n -- Random Number Generation Test with default RNG Algorithm (20 Rounds): ");
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
);
196 Print (L
"\n %02d) - ", Index
+ 1);
197 for (Index2
= 0; Index2
< RandSize
; Index2
++) {
198 Print (L
"%02x", Rand
[Index2
]);
205 //-----------------------------------------
206 // Random Number Generator test.
207 //-----------------------------------------
208 Print (L
"\n -- RAW Entropy Generation Test (20 Rounds) : ");
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
);
217 Print (L
"\n %02d) - ", Index
+ 1);
218 for (Index2
= 0; Index2
< RandSize
; Index2
++) {
219 Print (L
"%02x", Rand
[Index2
]);
224 Print (L
"\n -- Exit UEFI RNG Protocol Test (Status = %r).\n", Status
);