Merge tag 'efi-2020-07-rc5' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / lib / efi_loader / efi_rng.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <efi_loader.h>
9 #include <efi_rng.h>
10 #include <log.h>
11 #include <rng.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
16
17 /**
18  * platform_get_rng_device() - retrieve random number generator
19  *
20  * This function retrieves the udevice implementing a hardware random
21  * number generator.
22  *
23  * This function may be overridden if special initialization is needed.
24  *
25  * @dev:        udevice
26  * Return:      status code
27  */
28 __weak efi_status_t platform_get_rng_device(struct udevice **dev)
29 {
30         int ret;
31         struct udevice *devp;
32
33         ret = uclass_get_device(UCLASS_RNG, 0, &devp);
34         if (ret) {
35                 debug("Unable to get rng device\n");
36                 return EFI_DEVICE_ERROR;
37         }
38
39         *dev = devp;
40
41         return EFI_SUCCESS;
42 }
43
44 /**
45  * rng_getinfo() - get information about random number generation
46  *
47  * This function implement the GetInfo() service of the EFI random number
48  * generator protocol. See the UEFI spec for details.
49  *
50  * @this:                       random number generator protocol instance
51  * @rng_algorithm_list_size:    number of random number generation algorithms
52  * @rng_algorithm_list:         descriptions of random number generation
53  *                              algorithms
54  * Return:                      status code
55  */
56 static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
57                                        efi_uintn_t *rng_algorithm_list_size,
58                                        efi_guid_t *rng_algorithm_list)
59 {
60         efi_status_t ret = EFI_SUCCESS;
61         efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
62
63         EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
64                   rng_algorithm_list);
65
66         if (!this || !rng_algorithm_list_size) {
67                 ret = EFI_INVALID_PARAMETER;
68                 goto back;
69         }
70
71         if (!rng_algorithm_list ||
72             *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
73                 *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
74                 ret = EFI_BUFFER_TOO_SMALL;
75                 goto back;
76         }
77
78         /*
79          * For now, use EFI_RNG_ALGORITHM_RAW as the default
80          * algorithm. If a new algorithm gets added in the
81          * future through a Kconfig, rng_algo_guid will be set
82          * based on that Kconfig option
83          */
84         *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
85         guidcpy(rng_algorithm_list, &rng_algo_guid);
86
87 back:
88         return EFI_EXIT(ret);
89 }
90
91 /**
92  * rng_getrng() - get random value
93  *
94  * This function implement the GetRng() service of the EFI random number
95  * generator protocol. See the UEFI spec for details.
96  *
97  * @this:               random number generator protocol instance
98  * @rng_algorithm:      random number generation algorithm
99  * @rng_value_length:   number of random bytes to generate, buffer length
100  * @rng_value:          buffer to receive random bytes
101  * Return:              status code
102  */
103 static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
104                                   efi_guid_t *rng_algorithm,
105                                   efi_uintn_t rng_value_length,
106                                   uint8_t *rng_value)
107 {
108         int ret;
109         efi_status_t status = EFI_SUCCESS;
110         struct udevice *dev;
111         const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
112
113         EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
114                   rng_value);
115
116         if (!this || !rng_value || !rng_value_length) {
117                 status = EFI_INVALID_PARAMETER;
118                 goto back;
119         }
120
121         if (rng_algorithm) {
122                 EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm);
123                 if (guidcmp(rng_algorithm, &rng_raw_guid)) {
124                         status = EFI_UNSUPPORTED;
125                         goto back;
126                 }
127         }
128
129         ret = platform_get_rng_device(&dev);
130         if (ret != EFI_SUCCESS) {
131                 EFI_PRINT("Rng device not found\n");
132                 status = EFI_UNSUPPORTED;
133                 goto back;
134         }
135
136         ret = dm_rng_read(dev, rng_value, rng_value_length);
137         if (ret < 0) {
138                 EFI_PRINT("Rng device read failed\n");
139                 status = EFI_DEVICE_ERROR;
140                 goto back;
141         }
142
143 back:
144         return EFI_EXIT(status);
145 }
146
147 const struct efi_rng_protocol efi_rng_protocol = {
148         .get_info = rng_getinfo,
149         .get_rng = getrng,
150 };