14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 2e4fbf476SArd Biesheuvel /* 3e4fbf476SArd Biesheuvel * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org> 4e4fbf476SArd Biesheuvel */ 5e4fbf476SArd Biesheuvel 6e4fbf476SArd Biesheuvel #include <linux/efi.h> 7e4fbf476SArd Biesheuvel #include <asm/efi.h> 8e4fbf476SArd Biesheuvel 9e4fbf476SArd Biesheuvel #include "efistub.h" 10e4fbf476SArd Biesheuvel 111786e830SArd Biesheuvel typedef union efi_rng_protocol efi_rng_protocol_t; 1241e8a7c2SDominik Brodowski 131786e830SArd Biesheuvel union efi_rng_protocol { 141786e830SArd Biesheuvel struct { 158f24f8c2SArd Biesheuvel efi_status_t (__efiapi *get_info)(efi_rng_protocol_t *, 168f24f8c2SArd Biesheuvel unsigned long *, 178f24f8c2SArd Biesheuvel efi_guid_t *); 188f24f8c2SArd Biesheuvel efi_status_t (__efiapi *get_rng)(efi_rng_protocol_t *, 198f24f8c2SArd Biesheuvel efi_guid_t *, unsigned long, 208f24f8c2SArd Biesheuvel u8 *out); 21e4fbf476SArd Biesheuvel }; 221786e830SArd Biesheuvel struct { 231786e830SArd Biesheuvel u32 get_info; 241786e830SArd Biesheuvel u32 get_rng; 251786e830SArd Biesheuvel } mixed_mode; 261786e830SArd Biesheuvel }; 27e4fbf476SArd Biesheuvel 28ba832f68SHeinrich Schuchardt /** 29ba832f68SHeinrich Schuchardt * efi_get_random_bytes() - fill a buffer with random bytes 30ba832f68SHeinrich Schuchardt * @size: size of the buffer 31ba832f68SHeinrich Schuchardt * @out: caller allocated buffer to receive the random bytes 32ba832f68SHeinrich Schuchardt * 33ba832f68SHeinrich Schuchardt * The call will fail if either the firmware does not implement the 34ba832f68SHeinrich Schuchardt * EFI_RNG_PROTOCOL or there are not enough random bytes available to fill 35ba832f68SHeinrich Schuchardt * the buffer. 36ba832f68SHeinrich Schuchardt * 37ba832f68SHeinrich Schuchardt * Return: status code 38ba832f68SHeinrich Schuchardt */ 39cd33a5c1SArd Biesheuvel efi_status_t efi_get_random_bytes(unsigned long size, u8 *out) 40e4fbf476SArd Biesheuvel { 41e4fbf476SArd Biesheuvel efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID; 42e4fbf476SArd Biesheuvel efi_status_t status; 431786e830SArd Biesheuvel efi_rng_protocol_t *rng = NULL; 44e4fbf476SArd Biesheuvel 45966291f6SArd Biesheuvel status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng); 46e4fbf476SArd Biesheuvel if (status != EFI_SUCCESS) 47e4fbf476SArd Biesheuvel return status; 48e4fbf476SArd Biesheuvel 4947c0fd39SArd Biesheuvel return efi_call_proto(rng, get_rng, NULL, size, out); 50e4fbf476SArd Biesheuvel } 512ddbfc81SArd Biesheuvel 52ba832f68SHeinrich Schuchardt /** 53ba832f68SHeinrich Schuchardt * efi_random_get_seed() - provide random seed as configuration table 54ba832f68SHeinrich Schuchardt * 55ba832f68SHeinrich Schuchardt * The EFI_RNG_PROTOCOL is used to read random bytes. These random bytes are 56ba832f68SHeinrich Schuchardt * saved as a configuration table which can be used as entropy by the kernel 57ba832f68SHeinrich Schuchardt * for the initialization of its pseudo random number generator. 58ba832f68SHeinrich Schuchardt * 59ba832f68SHeinrich Schuchardt * If the EFI_RNG_PROTOCOL is not available or there are not enough random bytes 60ba832f68SHeinrich Schuchardt * available, the configuration table will not be installed and an error code 61ba832f68SHeinrich Schuchardt * will be returned. 62ba832f68SHeinrich Schuchardt * 63ba832f68SHeinrich Schuchardt * Return: status code 64ba832f68SHeinrich Schuchardt */ 65cd33a5c1SArd Biesheuvel efi_status_t efi_random_get_seed(void) 66568bc4e8SArd Biesheuvel { 67568bc4e8SArd Biesheuvel efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID; 68568bc4e8SArd Biesheuvel efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW; 69568bc4e8SArd Biesheuvel efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID; 70*196dff27SArd Biesheuvel struct linux_efi_random_seed *prev_seed, *seed = NULL; 71*196dff27SArd Biesheuvel int prev_seed_size = 0, seed_size = EFI_RANDOM_SEED_SIZE; 721786e830SArd Biesheuvel efi_rng_protocol_t *rng = NULL; 73568bc4e8SArd Biesheuvel efi_status_t status; 74568bc4e8SArd Biesheuvel 75966291f6SArd Biesheuvel status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng); 76568bc4e8SArd Biesheuvel if (status != EFI_SUCCESS) 77568bc4e8SArd Biesheuvel return status; 78568bc4e8SArd Biesheuvel 797d866e38SArd Biesheuvel /* 80*196dff27SArd Biesheuvel * Check whether a seed was provided by a prior boot stage. In that 81*196dff27SArd Biesheuvel * case, instead of overwriting it, let's create a new buffer that can 82*196dff27SArd Biesheuvel * hold both, and concatenate the existing and the new seeds. 83*196dff27SArd Biesheuvel * Note that we should read the seed size with caution, in case the 84*196dff27SArd Biesheuvel * table got corrupted in memory somehow. 85*196dff27SArd Biesheuvel */ 86*196dff27SArd Biesheuvel prev_seed = get_efi_config_table(LINUX_EFI_RANDOM_SEED_TABLE_GUID); 87*196dff27SArd Biesheuvel if (prev_seed && prev_seed->size <= 512U) { 88*196dff27SArd Biesheuvel prev_seed_size = prev_seed->size; 89*196dff27SArd Biesheuvel seed_size += prev_seed_size; 90*196dff27SArd Biesheuvel } 91*196dff27SArd Biesheuvel 92*196dff27SArd Biesheuvel /* 937d866e38SArd Biesheuvel * Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the 947d866e38SArd Biesheuvel * allocation will survive a kexec reboot (although we refresh the seed 957d866e38SArd Biesheuvel * beforehand) 967d866e38SArd Biesheuvel */ 977d866e38SArd Biesheuvel status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY, 98*196dff27SArd Biesheuvel struct_size(seed, bits, seed_size), 99568bc4e8SArd Biesheuvel (void **)&seed); 100*196dff27SArd Biesheuvel if (status != EFI_SUCCESS) { 101*196dff27SArd Biesheuvel efi_warn("Failed to allocate memory for RNG seed.\n"); 102*196dff27SArd Biesheuvel goto err_warn; 103*196dff27SArd Biesheuvel } 104568bc4e8SArd Biesheuvel 10547c0fd39SArd Biesheuvel status = efi_call_proto(rng, get_rng, &rng_algo_raw, 10641e8a7c2SDominik Brodowski EFI_RANDOM_SEED_SIZE, seed->bits); 10741e8a7c2SDominik Brodowski 108568bc4e8SArd Biesheuvel if (status == EFI_UNSUPPORTED) 109568bc4e8SArd Biesheuvel /* 110568bc4e8SArd Biesheuvel * Use whatever algorithm we have available if the raw algorithm 111568bc4e8SArd Biesheuvel * is not implemented. 112568bc4e8SArd Biesheuvel */ 11347c0fd39SArd Biesheuvel status = efi_call_proto(rng, get_rng, NULL, 11441e8a7c2SDominik Brodowski EFI_RANDOM_SEED_SIZE, seed->bits); 115568bc4e8SArd Biesheuvel 116568bc4e8SArd Biesheuvel if (status != EFI_SUCCESS) 117568bc4e8SArd Biesheuvel goto err_freepool; 118568bc4e8SArd Biesheuvel 119*196dff27SArd Biesheuvel seed->size = seed_size; 120*196dff27SArd Biesheuvel if (prev_seed_size) 121*196dff27SArd Biesheuvel memcpy(seed->bits + EFI_RANDOM_SEED_SIZE, prev_seed->bits, 122*196dff27SArd Biesheuvel prev_seed_size); 123*196dff27SArd Biesheuvel 124966291f6SArd Biesheuvel status = efi_bs_call(install_configuration_table, &rng_table_guid, seed); 125568bc4e8SArd Biesheuvel if (status != EFI_SUCCESS) 126568bc4e8SArd Biesheuvel goto err_freepool; 127568bc4e8SArd Biesheuvel 128*196dff27SArd Biesheuvel if (prev_seed_size) { 129*196dff27SArd Biesheuvel /* wipe and free the old seed if we managed to install the new one */ 130*196dff27SArd Biesheuvel memzero_explicit(prev_seed->bits, prev_seed_size); 131*196dff27SArd Biesheuvel efi_bs_call(free_pool, prev_seed); 132*196dff27SArd Biesheuvel } 133568bc4e8SArd Biesheuvel return EFI_SUCCESS; 134568bc4e8SArd Biesheuvel 135568bc4e8SArd Biesheuvel err_freepool: 136*196dff27SArd Biesheuvel memzero_explicit(seed, struct_size(seed, bits, seed_size)); 137966291f6SArd Biesheuvel efi_bs_call(free_pool, seed); 138*196dff27SArd Biesheuvel efi_warn("Failed to obtain seed from EFI_RNG_PROTOCOL\n"); 139*196dff27SArd Biesheuvel err_warn: 140*196dff27SArd Biesheuvel if (prev_seed) 141*196dff27SArd Biesheuvel efi_warn("Retaining bootloader-supplied seed only"); 142568bc4e8SArd Biesheuvel return status; 143568bc4e8SArd Biesheuvel } 144