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>
48 #include <openssl/crypto.h>
50 // for stupid OpenSSL versions in Mac OS X
51 #ifndef BN_is_negative
52 #define BN_is_negative(a) ((a)->neg != 0)
53 #define BN_set_negative(a,n) ((a)->neg = ((n) && !BN_is_zero(a)))
61 static d0_bignum_t temp;
63 static void *tempmutex = NULL; // hold this mutex when using ctx or temp
70 void locking_function(int mode, int l, const char *file, int line)
73 if(mode & CRYPTO_LOCK)
79 typedef struct CRYPTO_dynlock_value
84 struct CRYPTO_dynlock_value *dyn_create_function(const char *file, int line)
86 return (struct CRYPTO_dynlock_value *) d0_createmutex();
89 void dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
92 if(mode & CRYPTO_LOCK)
98 void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char *file, int line)
100 void *m = (void *) l;
104 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
108 unsigned char buf[256];
111 tempmutex = d0_createmutex();
112 d0_lockmutex(tempmutex);
114 n = CRYPTO_num_locks();
115 locks = d0_malloc(n * sizeof(*locks));
116 for(i = 0; i < n; ++i)
117 locks[i] = d0_createmutex();
119 CRYPTO_set_locking_callback(locking_function);
120 CRYPTO_set_dynlock_create_callback(dyn_create_function);
121 CRYPTO_set_dynlock_lock_callback(dyn_lock_function);
122 CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function);
125 d0_bignum_init(&temp);
129 HCRYPTPROV hCryptProv;
130 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
132 if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
134 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
137 CryptReleaseContext(hCryptProv, 0);
139 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
141 if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
143 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
146 CryptReleaseContext(hCryptProv, 0);
150 fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
155 f = fopen("/dev/urandom", "rb");
157 f = fopen("/dev/random", "rb");
161 if(fread(buf, sizeof(buf), 1, f) != 1)
163 fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
170 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
174 RAND_add(buf, sizeof(buf), sizeof(buf));
176 d0_unlockmutex(tempmutex);
179 // FIXME initialize the RNG on Windows on UNIX it is done right already
182 void d0_bignum_SHUTDOWN(void)
186 d0_lockmutex(tempmutex);
188 d0_bignum_clear(&temp);
192 n = CRYPTO_num_locks();
193 for(i = 0; i < n; ++i)
194 d0_destroymutex(locks[i]);
197 d0_unlockmutex(tempmutex);
198 d0_destroymutex(tempmutex);
202 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
204 static __thread unsigned char numbuf[65536];
206 numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
207 if((numbuf[0] & 3) != 0) // nonzero
209 count = BN_num_bytes(&bignum->z);
210 if(count > sizeof(numbuf) - 1)
212 BN_bn2bin(&bignum->z, numbuf+1);
214 return d0_iobuf_write_packet(buf, numbuf, count + 1);
217 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
219 static __thread unsigned char numbuf[65536];
220 size_t count = sizeof(numbuf);
221 if(!d0_iobuf_read_packet(buf, numbuf, &count))
225 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
226 if(numbuf[0] & 3) // nonzero
228 BN_bin2bn(numbuf+1, count-1, &bignum->z);
229 if(numbuf[0] & 2) // negative
230 BN_set_negative(&bignum->z, 1);
239 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
242 count = BN_num_bytes(&bignum->z);
247 // pad from left (big endian numbers!)
248 memset(buf, 0, bufsize - count);
249 buf += bufsize - count;
251 BN_bn2bin(&bignum->z, buf);
255 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
258 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
259 BN_bin2bn(buf, bufsize, &bignum->z);
263 d0_bignum_t *d0_bignum_new(void)
265 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
270 void d0_bignum_free(d0_bignum_t *a)
276 void d0_bignum_init(d0_bignum_t *b)
281 void d0_bignum_clear(d0_bignum_t *a)
286 size_t d0_bignum_size(const d0_bignum_t *r)
288 return BN_num_bits(&r->z);
291 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
293 return BN_cmp(&a->z, &b->z);
296 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
298 if(!r) r = d0_bignum_new(); if(!r) return NULL;
299 d0_lockmutex(tempmutex);
300 BN_sub(&temp.z, &max->z, &min->z);
301 BN_rand_range(&r->z, &temp.z);
302 d0_unlockmutex(tempmutex);
303 BN_add(&r->z, &r->z, &min->z);
307 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
309 if(!r) r = d0_bignum_new(); if(!r) return NULL;
310 BN_rand(&r->z, n, -1, 0);
314 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
316 if(!r) r = d0_bignum_new(); if(!r) return NULL;
317 BN_rand(&r->z, n, 0, 0);
321 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
323 if(!r) r = d0_bignum_new(); if(!r) return NULL;
328 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
330 if(!r) r = d0_bignum_new(); if(!r) return NULL;
335 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
337 if(!r) r = d0_bignum_new(); if(!r) return NULL;
338 BN_set_word(&r->z, n);
342 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
346 if(!r) r = d0_bignum_new(); if(!r) return NULL;
347 BN_copy(&r->z, &a->z);
351 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
353 if(!r) r = d0_bignum_new(); if(!r) return NULL;
355 BN_copy(&r->z, &a->z);
356 BN_set_negative(&r->z, !BN_is_negative(&r->z));
360 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
362 if(!r) r = d0_bignum_new(); if(!r) return NULL;
364 BN_lshift(&r->z, &a->z, n);
366 BN_rshift(&r->z, &a->z, -n);
368 BN_copy(&r->z, &a->z);
372 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
374 if(!r) r = d0_bignum_new(); if(!r) return NULL;
375 BN_add(&r->z, &a->z, &b->z);
379 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
381 if(!r) r = d0_bignum_new(); if(!r) return NULL;
382 BN_sub(&r->z, &a->z, &b->z);
386 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
388 if(!r) r = d0_bignum_new(); if(!r) return NULL;
389 d0_lockmutex(tempmutex);
390 BN_mul(&r->z, &a->z, &b->z, ctx);
391 d0_unlockmutex(tempmutex);
395 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
399 d0_lockmutex(tempmutex);
403 BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
405 BN_div(&q->z, NULL, &a->z, &b->z, ctx);
406 assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
409 BN_nnmod(&m->z, &a->z, &b->z, ctx);
410 d0_unlockmutex(tempmutex);
417 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)
419 if(!r) r = d0_bignum_new(); if(!r) return NULL;
420 d0_lockmutex(tempmutex);
421 BN_mod_add(&r->z, &a->z, &b->z, &m->z, ctx);
422 d0_unlockmutex(tempmutex);
426 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)
428 if(!r) r = d0_bignum_new(); if(!r) return NULL;
429 d0_lockmutex(tempmutex);
430 BN_mod_sub(&r->z, &a->z, &b->z, &m->z, ctx);
431 d0_unlockmutex(tempmutex);
435 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)
437 if(!r) r = d0_bignum_new(); if(!r) return NULL;
438 d0_lockmutex(tempmutex);
439 BN_mod_mul(&r->z, &a->z, &b->z, &m->z, ctx);
440 d0_unlockmutex(tempmutex);
444 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)
446 if(!r) r = d0_bignum_new(); if(!r) return NULL;
447 d0_lockmutex(tempmutex);
448 BN_mod_exp(&r->z, &a->z, &b->z, &m->z, ctx);
449 d0_unlockmutex(tempmutex);
453 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
455 // here, r MUST be set, as otherwise we cannot return error state!
457 d0_lockmutex(tempmutex);
458 ret = !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
459 d0_unlockmutex(tempmutex);
463 int d0_bignum_isprime(const d0_bignum_t *r, int param)
466 d0_lockmutex(tempmutex);
468 ret = BN_is_prime_fasttest(&r->z, 1, NULL, ctx, NULL, 1);
470 ret = BN_is_prime(&r->z, param, NULL, ctx, NULL);
471 d0_unlockmutex(tempmutex);
475 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)
477 if(!r) r = d0_bignum_new(); if(!r) return NULL;
479 assert(!"Extended gcd not implemented");
481 assert(!"Extended gcd not implemented");
484 d0_lockmutex(tempmutex);
485 BN_gcd(&r->z, &a->z, &b->z, ctx);
486 d0_unlockmutex(tempmutex);
491 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
494 return BN_bn2dec(&x->z);
496 return BN_bn2hex(&x->z);
498 assert(!"Other bases not implemented");