2 * FILE: d0_bignum-openssl.c
3 * AUTHOR: Rudolf Polzer - divVerent@xonotic.org
5 * Copyright (c) 2010, Rudolf Polzer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 /* NOTE: this file links against openssl (http://www.openssl.org), which is
37 * under the OpenSSL License. You may have to abide to its terms too if you use
39 * To alternatively link to GMP, provide the option --without-openssl to
43 #include "d0_bignum.h"
47 #include <openssl/bn.h>
49 // for stupid OpenSSL versions in Mac OS X
50 #ifndef BN_is_negative
51 #define BN_is_negative(a) ((a)->neg != 0)
52 #define BN_set_negative(a,n) ((a)->neg = ((n) && !BN_is_zero(a)))
60 // FIXME implement http://www.openssl.org/docs/crypto/threads.html
62 static d0_bignum_t temp;
64 static void *tempmutex = NULL; // hold this mutex when using ctx or temp
69 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
71 tempmutex = d0_createmutex();
72 d0_lockmutex(tempmutex);
75 d0_bignum_init(&temp);
77 d0_unlockmutex(tempmutex);
80 // FIXME initialize the RNG on Windows on UNIX it is done right already
83 void d0_bignum_SHUTDOWN(void)
85 d0_lockmutex(tempmutex);
87 d0_bignum_clear(&temp);
91 d0_unlockmutex(tempmutex);
92 d0_destroymutex(tempmutex);
96 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
98 static __thread unsigned char numbuf[65536];
100 numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
101 if((numbuf[0] & 3) != 0) // nonzero
103 count = BN_num_bytes(&bignum->z);
104 if(count > sizeof(numbuf) - 1)
106 BN_bn2bin(&bignum->z, numbuf+1);
108 return d0_iobuf_write_packet(buf, numbuf, count + 1);
111 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
113 static __thread unsigned char numbuf[65536];
114 size_t count = sizeof(numbuf);
115 if(!d0_iobuf_read_packet(buf, numbuf, &count))
119 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
120 if(numbuf[0] & 3) // nonzero
122 BN_bin2bn(numbuf+1, count-1, &bignum->z);
123 if(numbuf[0] & 2) // negative
124 BN_set_negative(&bignum->z, 1);
133 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
136 count = BN_num_bytes(&bignum->z);
141 // pad from left (big endian numbers!)
142 memset(buf, 0, bufsize - count);
143 buf += bufsize - count;
145 BN_bn2bin(&bignum->z, buf);
149 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
152 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
153 BN_bin2bn(buf, bufsize, &bignum->z);
157 d0_bignum_t *d0_bignum_new(void)
159 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
164 void d0_bignum_free(d0_bignum_t *a)
170 void d0_bignum_init(d0_bignum_t *b)
175 void d0_bignum_clear(d0_bignum_t *a)
180 size_t d0_bignum_size(const d0_bignum_t *r)
182 return BN_num_bits(&r->z);
185 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
187 return BN_cmp(&a->z, &b->z);
190 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
192 if(!r) r = d0_bignum_new(); if(!r) return NULL;
193 d0_lockmutex(tempmutex);
194 BN_sub(&temp.z, &max->z, &min->z);
195 BN_rand_range(&r->z, &temp.z);
196 d0_unlockmutex(tempmutex);
197 BN_add(&r->z, &r->z, &min->z);
201 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
203 if(!r) r = d0_bignum_new(); if(!r) return NULL;
204 BN_rand(&r->z, n, -1, 0);
208 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
210 if(!r) r = d0_bignum_new(); if(!r) return NULL;
211 BN_rand(&r->z, n, 0, 0);
215 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
217 if(!r) r = d0_bignum_new(); if(!r) return NULL;
222 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
224 if(!r) r = d0_bignum_new(); if(!r) return NULL;
229 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
231 if(!r) r = d0_bignum_new(); if(!r) return NULL;
232 BN_set_word(&r->z, n);
236 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
240 if(!r) r = d0_bignum_new(); if(!r) return NULL;
241 BN_copy(&r->z, &a->z);
245 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
247 if(!r) r = d0_bignum_new(); if(!r) return NULL;
249 BN_copy(&r->z, &a->z);
250 BN_set_negative(&r->z, !BN_is_negative(&r->z));
254 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
256 if(!r) r = d0_bignum_new(); if(!r) return NULL;
258 BN_lshift(&r->z, &a->z, n);
260 BN_rshift(&r->z, &a->z, -n);
262 BN_copy(&r->z, &a->z);
266 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
268 if(!r) r = d0_bignum_new(); if(!r) return NULL;
269 BN_add(&r->z, &a->z, &b->z);
273 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
275 if(!r) r = d0_bignum_new(); if(!r) return NULL;
276 BN_sub(&r->z, &a->z, &b->z);
280 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
282 if(!r) r = d0_bignum_new(); if(!r) return NULL;
283 d0_lockmutex(tempmutex);
284 BN_mul(&r->z, &a->z, &b->z, ctx);
285 d0_unlockmutex(tempmutex);
289 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
293 d0_lockmutex(tempmutex);
297 BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
299 BN_div(&q->z, NULL, &a->z, &b->z, ctx);
300 assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
303 BN_nnmod(&m->z, &a->z, &b->z, ctx);
304 d0_unlockmutex(tempmutex);
311 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)
313 if(!r) r = d0_bignum_new(); if(!r) return NULL;
314 d0_lockmutex(tempmutex);
315 BN_mod_add(&r->z, &a->z, &b->z, &m->z, ctx);
316 d0_unlockmutex(tempmutex);
320 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)
322 if(!r) r = d0_bignum_new(); if(!r) return NULL;
323 d0_lockmutex(tempmutex);
324 BN_mod_sub(&r->z, &a->z, &b->z, &m->z, ctx);
325 d0_unlockmutex(tempmutex);
329 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)
331 if(!r) r = d0_bignum_new(); if(!r) return NULL;
332 d0_lockmutex(tempmutex);
333 BN_mod_mul(&r->z, &a->z, &b->z, &m->z, ctx);
334 d0_unlockmutex(tempmutex);
338 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)
340 if(!r) r = d0_bignum_new(); if(!r) return NULL;
341 d0_lockmutex(tempmutex);
342 BN_mod_exp(&r->z, &a->z, &b->z, &m->z, ctx);
343 d0_unlockmutex(tempmutex);
347 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
349 // here, r MUST be set, as otherwise we cannot return error state!
351 d0_lockmutex(tempmutex);
352 ret = !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
353 d0_unlockmutex(tempmutex);
357 int d0_bignum_isprime(const d0_bignum_t *r, int param)
360 d0_lockmutex(tempmutex);
362 ret = BN_is_prime_fasttest(&r->z, 1, NULL, ctx, NULL, 1);
364 ret = BN_is_prime(&r->z, param, NULL, ctx, NULL);
365 d0_unlockmutex(tempmutex);
369 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)
371 if(!r) r = d0_bignum_new(); if(!r) return NULL;
373 assert(!"Extended gcd not implemented");
375 assert(!"Extended gcd not implemented");
378 d0_lockmutex(tempmutex);
379 BN_gcd(&r->z, &a->z, &b->z, ctx);
380 d0_unlockmutex(tempmutex);
385 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
388 return BN_bn2dec(&x->z);
390 return BN_bn2hex(&x->z);
392 assert(!"Other bases not implemented");