Debian Codegraph @

src/libsystemd/sd-journal/fsprg.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 *
3 * fsprg v0.1 - (seekable) forward-secure pseudorandom generator
4 * Copyright © 2012 B. Poettering
5 * Contact: [email protected]
6 */
7
8 /*
9 * See "Practical Secure Logging: Seekable Sequential Key Generators"
10 * by G. A. Marson, B. Poettering for details:
11 *
12 * http://eprint.iacr.org/2013/397
13 */
14
15 #include <string.h>
16
17 #include "fsprg.h"
18 #include "gcrypt-util.h"
19 #include "memory-util.h"
20
21 #define ISVALID_SECPAR(secpar) (((secpar) % 16 == 0) && ((secpar) >= 16) && ((secpar) <= 16384))
22 #define VALIDATE_SECPAR(secpar) assert(ISVALID_SECPAR(secpar));
23
24 #define RND_HASH GCRY_MD_SHA256
25 #define RND_GEN_P 0x01
26 #define RND_GEN_Q 0x02
27 #define RND_GEN_X 0x03
28
29 #if HAVE_GCRYPT
30
31 #pragma GCC diagnostic ignored "-Wpointer-arith"
32 /* TODO: remove void* arithmetic and this work-around */
33
34 /******************************************************************************/
35
36 static void mpi_export(void *buf, size_t buflen, gcry_mpi_t x) {
37 unsigned len;
38 size_t nwritten;
39
40 assert(sym_gcry_mpi_cmp_ui(x, 0) >= 0);
41 len = (sym_gcry_mpi_get_nbits(x) + 7) / 8;
42 assert(len <= buflen);
43 memzero(buf, buflen);
44 sym_gcry_mpi_print(GCRYMPI_FMT_USG, buf + (buflen - len), len, &nwritten, x);
45 assert(nwritten == len);
46 }
47
48 static gcry_mpi_t mpi_import(const void *buf, size_t buflen) {
49 gcry_mpi_t h;
50 _unused_ unsigned len;
51
52 assert_se(sym_gcry_mpi_scan(&h, GCRYMPI_FMT_USG, buf, buflen, NULL) == 0);
53 len = (sym_gcry_mpi_get_nbits(h) + 7) / 8;
54 assert(len <= buflen);
55 assert(sym_gcry_mpi_cmp_ui(h, 0) >= 0);
56
57 return h;
58 }
59
60 static void uint64_export(void *buf, size_t buflen, uint64_t x) {
61 assert(buflen == 8);
62 ((uint8_t*) buf)[0] = (x >> 56) & 0xff;
63 ((uint8_t*) buf)[1] = (x >> 48) & 0xff;
64 ((uint8_t*) buf)[2] = (x >> 40) & 0xff;
65 ((uint8_t*) buf)[3] = (x >> 32) & 0xff;
66 ((uint8_t*) buf)[4] = (x >> 24) & 0xff;
67 ((uint8_t*) buf)[5] = (x >> 16) & 0xff;
68 ((uint8_t*) buf)[6] = (x >> 8) & 0xff;
69 ((uint8_t*) buf)[7] = (x >> 0) & 0xff;
70 }
71
72 static uint64_t uint64_import(const void *buf, size_t buflen) {
73 assert(buflen == 8);
74 return
75 (uint64_t)(((uint8_t*) buf)[0]) << 56 |
76 (uint64_t)(((uint8_t*) buf)[1]) << 48 |
77 (uint64_t)(((uint8_t*) buf)[2]) << 40 |
78 (uint64_t)(((uint8_t*) buf)[3]) << 32 |
79 (uint64_t)(((uint8_t*) buf)[4]) << 24 |
80 (uint64_t)(((uint8_t*) buf)[5]) << 16 |
81 (uint64_t)(((uint8_t*) buf)[6]) << 8 |
82 (uint64_t)(((uint8_t*) buf)[7]) << 0;
83 }
84
85 /* deterministically generate from seed/idx a string of buflen pseudorandom bytes */
86 static void det_randomize(void *buf, size_t buflen, const void *seed, size_t seedlen, uint32_t idx) {
87 gcry_md_hd_t hd, hd2;
88 size_t olen, cpylen;
89 gcry_error_t err;
90 uint32_t ctr;
91
92 olen = sym_gcry_md_get_algo_dlen(RND_HASH);
93 err = sym_gcry_md_open(&hd, RND_HASH, 0);
94 assert_se(gcry_err_code(err) == GPG_ERR_NO_ERROR); /* This shouldn't happen */
95 sym_gcry_md_write(hd, seed, seedlen);
96 sym_gcry_md_putc(hd, (idx >> 24) & 0xff);
97 sym_gcry_md_putc(hd, (idx >> 16) & 0xff);
98 sym_gcry_md_putc(hd, (idx >> 8) & 0xff);
99 sym_gcry_md_putc(hd, (idx >> 0) & 0xff);
100
101 for (ctr = 0; buflen; ctr++) {
102 err = sym_gcry_md_copy(&hd2, hd);
103 assert_se(gcry_err_code(err) == GPG_ERR_NO_ERROR); /* This shouldn't happen */
104 sym_gcry_md_putc(hd2, (ctr >> 24) & 0xff);
105 sym_gcry_md_putc(hd2, (ctr >> 16) & 0xff);
106 sym_gcry_md_putc(hd2, (ctr >> 8) & 0xff);
107 sym_gcry_md_putc(hd2, (ctr >> 0) & 0xff);
108 sym_gcry_md_ctl(hd2, GCRYCTL_FINALIZE, NULL, 0);
109 cpylen = (buflen < olen) ? buflen : olen;
110 memcpy(buf, sym_gcry_md_read(hd2, RND_HASH), cpylen);
111 sym_gcry_md_close(hd2);
112 buf += cpylen;
113 buflen -= cpylen;
114 }
115 sym_gcry_md_close(hd);
116 }
117
118 /* deterministically generate from seed/idx a prime of length `bits' that is 3 (mod 4) */
119 static gcry_mpi_t genprime3mod4(int bits, const void *seed, size_t seedlen, uint32_t idx) {
120 size_t buflen = bits / 8;
121 uint8_t buf[buflen];
122 gcry_mpi_t p;
123
124 assert(bits % 8 == 0);
125 assert(buflen > 0);
126
127 det_randomize(buf, buflen, seed, seedlen, idx);
128 buf[0] |= 0xc0; /* set upper two bits, so that n=pq has maximum size */
129 buf[buflen - 1] |= 0x03; /* set lower two bits, to have result 3 (mod 4) */
130
131 p = mpi_import(buf, buflen);
132 while (sym_gcry_prime_check(p, 0))
133 sym_gcry_mpi_add_ui(p, p, 4);
134
135 return p;
136 }
137
138 /* deterministically generate from seed/idx a quadratic residue (mod n) */
139 static gcry_mpi_t gensquare(gcry_mpi_t n, const void *seed, size_t seedlen, uint32_t idx, unsigned secpar) {
140 size_t buflen = secpar / 8;
141 uint8_t buf[buflen];
142 gcry_mpi_t x;
143
144 det_randomize(buf, buflen, seed, seedlen, idx);
145 buf[0] &= 0x7f; /* clear upper bit, so that we have x < n */
146 x = mpi_import(buf, buflen);
147 assert(sym_gcry_mpi_cmp(x, n) < 0);
148 sym_gcry_mpi_mulm(x, x, x, n);
149 return x;
150 }
151
152 /* compute 2^m (mod phi(p)), for a prime p */
153 static gcry_mpi_t twopowmodphi(uint64_t m, gcry_mpi_t p) {
154 gcry_mpi_t phi, r;
155 int n;
156
157 phi = sym_gcry_mpi_new(0);
158 sym_gcry_mpi_sub_ui(phi, p, 1);
159
160 /* count number of used bits in m */
161 for (n = 0; (1ULL << n) <= m; n++)
162 ;
163
164 r = sym_gcry_mpi_new(0);
165 sym_gcry_mpi_set_ui(r, 1);
166 while (n) { /* square and multiply algorithm for fast exponentiation */
167 n--;
168 sym_gcry_mpi_mulm(r, r, r, phi);
169 if (m & ((uint64_t)1 << n)) {
170 sym_gcry_mpi_add(r, r, r);
171 if (sym_gcry_mpi_cmp(r, phi) >= 0)
172 sym_gcry_mpi_sub(r, r, phi);
173 }
174 }
175
176 sym_gcry_mpi_release(phi);
177 return r;
178 }
179
180 /* Decompose $x \in Z_n$ into $(xp,xq) \in Z_p \times Z_q$ using Chinese Remainder Theorem */
181 static void CRT_decompose(gcry_mpi_t *xp, gcry_mpi_t *xq, gcry_mpi_t x, gcry_mpi_t p, gcry_mpi_t q) {
182 *xp = sym_gcry_mpi_new(0);
183 *xq = sym_gcry_mpi_new(0);
184 sym_gcry_mpi_mod(*xp, x, p);
185 sym_gcry_mpi_mod(*xq, x, q);
186 }
187
188 /* Compose $(xp,xq) \in Z_p \times Z_q$ into $x \in Z_n$ using Chinese Remainder Theorem */
189 static void CRT_compose(gcry_mpi_t *x, gcry_mpi_t xp, gcry_mpi_t xq, gcry_mpi_t p, gcry_mpi_t q) {
190 gcry_mpi_t a, u;
191
192 a = sym_gcry_mpi_new(0);
193 u = sym_gcry_mpi_new(0);
194 *x = sym_gcry_mpi_new(0);
195 sym_gcry_mpi_subm(a, xq, xp, q);
196 sym_gcry_mpi_invm(u, p, q);
197 sym_gcry_mpi_mulm(a, a, u, q); /* a = (xq - xp) / p (mod q) */
198 sym_gcry_mpi_mul(*x, p, a);
199 sym_gcry_mpi_add(*x, *x, xp); /* x = p * ((xq - xp) / p mod q) + xp */
200 sym_gcry_mpi_release(a);
201 sym_gcry_mpi_release(u);
202 }
203
204 /******************************************************************************/
205
206 #endif
207
208 size_t FSPRG_mskinbytes(unsigned _secpar) {
209 VALIDATE_SECPAR(_secpar);
210 return 2 + 2 * (_secpar / 2) / 8; /* to store header,p,q */
211 }
212
213 size_t FSPRG_mpkinbytes(unsigned _secpar) {
214 VALIDATE_SECPAR(_secpar);
215 return 2 + _secpar / 8; /* to store header,n */
216 }
217
218 size_t FSPRG_stateinbytes(unsigned _secpar) {
219 VALIDATE_SECPAR(_secpar);
220 return 2 + 2 * _secpar / 8 + 8; /* to store header,n,x,epoch */
221 }
222
223 #if HAVE_GCRYPT
224 static void store_secpar(void *buf, uint16_t secpar) {
225 secpar = secpar / 16 - 1;
226 ((uint8_t*) buf)[0] = (secpar >> 8) & 0xff;
227 ((uint8_t*) buf)[1] = (secpar >> 0) & 0xff;
228 }
229
230 static uint16_t read_secpar(const void *buf) {
231 uint16_t secpar;
232 secpar =
233 (uint16_t)(((uint8_t*) buf)[0]) << 8 |
234 (uint16_t)(((uint8_t*) buf)[1]) << 0;
235 return 16 * (secpar + 1);
236 }
237 #endif
238
239 int FSPRG_GenMK(void *msk, void *mpk, const void *seed, size_t seedlen, unsigned _secpar) {
240 #if HAVE_GCRYPT
241 uint8_t iseed[FSPRG_RECOMMENDED_SEEDLEN];
242 gcry_mpi_t n, p, q;
243 uint16_t secpar;
244 int r;
245
246 VALIDATE_SECPAR(_secpar);
247 secpar = _secpar;
248
249 r = initialize_libgcrypt(false);
250 if (r < 0)
251 return r;
252
253 if (!seed) {
254 sym_gcry_randomize(iseed, FSPRG_RECOMMENDED_SEEDLEN, GCRY_STRONG_RANDOM);
255 seed = iseed;
256 seedlen = FSPRG_RECOMMENDED_SEEDLEN;
257 }
258
259 p = genprime3mod4(secpar / 2, seed, seedlen, RND_GEN_P);
260 q = genprime3mod4(secpar / 2, seed, seedlen, RND_GEN_Q);
261
262 if (msk) {
263 store_secpar(msk + 0, secpar);
264 mpi_export(msk + 2 + 0 * (secpar / 2) / 8, (secpar / 2) / 8, p);
265 mpi_export(msk + 2 + 1 * (secpar / 2) / 8, (secpar / 2) / 8, q);
266 }
267
268 if (mpk) {
269 n = sym_gcry_mpi_new(0);
270 sym_gcry_mpi_mul(n, p, q);
271 assert(sym_gcry_mpi_get_nbits(n) == secpar);
272
273 store_secpar(mpk + 0, secpar);
274 mpi_export(mpk + 2, secpar / 8, n);
275
276 sym_gcry_mpi_release(n);
277 }
278
279 sym_gcry_mpi_release(p);
280 sym_gcry_mpi_release(q);
281
282 return 0;
283 #else
284 return -EOPNOTSUPP;
285 #endif
286 }
287
288 int FSPRG_GenState0(void *state, const void *mpk, const void *seed, size_t seedlen) {
289 #if HAVE_GCRYPT
290 gcry_mpi_t n, x;
291 uint16_t secpar;
292 int r;
293
294 r = initialize_libgcrypt(false);
295 if (r < 0)
296 return r;
297
298 secpar = read_secpar(mpk + 0);
299 n = mpi_import(mpk + 2, secpar / 8);
300 x = gensquare(n, seed, seedlen, RND_GEN_X, secpar);
301
302 memcpy(state, mpk, 2 + secpar / 8);
303 mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, x);
304 memzero(state + 2 + 2 * secpar / 8, 8);
305
306 sym_gcry_mpi_release(n);
307 sym_gcry_mpi_release(x);
308
309 return 0;
310 #else
311 return -EOPNOTSUPP;
312 #endif
313 }
314
315 int FSPRG_Evolve(void *state) {
316 #if HAVE_GCRYPT
317 gcry_mpi_t n, x;
318 uint16_t secpar;
319 uint64_t epoch;
320 int r;
321
322 r = initialize_libgcrypt(false);
323 if (r < 0)
324 return r;
325
326 secpar = read_secpar(state + 0);
327 n = mpi_import(state + 2 + 0 * secpar / 8, secpar / 8);
328 x = mpi_import(state + 2 + 1 * secpar / 8, secpar / 8);
329 epoch = uint64_import(state + 2 + 2 * secpar / 8, 8);
330
331 sym_gcry_mpi_mulm(x, x, x, n);
332 epoch++;
333
334 mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, x);
335 uint64_export(state + 2 + 2 * secpar / 8, 8, epoch);
336
337 sym_gcry_mpi_release(n);
338 sym_gcry_mpi_release(x);
339
340 return 0;
341 #else
342 return -EOPNOTSUPP;
343 #endif
344 }
345
346 uint64_t FSPRG_GetEpoch(const void *state) {
347 #if HAVE_GCRYPT
348 uint16_t secpar;
349 secpar = read_secpar(state + 0);
350 return uint64_import(state + 2 + 2 * secpar / 8, 8);
351 #else
352 return -EOPNOTSUPP;
353 #endif
354 }
355
356 int FSPRG_Seek(void *state, uint64_t epoch, const void *msk, const void *seed, size_t seedlen) {
357 #if HAVE_GCRYPT
358 gcry_mpi_t p, q, n, x, xp, xq, kp, kq, xm;
359 uint16_t secpar;
360 int r;
361
362 r = initialize_libgcrypt(false);
363 if (r < 0)
364 return r;
365
366 secpar = read_secpar(msk + 0);
367 p = mpi_import(msk + 2 + 0 * (secpar / 2) / 8, (secpar / 2) / 8);
368 q = mpi_import(msk + 2 + 1 * (secpar / 2) / 8, (secpar / 2) / 8);
369
370 n = sym_gcry_mpi_new(0);
371 sym_gcry_mpi_mul(n, p, q);
372
373 x = gensquare(n, seed, seedlen, RND_GEN_X, secpar);
374 CRT_decompose(&xp, &xq, x, p, q); /* split (mod n) into (mod p) and (mod q) using CRT */
375
376 kp = twopowmodphi(epoch, p); /* compute 2^epoch (mod phi(p)) */
377 kq = twopowmodphi(epoch, q); /* compute 2^epoch (mod phi(q)) */
378
379 sym_gcry_mpi_powm(xp, xp, kp, p); /* compute x^(2^epoch) (mod p) */
380 sym_gcry_mpi_powm(xq, xq, kq, q); /* compute x^(2^epoch) (mod q) */
381
382 CRT_compose(&xm, xp, xq, p, q); /* combine (mod p) and (mod q) to (mod n) using CRT */
383
384 store_secpar(state + 0, secpar);
385 mpi_export(state + 2 + 0 * secpar / 8, secpar / 8, n);
386 mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, xm);
387 uint64_export(state + 2 + 2 * secpar / 8, 8, epoch);
388
389 sym_gcry_mpi_release(p);
390 sym_gcry_mpi_release(q);
391 sym_gcry_mpi_release(n);
392 sym_gcry_mpi_release(x);
393 sym_gcry_mpi_release(xp);
394 sym_gcry_mpi_release(xq);
395 sym_gcry_mpi_release(kp);
396 sym_gcry_mpi_release(kq);
397 sym_gcry_mpi_release(xm);
398
399 return 0;
400 #else
401 return -EOPNOTSUPP;
402 #endif
403 }
404
405 int FSPRG_GetKey(const void *state, void *key, size_t keylen, uint32_t idx) {
406 #if HAVE_GCRYPT
407 uint16_t secpar;
408 int r;
409
410 r = initialize_libgcrypt(false);
411 if (r < 0)
412 return r;
413
414 secpar = read_secpar(state + 0);
415 det_randomize(key, keylen, state + 2, 2 * secpar / 8 + 8, idx);
416
417 return 0;
418 #else
419 return -EOPNOTSUPP;
420 #endif
421 }