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