]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/RandomNumberGenerator/RngDxe/RngDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / RngDxe.c
CommitLineData
b3548d32 1/** @file\r
3aa8dc6c
LQ
2 RNG Driver to produce the UEFI Random Number Generator protocol.\r
3\r
4e5ecdba
RC
4 The driver uses CPU RNG instructions to produce high-quality,\r
5 high-performance entropy and random number.\r
3aa8dc6c 6\r
fc70522f 7 RNG Algorithms defined in UEFI 2.4:\r
4e5ecdba
RC
8 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID\r
9 - EFI_RNG_ALGORITHM_RAW\r
10 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID\r
11 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID\r
12 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID\r
13 - EFI_RNG_ALGORITHM_X9_31_AES_GUID\r
3aa8dc6c 14\r
b3548d32 15Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
3b60842c 16(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
4e5ecdba 17\r
289b714b 18SPDX-License-Identifier: BSD-2-Clause-Patent\r
3aa8dc6c
LQ
19\r
20**/\r
21\r
4e5ecdba
RC
22#include <Library/BaseLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/UefiBootServicesTableLib.h>\r
25#include <Library/RngLib.h>\r
4e5ecdba 26#include <Protocol/Rng.h>\r
3aa8dc6c 27\r
4e5ecdba 28#include "RngDxeInternals.h"\r
3aa8dc6c 29\r
4b3e9d80
PG
30//\r
31// Array containing the validated Rng algorithm.\r
32// The entry with the lowest index will be the default algorithm.\r
33//\r
34UINTN mAvailableAlgoArrayCount;\r
35EFI_RNG_ALGORITHM *mAvailableAlgoArray;\r
36\r
3aa8dc6c
LQ
37//\r
38// The Random Number Generator (RNG) protocol\r
39//\r
c411b485 40EFI_RNG_PROTOCOL mRngRdRand = {\r
3aa8dc6c
LQ
41 RngGetInfo,\r
42 RngGetRNG\r
43};\r
44\r
45/**\r
46 The user Entry Point for the Random Number Generator (RNG) driver.\r
47\r
48 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
49 @param[in] SystemTable A pointer to the EFI System Table.\r
50\r
51 @retval EFI_SUCCESS The entry point is executed successfully.\r
52 @retval EFI_NOT_SUPPORTED Platform does not support RNG.\r
53 @retval Other Some error occurs when executing this entry point.\r
54\r
55**/\r
56EFI_STATUS\r
57EFIAPI\r
58RngDriverEntry (\r
c411b485
MK
59 IN EFI_HANDLE ImageHandle,\r
60 IN EFI_SYSTEM_TABLE *SystemTable\r
3aa8dc6c
LQ
61 )\r
62{\r
c411b485
MK
63 EFI_STATUS Status;\r
64 EFI_HANDLE Handle;\r
3aa8dc6c 65\r
3aa8dc6c
LQ
66 //\r
67 // Install UEFI RNG (Random Number Generator) Protocol\r
68 //\r
69 Handle = NULL;\r
70 Status = gBS->InstallMultipleProtocolInterfaces (\r
71 &Handle,\r
72 &gEfiRngProtocolGuid,\r
73 &mRngRdRand,\r
74 NULL\r
75 );\r
4b3e9d80
PG
76 if (EFI_ERROR (Status)) {\r
77 return Status;\r
78 }\r
79\r
80 //\r
81 // Get the list of available algorithm.\r
82 //\r
83 return GetAvailableAlgorithms ();\r
84}\r
85\r
86/**\r
87 This is the unload handle for RndgDxe module.\r
88\r
89 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
90 Uninstall all the protocols installed in the driver entry point.\r
3b60842c 91\r
4b3e9d80
PG
92 @param[in] ImageHandle The drivers' driver image.\r
93\r
94 @retval EFI_SUCCESS The image is unloaded.\r
95 @retval Others Failed to unload the image.\r
96\r
97**/\r
98EFI_STATUS\r
99EFIAPI\r
100RngDriverUnLoad (\r
101 IN EFI_HANDLE ImageHandle\r
102 )\r
103{\r
104 //\r
105 // Free the list of available algorithm.\r
106 //\r
107 FreeAvailableAlgorithms ();\r
108 return EFI_SUCCESS;\r
3aa8dc6c 109}\r
4e5ecdba 110\r
4e5ecdba 111/**\r
199031b2 112 Runs CPU RNG instruction to fill a buffer of arbitrary size with random bytes.\r
4e5ecdba
RC
113\r
114 @param[in] Length Size of the buffer, in bytes, to fill with.\r
115 @param[out] RandBuffer Pointer to the buffer to store the random result.\r
116\r
117 @retval EFI_SUCCESS Random bytes generation succeeded.\r
118 @retval EFI_NOT_READY Failed to request random bytes.\r
119\r
120**/\r
121EFI_STATUS\r
122EFIAPI\r
123RngGetBytes (\r
c411b485
MK
124 IN UINTN Length,\r
125 OUT UINT8 *RandBuffer\r
4e5ecdba
RC
126 )\r
127{\r
c411b485
MK
128 BOOLEAN IsRandom;\r
129 UINT64 TempRand[2];\r
4e5ecdba
RC
130\r
131 while (Length > 0) {\r
132 IsRandom = GetRandomNumber128 (TempRand);\r
133 if (!IsRandom) {\r
134 return EFI_NOT_READY;\r
135 }\r
c411b485 136\r
4e5ecdba 137 if (Length >= sizeof (TempRand)) {\r
c411b485 138 WriteUnaligned64 ((UINT64 *)RandBuffer, TempRand[0]);\r
4e5ecdba 139 RandBuffer += sizeof (UINT64);\r
c411b485 140 WriteUnaligned64 ((UINT64 *)RandBuffer, TempRand[1]);\r
4e5ecdba 141 RandBuffer += sizeof (UINT64);\r
c411b485 142 Length -= sizeof (TempRand);\r
4e5ecdba
RC
143 } else {\r
144 CopyMem (RandBuffer, TempRand, Length);\r
145 Length = 0;\r
146 }\r
147 }\r
148\r
149 return EFI_SUCCESS;\r
150}\r