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