]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
SecurityPkg/RngDxe: Documentation/include/parameter cleanup
[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 <Protocol/Rng.h>
27
28 #include "RngDxeInternals.h"
29
30 /**
31 Produces and returns an RNG value using either the default or specified RNG algorithm.
32
33 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
34 @param[in] RNGAlgorithm A pointer to the EFI_RNG_ALGORITHM that identifies the RNG
35 algorithm to use. May be NULL in which case the function will
36 use its default RNG algorithm.
37 @param[in] RNGValueLength The length in bytes of the memory buffer pointed to by
38 RNGValue. The driver shall return exactly this numbers of bytes.
39 @param[out] RNGValue A caller-allocated memory buffer filled by the driver with the
40 resulting RNG value.
41
42 @retval EFI_SUCCESS The RNG value was returned successfully.
43 @retval EFI_UNSUPPORTED The algorithm specified by RNGAlgorithm is not supported by
44 this driver.
45 @retval EFI_DEVICE_ERROR An RNG value could not be retrieved due to a hardware or
46 firmware error.
47 @retval EFI_NOT_READY There is not enough random data available to satisfy the length
48 requested by RNGValueLength.
49 @retval EFI_INVALID_PARAMETER RNGValue is NULL or RNGValueLength is zero.
50
51 **/
52 EFI_STATUS
53 EFIAPI
54 RngGetRNG (
55 IN EFI_RNG_PROTOCOL *This,
56 IN EFI_RNG_ALGORITHM *RNGAlgorithm OPTIONAL,
57 IN UINTN RNGValueLength,
58 OUT UINT8 *RNGValue
59 )
60 {
61 EFI_STATUS Status;
62
63 if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {
64 return EFI_INVALID_PARAMETER;
65 }
66
67 if (RNGAlgorithm == NULL) {
68 //
69 // Use the default RNG algorithm if RNGAlgorithm is NULL.
70 //
71 RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
72 }
73
74 if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
75 Status = RngGetBytes (RNGValueLength, RNGValue);
76 return Status;
77 }
78
79 //
80 // Other algorithms are unsupported by this driver.
81 //
82 return EFI_UNSUPPORTED;
83 }
84
85 /**
86 Returns information about the random number generation implementation.
87
88 @param[in] This A pointer to the EFI_RNG_PROTOCOL instance.
89 @param[in,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
90 On output with a return code of EFI_SUCCESS, the size
91 in bytes of the data returned in RNGAlgorithmList. On output
92 with a return code of EFI_BUFFER_TOO_SMALL,
93 the size of RNGAlgorithmList required to obtain the list.
94 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
95 with one EFI_RNG_ALGORITHM element for each supported
96 RNG algorithm. The list must not change across multiple
97 calls to the same driver. The first algorithm in the list
98 is the default algorithm for the driver.
99
100 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
101 @retval EFI_UNSUPPORTED The services is not supported by this driver.
102 @retval EFI_DEVICE_ERROR The list of algorithms could not be retrieved due to a
103 hardware or firmware error.
104 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
105 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
106
107 **/
108 EFI_STATUS
109 EFIAPI
110 RngGetInfo (
111 IN EFI_RNG_PROTOCOL *This,
112 IN OUT UINTN *RNGAlgorithmListSize,
113 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
114 )
115 {
116 UINTN RequiredSize;
117 EFI_RNG_ALGORITHM *CpuRngSupportedAlgorithm;
118
119 RequiredSize = sizeof (EFI_RNG_ALGORITHM);
120
121 if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {
122 return EFI_INVALID_PARAMETER;
123 }
124
125 if (*RNGAlgorithmListSize < RequiredSize) {
126 *RNGAlgorithmListSize = RequiredSize;
127 return EFI_BUFFER_TOO_SMALL;
128 }
129
130 CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
131
132 CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
133
134 *RNGAlgorithmListSize = RequiredSize;
135 return EFI_SUCCESS;
136 }