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  */
efi_get_random_bytes(unsigned long size,u8 * out)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  */
efi_random_get_seed(void)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;
70196dff27SArd Biesheuvel 	struct linux_efi_random_seed *prev_seed, *seed = NULL;
71196dff27SArd Biesheuvel 	int prev_seed_size = 0, seed_size = EFI_RANDOM_SEED_SIZE;
72*a89474aaSJason A. Donenfeld 	unsigned long nv_seed_size = 0, offset = 0;
731786e830SArd Biesheuvel 	efi_rng_protocol_t *rng = NULL;
74568bc4e8SArd Biesheuvel 	efi_status_t status;
75568bc4e8SArd Biesheuvel 
76966291f6SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
77568bc4e8SArd Biesheuvel 	if (status != EFI_SUCCESS)
78*a89474aaSJason A. Donenfeld 		seed_size = 0;
79*a89474aaSJason A. Donenfeld 
80*a89474aaSJason A. Donenfeld 	// Call GetVariable() with a zero length buffer to obtain the size
81*a89474aaSJason A. Donenfeld 	get_efi_var(L"RandomSeed", &rng_table_guid, NULL, &nv_seed_size, NULL);
82*a89474aaSJason A. Donenfeld 	if (!seed_size && !nv_seed_size)
83568bc4e8SArd Biesheuvel 		return status;
84568bc4e8SArd Biesheuvel 
85*a89474aaSJason A. Donenfeld 	seed_size += nv_seed_size;
86*a89474aaSJason A. Donenfeld 
877d866e38SArd Biesheuvel 	/*
88196dff27SArd Biesheuvel 	 * Check whether a seed was provided by a prior boot stage. In that
89196dff27SArd Biesheuvel 	 * case, instead of overwriting it, let's create a new buffer that can
90196dff27SArd Biesheuvel 	 * hold both, and concatenate the existing and the new seeds.
91196dff27SArd Biesheuvel 	 * Note that we should read the seed size with caution, in case the
92196dff27SArd Biesheuvel 	 * table got corrupted in memory somehow.
93196dff27SArd Biesheuvel 	 */
94*a89474aaSJason A. Donenfeld 	prev_seed = get_efi_config_table(rng_table_guid);
95196dff27SArd Biesheuvel 	if (prev_seed && prev_seed->size <= 512U) {
96196dff27SArd Biesheuvel 		prev_seed_size = prev_seed->size;
97196dff27SArd Biesheuvel 		seed_size += prev_seed_size;
98196dff27SArd Biesheuvel 	}
99196dff27SArd Biesheuvel 
100196dff27SArd Biesheuvel 	/*
1017d866e38SArd Biesheuvel 	 * Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the
1027d866e38SArd Biesheuvel 	 * allocation will survive a kexec reboot (although we refresh the seed
1037d866e38SArd Biesheuvel 	 * beforehand)
1047d866e38SArd Biesheuvel 	 */
1057d866e38SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
106196dff27SArd Biesheuvel 			     struct_size(seed, bits, seed_size),
107568bc4e8SArd Biesheuvel 			     (void **)&seed);
108196dff27SArd Biesheuvel 	if (status != EFI_SUCCESS) {
109196dff27SArd Biesheuvel 		efi_warn("Failed to allocate memory for RNG seed.\n");
110196dff27SArd Biesheuvel 		goto err_warn;
111196dff27SArd Biesheuvel 	}
112568bc4e8SArd Biesheuvel 
113*a89474aaSJason A. Donenfeld 	if (rng) {
11447c0fd39SArd Biesheuvel 		status = efi_call_proto(rng, get_rng, &rng_algo_raw,
11541e8a7c2SDominik Brodowski 					EFI_RANDOM_SEED_SIZE, seed->bits);
11641e8a7c2SDominik Brodowski 
117568bc4e8SArd Biesheuvel 		if (status == EFI_UNSUPPORTED)
118568bc4e8SArd Biesheuvel 			/*
119568bc4e8SArd Biesheuvel 			 * Use whatever algorithm we have available if the raw algorithm
120568bc4e8SArd Biesheuvel 			 * is not implemented.
121568bc4e8SArd Biesheuvel 			 */
12247c0fd39SArd Biesheuvel 			status = efi_call_proto(rng, get_rng, NULL,
12341e8a7c2SDominik Brodowski 						EFI_RANDOM_SEED_SIZE, seed->bits);
124568bc4e8SArd Biesheuvel 
125*a89474aaSJason A. Donenfeld 		if (status == EFI_SUCCESS)
126*a89474aaSJason A. Donenfeld 			offset = EFI_RANDOM_SEED_SIZE;
127*a89474aaSJason A. Donenfeld 	}
128*a89474aaSJason A. Donenfeld 
129*a89474aaSJason A. Donenfeld 	if (nv_seed_size) {
130*a89474aaSJason A. Donenfeld 		status = get_efi_var(L"RandomSeed", &rng_table_guid, NULL,
131*a89474aaSJason A. Donenfeld 				     &nv_seed_size, seed->bits + offset);
132*a89474aaSJason A. Donenfeld 
133*a89474aaSJason A. Donenfeld 		if (status == EFI_SUCCESS)
134*a89474aaSJason A. Donenfeld 			/*
135*a89474aaSJason A. Donenfeld 			 * We delete the seed here, and /hope/ that this causes
136*a89474aaSJason A. Donenfeld 			 * EFI to also zero out its representation on disk.
137*a89474aaSJason A. Donenfeld 			 * This is somewhat idealistic, but overwriting the
138*a89474aaSJason A. Donenfeld 			 * variable with zeros is likely just as fraught too.
139*a89474aaSJason A. Donenfeld 			 * TODO: in the future, maybe we can hash it forward
140*a89474aaSJason A. Donenfeld 			 * instead, and write a new seed.
141*a89474aaSJason A. Donenfeld 			 */
142*a89474aaSJason A. Donenfeld 			status = set_efi_var(L"RandomSeed", &rng_table_guid, 0,
143*a89474aaSJason A. Donenfeld 					     0, NULL);
144*a89474aaSJason A. Donenfeld 
145*a89474aaSJason A. Donenfeld 		if (status == EFI_SUCCESS)
146*a89474aaSJason A. Donenfeld 			offset += nv_seed_size;
147*a89474aaSJason A. Donenfeld 		else
148*a89474aaSJason A. Donenfeld 			memzero_explicit(seed->bits + offset, nv_seed_size);
149*a89474aaSJason A. Donenfeld 	}
150*a89474aaSJason A. Donenfeld 
151*a89474aaSJason A. Donenfeld 	if (!offset)
152568bc4e8SArd Biesheuvel 		goto err_freepool;
153568bc4e8SArd Biesheuvel 
154*a89474aaSJason A. Donenfeld 	if (prev_seed_size) {
155*a89474aaSJason A. Donenfeld 		memcpy(seed->bits + offset, prev_seed->bits, prev_seed_size);
156*a89474aaSJason A. Donenfeld 		offset += prev_seed_size;
157*a89474aaSJason A. Donenfeld 	}
158196dff27SArd Biesheuvel 
159*a89474aaSJason A. Donenfeld 	seed->size = offset;
160966291f6SArd Biesheuvel 	status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
161568bc4e8SArd Biesheuvel 	if (status != EFI_SUCCESS)
162568bc4e8SArd Biesheuvel 		goto err_freepool;
163568bc4e8SArd Biesheuvel 
164196dff27SArd Biesheuvel 	if (prev_seed_size) {
165196dff27SArd Biesheuvel 		/* wipe and free the old seed if we managed to install the new one */
166196dff27SArd Biesheuvel 		memzero_explicit(prev_seed->bits, prev_seed_size);
167196dff27SArd Biesheuvel 		efi_bs_call(free_pool, prev_seed);
168196dff27SArd Biesheuvel 	}
169568bc4e8SArd Biesheuvel 	return EFI_SUCCESS;
170568bc4e8SArd Biesheuvel 
171568bc4e8SArd Biesheuvel err_freepool:
172196dff27SArd Biesheuvel 	memzero_explicit(seed, struct_size(seed, bits, seed_size));
173966291f6SArd Biesheuvel 	efi_bs_call(free_pool, seed);
174*a89474aaSJason A. Donenfeld 	efi_warn("Failed to obtain seed from EFI_RNG_PROTOCOL or EFI variable\n");
175196dff27SArd Biesheuvel err_warn:
176196dff27SArd Biesheuvel 	if (prev_seed)
177196dff27SArd Biesheuvel 		efi_warn("Retaining bootloader-supplied seed only");
178568bc4e8SArd Biesheuvel 	return status;
179568bc4e8SArd Biesheuvel }
180