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