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
20 #include "d0_bignum.h"
24 #include <openssl/bn.h>
31 static d0_bignum_t temp;
37 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
40 d0_bignum_init(&temp);
44 void d0_bignum_SHUTDOWN(void)
46 d0_bignum_clear(&temp);
51 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
53 static unsigned char numbuf[65536];
55 numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
56 if((numbuf[0] & 3) != 0) // nonzero
58 count = BN_num_bytes(&bignum->z);
59 if(count > sizeof(numbuf) - 1)
61 BN_bn2bin(&bignum->z, numbuf+1);
63 return d0_iobuf_write_packet(buf, numbuf, count + 1);
66 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
68 static unsigned char numbuf[65536];
69 size_t count = sizeof(numbuf);
70 if(!d0_iobuf_read_packet(buf, numbuf, &count))
74 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
75 if(numbuf[0] & 3) // nonzero
77 BN_bin2bn(numbuf+1, count-1, &bignum->z);
78 if(numbuf[0] & 2) // negative
79 BN_set_negative(&bignum->z, 1);
88 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
91 count = BN_num_bytes(&bignum->z);
96 // pad from left (big endian numbers!)
97 memset(buf, 0, bufsize - count);
98 buf += bufsize - count;
100 BN_bn2bin(&bignum->z, buf);
104 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
107 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
108 BN_bin2bn(buf, bufsize, &bignum->z);
112 d0_bignum_t *d0_bignum_new(void)
114 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
119 void d0_bignum_free(d0_bignum_t *a)
125 void d0_bignum_init(d0_bignum_t *b)
130 void d0_bignum_clear(d0_bignum_t *a)
135 size_t d0_bignum_size(const d0_bignum_t *r)
137 return BN_num_bits(&r->z);
140 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
142 return BN_cmp(&a->z, &b->z);
145 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
147 if(!r) r = d0_bignum_new(); if(!r) return NULL;
148 BN_sub(&temp.z, &max->z, &min->z);
149 BN_rand_range(&r->z, &temp.z);
150 BN_add(&r->z, &r->z, &min->z);
154 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
156 if(!r) r = d0_bignum_new(); if(!r) return NULL;
157 BN_rand(&r->z, n, -1, 0);
161 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
163 if(!r) r = d0_bignum_new(); if(!r) return NULL;
164 BN_rand(&r->z, n, 0, 0);
168 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
170 if(!r) r = d0_bignum_new(); if(!r) return NULL;
175 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
177 if(!r) r = d0_bignum_new(); if(!r) return NULL;
182 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
184 if(!r) r = d0_bignum_new(); if(!r) return NULL;
185 BN_set_word(&r->z, n);
189 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
193 if(!r) r = d0_bignum_new(); if(!r) return NULL;
194 BN_copy(&r->z, &a->z);
198 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
200 if(!r) r = d0_bignum_new(); if(!r) return NULL;
202 BN_copy(&r->z, &a->z);
203 BN_set_negative(&r->z, !BN_is_negative(&r->z));
207 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
209 if(!r) r = d0_bignum_new(); if(!r) return NULL;
211 BN_lshift(&r->z, &a->z, n);
213 BN_rshift(&r->z, &a->z, n);
215 BN_copy(&r->z, &a->z);
219 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
221 if(!r) r = d0_bignum_new(); if(!r) return NULL;
222 BN_add(&r->z, &a->z, &b->z);
226 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
228 if(!r) r = d0_bignum_new(); if(!r) return NULL;
229 BN_sub(&r->z, &a->z, &b->z);
233 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
235 if(!r) r = d0_bignum_new(); if(!r) return NULL;
236 BN_mul(&r->z, &a->z, &b->z, ctx);
240 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
247 BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
249 BN_div(&q->z, NULL, &a->z, &b->z, ctx);
250 assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
253 BN_nnmod(&m->z, &a->z, &b->z, ctx);
260 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)
262 if(!r) r = d0_bignum_new(); if(!r) return NULL;
263 BN_mod_add(&r->z, &a->z, &b->z, &m->z, ctx);
267 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)
269 if(!r) r = d0_bignum_new(); if(!r) return NULL;
270 BN_mod_mul(&r->z, &a->z, &b->z, &m->z, ctx);
274 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)
276 if(!r) r = d0_bignum_new(); if(!r) return NULL;
277 BN_mod_exp(&r->z, &a->z, &b->z, &m->z, ctx);
281 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
283 // here, r MUST be set, as otherwise we cannot return error state!
284 return !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
287 int d0_bignum_isprime(d0_bignum_t *r, int param)
289 return BN_is_prime(&r->z, param, NULL, ctx, NULL);
292 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)
294 if(!r) r = d0_bignum_new(); if(!r) return NULL;
296 assert(!"Extended gcd not implemented");
298 assert(!"Extended gcd not implemented");
300 BN_gcd(&r->z, &a->z, &b->z, ctx);
304 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
307 return BN_bn2dec(&x->z);
309 return BN_bn2hex(&x->z);
311 assert(!"Other bases not implemented");