]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-gmp.c
detect failed RNG initialization
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
1 /*
2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010  Rudolf Polzer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #ifdef WIN32
21 #include <windows.h>
22 #include <wincrypt.h>
23 #endif
24
25 #include "d0_bignum.h"
26
27 #include <gmp.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 struct d0_bignum_s
32 {
33         mpz_t z;
34 };
35
36 static gmp_randstate_t RANDSTATE;
37 static d0_bignum_t temp;
38
39 #include <time.h>
40 #include <stdio.h>
41
42 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
43 {
44         FILE *f;
45         BOOL ret = 1;
46         unsigned char buf[256];
47         d0_bignum_init(&temp);
48         gmp_randinit_mt(RANDSTATE);
49         gmp_randseed_ui(RANDSTATE, time(NULL));
50         * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data
51 #ifdef WIN32
52         {
53                 HCRYPTPROV hCryptProv;
54                 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
55                 {
56                         if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
57                         {
58                                 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
59                                 ret = 0;
60                         }
61                         CryptReleaseContext(hCryptProv, 0);
62                 }
63                 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
64                 {
65                         if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
66                         {
67                                 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
68                                 ret = 0;
69                         }
70                         CryptReleaseContext(hCryptProv, 0);
71                 }
72                 else
73                 {
74                         fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
75                         ret = 0;
76                 }
77         }
78 #else
79         f = fopen("/dev/urandom", "rb");
80         if(!f)
81                 f = fopen("/dev/random", "rb");
82         if(f)
83         {
84                 setbuf(f, NULL);
85                 if(fread(buf, sizeof(buf), 1, f) != 1)
86                 {
87                         fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
88                         ret = 0;
89                 }
90                 fclose(f);
91         }
92         else
93         {
94                 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
95                 ret = 0;
96         }
97 #endif
98
99         mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
100         gmp_randseed(RANDSTATE, temp.z);
101
102         return ret;
103 }
104
105 void d0_bignum_SHUTDOWN(void)
106 {
107         d0_bignum_clear(&temp);
108         gmp_randclear(RANDSTATE);
109 }
110
111 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
112 {
113         static unsigned char numbuf[65536];
114         size_t count = 0;
115         numbuf[0] = mpz_sgn(bignum->z) & 3;
116         if((numbuf[0] & 3) != 0) // nonzero
117         {
118                 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
119                 if(count > sizeof(numbuf) - 1)
120                         return 0;
121                 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
122         }
123         return d0_iobuf_write_packet(buf, numbuf, count + 1);
124 }
125
126 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
127 {
128         static unsigned char numbuf[65536];
129         size_t count = sizeof(numbuf);
130         if(!d0_iobuf_read_packet(buf, numbuf, &count))
131                 return NULL;
132         if(count < 1)
133                 return NULL;
134         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
135         if(numbuf[0] & 3) // nonzero
136         {
137                 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
138                 if(numbuf[0] & 2) // negative
139                         mpz_neg(bignum->z, bignum->z);
140         }
141         else // zero
142         {
143                 mpz_set_ui(bignum->z, 0);
144         }
145         return bignum;
146 }
147
148 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
149 {
150         size_t count;
151         count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
152         if(count > bufsize)
153                 return -1;
154         if(bufsize > count)
155         {
156                 // pad from left (big endian numbers!)
157                 memset(buf, 0, bufsize - count);
158                 buf += bufsize - count;
159         }
160         bufsize = count;
161         mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
162         if(bufsize > count)
163         {
164                 // REALLY BAD
165                 // mpz_sizeinbase lied to us
166                 // buffer overflow
167                 // there is no sane way whatsoever to handle this
168                 abort();
169         }
170         if(bufsize < count)
171         {
172                 // BAD
173                 // mpz_sizeinbase lied to us
174                 // move the number
175                 if(bufsize == 0)
176                 {
177                         memset(buf, 0, count);
178                 }
179                 else
180                 {
181                         memmove(buf + count - bufsize, buf, bufsize);
182                         memset(buf, 0, count - bufsize);
183                 }
184         }
185         return bufsize;
186 }
187
188 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
189 {
190         size_t count;
191         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
192         mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
193         return bignum;
194 }
195
196 d0_bignum_t *d0_bignum_new(void)
197 {
198         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
199         mpz_init(b->z);
200         return b;
201 }
202
203 void d0_bignum_free(d0_bignum_t *a)
204 {
205         mpz_clear(a->z);
206         d0_free(a);
207 }
208
209 void d0_bignum_init(d0_bignum_t *b)
210 {
211         mpz_init(b->z);
212 }
213
214 void d0_bignum_clear(d0_bignum_t *a)
215 {
216         mpz_clear(a->z);
217 }
218
219 size_t d0_bignum_size(const d0_bignum_t *r)
220 {
221         return mpz_sizeinbase(r->z, 2);
222 }
223
224 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
225 {
226         return mpz_cmp(a->z, b->z);
227 }
228
229 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
230 {
231         if(!r) r = d0_bignum_new(); if(!r) return NULL;
232         mpz_sub(temp.z, max->z, min->z);
233         mpz_urandomm(r->z, RANDSTATE, temp.z);
234         mpz_add(r->z, r->z, min->z);
235         return r;
236 }
237
238 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
239 {
240         if(!r) r = d0_bignum_new(); if(!r) return NULL;
241         mpz_urandomb(r->z, RANDSTATE, n);
242         return r;
243 }
244
245 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
246 {
247         if(!r) r = d0_bignum_new(); if(!r) return NULL;
248         mpz_urandomb(r->z, RANDSTATE, n-1);
249         mpz_setbit(r->z, n-1);
250         return r;
251 }
252
253 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
254 {
255         return d0_bignum_int(r, 0);
256 }
257
258 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
259 {
260         return d0_bignum_int(r, 1);
261 }
262
263 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
264 {
265         if(!r) r = d0_bignum_new(); if(!r) return NULL;
266         mpz_set_si(r->z, n);
267         return r;
268 }
269
270 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
271 {
272         if(r == a)
273                 return r; // trivial
274         if(!r) r = d0_bignum_new(); if(!r) return NULL;
275         mpz_set(r->z, a->z);
276         return r;
277 }
278
279 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
280 {
281         if(!r) r = d0_bignum_new(); if(!r) return NULL;
282         mpz_neg(r->z, a->z);
283         return r;
284 }
285
286 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
287 {
288         if(!r) r = d0_bignum_new(); if(!r) return NULL;
289         if(n > 0)
290                 mpz_mul_2exp(r->z, a->z, n);
291         else if(n < 0)
292                 mpz_fdiv_q_2exp(r->z, a->z, -n);
293         else
294                 mpz_set(r->z, a->z);
295         return r;
296 }
297
298 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
299 {
300         if(!r) r = d0_bignum_new(); if(!r) return NULL;
301         mpz_add(r->z, a->z, b->z);
302         return r;
303 }
304
305 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
306 {
307         if(!r) r = d0_bignum_new(); if(!r) return NULL;
308         mpz_sub(r->z, a->z, b->z);
309         return r;
310 }
311
312 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
313 {
314         if(!r) r = d0_bignum_new(); if(!r) return NULL;
315         mpz_mul(r->z, a->z, b->z);
316         return r;
317 }
318
319 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
320 {
321         if(!q && !m)
322                 m = d0_bignum_new();
323         if(q)
324                 if(m)
325                         mpz_fdiv_qr(q->z, m->z, a->z, b->z);
326                 else
327                         mpz_fdiv_q(q->z, a->z, b->z);
328         else
329                 mpz_fdiv_r(m->z, a->z, b->z);
330         if(m)
331                 return m;
332         else
333                 return q;
334 }
335
336 d0_bignum_t *d0_bignum_mod_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
337 {
338         r = d0_bignum_add(r, a, b);
339         mpz_fdiv_r(r->z, r->z, m->z);
340         return r;
341 }
342
343 d0_bignum_t *d0_bignum_mod_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
344 {
345         r = d0_bignum_mul(r, a, b);
346         mpz_fdiv_r(r->z, r->z, m->z);
347         return r;
348 }
349
350 d0_bignum_t *d0_bignum_mod_pow(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
351 {
352         if(!r) r = d0_bignum_new(); if(!r) return NULL;
353         mpz_powm(r->z, a->z, b->z, m->z);
354         return r;
355 }
356
357 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
358 {
359         // here, r MUST be set, as otherwise we cannot return error state!
360         return mpz_invert(r->z, a->z, m->z);
361 }
362
363 int d0_bignum_isprime(d0_bignum_t *r, int param)
364 {
365         return mpz_probab_prime_p(r->z, param);
366 }
367
368 d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const d0_bignum_t *a, const d0_bignum_t *b)
369 {
370         if(!r) r = d0_bignum_new(); if(!r) return NULL;
371         if(s)
372                 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
373         else if(t)
374                 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
375         else
376                 mpz_gcd(r->z, a->z, b->z);
377         return r;
378 }
379
380 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
381 {
382         return mpz_get_str(NULL, base, x->z);
383 }