1 // SPDX-License-Identifier: GPL-2.0
11 typedef union efi_rng_protocol efi_rng_protocol_t;
13 union efi_rng_protocol {
15 efi_status_t (__efiapi *get_info)(efi_rng_protocol_t *,
18 efi_status_t (__efiapi *get_rng)(efi_rng_protocol_t *,
19 efi_guid_t *, unsigned long,
29 * efi_get_random_bytes() - fill a buffer with random bytes
30 * @size: size of the buffer
31 * @out: caller allocated buffer to receive the random bytes
33 * The call will fail if either the firmware does not implement the
34 * EFI_RNG_PROTOCOL or there are not enough random bytes available to fill
39 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out)
41 efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
43 efi_rng_protocol_t *rng = NULL;
45 status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
46 if (status != EFI_SUCCESS)
49 return efi_call_proto(rng, get_rng, NULL, size, out);
53 * efi_random_get_seed() - provide random seed as configuration table
55 * The EFI_RNG_PROTOCOL is used to read random bytes. These random bytes are
56 * saved as a configuration table which can be used as entropy by the kernel
57 * for the initialization of its pseudo random number generator.
59 * If the EFI_RNG_PROTOCOL is not available or there are not enough random bytes
60 * available, the configuration table will not be installed and an error code
65 efi_status_t efi_random_get_seed(void)
67 efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
68 efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
69 efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
70 efi_rng_protocol_t *rng = NULL;
71 struct linux_efi_random_seed *seed = NULL;
74 status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
75 if (status != EFI_SUCCESS)
78 status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
79 sizeof(*seed) + EFI_RANDOM_SEED_SIZE,
81 if (status != EFI_SUCCESS)
84 status = efi_call_proto(rng, get_rng, &rng_algo_raw,
85 EFI_RANDOM_SEED_SIZE, seed->bits);
87 if (status == EFI_UNSUPPORTED)
89 * Use whatever algorithm we have available if the raw algorithm
92 status = efi_call_proto(rng, get_rng, NULL,
93 EFI_RANDOM_SEED_SIZE, seed->bits);
95 if (status != EFI_SUCCESS)
98 seed->size = EFI_RANDOM_SEED_SIZE;
99 status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
100 if (status != EFI_SUCCESS)
106 efi_bs_call(free_pool, seed);