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"
22 #include <openssl/bn.h>
29 static d0_bignum_t temp;
35 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
38 d0_bignum_init(&temp);
42 void d0_bignum_SHUTDOWN(void)
44 d0_bignum_clear(&temp);
48 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
50 static unsigned char numbuf[65536];
52 numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
53 if((numbuf[0] & 3) != 0) // nonzero
55 count = BN_num_bytes(&bignum->z);
56 if(count > sizeof(numbuf) - 1)
58 BN_bn2bin(numbuf+1, &bignum->z);
60 return d0_iobuf_write_packet(buf, numbuf, count + 1);
63 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
65 static unsigned char numbuf[65536];
66 size_t count = sizeof(numbuf);
67 if(!d0_iobuf_read_packet(buf, numbuf, &count))
71 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
72 if(numbuf[0] & 3) // nonzero
74 BN_bin2bn(numbuf+1, count-1, &bignum->z);
75 if(numbuf[0] & 2) // negative
76 BN_set_negative(&bignum->z, 1);
85 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
88 count = BN_num_bytes(&bignum->z);
93 // pad from left (big endian numbers!)
94 memset(buf, 0, bufsize - count);
95 buf += bufsize - count;
97 BN_bn2bin(&bignum->z, buf);
101 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
104 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
105 BN_bin2bn(buf, bufsize, &bignum->z);
109 d0_bignum_t *d0_bignum_new(void)
111 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
116 void d0_bignum_free(d0_bignum_t *a)
122 void d0_bignum_init(d0_bignum_t *b)
127 void d0_bignum_clear(d0_bignum_t *a)
132 size_t d0_bignum_size(const d0_bignum_t *r)
134 return BN_num_bits(&r->z);
137 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
139 return BN_cmp(&a->z, &b->z);
142 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
144 if(!r) r = d0_bignum_new(); if(!r) return NULL;
145 BN_sub(&temp.z, &max->z, &min->z);
146 BN_rand_range(&r->z, &temp.z);
147 BN_add(&r->z, &r->z, &min->z);
151 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
153 if(!r) r = d0_bignum_new(); if(!r) return NULL;
154 BN_rand(&r->z, n, -1, 0);
158 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
160 if(!r) r = d0_bignum_new(); if(!r) return NULL;
161 BN_rand(&r->z, n, 0, 0);
165 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
167 if(!r) r = d0_bignum_new(); if(!r) return NULL;
172 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
174 if(!r) r = d0_bignum_new(); if(!r) return NULL;
179 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
181 if(!r) r = d0_bignum_new(); if(!r) return NULL;
182 BN_set_word(&r->z, n);
186 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
190 if(!r) r = d0_bignum_new(); if(!r) return NULL;
191 BN_copy(&r->z, &a->z);
195 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
197 if(!r) r = d0_bignum_new(); if(!r) return NULL;
199 BN_copy(&r->z, &a->z);
200 BN_set_negative(&r->z, !BN_is_negative(&r->z));
204 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
206 if(!r) r = d0_bignum_new(); if(!r) return NULL;
208 BN_lshift(&r->z, &a->z, n);
210 BN_rshift(&r->z, &a->z, n);
212 BN_copy(&r->z, &a->z);
216 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
218 if(!r) r = d0_bignum_new(); if(!r) return NULL;
219 BN_add(&r->z, &a->z, &b->z);
223 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
225 if(!r) r = d0_bignum_new(); if(!r) return NULL;
226 BN_sub(&r->z, &a->z, &b->z);
230 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
232 if(!r) r = d0_bignum_new(); if(!r) return NULL;
233 BN_mul(&r->z, &a->z, &b->z);
237 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
244 BN_div(&q->z, &m->z, &a->z, &b->z, &ctx);
246 BN_div(&q->z, NULL, &a->z, &b->z, &ctx);
247 assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
250 BN_nnmod(&q->z, NULL, &a->z, &b->z, &ctx);
257 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)
259 if(!r) r = d0_bignum_new(); if(!r) return NULL;
260 BN_mod_add(&r->z, &a->z, &b->z, &m->z, &ctx);
264 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)
266 if(!r) r = d0_bignum_new(); if(!r) return NULL;
267 BN_mod_mul(&r->z, &a->z, &b->z, &m->z, &ctx);
271 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)
273 if(!r) r = d0_bignum_new(); if(!r) return NULL;
274 BN_mod_exp(&r->z, &a->z, &b->z, &m->z, &ctx);
278 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
280 // here, r MUST be set, as otherwise we cannot return error state!
281 return !!BN_mod_inverse(&r->z, &a->z, &m->z, &ctx);
284 int d0_bignum_isprime(d0_bignum_t *r, int param)
286 return BN_is_prime(&r->z, param, NULL, &ctx, NULL);
289 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)
291 if(!r) r = d0_bignum_new(); if(!r) return NULL;
293 assert(!"Extended gcd not implemented");
295 assert(!"Extended gcd not implemented");
297 BN_gcd(&r->z, &a->z, &b->z, &ctx);
301 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
304 return BN_bn2dec(&x->z);
306 return BN_bn2hex(&x->z);
308 assert(!"Other bases not implemented");