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