diff options
| author | tb <> | 2023-04-25 19:53:30 +0000 |
|---|---|---|
| committer | tb <> | 2023-04-25 19:53:30 +0000 |
| commit | 0725c451bef02fc23e8b5bafa353df9cd02984b7 (patch) | |
| tree | e9be44ea96c7294efcc800d9cb419edbab4fe999 /src/lib/libcrypto/bn | |
| parent | ae6c86218c35619cec69850fd313bea6175da6dc (diff) | |
| download | openbsd-0725c451bef02fc23e8b5bafa353df9cd02984b7.tar.gz openbsd-0725c451bef02fc23e8b5bafa353df9cd02984b7.tar.bz2 openbsd-0725c451bef02fc23e8b5bafa353df9cd02984b7.zip | |
GF2m bites the dust. It won't be missed.
Diffstat (limited to 'src/lib/libcrypto/bn')
| -rw-r--r-- | src/lib/libcrypto/bn/bn.h | 63 | ||||
| -rw-r--r-- | src/lib/libcrypto/bn/bn_gf2m.c | 1268 |
2 files changed, 1 insertions, 1330 deletions
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h index 52e3d078ab..b15e6311f9 100644 --- a/src/lib/libcrypto/bn/bn.h +++ b/src/lib/libcrypto/bn/bn.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn.h,v 1.68 2023/04/25 17:42:07 tb Exp $ */ | 1 | /* $OpenBSD: bn.h,v 1.69 2023/04/25 19:53:30 tb Exp $ */ |
| 2 | /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -505,67 +505,6 @@ void BN_set_params(int mul, int high, int low, int mont); | |||
| 505 | int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ | 505 | int BN_get_params(int which); /* 0, mul, 1 high, 2 low, 3 mont */ |
| 506 | #endif | 506 | #endif |
| 507 | 507 | ||
| 508 | #ifndef OPENSSL_NO_EC2M | ||
| 509 | |||
| 510 | /* Functions for arithmetic over binary polynomials represented by BIGNUMs. | ||
| 511 | * | ||
| 512 | * The BIGNUM::neg property of BIGNUMs representing binary polynomials is | ||
| 513 | * ignored. | ||
| 514 | * | ||
| 515 | * Note that input arguments are not const so that their bit arrays can | ||
| 516 | * be expanded to the appropriate size if needed. | ||
| 517 | */ | ||
| 518 | |||
| 519 | int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); /*r = a + b*/ | ||
| 520 | #define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b) | ||
| 521 | int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p); /*r=a mod p*/ | ||
| 522 | int | ||
| 523 | BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
| 524 | const BIGNUM *p, BN_CTX *ctx); /* r = (a * b) mod p */ | ||
| 525 | int | ||
| 526 | BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 527 | BN_CTX *ctx); /* r = (a * a) mod p */ | ||
| 528 | int | ||
| 529 | BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, | ||
| 530 | BN_CTX *ctx); /* r = (1 / b) mod p */ | ||
| 531 | int | ||
| 532 | BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
| 533 | const BIGNUM *p, BN_CTX *ctx); /* r = (a / b) mod p */ | ||
| 534 | int | ||
| 535 | BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
| 536 | const BIGNUM *p, BN_CTX *ctx); /* r = (a ^ b) mod p */ | ||
| 537 | int | ||
| 538 | BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 539 | BN_CTX *ctx); /* r = sqrt(a) mod p */ | ||
| 540 | int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 541 | BN_CTX *ctx); /* r^2 + r = a mod p */ | ||
| 542 | #define BN_GF2m_cmp(a, b) BN_ucmp((a), (b)) | ||
| 543 | /* Some functions allow for representation of the irreducible polynomials | ||
| 544 | * as an unsigned int[], say p. The irreducible f(t) is then of the form: | ||
| 545 | * t^p[0] + t^p[1] + ... + t^p[k] | ||
| 546 | * where m = p[0] > p[1] > ... > p[k] = 0. | ||
| 547 | */ | ||
| 548 | int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]); | ||
| 549 | /* r = a mod p */ | ||
| 550 | int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
| 551 | const int p[], BN_CTX *ctx); /* r = (a * b) mod p */ | ||
| 552 | int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], | ||
| 553 | BN_CTX *ctx); /* r = (a * a) mod p */ | ||
| 554 | int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[], | ||
| 555 | BN_CTX *ctx); /* r = (1 / b) mod p */ | ||
| 556 | int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
| 557 | const int p[], BN_CTX *ctx); /* r = (a / b) mod p */ | ||
| 558 | int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | ||
| 559 | const int p[], BN_CTX *ctx); /* r = (a ^ b) mod p */ | ||
| 560 | int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, | ||
| 561 | const int p[], BN_CTX *ctx); /* r = sqrt(a) mod p */ | ||
| 562 | int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a, | ||
| 563 | const int p[], BN_CTX *ctx); /* r^2 + r = a mod p */ | ||
| 564 | int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max); | ||
| 565 | int BN_GF2m_arr2poly(const int p[], BIGNUM *a); | ||
| 566 | |||
| 567 | #endif | ||
| 568 | |||
| 569 | /* Primes from RFC 2409 */ | 508 | /* Primes from RFC 2409 */ |
| 570 | BIGNUM *get_rfc2409_prime_768(BIGNUM *bn); | 509 | BIGNUM *get_rfc2409_prime_768(BIGNUM *bn); |
| 571 | BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn); | 510 | BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn); |
diff --git a/src/lib/libcrypto/bn/bn_gf2m.c b/src/lib/libcrypto/bn/bn_gf2m.c deleted file mode 100644 index 62ac2a5151..0000000000 --- a/src/lib/libcrypto/bn/bn_gf2m.c +++ /dev/null | |||
| @@ -1,1268 +0,0 @@ | |||
| 1 | /* $OpenBSD: bn_gf2m.c,v 1.32 2023/03/27 10:25:02 tb 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_local.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 | |||
| 105 | static const BN_ULONG SQR_tb[16] = | ||
| 106 | { 0, 1, 4, 5, 16, 17, 20, 21, | ||
| 107 | 64, 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 | */ | ||
| 135 | static void | ||
| 136 | bn_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 | */ | ||
| 300 | static void | ||
| 301 | bn_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 | ||
| 315 | void 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 | */ | ||
| 322 | int | ||
| 323 | BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) | ||
| 324 | { | ||
| 325 | int i; | ||
| 326 | const BIGNUM *at, *bt; | ||
| 327 | |||
| 328 | |||
| 329 | if (a->top < b->top) { | ||
| 330 | at = b; | ||
| 331 | bt = a; | ||
| 332 | } else { | ||
| 333 | at = a; | ||
| 334 | bt = b; | ||
| 335 | } | ||
| 336 | |||
| 337 | if (!bn_wexpand(r, at->top)) | ||
| 338 | return 0; | ||
| 339 | |||
| 340 | for (i = 0; i < bt->top; i++) { | ||
| 341 | r->d[i] = at->d[i] ^ bt->d[i]; | ||
| 342 | } | ||
| 343 | for (; i < at->top; i++) { | ||
| 344 | r->d[i] = at->d[i]; | ||
| 345 | } | ||
| 346 | |||
| 347 | r->top = at->top; | ||
| 348 | bn_correct_top(r); | ||
| 349 | |||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | |||
| 354 | /* Some functions allow for representation of the irreducible polynomials | ||
| 355 | * as an int[], say p. The irreducible f(t) is then of the form: | ||
| 356 | * t^p[0] + t^p[1] + ... + t^p[k] | ||
| 357 | * where m = p[0] > p[1] > ... > p[k] = 0. | ||
| 358 | */ | ||
| 359 | |||
| 360 | |||
| 361 | /* Performs modular reduction of a and store result in r. r could be a. */ | ||
| 362 | int | ||
| 363 | BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]) | ||
| 364 | { | ||
| 365 | int j, k; | ||
| 366 | int n, dN, d0, d1; | ||
| 367 | BN_ULONG zz, *z; | ||
| 368 | |||
| 369 | |||
| 370 | if (!p[0]) { | ||
| 371 | /* reduction mod 1 => return 0 */ | ||
| 372 | BN_zero(r); | ||
| 373 | return 1; | ||
| 374 | } | ||
| 375 | |||
| 376 | /* Since the algorithm does reduction in the r value, if a != r, copy | ||
| 377 | * the contents of a into r so we can do reduction in r. | ||
| 378 | */ | ||
| 379 | if (a != r) { | ||
| 380 | if (!bn_wexpand(r, a->top)) | ||
| 381 | return 0; | ||
| 382 | for (j = 0; j < a->top; j++) { | ||
| 383 | r->d[j] = a->d[j]; | ||
| 384 | } | ||
| 385 | r->top = a->top; | ||
| 386 | } | ||
| 387 | z = r->d; | ||
| 388 | |||
| 389 | /* start reduction */ | ||
| 390 | dN = p[0] / BN_BITS2; | ||
| 391 | for (j = r->top - 1; j > dN; ) { | ||
| 392 | zz = z[j]; | ||
| 393 | if (z[j] == 0) { | ||
| 394 | j--; | ||
| 395 | continue; | ||
| 396 | } | ||
| 397 | z[j] = 0; | ||
| 398 | |||
| 399 | for (k = 1; p[k] != 0; k++) { | ||
| 400 | /* reducing component t^p[k] */ | ||
| 401 | n = p[0] - p[k]; | ||
| 402 | d0 = n % BN_BITS2; | ||
| 403 | d1 = BN_BITS2 - d0; | ||
| 404 | n /= BN_BITS2; | ||
| 405 | z[j - n] ^= (zz >> d0); | ||
| 406 | if (d0) | ||
| 407 | z[j - n - 1] ^= (zz << d1); | ||
| 408 | } | ||
| 409 | |||
| 410 | /* reducing component t^0 */ | ||
| 411 | n = dN; | ||
| 412 | d0 = p[0] % BN_BITS2; | ||
| 413 | d1 = BN_BITS2 - d0; | ||
| 414 | z[j - n] ^= (zz >> d0); | ||
| 415 | if (d0) | ||
| 416 | z[j - n - 1] ^= (zz << d1); | ||
| 417 | } | ||
| 418 | |||
| 419 | /* final round of reduction */ | ||
| 420 | while (j == dN) { | ||
| 421 | |||
| 422 | d0 = p[0] % BN_BITS2; | ||
| 423 | zz = z[dN] >> d0; | ||
| 424 | if (zz == 0) | ||
| 425 | break; | ||
| 426 | d1 = BN_BITS2 - d0; | ||
| 427 | |||
| 428 | /* clear up the top d1 bits */ | ||
| 429 | if (d0) | ||
| 430 | z[dN] = (z[dN] << d1) >> d1; | ||
| 431 | else | ||
| 432 | z[dN] = 0; | ||
| 433 | z[0] ^= zz; /* reduction t^0 component */ | ||
| 434 | |||
| 435 | for (k = 1; p[k] != 0; k++) { | ||
| 436 | BN_ULONG tmp_ulong; | ||
| 437 | |||
| 438 | /* reducing component t^p[k]*/ | ||
| 439 | n = p[k] / BN_BITS2; | ||
| 440 | d0 = p[k] % BN_BITS2; | ||
| 441 | d1 = BN_BITS2 - d0; | ||
| 442 | z[n] ^= (zz << d0); | ||
| 443 | if (d0 && (tmp_ulong = zz >> d1)) | ||
| 444 | z[n + 1] ^= tmp_ulong; | ||
| 445 | } | ||
| 446 | |||
| 447 | |||
| 448 | } | ||
| 449 | |||
| 450 | bn_correct_top(r); | ||
| 451 | return 1; | ||
| 452 | } | ||
| 453 | |||
| 454 | /* Performs modular reduction of a by p and store result in r. r could be a. | ||
| 455 | * | ||
| 456 | * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper | ||
| 457 | * function is only provided for convenience; for best performance, use the | ||
| 458 | * BN_GF2m_mod_arr function. | ||
| 459 | */ | ||
| 460 | int | ||
| 461 | BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p) | ||
| 462 | { | ||
| 463 | int ret = 0; | ||
| 464 | const int max = BN_num_bits(p) + 1; | ||
| 465 | int *arr = NULL; | ||
| 466 | |||
| 467 | if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) | ||
| 468 | goto err; | ||
| 469 | ret = BN_GF2m_poly2arr(p, arr, max); | ||
| 470 | if (!ret || ret > max) { | ||
| 471 | BNerror(BN_R_INVALID_LENGTH); | ||
| 472 | goto err; | ||
| 473 | } | ||
| 474 | ret = BN_GF2m_mod_arr(r, a, arr); | ||
| 475 | |||
| 476 | err: | ||
| 477 | free(arr); | ||
| 478 | return ret; | ||
| 479 | } | ||
| 480 | |||
| 481 | |||
| 482 | /* Compute the product of two polynomials a and b, reduce modulo p, and store | ||
| 483 | * the result in r. r could be a or b; a could be b. | ||
| 484 | */ | ||
| 485 | int | ||
| 486 | BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[], | ||
| 487 | BN_CTX *ctx) | ||
| 488 | { | ||
| 489 | int zlen, i, j, k, ret = 0; | ||
| 490 | BIGNUM *s; | ||
| 491 | BN_ULONG x1, x0, y1, y0, zz[4]; | ||
| 492 | |||
| 493 | |||
| 494 | if (a == b) { | ||
| 495 | return BN_GF2m_mod_sqr_arr(r, a, p, ctx); | ||
| 496 | } | ||
| 497 | |||
| 498 | BN_CTX_start(ctx); | ||
| 499 | if ((s = BN_CTX_get(ctx)) == NULL) | ||
| 500 | goto err; | ||
| 501 | |||
| 502 | zlen = a->top + b->top + 4; | ||
| 503 | if (!bn_wexpand(s, zlen)) | ||
| 504 | goto err; | ||
| 505 | s->top = zlen; | ||
| 506 | |||
| 507 | for (i = 0; i < zlen; i++) | ||
| 508 | s->d[i] = 0; | ||
| 509 | |||
| 510 | for (j = 0; j < b->top; j += 2) { | ||
| 511 | y0 = b->d[j]; | ||
| 512 | y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1]; | ||
| 513 | for (i = 0; i < a->top; i += 2) { | ||
| 514 | x0 = a->d[i]; | ||
| 515 | x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1]; | ||
| 516 | bn_GF2m_mul_2x2(zz, x1, x0, y1, y0); | ||
| 517 | for (k = 0; k < 4; k++) | ||
| 518 | s->d[i + j + k] ^= zz[k]; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | bn_correct_top(s); | ||
| 523 | if (BN_GF2m_mod_arr(r, s, p)) | ||
| 524 | ret = 1; | ||
| 525 | |||
| 526 | err: | ||
| 527 | BN_CTX_end(ctx); | ||
| 528 | return ret; | ||
| 529 | } | ||
| 530 | |||
| 531 | /* Compute the product of two polynomials a and b, reduce modulo p, and store | ||
| 532 | * the result in r. r could be a or b; a could equal b. | ||
| 533 | * | ||
| 534 | * This function calls down to the BN_GF2m_mod_mul_arr implementation; this wrapper | ||
| 535 | * function is only provided for convenience; for best performance, use the | ||
| 536 | * BN_GF2m_mod_mul_arr function. | ||
| 537 | */ | ||
| 538 | int | ||
| 539 | BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, | ||
| 540 | BN_CTX *ctx) | ||
| 541 | { | ||
| 542 | int ret = 0; | ||
| 543 | const int max = BN_num_bits(p) + 1; | ||
| 544 | int *arr = NULL; | ||
| 545 | |||
| 546 | if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) | ||
| 547 | goto err; | ||
| 548 | ret = BN_GF2m_poly2arr(p, arr, max); | ||
| 549 | if (!ret || ret > max) { | ||
| 550 | BNerror(BN_R_INVALID_LENGTH); | ||
| 551 | goto err; | ||
| 552 | } | ||
| 553 | ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx); | ||
| 554 | |||
| 555 | err: | ||
| 556 | free(arr); | ||
| 557 | return ret; | ||
| 558 | } | ||
| 559 | |||
| 560 | |||
| 561 | /* Square a, reduce the result mod p, and store it in a. r could be a. */ | ||
| 562 | int | ||
| 563 | BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx) | ||
| 564 | { | ||
| 565 | int i, ret = 0; | ||
| 566 | BIGNUM *s; | ||
| 567 | |||
| 568 | BN_CTX_start(ctx); | ||
| 569 | if ((s = BN_CTX_get(ctx)) == NULL) | ||
| 570 | goto err; | ||
| 571 | if (!bn_wexpand(s, 2 * a->top)) | ||
| 572 | goto err; | ||
| 573 | |||
| 574 | for (i = a->top - 1; i >= 0; i--) { | ||
| 575 | s->d[2 * i + 1] = SQR1(a->d[i]); | ||
| 576 | s->d[2 * i] = SQR0(a->d[i]); | ||
| 577 | } | ||
| 578 | |||
| 579 | s->top = 2 * a->top; | ||
| 580 | bn_correct_top(s); | ||
| 581 | if (!BN_GF2m_mod_arr(r, s, p)) | ||
| 582 | goto err; | ||
| 583 | ret = 1; | ||
| 584 | |||
| 585 | err: | ||
| 586 | BN_CTX_end(ctx); | ||
| 587 | return ret; | ||
| 588 | } | ||
| 589 | |||
| 590 | /* Square a, reduce the result mod p, and store it in a. r could be a. | ||
| 591 | * | ||
| 592 | * This function calls down to the BN_GF2m_mod_sqr_arr implementation; this wrapper | ||
| 593 | * function is only provided for convenience; for best performance, use the | ||
| 594 | * BN_GF2m_mod_sqr_arr function. | ||
| 595 | */ | ||
| 596 | int | ||
| 597 | BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | ||
| 598 | { | ||
| 599 | int ret = 0; | ||
| 600 | const int max = BN_num_bits(p) + 1; | ||
| 601 | int *arr = NULL; | ||
| 602 | |||
| 603 | if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) | ||
| 604 | goto err; | ||
| 605 | ret = BN_GF2m_poly2arr(p, arr, max); | ||
| 606 | if (!ret || ret > max) { | ||
| 607 | BNerror(BN_R_INVALID_LENGTH); | ||
| 608 | goto err; | ||
| 609 | } | ||
| 610 | ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx); | ||
| 611 | |||
| 612 | err: | ||
| 613 | free(arr); | ||
| 614 | return ret; | ||
| 615 | } | ||
| 616 | |||
| 617 | |||
| 618 | /* Invert a, reduce modulo p, and store the result in r. r could be a. | ||
| 619 | * Uses Modified Almost Inverse Algorithm (Algorithm 10) from | ||
| 620 | * Hankerson, D., Hernandez, J.L., and Menezes, A. "Software Implementation | ||
| 621 | * of Elliptic Curve Cryptography Over Binary Fields". | ||
| 622 | */ | ||
| 623 | int | ||
| 624 | BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | ||
| 625 | { | ||
| 626 | BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp; | ||
| 627 | int ret = 0; | ||
| 628 | |||
| 629 | |||
| 630 | BN_CTX_start(ctx); | ||
| 631 | |||
| 632 | if ((b = BN_CTX_get(ctx)) == NULL) | ||
| 633 | goto err; | ||
| 634 | if ((c = BN_CTX_get(ctx)) == NULL) | ||
| 635 | goto err; | ||
| 636 | if ((u = BN_CTX_get(ctx)) == NULL) | ||
| 637 | goto err; | ||
| 638 | if ((v = BN_CTX_get(ctx)) == NULL) | ||
| 639 | goto err; | ||
| 640 | |||
| 641 | if (!BN_GF2m_mod(u, a, p)) | ||
| 642 | goto err; | ||
| 643 | if (BN_is_zero(u)) | ||
| 644 | goto err; | ||
| 645 | |||
| 646 | if (!bn_copy(v, p)) | ||
| 647 | goto err; | ||
| 648 | #if 0 | ||
| 649 | if (!BN_one(b)) | ||
| 650 | goto err; | ||
| 651 | |||
| 652 | while (1) { | ||
| 653 | while (!BN_is_odd(u)) { | ||
| 654 | if (BN_is_zero(u)) | ||
| 655 | goto err; | ||
| 656 | if (!BN_rshift1(u, u)) | ||
| 657 | goto err; | ||
| 658 | if (BN_is_odd(b)) { | ||
| 659 | if (!BN_GF2m_add(b, b, p)) | ||
| 660 | goto err; | ||
| 661 | } | ||
| 662 | if (!BN_rshift1(b, b)) | ||
| 663 | goto err; | ||
| 664 | } | ||
| 665 | |||
| 666 | if (BN_abs_is_word(u, 1)) | ||
| 667 | break; | ||
| 668 | |||
| 669 | if (BN_num_bits(u) < BN_num_bits(v)) { | ||
| 670 | tmp = u; | ||
| 671 | u = v; | ||
| 672 | v = tmp; | ||
| 673 | tmp = b; | ||
| 674 | b = c; | ||
| 675 | c = tmp; | ||
| 676 | } | ||
| 677 | |||
| 678 | if (!BN_GF2m_add(u, u, v)) | ||
| 679 | goto err; | ||
| 680 | if (!BN_GF2m_add(b, b, c)) | ||
| 681 | goto err; | ||
| 682 | } | ||
| 683 | #else | ||
| 684 | { | ||
| 685 | int i, ubits = BN_num_bits(u), | ||
| 686 | vbits = BN_num_bits(v), /* v is copy of p */ | ||
| 687 | top = p->top; | ||
| 688 | BN_ULONG *udp, *bdp, *vdp, *cdp; | ||
| 689 | |||
| 690 | if (!bn_wexpand(u, top)) | ||
| 691 | goto err; | ||
| 692 | udp = u->d; | ||
| 693 | for (i = u->top; i < top; i++) | ||
| 694 | udp[i] = 0; | ||
| 695 | u->top = top; | ||
| 696 | if (!bn_wexpand(b, top)) | ||
| 697 | goto err; | ||
| 698 | bdp = b->d; | ||
| 699 | bdp[0] = 1; | ||
| 700 | for (i = 1; i < top; i++) | ||
| 701 | bdp[i] = 0; | ||
| 702 | b->top = top; | ||
| 703 | if (!bn_wexpand(c, top)) | ||
| 704 | goto err; | ||
| 705 | cdp = c->d; | ||
| 706 | for (i = 0; i < top; i++) | ||
| 707 | cdp[i] = 0; | ||
| 708 | c->top = top; | ||
| 709 | vdp = v->d; /* It pays off to "cache" *->d pointers, because | ||
| 710 | * it allows optimizer to be more aggressive. | ||
| 711 | * But we don't have to "cache" p->d, because *p | ||
| 712 | * is declared 'const'... */ | ||
| 713 | while (1) { | ||
| 714 | while (ubits && !(udp[0]&1)) { | ||
| 715 | BN_ULONG u0, u1, b0, b1, mask; | ||
| 716 | |||
| 717 | u0 = udp[0]; | ||
| 718 | b0 = bdp[0]; | ||
| 719 | mask = (BN_ULONG)0 - (b0 & 1); | ||
| 720 | b0 ^= p->d[0] & mask; | ||
| 721 | for (i = 0; i < top - 1; i++) { | ||
| 722 | u1 = udp[i + 1]; | ||
| 723 | udp[i] = ((u0 >> 1) | | ||
| 724 | (u1 << (BN_BITS2 - 1))) & BN_MASK2; | ||
| 725 | u0 = u1; | ||
| 726 | b1 = bdp[i + 1] ^ (p->d[i + 1] & mask); | ||
| 727 | bdp[i] = ((b0 >> 1) | | ||
| 728 | (b1 << (BN_BITS2 - 1))) & BN_MASK2; | ||
| 729 | b0 = b1; | ||
| 730 | } | ||
| 731 | udp[i] = u0 >> 1; | ||
| 732 | bdp[i] = b0 >> 1; | ||
| 733 | ubits--; | ||
| 734 | } | ||
| 735 | |||
| 736 | if (ubits <= BN_BITS2) { | ||
| 737 | /* See if poly was reducible. */ | ||
| 738 | if (udp[0] == 0) | ||
| 739 | goto err; | ||
| 740 | if (udp[0] == 1) | ||
| 741 | break; | ||
| 742 | } | ||
| 743 | |||
| 744 | if (ubits < vbits) { | ||
| 745 | i = ubits; | ||
| 746 | ubits = vbits; | ||
| 747 | vbits = i; | ||
| 748 | tmp = u; | ||
| 749 | u = v; | ||
| 750 | v = tmp; | ||
| 751 | tmp = b; | ||
| 752 | b = c; | ||
| 753 | c = tmp; | ||
| 754 | udp = vdp; | ||
| 755 | vdp = v->d; | ||
| 756 | bdp = cdp; | ||
| 757 | cdp = c->d; | ||
| 758 | } | ||
| 759 | for (i = 0; i < top; i++) { | ||
| 760 | udp[i] ^= vdp[i]; | ||
| 761 | bdp[i] ^= cdp[i]; | ||
| 762 | } | ||
| 763 | if (ubits == vbits) { | ||
| 764 | BN_ULONG ul; | ||
| 765 | int utop = (ubits - 1) / BN_BITS2; | ||
| 766 | |||
| 767 | while ((ul = udp[utop]) == 0 && utop) | ||
| 768 | utop--; | ||
| 769 | ubits = utop*BN_BITS2 + BN_num_bits_word(ul); | ||
| 770 | } | ||
| 771 | } | ||
| 772 | bn_correct_top(b); | ||
| 773 | } | ||
| 774 | #endif | ||
| 775 | |||
| 776 | if (!bn_copy(r, b)) | ||
| 777 | goto err; | ||
| 778 | ret = 1; | ||
| 779 | |||
| 780 | err: | ||
| 781 | BN_CTX_end(ctx); | ||
| 782 | return ret; | ||
| 783 | } | ||
| 784 | |||
| 785 | /* Invert xx, reduce modulo p, and store the result in r. r could be xx. | ||
| 786 | * | ||
| 787 | * This function calls down to the BN_GF2m_mod_inv implementation; this wrapper | ||
| 788 | * function is only provided for convenience; for best performance, use the | ||
| 789 | * BN_GF2m_mod_inv function. | ||
| 790 | */ | ||
| 791 | int | ||
| 792 | BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[], BN_CTX *ctx) | ||
| 793 | { | ||
| 794 | BIGNUM *field; | ||
| 795 | int ret = 0; | ||
| 796 | |||
| 797 | BN_CTX_start(ctx); | ||
| 798 | if ((field = BN_CTX_get(ctx)) == NULL) | ||
| 799 | goto err; | ||
| 800 | if (!BN_GF2m_arr2poly(p, field)) | ||
| 801 | goto err; | ||
| 802 | |||
| 803 | ret = BN_GF2m_mod_inv(r, xx, field, ctx); | ||
| 804 | |||
| 805 | err: | ||
| 806 | BN_CTX_end(ctx); | ||
| 807 | return ret; | ||
| 808 | } | ||
| 809 | |||
| 810 | |||
| 811 | #ifndef OPENSSL_SUN_GF2M_DIV | ||
| 812 | /* Divide y by x, reduce modulo p, and store the result in r. r could be x | ||
| 813 | * or y, x could equal y. | ||
| 814 | */ | ||
| 815 | int | ||
| 816 | BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, | ||
| 817 | BN_CTX *ctx) | ||
| 818 | { | ||
| 819 | BIGNUM *xinv = NULL; | ||
| 820 | int ret = 0; | ||
| 821 | |||
| 822 | |||
| 823 | BN_CTX_start(ctx); | ||
| 824 | if ((xinv = BN_CTX_get(ctx)) == NULL) | ||
| 825 | goto err; | ||
| 826 | |||
| 827 | if (!BN_GF2m_mod_inv(xinv, x, p, ctx)) | ||
| 828 | goto err; | ||
| 829 | if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx)) | ||
| 830 | goto err; | ||
| 831 | ret = 1; | ||
| 832 | |||
| 833 | err: | ||
| 834 | BN_CTX_end(ctx); | ||
| 835 | return ret; | ||
| 836 | } | ||
| 837 | #else | ||
| 838 | /* Divide y by x, reduce modulo p, and store the result in r. r could be x | ||
| 839 | * or y, x could equal y. | ||
| 840 | * Uses algorithm Modular_Division_GF(2^m) from | ||
| 841 | * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to | ||
| 842 | * the Great Divide". | ||
| 843 | */ | ||
| 844 | int | ||
| 845 | BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, | ||
| 846 | BN_CTX *ctx) | ||
| 847 | { | ||
| 848 | BIGNUM *a, *b, *u, *v; | ||
| 849 | int ret = 0; | ||
| 850 | |||
| 851 | |||
| 852 | BN_CTX_start(ctx); | ||
| 853 | |||
| 854 | if ((a = BN_CTX_get(ctx)) == NULL) | ||
| 855 | goto err; | ||
| 856 | if ((b = BN_CTX_get(ctx)) == NULL) | ||
| 857 | goto err; | ||
| 858 | if ((u = BN_CTX_get(ctx)) == NULL) | ||
| 859 | goto err; | ||
| 860 | if ((v = BN_CTX_get(ctx)) == NULL) | ||
| 861 | goto err; | ||
| 862 | |||
| 863 | /* reduce x and y mod p */ | ||
| 864 | if (!BN_GF2m_mod(u, y, p)) | ||
| 865 | goto err; | ||
| 866 | if (!BN_GF2m_mod(a, x, p)) | ||
| 867 | goto err; | ||
| 868 | if (!bn_copy(b, p)) | ||
| 869 | goto err; | ||
| 870 | |||
| 871 | while (!BN_is_odd(a)) { | ||
| 872 | if (!BN_rshift1(a, a)) | ||
| 873 | goto err; | ||
| 874 | if (BN_is_odd(u)) | ||
| 875 | if (!BN_GF2m_add(u, u, p)) | ||
| 876 | goto err; | ||
| 877 | if (!BN_rshift1(u, u)) | ||
| 878 | goto err; | ||
| 879 | } | ||
| 880 | |||
| 881 | do { | ||
| 882 | if (BN_GF2m_cmp(b, a) > 0) { | ||
| 883 | if (!BN_GF2m_add(b, b, a)) | ||
| 884 | goto err; | ||
| 885 | if (!BN_GF2m_add(v, v, u)) | ||
| 886 | goto err; | ||
| 887 | do { | ||
| 888 | if (!BN_rshift1(b, b)) | ||
| 889 | goto err; | ||
| 890 | if (BN_is_odd(v)) | ||
| 891 | if (!BN_GF2m_add(v, v, p)) | ||
| 892 | goto err; | ||
| 893 | if (!BN_rshift1(v, v)) | ||
| 894 | goto err; | ||
| 895 | } while (!BN_is_odd(b)); | ||
| 896 | } else if (BN_abs_is_word(a, 1)) | ||
| 897 | break; | ||
| 898 | else { | ||
| 899 | if (!BN_GF2m_add(a, a, b)) | ||
| 900 | goto err; | ||
| 901 | if (!BN_GF2m_add(u, u, v)) | ||
| 902 | goto err; | ||
| 903 | do { | ||
| 904 | if (!BN_rshift1(a, a)) | ||
| 905 | goto err; | ||
| 906 | if (BN_is_odd(u)) | ||
| 907 | if (!BN_GF2m_add(u, u, p)) | ||
| 908 | goto err; | ||
| 909 | if (!BN_rshift1(u, u)) | ||
| 910 | goto err; | ||
| 911 | } while (!BN_is_odd(a)); | ||
| 912 | } | ||
| 913 | } while (1); | ||
| 914 | |||
| 915 | if (!bn_copy(r, u)) | ||
| 916 | goto err; | ||
| 917 | ret = 1; | ||
| 918 | |||
| 919 | err: | ||
| 920 | BN_CTX_end(ctx); | ||
| 921 | return ret; | ||
| 922 | } | ||
| 923 | #endif | ||
| 924 | |||
| 925 | /* Divide yy by xx, reduce modulo p, and store the result in r. r could be xx | ||
| 926 | * or yy, xx could equal yy. | ||
| 927 | * | ||
| 928 | * This function calls down to the BN_GF2m_mod_div implementation; this wrapper | ||
| 929 | * function is only provided for convenience; for best performance, use the | ||
| 930 | * BN_GF2m_mod_div function. | ||
| 931 | */ | ||
| 932 | int | ||
| 933 | BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx, | ||
| 934 | const int p[], BN_CTX *ctx) | ||
| 935 | { | ||
| 936 | BIGNUM *field; | ||
| 937 | int ret = 0; | ||
| 938 | |||
| 939 | |||
| 940 | BN_CTX_start(ctx); | ||
| 941 | if ((field = BN_CTX_get(ctx)) == NULL) | ||
| 942 | goto err; | ||
| 943 | if (!BN_GF2m_arr2poly(p, field)) | ||
| 944 | goto err; | ||
| 945 | |||
| 946 | ret = BN_GF2m_mod_div(r, yy, xx, field, ctx); | ||
| 947 | |||
| 948 | err: | ||
| 949 | BN_CTX_end(ctx); | ||
| 950 | return ret; | ||
| 951 | } | ||
| 952 | |||
| 953 | |||
| 954 | /* Compute the bth power of a, reduce modulo p, and store | ||
| 955 | * the result in r. r could be a. | ||
| 956 | * Uses simple square-and-multiply algorithm A.5.1 from IEEE P1363. | ||
| 957 | */ | ||
| 958 | int | ||
| 959 | BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[], | ||
| 960 | BN_CTX *ctx) | ||
| 961 | { | ||
| 962 | int ret = 0, i, n; | ||
| 963 | BIGNUM *u; | ||
| 964 | |||
| 965 | |||
| 966 | if (BN_is_zero(b)) | ||
| 967 | return BN_one(r); | ||
| 968 | |||
| 969 | if (BN_abs_is_word(b, 1)) | ||
| 970 | return bn_copy(r, a); | ||
| 971 | |||
| 972 | BN_CTX_start(ctx); | ||
| 973 | if ((u = BN_CTX_get(ctx)) == NULL) | ||
| 974 | goto err; | ||
| 975 | |||
| 976 | if (!BN_GF2m_mod_arr(u, a, p)) | ||
| 977 | goto err; | ||
| 978 | |||
| 979 | n = BN_num_bits(b) - 1; | ||
| 980 | for (i = n - 1; i >= 0; i--) { | ||
| 981 | if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx)) | ||
| 982 | goto err; | ||
| 983 | if (BN_is_bit_set(b, i)) { | ||
| 984 | if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx)) | ||
| 985 | goto err; | ||
| 986 | } | ||
| 987 | } | ||
| 988 | if (!bn_copy(r, u)) | ||
| 989 | goto err; | ||
| 990 | ret = 1; | ||
| 991 | |||
| 992 | err: | ||
| 993 | BN_CTX_end(ctx); | ||
| 994 | return ret; | ||
| 995 | } | ||
| 996 | |||
| 997 | /* Compute the bth power of a, reduce modulo p, and store | ||
| 998 | * the result in r. r could be a. | ||
| 999 | * | ||
| 1000 | * This function calls down to the BN_GF2m_mod_exp_arr implementation; this wrapper | ||
| 1001 | * function is only provided for convenience; for best performance, use the | ||
| 1002 | * BN_GF2m_mod_exp_arr function. | ||
| 1003 | */ | ||
| 1004 | int | ||
| 1005 | BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, | ||
| 1006 | BN_CTX *ctx) | ||
| 1007 | { | ||
| 1008 | int ret = 0; | ||
| 1009 | const int max = BN_num_bits(p) + 1; | ||
| 1010 | int *arr = NULL; | ||
| 1011 | |||
| 1012 | if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) | ||
| 1013 | goto err; | ||
| 1014 | ret = BN_GF2m_poly2arr(p, arr, max); | ||
| 1015 | if (!ret || ret > max) { | ||
| 1016 | BNerror(BN_R_INVALID_LENGTH); | ||
| 1017 | goto err; | ||
| 1018 | } | ||
| 1019 | ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx); | ||
| 1020 | |||
| 1021 | err: | ||
| 1022 | free(arr); | ||
| 1023 | return ret; | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | /* Compute the square root of a, reduce modulo p, and store | ||
| 1027 | * the result in r. r could be a. | ||
| 1028 | * Uses exponentiation as in algorithm A.4.1 from IEEE P1363. | ||
| 1029 | */ | ||
| 1030 | int | ||
| 1031 | BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx) | ||
| 1032 | { | ||
| 1033 | int ret = 0; | ||
| 1034 | BIGNUM *u; | ||
| 1035 | |||
| 1036 | |||
| 1037 | if (!p[0]) { | ||
| 1038 | /* reduction mod 1 => return 0 */ | ||
| 1039 | BN_zero(r); | ||
| 1040 | return 1; | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | BN_CTX_start(ctx); | ||
| 1044 | if ((u = BN_CTX_get(ctx)) == NULL) | ||
| 1045 | goto err; | ||
| 1046 | |||
| 1047 | if (!BN_set_bit(u, p[0] - 1)) | ||
| 1048 | goto err; | ||
| 1049 | ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx); | ||
| 1050 | |||
| 1051 | err: | ||
| 1052 | BN_CTX_end(ctx); | ||
| 1053 | return ret; | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | /* Compute the square root of a, reduce modulo p, and store | ||
| 1057 | * the result in r. r could be a. | ||
| 1058 | * | ||
| 1059 | * This function calls down to the BN_GF2m_mod_sqrt_arr implementation; this wrapper | ||
| 1060 | * function is only provided for convenience; for best performance, use the | ||
| 1061 | * BN_GF2m_mod_sqrt_arr function. | ||
| 1062 | */ | ||
| 1063 | int | ||
| 1064 | BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | ||
| 1065 | { | ||
| 1066 | int ret = 0; | ||
| 1067 | const int max = BN_num_bits(p) + 1; | ||
| 1068 | int *arr = NULL; | ||
| 1069 | if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) | ||
| 1070 | goto err; | ||
| 1071 | ret = BN_GF2m_poly2arr(p, arr, max); | ||
| 1072 | if (!ret || ret > max) { | ||
| 1073 | BNerror(BN_R_INVALID_LENGTH); | ||
| 1074 | goto err; | ||
| 1075 | } | ||
| 1076 | ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx); | ||
| 1077 | |||
| 1078 | err: | ||
| 1079 | free(arr); | ||
| 1080 | return ret; | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | /* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0. | ||
| 1084 | * Uses algorithms A.4.7 and A.4.6 from IEEE P1363. | ||
| 1085 | */ | ||
| 1086 | int | ||
| 1087 | BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[], | ||
| 1088 | BN_CTX *ctx) | ||
| 1089 | { | ||
| 1090 | int ret = 0, count = 0, j; | ||
| 1091 | BIGNUM *a, *z, *rho, *w, *w2, *tmp; | ||
| 1092 | |||
| 1093 | |||
| 1094 | if (!p[0]) { | ||
| 1095 | /* reduction mod 1 => return 0 */ | ||
| 1096 | BN_zero(r); | ||
| 1097 | return 1; | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | BN_CTX_start(ctx); | ||
| 1101 | if ((a = BN_CTX_get(ctx)) == NULL) | ||
| 1102 | goto err; | ||
| 1103 | if ((z = BN_CTX_get(ctx)) == NULL) | ||
| 1104 | goto err; | ||
| 1105 | if ((w = BN_CTX_get(ctx)) == NULL) | ||
| 1106 | goto err; | ||
| 1107 | |||
| 1108 | if (!BN_GF2m_mod_arr(a, a_, p)) | ||
| 1109 | goto err; | ||
| 1110 | |||
| 1111 | if (BN_is_zero(a)) { | ||
| 1112 | BN_zero(r); | ||
| 1113 | ret = 1; | ||
| 1114 | goto err; | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | if (p[0] & 0x1) /* m is odd */ | ||
| 1118 | { | ||
| 1119 | /* compute half-trace of a */ | ||
| 1120 | if (!bn_copy(z, a)) | ||
| 1121 | goto err; | ||
| 1122 | for (j = 1; j <= (p[0] - 1) / 2; j++) { | ||
| 1123 | if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) | ||
| 1124 | goto err; | ||
| 1125 | if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) | ||
| 1126 | goto err; | ||
| 1127 | if (!BN_GF2m_add(z, z, a)) | ||
| 1128 | goto err; | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | } | ||
| 1132 | else /* m is even */ | ||
| 1133 | { | ||
| 1134 | if ((rho = BN_CTX_get(ctx)) == NULL) | ||
| 1135 | goto err; | ||
| 1136 | if ((w2 = BN_CTX_get(ctx)) == NULL) | ||
| 1137 | goto err; | ||
| 1138 | if ((tmp = BN_CTX_get(ctx)) == NULL) | ||
| 1139 | goto err; | ||
| 1140 | do { | ||
| 1141 | if (!BN_rand(rho, p[0], 0, 0)) | ||
| 1142 | goto err; | ||
| 1143 | if (!BN_GF2m_mod_arr(rho, rho, p)) | ||
| 1144 | goto err; | ||
| 1145 | BN_zero(z); | ||
| 1146 | if (!bn_copy(w, rho)) | ||
| 1147 | goto err; | ||
| 1148 | for (j = 1; j <= p[0] - 1; j++) { | ||
| 1149 | if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) | ||
| 1150 | goto err; | ||
| 1151 | if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx)) | ||
| 1152 | goto err; | ||
| 1153 | if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx)) | ||
| 1154 | goto err; | ||
| 1155 | if (!BN_GF2m_add(z, z, tmp)) | ||
| 1156 | goto err; | ||
| 1157 | if (!BN_GF2m_add(w, w2, rho)) | ||
| 1158 | goto err; | ||
| 1159 | } | ||
| 1160 | count++; | ||
| 1161 | } while (BN_is_zero(w) && (count < MAX_ITERATIONS)); | ||
| 1162 | if (BN_is_zero(w)) { | ||
| 1163 | BNerror(BN_R_TOO_MANY_ITERATIONS); | ||
| 1164 | goto err; | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx)) | ||
| 1169 | goto err; | ||
| 1170 | if (!BN_GF2m_add(w, z, w)) | ||
| 1171 | goto err; | ||
| 1172 | if (BN_GF2m_cmp(w, a)) { | ||
| 1173 | BNerror(BN_R_NO_SOLUTION); | ||
| 1174 | goto err; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | if (!bn_copy(r, z)) | ||
| 1178 | goto err; | ||
| 1179 | |||
| 1180 | ret = 1; | ||
| 1181 | |||
| 1182 | err: | ||
| 1183 | BN_CTX_end(ctx); | ||
| 1184 | return ret; | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | /* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0. | ||
| 1188 | * | ||
| 1189 | * This function calls down to the BN_GF2m_mod_solve_quad_arr implementation; this wrapper | ||
| 1190 | * function is only provided for convenience; for best performance, use the | ||
| 1191 | * BN_GF2m_mod_solve_quad_arr function. | ||
| 1192 | */ | ||
| 1193 | int | ||
| 1194 | BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | ||
| 1195 | { | ||
| 1196 | int ret = 0; | ||
| 1197 | const int max = BN_num_bits(p) + 1; | ||
| 1198 | int *arr = NULL; | ||
| 1199 | |||
| 1200 | if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) | ||
| 1201 | goto err; | ||
| 1202 | ret = BN_GF2m_poly2arr(p, arr, max); | ||
| 1203 | if (!ret || ret > max) { | ||
| 1204 | BNerror(BN_R_INVALID_LENGTH); | ||
| 1205 | goto err; | ||
| 1206 | } | ||
| 1207 | ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx); | ||
| 1208 | |||
| 1209 | err: | ||
| 1210 | free(arr); | ||
| 1211 | return ret; | ||
| 1212 | } | ||
| 1213 | |||
| 1214 | /* Convert the bit-string representation of a polynomial | ||
| 1215 | * ( \sum_{i=0}^n a_i * x^i) into an array of integers corresponding | ||
| 1216 | * to the bits with non-zero coefficient. Array is terminated with -1. | ||
| 1217 | * Up to max elements of the array will be filled. Return value is total | ||
| 1218 | * number of array elements that would be filled if array was large enough. | ||
| 1219 | */ | ||
| 1220 | int | ||
| 1221 | BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max) | ||
| 1222 | { | ||
| 1223 | int i, j, k = 0; | ||
| 1224 | BN_ULONG mask; | ||
| 1225 | |||
| 1226 | if (BN_is_zero(a)) | ||
| 1227 | return 0; | ||
| 1228 | |||
| 1229 | for (i = a->top - 1; i >= 0; i--) { | ||
| 1230 | if (!a->d[i]) | ||
| 1231 | /* skip word if a->d[i] == 0 */ | ||
| 1232 | continue; | ||
| 1233 | mask = BN_TBIT; | ||
| 1234 | for (j = BN_BITS2 - 1; j >= 0; j--) { | ||
| 1235 | if (a->d[i] & mask) { | ||
| 1236 | if (k < max) | ||
| 1237 | p[k] = BN_BITS2 * i + j; | ||
| 1238 | k++; | ||
| 1239 | } | ||
| 1240 | mask >>= 1; | ||
| 1241 | } | ||
| 1242 | } | ||
| 1243 | |||
| 1244 | if (k < max) | ||
| 1245 | p[k] = -1; | ||
| 1246 | k++; | ||
| 1247 | |||
| 1248 | return k; | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | /* Convert the coefficient array representation of a polynomial to a | ||
| 1252 | * bit-string. The array must be terminated by -1. | ||
| 1253 | */ | ||
| 1254 | int | ||
| 1255 | BN_GF2m_arr2poly(const int p[], BIGNUM *a) | ||
| 1256 | { | ||
| 1257 | int i; | ||
| 1258 | |||
| 1259 | BN_zero(a); | ||
| 1260 | for (i = 0; p[i] != -1; i++) { | ||
| 1261 | if (BN_set_bit(a, p[i]) == 0) | ||
| 1262 | return 0; | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | return 1; | ||
| 1266 | } | ||
| 1267 | |||
| 1268 | #endif | ||
