]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-openssl.c
automatically include the license notice into .so or .a files
[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  * $Id$
33  */
34
35 /* NOTE: this file links against openssl (http://www.openssl.org), which is
36  * under the OpenSSL License. You may have to abide to its terms too if you use
37  * this file.
38  * To alternatively link to GMP, provide the option --without-openssl to
39  * ./configure.
40  */
41
42 #include "d0_bignum.h"
43
44 #include <assert.h>
45 #include <string.h>
46 #include <openssl/bn.h>
47
48 struct d0_bignum_s
49 {
50         BIGNUM z;
51 };
52
53 static d0_bignum_t temp;
54 static BN_CTX *ctx;
55
56 #include <time.h>
57 #include <stdio.h>
58
59 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
60 {
61         ctx = BN_CTX_new();
62         d0_bignum_init(&temp);
63         return 1;
64 }
65
66 void d0_bignum_SHUTDOWN(void)
67 {
68         d0_bignum_clear(&temp);
69         BN_CTX_free(ctx);
70         ctx = NULL;
71 }
72
73 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
74 {
75         static unsigned char numbuf[65536];
76         size_t count = 0;
77         numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
78         if((numbuf[0] & 3) != 0) // nonzero
79         {
80                 count = BN_num_bytes(&bignum->z);
81                 if(count > sizeof(numbuf) - 1)
82                         return 0;
83                 BN_bn2bin(&bignum->z, numbuf+1);
84         }
85         return d0_iobuf_write_packet(buf, numbuf, count + 1);
86 }
87
88 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
89 {
90         static unsigned char numbuf[65536];
91         size_t count = sizeof(numbuf);
92         if(!d0_iobuf_read_packet(buf, numbuf, &count))
93                 return NULL;
94         if(count < 1)
95                 return NULL;
96         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
97         if(numbuf[0] & 3) // nonzero
98         {
99                 BN_bin2bn(numbuf+1, count-1, &bignum->z);
100                 if(numbuf[0] & 2) // negative
101                         BN_set_negative(&bignum->z, 1);
102         }
103         else // zero
104         {
105                 BN_zero(&bignum->z);
106         }
107         return bignum;
108 }
109
110 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
111 {
112         size_t count;
113         count = BN_num_bytes(&bignum->z);
114         if(count > bufsize)
115                 return -1;
116         if(bufsize > count)
117         {
118                 // pad from left (big endian numbers!)
119                 memset(buf, 0, bufsize - count);
120                 buf += bufsize - count;
121         }
122         BN_bn2bin(&bignum->z, buf);
123         return bufsize;
124 }
125
126 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
127 {
128         size_t count;
129         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
130         BN_bin2bn(buf, bufsize, &bignum->z);
131         return bignum;
132 }
133
134 d0_bignum_t *d0_bignum_new(void)
135 {
136         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
137         BN_init(&b->z);
138         return b;
139 }
140
141 void d0_bignum_free(d0_bignum_t *a)
142 {
143         BN_free(&a->z);
144         d0_free(a);
145 }
146
147 void d0_bignum_init(d0_bignum_t *b)
148 {
149         BN_init(&b->z);
150 }
151
152 void d0_bignum_clear(d0_bignum_t *a)
153 {
154         BN_free(&a->z);
155 }
156
157 size_t d0_bignum_size(const d0_bignum_t *r)
158 {
159         return BN_num_bits(&r->z);
160 }
161
162 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
163 {
164         return BN_cmp(&a->z, &b->z);
165 }
166
167 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
168 {
169         if(!r) r = d0_bignum_new(); if(!r) return NULL;
170         BN_sub(&temp.z, &max->z, &min->z);
171         BN_rand_range(&r->z, &temp.z);
172         BN_add(&r->z, &r->z, &min->z);
173         return r;
174 }
175
176 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
177 {
178         if(!r) r = d0_bignum_new(); if(!r) return NULL;
179         BN_rand(&r->z, n, -1, 0);
180         return r;
181 }
182
183 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
184 {
185         if(!r) r = d0_bignum_new(); if(!r) return NULL;
186         BN_rand(&r->z, n, 0, 0);
187         return r;
188 }
189
190 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
191 {
192         if(!r) r = d0_bignum_new(); if(!r) return NULL;
193         BN_zero(&r->z);
194         return r;
195 }
196
197 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
198 {
199         if(!r) r = d0_bignum_new(); if(!r) return NULL;
200         BN_one(&r->z);
201         return r;
202 }
203
204 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
205 {
206         if(!r) r = d0_bignum_new(); if(!r) return NULL;
207         BN_set_word(&r->z, n);
208         return r;
209 }
210
211 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
212 {
213         if(r == a)
214                 return r; // trivial
215         if(!r) r = d0_bignum_new(); if(!r) return NULL;
216         BN_copy(&r->z, &a->z);
217         return r;
218 }
219
220 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
221 {
222         if(!r) r = d0_bignum_new(); if(!r) return NULL;
223         if(r != a)
224                 BN_copy(&r->z, &a->z);
225         BN_set_negative(&r->z, !BN_is_negative(&r->z));
226         return r;
227 }
228
229 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
230 {
231         if(!r) r = d0_bignum_new(); if(!r) return NULL;
232         if(n > 0)
233                 BN_lshift(&r->z, &a->z, n);
234         else if(n < 0)
235                 BN_rshift(&r->z, &a->z, -n);
236         else if(r != a)
237                 BN_copy(&r->z, &a->z);
238         return r;
239 }
240
241 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
242 {
243         if(!r) r = d0_bignum_new(); if(!r) return NULL;
244         BN_add(&r->z, &a->z, &b->z);
245         return r;
246 }
247
248 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
249 {
250         if(!r) r = d0_bignum_new(); if(!r) return NULL;
251         BN_sub(&r->z, &a->z, &b->z);
252         return r;
253 }
254
255 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
256 {
257         if(!r) r = d0_bignum_new(); if(!r) return NULL;
258         BN_mul(&r->z, &a->z, &b->z, ctx);
259         return r;
260 }
261
262 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
263 {
264         if(!q && !m)
265                 m = d0_bignum_new();
266         if(q)
267         {
268                 if(m)
269                         BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
270                 else
271                         BN_div(&q->z, NULL, &a->z, &b->z, ctx);
272                 assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
273         }
274         else
275                 BN_nnmod(&m->z, &a->z, &b->z, ctx);
276         if(m)
277                 return m;
278         else
279                 return q;
280 }
281
282 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)
283 {
284         if(!r) r = d0_bignum_new(); if(!r) return NULL;
285         BN_mod_add(&r->z, &a->z, &b->z, &m->z, ctx);
286         return r;
287 }
288
289 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)
290 {
291         if(!r) r = d0_bignum_new(); if(!r) return NULL;
292         BN_mod_mul(&r->z, &a->z, &b->z, &m->z, ctx);
293         return r;
294 }
295
296 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)
297 {
298         if(!r) r = d0_bignum_new(); if(!r) return NULL;
299         BN_mod_exp(&r->z, &a->z, &b->z, &m->z, ctx);
300         return r;
301 }
302
303 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
304 {
305         // here, r MUST be set, as otherwise we cannot return error state!
306         return !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
307 }
308
309 int d0_bignum_isprime(d0_bignum_t *r, int param)
310 {
311         return BN_is_prime(&r->z, param, NULL, ctx, NULL);
312 }
313
314 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)
315 {
316         if(!r) r = d0_bignum_new(); if(!r) return NULL;
317         if(s)
318                 assert(!"Extended gcd not implemented");
319         else if(t)
320                 assert(!"Extended gcd not implemented");
321         else
322                 BN_gcd(&r->z, &a->z, &b->z, ctx);
323         return r;
324 }
325
326 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
327 {
328         if(base == 10)
329                 return BN_bn2dec(&x->z);
330         else if(base == 16)
331                 return BN_bn2hex(&x->z);
332         else
333                 assert(!"Other bases not implemented");
334 }