]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
SecurityPkg/RngDxe: Remove ArchGetSupportedRngAlgorithms()
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / AArch64 / RngDxe.c
1 /** @file
2 RNG Driver to produce the UEFI Random Number Generator protocol.
3
4 The driver will use the RNDR instruction to produce random numbers.
5
6 RNG Algorithms defined in UEFI 2.4:
7 - EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID
8 - EFI_RNG_ALGORITHM_RAW - Unsupported
9 - EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID
10 - EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID
11 - EFI_RNG_ALGORITHM_X9_31_3DES_GUID - Unsupported
12 - EFI_RNG_ALGORITHM_X9_31_AES_GUID - Unsupported
13
14 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
15 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
16 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
17 Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
18
19 SPDX-License-Identifier: BSD-2-Clause-Patent
20
21 **/
22
23 #include <Library/BaseLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/TimerLib.h>
27 #include <Protocol/Rng.h>
28
29 #include "RngDxeInternals.h"
30
31 /**
32 Produces and returns an RNG value using either the default or specified RNG algorithm.
33
34 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
35 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
36 algorithm to use. May be NULL in which case the function will
37 use its default RNG algorithm.
38 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
39 RNGValue. The driver shall return exactly this numbers of bytes.
40 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
41 resulting RNG value.
42
43 @retval EFI_SUCCESS The RNG value was returned successfully.
44 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
45 this driver.
46 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
47 firmware error.
48 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
49 requested by RNGValueLength.
50 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
51
52 **/
53 EFI_STATUS
54 EFIAPI
55 RngGetRNG (
56 IN EFI_RNG_PROTOCOL *This,
57 IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
58 IN UINTN RNGValueLength,
59 OUT UINT8 *RNGValue
60 )
61 {
62 EFI_STATUS Status;
63
64 if ((RNGValueLength == 0) || (RNGValue == NULL)) {
65 return EFI_INVALID_PARAMETER;
66 }
67
68 if (RNGAlgorithm == NULL) {
69 //
70 // Use the default RNG algorithm if RNGAlgorithm is NULL.
71 //
72 RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
73 }
74
75 if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
76 Status = RngGetBytes (RNGValueLength, RNGValue);
77 return Status;
78 }
79
80 //
81 // Other algorithms are unsupported by this driver.
82 //
83 return EFI_UNSUPPORTED;
84 }
85
86 /**
87 Returns information about the random number generation implementation.
88
89 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
90 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
91 On output with a return code of EFI_SUCCESS, the size
92 in bytes of the data returned in RNGAlgorithmList. On output
93 with a return code of EFI_BUFFER_TOO_SMALL,
94 the size of RNGAlgorithmList required to obtain the list.
95 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
96 with one EFI_RNG_ALGORITHM element for each supported
97 RNG algorithm. The list must not change across multiple
98 calls to the same driver. The first algorithm in the list
99 is the default algorithm for the driver.
100
101 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
102 @retval EFI_UNSUPPORTED The services is not supported by this driver.
103 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
104 hardware or firmware error.
105 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
106 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
107
108 **/
109 EFI_STATUS
110 EFIAPI
111 RngGetInfo (
112 IN EFI_RNG_PROTOCOL *This,
113 IN OUT UINTN *RNGAlgorithmListSize,
114 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
115 )
116 {
117 UINTN RequiredSize;
118 EFI_RNG_ALGORITHM *CpuRngSupportedAlgorithm;
119
120 RequiredSize = sizeof (EFI_RNG_ALGORITHM);
121
122 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
123 return EFI_INVALID_PARAMETER;
124 }
125
126 if (*RNGAlgorithmListSize < RequiredSize) {
127 *RNGAlgorithmListSize = RequiredSize;
128 return EFI_BUFFER_TOO_SMALL;
129 }
130
131 CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
132
133 CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
134
135 *RNGAlgorithmListSize = RequiredSize;
136 return EFI_SUCCESS;
137 }