2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010 Rudolf Polzer
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.
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.
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
25 #include "d0_bignum.h"
36 static gmp_randstate_t RANDSTATE;
37 static d0_bignum_t temp;
42 void d0_bignum_INITIALIZE(void)
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
52 HCRYPTPROV hCryptProv;
53 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
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);
61 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
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);
67 fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
71 f = fopen("/dev/urandom", "rb");
73 f = fopen("/dev/random", "rb");
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");
82 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
85 mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
86 gmp_randseed(RANDSTATE, temp.z);
89 void d0_bignum_SHUTDOWN(void)
91 d0_bignum_clear(&temp);
92 gmp_randclear(RANDSTATE);
95 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
97 static unsigned char numbuf[65536];
99 numbuf[0] = mpz_sgn(bignum->z) & 3;
100 if((numbuf[0] & 3) != 0) // nonzero
102 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
103 if(count > sizeof(numbuf) - 1)
105 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
107 return d0_iobuf_write_packet(buf, numbuf, count + 1);
110 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
112 static unsigned char numbuf[65536];
113 size_t count = sizeof(numbuf);
114 if(!d0_iobuf_read_packet(buf, numbuf, &count))
118 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
119 if(numbuf[0] & 3) // nonzero
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);
127 mpz_set_ui(bignum->z, 0);
132 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
135 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
140 // pad from left (big endian numbers!)
141 memset(buf, 0, bufsize - count);
142 buf += bufsize - count;
145 mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
149 // mpz_sizeinbase lied to us
151 // there is no sane way whatsoever to handle this
157 // mpz_sizeinbase lied to us
161 memset(buf, 0, count);
165 memmove(buf + count - bufsize, buf, bufsize);
166 memset(buf, 0, count - bufsize);
172 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
175 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
176 mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
180 d0_bignum_t *d0_bignum_new(void)
182 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
187 void d0_bignum_free(d0_bignum_t *a)
193 void d0_bignum_init(d0_bignum_t *b)
198 void d0_bignum_clear(d0_bignum_t *a)
203 size_t d0_bignum_size(const d0_bignum_t *r)
205 return mpz_sizeinbase(r->z, 2);
208 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
210 return mpz_cmp(a->z, b->z);
213 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
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);
222 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
224 if(!r) r = d0_bignum_new(); if(!r) return NULL;
225 mpz_urandomb(r->z, RANDSTATE, n);
229 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
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);
237 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
239 return d0_bignum_int(r, 0);
242 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
244 return d0_bignum_int(r, 1);
247 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
249 if(!r) r = d0_bignum_new(); if(!r) return NULL;
254 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
258 if(!r) r = d0_bignum_new(); if(!r) return NULL;
263 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
265 if(!r) r = d0_bignum_new(); if(!r) return NULL;
270 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
272 if(!r) r = d0_bignum_new(); if(!r) return NULL;
274 mpz_mul_2exp(r->z, a->z, n);
276 mpz_fdiv_q_2exp(r->z, a->z, -n);
282 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
284 if(!r) r = d0_bignum_new(); if(!r) return NULL;
285 mpz_add(r->z, a->z, b->z);
289 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
291 if(!r) r = d0_bignum_new(); if(!r) return NULL;
292 mpz_sub(r->z, a->z, b->z);
296 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
298 if(!r) r = d0_bignum_new(); if(!r) return NULL;
299 mpz_mul(r->z, a->z, b->z);
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)
309 mpz_fdiv_qr(q->z, m->z, a->z, b->z);
311 mpz_fdiv_q(q->z, a->z, b->z);
313 mpz_fdiv_r(m->z, a->z, b->z);
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)
322 r = d0_bignum_add(r, a, b);
323 mpz_fdiv_r(r->z, r->z, m->z);
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)
329 r = d0_bignum_mul(r, a, b);
330 mpz_fdiv_r(r->z, r->z, m->z);
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)
336 if(!r) r = d0_bignum_new(); if(!r) return NULL;
337 mpz_powm(r->z, a->z, b->z, m->z);
341 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
343 // here, r MUST be set, as otherwise we cannot return error state!
344 return mpz_invert(r->z, a->z, m->z);
347 int d0_bignum_isprime(d0_bignum_t *r, int param)
349 return mpz_probab_prime_p(r->z, param);
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)
354 if(!r) r = d0_bignum_new(); if(!r) return NULL;
356 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
358 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
360 mpz_gcd(r->z, a->z, b->z);
364 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
366 return mpz_get_str(NULL, base, x->z);