]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-openssl.c
mutexify the internal bignum functions
[xonotic/d0_blind_id.git] / d0_bignum-openssl.c
1 /*
2  * FILE:        d0_bignum-openssl.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  * 
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
30  * SUCH DAMAGE.
31  *
32  * $Format:commit %H$
33  * $Id$
34  */
35
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
38  * this file.
39  * To alternatively link to GMP, provide the option --without-openssl to
40  * ./configure.
41  */
42
43 #include "d0_bignum.h"
44
45 #include <assert.h>
46 #include <string.h>
47 #include <openssl/bn.h>
48
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)))
53 #endif
54
55 struct d0_bignum_s
56 {
57         BIGNUM z;
58 };
59
60 // FIXME implement http://www.openssl.org/docs/crypto/threads.html
61
62 static d0_bignum_t temp;
63 static BN_CTX *ctx;
64 static void *tempmutex = NULL; // hold this mutex when using ctx or temp
65
66 #include <time.h>
67 #include <stdio.h>
68
69 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
70 {
71         tempmutex = d0_createmutex();
72         d0_lockmutex(tempmutex);
73
74         ctx = BN_CTX_new();
75         d0_bignum_init(&temp);
76
77         d0_unlockmutex(tempmutex);
78
79         return 1;
80         // FIXME initialize the RNG on Windows on UNIX it is done right already
81 }
82
83 void d0_bignum_SHUTDOWN(void)
84 {
85         d0_lockmutex(tempmutex);
86
87         d0_bignum_clear(&temp);
88         BN_CTX_free(ctx);
89         ctx = NULL;
90
91         d0_unlockmutex(tempmutex);
92         d0_destroymutex(tempmutex);
93         tempmutex = NULL;
94 }
95
96 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
97 {
98         static __thread unsigned char numbuf[65536];
99         size_t count = 0;
100         numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
101         if((numbuf[0] & 3) != 0) // nonzero
102         {
103                 count = BN_num_bytes(&bignum->z);
104                 if(count > sizeof(numbuf) - 1)
105                         return 0;
106                 BN_bn2bin(&bignum->z, numbuf+1);
107         }
108         return d0_iobuf_write_packet(buf, numbuf, count + 1);
109 }
110
111 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
112 {
113         static __thread unsigned char numbuf[65536];
114         size_t count = sizeof(numbuf);
115         if(!d0_iobuf_read_packet(buf, numbuf, &count))
116                 return NULL;
117         if(count < 1)
118                 return NULL;
119         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
120         if(numbuf[0] & 3) // nonzero
121         {
122                 BN_bin2bn(numbuf+1, count-1, &bignum->z);
123                 if(numbuf[0] & 2) // negative
124                         BN_set_negative(&bignum->z, 1);
125         }
126         else // zero
127         {
128                 BN_zero(&bignum->z);
129         }
130         return bignum;
131 }
132
133 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
134 {
135         size_t count;
136         count = BN_num_bytes(&bignum->z);
137         if(count > bufsize)
138                 return -1;
139         if(bufsize > count)
140         {
141                 // pad from left (big endian numbers!)
142                 memset(buf, 0, bufsize - count);
143                 buf += bufsize - count;
144         }
145         BN_bn2bin(&bignum->z, buf);
146         return bufsize;
147 }
148
149 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
150 {
151         size_t count;
152         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
153         BN_bin2bn(buf, bufsize, &bignum->z);
154         return bignum;
155 }
156
157 d0_bignum_t *d0_bignum_new(void)
158 {
159         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
160         BN_init(&b->z);
161         return b;
162 }
163
164 void d0_bignum_free(d0_bignum_t *a)
165 {
166         BN_free(&a->z);
167         d0_free(a);
168 }
169
170 void d0_bignum_init(d0_bignum_t *b)
171 {
172         BN_init(&b->z);
173 }
174
175 void d0_bignum_clear(d0_bignum_t *a)
176 {
177         BN_free(&a->z);
178 }
179
180 size_t d0_bignum_size(const d0_bignum_t *r)
181 {
182         return BN_num_bits(&r->z);
183 }
184
185 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
186 {
187         return BN_cmp(&a->z, &b->z);
188 }
189
190 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
191 {
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);
198         return r;
199 }
200
201 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
202 {
203         if(!r) r = d0_bignum_new(); if(!r) return NULL;
204         BN_rand(&r->z, n, -1, 0);
205         return r;
206 }
207
208 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
209 {
210         if(!r) r = d0_bignum_new(); if(!r) return NULL;
211         BN_rand(&r->z, n, 0, 0);
212         return r;
213 }
214
215 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
216 {
217         if(!r) r = d0_bignum_new(); if(!r) return NULL;
218         BN_zero(&r->z);
219         return r;
220 }
221
222 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
223 {
224         if(!r) r = d0_bignum_new(); if(!r) return NULL;
225         BN_one(&r->z);
226         return r;
227 }
228
229 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
230 {
231         if(!r) r = d0_bignum_new(); if(!r) return NULL;
232         BN_set_word(&r->z, n);
233         return r;
234 }
235
236 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
237 {
238         if(r == a)
239                 return r; // trivial
240         if(!r) r = d0_bignum_new(); if(!r) return NULL;
241         BN_copy(&r->z, &a->z);
242         return r;
243 }
244
245 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
246 {
247         if(!r) r = d0_bignum_new(); if(!r) return NULL;
248         if(r != a)
249                 BN_copy(&r->z, &a->z);
250         BN_set_negative(&r->z, !BN_is_negative(&r->z));
251         return r;
252 }
253
254 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
255 {
256         if(!r) r = d0_bignum_new(); if(!r) return NULL;
257         if(n > 0)
258                 BN_lshift(&r->z, &a->z, n);
259         else if(n < 0)
260                 BN_rshift(&r->z, &a->z, -n);
261         else if(r != a)
262                 BN_copy(&r->z, &a->z);
263         return r;
264 }
265
266 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
267 {
268         if(!r) r = d0_bignum_new(); if(!r) return NULL;
269         BN_add(&r->z, &a->z, &b->z);
270         return r;
271 }
272
273 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
274 {
275         if(!r) r = d0_bignum_new(); if(!r) return NULL;
276         BN_sub(&r->z, &a->z, &b->z);
277         return r;
278 }
279
280 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
281 {
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);
286         return r;
287 }
288
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)
290 {
291         if(!q && !m)
292                 m = d0_bignum_new();
293         d0_lockmutex(tempmutex);
294         if(q)
295         {
296                 if(m)
297                         BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
298                 else
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");
301         }
302         else
303                 BN_nnmod(&m->z, &a->z, &b->z, ctx);
304         d0_unlockmutex(tempmutex);
305         if(m)
306                 return m;
307         else
308                 return q;
309 }
310
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)
312 {
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);
317         return r;
318 }
319
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)
321 {
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);
326         return r;
327 }
328
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)
330 {
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);
335         return r;
336 }
337
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)
339 {
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);
344         return r;
345 }
346
347 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
348 {
349         // here, r MUST be set, as otherwise we cannot return error state!
350         int ret;
351         d0_lockmutex(tempmutex);
352         ret = !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
353         d0_unlockmutex(tempmutex);
354         return ret;
355 }
356
357 int d0_bignum_isprime(const d0_bignum_t *r, int param)
358 {
359         int ret;
360         d0_lockmutex(tempmutex);
361         if(param <= 0)
362                 ret = BN_is_prime_fasttest(&r->z, 1, NULL, ctx, NULL, 1);
363         else
364                 ret = BN_is_prime(&r->z, param, NULL, ctx, NULL);
365         d0_unlockmutex(tempmutex);
366         return ret;
367 }
368
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)
370 {
371         if(!r) r = d0_bignum_new(); if(!r) return NULL;
372         if(s)
373                 assert(!"Extended gcd not implemented");
374         else if(t)
375                 assert(!"Extended gcd not implemented");
376         else
377         {
378                 d0_lockmutex(tempmutex);
379                 BN_gcd(&r->z, &a->z, &b->z, ctx);
380                 d0_unlockmutex(tempmutex);
381         }
382         return r;
383 }
384
385 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
386 {
387         if(base == 10)
388                 return BN_bn2dec(&x->z);
389         else if(base == 16)
390                 return BN_bn2hex(&x->z);
391         else
392                 assert(!"Other bases not implemented");
393 }