]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/fsprg.c
Merge pull request #5164 from Werkov/ordering-for-_netdev-devices
[thirdparty/systemd.git] / src / journal / fsprg.c
CommitLineData
7560fffc
LP
1/*
2 * fsprg v0.1 - (seekable) forward-secure pseudorandom generator
3 * Copyright (C) 2012 B. Poettering
4 * Contact: fsprg@point-at-infinity.org
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA
fe004b7c
LP
20 */
21
22/*
23 * See "Practical Secure Logging: Seekable Sequential Key Generators"
24 * by G. A. Marson, B. Poettering for details:
7560fffc 25 *
fe004b7c 26 * http://eprint.iacr.org/2013/397
7560fffc
LP
27 */
28
29#include <gcrypt.h>
30#include <string.h>
7560fffc
LP
31
32#include "fsprg.h"
91e023d8 33#include "gcrypt-util.h"
7560fffc
LP
34
35#define ISVALID_SECPAR(secpar) (((secpar) % 16 == 0) && ((secpar) >= 16) && ((secpar) <= 16384))
36#define VALIDATE_SECPAR(secpar) assert(ISVALID_SECPAR(secpar));
37
38#define RND_HASH GCRY_MD_SHA256
39#define RND_GEN_P 0x01
40#define RND_GEN_Q 0x02
41#define RND_GEN_X 0x03
42
521e7c3a
ZJS
43#pragma GCC diagnostic ignored "-Wpointer-arith"
44/* TODO: remove void* arithmetic and this work-around */
45
7560fffc
LP
46/******************************************************************************/
47
48static void mpi_export(void *buf, size_t buflen, const gcry_mpi_t x) {
49 unsigned len;
50 size_t nwritten;
51
52 assert(gcry_mpi_cmp_ui(x, 0) >= 0);
53 len = (gcry_mpi_get_nbits(x) + 7) / 8;
54 assert(len <= buflen);
29804cc1 55 memzero(buf, buflen);
7560fffc
LP
56 gcry_mpi_print(GCRYMPI_FMT_USG, buf + (buflen - len), len, &nwritten, x);
57 assert(nwritten == len);
58}
59
60static gcry_mpi_t mpi_import(const void *buf, size_t buflen) {
61 gcry_mpi_t h;
62 unsigned len;
63
ddea4462 64 assert_se(gcry_mpi_scan(&h, GCRYMPI_FMT_USG, buf, buflen, NULL) == 0);
7560fffc
LP
65 len = (gcry_mpi_get_nbits(h) + 7) / 8;
66 assert(len <= buflen);
67 assert(gcry_mpi_cmp_ui(h, 0) >= 0);
68
69 return h;
70}
71
72static void uint64_export(void *buf, size_t buflen, uint64_t x) {
73 assert(buflen == 8);
74 ((uint8_t*) buf)[0] = (x >> 56) & 0xff;
75 ((uint8_t*) buf)[1] = (x >> 48) & 0xff;
76 ((uint8_t*) buf)[2] = (x >> 40) & 0xff;
77 ((uint8_t*) buf)[3] = (x >> 32) & 0xff;
78 ((uint8_t*) buf)[4] = (x >> 24) & 0xff;
79 ((uint8_t*) buf)[5] = (x >> 16) & 0xff;
80 ((uint8_t*) buf)[6] = (x >> 8) & 0xff;
81 ((uint8_t*) buf)[7] = (x >> 0) & 0xff;
82}
83
44a6b1b6 84_pure_ static uint64_t uint64_import(const void *buf, size_t buflen) {
7560fffc
LP
85 assert(buflen == 8);
86 return
87 (uint64_t)(((uint8_t*) buf)[0]) << 56 |
88 (uint64_t)(((uint8_t*) buf)[1]) << 48 |
89 (uint64_t)(((uint8_t*) buf)[2]) << 40 |
90 (uint64_t)(((uint8_t*) buf)[3]) << 32 |
91 (uint64_t)(((uint8_t*) buf)[4]) << 24 |
92 (uint64_t)(((uint8_t*) buf)[5]) << 16 |
93 (uint64_t)(((uint8_t*) buf)[6]) << 8 |
94 (uint64_t)(((uint8_t*) buf)[7]) << 0;
95}
96
97/* deterministically generate from seed/idx a string of buflen pseudorandom bytes */
98static void det_randomize(void *buf, size_t buflen, const void *seed, size_t seedlen, uint32_t idx) {
99 gcry_md_hd_t hd, hd2;
100 size_t olen, cpylen;
101 uint32_t ctr;
102
103 olen = gcry_md_get_algo_dlen(RND_HASH);
104 gcry_md_open(&hd, RND_HASH, 0);
105 gcry_md_write(hd, seed, seedlen);
106 gcry_md_putc(hd, (idx >> 24) & 0xff);
107 gcry_md_putc(hd, (idx >> 16) & 0xff);
108 gcry_md_putc(hd, (idx >> 8) & 0xff);
109 gcry_md_putc(hd, (idx >> 0) & 0xff);
110
111 for (ctr = 0; buflen; ctr++) {
112 gcry_md_copy(&hd2, hd);
113 gcry_md_putc(hd2, (ctr >> 24) & 0xff);
114 gcry_md_putc(hd2, (ctr >> 16) & 0xff);
115 gcry_md_putc(hd2, (ctr >> 8) & 0xff);
116 gcry_md_putc(hd2, (ctr >> 0) & 0xff);
117 gcry_md_final(hd2);
118 cpylen = (buflen < olen) ? buflen : olen;
119 memcpy(buf, gcry_md_read(hd2, RND_HASH), cpylen);
120 gcry_md_close(hd2);
121 buf += cpylen;
122 buflen -= cpylen;
123 }
124 gcry_md_close(hd);
125}
126
127/* deterministically generate from seed/idx a prime of length `bits' that is 3 (mod 4) */
128static gcry_mpi_t genprime3mod4(int bits, const void *seed, size_t seedlen, uint32_t idx) {
129 size_t buflen = bits / 8;
130 uint8_t buf[buflen];
131 gcry_mpi_t p;
132
133 assert(bits % 8 == 0);
134 assert(buflen > 0);
135
136 det_randomize(buf, buflen, seed, seedlen, idx);
137 buf[0] |= 0xc0; /* set upper two bits, so that n=pq has maximum size */
138 buf[buflen - 1] |= 0x03; /* set lower two bits, to have result 3 (mod 4) */
139
140 p = mpi_import(buf, buflen);
141 while (gcry_prime_check(p, 0))
142 gcry_mpi_add_ui(p, p, 4);
143
144 return p;
145}
146
147/* deterministically generate from seed/idx a quadratic residue (mod n) */
148static gcry_mpi_t gensquare(const gcry_mpi_t n, const void *seed, size_t seedlen, uint32_t idx, unsigned secpar) {
149 size_t buflen = secpar / 8;
150 uint8_t buf[buflen];
151 gcry_mpi_t x;
152
153 det_randomize(buf, buflen, seed, seedlen, idx);
154 buf[0] &= 0x7f; /* clear upper bit, so that we have x < n */
155 x = mpi_import(buf, buflen);
156 assert(gcry_mpi_cmp(x, n) < 0);
157 gcry_mpi_mulm(x, x, x, n);
158 return x;
159}
160
161/* compute 2^m (mod phi(p)), for a prime p */
162static gcry_mpi_t twopowmodphi(uint64_t m, const gcry_mpi_t p) {
163 gcry_mpi_t phi, r;
164 int n;
165
166 phi = gcry_mpi_new(0);
167 gcry_mpi_sub_ui(phi, p, 1);
168
169 /* count number of used bits in m */
fc89a139 170 for (n = 0; (1ULL << n) <= m; n++)
7560fffc
LP
171 ;
172
173 r = gcry_mpi_new(0);
174 gcry_mpi_set_ui(r, 1);
175 while (n) { /* square and multiply algorithm for fast exponentiation */
176 n--;
177 gcry_mpi_mulm(r, r, r, phi);
178 if (m & ((uint64_t)1 << n)) {
179 gcry_mpi_add(r, r, r);
180 if (gcry_mpi_cmp(r, phi) >= 0)
181 gcry_mpi_sub(r, r, phi);
182 }
183 }
184
185 gcry_mpi_release(phi);
186 return r;
187}
188
189/* Decompose $x \in Z_n$ into $(xp,xq) \in Z_p \times Z_q$ using Chinese Remainder Theorem */
190static void CRT_decompose(gcry_mpi_t *xp, gcry_mpi_t *xq, const gcry_mpi_t x, const gcry_mpi_t p, const gcry_mpi_t q) {
191 *xp = gcry_mpi_new(0);
192 *xq = gcry_mpi_new(0);
193 gcry_mpi_mod(*xp, x, p);
194 gcry_mpi_mod(*xq, x, q);
195}
196
197/* Compose $(xp,xq) \in Z_p \times Z_q$ into $x \in Z_n$ using Chinese Remainder Theorem */
198static void CRT_compose(gcry_mpi_t *x, const gcry_mpi_t xp, const gcry_mpi_t xq, const gcry_mpi_t p, const gcry_mpi_t q) {
199 gcry_mpi_t a, u;
200
201 a = gcry_mpi_new(0);
202 u = gcry_mpi_new(0);
203 *x = gcry_mpi_new(0);
204 gcry_mpi_subm(a, xq, xp, q);
205 gcry_mpi_invm(u, p, q);
206 gcry_mpi_mulm(a, a, u, q); /* a = (xq - xp) / p (mod q) */
207 gcry_mpi_mul(*x, p, a);
208 gcry_mpi_add(*x, *x, xp); /* x = p * ((xq - xp) / p mod q) + xp */
209 gcry_mpi_release(a);
210 gcry_mpi_release(u);
211}
212
7560fffc
LP
213/******************************************************************************/
214
215size_t FSPRG_mskinbytes(unsigned _secpar) {
216 VALIDATE_SECPAR(_secpar);
217 return 2 + 2 * (_secpar / 2) / 8; /* to store header,p,q */
218}
219
220size_t FSPRG_mpkinbytes(unsigned _secpar) {
221 VALIDATE_SECPAR(_secpar);
222 return 2 + _secpar / 8; /* to store header,n */
223}
224
225size_t FSPRG_stateinbytes(unsigned _secpar) {
226 VALIDATE_SECPAR(_secpar);
227 return 2 + 2 * _secpar / 8 + 8; /* to store header,n,x,epoch */
228}
229
230static void store_secpar(void *buf, uint16_t secpar) {
231 secpar = secpar / 16 - 1;
232 ((uint8_t*) buf)[0] = (secpar >> 8) & 0xff;
233 ((uint8_t*) buf)[1] = (secpar >> 0) & 0xff;
234}
235
236static uint16_t read_secpar(const void *buf) {
237 uint16_t secpar;
238 secpar =
239 (uint16_t)(((uint8_t*) buf)[0]) << 8 |
240 (uint16_t)(((uint8_t*) buf)[1]) << 0;
241 return 16 * (secpar + 1);
242}
243
244void FSPRG_GenMK(void *msk, void *mpk, const void *seed, size_t seedlen, unsigned _secpar) {
245 uint8_t iseed[FSPRG_RECOMMENDED_SEEDLEN];
246 gcry_mpi_t n, p, q;
247 uint16_t secpar;
248
249 VALIDATE_SECPAR(_secpar);
250 secpar = _secpar;
251
91e023d8 252 initialize_libgcrypt(false);
7560fffc
LP
253
254 if (!seed) {
255 gcry_randomize(iseed, FSPRG_RECOMMENDED_SEEDLEN, GCRY_STRONG_RANDOM);
256 seed = iseed;
257 seedlen = FSPRG_RECOMMENDED_SEEDLEN;
258 }
259
260 p = genprime3mod4(secpar / 2, seed, seedlen, RND_GEN_P);
261 q = genprime3mod4(secpar / 2, seed, seedlen, RND_GEN_Q);
262
263 if (msk) {
264 store_secpar(msk + 0, secpar);
265 mpi_export(msk + 2 + 0 * (secpar / 2) / 8, (secpar / 2) / 8, p);
266 mpi_export(msk + 2 + 1 * (secpar / 2) / 8, (secpar / 2) / 8, q);
267 }
268
269 if (mpk) {
270 n = gcry_mpi_new(0);
271 gcry_mpi_mul(n, p, q);
272 assert(gcry_mpi_get_nbits(n) == secpar);
273
274 store_secpar(mpk + 0, secpar);
275 mpi_export(mpk + 2, secpar / 8, n);
276
277 gcry_mpi_release(n);
278 }
279
280 gcry_mpi_release(p);
281 gcry_mpi_release(q);
282}
283
284void FSPRG_GenState0(void *state, const void *mpk, const void *seed, size_t seedlen) {
285 gcry_mpi_t n, x;
286 uint16_t secpar;
287
91e023d8 288 initialize_libgcrypt(false);
7560fffc
LP
289
290 secpar = read_secpar(mpk + 0);
291 n = mpi_import(mpk + 2, secpar / 8);
292 x = gensquare(n, seed, seedlen, RND_GEN_X, secpar);
293
294 memcpy(state, mpk, 2 + secpar / 8);
295 mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, x);
29804cc1 296 memzero(state + 2 + 2 * secpar / 8, 8);
7560fffc
LP
297
298 gcry_mpi_release(n);
299 gcry_mpi_release(x);
300}
301
302void FSPRG_Evolve(void *state) {
303 gcry_mpi_t n, x;
304 uint16_t secpar;
305 uint64_t epoch;
306
91e023d8 307 initialize_libgcrypt(false);
7560fffc
LP
308
309 secpar = read_secpar(state + 0);
310 n = mpi_import(state + 2 + 0 * secpar / 8, secpar / 8);
311 x = mpi_import(state + 2 + 1 * secpar / 8, secpar / 8);
312 epoch = uint64_import(state + 2 + 2 * secpar / 8, 8);
313
314 gcry_mpi_mulm(x, x, x, n);
315 epoch++;
316
317 mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, x);
318 uint64_export(state + 2 + 2 * secpar / 8, 8, epoch);
319
320 gcry_mpi_release(n);
321 gcry_mpi_release(x);
322}
323
324uint64_t FSPRG_GetEpoch(const void *state) {
325 uint16_t secpar;
326 secpar = read_secpar(state + 0);
327 return uint64_import(state + 2 + 2 * secpar / 8, 8);
328}
329
330void FSPRG_Seek(void *state, uint64_t epoch, const void *msk, const void *seed, size_t seedlen) {
331 gcry_mpi_t p, q, n, x, xp, xq, kp, kq, xm;
332 uint16_t secpar;
333
91e023d8 334 initialize_libgcrypt(false);
7560fffc
LP
335
336 secpar = read_secpar(msk + 0);
337 p = mpi_import(msk + 2 + 0 * (secpar / 2) / 8, (secpar / 2) / 8);
338 q = mpi_import(msk + 2 + 1 * (secpar / 2) / 8, (secpar / 2) / 8);
339
340 n = gcry_mpi_new(0);
341 gcry_mpi_mul(n, p, q);
342
343 x = gensquare(n, seed, seedlen, RND_GEN_X, secpar);
344 CRT_decompose(&xp, &xq, x, p, q); /* split (mod n) into (mod p) and (mod q) using CRT */
345
346 kp = twopowmodphi(epoch, p); /* compute 2^epoch (mod phi(p)) */
347 kq = twopowmodphi(epoch, q); /* compute 2^epoch (mod phi(q)) */
348
349 gcry_mpi_powm(xp, xp, kp, p); /* compute x^(2^epoch) (mod p) */
350 gcry_mpi_powm(xq, xq, kq, q); /* compute x^(2^epoch) (mod q) */
351
352 CRT_compose(&xm, xp, xq, p, q); /* combine (mod p) and (mod q) to (mod n) using CRT */
353
354 store_secpar(state + 0, secpar);
355 mpi_export(state + 2 + 0 * secpar / 8, secpar / 8, n);
356 mpi_export(state + 2 + 1 * secpar / 8, secpar / 8, xm);
357 uint64_export(state + 2 + 2 * secpar / 8, 8, epoch);
358
359 gcry_mpi_release(p);
360 gcry_mpi_release(q);
361 gcry_mpi_release(n);
362 gcry_mpi_release(x);
363 gcry_mpi_release(xp);
364 gcry_mpi_release(xq);
365 gcry_mpi_release(kp);
366 gcry_mpi_release(kq);
367 gcry_mpi_release(xm);
368}
369
370void FSPRG_GetKey(const void *state, void *key, size_t keylen, uint32_t idx) {
371 uint16_t secpar;
372
91e023d8 373 initialize_libgcrypt(false);
7560fffc
LP
374
375 secpar = read_secpar(state + 0);
376 det_randomize(key, keylen, state + 2, 2 * secpar / 8 + 8, idx);
377}