]> git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-gmp.c
un-debianize libd0_blind_id's RNG on Win32
[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 void d0_bignum_INITIALIZE(void)
43 {
44         FILE *f;
45         unsigned char buf[256];
46         d0_bignum_init(&temp);
47         gmp_randinit_mt(RANDSTATE);
48         gmp_randseed_ui(RANDSTATE, time(NULL));
49         * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data
50 #ifdef WIN32
51         {
52                 HCRYPTPROV hCryptProv;
53                 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
54                 {
55                         if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
56                                 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
57                 }
58                 else
59                         fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
60         }
61 #else
62         f = fopen("/dev/urandom", "rb");
63         if(!f)
64                 f = fopen("/dev/random", "rb");
65         if(f)
66         {
67                 setbuf(f, NULL);
68                 if(fread(buf, sizeof(buf), 1, f) != 1)
69                         fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
70                 fclose(f);
71         }
72         else
73                 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
74 #endif
75
76         mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
77         gmp_randseed(RANDSTATE, temp.z);
78 }
79
80 void d0_bignum_SHUTDOWN(void)
81 {
82         d0_bignum_clear(&temp);
83         gmp_randclear(RANDSTATE);
84 }
85
86 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
87 {
88         static unsigned char numbuf[65536];
89         size_t count = 0;
90         numbuf[0] = mpz_sgn(bignum->z) & 3;
91         if((numbuf[0] & 3) != 0) // nonzero
92         {
93                 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
94                 if(count > sizeof(numbuf) - 1)
95                         return 0;
96                 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
97         }
98         return d0_iobuf_write_packet(buf, numbuf, count + 1);
99 }
100
101 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
102 {
103         static unsigned char numbuf[65536];
104         size_t count = sizeof(numbuf);
105         if(!d0_iobuf_read_packet(buf, numbuf, &count))
106                 return NULL;
107         if(count < 1)
108                 return NULL;
109         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
110         if(numbuf[0] & 3) // nonzero
111         {
112                 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
113                 if(numbuf[0] & 2) // negative
114                         mpz_neg(bignum->z, bignum->z);
115         }
116         else // zero
117         {
118                 mpz_set_ui(bignum->z, 0);
119         }
120         return bignum;
121 }
122
123 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
124 {
125         size_t count;
126         count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
127         if(count > bufsize)
128                 return -1;
129         if(bufsize > count)
130         {
131                 // pad from left (big endian numbers!)
132                 memset(buf, 0, bufsize - count);
133                 buf += bufsize - count;
134         }
135         bufsize = count;
136         mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
137         if(bufsize > count)
138         {
139                 // REALLY BAD
140                 // mpz_sizeinbase lied to us
141                 // buffer overflow
142                 // there is no sane way whatsoever to handle this
143                 abort();
144         }
145         if(bufsize < count)
146         {
147                 // BAD
148                 // mpz_sizeinbase lied to us
149                 // move the number
150                 if(bufsize == 0)
151                 {
152                         memset(buf, 0, count);
153                 }
154                 else
155                 {
156                         memmove(buf + count - bufsize, buf, bufsize);
157                         memset(buf, 0, count - bufsize);
158                 }
159         }
160         return bufsize;
161 }
162
163 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
164 {
165         size_t count;
166         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
167         mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
168         return bignum;
169 }
170
171 d0_bignum_t *d0_bignum_new(void)
172 {
173         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
174         mpz_init(b->z);
175         return b;
176 }
177
178 void d0_bignum_free(d0_bignum_t *a)
179 {
180         mpz_clear(a->z);
181         d0_free(a);
182 }
183
184 void d0_bignum_init(d0_bignum_t *b)
185 {
186         mpz_init(b->z);
187 }
188
189 void d0_bignum_clear(d0_bignum_t *a)
190 {
191         mpz_clear(a->z);
192 }
193
194 size_t d0_bignum_size(const d0_bignum_t *r)
195 {
196         return mpz_sizeinbase(r->z, 2);
197 }
198
199 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
200 {
201         return mpz_cmp(a->z, b->z);
202 }
203
204 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
205 {
206         if(!r) r = d0_bignum_new(); if(!r) return NULL;
207         mpz_sub(temp.z, max->z, min->z);
208         mpz_urandomm(r->z, RANDSTATE, temp.z);
209         mpz_add(r->z, r->z, min->z);
210         return r;
211 }
212
213 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
214 {
215         if(!r) r = d0_bignum_new(); if(!r) return NULL;
216         mpz_urandomb(r->z, RANDSTATE, n);
217         return r;
218 }
219
220 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
221 {
222         if(!r) r = d0_bignum_new(); if(!r) return NULL;
223         mpz_urandomb(r->z, RANDSTATE, n-1);
224         mpz_setbit(r->z, n-1);
225         return r;
226 }
227
228 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
229 {
230         return d0_bignum_int(r, 0);
231 }
232
233 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
234 {
235         return d0_bignum_int(r, 1);
236 }
237
238 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
239 {
240         if(!r) r = d0_bignum_new(); if(!r) return NULL;
241         mpz_set_si(r->z, n);
242         return r;
243 }
244
245 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
246 {
247         if(r == a)
248                 return r; // trivial
249         if(!r) r = d0_bignum_new(); if(!r) return NULL;
250         mpz_set(r->z, a->z);
251         return r;
252 }
253
254 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
255 {
256         if(!r) r = d0_bignum_new(); if(!r) return NULL;
257         mpz_neg(r->z, a->z);
258         return r;
259 }
260
261 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
262 {
263         if(!r) r = d0_bignum_new(); if(!r) return NULL;
264         if(n > 0)
265                 mpz_mul_2exp(r->z, a->z, n);
266         else if(n < 0)
267                 mpz_fdiv_q_2exp(r->z, a->z, -n);
268         else
269                 mpz_set(r->z, a->z);
270         return r;
271 }
272
273 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
274 {
275         if(!r) r = d0_bignum_new(); if(!r) return NULL;
276         mpz_add(r->z, a->z, b->z);
277         return r;
278 }
279
280 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
281 {
282         if(!r) r = d0_bignum_new(); if(!r) return NULL;
283         mpz_sub(r->z, a->z, b->z);
284         return r;
285 }
286
287 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
288 {
289         if(!r) r = d0_bignum_new(); if(!r) return NULL;
290         mpz_mul(r->z, a->z, b->z);
291         return r;
292 }
293
294 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
295 {
296         if(!q && !m)
297                 m = d0_bignum_new();
298         if(q)
299                 if(m)
300                         mpz_fdiv_qr(q->z, m->z, a->z, b->z);
301                 else
302                         mpz_fdiv_q(q->z, a->z, b->z);
303         else
304                 mpz_fdiv_r(m->z, a->z, b->z);
305         if(m)
306                 return m;
307         else
308                 return q;
309 }
310
311 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)
312 {
313         r = d0_bignum_add(r, a, b);
314         mpz_fdiv_r(r->z, r->z, m->z);
315         return r;
316 }
317
318 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)
319 {
320         r = d0_bignum_mul(r, a, b);
321         mpz_fdiv_r(r->z, r->z, m->z);
322         return r;
323 }
324
325 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)
326 {
327         if(!r) r = d0_bignum_new(); if(!r) return NULL;
328         mpz_powm(r->z, a->z, b->z, m->z);
329         return r;
330 }
331
332 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
333 {
334         // here, r MUST be set, as otherwise we cannot return error state!
335         return mpz_invert(r->z, a->z, m->z);
336 }
337
338 int d0_bignum_isprime(d0_bignum_t *r, int param)
339 {
340         return mpz_probab_prime_p(r->z, param);
341 }
342
343 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)
344 {
345         if(!r) r = d0_bignum_new(); if(!r) return NULL;
346         if(s)
347                 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
348         else if(t)
349                 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
350         else
351                 mpz_gcd(r->z, a->z, b->z);
352         return r;
353 }
354
355 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
356 {
357         return mpz_get_str(NULL, base, x->z);
358 }