TianoCore EDK2 master
Loading...
Searching...
No Matches
RngDxe.c
Go to the documentation of this file.
1
22#include <Library/BaseLib.h>
25#include <Library/RngLib.h>
26#include <Protocol/Rng.h>
27
28#include "RngDxeInternals.h"
29
30//
31// Array containing the validated Rng algorithm.
32// The entry with the lowest index will be the default algorithm.
33//
34UINTN mAvailableAlgoArrayCount;
35EFI_RNG_ALGORITHM *mAvailableAlgoArray;
36
37//
38// The Random Number Generator (RNG) protocol
39//
40EFI_RNG_PROTOCOL mRngRdRand = {
43};
44
57EFIAPI
59 IN EFI_HANDLE ImageHandle,
60 IN EFI_SYSTEM_TABLE *SystemTable
61 )
62{
63 EFI_STATUS Status;
64 EFI_HANDLE Handle;
65
66 //
67 // Get the list of available algorithm.
68 //
69 Status = GetAvailableAlgorithms ();
70 if (EFI_ERROR (Status)) {
71 return Status;
72 }
73
74 if (mAvailableAlgoArrayCount == 0) {
76 }
77
78 //
79 // Install UEFI RNG (Random Number Generator) Protocol
80 //
81 Handle = NULL;
82 Status = gBS->InstallMultipleProtocolInterfaces (
83 &Handle,
84 &gEfiRngProtocolGuid,
85 &mRngRdRand,
86 NULL
87 );
88 if (EFI_ERROR (Status)) {
90 }
91
92 return Status;
93}
94
108EFIAPI
110 IN EFI_HANDLE ImageHandle
111 )
112{
113 //
114 // Free the list of available algorithm.
115 //
117 return EFI_SUCCESS;
118}
119
131EFIAPI
133 IN UINTN Length,
134 OUT UINT8 *RandBuffer
135 )
136{
137 BOOLEAN IsRandom;
138 UINT64 TempRand[2];
139
140 while (Length > 0) {
141 IsRandom = GetRandomNumber128 (TempRand);
142 if (!IsRandom) {
143 return EFI_NOT_READY;
144 }
145
146 if (Length >= sizeof (TempRand)) {
147 WriteUnaligned64 ((UINT64 *)RandBuffer, TempRand[0]);
148 RandBuffer += sizeof (UINT64);
149 WriteUnaligned64 ((UINT64 *)RandBuffer, TempRand[1]);
150 RandBuffer += sizeof (UINT64);
151 Length -= sizeof (TempRand);
152 } else {
153 CopyMem (RandBuffer, TempRand, Length);
154 Length = 0;
155 }
156 }
157
158 return EFI_SUCCESS;
159}
UINT64 UINTN
UINT64 EFIAPI WriteUnaligned64(OUT UINT64 *Buffer, IN UINT64 Value)
Definition: Unaligned.c:236
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
BOOLEAN EFIAPI GetRandomNumber128(OUT UINT64 *Rand)
Definition: RngLibTimer.c:182
#define NULL
Definition: Base.h:319
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define EFI_REQUEST_UNLOAD_IMAGE
Definition: PiMultiPhase.h:48
EFI_STATUS EFIAPI GetAvailableAlgorithms(VOID)
Definition: RngDxe.c:43
VOID EFIAPI FreeAvailableAlgorithms(VOID)
Definition: RngDxe.c:60
EFI_STATUS EFIAPI RngGetInfo(IN EFI_RNG_PROTOCOL *This, IN OUT UINTN *RNGAlgorithmListSize, OUT EFI_RNG_ALGORITHM *RNGAlgorithmList)
Definition: RngDxe.c:160
EFI_STATUS EFIAPI RngGetRNG(IN EFI_RNG_PROTOCOL *This, IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL, IN UINTN RNGValueLength, OUT UINT8 *RNGValue)
Definition: RngDxe.c:92
EFI_STATUS EFIAPI RngDriverUnLoad(IN EFI_HANDLE ImageHandle)
Definition: RngDxe.c:109
EFI_STATUS EFIAPI RngDriverEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: RngDxe.c:58
EFI_STATUS EFIAPI RngGetBytes(IN UINTN Length, OUT UINT8 *RandBuffer)
Definition: RngDxe.c:132
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
Definition: Base.h:213