summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_gf2m.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_gf2m.c')
-rw-r--r--src/lib/libcrypto/bn/bn_gf2m.c1320
1 files changed, 0 insertions, 1320 deletions
diff --git a/src/lib/libcrypto/bn/bn_gf2m.c b/src/lib/libcrypto/bn/bn_gf2m.c
deleted file mode 100644
index 40c1a94220..0000000000
--- a/src/lib/libcrypto/bn/bn_gf2m.c
+++ /dev/null
@@ -1,1320 +0,0 @@
1/* $OpenBSD: bn_gf2m.c,v 1.20 2015/06/11 15:55:28 jsing Exp $ */
2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 *
5 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7 * to the OpenSSL project.
8 *
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
11 *
12 * In addition, Sun covenants to all licensees who provide a reciprocal
13 * covenant with respect to their own patents if any, not to sue under
14 * current and future patent claims necessarily infringed by the making,
15 * using, practicing, selling, offering for sale and/or otherwise
16 * disposing of the ECC Code as delivered hereunder (or portions thereof),
17 * provided that such covenant shall not apply:
18 * 1) for code that a licensee deletes from the ECC Code;
19 * 2) separates from the ECC Code; or
20 * 3) for infringements caused by:
21 * i) the modification of the ECC Code or
22 * ii) the combination of the ECC Code with other software or
23 * devices where such combination causes the infringement.
24 *
25 * The software is originally written by Sheueling Chang Shantz and
26 * Douglas Stebila of Sun Microsystems Laboratories.
27 *
28 */
29
30/* NOTE: This file is licensed pursuant to the OpenSSL license below
31 * and may be modified; but after modifications, the above covenant
32 * may no longer apply! In such cases, the corresponding paragraph
33 * ["In addition, Sun covenants ... causes the infringement."] and
34 * this note can be edited out; but please keep the Sun copyright
35 * notice and attribution. */
36
37/* ====================================================================
38 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 *
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in
49 * the documentation and/or other materials provided with the
50 * distribution.
51 *
52 * 3. All advertising materials mentioning features or use of this
53 * software must display the following acknowledgment:
54 * "This product includes software developed by the OpenSSL Project
55 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
56 *
57 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
58 * endorse or promote products derived from this software without
59 * prior written permission. For written permission, please contact
60 * openssl-core@openssl.org.
61 *
62 * 5. Products derived from this software may not be called "OpenSSL"
63 * nor may "OpenSSL" appear in their names without prior written
64 * permission of the OpenSSL Project.
65 *
66 * 6. Redistributions of any form whatsoever must retain the following
67 * acknowledgment:
68 * "This product includes software developed by the OpenSSL Project
69 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
70 *
71 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
72 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
74 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
75 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
77 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
78 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
80 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
81 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
82 * OF THE POSSIBILITY OF SUCH DAMAGE.
83 * ====================================================================
84 *
85 * This product includes cryptographic software written by Eric Young
86 * (eay@cryptsoft.com). This product includes software written by Tim
87 * Hudson (tjh@cryptsoft.com).
88 *
89 */
90
91#include <limits.h>
92#include <stdio.h>
93
94#include <openssl/opensslconf.h>
95
96#include <openssl/err.h>
97
98#include "bn_lcl.h"
99
100#ifndef OPENSSL_NO_EC2M
101
102/* Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should fail. */
103#define MAX_ITERATIONS 50
104
105static const BN_ULONG SQR_tb[16] =
106 { 0, 1, 4, 5, 16, 17, 20, 21,
10764, 65, 68, 69, 80, 81, 84, 85 };
108/* Platform-specific macros to accelerate squaring. */
109#ifdef _LP64
110#define SQR1(w) \
111 SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
112 SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
113 SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
114 SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
115#define SQR0(w) \
116 SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
117 SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
118 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
119 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
120#else
121#define SQR1(w) \
122 SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
123 SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
124#define SQR0(w) \
125 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
126 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
127#endif
128
129#if !defined(OPENSSL_BN_ASM_GF2m)
130/* Product of two polynomials a, b each with degree < BN_BITS2 - 1,
131 * result is a polynomial r with degree < 2 * BN_BITS - 1
132 * The caller MUST ensure that the variables have the right amount
133 * of space allocated.
134 */
135static void
136bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b)
137{
138#ifndef _LP64
139 BN_ULONG h, l, s;
140 BN_ULONG tab[8], top2b = a >> 30;
141 BN_ULONG a1, a2, a4;
142
143 a1 = a & (0x3FFFFFFF);
144 a2 = a1 << 1;
145 a4 = a2 << 1;
146
147 tab[0] = 0;
148 tab[1] = a1;
149 tab[2] = a2;
150 tab[3] = a1 ^ a2;
151 tab[4] = a4;
152 tab[5] = a1 ^ a4;
153 tab[6] = a2 ^ a4;
154 tab[7] = a1 ^ a2 ^ a4;
155
156 s = tab[b & 0x7];
157 l = s;
158 s = tab[b >> 3 & 0x7];
159 l ^= s << 3;
160 h = s >> 29;
161 s = tab[b >> 6 & 0x7];
162 l ^= s << 6;
163 h ^= s >> 26;
164 s = tab[b >> 9 & 0x7];
165 l ^= s << 9;
166 h ^= s >> 23;
167 s = tab[b >> 12 & 0x7];
168 l ^= s << 12;
169 h ^= s >> 20;
170 s = tab[b >> 15 & 0x7];
171 l ^= s << 15;
172 h ^= s >> 17;
173 s = tab[b >> 18 & 0x7];
174 l ^= s << 18;
175 h ^= s >> 14;
176 s = tab[b >> 21 & 0x7];
177 l ^= s << 21;
178 h ^= s >> 11;
179 s = tab[b >> 24 & 0x7];
180 l ^= s << 24;
181 h ^= s >> 8;
182 s = tab[b >> 27 & 0x7];
183 l ^= s << 27;
184 h ^= s >> 5;
185 s = tab[b >> 30];
186 l ^= s << 30;
187 h ^= s >> 2;
188
189 /* compensate for the top two bits of a */
190 if (top2b & 01) {
191 l ^= b << 30;
192 h ^= b >> 2;
193 }
194 if (top2b & 02) {
195 l ^= b << 31;
196 h ^= b >> 1;
197 }
198
199 *r1 = h;
200 *r0 = l;
201#else
202 BN_ULONG h, l, s;
203 BN_ULONG tab[16], top3b = a >> 61;
204 BN_ULONG a1, a2, a4, a8;
205
206 a1 = a & (0x1FFFFFFFFFFFFFFFULL);
207 a2 = a1 << 1;
208 a4 = a2 << 1;
209 a8 = a4 << 1;
210
211 tab[0] = 0;
212 tab[1] = a1;
213 tab[2] = a2;
214 tab[3] = a1 ^ a2;
215 tab[4] = a4;
216 tab[5] = a1 ^ a4;
217 tab[6] = a2 ^ a4;
218 tab[7] = a1 ^ a2 ^ a4;
219 tab[8] = a8;
220 tab[9] = a1 ^ a8;
221 tab[10] = a2 ^ a8;
222 tab[11] = a1 ^ a2 ^ a8;
223 tab[12] = a4 ^ a8;
224 tab[13] = a1 ^ a4 ^ a8;
225 tab[14] = a2 ^ a4 ^ a8;
226 tab[15] = a1 ^ a2 ^ a4 ^ a8;
227
228 s = tab[b & 0xF];
229 l = s;
230 s = tab[b >> 4 & 0xF];
231 l ^= s << 4;
232 h = s >> 60;
233 s = tab[b >> 8 & 0xF];
234 l ^= s << 8;
235 h ^= s >> 56;
236 s = tab[b >> 12 & 0xF];
237 l ^= s << 12;
238 h ^= s >> 52;
239 s = tab[b >> 16 & 0xF];
240 l ^= s << 16;
241 h ^= s >> 48;
242 s = tab[b >> 20 & 0xF];
243 l ^= s << 20;
244 h ^= s >> 44;
245 s = tab[b >> 24 & 0xF];
246 l ^= s << 24;
247 h ^= s >> 40;
248 s = tab[b >> 28 & 0xF];
249 l ^= s << 28;
250 h ^= s >> 36;
251 s = tab[b >> 32 & 0xF];
252 l ^= s << 32;
253 h ^= s >> 32;
254 s = tab[b >> 36 & 0xF];
255 l ^= s << 36;
256 h ^= s >> 28;
257 s = tab[b >> 40 & 0xF];
258 l ^= s << 40;
259 h ^= s >> 24;
260 s = tab[b >> 44 & 0xF];
261 l ^= s << 44;
262 h ^= s >> 20;
263 s = tab[b >> 48 & 0xF];
264 l ^= s << 48;
265 h ^= s >> 16;
266 s = tab[b >> 52 & 0xF];
267 l ^= s << 52;
268 h ^= s >> 12;
269 s = tab[b >> 56 & 0xF];
270 l ^= s << 56;
271 h ^= s >> 8;
272 s = tab[b >> 60];
273 l ^= s << 60;
274 h ^= s >> 4;
275
276 /* compensate for the top three bits of a */
277 if (top3b & 01) {
278 l ^= b << 61;
279 h ^= b >> 3;
280 }
281 if (top3b & 02) {
282 l ^= b << 62;
283 h ^= b >> 2;
284 }
285 if (top3b & 04) {
286 l ^= b << 63;
287 h ^= b >> 1;
288 }
289
290 *r1 = h;
291 *r0 = l;
292#endif
293}
294
295/* Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
296 * result is a polynomial r with degree < 4 * BN_BITS2 - 1
297 * The caller MUST ensure that the variables have the right amount
298 * of space allocated.
299 */
300static void
301bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
302 const BN_ULONG b1, const BN_ULONG b0)
303{
304 BN_ULONG m1, m0;
305
306 /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
307 bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
308 bn_GF2m_mul_1x1(r + 1, r, a0, b0);
309 bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
310 /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
311 r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
312 r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
313}
314#else
315void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
316 BN_ULONG b0);
317#endif
318
319/* Add polynomials a and b and store result in r; r could be a or b, a and b
320 * could be equal; r is the bitwise XOR of a and b.
321 */
322int
323BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
324{
325 int i;
326 const BIGNUM *at, *bt;
327
328 bn_check_top(a);
329 bn_check_top(b);
330
331 if (a->top < b->top) {
332 at = b;
333 bt = a;
334 } else {
335 at = a;
336 bt = b;
337 }
338
339 if (bn_wexpand(r, at->top) == NULL)
340 return 0;
341
342 for (i = 0; i < bt->top; i++) {
343 r->d[i] = at->d[i] ^ bt->d[i];
344 }
345 for (; i < at->top; i++) {
346 r->d[i] = at->d[i];
347 }
348
349 r->top = at->top;
350 bn_correct_top(r);
351
352 return 1;
353}
354
355
356/* Some functions allow for representation of the irreducible polynomials
357 * as an int[], say p. The irreducible f(t) is then of the form:
358 * t^p[0] + t^p[1] + ... + t^p[k]
359 * where m = p[0] > p[1] > ... > p[k] = 0.
360 */
361
362
363/* Performs modular reduction of a and store result in r. r could be a. */
364int
365BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
366{
367 int j, k;
368 int n, dN, d0, d1;
369 BN_ULONG zz, *z;
370
371 bn_check_top(a);
372
373 if (!p[0]) {
374 /* reduction mod 1 => return 0 */
375 BN_zero(r);
376 return 1;
377 }
378
379 /* Since the algorithm does reduction in the r value, if a != r, copy
380 * the contents of a into r so we can do reduction in r.
381 */
382 if (a != r) {
383 if (!bn_wexpand(r, a->top))
384 return 0;
385 for (j = 0; j < a->top; j++) {
386 r->d[j] = a->d[j];
387 }
388 r->top = a->top;
389 }
390 z = r->d;
391
392 /* start reduction */
393 dN = p[0] / BN_BITS2;
394 for (j = r->top - 1; j > dN; ) {
395 zz = z[j];
396 if (z[j] == 0) {
397 j--;
398 continue;
399 }
400 z[j] = 0;
401
402 for (k = 1; p[k] != 0; k++) {
403 /* reducing component t^p[k] */
404 n = p[0] - p[k];
405 d0 = n % BN_BITS2;
406 d1 = BN_BITS2 - d0;
407 n /= BN_BITS2;
408 z[j - n] ^= (zz >> d0);
409 if (d0)
410 z[j - n - 1] ^= (zz << d1);
411 }
412
413 /* reducing component t^0 */
414 n = dN;
415 d0 = p[0] % BN_BITS2;
416 d1 = BN_BITS2 - d0;
417 z[j - n] ^= (zz >> d0);
418 if (d0)
419 z[j - n - 1] ^= (zz << d1);
420 }
421
422 /* final round of reduction */
423 while (j == dN) {
424
425 d0 = p[0] % BN_BITS2;
426 zz = z[dN] >> d0;
427 if (zz == 0)
428 break;
429 d1 = BN_BITS2 - d0;
430
431 /* clear up the top d1 bits */
432 if (d0)
433 z[dN] = (z[dN] << d1) >> d1;
434 else
435 z[dN] = 0;
436 z[0] ^= zz; /* reduction t^0 component */
437
438 for (k = 1; p[k] != 0; k++) {
439 BN_ULONG tmp_ulong;
440
441 /* reducing component t^p[k]*/
442 n = p[k] / BN_BITS2;
443 d0 = p[k] % BN_BITS2;
444 d1 = BN_BITS2 - d0;
445 z[n] ^= (zz << d0);
446 tmp_ulong = zz >> d1;
447 if (d0 && tmp_ulong)
448 z[n + 1] ^= tmp_ulong;
449 }
450
451
452 }
453
454 bn_correct_top(r);
455 return 1;
456}
457
458/* Performs modular reduction of a by p and store result in r. r could be a.
459 *
460 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
461 * function is only provided for convenience; for best performance, use the
462 * BN_GF2m_mod_arr function.
463 */
464int
465BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
466{
467 int ret = 0;
468 int arr[6];
469
470 bn_check_top(a);
471 bn_check_top(p);
472 ret = BN_GF2m_poly2arr(p, arr, sizeof(arr) / sizeof(arr[0]));
473 if (!ret || ret > (int)(sizeof(arr) / sizeof(arr[0]))) {
474 BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH);
475 return 0;
476 }
477 ret = BN_GF2m_mod_arr(r, a, arr);
478 bn_check_top(r);
479 return ret;
480}
481
482
483/* Compute the product of two polynomials a and b, reduce modulo p, and store
484 * the result in r. r could be a or b; a could be b.
485 */
486int
487BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[],
488 BN_CTX *ctx)
489{
490 int zlen, i, j, k, ret = 0;
491 BIGNUM *s;
492 BN_ULONG x1, x0, y1, y0, zz[4];
493
494 bn_check_top(a);
495 bn_check_top(b);
496
497 if (a == b) {
498 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
499 }
500
501 BN_CTX_start(ctx);
502 if ((s = BN_CTX_get(ctx)) == NULL)
503 goto err;
504
505 zlen = a->top + b->top + 4;
506 if (!bn_wexpand(s, zlen))
507 goto err;
508 s->top = zlen;
509
510 for (i = 0; i < zlen; i++)
511 s->d[i] = 0;
512
513 for (j = 0; j < b->top; j += 2) {
514 y0 = b->d[j];
515 y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
516 for (i = 0; i < a->top; i += 2) {
517 x0 = a->d[i];
518 x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
519 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
520 for (k = 0; k < 4; k++)
521 s->d[i + j + k] ^= zz[k];
522 }
523 }
524
525 bn_correct_top(s);
526 if (BN_GF2m_mod_arr(r, s, p))
527 ret = 1;
528 bn_check_top(r);
529
530err:
531 BN_CTX_end(ctx);
532 return ret;
533}
534
535/* Compute the product of two polynomials a and b, reduce modulo p, and store
536 * the result in r. r could be a or b; a could equal b.
537 *
538 * This function calls down to the BN_GF2m_mod_mul_arr implementation; this wrapper
539 * function is only provided for convenience; for best performance, use the
540 * BN_GF2m_mod_mul_arr function.
541 */
542int
543BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p,
544 BN_CTX *ctx)
545{
546 int ret = 0;
547 const int max = BN_num_bits(p) + 1;
548 int *arr = NULL;
549
550 bn_check_top(a);
551 bn_check_top(b);
552 bn_check_top(p);
553 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
554 goto err;
555 ret = BN_GF2m_poly2arr(p, arr, max);
556 if (!ret || ret > max) {
557 BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);
558 goto err;
559 }
560 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
561 bn_check_top(r);
562
563err:
564 free(arr);
565 return ret;
566}
567
568
569/* Square a, reduce the result mod p, and store it in a. r could be a. */
570int
571BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
572{
573 int i, ret = 0;
574 BIGNUM *s;
575
576 bn_check_top(a);
577 BN_CTX_start(ctx);
578 if ((s = BN_CTX_get(ctx)) == NULL)
579 goto err;
580 if (!bn_wexpand(s, 2 * a->top))
581 goto err;
582
583 for (i = a->top - 1; i >= 0; i--) {
584 s->d[2 * i + 1] = SQR1(a->d[i]);
585 s->d[2 * i] = SQR0(a->d[i]);
586 }
587
588 s->top = 2 * a->top;
589 bn_correct_top(s);
590 if (!BN_GF2m_mod_arr(r, s, p))
591 goto err;
592 bn_check_top(r);
593 ret = 1;
594
595err:
596 BN_CTX_end(ctx);
597 return ret;
598}
599
600/* Square a, reduce the result mod p, and store it in a. r could be a.
601 *
602 * This function calls down to the BN_GF2m_mod_sqr_arr implementation; this wrapper
603 * function is only provided for convenience; for best performance, use the
604 * BN_GF2m_mod_sqr_arr function.
605 */
606int
607BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
608{
609 int ret = 0;
610 const int max = BN_num_bits(p) + 1;
611 int *arr = NULL;
612
613 bn_check_top(a);
614 bn_check_top(p);
615 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
616 goto err;
617 ret = BN_GF2m_poly2arr(p, arr, max);
618 if (!ret || ret > max) {
619 BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);
620 goto err;
621 }
622 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
623 bn_check_top(r);
624
625err:
626 free(arr);
627 return ret;
628}
629
630
631/* Invert a, reduce modulo p, and store the result in r. r could be a.
632 * Uses Modified Almost Inverse Algorithm (Algorithm 10) from
633 * Hankerson, D., Hernandez, J.L., and Menezes, A. "Software Implementation
634 * of Elliptic Curve Cryptography Over Binary Fields".
635 */
636int
637BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
638{
639 BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
640 int ret = 0;
641
642 bn_check_top(a);
643 bn_check_top(p);
644
645 BN_CTX_start(ctx);
646
647 if ((b = BN_CTX_get(ctx)) == NULL)
648 goto err;
649 if ((c = BN_CTX_get(ctx)) == NULL)
650 goto err;
651 if ((u = BN_CTX_get(ctx)) == NULL)
652 goto err;
653 if ((v = BN_CTX_get(ctx)) == NULL)
654 goto err;
655
656 if (!BN_GF2m_mod(u, a, p))
657 goto err;
658 if (BN_is_zero(u))
659 goto err;
660
661 if (!BN_copy(v, p))
662 goto err;
663#if 0
664 if (!BN_one(b))
665 goto err;
666
667 while (1) {
668 while (!BN_is_odd(u)) {
669 if (BN_is_zero(u))
670 goto err;
671 if (!BN_rshift1(u, u))
672 goto err;
673 if (BN_is_odd(b)) {
674 if (!BN_GF2m_add(b, b, p))
675 goto err;
676 }
677 if (!BN_rshift1(b, b))
678 goto err;
679 }
680
681 if (BN_abs_is_word(u, 1))
682 break;
683
684 if (BN_num_bits(u) < BN_num_bits(v)) {
685 tmp = u;
686 u = v;
687 v = tmp;
688 tmp = b;
689 b = c;
690 c = tmp;
691 }
692
693 if (!BN_GF2m_add(u, u, v))
694 goto err;
695 if (!BN_GF2m_add(b, b, c))
696 goto err;
697 }
698#else
699 {
700 int i, ubits = BN_num_bits(u),
701 vbits = BN_num_bits(v), /* v is copy of p */
702 top = p->top;
703 BN_ULONG *udp, *bdp, *vdp, *cdp;
704
705 bn_wexpand(u, top);
706 udp = u->d;
707 for (i = u->top; i < top; i++)
708 udp[i] = 0;
709 u->top = top;
710 bn_wexpand(b, top);
711 bdp = b->d;
712 bdp[0] = 1;
713 for (i = 1; i < top; i++)
714 bdp[i] = 0;
715 b->top = top;
716 bn_wexpand(c, top);
717 cdp = c->d;
718 for (i = 0; i < top; i++)
719 cdp[i] = 0;
720 c->top = top;
721 vdp = v->d; /* It pays off to "cache" *->d pointers, because
722 * it allows optimizer to be more aggressive.
723 * But we don't have to "cache" p->d, because *p
724 * is declared 'const'... */
725 while (1) {
726 while (ubits && !(udp[0]&1)) {
727 BN_ULONG u0, u1, b0, b1, mask;
728
729 u0 = udp[0];
730 b0 = bdp[0];
731 mask = (BN_ULONG)0 - (b0 & 1);
732 b0 ^= p->d[0] & mask;
733 for (i = 0; i < top - 1; i++) {
734 u1 = udp[i + 1];
735 udp[i] = ((u0 >> 1) |
736 (u1 << (BN_BITS2 - 1))) & BN_MASK2;
737 u0 = u1;
738 b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
739 bdp[i] = ((b0 >> 1) |
740 (b1 << (BN_BITS2 - 1))) & BN_MASK2;
741 b0 = b1;
742 }
743 udp[i] = u0 >> 1;
744 bdp[i] = b0 >> 1;
745 ubits--;
746 }
747
748 if (ubits <= BN_BITS2) {
749 /* See if poly was reducible. */
750 if (udp[0] == 0)
751 goto err;
752 if (udp[0] == 1)
753 break;
754 }
755
756 if (ubits < vbits) {
757 i = ubits;
758 ubits = vbits;
759 vbits = i;
760 tmp = u;
761 u = v;
762 v = tmp;
763 tmp = b;
764 b = c;
765 c = tmp;
766 udp = vdp;
767 vdp = v->d;
768 bdp = cdp;
769 cdp = c->d;
770 }
771 for (i = 0; i < top; i++) {
772 udp[i] ^= vdp[i];
773 bdp[i] ^= cdp[i];
774 }
775 if (ubits == vbits) {
776 BN_ULONG ul;
777 int utop = (ubits - 1) / BN_BITS2;
778
779 while ((ul = udp[utop]) == 0 && utop)
780 utop--;
781 ubits = utop*BN_BITS2 + BN_num_bits_word(ul);
782 }
783 }
784 bn_correct_top(b);
785 }
786#endif
787
788 if (!BN_copy(r, b))
789 goto err;
790 bn_check_top(r);
791 ret = 1;
792
793err:
794#ifdef BN_DEBUG /* BN_CTX_end would complain about the expanded form */
795 bn_correct_top(c);
796 bn_correct_top(u);
797 bn_correct_top(v);
798#endif
799 BN_CTX_end(ctx);
800 return ret;
801}
802
803/* Invert xx, reduce modulo p, and store the result in r. r could be xx.
804 *
805 * This function calls down to the BN_GF2m_mod_inv implementation; this wrapper
806 * function is only provided for convenience; for best performance, use the
807 * BN_GF2m_mod_inv function.
808 */
809int
810BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[], BN_CTX *ctx)
811{
812 BIGNUM *field;
813 int ret = 0;
814
815 bn_check_top(xx);
816 BN_CTX_start(ctx);
817 if ((field = BN_CTX_get(ctx)) == NULL)
818 goto err;
819 if (!BN_GF2m_arr2poly(p, field))
820 goto err;
821
822 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
823 bn_check_top(r);
824
825err:
826 BN_CTX_end(ctx);
827 return ret;
828}
829
830
831#ifndef OPENSSL_SUN_GF2M_DIV
832/* Divide y by x, reduce modulo p, and store the result in r. r could be x
833 * or y, x could equal y.
834 */
835int
836BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
837 BN_CTX *ctx)
838{
839 BIGNUM *xinv = NULL;
840 int ret = 0;
841
842 bn_check_top(y);
843 bn_check_top(x);
844 bn_check_top(p);
845
846 BN_CTX_start(ctx);
847 if ((xinv = BN_CTX_get(ctx)) == NULL)
848 goto err;
849
850 if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
851 goto err;
852 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
853 goto err;
854 bn_check_top(r);
855 ret = 1;
856
857err:
858 BN_CTX_end(ctx);
859 return ret;
860}
861#else
862/* Divide y by x, reduce modulo p, and store the result in r. r could be x
863 * or y, x could equal y.
864 * Uses algorithm Modular_Division_GF(2^m) from
865 * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to
866 * the Great Divide".
867 */
868int
869BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
870 BN_CTX *ctx)
871{
872 BIGNUM *a, *b, *u, *v;
873 int ret = 0;
874
875 bn_check_top(y);
876 bn_check_top(x);
877 bn_check_top(p);
878
879 BN_CTX_start(ctx);
880
881 if ((a = BN_CTX_get(ctx)) == NULL)
882 goto err;
883 if ((b = BN_CTX_get(ctx)) == NULL)
884 goto err;
885 if ((u = BN_CTX_get(ctx)) == NULL)
886 goto err;
887 if ((v = BN_CTX_get(ctx)) == NULL)
888 goto err;
889
890 /* reduce x and y mod p */
891 if (!BN_GF2m_mod(u, y, p))
892 goto err;
893 if (!BN_GF2m_mod(a, x, p))
894 goto err;
895 if (!BN_copy(b, p))
896 goto err;
897
898 while (!BN_is_odd(a)) {
899 if (!BN_rshift1(a, a))
900 goto err;
901 if (BN_is_odd(u))
902 if (!BN_GF2m_add(u, u, p))
903 goto err;
904 if (!BN_rshift1(u, u))
905 goto err;
906 }
907
908 do {
909 if (BN_GF2m_cmp(b, a) > 0) {
910 if (!BN_GF2m_add(b, b, a))
911 goto err;
912 if (!BN_GF2m_add(v, v, u))
913 goto err;
914 do {
915 if (!BN_rshift1(b, b))
916 goto err;
917 if (BN_is_odd(v))
918 if (!BN_GF2m_add(v, v, p))
919 goto err;
920 if (!BN_rshift1(v, v))
921 goto err;
922 } while (!BN_is_odd(b));
923 } else if (BN_abs_is_word(a, 1))
924 break;
925 else {
926 if (!BN_GF2m_add(a, a, b))
927 goto err;
928 if (!BN_GF2m_add(u, u, v))
929 goto err;
930 do {
931 if (!BN_rshift1(a, a))
932 goto err;
933 if (BN_is_odd(u))
934 if (!BN_GF2m_add(u, u, p))
935 goto err;
936 if (!BN_rshift1(u, u))
937 goto err;
938 } while (!BN_is_odd(a));
939 }
940 } while (1);
941
942 if (!BN_copy(r, u))
943 goto err;
944 bn_check_top(r);
945 ret = 1;
946
947err:
948 BN_CTX_end(ctx);
949 return ret;
950}
951#endif
952
953/* Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
954 * or yy, xx could equal yy.
955 *
956 * This function calls down to the BN_GF2m_mod_div implementation; this wrapper
957 * function is only provided for convenience; for best performance, use the
958 * BN_GF2m_mod_div function.
959 */
960int
961BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
962 const int p[], BN_CTX *ctx)
963{
964 BIGNUM *field;
965 int ret = 0;
966
967 bn_check_top(yy);
968 bn_check_top(xx);
969
970 BN_CTX_start(ctx);
971 if ((field = BN_CTX_get(ctx)) == NULL)
972 goto err;
973 if (!BN_GF2m_arr2poly(p, field))
974 goto err;
975
976 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
977 bn_check_top(r);
978
979err:
980 BN_CTX_end(ctx);
981 return ret;
982}
983
984
985/* Compute the bth power of a, reduce modulo p, and store
986 * the result in r. r could be a.
987 * Uses simple square-and-multiply algorithm A.5.1 from IEEE P1363.
988 */
989int
990BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[],
991 BN_CTX *ctx)
992{
993 int ret = 0, i, n;
994 BIGNUM *u;
995
996 bn_check_top(a);
997 bn_check_top(b);
998
999 if (BN_is_zero(b))
1000 return (BN_one(r));
1001
1002 if (BN_abs_is_word(b, 1))
1003 return (BN_copy(r, a) != NULL);
1004
1005 BN_CTX_start(ctx);
1006 if ((u = BN_CTX_get(ctx)) == NULL)
1007 goto err;
1008
1009 if (!BN_GF2m_mod_arr(u, a, p))
1010 goto err;
1011
1012 n = BN_num_bits(b) - 1;
1013 for (i = n - 1; i >= 0; i--) {
1014 if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
1015 goto err;
1016 if (BN_is_bit_set(b, i)) {
1017 if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
1018 goto err;
1019 }
1020 }
1021 if (!BN_copy(r, u))
1022 goto err;
1023 bn_check_top(r);
1024 ret = 1;
1025
1026err:
1027 BN_CTX_end(ctx);
1028 return ret;
1029}
1030
1031/* Compute the bth power of a, reduce modulo p, and store
1032 * the result in r. r could be a.
1033 *
1034 * This function calls down to the BN_GF2m_mod_exp_arr implementation; this wrapper
1035 * function is only provided for convenience; for best performance, use the
1036 * BN_GF2m_mod_exp_arr function.
1037 */
1038int
1039BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p,
1040 BN_CTX *ctx)
1041{
1042 int ret = 0;
1043 const int max = BN_num_bits(p) + 1;
1044 int *arr = NULL;
1045
1046 bn_check_top(a);
1047 bn_check_top(b);
1048 bn_check_top(p);
1049 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
1050 goto err;
1051 ret = BN_GF2m_poly2arr(p, arr, max);
1052 if (!ret || ret > max) {
1053 BNerr(BN_F_BN_GF2M_MOD_EXP, BN_R_INVALID_LENGTH);
1054 goto err;
1055 }
1056 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
1057 bn_check_top(r);
1058
1059err:
1060 free(arr);
1061 return ret;
1062}
1063
1064/* Compute the square root of a, reduce modulo p, and store
1065 * the result in r. r could be a.
1066 * Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
1067 */
1068int
1069BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
1070{
1071 int ret = 0;
1072 BIGNUM *u;
1073
1074 bn_check_top(a);
1075
1076 if (!p[0]) {
1077 /* reduction mod 1 => return 0 */
1078 BN_zero(r);
1079 return 1;
1080 }
1081
1082 BN_CTX_start(ctx);
1083 if ((u = BN_CTX_get(ctx)) == NULL)
1084 goto err;
1085
1086 if (!BN_set_bit(u, p[0] - 1))
1087 goto err;
1088 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
1089 bn_check_top(r);
1090
1091err:
1092 BN_CTX_end(ctx);
1093 return ret;
1094}
1095
1096/* Compute the square root of a, reduce modulo p, and store
1097 * the result in r. r could be a.
1098 *
1099 * This function calls down to the BN_GF2m_mod_sqrt_arr implementation; this wrapper
1100 * function is only provided for convenience; for best performance, use the
1101 * BN_GF2m_mod_sqrt_arr function.
1102 */
1103int
1104BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1105{
1106 int ret = 0;
1107 const int max = BN_num_bits(p) + 1;
1108 int *arr = NULL;
1109 bn_check_top(a);
1110 bn_check_top(p);
1111 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
1112 goto err;
1113 ret = BN_GF2m_poly2arr(p, arr, max);
1114 if (!ret || ret > max) {
1115 BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);
1116 goto err;
1117 }
1118 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
1119 bn_check_top(r);
1120
1121err:
1122 free(arr);
1123 return ret;
1124}
1125
1126/* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0.
1127 * Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
1128 */
1129int
1130BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1131 BN_CTX *ctx)
1132{
1133 int ret = 0, count = 0, j;
1134 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1135
1136 bn_check_top(a_);
1137
1138 if (!p[0]) {
1139 /* reduction mod 1 => return 0 */
1140 BN_zero(r);
1141 return 1;
1142 }
1143
1144 BN_CTX_start(ctx);
1145 if ((a = BN_CTX_get(ctx)) == NULL)
1146 goto err;
1147 if ((z = BN_CTX_get(ctx)) == NULL)
1148 goto err;
1149 if ((w = BN_CTX_get(ctx)) == NULL)
1150 goto err;
1151
1152 if (!BN_GF2m_mod_arr(a, a_, p))
1153 goto err;
1154
1155 if (BN_is_zero(a)) {
1156 BN_zero(r);
1157 ret = 1;
1158 goto err;
1159 }
1160
1161 if (p[0] & 0x1) /* m is odd */
1162 {
1163 /* compute half-trace of a */
1164 if (!BN_copy(z, a))
1165 goto err;
1166 for (j = 1; j <= (p[0] - 1) / 2; j++) {
1167 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1168 goto err;
1169 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1170 goto err;
1171 if (!BN_GF2m_add(z, z, a))
1172 goto err;
1173 }
1174
1175 }
1176 else /* m is even */
1177 {
1178 if ((rho = BN_CTX_get(ctx)) == NULL)
1179 goto err;
1180 if ((w2 = BN_CTX_get(ctx)) == NULL)
1181 goto err;
1182 if ((tmp = BN_CTX_get(ctx)) == NULL)
1183 goto err;
1184 do {
1185 if (!BN_rand(rho, p[0], 0, 0))
1186 goto err;
1187 if (!BN_GF2m_mod_arr(rho, rho, p))
1188 goto err;
1189 BN_zero(z);
1190 if (!BN_copy(w, rho))
1191 goto err;
1192 for (j = 1; j <= p[0] - 1; j++) {
1193 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1194 goto err;
1195 if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1196 goto err;
1197 if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1198 goto err;
1199 if (!BN_GF2m_add(z, z, tmp))
1200 goto err;
1201 if (!BN_GF2m_add(w, w2, rho))
1202 goto err;
1203 }
1204 count++;
1205 } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1206 if (BN_is_zero(w)) {
1207 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR,
1208 BN_R_TOO_MANY_ITERATIONS);
1209 goto err;
1210 }
1211 }
1212
1213 if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1214 goto err;
1215 if (!BN_GF2m_add(w, z, w))
1216 goto err;
1217 if (BN_GF2m_cmp(w, a)) {
1218 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
1219 goto err;
1220 }
1221
1222 if (!BN_copy(r, z))
1223 goto err;
1224 bn_check_top(r);
1225
1226 ret = 1;
1227
1228err:
1229 BN_CTX_end(ctx);
1230 return ret;
1231}
1232
1233/* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0.
1234 *
1235 * This function calls down to the BN_GF2m_mod_solve_quad_arr implementation; this wrapper
1236 * function is only provided for convenience; for best performance, use the
1237 * BN_GF2m_mod_solve_quad_arr function.
1238 */
1239int
1240BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1241{
1242 int ret = 0;
1243 const int max = BN_num_bits(p) + 1;
1244 int *arr = NULL;
1245
1246 bn_check_top(a);
1247 bn_check_top(p);
1248 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
1249 goto err;
1250 ret = BN_GF2m_poly2arr(p, arr, max);
1251 if (!ret || ret > max) {
1252 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);
1253 goto err;
1254 }
1255 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1256 bn_check_top(r);
1257
1258err:
1259 free(arr);
1260 return ret;
1261}
1262
1263/* Convert the bit-string representation of a polynomial
1264 * ( \sum_{i=0}^n a_i * x^i) into an array of integers corresponding
1265 * to the bits with non-zero coefficient. Array is terminated with -1.
1266 * Up to max elements of the array will be filled. Return value is total
1267 * number of array elements that would be filled if array was large enough.
1268 */
1269int
1270BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1271{
1272 int i, j, k = 0;
1273 BN_ULONG mask;
1274
1275 if (BN_is_zero(a))
1276 return 0;
1277
1278 for (i = a->top - 1; i >= 0; i--) {
1279 if (!a->d[i])
1280 /* skip word if a->d[i] == 0 */
1281 continue;
1282 mask = BN_TBIT;
1283 for (j = BN_BITS2 - 1; j >= 0; j--) {
1284 if (a->d[i] & mask) {
1285 if (k < max)
1286 p[k] = BN_BITS2 * i + j;
1287 k++;
1288 }
1289 mask >>= 1;
1290 }
1291 }
1292
1293 if (k < max) {
1294 p[k] = -1;
1295 k++;
1296 }
1297
1298 return k;
1299}
1300
1301/* Convert the coefficient array representation of a polynomial to a
1302 * bit-string. The array must be terminated by -1.
1303 */
1304int
1305BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1306{
1307 int i;
1308
1309 bn_check_top(a);
1310 BN_zero(a);
1311 for (i = 0; p[i] != -1; i++) {
1312 if (BN_set_bit(a, p[i]) == 0)
1313 return 0;
1314 }
1315 bn_check_top(a);
1316
1317 return 1;
1318}
1319
1320#endif