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