1 | #include "../oscam-config.h"
|
---|
2 | #ifndef WITH_LIBCRYPTO
|
---|
3 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
4 | /* crypto/bn/bn_lib.c */
|
---|
5 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
---|
6 | * All rights reserved.
|
---|
7 | *
|
---|
8 | * This package is an SSL implementation written
|
---|
9 | * by Eric Young (eay@cryptsoft.com).
|
---|
10 | * The implementation was written so as to conform with Netscapes SSL.
|
---|
11 | *
|
---|
12 | * This library is free for commercial and non-commercial use as long as
|
---|
13 | * the following conditions are aheared to. The following conditions
|
---|
14 | * apply to all code found in this distribution, be it the RC4, RSA,
|
---|
15 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
---|
16 | * included with this distribution is covered by the same copyright terms
|
---|
17 | * except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
---|
18 | *
|
---|
19 | * Copyright remains Eric Young's, and as such any Copyright notices in
|
---|
20 | * the code are not to be removed.
|
---|
21 | * If this package is used in a product, Eric Young should be given attribution
|
---|
22 | * as the author of the parts of the library used.
|
---|
23 | * This can be in the form of a textual message at program startup or
|
---|
24 | * in documentation (online or textual) provided with the package.
|
---|
25 | *
|
---|
26 | * Redistribution and use in source and binary forms, with or without
|
---|
27 | * modification, are permitted provided that the following conditions
|
---|
28 | * are met:
|
---|
29 | * 1. Redistributions of source code must retain the copyright
|
---|
30 | * notice, this list of conditions and the following disclaimer.
|
---|
31 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
32 | * notice, this list of conditions and the following disclaimer in the
|
---|
33 | * documentation and/or other materials provided with the distribution.
|
---|
34 | * 3. All advertising materials mentioning features or use of this software
|
---|
35 | * must display the following acknowledgement:
|
---|
36 | * "This product includes cryptographic software written by
|
---|
37 | * Eric Young (eay@cryptsoft.com)"
|
---|
38 | * The word 'cryptographic' can be left out if the rouines from the library
|
---|
39 | * being used are not cryptographic related :-).
|
---|
40 | * 4. If you include any Windows specific code (or a derivative thereof) from
|
---|
41 | * the apps directory (application code) you must include an acknowledgement:
|
---|
42 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
---|
43 | *
|
---|
44 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
---|
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
---|
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
54 | * SUCH DAMAGE.
|
---|
55 | *
|
---|
56 | * The licence and distribution terms for any publically available version or
|
---|
57 | * derivative of this code cannot be changed. i.e. this code cannot simply be
|
---|
58 | * copied and put under another distribution licence
|
---|
59 | * [including the GNU Public Licence.]
|
---|
60 | */
|
---|
61 |
|
---|
62 | #ifndef BN_DEBUG
|
---|
63 | # undef NDEBUG /* avoid conflicting definitions */
|
---|
64 | # define NDEBUG
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #include <string.h>
|
---|
68 | #include <assert.h>
|
---|
69 | #include <limits.h>
|
---|
70 | #include <stdio.h>
|
---|
71 | #include "bn_lcl.h"
|
---|
72 | #include "openssl_mods.h"
|
---|
73 |
|
---|
74 | const char *BN_version="Big Number 42";
|
---|
75 |
|
---|
76 | /* For a 32 bit machine
|
---|
77 | * 2 - 4 == 128
|
---|
78 | * 3 - 8 == 256
|
---|
79 | * 4 - 16 == 512
|
---|
80 | * 5 - 32 == 1024
|
---|
81 | * 6 - 64 == 2048
|
---|
82 | * 7 - 128 == 4096
|
---|
83 | * 8 - 256 == 8192
|
---|
84 | */
|
---|
85 | static int bn_limit_bits=0;
|
---|
86 | static int bn_limit_num=8; /* (1<<bn_limit_bits) */
|
---|
87 | static int bn_limit_bits_low=0;
|
---|
88 | static int bn_limit_num_low=8; /* (1<<bn_limit_bits_low) */
|
---|
89 | static int bn_limit_bits_high=0;
|
---|
90 | static int bn_limit_num_high=8; /* (1<<bn_limit_bits_high) */
|
---|
91 | static int bn_limit_bits_mont=0;
|
---|
92 | static int bn_limit_num_mont=8; /* (1<<bn_limit_bits_mont) */
|
---|
93 |
|
---|
94 | void BN_set_params(int mult, int high, int low, int mont)
|
---|
95 | {
|
---|
96 | if (mult >= 0)
|
---|
97 | {
|
---|
98 | if (mult > (int)(sizeof(int)*8)-1)
|
---|
99 | mult=sizeof(int)*8-1;
|
---|
100 | bn_limit_bits=mult;
|
---|
101 | bn_limit_num=1<<mult;
|
---|
102 | }
|
---|
103 | if (high >= 0)
|
---|
104 | {
|
---|
105 | if (high > (int)(sizeof(int)*8)-1)
|
---|
106 | high=sizeof(int)*8-1;
|
---|
107 | bn_limit_bits_high=high;
|
---|
108 | bn_limit_num_high=1<<high;
|
---|
109 | }
|
---|
110 | if (low >= 0)
|
---|
111 | {
|
---|
112 | if (low > (int)(sizeof(int)*8)-1)
|
---|
113 | low=sizeof(int)*8-1;
|
---|
114 | bn_limit_bits_low=low;
|
---|
115 | bn_limit_num_low=1<<low;
|
---|
116 | }
|
---|
117 | if (mont >= 0)
|
---|
118 | {
|
---|
119 | if (mont > (int)(sizeof(int)*8)-1)
|
---|
120 | mont=sizeof(int)*8-1;
|
---|
121 | bn_limit_bits_mont=mont;
|
---|
122 | bn_limit_num_mont=1<<mont;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | int BN_get_params(int which)
|
---|
127 | {
|
---|
128 | if (which == 0) return(bn_limit_bits);
|
---|
129 | else if (which == 1) return(bn_limit_bits_high);
|
---|
130 | else if (which == 2) return(bn_limit_bits_low);
|
---|
131 | else if (which == 3) return(bn_limit_bits_mont);
|
---|
132 | else return(0);
|
---|
133 | }
|
---|
134 |
|
---|
135 | const BIGNUM *BN_value_one(void)
|
---|
136 | {
|
---|
137 | static const BN_ULONG data_one=1L;
|
---|
138 | static const BIGNUM const_one={(BN_ULONG *)&data_one,1,1,0,BN_FLG_STATIC_DATA};
|
---|
139 |
|
---|
140 | return(&const_one);
|
---|
141 | }
|
---|
142 |
|
---|
143 | char *BN_options(void)
|
---|
144 | {
|
---|
145 | static int init=0;
|
---|
146 | static char data[16];
|
---|
147 |
|
---|
148 | if (!init)
|
---|
149 | {
|
---|
150 | init++;
|
---|
151 | #ifdef BN_LLONG
|
---|
152 | sprintf(data,"bn(%d,%d)",(int)sizeof(BN_ULLONG)*8,
|
---|
153 | (int)sizeof(BN_ULONG)*8);
|
---|
154 | #else
|
---|
155 | sprintf(data,"bn(%d,%d)",(int)sizeof(BN_ULONG)*8,
|
---|
156 | (int)sizeof(BN_ULONG)*8);
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 | return(data);
|
---|
160 | }
|
---|
161 |
|
---|
162 | int BN_num_bits_word(BN_ULONG l)
|
---|
163 | {
|
---|
164 | static const char bits[256]={
|
---|
165 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
|
---|
166 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
---|
167 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
---|
168 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
---|
169 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
---|
170 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
---|
171 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
---|
172 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
---|
173 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
174 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
175 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
176 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
177 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
178 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
179 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
180 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
---|
181 | };
|
---|
182 |
|
---|
183 | #if defined(SIXTY_FOUR_BIT_LONG)
|
---|
184 | if (l & 0xffffffff00000000L)
|
---|
185 | {
|
---|
186 | if (l & 0xffff000000000000L)
|
---|
187 | {
|
---|
188 | if (l & 0xff00000000000000L)
|
---|
189 | {
|
---|
190 | return(bits[(int)(l>>56)]+56);
|
---|
191 | }
|
---|
192 | else return(bits[(int)(l>>48)]+48);
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | if (l & 0x0000ff0000000000L)
|
---|
197 | {
|
---|
198 | return(bits[(int)(l>>40)]+40);
|
---|
199 | }
|
---|
200 | else return(bits[(int)(l>>32)]+32);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | else
|
---|
204 | #else
|
---|
205 | #ifdef SIXTY_FOUR_BIT
|
---|
206 | if (l & 0xffffffff00000000LL)
|
---|
207 | {
|
---|
208 | if (l & 0xffff000000000000LL)
|
---|
209 | {
|
---|
210 | if (l & 0xff00000000000000LL)
|
---|
211 | {
|
---|
212 | return(bits[(int)(l>>56)]+56);
|
---|
213 | }
|
---|
214 | else return(bits[(int)(l>>48)]+48);
|
---|
215 | }
|
---|
216 | else
|
---|
217 | {
|
---|
218 | if (l & 0x0000ff0000000000LL)
|
---|
219 | {
|
---|
220 | return(bits[(int)(l>>40)]+40);
|
---|
221 | }
|
---|
222 | else return(bits[(int)(l>>32)]+32);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | else
|
---|
226 | #endif
|
---|
227 | #endif
|
---|
228 | {
|
---|
229 | #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
|
---|
230 | if (l & 0xffff0000L)
|
---|
231 | {
|
---|
232 | if (l & 0xff000000L)
|
---|
233 | return(bits[(int)(l>>24L)]+24);
|
---|
234 | else return(bits[(int)(l>>16L)]+16);
|
---|
235 | }
|
---|
236 | else
|
---|
237 | #endif
|
---|
238 | {
|
---|
239 | #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
|
---|
240 | if (l & 0xff00L)
|
---|
241 | return(bits[(int)(l>>8)]+8);
|
---|
242 | else
|
---|
243 | #endif
|
---|
244 | return(bits[(int)(l )] );
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | int BN_num_bits(const BIGNUM *a)
|
---|
250 | {
|
---|
251 | BN_ULONG l;
|
---|
252 | int i;
|
---|
253 |
|
---|
254 | bn_check_top(a);
|
---|
255 |
|
---|
256 | if (a->top == 0) return(0);
|
---|
257 | l=a->d[a->top-1];
|
---|
258 | assert(l != 0);
|
---|
259 | i=(a->top-1)*BN_BITS2;
|
---|
260 | return(i+BN_num_bits_word(l));
|
---|
261 | }
|
---|
262 |
|
---|
263 | void BN_clear_free(BIGNUM *a)
|
---|
264 | {
|
---|
265 | int i;
|
---|
266 |
|
---|
267 | if (a == NULL) return;
|
---|
268 | if (a->d != NULL)
|
---|
269 | {
|
---|
270 | memset(a->d,0,a->dmax*sizeof(a->d[0]));
|
---|
271 | if (!(BN_get_flags(a,BN_FLG_STATIC_DATA)))
|
---|
272 | OPENSSL_free(a->d);
|
---|
273 | }
|
---|
274 | i=BN_get_flags(a,BN_FLG_MALLOCED);
|
---|
275 | memset(a,0,sizeof(BIGNUM));
|
---|
276 | if (i)
|
---|
277 | OPENSSL_free(a);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void BN_free(BIGNUM *a)
|
---|
281 | {
|
---|
282 | if (a == NULL) return;
|
---|
283 | if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA)))
|
---|
284 | OPENSSL_free(a->d);
|
---|
285 | a->flags|=BN_FLG_FREE; /* REMOVE? */
|
---|
286 | if (a->flags & BN_FLG_MALLOCED)
|
---|
287 | OPENSSL_free(a);
|
---|
288 | }
|
---|
289 |
|
---|
290 | void BN_init(BIGNUM *a)
|
---|
291 | {
|
---|
292 | memset(a,0,sizeof(BIGNUM));
|
---|
293 | }
|
---|
294 |
|
---|
295 | BIGNUM *BN_new(void)
|
---|
296 | {
|
---|
297 | BIGNUM *ret;
|
---|
298 |
|
---|
299 | if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL)
|
---|
300 | {
|
---|
301 | return(NULL);
|
---|
302 | }
|
---|
303 | ret->flags=BN_FLG_MALLOCED;
|
---|
304 | ret->top=0;
|
---|
305 | ret->neg=0;
|
---|
306 | ret->dmax=0;
|
---|
307 | ret->d=NULL;
|
---|
308 | return(ret);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* This is an internal function that should not be used in applications.
|
---|
312 | * It ensures that 'b' has enough room for a 'words' word number number.
|
---|
313 | * It is mostly used by the various BIGNUM routines. If there is an error,
|
---|
314 | * NULL is returned. If not, 'b' is returned. */
|
---|
315 |
|
---|
316 | BIGNUM *bn_expand2(BIGNUM *b, int words)
|
---|
317 | {
|
---|
318 | BN_ULONG *A,*a;
|
---|
319 | const BN_ULONG *B;
|
---|
320 | int i;
|
---|
321 |
|
---|
322 | bn_check_top(b);
|
---|
323 |
|
---|
324 | if (words > b->dmax)
|
---|
325 | {
|
---|
326 | if (words > (INT_MAX/(4*BN_BITS2)))
|
---|
327 | {
|
---|
328 | return NULL;
|
---|
329 | }
|
---|
330 |
|
---|
331 | bn_check_top(b);
|
---|
332 | if (BN_get_flags(b,BN_FLG_STATIC_DATA))
|
---|
333 | {
|
---|
334 | return(NULL);
|
---|
335 | }
|
---|
336 | a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*(words+1));
|
---|
337 | if (A == NULL)
|
---|
338 | {
|
---|
339 | return(NULL);
|
---|
340 | }
|
---|
341 | #if 1
|
---|
342 | B=b->d;
|
---|
343 | /* Check if the previous number needs to be copied */
|
---|
344 | if (B != NULL)
|
---|
345 | {
|
---|
346 | #if 0
|
---|
347 | /* This lot is an unrolled loop to copy b->top
|
---|
348 | * BN_ULONGs from B to A
|
---|
349 | */
|
---|
350 | /*
|
---|
351 | * I have nothing against unrolling but it's usually done for
|
---|
352 | * several reasons, namely:
|
---|
353 | * - minimize percentage of decision making code, i.e. branches;
|
---|
354 | * - avoid cache trashing;
|
---|
355 | * - make it possible to schedule loads earlier;
|
---|
356 | * Now let's examine the code below. The cornerstone of C is
|
---|
357 | * "programmer is always right" and that's what we love it for:-)
|
---|
358 | * For this very reason C compilers have to be paranoid when it
|
---|
359 | * comes to data aliasing and assume the worst. Yeah, but what
|
---|
360 | * does it mean in real life? This means that loop body below will
|
---|
361 | * be compiled to sequence of loads immediately followed by stores
|
---|
362 | * as compiler assumes the worst, something in A==B+1 style. As a
|
---|
363 | * result CPU pipeline is going to starve for incoming data. Secondly
|
---|
364 | * if A and B happen to share same cache line such code is going to
|
---|
365 | * cause severe cache trashing. Both factors have severe impact on
|
---|
366 | * performance of modern CPUs and this is the reason why this
|
---|
367 | * particular piece of code is #ifdefed away and replaced by more
|
---|
368 | * "friendly" version found in #else section below. This comment
|
---|
369 | * also applies to BN_copy function.
|
---|
370 | *
|
---|
371 | * <appro@fy.chalmers.se>
|
---|
372 | */
|
---|
373 | for (i=b->top&(~7); i>0; i-=8)
|
---|
374 | {
|
---|
375 | A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];
|
---|
376 | A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];
|
---|
377 | A+=8;
|
---|
378 | B+=8;
|
---|
379 | }
|
---|
380 | switch (b->top&7)
|
---|
381 | {
|
---|
382 | case 7:
|
---|
383 | A[6]=B[6];
|
---|
384 | case 6:
|
---|
385 | A[5]=B[5];
|
---|
386 | case 5:
|
---|
387 | A[4]=B[4];
|
---|
388 | case 4:
|
---|
389 | A[3]=B[3];
|
---|
390 | case 3:
|
---|
391 | A[2]=B[2];
|
---|
392 | case 2:
|
---|
393 | A[1]=B[1];
|
---|
394 | case 1:
|
---|
395 | A[0]=B[0];
|
---|
396 | case 0:
|
---|
397 | /* I need the 'case 0' entry for utrix cc.
|
---|
398 | * If the optimizer is turned on, it does the
|
---|
399 | * switch table by doing
|
---|
400 | * a=top&7
|
---|
401 | * a--;
|
---|
402 | * goto jump_table[a];
|
---|
403 | * If top is 0, this makes us jump to 0xffffffc
|
---|
404 | * which is rather bad :-(.
|
---|
405 | * eric 23-Apr-1998
|
---|
406 | */
|
---|
407 | ;
|
---|
408 | }
|
---|
409 | #else
|
---|
410 | for (i=b->top>>2; i>0; i--,A+=4,B+=4)
|
---|
411 | {
|
---|
412 | /*
|
---|
413 | * The fact that the loop is unrolled
|
---|
414 | * 4-wise is a tribute to Intel. It's
|
---|
415 | * the one that doesn't have enough
|
---|
416 | * registers to accomodate more data.
|
---|
417 | * I'd unroll it 8-wise otherwise:-)
|
---|
418 | *
|
---|
419 | * <appro@fy.chalmers.se>
|
---|
420 | */
|
---|
421 | BN_ULONG a0,a1,a2,a3;
|
---|
422 | a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
|
---|
423 | A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
|
---|
424 | }
|
---|
425 | switch (b->top&3)
|
---|
426 | {
|
---|
427 | case 3: A[2]=B[2];
|
---|
428 | case 2: A[1]=B[1];
|
---|
429 | case 1: A[0]=B[0];
|
---|
430 | case 0: ; /* ultrix cc workaround, see above */
|
---|
431 | }
|
---|
432 | #endif
|
---|
433 | OPENSSL_free(b->d);
|
---|
434 | }
|
---|
435 |
|
---|
436 | b->d=a;
|
---|
437 | b->dmax=words;
|
---|
438 |
|
---|
439 | /* Now need to zero any data between b->top and b->max */
|
---|
440 |
|
---|
441 | A= &(b->d[b->top]);
|
---|
442 | for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8)
|
---|
443 | {
|
---|
444 | A[0]=0; A[1]=0; A[2]=0; A[3]=0;
|
---|
445 | A[4]=0; A[5]=0; A[6]=0; A[7]=0;
|
---|
446 | }
|
---|
447 | for (i=(b->dmax - b->top)&7; i>0; i--,A++)
|
---|
448 | A[0]=0;
|
---|
449 | #else
|
---|
450 | memset(A,0,sizeof(BN_ULONG)*(words+1));
|
---|
451 | memcpy(A,b->d,sizeof(b->d[0])*b->top);
|
---|
452 | b->d=a;
|
---|
453 | b->max=words;
|
---|
454 | #endif
|
---|
455 |
|
---|
456 | /* memset(&(p[b->max]),0,((words+1)-b->max)*sizeof(BN_ULONG)); */
|
---|
457 | /* { int i; for (i=b->max; i<words+1; i++) p[i]=i;} */
|
---|
458 |
|
---|
459 | }
|
---|
460 | return(b);
|
---|
461 | }
|
---|
462 |
|
---|
463 | BIGNUM *BN_dup(const BIGNUM *a)
|
---|
464 | {
|
---|
465 | BIGNUM *r;
|
---|
466 |
|
---|
467 | if (a == NULL) return NULL;
|
---|
468 |
|
---|
469 | bn_check_top(a);
|
---|
470 |
|
---|
471 | r=BN_new();
|
---|
472 | if (r == NULL) return(NULL);
|
---|
473 | return((BIGNUM *)BN_copy(r,a));
|
---|
474 | }
|
---|
475 |
|
---|
476 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
|
---|
477 | {
|
---|
478 | int i;
|
---|
479 | BN_ULONG *A;
|
---|
480 | const BN_ULONG *B;
|
---|
481 |
|
---|
482 | bn_check_top(b);
|
---|
483 |
|
---|
484 | if (a == b) return(a);
|
---|
485 | if (bn_wexpand(a,b->top) == NULL) return(NULL);
|
---|
486 |
|
---|
487 | #if 1
|
---|
488 | A=a->d;
|
---|
489 | B=b->d;
|
---|
490 | for (i=b->top>>2; i>0; i--,A+=4,B+=4)
|
---|
491 | {
|
---|
492 | BN_ULONG a0,a1,a2,a3;
|
---|
493 | a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
|
---|
494 | A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
|
---|
495 | }
|
---|
496 | switch (b->top&3)
|
---|
497 | {
|
---|
498 | case 3: A[2]=B[2];
|
---|
499 | case 2: A[1]=B[1];
|
---|
500 | case 1: A[0]=B[0];
|
---|
501 | case 0: ; /* ultrix cc workaround, see comments in bn_expand2 */
|
---|
502 | }
|
---|
503 | #else
|
---|
504 | memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
|
---|
505 | #endif
|
---|
506 |
|
---|
507 | /* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/
|
---|
508 | a->top=b->top;
|
---|
509 | if ((a->top == 0) && (a->d != NULL))
|
---|
510 | a->d[0]=0;
|
---|
511 | a->neg=b->neg;
|
---|
512 | return(a);
|
---|
513 | }
|
---|
514 |
|
---|
515 | void BN_clear(BIGNUM *a)
|
---|
516 | {
|
---|
517 | if (a->d != NULL)
|
---|
518 | memset(a->d,0,a->dmax*sizeof(a->d[0]));
|
---|
519 | a->top=0;
|
---|
520 | a->neg=0;
|
---|
521 | }
|
---|
522 |
|
---|
523 | BN_ULONG BN_get_word(const BIGNUM *a)
|
---|
524 | {
|
---|
525 | if (a->top > 1)
|
---|
526 | return BN_MASK2;
|
---|
527 | else if (a->top == 1)
|
---|
528 | return a->d[0];
|
---|
529 | /* a->top == 0 */
|
---|
530 | return 0;
|
---|
531 | }
|
---|
532 |
|
---|
533 | int BN_set_word(BIGNUM *a, BN_ULONG w)
|
---|
534 | {
|
---|
535 | bn_check_top(a);
|
---|
536 | if (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0);
|
---|
537 | a->neg = 0;
|
---|
538 | a->d[0] = w;
|
---|
539 | a->top = (w ? 1 : 0);
|
---|
540 | bn_check_top(a);
|
---|
541 | return(1);
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* ignore negative */
|
---|
545 | BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
|
---|
546 | {
|
---|
547 | unsigned int i,m;
|
---|
548 | unsigned int n;
|
---|
549 | BN_ULONG l;
|
---|
550 |
|
---|
551 | if (ret == NULL) ret=BN_new();
|
---|
552 | if (ret == NULL) return(NULL);
|
---|
553 | l=0;
|
---|
554 | n=len;
|
---|
555 | if (n == 0)
|
---|
556 | {
|
---|
557 | ret->top=0;
|
---|
558 | return(ret);
|
---|
559 | }
|
---|
560 | if (bn_expand(ret,(int)(n+2)*8) == NULL)
|
---|
561 | return(NULL);
|
---|
562 | i=((n-1)/BN_BYTES)+1;
|
---|
563 | m=((n-1)%(BN_BYTES));
|
---|
564 | ret->top=i;
|
---|
565 | while (n-- > 0)
|
---|
566 | {
|
---|
567 | l=(l<<8L)| *(s++);
|
---|
568 | if (m-- == 0)
|
---|
569 | {
|
---|
570 | ret->d[--i]=l;
|
---|
571 | l=0;
|
---|
572 | m=BN_BYTES-1;
|
---|
573 | }
|
---|
574 | }
|
---|
575 | /* need to call this due to clear byte at top if avoiding
|
---|
576 | * having the top bit set (-ve number) */
|
---|
577 | bn_fix_top(ret);
|
---|
578 | return(ret);
|
---|
579 | }
|
---|
580 |
|
---|
581 | /* ignore negative */
|
---|
582 | int BN_bn2bin(const BIGNUM *a, unsigned char *to)
|
---|
583 | {
|
---|
584 | int n,i;
|
---|
585 | BN_ULONG l;
|
---|
586 |
|
---|
587 | n=i=BN_num_bytes(a);
|
---|
588 | while (i-- > 0)
|
---|
589 | {
|
---|
590 | l=a->d[i/BN_BYTES];
|
---|
591 | *(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;
|
---|
592 | }
|
---|
593 | return(n);
|
---|
594 | }
|
---|
595 |
|
---|
596 | int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
|
---|
597 | {
|
---|
598 | int i;
|
---|
599 | BN_ULONG t1,t2,*ap,*bp;
|
---|
600 |
|
---|
601 | bn_check_top(a);
|
---|
602 | bn_check_top(b);
|
---|
603 |
|
---|
604 | i=a->top-b->top;
|
---|
605 | if (i != 0) return(i);
|
---|
606 | ap=a->d;
|
---|
607 | bp=b->d;
|
---|
608 | for (i=a->top-1; i>=0; i--)
|
---|
609 | {
|
---|
610 | t1= ap[i];
|
---|
611 | t2= bp[i];
|
---|
612 | if (t1 != t2)
|
---|
613 | return(t1 > t2?1:-1);
|
---|
614 | }
|
---|
615 | return(0);
|
---|
616 | }
|
---|
617 |
|
---|
618 | int BN_cmp(const BIGNUM *a, const BIGNUM *b)
|
---|
619 | {
|
---|
620 | int i;
|
---|
621 | int gt,lt;
|
---|
622 | BN_ULONG t1,t2;
|
---|
623 |
|
---|
624 | if ((a == NULL) || (b == NULL))
|
---|
625 | {
|
---|
626 | if (a != NULL)
|
---|
627 | return(-1);
|
---|
628 | else if (b != NULL)
|
---|
629 | return(1);
|
---|
630 | else
|
---|
631 | return(0);
|
---|
632 | }
|
---|
633 |
|
---|
634 | bn_check_top(a);
|
---|
635 | bn_check_top(b);
|
---|
636 |
|
---|
637 | if (a->neg != b->neg)
|
---|
638 | {
|
---|
639 | if (a->neg)
|
---|
640 | return(-1);
|
---|
641 | else return(1);
|
---|
642 | }
|
---|
643 | if (a->neg == 0)
|
---|
644 | { gt=1; lt= -1; }
|
---|
645 | else { gt= -1; lt=1; }
|
---|
646 |
|
---|
647 | if (a->top > b->top) return(gt);
|
---|
648 | if (a->top < b->top) return(lt);
|
---|
649 | for (i=a->top-1; i>=0; i--)
|
---|
650 | {
|
---|
651 | t1=a->d[i];
|
---|
652 | t2=b->d[i];
|
---|
653 | if (t1 > t2) return(gt);
|
---|
654 | if (t1 < t2) return(lt);
|
---|
655 | }
|
---|
656 | return(0);
|
---|
657 | }
|
---|
658 |
|
---|
659 | int BN_set_bit(BIGNUM *a, int n)
|
---|
660 | {
|
---|
661 | int i,j,k;
|
---|
662 |
|
---|
663 | i=n/BN_BITS2;
|
---|
664 | j=n%BN_BITS2;
|
---|
665 | if (a->top <= i)
|
---|
666 | {
|
---|
667 | if (bn_wexpand(a,i+1) == NULL) return(0);
|
---|
668 | for(k=a->top; k<i+1; k++)
|
---|
669 | a->d[k]=0;
|
---|
670 | a->top=i+1;
|
---|
671 | }
|
---|
672 |
|
---|
673 | a->d[i]|=(((BN_ULONG)1)<<j);
|
---|
674 | return(1);
|
---|
675 | }
|
---|
676 |
|
---|
677 | int BN_clear_bit(BIGNUM *a, int n)
|
---|
678 | {
|
---|
679 | int i,j;
|
---|
680 |
|
---|
681 | i=n/BN_BITS2;
|
---|
682 | j=n%BN_BITS2;
|
---|
683 | if (a->top <= i) return(0);
|
---|
684 |
|
---|
685 | a->d[i]&=(~(((BN_ULONG)1)<<j));
|
---|
686 | bn_fix_top(a);
|
---|
687 | return(1);
|
---|
688 | }
|
---|
689 |
|
---|
690 | int BN_is_bit_set(const BIGNUM *a, int n)
|
---|
691 | {
|
---|
692 | int i,j;
|
---|
693 |
|
---|
694 | if (n < 0) return(0);
|
---|
695 | i=n/BN_BITS2;
|
---|
696 | j=n%BN_BITS2;
|
---|
697 | if (a->top <= i) return(0);
|
---|
698 | return((a->d[i]&(((BN_ULONG)1)<<j))?1:0);
|
---|
699 | }
|
---|
700 |
|
---|
701 | int BN_mask_bits(BIGNUM *a, int n)
|
---|
702 | {
|
---|
703 | int b,w;
|
---|
704 |
|
---|
705 | w=n/BN_BITS2;
|
---|
706 | b=n%BN_BITS2;
|
---|
707 | if (w >= a->top) return(0);
|
---|
708 | if (b == 0)
|
---|
709 | a->top=w;
|
---|
710 | else
|
---|
711 | {
|
---|
712 | a->top=w+1;
|
---|
713 | a->d[w]&= ~(BN_MASK2<<b);
|
---|
714 | }
|
---|
715 | bn_fix_top(a);
|
---|
716 | return(1);
|
---|
717 | }
|
---|
718 |
|
---|
719 | int bn_cmp_words(BN_ULONG *a, BN_ULONG *b, int n)
|
---|
720 | {
|
---|
721 | int i;
|
---|
722 | BN_ULONG aa,bb;
|
---|
723 |
|
---|
724 | aa=a[n-1];
|
---|
725 | bb=b[n-1];
|
---|
726 | if (aa != bb) return((aa > bb)?1:-1);
|
---|
727 | for (i=n-2; i>=0; i--)
|
---|
728 | {
|
---|
729 | aa=a[i];
|
---|
730 | bb=b[i];
|
---|
731 | if (aa != bb) return((aa > bb)?1:-1);
|
---|
732 | }
|
---|
733 | return(0);
|
---|
734 | }
|
---|
735 |
|
---|
736 | #endif
|
---|