prng.c revision 1.4 1 /* $NetBSD: prng.c,v 1.4 2021/05/04 21:10:25 khorben Exp $ */
2
3 /*
4 * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Maxime Villard.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "prekern.h"
32 #include <sys/sha1.h>
33 #include <sys/sha2.h>
34
35 #define _KERNEL
36 #include <machine/bootinfo.h>
37 #undef _KERNEL
38
39 #define CPUID_SEF_RDSEED __BIT(18)
40 #define CPUID2_RDRAND 0x40000000
41 static bool has_rdrand = false;
42 static bool has_rdseed = false;
43
44 #define RND_SAVEWORDS 128
45 typedef struct {
46 uint32_t entropy;
47 uint8_t data[RND_SAVEWORDS * sizeof(uint32_t)];
48 uint8_t digest[SHA1_DIGEST_LENGTH];
49 } rndsave_t;
50
51 #define RNGSTATE_SIZE (SHA512_DIGEST_LENGTH / 2)
52 #define RNGDATA_SIZE (SHA512_DIGEST_LENGTH / 2)
53 struct {
54 uint8_t state[RNGSTATE_SIZE];
55 uint8_t data[RNGDATA_SIZE];
56 size_t nused;
57 } rng;
58
59 static struct btinfo_common *
60 prng_lookup_bootinfo(int type)
61 {
62 extern struct bootinfo bootinfo;
63 struct btinfo_common *bic;
64 bool found;
65 int i;
66
67 bic = (struct btinfo_common *)(bootinfo.bi_data);
68 found = false;
69 for (i = 0; i < bootinfo.bi_nentries && !found; i++) {
70 if (bic->type == type)
71 found = true;
72 else
73 bic = (struct btinfo_common *)
74 ((uint8_t *)bic + bic->len);
75 }
76 return found ? bic : NULL;
77 }
78
79 static void
80 prng_get_entropy_file(SHA512_CTX *ctx)
81 {
82 struct bi_modulelist_entry *bi, *bimax;
83 struct btinfo_modulelist *biml;
84 uint8_t digest[SHA1_DIGEST_LENGTH];
85 rndsave_t *rndsave;
86 SHA1_CTX sig;
87
88 biml =
89 (struct btinfo_modulelist *)prng_lookup_bootinfo(BTINFO_MODULELIST);
90 if (biml == NULL) {
91 return;
92 }
93
94 bi = (struct bi_modulelist_entry *)((uint8_t *)biml + sizeof(*biml));
95 bimax = bi + biml->num;
96 for (; bi < bimax; bi++) {
97 if (bi->type != BI_MODULE_RND) {
98 continue;
99 }
100 if (bi->len != sizeof(rndsave_t)) {
101 print_state(STATE_WARNING,
102 "size mismatch in entropy file");
103 continue;
104 }
105 rndsave = (rndsave_t *)(vaddr_t)bi->base;
106
107 /* check the signature */
108 SHA1Init(&sig);
109 SHA1Update(&sig, (uint8_t *)&rndsave->entropy,
110 sizeof(rndsave->entropy));
111 SHA1Update(&sig, rndsave->data, sizeof(rndsave->data));
112 SHA1Final(digest, &sig);
113 if (memcmp(digest, rndsave->digest, sizeof(digest))) {
114 print_state(STATE_WARNING,
115 "bad SHA1 checksum in entropy file");
116 continue;
117 }
118
119 SHA512_Update(ctx, rndsave->data, sizeof(rndsave->data));
120 }
121 }
122
123 /*
124 * Add 32 bytes of rdseed/rdrand and 8 bytes of rdtsc to the context.
125 */
126 static void
127 prng_get_entropy_data(SHA512_CTX *ctx)
128 {
129 uint64_t buf[8], val;
130 size_t i;
131
132 if (has_rdseed) {
133 for (i = 0; i < 8; i++) {
134 if (rdseed(&buf[i]) == -1) {
135 break;
136 }
137 }
138 SHA512_Update(ctx, (uint8_t *)buf, i * sizeof(uint64_t));
139 } else if (has_rdrand) {
140 for (i = 0; i < 8; i++) {
141 if (rdrand(&buf[i]) == -1) {
142 break;
143 }
144 }
145 SHA512_Update(ctx, (uint8_t *)buf, i * sizeof(uint64_t));
146 }
147
148 val = rdtsc();
149 SHA512_Update(ctx, (uint8_t *)&val, sizeof(val));
150 }
151
152 void
153 prng_init(void)
154 {
155 extern int cpuid_level;
156 uint8_t digest[SHA512_DIGEST_LENGTH];
157 SHA512_CTX ctx;
158 u_int descs[4];
159
160 memset(&rng, 0, sizeof(rng));
161
162 /* detect cpu features */
163 if (cpuid_level >= 0x07) {
164 cpuid(0x07, 0x00, descs);
165 has_rdseed = (descs[1] & CPUID_SEF_RDSEED) != 0;
166 }
167 if (cpuid_level >= 0x01) {
168 cpuid(0x01, 0x00, descs);
169 has_rdrand = (descs[2] & CPUID2_RDRAND) != 0;
170 }
171
172 SHA512_Init(&ctx);
173 prng_get_entropy_file(&ctx);
174 prng_get_entropy_data(&ctx);
175 SHA512_Final(digest, &ctx);
176
177 memcpy(rng.state, digest, RNGSTATE_SIZE);
178 memcpy(rng.data, digest + RNGSTATE_SIZE, RNGDATA_SIZE);
179 }
180
181 static void
182 prng_round(void)
183 {
184 uint8_t digest[SHA512_DIGEST_LENGTH];
185 SHA512_CTX ctx;
186
187 SHA512_Init(&ctx);
188 SHA512_Update(&ctx, rng.state, RNGSTATE_SIZE);
189 prng_get_entropy_data(&ctx);
190 SHA512_Final(digest, &ctx);
191
192 memcpy(rng.state, digest, RNGSTATE_SIZE);
193 memcpy(rng.data, digest + RNGSTATE_SIZE, RNGDATA_SIZE);
194
195 rng.nused = 0;
196 }
197
198 void
199 prng_get_rand(void *buf, size_t sz)
200 {
201 uint8_t *ptr = (uint8_t *)buf;
202 size_t consumed;
203
204 ASSERT(sz <= RNGDATA_SIZE);
205 if (rng.nused + sz > RNGDATA_SIZE) {
206 /* Fill what can be */
207 consumed = RNGDATA_SIZE - rng.nused;
208 memcpy(ptr, &rng.data[rng.nused], consumed);
209
210 /* Go through another round */
211 prng_round();
212
213 /* Fill the rest */
214 memcpy(ptr + consumed, &rng.data[rng.nused],
215 sz - consumed);
216
217 rng.nused += (sz - consumed);
218 } else {
219 memcpy(ptr, &rng.data[rng.nused], sz);
220 rng.nused += sz;
221 }
222 }
223