2 * fsprg v0.1 - (seekable) forward-secure pseudorandom generator
3 * Copyright (C) 2012 B. Poettering
4 * Contact: fsprg@point-at-infinity.org
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.
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.
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
23 * See "Practical Secure Logging: Seekable Sequential Key Generators"
24 * by G. A. Marson, B. Poettering for details:
26 * http://eprint.iacr.org/2013/397
33 #include "gcrypt-util.h"
35 #define ISVALID_SECPAR(secpar) (((secpar) % 16 == 0) && ((secpar) >= 16) && ((secpar) <= 16384))
36 #define VALIDATE_SECPAR(secpar) assert(ISVALID_SECPAR(secpar));
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
43 #pragma GCC diagnostic ignored "-Wpointer-arith"
44 /* TODO: remove void* arithmetic and this work-around */
46 /******************************************************************************/
48 static void mpi_export(void *buf
, size_t buflen
, const gcry_mpi_t x
) {
52 assert(gcry_mpi_cmp_ui(x
, 0) >= 0);
53 len
= (gcry_mpi_get_nbits(x
) + 7) / 8;
54 assert(len
<= buflen
);
56 gcry_mpi_print(GCRYMPI_FMT_USG
, buf
+ (buflen
- len
), len
, &nwritten
, x
);
57 assert(nwritten
== len
);
60 static gcry_mpi_t
mpi_import(const void *buf
, size_t buflen
) {
64 assert_se(gcry_mpi_scan(&h
, GCRYMPI_FMT_USG
, buf
, buflen
, NULL
) == 0);
65 len
= (gcry_mpi_get_nbits(h
) + 7) / 8;
66 assert(len
<= buflen
);
67 assert(gcry_mpi_cmp_ui(h
, 0) >= 0);
72 static void uint64_export(void *buf
, size_t buflen
, uint64_t x
) {
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;
84 _pure_
static uint64_t uint64_import(const void *buf
, size_t buflen
) {
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;
97 /* deterministically generate from seed/idx a string of buflen pseudorandom bytes */
98 static void det_randomize(void *buf
, size_t buflen
, const void *seed
, size_t seedlen
, uint32_t idx
) {
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);
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);
118 cpylen
= (buflen
< olen
) ? buflen
: olen
;
119 memcpy(buf
, gcry_md_read(hd2
, RND_HASH
), cpylen
);
127 /* deterministically generate from seed/idx a prime of length `bits' that is 3 (mod 4) */
128 static gcry_mpi_t
genprime3mod4(int bits
, const void *seed
, size_t seedlen
, uint32_t idx
) {
129 size_t buflen
= bits
/ 8;
133 assert(bits
% 8 == 0);
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) */
140 p
= mpi_import(buf
, buflen
);
141 while (gcry_prime_check(p
, 0))
142 gcry_mpi_add_ui(p
, p
, 4);
147 /* deterministically generate from seed/idx a quadratic residue (mod n) */
148 static 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;
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
);
161 /* compute 2^m (mod phi(p)), for a prime p */
162 static gcry_mpi_t
twopowmodphi(uint64_t m
, const gcry_mpi_t p
) {
166 phi
= gcry_mpi_new(0);
167 gcry_mpi_sub_ui(phi
, p
, 1);
169 /* count number of used bits in m */
170 for (n
= 0; (1ULL << n
) <= m
; n
++)
174 gcry_mpi_set_ui(r
, 1);
175 while (n
) { /* square and multiply algorithm for fast exponentiation */
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
);
185 gcry_mpi_release(phi
);
189 /* Decompose $x \in Z_n$ into $(xp,xq) \in Z_p \times Z_q$ using Chinese Remainder Theorem */
190 static 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
);
197 /* Compose $(xp,xq) \in Z_p \times Z_q$ into $x \in Z_n$ using Chinese Remainder Theorem */
198 static 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
) {
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 */
213 /******************************************************************************/
215 size_t FSPRG_mskinbytes(unsigned _secpar
) {
216 VALIDATE_SECPAR(_secpar
);
217 return 2 + 2 * (_secpar
/ 2) / 8; /* to store header,p,q */
220 size_t FSPRG_mpkinbytes(unsigned _secpar
) {
221 VALIDATE_SECPAR(_secpar
);
222 return 2 + _secpar
/ 8; /* to store header,n */
225 size_t FSPRG_stateinbytes(unsigned _secpar
) {
226 VALIDATE_SECPAR(_secpar
);
227 return 2 + 2 * _secpar
/ 8 + 8; /* to store header,n,x,epoch */
230 static 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;
236 static uint16_t read_secpar(const void *buf
) {
239 (uint16_t)(((uint8_t*) buf
)[0]) << 8 |
240 (uint16_t)(((uint8_t*) buf
)[1]) << 0;
241 return 16 * (secpar
+ 1);
244 void FSPRG_GenMK(void *msk
, void *mpk
, const void *seed
, size_t seedlen
, unsigned _secpar
) {
245 uint8_t iseed
[FSPRG_RECOMMENDED_SEEDLEN
];
249 VALIDATE_SECPAR(_secpar
);
252 initialize_libgcrypt(false);
255 gcry_randomize(iseed
, FSPRG_RECOMMENDED_SEEDLEN
, GCRY_STRONG_RANDOM
);
257 seedlen
= FSPRG_RECOMMENDED_SEEDLEN
;
260 p
= genprime3mod4(secpar
/ 2, seed
, seedlen
, RND_GEN_P
);
261 q
= genprime3mod4(secpar
/ 2, seed
, seedlen
, RND_GEN_Q
);
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
);
271 gcry_mpi_mul(n
, p
, q
);
272 assert(gcry_mpi_get_nbits(n
) == secpar
);
274 store_secpar(mpk
+ 0, secpar
);
275 mpi_export(mpk
+ 2, secpar
/ 8, n
);
284 void FSPRG_GenState0(void *state
, const void *mpk
, const void *seed
, size_t seedlen
) {
288 initialize_libgcrypt(false);
290 secpar
= read_secpar(mpk
+ 0);
291 n
= mpi_import(mpk
+ 2, secpar
/ 8);
292 x
= gensquare(n
, seed
, seedlen
, RND_GEN_X
, secpar
);
294 memcpy(state
, mpk
, 2 + secpar
/ 8);
295 mpi_export(state
+ 2 + 1 * secpar
/ 8, secpar
/ 8, x
);
296 memzero(state
+ 2 + 2 * secpar
/ 8, 8);
302 void FSPRG_Evolve(void *state
) {
307 initialize_libgcrypt(false);
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);
314 gcry_mpi_mulm(x
, x
, x
, n
);
317 mpi_export(state
+ 2 + 1 * secpar
/ 8, secpar
/ 8, x
);
318 uint64_export(state
+ 2 + 2 * secpar
/ 8, 8, epoch
);
324 uint64_t FSPRG_GetEpoch(const void *state
) {
326 secpar
= read_secpar(state
+ 0);
327 return uint64_import(state
+ 2 + 2 * secpar
/ 8, 8);
330 void 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
;
334 initialize_libgcrypt(false);
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);
341 gcry_mpi_mul(n
, p
, q
);
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 */
346 kp
= twopowmodphi(epoch
, p
); /* compute 2^epoch (mod phi(p)) */
347 kq
= twopowmodphi(epoch
, q
); /* compute 2^epoch (mod phi(q)) */
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) */
352 CRT_compose(&xm
, xp
, xq
, p
, q
); /* combine (mod p) and (mod q) to (mod n) using CRT */
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
);
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
);
370 void FSPRG_GetKey(const void *state
, void *key
, size_t keylen
, uint32_t idx
) {
373 initialize_libgcrypt(false);
375 secpar
= read_secpar(state
+ 0);
376 det_randomize(key
, keylen
, state
+ 2, 2 * secpar
/ 8 + 8, idx
);