]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/RandomNumberGenerator/RngDxe/AArch64/RngDxe.c
SecurityPkg: Apply uncrustify changes
[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
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/TimerLib.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 ((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,out] RNGAlgorithmListSize On input, the size in bytes of RNGAlgorithmList.
89 On output with a return code of EFI_SUCCESS, the size
90 in bytes of the data returned in RNGAlgorithmList. On output
91 with a return code of EFI_BUFFER_TOO_SMALL,
92 the size of RNGAlgorithmList required to obtain the list.
93 @param[out] RNGAlgorithmList A caller-allocated memory buffer filled by the driver
94 with one EFI_RNG_ALGORITHM element for each supported
95 RNG algorithm. The list must not change across multiple
96 calls to the same driver. The first algorithm in the list
97 is the default algorithm for the driver.
98
99 @retval EFI_SUCCESS The RNG algorithm list was returned successfully.
100 @retval EFI_BUFFER_TOO_SMALL The buffer RNGAlgorithmList is too small to hold the result.
101
102 **/
103 UINTN
104 EFIAPI
105 ArchGetSupportedRngAlgorithms (
106 IN OUT UINTN *RNGAlgorithmListSize,
107 OUT EFI_RNG_ALGORITHM *RNGAlgorithmList
108 )
109 {
110 UINTN RequiredSize;
111 EFI_RNG_ALGORITHM *CpuRngSupportedAlgorithm;
112
113 RequiredSize = sizeof (EFI_RNG_ALGORITHM);
114
115 if (*RNGAlgorithmListSize < RequiredSize) {
116 *RNGAlgorithmListSize = RequiredSize;
117 return EFI_BUFFER_TOO_SMALL;
118 }
119
120 CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);
121
122 CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));
123
124 *RNGAlgorithmListSize = RequiredSize;
125 return EFI_SUCCESS;
126 }