|
3 | 3 | * Confidential Computing Platform Capability checks
|
4 | 4 | *
|
5 | 5 | * Copyright (C) 2021 Advanced Micro Devices, Inc.
|
| 6 | + * Copyright (C) 2024 Jason A. Donenfeld <[email protected]>. All Rights Reserved. |
6 | 7 | *
|
7 | 8 | * Author: Tom Lendacky <[email protected]>
|
8 | 9 | */
|
9 | 10 |
|
10 | 11 | #include <linux/export.h>
|
11 | 12 | #include <linux/cc_platform.h>
|
| 13 | +#include <linux/string.h> |
| 14 | +#include <linux/random.h> |
12 | 15 |
|
| 16 | +#include <asm/archrandom.h> |
13 | 17 | #include <asm/coco.h>
|
14 | 18 | #include <asm/processor.h>
|
15 | 19 |
|
@@ -148,3 +152,40 @@ u64 cc_mkdec(u64 val)
|
148 | 152 | }
|
149 | 153 | }
|
150 | 154 | EXPORT_SYMBOL_GPL(cc_mkdec);
|
| 155 | + |
| 156 | +__init void cc_random_init(void) |
| 157 | +{ |
| 158 | + /* |
| 159 | + * The seed is 32 bytes (in units of longs), which is 256 bits, which |
| 160 | + * is the security level that the RNG is targeting. |
| 161 | + */ |
| 162 | + unsigned long rng_seed[32 / sizeof(long)]; |
| 163 | + size_t i, longs; |
| 164 | + |
| 165 | + if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) |
| 166 | + return; |
| 167 | + |
| 168 | + /* |
| 169 | + * Since the CoCo threat model includes the host, the only reliable |
| 170 | + * source of entropy that can be neither observed nor manipulated is |
| 171 | + * RDRAND. Usually, RDRAND failure is considered tolerable, but since |
| 172 | + * CoCo guests have no other unobservable source of entropy, it's |
| 173 | + * important to at least ensure the RNG gets some initial random seeds. |
| 174 | + */ |
| 175 | + for (i = 0; i < ARRAY_SIZE(rng_seed); i += longs) { |
| 176 | + longs = arch_get_random_longs(&rng_seed[i], ARRAY_SIZE(rng_seed) - i); |
| 177 | + |
| 178 | + /* |
| 179 | + * A zero return value means that the guest doesn't have RDRAND |
| 180 | + * or the CPU is physically broken, and in both cases that |
| 181 | + * means most crypto inside of the CoCo instance will be |
| 182 | + * broken, defeating the purpose of CoCo in the first place. So |
| 183 | + * just panic here because it's absolutely unsafe to continue |
| 184 | + * executing. |
| 185 | + */ |
| 186 | + if (longs == 0) |
| 187 | + panic("RDRAND is defective."); |
| 188 | + } |
| 189 | + add_device_randomness(rng_seed, sizeof(rng_seed)); |
| 190 | + memzero_explicit(rng_seed, sizeof(rng_seed)); |
| 191 | +} |
0 commit comments