]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-tommath.c
don't use AX_PTHREAD
[xonotic/d0_blind_id.git] / d0_bignum-tommath.c
1 /*
2  * FILE:        d0_bignum-tommath.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Format:commit %H$
33  * $Id$
34  */
35
36 #ifdef WIN32
37 #include <windows.h>
38 #include <wincrypt.h>
39 #endif
40
41 #include "d0_bignum.h"
42
43 #include <tommath.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <assert.h>
47
48 struct d0_bignum_s
49 {
50         mp_int z;
51 };
52
53 static d0_bignum_t temp;
54 static unsigned char numbuf[65536];
55 static void *tempmutex = NULL; // hold this mutex when using temp or numbuf
56
57 #include <stdio.h>
58
59 #ifdef WIN32
60 HCRYPTPROV hCryptProv;
61 #else
62 static FILE *randf;
63 #endif
64
65 void rand_bytes(unsigned char *buf, size_t n)
66 {
67 #ifdef WIN32
68         CryptGenRandom(hCryptProv, n, (PBYTE) buf);
69 #else
70         if(!randf)
71                 return;
72         fread(buf, 1, n, randf);
73 #endif
74 }
75
76 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
77 {
78         D0_BOOL ret = 1;
79         unsigned char buf[256];
80
81         tempmutex = d0_createmutex();
82         d0_lockmutex(tempmutex);
83
84         d0_bignum_init(&temp);
85 #ifdef WIN32
86         {
87                 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
88                 {
89                 }
90                 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
91                 {
92                 }
93                 else
94                 {
95                         fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
96                         ret = 0;
97                         hCryptProv = NULL;
98                 }
99         }
100 #else
101         randf = fopen("/dev/urandom", "rb");
102         if(!randf)
103                 randf = fopen("/dev/random", "rb");
104         if(randf)
105         {
106                 setbuf(randf, NULL);
107         }
108         else
109         {
110                 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
111                 ret = 0;
112         }
113 #endif
114
115         d0_unlockmutex(tempmutex);
116
117         return ret;
118 }
119
120 void d0_bignum_SHUTDOWN(void)
121 {
122         d0_lockmutex(tempmutex);
123
124         d0_bignum_clear(&temp);
125 #ifdef WIN32
126         if(hCryptProv)
127         {
128                 CryptReleaseContext(hCryptProv, 0);
129                 hCryptProv = NULL;
130         }
131 #endif
132
133         d0_unlockmutex(tempmutex);
134         d0_destroymutex(tempmutex);
135         tempmutex = NULL;
136 }
137
138 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
139 {
140         D0_BOOL ret;
141         size_t count = 0;
142
143         d0_lockmutex(tempmutex);
144         numbuf[0] = (mp_iszero(&bignum->z) ? 0 : (bignum->z.sign == MP_ZPOS) ? 1 : 3);
145         if((numbuf[0] & 3) != 0) // nonzero
146         {
147                 count = mp_unsigned_bin_size((mp_int *) &bignum->z);
148                 if(count > sizeof(numbuf) - 1)
149                 {
150                         d0_unlockmutex(tempmutex);
151                         return 0;
152                 }
153                 mp_to_unsigned_bin((mp_int *) &bignum->z, numbuf+1);
154         }
155         ret = d0_iobuf_write_packet(buf, numbuf, count + 1);
156         d0_unlockmutex(tempmutex);
157         return ret;
158 }
159
160 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
161 {
162         size_t count = sizeof(numbuf);
163         d0_lockmutex(tempmutex);
164         if(!d0_iobuf_read_packet(buf, numbuf, &count))
165         {
166                 d0_unlockmutex(tempmutex);
167                 return NULL;
168         }
169         if(count < 1)
170         {
171                 d0_unlockmutex(tempmutex);
172                 return NULL;
173         }
174         if(!bignum)
175                 bignum = d0_bignum_new();
176         if(!bignum)
177         {
178                 d0_unlockmutex(tempmutex);
179                 return NULL;
180         }
181         if(numbuf[0] & 3) // nonzero
182         {
183                 mp_read_unsigned_bin(&bignum->z, numbuf+1, count-1);
184                 if(numbuf[0] & 2) // negative
185                         bignum->z.sign = MP_NEG;
186         }
187         else // zero
188         {
189                 mp_zero(&bignum->z);
190         }
191         d0_unlockmutex(tempmutex);
192         return bignum;
193 }
194
195 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
196 {
197         unsigned long bufsize_;
198         unsigned long count;
199         count = mp_unsigned_bin_size((mp_int *) &bignum->z);
200         if(count > bufsize)
201                 return -1;
202         if(bufsize > count)
203         {
204                 // pad from left (big endian numbers!)
205                 memset(buf, 0, bufsize - count);
206                 buf += bufsize - count;
207         }
208         bufsize_ = count;
209         mp_to_unsigned_bin_n((mp_int *) &bignum->z, buf, &bufsize_);
210         bufsize = bufsize_;
211         if(bufsize > count)
212         {
213                 // REALLY BAD
214                 // mpz_sizeinbase lied to us
215                 // buffer overflow
216                 // there is no sane way whatsoever to handle this
217                 abort();
218         }
219         if(bufsize < count)
220         {
221                 // BAD
222                 // mpz_sizeinbase lied to us
223                 // move the number
224                 if(count == 0)
225                 {
226                         memset(buf, 0, count);
227                 }
228                 else
229                 {
230                         memmove(buf + count - bufsize, buf, bufsize);
231                         memset(buf, 0, count - bufsize);
232                 }
233         }
234         return bufsize;
235 }
236
237 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
238 {
239         size_t count;
240         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
241         mp_read_unsigned_bin(&bignum->z, buf, bufsize);
242         return bignum;
243 }
244
245 d0_bignum_t *d0_bignum_new(void)
246 {
247         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
248         mp_init(&b->z);
249         return b;
250 }
251
252 void d0_bignum_free(d0_bignum_t *a)
253 {
254         mp_clear(&a->z);
255         d0_free(a);
256 }
257
258 void d0_bignum_init(d0_bignum_t *b)
259 {
260         mp_init(&b->z);
261 }
262
263 void d0_bignum_clear(d0_bignum_t *a)
264 {
265         mp_clear(&a->z);
266 }
267
268 size_t d0_bignum_size(const d0_bignum_t *r)
269 {
270         return mp_count_bits((mp_int *) &r->z);
271 }
272
273 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
274 {
275         return mp_cmp((mp_int *) &a->z, (mp_int *) &b->z);
276 }
277
278 static d0_bignum_t *d0_bignum_rand_0_to_limit(d0_bignum_t *r, const d0_bignum_t *limit)
279 {
280         size_t n = d0_bignum_size(limit);
281         size_t b = (n + 7) / 8;
282         unsigned char mask = "\xFF\x7F\x3F\x1F\x0F\x07\x03\x01"[8*b - n];
283         assert(b <= sizeof(numbuf));
284         d0_lockmutex(tempmutex);
285         for(;;)
286         {
287                 rand_bytes(numbuf, b);
288                 numbuf[0] &= mask;
289                 r = d0_bignum_import_unsigned(r, numbuf, b);
290                 if(d0_bignum_cmp(r, limit) < 0)
291                 {
292                         d0_unlockmutex(tempmutex);
293                         return r;
294                 }
295         }
296 }
297
298 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
299 {
300         d0_lockmutex(tempmutex);
301         mp_sub((mp_int *) &max->z, (mp_int *) &min->z, &temp.z);
302         r = d0_bignum_rand_0_to_limit(r, &temp);
303         d0_unlockmutex(tempmutex);
304         mp_add((mp_int *) &r->z, (mp_int *) &min->z, &r->z);
305         return r;
306 }
307
308 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
309 {
310         d0_lockmutex(tempmutex);
311         if(!d0_bignum_one(&temp))
312         {
313                 d0_unlockmutex(tempmutex);
314                 return NULL;
315         }
316         if(!d0_bignum_shl(&temp, &temp, n))
317         {
318                 d0_unlockmutex(tempmutex);
319                 return NULL;
320         }
321         r = d0_bignum_rand_0_to_limit(r, &temp);
322         d0_unlockmutex(tempmutex);
323         return r;
324 }
325
326 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
327 {
328         d0_lockmutex(tempmutex);
329         if(!d0_bignum_one(&temp))
330         {
331                 d0_unlockmutex(tempmutex);
332                 return NULL;
333         }
334         if(!d0_bignum_shl(&temp, &temp, n-1))
335         {
336                 d0_unlockmutex(tempmutex);
337                 return NULL;
338         }
339         r = d0_bignum_rand_0_to_limit(r, &temp);
340         if(!d0_bignum_add(r, r, &temp))
341         {
342                 d0_unlockmutex(tempmutex);
343                 return NULL;
344         }
345         d0_unlockmutex(tempmutex);
346         return r;
347 }
348
349 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
350 {
351         if(!r) r = d0_bignum_new(); if(!r) return NULL;
352         mp_zero(&r->z);
353         return r;
354 }
355
356 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
357 {
358         return d0_bignum_int(r, 1);
359 }
360
361 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
362 {
363         if(!r) r = d0_bignum_new(); if(!r) return NULL;
364         mp_set_int(&r->z, n);
365         return r;
366 }
367
368 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
369 {
370         if(r == a)
371                 return r; // trivial
372         if(!r) r = d0_bignum_new(); if(!r) return NULL;
373         mp_copy((mp_int *) &a->z, &r->z);
374         return r;
375 }
376
377 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
378 {
379         if(!r) r = d0_bignum_new(); if(!r) return NULL;
380         mp_neg((mp_int *) &a->z, &r->z);
381         return r;
382 }
383
384 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
385 {
386         if(!r) r = d0_bignum_new(); if(!r) return NULL;
387         if(n > 0)
388                 mp_mul_2d((mp_int *) &a->z,  n, &r->z);
389         else if(n < 0)
390                 mp_div_2d((mp_int *) &a->z, -n, &r->z, NULL);
391         else
392                 mp_copy((mp_int *) &a->z, &r->z);
393         return r;
394 }
395
396 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
397 {
398         if(!r) r = d0_bignum_new(); if(!r) return NULL;
399         mp_add((mp_int *) &a->z, (mp_int *) &b->z, &r->z);
400         return r;
401 }
402
403 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
404 {
405         if(!r) r = d0_bignum_new(); if(!r) return NULL;
406         mp_sub((mp_int *) &a->z, (mp_int *) &b->z, &r->z);
407         return r;
408 }
409
410 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
411 {
412         if(!r) r = d0_bignum_new(); if(!r) return NULL;
413         mp_mul((mp_int *) &a->z, (mp_int *) &b->z, &r->z);
414         return r;
415 }
416
417 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
418 {
419         if(!q && !m)
420                 m = d0_bignum_new();
421         if(q)
422                 mp_div((mp_int *) &a->z, (mp_int *) &b->z, &q->z, m ? &m->z : NULL);
423         else
424                 mp_mod((mp_int *) &a->z, (mp_int *) &b->z, &m->z);
425         if(m)
426                 return m;
427         else
428                 return q;
429 }
430
431 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)
432 {
433         if(!r) r = d0_bignum_new(); if(!r) return NULL;
434         mp_addmod((mp_int *) &a->z, (mp_int *) &b->z, (mp_int *) &m->z, &r->z);
435         return r;
436 }
437
438 d0_bignum_t *d0_bignum_mod_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
439 {
440         if(!r) r = d0_bignum_new(); if(!r) return NULL;
441         mp_submod((mp_int *) &a->z, (mp_int *) &b->z, (mp_int *) &m->z, &r->z);
442         return r;
443 }
444
445 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)
446 {
447         if(!r) r = d0_bignum_new(); if(!r) return NULL;
448         mp_mulmod((mp_int *) &a->z, (mp_int *) &b->z, (mp_int *) &m->z, &r->z);
449         return r;
450 }
451
452 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)
453 {
454         if(!r) r = d0_bignum_new(); if(!r) return NULL;
455         mp_exptmod((mp_int *) &a->z, (mp_int *) &b->z, (mp_int *) &m->z, &r->z);
456         return r;
457 }
458
459 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
460 {
461         // here, r MUST be set, as otherwise we cannot return error state!
462         return mp_invmod((mp_int *) &a->z, (mp_int *) &m->z, &r->z) == MP_OKAY;
463 }
464
465 int d0_bignum_isprime(const d0_bignum_t *r, int param)
466 {
467         int ret = 0;
468         if(param < 1)
469                 param = 1;
470         mp_prime_is_prime((mp_int *) &r->z, param, &ret);
471         return ret;
472 }
473
474 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)
475 {
476         if(!r) r = d0_bignum_new(); if(!r) return NULL;
477         if(s || t)
478                 mp_exteuclid((mp_int *) &a->z, (mp_int *) &b->z, s ? &s->z : NULL, t ? &t->z : NULL, &r->z);
479         else
480                 mp_gcd((mp_int *) &a->z, (mp_int *) &b->z, &r->z);
481         return r;
482 }
483
484 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
485 {
486         char *str;
487         int sz = 0;
488         mp_radix_size((mp_int *) &x->z, base, &sz);
489         str = d0_malloc(sz + 1);
490         mp_toradix_n((mp_int *) &x->z, str, base, sz + 1);
491         return str;
492 }