SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/integer.c@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* integer.c |
wolfSSL | 0:9d17e4342598 | 2 | * |
wolfSSL | 0:9d17e4342598 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
wolfSSL | 0:9d17e4342598 | 4 | * |
wolfSSL | 0:9d17e4342598 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:9d17e4342598 | 6 | * |
wolfSSL | 0:9d17e4342598 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:9d17e4342598 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:9d17e4342598 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:9d17e4342598 | 10 | * (at your option) any later version. |
wolfSSL | 0:9d17e4342598 | 11 | * |
wolfSSL | 0:9d17e4342598 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:9d17e4342598 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:9d17e4342598 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:9d17e4342598 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:9d17e4342598 | 16 | * |
wolfSSL | 0:9d17e4342598 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:9d17e4342598 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:9d17e4342598 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
wolfSSL | 0:9d17e4342598 | 20 | */ |
wolfSSL | 0:9d17e4342598 | 21 | |
wolfSSL | 0:9d17e4342598 | 22 | |
wolfSSL | 0:9d17e4342598 | 23 | /* |
wolfSSL | 0:9d17e4342598 | 24 | * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca, |
wolfSSL | 0:9d17e4342598 | 25 | * http://math.libtomcrypt.com |
wolfSSL | 0:9d17e4342598 | 26 | */ |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | |
wolfSSL | 0:9d17e4342598 | 29 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 30 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 31 | #endif |
wolfSSL | 0:9d17e4342598 | 32 | |
wolfSSL | 0:9d17e4342598 | 33 | /* in case user set USE_FAST_MATH there */ |
wolfSSL | 0:9d17e4342598 | 34 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 35 | |
wolfSSL | 0:9d17e4342598 | 36 | #ifndef NO_BIG_INT |
wolfSSL | 0:9d17e4342598 | 37 | |
wolfSSL | 0:9d17e4342598 | 38 | #ifndef USE_FAST_MATH |
wolfSSL | 0:9d17e4342598 | 39 | |
wolfSSL | 0:9d17e4342598 | 40 | #include <cyassl/ctaocrypt/integer.h> |
wolfSSL | 0:9d17e4342598 | 41 | |
wolfSSL | 0:9d17e4342598 | 42 | #ifndef NO_CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 43 | #ifndef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 44 | #define CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 45 | #endif |
wolfSSL | 0:9d17e4342598 | 46 | #endif |
wolfSSL | 0:9d17e4342598 | 47 | |
wolfSSL | 0:9d17e4342598 | 48 | static void bn_reverse (unsigned char *s, int len); |
wolfSSL | 0:9d17e4342598 | 49 | |
wolfSSL | 0:9d17e4342598 | 50 | /* math settings check */ |
wolfSSL | 0:9d17e4342598 | 51 | word32 CheckRunTimeSettings(void) |
wolfSSL | 0:9d17e4342598 | 52 | { |
wolfSSL | 0:9d17e4342598 | 53 | return CTC_SETTINGS; |
wolfSSL | 0:9d17e4342598 | 54 | } |
wolfSSL | 0:9d17e4342598 | 55 | |
wolfSSL | 0:9d17e4342598 | 56 | |
wolfSSL | 0:9d17e4342598 | 57 | /* handle up to 6 inits */ |
wolfSSL | 0:9d17e4342598 | 58 | int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, |
wolfSSL | 0:9d17e4342598 | 59 | mp_int* f) |
wolfSSL | 0:9d17e4342598 | 60 | { |
wolfSSL | 0:9d17e4342598 | 61 | int res = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 62 | |
wolfSSL | 0:9d17e4342598 | 63 | if (a && ((res = mp_init(a)) != MP_OKAY)) |
wolfSSL | 0:9d17e4342598 | 64 | return res; |
wolfSSL | 0:9d17e4342598 | 65 | |
wolfSSL | 0:9d17e4342598 | 66 | if (b && ((res = mp_init(b)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 67 | mp_clear(a); |
wolfSSL | 0:9d17e4342598 | 68 | return res; |
wolfSSL | 0:9d17e4342598 | 69 | } |
wolfSSL | 0:9d17e4342598 | 70 | |
wolfSSL | 0:9d17e4342598 | 71 | if (c && ((res = mp_init(c)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 72 | mp_clear(a); mp_clear(b); |
wolfSSL | 0:9d17e4342598 | 73 | return res; |
wolfSSL | 0:9d17e4342598 | 74 | } |
wolfSSL | 0:9d17e4342598 | 75 | |
wolfSSL | 0:9d17e4342598 | 76 | if (d && ((res = mp_init(d)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 77 | mp_clear(a); mp_clear(b); mp_clear(c); |
wolfSSL | 0:9d17e4342598 | 78 | return res; |
wolfSSL | 0:9d17e4342598 | 79 | } |
wolfSSL | 0:9d17e4342598 | 80 | |
wolfSSL | 0:9d17e4342598 | 81 | if (e && ((res = mp_init(e)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 82 | mp_clear(a); mp_clear(b); mp_clear(c); mp_clear(d); |
wolfSSL | 0:9d17e4342598 | 83 | return res; |
wolfSSL | 0:9d17e4342598 | 84 | } |
wolfSSL | 0:9d17e4342598 | 85 | |
wolfSSL | 0:9d17e4342598 | 86 | if (f && ((res = mp_init(f)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 87 | mp_clear(a); mp_clear(b); mp_clear(c); mp_clear(d); mp_clear(e); |
wolfSSL | 0:9d17e4342598 | 88 | return res; |
wolfSSL | 0:9d17e4342598 | 89 | } |
wolfSSL | 0:9d17e4342598 | 90 | |
wolfSSL | 0:9d17e4342598 | 91 | return res; |
wolfSSL | 0:9d17e4342598 | 92 | } |
wolfSSL | 0:9d17e4342598 | 93 | |
wolfSSL | 0:9d17e4342598 | 94 | |
wolfSSL | 0:9d17e4342598 | 95 | /* init a new mp_int */ |
wolfSSL | 0:9d17e4342598 | 96 | int mp_init (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 97 | { |
wolfSSL | 0:9d17e4342598 | 98 | int i; |
wolfSSL | 0:9d17e4342598 | 99 | |
wolfSSL | 0:9d17e4342598 | 100 | /* allocate memory required and clear it */ |
wolfSSL | 0:9d17e4342598 | 101 | a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC, 0, |
wolfSSL | 0:9d17e4342598 | 102 | DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 103 | if (a->dp == NULL) { |
wolfSSL | 0:9d17e4342598 | 104 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 105 | } |
wolfSSL | 0:9d17e4342598 | 106 | |
wolfSSL | 0:9d17e4342598 | 107 | /* set the digits to zero */ |
wolfSSL | 0:9d17e4342598 | 108 | for (i = 0; i < MP_PREC; i++) { |
wolfSSL | 0:9d17e4342598 | 109 | a->dp[i] = 0; |
wolfSSL | 0:9d17e4342598 | 110 | } |
wolfSSL | 0:9d17e4342598 | 111 | |
wolfSSL | 0:9d17e4342598 | 112 | /* set the used to zero, allocated digits to the default precision |
wolfSSL | 0:9d17e4342598 | 113 | * and sign to positive */ |
wolfSSL | 0:9d17e4342598 | 114 | a->used = 0; |
wolfSSL | 0:9d17e4342598 | 115 | a->alloc = MP_PREC; |
wolfSSL | 0:9d17e4342598 | 116 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 117 | |
wolfSSL | 0:9d17e4342598 | 118 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 119 | } |
wolfSSL | 0:9d17e4342598 | 120 | |
wolfSSL | 0:9d17e4342598 | 121 | |
wolfSSL | 0:9d17e4342598 | 122 | /* clear one (frees) */ |
wolfSSL | 0:9d17e4342598 | 123 | void |
wolfSSL | 0:9d17e4342598 | 124 | mp_clear (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 125 | { |
wolfSSL | 0:9d17e4342598 | 126 | int i; |
wolfSSL | 0:9d17e4342598 | 127 | |
wolfSSL | 0:9d17e4342598 | 128 | if (a == NULL) |
wolfSSL | 0:9d17e4342598 | 129 | return; |
wolfSSL | 0:9d17e4342598 | 130 | |
wolfSSL | 0:9d17e4342598 | 131 | /* only do anything if a hasn't been freed previously */ |
wolfSSL | 0:9d17e4342598 | 132 | if (a->dp != NULL) { |
wolfSSL | 0:9d17e4342598 | 133 | /* first zero the digits */ |
wolfSSL | 0:9d17e4342598 | 134 | for (i = 0; i < a->used; i++) { |
wolfSSL | 0:9d17e4342598 | 135 | a->dp[i] = 0; |
wolfSSL | 0:9d17e4342598 | 136 | } |
wolfSSL | 0:9d17e4342598 | 137 | |
wolfSSL | 0:9d17e4342598 | 138 | /* free ram */ |
wolfSSL | 0:9d17e4342598 | 139 | XFREE(a->dp, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 140 | |
wolfSSL | 0:9d17e4342598 | 141 | /* reset members to make debugging easier */ |
wolfSSL | 0:9d17e4342598 | 142 | a->dp = NULL; |
wolfSSL | 0:9d17e4342598 | 143 | a->alloc = a->used = 0; |
wolfSSL | 0:9d17e4342598 | 144 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 145 | } |
wolfSSL | 0:9d17e4342598 | 146 | } |
wolfSSL | 0:9d17e4342598 | 147 | |
wolfSSL | 0:9d17e4342598 | 148 | |
wolfSSL | 0:9d17e4342598 | 149 | /* get the size for an unsigned equivalent */ |
wolfSSL | 0:9d17e4342598 | 150 | int mp_unsigned_bin_size (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 151 | { |
wolfSSL | 0:9d17e4342598 | 152 | int size = mp_count_bits (a); |
wolfSSL | 0:9d17e4342598 | 153 | return (size / 8 + ((size & 7) != 0 ? 1 : 0)); |
wolfSSL | 0:9d17e4342598 | 154 | } |
wolfSSL | 0:9d17e4342598 | 155 | |
wolfSSL | 0:9d17e4342598 | 156 | |
wolfSSL | 0:9d17e4342598 | 157 | /* returns the number of bits in an int */ |
wolfSSL | 0:9d17e4342598 | 158 | int |
wolfSSL | 0:9d17e4342598 | 159 | mp_count_bits (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 160 | { |
wolfSSL | 0:9d17e4342598 | 161 | int r; |
wolfSSL | 0:9d17e4342598 | 162 | mp_digit q; |
wolfSSL | 0:9d17e4342598 | 163 | |
wolfSSL | 0:9d17e4342598 | 164 | /* shortcut */ |
wolfSSL | 0:9d17e4342598 | 165 | if (a->used == 0) { |
wolfSSL | 0:9d17e4342598 | 166 | return 0; |
wolfSSL | 0:9d17e4342598 | 167 | } |
wolfSSL | 0:9d17e4342598 | 168 | |
wolfSSL | 0:9d17e4342598 | 169 | /* get number of digits and add that */ |
wolfSSL | 0:9d17e4342598 | 170 | r = (a->used - 1) * DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 171 | |
wolfSSL | 0:9d17e4342598 | 172 | /* take the last digit and count the bits in it */ |
wolfSSL | 0:9d17e4342598 | 173 | q = a->dp[a->used - 1]; |
wolfSSL | 0:9d17e4342598 | 174 | while (q > ((mp_digit) 0)) { |
wolfSSL | 0:9d17e4342598 | 175 | ++r; |
wolfSSL | 0:9d17e4342598 | 176 | q >>= ((mp_digit) 1); |
wolfSSL | 0:9d17e4342598 | 177 | } |
wolfSSL | 0:9d17e4342598 | 178 | return r; |
wolfSSL | 0:9d17e4342598 | 179 | } |
wolfSSL | 0:9d17e4342598 | 180 | |
wolfSSL | 0:9d17e4342598 | 181 | |
wolfSSL | 0:9d17e4342598 | 182 | int mp_leading_bit (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 183 | { |
wolfSSL | 0:9d17e4342598 | 184 | int bit = 0; |
wolfSSL | 0:9d17e4342598 | 185 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 186 | |
wolfSSL | 0:9d17e4342598 | 187 | if (mp_init_copy(&t, a) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 188 | return 0; |
wolfSSL | 0:9d17e4342598 | 189 | |
wolfSSL | 0:9d17e4342598 | 190 | while (mp_iszero(&t) == 0) { |
wolfSSL | 0:9d17e4342598 | 191 | #ifndef MP_8BIT |
wolfSSL | 0:9d17e4342598 | 192 | bit = (t.dp[0] & 0x80) != 0; |
wolfSSL | 0:9d17e4342598 | 193 | #else |
wolfSSL | 0:9d17e4342598 | 194 | bit = (t.dp[0] | ((t.dp[1] & 0x01) << 7)) & 0x80 != 0; |
wolfSSL | 0:9d17e4342598 | 195 | #endif |
wolfSSL | 0:9d17e4342598 | 196 | if (mp_div_2d (&t, 8, &t, NULL) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 197 | break; |
wolfSSL | 0:9d17e4342598 | 198 | } |
wolfSSL | 0:9d17e4342598 | 199 | mp_clear(&t); |
wolfSSL | 0:9d17e4342598 | 200 | return bit; |
wolfSSL | 0:9d17e4342598 | 201 | } |
wolfSSL | 0:9d17e4342598 | 202 | |
wolfSSL | 0:9d17e4342598 | 203 | |
wolfSSL | 0:9d17e4342598 | 204 | /* store in unsigned [big endian] format */ |
wolfSSL | 0:9d17e4342598 | 205 | int mp_to_unsigned_bin (mp_int * a, unsigned char *b) |
wolfSSL | 0:9d17e4342598 | 206 | { |
wolfSSL | 0:9d17e4342598 | 207 | int x, res; |
wolfSSL | 0:9d17e4342598 | 208 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 209 | |
wolfSSL | 0:9d17e4342598 | 210 | if ((res = mp_init_copy (&t, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 211 | return res; |
wolfSSL | 0:9d17e4342598 | 212 | } |
wolfSSL | 0:9d17e4342598 | 213 | |
wolfSSL | 0:9d17e4342598 | 214 | x = 0; |
wolfSSL | 0:9d17e4342598 | 215 | while (mp_iszero (&t) == 0) { |
wolfSSL | 0:9d17e4342598 | 216 | #ifndef MP_8BIT |
wolfSSL | 0:9d17e4342598 | 217 | b[x++] = (unsigned char) (t.dp[0] & 255); |
wolfSSL | 0:9d17e4342598 | 218 | #else |
wolfSSL | 0:9d17e4342598 | 219 | b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7)); |
wolfSSL | 0:9d17e4342598 | 220 | #endif |
wolfSSL | 0:9d17e4342598 | 221 | if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 222 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 223 | return res; |
wolfSSL | 0:9d17e4342598 | 224 | } |
wolfSSL | 0:9d17e4342598 | 225 | } |
wolfSSL | 0:9d17e4342598 | 226 | bn_reverse (b, x); |
wolfSSL | 0:9d17e4342598 | 227 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 228 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 229 | } |
wolfSSL | 0:9d17e4342598 | 230 | |
wolfSSL | 0:9d17e4342598 | 231 | |
wolfSSL | 0:9d17e4342598 | 232 | /* creates "a" then copies b into it */ |
wolfSSL | 0:9d17e4342598 | 233 | int mp_init_copy (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 234 | { |
wolfSSL | 0:9d17e4342598 | 235 | int res; |
wolfSSL | 0:9d17e4342598 | 236 | |
wolfSSL | 0:9d17e4342598 | 237 | if ((res = mp_init (a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 238 | return res; |
wolfSSL | 0:9d17e4342598 | 239 | } |
wolfSSL | 0:9d17e4342598 | 240 | return mp_copy (b, a); |
wolfSSL | 0:9d17e4342598 | 241 | } |
wolfSSL | 0:9d17e4342598 | 242 | |
wolfSSL | 0:9d17e4342598 | 243 | |
wolfSSL | 0:9d17e4342598 | 244 | /* copy, b = a */ |
wolfSSL | 0:9d17e4342598 | 245 | int |
wolfSSL | 0:9d17e4342598 | 246 | mp_copy (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 247 | { |
wolfSSL | 0:9d17e4342598 | 248 | int res, n; |
wolfSSL | 0:9d17e4342598 | 249 | |
wolfSSL | 0:9d17e4342598 | 250 | /* if dst == src do nothing */ |
wolfSSL | 0:9d17e4342598 | 251 | if (a == b) { |
wolfSSL | 0:9d17e4342598 | 252 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 253 | } |
wolfSSL | 0:9d17e4342598 | 254 | |
wolfSSL | 0:9d17e4342598 | 255 | /* grow dest */ |
wolfSSL | 0:9d17e4342598 | 256 | if (b->alloc < a->used) { |
wolfSSL | 0:9d17e4342598 | 257 | if ((res = mp_grow (b, a->used)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 258 | return res; |
wolfSSL | 0:9d17e4342598 | 259 | } |
wolfSSL | 0:9d17e4342598 | 260 | } |
wolfSSL | 0:9d17e4342598 | 261 | |
wolfSSL | 0:9d17e4342598 | 262 | /* zero b and copy the parameters over */ |
wolfSSL | 0:9d17e4342598 | 263 | { |
wolfSSL | 0:9d17e4342598 | 264 | register mp_digit *tmpa, *tmpb; |
wolfSSL | 0:9d17e4342598 | 265 | |
wolfSSL | 0:9d17e4342598 | 266 | /* pointer aliases */ |
wolfSSL | 0:9d17e4342598 | 267 | |
wolfSSL | 0:9d17e4342598 | 268 | /* source */ |
wolfSSL | 0:9d17e4342598 | 269 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 270 | |
wolfSSL | 0:9d17e4342598 | 271 | /* destination */ |
wolfSSL | 0:9d17e4342598 | 272 | tmpb = b->dp; |
wolfSSL | 0:9d17e4342598 | 273 | |
wolfSSL | 0:9d17e4342598 | 274 | /* copy all the digits */ |
wolfSSL | 0:9d17e4342598 | 275 | for (n = 0; n < a->used; n++) { |
wolfSSL | 0:9d17e4342598 | 276 | *tmpb++ = *tmpa++; |
wolfSSL | 0:9d17e4342598 | 277 | } |
wolfSSL | 0:9d17e4342598 | 278 | |
wolfSSL | 0:9d17e4342598 | 279 | /* clear high digits */ |
wolfSSL | 0:9d17e4342598 | 280 | for (; n < b->used; n++) { |
wolfSSL | 0:9d17e4342598 | 281 | *tmpb++ = 0; |
wolfSSL | 0:9d17e4342598 | 282 | } |
wolfSSL | 0:9d17e4342598 | 283 | } |
wolfSSL | 0:9d17e4342598 | 284 | |
wolfSSL | 0:9d17e4342598 | 285 | /* copy used count and sign */ |
wolfSSL | 0:9d17e4342598 | 286 | b->used = a->used; |
wolfSSL | 0:9d17e4342598 | 287 | b->sign = a->sign; |
wolfSSL | 0:9d17e4342598 | 288 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 289 | } |
wolfSSL | 0:9d17e4342598 | 290 | |
wolfSSL | 0:9d17e4342598 | 291 | |
wolfSSL | 0:9d17e4342598 | 292 | /* grow as required */ |
wolfSSL | 0:9d17e4342598 | 293 | int mp_grow (mp_int * a, int size) |
wolfSSL | 0:9d17e4342598 | 294 | { |
wolfSSL | 0:9d17e4342598 | 295 | int i; |
wolfSSL | 0:9d17e4342598 | 296 | mp_digit *tmp; |
wolfSSL | 0:9d17e4342598 | 297 | |
wolfSSL | 0:9d17e4342598 | 298 | /* if the alloc size is smaller alloc more ram */ |
wolfSSL | 0:9d17e4342598 | 299 | if (a->alloc < size) { |
wolfSSL | 0:9d17e4342598 | 300 | /* ensure there are always at least MP_PREC digits extra on top */ |
wolfSSL | 0:9d17e4342598 | 301 | size += (MP_PREC * 2) - (size % MP_PREC); |
wolfSSL | 0:9d17e4342598 | 302 | |
wolfSSL | 0:9d17e4342598 | 303 | /* reallocate the array a->dp |
wolfSSL | 0:9d17e4342598 | 304 | * |
wolfSSL | 0:9d17e4342598 | 305 | * We store the return in a temporary variable |
wolfSSL | 0:9d17e4342598 | 306 | * in case the operation failed we don't want |
wolfSSL | 0:9d17e4342598 | 307 | * to overwrite the dp member of a. |
wolfSSL | 0:9d17e4342598 | 308 | */ |
wolfSSL | 0:9d17e4342598 | 309 | tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size, 0, |
wolfSSL | 0:9d17e4342598 | 310 | DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 311 | if (tmp == NULL) { |
wolfSSL | 0:9d17e4342598 | 312 | /* reallocation failed but "a" is still valid [can be freed] */ |
wolfSSL | 0:9d17e4342598 | 313 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 314 | } |
wolfSSL | 0:9d17e4342598 | 315 | |
wolfSSL | 0:9d17e4342598 | 316 | /* reallocation succeeded so set a->dp */ |
wolfSSL | 0:9d17e4342598 | 317 | a->dp = tmp; |
wolfSSL | 0:9d17e4342598 | 318 | |
wolfSSL | 0:9d17e4342598 | 319 | /* zero excess digits */ |
wolfSSL | 0:9d17e4342598 | 320 | i = a->alloc; |
wolfSSL | 0:9d17e4342598 | 321 | a->alloc = size; |
wolfSSL | 0:9d17e4342598 | 322 | for (; i < a->alloc; i++) { |
wolfSSL | 0:9d17e4342598 | 323 | a->dp[i] = 0; |
wolfSSL | 0:9d17e4342598 | 324 | } |
wolfSSL | 0:9d17e4342598 | 325 | } |
wolfSSL | 0:9d17e4342598 | 326 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 327 | } |
wolfSSL | 0:9d17e4342598 | 328 | |
wolfSSL | 0:9d17e4342598 | 329 | |
wolfSSL | 0:9d17e4342598 | 330 | /* reverse an array, used for radix code */ |
wolfSSL | 0:9d17e4342598 | 331 | void |
wolfSSL | 0:9d17e4342598 | 332 | bn_reverse (unsigned char *s, int len) |
wolfSSL | 0:9d17e4342598 | 333 | { |
wolfSSL | 0:9d17e4342598 | 334 | int ix, iy; |
wolfSSL | 0:9d17e4342598 | 335 | unsigned char t; |
wolfSSL | 0:9d17e4342598 | 336 | |
wolfSSL | 0:9d17e4342598 | 337 | ix = 0; |
wolfSSL | 0:9d17e4342598 | 338 | iy = len - 1; |
wolfSSL | 0:9d17e4342598 | 339 | while (ix < iy) { |
wolfSSL | 0:9d17e4342598 | 340 | t = s[ix]; |
wolfSSL | 0:9d17e4342598 | 341 | s[ix] = s[iy]; |
wolfSSL | 0:9d17e4342598 | 342 | s[iy] = t; |
wolfSSL | 0:9d17e4342598 | 343 | ++ix; |
wolfSSL | 0:9d17e4342598 | 344 | --iy; |
wolfSSL | 0:9d17e4342598 | 345 | } |
wolfSSL | 0:9d17e4342598 | 346 | } |
wolfSSL | 0:9d17e4342598 | 347 | |
wolfSSL | 0:9d17e4342598 | 348 | |
wolfSSL | 0:9d17e4342598 | 349 | /* shift right by a certain bit count (store quotient in c, optional |
wolfSSL | 0:9d17e4342598 | 350 | remainder in d) */ |
wolfSSL | 0:9d17e4342598 | 351 | int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d) |
wolfSSL | 0:9d17e4342598 | 352 | { |
wolfSSL | 0:9d17e4342598 | 353 | int D, res; |
wolfSSL | 0:9d17e4342598 | 354 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 355 | |
wolfSSL | 0:9d17e4342598 | 356 | |
wolfSSL | 0:9d17e4342598 | 357 | /* if the shift count is <= 0 then we do no work */ |
wolfSSL | 0:9d17e4342598 | 358 | if (b <= 0) { |
wolfSSL | 0:9d17e4342598 | 359 | res = mp_copy (a, c); |
wolfSSL | 0:9d17e4342598 | 360 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 361 | mp_zero (d); |
wolfSSL | 0:9d17e4342598 | 362 | } |
wolfSSL | 0:9d17e4342598 | 363 | return res; |
wolfSSL | 0:9d17e4342598 | 364 | } |
wolfSSL | 0:9d17e4342598 | 365 | |
wolfSSL | 0:9d17e4342598 | 366 | if ((res = mp_init (&t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 367 | return res; |
wolfSSL | 0:9d17e4342598 | 368 | } |
wolfSSL | 0:9d17e4342598 | 369 | |
wolfSSL | 0:9d17e4342598 | 370 | /* get the remainder */ |
wolfSSL | 0:9d17e4342598 | 371 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 372 | if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 373 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 374 | return res; |
wolfSSL | 0:9d17e4342598 | 375 | } |
wolfSSL | 0:9d17e4342598 | 376 | } |
wolfSSL | 0:9d17e4342598 | 377 | |
wolfSSL | 0:9d17e4342598 | 378 | /* copy */ |
wolfSSL | 0:9d17e4342598 | 379 | if ((res = mp_copy (a, c)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 380 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 381 | return res; |
wolfSSL | 0:9d17e4342598 | 382 | } |
wolfSSL | 0:9d17e4342598 | 383 | |
wolfSSL | 0:9d17e4342598 | 384 | /* shift by as many digits in the bit count */ |
wolfSSL | 0:9d17e4342598 | 385 | if (b >= (int)DIGIT_BIT) { |
wolfSSL | 0:9d17e4342598 | 386 | mp_rshd (c, b / DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 387 | } |
wolfSSL | 0:9d17e4342598 | 388 | |
wolfSSL | 0:9d17e4342598 | 389 | /* shift any bit count < DIGIT_BIT */ |
wolfSSL | 0:9d17e4342598 | 390 | D = (b % DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 391 | if (D != 0) { |
wolfSSL | 0:9d17e4342598 | 392 | mp_rshb(c, D); |
wolfSSL | 0:9d17e4342598 | 393 | } |
wolfSSL | 0:9d17e4342598 | 394 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 395 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 396 | mp_exch (&t, d); |
wolfSSL | 0:9d17e4342598 | 397 | } |
wolfSSL | 0:9d17e4342598 | 398 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 399 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 400 | } |
wolfSSL | 0:9d17e4342598 | 401 | |
wolfSSL | 0:9d17e4342598 | 402 | |
wolfSSL | 0:9d17e4342598 | 403 | /* set to zero */ |
wolfSSL | 0:9d17e4342598 | 404 | void mp_zero (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 405 | { |
wolfSSL | 0:9d17e4342598 | 406 | int n; |
wolfSSL | 0:9d17e4342598 | 407 | mp_digit *tmp; |
wolfSSL | 0:9d17e4342598 | 408 | |
wolfSSL | 0:9d17e4342598 | 409 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 410 | a->used = 0; |
wolfSSL | 0:9d17e4342598 | 411 | |
wolfSSL | 0:9d17e4342598 | 412 | tmp = a->dp; |
wolfSSL | 0:9d17e4342598 | 413 | for (n = 0; n < a->alloc; n++) { |
wolfSSL | 0:9d17e4342598 | 414 | *tmp++ = 0; |
wolfSSL | 0:9d17e4342598 | 415 | } |
wolfSSL | 0:9d17e4342598 | 416 | } |
wolfSSL | 0:9d17e4342598 | 417 | |
wolfSSL | 0:9d17e4342598 | 418 | |
wolfSSL | 0:9d17e4342598 | 419 | /* trim unused digits |
wolfSSL | 0:9d17e4342598 | 420 | * |
wolfSSL | 0:9d17e4342598 | 421 | * This is used to ensure that leading zero digits are |
wolfSSL | 0:9d17e4342598 | 422 | * trimed and the leading "used" digit will be non-zero |
wolfSSL | 0:9d17e4342598 | 423 | * Typically very fast. Also fixes the sign if there |
wolfSSL | 0:9d17e4342598 | 424 | * are no more leading digits |
wolfSSL | 0:9d17e4342598 | 425 | */ |
wolfSSL | 0:9d17e4342598 | 426 | void |
wolfSSL | 0:9d17e4342598 | 427 | mp_clamp (mp_int * a) |
wolfSSL | 0:9d17e4342598 | 428 | { |
wolfSSL | 0:9d17e4342598 | 429 | /* decrease used while the most significant digit is |
wolfSSL | 0:9d17e4342598 | 430 | * zero. |
wolfSSL | 0:9d17e4342598 | 431 | */ |
wolfSSL | 0:9d17e4342598 | 432 | while (a->used > 0 && a->dp[a->used - 1] == 0) { |
wolfSSL | 0:9d17e4342598 | 433 | --(a->used); |
wolfSSL | 0:9d17e4342598 | 434 | } |
wolfSSL | 0:9d17e4342598 | 435 | |
wolfSSL | 0:9d17e4342598 | 436 | /* reset the sign flag if used == 0 */ |
wolfSSL | 0:9d17e4342598 | 437 | if (a->used == 0) { |
wolfSSL | 0:9d17e4342598 | 438 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 439 | } |
wolfSSL | 0:9d17e4342598 | 440 | } |
wolfSSL | 0:9d17e4342598 | 441 | |
wolfSSL | 0:9d17e4342598 | 442 | |
wolfSSL | 0:9d17e4342598 | 443 | /* swap the elements of two integers, for cases where you can't simply swap the |
wolfSSL | 0:9d17e4342598 | 444 | * mp_int pointers around |
wolfSSL | 0:9d17e4342598 | 445 | */ |
wolfSSL | 0:9d17e4342598 | 446 | void |
wolfSSL | 0:9d17e4342598 | 447 | mp_exch (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 448 | { |
wolfSSL | 0:9d17e4342598 | 449 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 450 | |
wolfSSL | 0:9d17e4342598 | 451 | t = *a; |
wolfSSL | 0:9d17e4342598 | 452 | *a = *b; |
wolfSSL | 0:9d17e4342598 | 453 | *b = t; |
wolfSSL | 0:9d17e4342598 | 454 | } |
wolfSSL | 0:9d17e4342598 | 455 | |
wolfSSL | 0:9d17e4342598 | 456 | |
wolfSSL | 0:9d17e4342598 | 457 | /* shift right a certain number of bits */ |
wolfSSL | 0:9d17e4342598 | 458 | void mp_rshb (mp_int *c, int x) |
wolfSSL | 0:9d17e4342598 | 459 | { |
wolfSSL | 0:9d17e4342598 | 460 | register mp_digit *tmpc, mask, shift; |
wolfSSL | 0:9d17e4342598 | 461 | mp_digit r, rr; |
wolfSSL | 0:9d17e4342598 | 462 | mp_digit D = x; |
wolfSSL | 0:9d17e4342598 | 463 | |
wolfSSL | 0:9d17e4342598 | 464 | /* mask */ |
wolfSSL | 0:9d17e4342598 | 465 | mask = (((mp_digit)1) << D) - 1; |
wolfSSL | 0:9d17e4342598 | 466 | |
wolfSSL | 0:9d17e4342598 | 467 | /* shift for lsb */ |
wolfSSL | 0:9d17e4342598 | 468 | shift = DIGIT_BIT - D; |
wolfSSL | 0:9d17e4342598 | 469 | |
wolfSSL | 0:9d17e4342598 | 470 | /* alias */ |
wolfSSL | 0:9d17e4342598 | 471 | tmpc = c->dp + (c->used - 1); |
wolfSSL | 0:9d17e4342598 | 472 | |
wolfSSL | 0:9d17e4342598 | 473 | /* carry */ |
wolfSSL | 0:9d17e4342598 | 474 | r = 0; |
wolfSSL | 0:9d17e4342598 | 475 | for (x = c->used - 1; x >= 0; x--) { |
wolfSSL | 0:9d17e4342598 | 476 | /* get the lower bits of this word in a temp */ |
wolfSSL | 0:9d17e4342598 | 477 | rr = *tmpc & mask; |
wolfSSL | 0:9d17e4342598 | 478 | |
wolfSSL | 0:9d17e4342598 | 479 | /* shift the current word and mix in the carry bits from previous word */ |
wolfSSL | 0:9d17e4342598 | 480 | *tmpc = (*tmpc >> D) | (r << shift); |
wolfSSL | 0:9d17e4342598 | 481 | --tmpc; |
wolfSSL | 0:9d17e4342598 | 482 | |
wolfSSL | 0:9d17e4342598 | 483 | /* set the carry to the carry bits of the current word found above */ |
wolfSSL | 0:9d17e4342598 | 484 | r = rr; |
wolfSSL | 0:9d17e4342598 | 485 | } |
wolfSSL | 0:9d17e4342598 | 486 | } |
wolfSSL | 0:9d17e4342598 | 487 | |
wolfSSL | 0:9d17e4342598 | 488 | |
wolfSSL | 0:9d17e4342598 | 489 | /* shift right a certain amount of digits */ |
wolfSSL | 0:9d17e4342598 | 490 | void mp_rshd (mp_int * a, int b) |
wolfSSL | 0:9d17e4342598 | 491 | { |
wolfSSL | 0:9d17e4342598 | 492 | int x; |
wolfSSL | 0:9d17e4342598 | 493 | |
wolfSSL | 0:9d17e4342598 | 494 | /* if b <= 0 then ignore it */ |
wolfSSL | 0:9d17e4342598 | 495 | if (b <= 0) { |
wolfSSL | 0:9d17e4342598 | 496 | return; |
wolfSSL | 0:9d17e4342598 | 497 | } |
wolfSSL | 0:9d17e4342598 | 498 | |
wolfSSL | 0:9d17e4342598 | 499 | /* if b > used then simply zero it and return */ |
wolfSSL | 0:9d17e4342598 | 500 | if (a->used <= b) { |
wolfSSL | 0:9d17e4342598 | 501 | mp_zero (a); |
wolfSSL | 0:9d17e4342598 | 502 | return; |
wolfSSL | 0:9d17e4342598 | 503 | } |
wolfSSL | 0:9d17e4342598 | 504 | |
wolfSSL | 0:9d17e4342598 | 505 | { |
wolfSSL | 0:9d17e4342598 | 506 | register mp_digit *bottom, *top; |
wolfSSL | 0:9d17e4342598 | 507 | |
wolfSSL | 0:9d17e4342598 | 508 | /* shift the digits down */ |
wolfSSL | 0:9d17e4342598 | 509 | |
wolfSSL | 0:9d17e4342598 | 510 | /* bottom */ |
wolfSSL | 0:9d17e4342598 | 511 | bottom = a->dp; |
wolfSSL | 0:9d17e4342598 | 512 | |
wolfSSL | 0:9d17e4342598 | 513 | /* top [offset into digits] */ |
wolfSSL | 0:9d17e4342598 | 514 | top = a->dp + b; |
wolfSSL | 0:9d17e4342598 | 515 | |
wolfSSL | 0:9d17e4342598 | 516 | /* this is implemented as a sliding window where |
wolfSSL | 0:9d17e4342598 | 517 | * the window is b-digits long and digits from |
wolfSSL | 0:9d17e4342598 | 518 | * the top of the window are copied to the bottom |
wolfSSL | 0:9d17e4342598 | 519 | * |
wolfSSL | 0:9d17e4342598 | 520 | * e.g. |
wolfSSL | 0:9d17e4342598 | 521 | |
wolfSSL | 0:9d17e4342598 | 522 | b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> |
wolfSSL | 0:9d17e4342598 | 523 | /\ | ----> |
wolfSSL | 0:9d17e4342598 | 524 | \-------------------/ ----> |
wolfSSL | 0:9d17e4342598 | 525 | */ |
wolfSSL | 0:9d17e4342598 | 526 | for (x = 0; x < (a->used - b); x++) { |
wolfSSL | 0:9d17e4342598 | 527 | *bottom++ = *top++; |
wolfSSL | 0:9d17e4342598 | 528 | } |
wolfSSL | 0:9d17e4342598 | 529 | |
wolfSSL | 0:9d17e4342598 | 530 | /* zero the top digits */ |
wolfSSL | 0:9d17e4342598 | 531 | for (; x < a->used; x++) { |
wolfSSL | 0:9d17e4342598 | 532 | *bottom++ = 0; |
wolfSSL | 0:9d17e4342598 | 533 | } |
wolfSSL | 0:9d17e4342598 | 534 | } |
wolfSSL | 0:9d17e4342598 | 535 | |
wolfSSL | 0:9d17e4342598 | 536 | /* remove excess digits */ |
wolfSSL | 0:9d17e4342598 | 537 | a->used -= b; |
wolfSSL | 0:9d17e4342598 | 538 | } |
wolfSSL | 0:9d17e4342598 | 539 | |
wolfSSL | 0:9d17e4342598 | 540 | |
wolfSSL | 0:9d17e4342598 | 541 | /* calc a value mod 2**b */ |
wolfSSL | 0:9d17e4342598 | 542 | int |
wolfSSL | 0:9d17e4342598 | 543 | mp_mod_2d (mp_int * a, int b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 544 | { |
wolfSSL | 0:9d17e4342598 | 545 | int x, res; |
wolfSSL | 0:9d17e4342598 | 546 | |
wolfSSL | 0:9d17e4342598 | 547 | /* if b is <= 0 then zero the int */ |
wolfSSL | 0:9d17e4342598 | 548 | if (b <= 0) { |
wolfSSL | 0:9d17e4342598 | 549 | mp_zero (c); |
wolfSSL | 0:9d17e4342598 | 550 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 551 | } |
wolfSSL | 0:9d17e4342598 | 552 | |
wolfSSL | 0:9d17e4342598 | 553 | /* if the modulus is larger than the value than return */ |
wolfSSL | 0:9d17e4342598 | 554 | if (b >= (int) (a->used * DIGIT_BIT)) { |
wolfSSL | 0:9d17e4342598 | 555 | res = mp_copy (a, c); |
wolfSSL | 0:9d17e4342598 | 556 | return res; |
wolfSSL | 0:9d17e4342598 | 557 | } |
wolfSSL | 0:9d17e4342598 | 558 | |
wolfSSL | 0:9d17e4342598 | 559 | /* copy */ |
wolfSSL | 0:9d17e4342598 | 560 | if ((res = mp_copy (a, c)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 561 | return res; |
wolfSSL | 0:9d17e4342598 | 562 | } |
wolfSSL | 0:9d17e4342598 | 563 | |
wolfSSL | 0:9d17e4342598 | 564 | /* zero digits above the last digit of the modulus */ |
wolfSSL | 0:9d17e4342598 | 565 | for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) { |
wolfSSL | 0:9d17e4342598 | 566 | c->dp[x] = 0; |
wolfSSL | 0:9d17e4342598 | 567 | } |
wolfSSL | 0:9d17e4342598 | 568 | /* clear the digit that is not completely outside/inside the modulus */ |
wolfSSL | 0:9d17e4342598 | 569 | c->dp[b / DIGIT_BIT] &= (mp_digit) ((((mp_digit) 1) << |
wolfSSL | 0:9d17e4342598 | 570 | (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); |
wolfSSL | 0:9d17e4342598 | 571 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 572 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 573 | } |
wolfSSL | 0:9d17e4342598 | 574 | |
wolfSSL | 0:9d17e4342598 | 575 | |
wolfSSL | 0:9d17e4342598 | 576 | /* reads a unsigned char array, assumes the msb is stored first [big endian] */ |
wolfSSL | 0:9d17e4342598 | 577 | int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c) |
wolfSSL | 0:9d17e4342598 | 578 | { |
wolfSSL | 0:9d17e4342598 | 579 | int res; |
wolfSSL | 0:9d17e4342598 | 580 | |
wolfSSL | 0:9d17e4342598 | 581 | /* make sure there are at least two digits */ |
wolfSSL | 0:9d17e4342598 | 582 | if (a->alloc < 2) { |
wolfSSL | 0:9d17e4342598 | 583 | if ((res = mp_grow(a, 2)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 584 | return res; |
wolfSSL | 0:9d17e4342598 | 585 | } |
wolfSSL | 0:9d17e4342598 | 586 | } |
wolfSSL | 0:9d17e4342598 | 587 | |
wolfSSL | 0:9d17e4342598 | 588 | /* zero the int */ |
wolfSSL | 0:9d17e4342598 | 589 | mp_zero (a); |
wolfSSL | 0:9d17e4342598 | 590 | |
wolfSSL | 0:9d17e4342598 | 591 | /* read the bytes in */ |
wolfSSL | 0:9d17e4342598 | 592 | while (c-- > 0) { |
wolfSSL | 0:9d17e4342598 | 593 | if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 594 | return res; |
wolfSSL | 0:9d17e4342598 | 595 | } |
wolfSSL | 0:9d17e4342598 | 596 | |
wolfSSL | 0:9d17e4342598 | 597 | #ifndef MP_8BIT |
wolfSSL | 0:9d17e4342598 | 598 | a->dp[0] |= *b++; |
wolfSSL | 0:9d17e4342598 | 599 | a->used += 1; |
wolfSSL | 0:9d17e4342598 | 600 | #else |
wolfSSL | 0:9d17e4342598 | 601 | a->dp[0] = (*b & MP_MASK); |
wolfSSL | 0:9d17e4342598 | 602 | a->dp[1] |= ((*b++ >> 7U) & 1); |
wolfSSL | 0:9d17e4342598 | 603 | a->used += 2; |
wolfSSL | 0:9d17e4342598 | 604 | #endif |
wolfSSL | 0:9d17e4342598 | 605 | } |
wolfSSL | 0:9d17e4342598 | 606 | mp_clamp (a); |
wolfSSL | 0:9d17e4342598 | 607 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 608 | } |
wolfSSL | 0:9d17e4342598 | 609 | |
wolfSSL | 0:9d17e4342598 | 610 | |
wolfSSL | 0:9d17e4342598 | 611 | /* shift left by a certain bit count */ |
wolfSSL | 0:9d17e4342598 | 612 | int mp_mul_2d (mp_int * a, int b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 613 | { |
wolfSSL | 0:9d17e4342598 | 614 | mp_digit d; |
wolfSSL | 0:9d17e4342598 | 615 | int res; |
wolfSSL | 0:9d17e4342598 | 616 | |
wolfSSL | 0:9d17e4342598 | 617 | /* copy */ |
wolfSSL | 0:9d17e4342598 | 618 | if (a != c) { |
wolfSSL | 0:9d17e4342598 | 619 | if ((res = mp_copy (a, c)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 620 | return res; |
wolfSSL | 0:9d17e4342598 | 621 | } |
wolfSSL | 0:9d17e4342598 | 622 | } |
wolfSSL | 0:9d17e4342598 | 623 | |
wolfSSL | 0:9d17e4342598 | 624 | if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) { |
wolfSSL | 0:9d17e4342598 | 625 | if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 626 | return res; |
wolfSSL | 0:9d17e4342598 | 627 | } |
wolfSSL | 0:9d17e4342598 | 628 | } |
wolfSSL | 0:9d17e4342598 | 629 | |
wolfSSL | 0:9d17e4342598 | 630 | /* shift by as many digits in the bit count */ |
wolfSSL | 0:9d17e4342598 | 631 | if (b >= (int)DIGIT_BIT) { |
wolfSSL | 0:9d17e4342598 | 632 | if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 633 | return res; |
wolfSSL | 0:9d17e4342598 | 634 | } |
wolfSSL | 0:9d17e4342598 | 635 | } |
wolfSSL | 0:9d17e4342598 | 636 | |
wolfSSL | 0:9d17e4342598 | 637 | /* shift any bit count < DIGIT_BIT */ |
wolfSSL | 0:9d17e4342598 | 638 | d = (mp_digit) (b % DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 639 | if (d != 0) { |
wolfSSL | 0:9d17e4342598 | 640 | register mp_digit *tmpc, shift, mask, r, rr; |
wolfSSL | 0:9d17e4342598 | 641 | register int x; |
wolfSSL | 0:9d17e4342598 | 642 | |
wolfSSL | 0:9d17e4342598 | 643 | /* bitmask for carries */ |
wolfSSL | 0:9d17e4342598 | 644 | mask = (((mp_digit)1) << d) - 1; |
wolfSSL | 0:9d17e4342598 | 645 | |
wolfSSL | 0:9d17e4342598 | 646 | /* shift for msbs */ |
wolfSSL | 0:9d17e4342598 | 647 | shift = DIGIT_BIT - d; |
wolfSSL | 0:9d17e4342598 | 648 | |
wolfSSL | 0:9d17e4342598 | 649 | /* alias */ |
wolfSSL | 0:9d17e4342598 | 650 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 651 | |
wolfSSL | 0:9d17e4342598 | 652 | /* carry */ |
wolfSSL | 0:9d17e4342598 | 653 | r = 0; |
wolfSSL | 0:9d17e4342598 | 654 | for (x = 0; x < c->used; x++) { |
wolfSSL | 0:9d17e4342598 | 655 | /* get the higher bits of the current word */ |
wolfSSL | 0:9d17e4342598 | 656 | rr = (*tmpc >> shift) & mask; |
wolfSSL | 0:9d17e4342598 | 657 | |
wolfSSL | 0:9d17e4342598 | 658 | /* shift the current word and OR in the carry */ |
wolfSSL | 0:9d17e4342598 | 659 | *tmpc = ((*tmpc << d) | r) & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 660 | ++tmpc; |
wolfSSL | 0:9d17e4342598 | 661 | |
wolfSSL | 0:9d17e4342598 | 662 | /* set the carry to the carry bits of the current word */ |
wolfSSL | 0:9d17e4342598 | 663 | r = rr; |
wolfSSL | 0:9d17e4342598 | 664 | } |
wolfSSL | 0:9d17e4342598 | 665 | |
wolfSSL | 0:9d17e4342598 | 666 | /* set final carry */ |
wolfSSL | 0:9d17e4342598 | 667 | if (r != 0) { |
wolfSSL | 0:9d17e4342598 | 668 | c->dp[(c->used)++] = r; |
wolfSSL | 0:9d17e4342598 | 669 | } |
wolfSSL | 0:9d17e4342598 | 670 | } |
wolfSSL | 0:9d17e4342598 | 671 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 672 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 673 | } |
wolfSSL | 0:9d17e4342598 | 674 | |
wolfSSL | 0:9d17e4342598 | 675 | |
wolfSSL | 0:9d17e4342598 | 676 | /* shift left a certain amount of digits */ |
wolfSSL | 0:9d17e4342598 | 677 | int mp_lshd (mp_int * a, int b) |
wolfSSL | 0:9d17e4342598 | 678 | { |
wolfSSL | 0:9d17e4342598 | 679 | int x, res; |
wolfSSL | 0:9d17e4342598 | 680 | |
wolfSSL | 0:9d17e4342598 | 681 | /* if its less than zero return */ |
wolfSSL | 0:9d17e4342598 | 682 | if (b <= 0) { |
wolfSSL | 0:9d17e4342598 | 683 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 684 | } |
wolfSSL | 0:9d17e4342598 | 685 | |
wolfSSL | 0:9d17e4342598 | 686 | /* grow to fit the new digits */ |
wolfSSL | 0:9d17e4342598 | 687 | if (a->alloc < a->used + b) { |
wolfSSL | 0:9d17e4342598 | 688 | if ((res = mp_grow (a, a->used + b)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 689 | return res; |
wolfSSL | 0:9d17e4342598 | 690 | } |
wolfSSL | 0:9d17e4342598 | 691 | } |
wolfSSL | 0:9d17e4342598 | 692 | |
wolfSSL | 0:9d17e4342598 | 693 | { |
wolfSSL | 0:9d17e4342598 | 694 | register mp_digit *top, *bottom; |
wolfSSL | 0:9d17e4342598 | 695 | |
wolfSSL | 0:9d17e4342598 | 696 | /* increment the used by the shift amount then copy upwards */ |
wolfSSL | 0:9d17e4342598 | 697 | a->used += b; |
wolfSSL | 0:9d17e4342598 | 698 | |
wolfSSL | 0:9d17e4342598 | 699 | /* top */ |
wolfSSL | 0:9d17e4342598 | 700 | top = a->dp + a->used - 1; |
wolfSSL | 0:9d17e4342598 | 701 | |
wolfSSL | 0:9d17e4342598 | 702 | /* base */ |
wolfSSL | 0:9d17e4342598 | 703 | bottom = a->dp + a->used - 1 - b; |
wolfSSL | 0:9d17e4342598 | 704 | |
wolfSSL | 0:9d17e4342598 | 705 | /* much like mp_rshd this is implemented using a sliding window |
wolfSSL | 0:9d17e4342598 | 706 | * except the window goes the otherway around. Copying from |
wolfSSL | 0:9d17e4342598 | 707 | * the bottom to the top. see bn_mp_rshd.c for more info. |
wolfSSL | 0:9d17e4342598 | 708 | */ |
wolfSSL | 0:9d17e4342598 | 709 | for (x = a->used - 1; x >= b; x--) { |
wolfSSL | 0:9d17e4342598 | 710 | *top-- = *bottom--; |
wolfSSL | 0:9d17e4342598 | 711 | } |
wolfSSL | 0:9d17e4342598 | 712 | |
wolfSSL | 0:9d17e4342598 | 713 | /* zero the lower digits */ |
wolfSSL | 0:9d17e4342598 | 714 | top = a->dp; |
wolfSSL | 0:9d17e4342598 | 715 | for (x = 0; x < b; x++) { |
wolfSSL | 0:9d17e4342598 | 716 | *top++ = 0; |
wolfSSL | 0:9d17e4342598 | 717 | } |
wolfSSL | 0:9d17e4342598 | 718 | } |
wolfSSL | 0:9d17e4342598 | 719 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 720 | } |
wolfSSL | 0:9d17e4342598 | 721 | |
wolfSSL | 0:9d17e4342598 | 722 | |
wolfSSL | 0:9d17e4342598 | 723 | /* this is a shell function that calls either the normal or Montgomery |
wolfSSL | 0:9d17e4342598 | 724 | * exptmod functions. Originally the call to the montgomery code was |
wolfSSL | 0:9d17e4342598 | 725 | * embedded in the normal function but that wasted alot of stack space |
wolfSSL | 0:9d17e4342598 | 726 | * for nothing (since 99% of the time the Montgomery code would be called) |
wolfSSL | 0:9d17e4342598 | 727 | */ |
wolfSSL | 0:9d17e4342598 | 728 | int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) |
wolfSSL | 0:9d17e4342598 | 729 | { |
wolfSSL | 0:9d17e4342598 | 730 | int dr; |
wolfSSL | 0:9d17e4342598 | 731 | |
wolfSSL | 0:9d17e4342598 | 732 | /* modulus P must be positive */ |
wolfSSL | 0:9d17e4342598 | 733 | if (P->sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 734 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 735 | } |
wolfSSL | 0:9d17e4342598 | 736 | |
wolfSSL | 0:9d17e4342598 | 737 | /* if exponent X is negative we have to recurse */ |
wolfSSL | 0:9d17e4342598 | 738 | if (X->sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 739 | #ifdef BN_MP_INVMOD_C |
wolfSSL | 0:9d17e4342598 | 740 | mp_int tmpG, tmpX; |
wolfSSL | 0:9d17e4342598 | 741 | int err; |
wolfSSL | 0:9d17e4342598 | 742 | |
wolfSSL | 0:9d17e4342598 | 743 | /* first compute 1/G mod P */ |
wolfSSL | 0:9d17e4342598 | 744 | if ((err = mp_init(&tmpG)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 745 | return err; |
wolfSSL | 0:9d17e4342598 | 746 | } |
wolfSSL | 0:9d17e4342598 | 747 | if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 748 | mp_clear(&tmpG); |
wolfSSL | 0:9d17e4342598 | 749 | return err; |
wolfSSL | 0:9d17e4342598 | 750 | } |
wolfSSL | 0:9d17e4342598 | 751 | |
wolfSSL | 0:9d17e4342598 | 752 | /* now get |X| */ |
wolfSSL | 0:9d17e4342598 | 753 | if ((err = mp_init(&tmpX)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 754 | mp_clear(&tmpG); |
wolfSSL | 0:9d17e4342598 | 755 | return err; |
wolfSSL | 0:9d17e4342598 | 756 | } |
wolfSSL | 0:9d17e4342598 | 757 | if ((err = mp_abs(X, &tmpX)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 758 | mp_clear(&tmpG); |
wolfSSL | 0:9d17e4342598 | 759 | mp_clear(&tmpX); |
wolfSSL | 0:9d17e4342598 | 760 | return err; |
wolfSSL | 0:9d17e4342598 | 761 | } |
wolfSSL | 0:9d17e4342598 | 762 | |
wolfSSL | 0:9d17e4342598 | 763 | /* and now compute (1/G)**|X| instead of G**X [X < 0] */ |
wolfSSL | 0:9d17e4342598 | 764 | err = mp_exptmod(&tmpG, &tmpX, P, Y); |
wolfSSL | 0:9d17e4342598 | 765 | mp_clear(&tmpG); |
wolfSSL | 0:9d17e4342598 | 766 | mp_clear(&tmpX); |
wolfSSL | 0:9d17e4342598 | 767 | return err; |
wolfSSL | 0:9d17e4342598 | 768 | #else |
wolfSSL | 0:9d17e4342598 | 769 | /* no invmod */ |
wolfSSL | 0:9d17e4342598 | 770 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 771 | #endif |
wolfSSL | 0:9d17e4342598 | 772 | } |
wolfSSL | 0:9d17e4342598 | 773 | |
wolfSSL | 0:9d17e4342598 | 774 | /* modified diminished radix reduction */ |
wolfSSL | 0:9d17e4342598 | 775 | #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && \ |
wolfSSL | 0:9d17e4342598 | 776 | defined(BN_S_MP_EXPTMOD_C) |
wolfSSL | 0:9d17e4342598 | 777 | if (mp_reduce_is_2k_l(P) == MP_YES) { |
wolfSSL | 0:9d17e4342598 | 778 | return s_mp_exptmod(G, X, P, Y, 1); |
wolfSSL | 0:9d17e4342598 | 779 | } |
wolfSSL | 0:9d17e4342598 | 780 | #endif |
wolfSSL | 0:9d17e4342598 | 781 | |
wolfSSL | 0:9d17e4342598 | 782 | #ifdef BN_MP_DR_IS_MODULUS_C |
wolfSSL | 0:9d17e4342598 | 783 | /* is it a DR modulus? */ |
wolfSSL | 0:9d17e4342598 | 784 | dr = mp_dr_is_modulus(P); |
wolfSSL | 0:9d17e4342598 | 785 | #else |
wolfSSL | 0:9d17e4342598 | 786 | /* default to no */ |
wolfSSL | 0:9d17e4342598 | 787 | dr = 0; |
wolfSSL | 0:9d17e4342598 | 788 | #endif |
wolfSSL | 0:9d17e4342598 | 789 | |
wolfSSL | 0:9d17e4342598 | 790 | #ifdef BN_MP_REDUCE_IS_2K_C |
wolfSSL | 0:9d17e4342598 | 791 | /* if not, is it a unrestricted DR modulus? */ |
wolfSSL | 0:9d17e4342598 | 792 | if (dr == 0) { |
wolfSSL | 0:9d17e4342598 | 793 | dr = mp_reduce_is_2k(P) << 1; |
wolfSSL | 0:9d17e4342598 | 794 | } |
wolfSSL | 0:9d17e4342598 | 795 | #endif |
wolfSSL | 0:9d17e4342598 | 796 | |
wolfSSL | 0:9d17e4342598 | 797 | /* if the modulus is odd or dr != 0 use the montgomery method */ |
wolfSSL | 0:9d17e4342598 | 798 | #ifdef BN_MP_EXPTMOD_FAST_C |
wolfSSL | 0:9d17e4342598 | 799 | if (mp_isodd (P) == 1 || dr != 0) { |
wolfSSL | 0:9d17e4342598 | 800 | return mp_exptmod_fast (G, X, P, Y, dr); |
wolfSSL | 0:9d17e4342598 | 801 | } else { |
wolfSSL | 0:9d17e4342598 | 802 | #endif |
wolfSSL | 0:9d17e4342598 | 803 | #ifdef BN_S_MP_EXPTMOD_C |
wolfSSL | 0:9d17e4342598 | 804 | /* otherwise use the generic Barrett reduction technique */ |
wolfSSL | 0:9d17e4342598 | 805 | return s_mp_exptmod (G, X, P, Y, 0); |
wolfSSL | 0:9d17e4342598 | 806 | #else |
wolfSSL | 0:9d17e4342598 | 807 | /* no exptmod for evens */ |
wolfSSL | 0:9d17e4342598 | 808 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 809 | #endif |
wolfSSL | 0:9d17e4342598 | 810 | #ifdef BN_MP_EXPTMOD_FAST_C |
wolfSSL | 0:9d17e4342598 | 811 | } |
wolfSSL | 0:9d17e4342598 | 812 | #endif |
wolfSSL | 0:9d17e4342598 | 813 | } |
wolfSSL | 0:9d17e4342598 | 814 | |
wolfSSL | 0:9d17e4342598 | 815 | |
wolfSSL | 0:9d17e4342598 | 816 | /* b = |a| |
wolfSSL | 0:9d17e4342598 | 817 | * |
wolfSSL | 0:9d17e4342598 | 818 | * Simple function copies the input and fixes the sign to positive |
wolfSSL | 0:9d17e4342598 | 819 | */ |
wolfSSL | 0:9d17e4342598 | 820 | int |
wolfSSL | 0:9d17e4342598 | 821 | mp_abs (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 822 | { |
wolfSSL | 0:9d17e4342598 | 823 | int res; |
wolfSSL | 0:9d17e4342598 | 824 | |
wolfSSL | 0:9d17e4342598 | 825 | /* copy a to b */ |
wolfSSL | 0:9d17e4342598 | 826 | if (a != b) { |
wolfSSL | 0:9d17e4342598 | 827 | if ((res = mp_copy (a, b)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 828 | return res; |
wolfSSL | 0:9d17e4342598 | 829 | } |
wolfSSL | 0:9d17e4342598 | 830 | } |
wolfSSL | 0:9d17e4342598 | 831 | |
wolfSSL | 0:9d17e4342598 | 832 | /* force the sign of b to positive */ |
wolfSSL | 0:9d17e4342598 | 833 | b->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 834 | |
wolfSSL | 0:9d17e4342598 | 835 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 836 | } |
wolfSSL | 0:9d17e4342598 | 837 | |
wolfSSL | 0:9d17e4342598 | 838 | |
wolfSSL | 0:9d17e4342598 | 839 | /* hac 14.61, pp608 */ |
wolfSSL | 0:9d17e4342598 | 840 | int mp_invmod (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 841 | { |
wolfSSL | 0:9d17e4342598 | 842 | /* b cannot be negative */ |
wolfSSL | 0:9d17e4342598 | 843 | if (b->sign == MP_NEG || mp_iszero(b) == 1) { |
wolfSSL | 0:9d17e4342598 | 844 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 845 | } |
wolfSSL | 0:9d17e4342598 | 846 | |
wolfSSL | 0:9d17e4342598 | 847 | #ifdef BN_FAST_MP_INVMOD_C |
wolfSSL | 0:9d17e4342598 | 848 | /* if the modulus is odd we can use a faster routine instead */ |
wolfSSL | 0:9d17e4342598 | 849 | if (mp_isodd (b) == 1) { |
wolfSSL | 0:9d17e4342598 | 850 | return fast_mp_invmod (a, b, c); |
wolfSSL | 0:9d17e4342598 | 851 | } |
wolfSSL | 0:9d17e4342598 | 852 | #endif |
wolfSSL | 0:9d17e4342598 | 853 | |
wolfSSL | 0:9d17e4342598 | 854 | #ifdef BN_MP_INVMOD_SLOW_C |
wolfSSL | 0:9d17e4342598 | 855 | return mp_invmod_slow(a, b, c); |
wolfSSL | 0:9d17e4342598 | 856 | #endif |
wolfSSL | 0:9d17e4342598 | 857 | } |
wolfSSL | 0:9d17e4342598 | 858 | |
wolfSSL | 0:9d17e4342598 | 859 | |
wolfSSL | 0:9d17e4342598 | 860 | /* computes the modular inverse via binary extended euclidean algorithm, |
wolfSSL | 0:9d17e4342598 | 861 | * that is c = 1/a mod b |
wolfSSL | 0:9d17e4342598 | 862 | * |
wolfSSL | 0:9d17e4342598 | 863 | * Based on slow invmod except this is optimized for the case where b is |
wolfSSL | 0:9d17e4342598 | 864 | * odd as per HAC Note 14.64 on pp. 610 |
wolfSSL | 0:9d17e4342598 | 865 | */ |
wolfSSL | 0:9d17e4342598 | 866 | int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 867 | { |
wolfSSL | 0:9d17e4342598 | 868 | mp_int x, y, u, v, B, D; |
wolfSSL | 0:9d17e4342598 | 869 | int res, neg; |
wolfSSL | 0:9d17e4342598 | 870 | |
wolfSSL | 0:9d17e4342598 | 871 | /* 2. [modified] b must be odd */ |
wolfSSL | 0:9d17e4342598 | 872 | if (mp_iseven (b) == 1) { |
wolfSSL | 0:9d17e4342598 | 873 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 874 | } |
wolfSSL | 0:9d17e4342598 | 875 | |
wolfSSL | 0:9d17e4342598 | 876 | /* init all our temps */ |
wolfSSL | 0:9d17e4342598 | 877 | if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 878 | return res; |
wolfSSL | 0:9d17e4342598 | 879 | } |
wolfSSL | 0:9d17e4342598 | 880 | |
wolfSSL | 0:9d17e4342598 | 881 | /* x == modulus, y == value to invert */ |
wolfSSL | 0:9d17e4342598 | 882 | if ((res = mp_copy (b, &x)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 883 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 884 | } |
wolfSSL | 0:9d17e4342598 | 885 | |
wolfSSL | 0:9d17e4342598 | 886 | /* we need y = |a| */ |
wolfSSL | 0:9d17e4342598 | 887 | if ((res = mp_mod (a, b, &y)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 888 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 889 | } |
wolfSSL | 0:9d17e4342598 | 890 | |
wolfSSL | 0:9d17e4342598 | 891 | /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ |
wolfSSL | 0:9d17e4342598 | 892 | if ((res = mp_copy (&x, &u)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 893 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 894 | } |
wolfSSL | 0:9d17e4342598 | 895 | if ((res = mp_copy (&y, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 896 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 897 | } |
wolfSSL | 0:9d17e4342598 | 898 | mp_set (&D, 1); |
wolfSSL | 0:9d17e4342598 | 899 | |
wolfSSL | 0:9d17e4342598 | 900 | top: |
wolfSSL | 0:9d17e4342598 | 901 | /* 4. while u is even do */ |
wolfSSL | 0:9d17e4342598 | 902 | while (mp_iseven (&u) == 1) { |
wolfSSL | 0:9d17e4342598 | 903 | /* 4.1 u = u/2 */ |
wolfSSL | 0:9d17e4342598 | 904 | if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 905 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 906 | } |
wolfSSL | 0:9d17e4342598 | 907 | /* 4.2 if B is odd then */ |
wolfSSL | 0:9d17e4342598 | 908 | if (mp_isodd (&B) == 1) { |
wolfSSL | 0:9d17e4342598 | 909 | if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 910 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 911 | } |
wolfSSL | 0:9d17e4342598 | 912 | } |
wolfSSL | 0:9d17e4342598 | 913 | /* B = B/2 */ |
wolfSSL | 0:9d17e4342598 | 914 | if ((res = mp_div_2 (&B, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 915 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 916 | } |
wolfSSL | 0:9d17e4342598 | 917 | } |
wolfSSL | 0:9d17e4342598 | 918 | |
wolfSSL | 0:9d17e4342598 | 919 | /* 5. while v is even do */ |
wolfSSL | 0:9d17e4342598 | 920 | while (mp_iseven (&v) == 1) { |
wolfSSL | 0:9d17e4342598 | 921 | /* 5.1 v = v/2 */ |
wolfSSL | 0:9d17e4342598 | 922 | if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 923 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 924 | } |
wolfSSL | 0:9d17e4342598 | 925 | /* 5.2 if D is odd then */ |
wolfSSL | 0:9d17e4342598 | 926 | if (mp_isodd (&D) == 1) { |
wolfSSL | 0:9d17e4342598 | 927 | /* D = (D-x)/2 */ |
wolfSSL | 0:9d17e4342598 | 928 | if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 929 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 930 | } |
wolfSSL | 0:9d17e4342598 | 931 | } |
wolfSSL | 0:9d17e4342598 | 932 | /* D = D/2 */ |
wolfSSL | 0:9d17e4342598 | 933 | if ((res = mp_div_2 (&D, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 934 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 935 | } |
wolfSSL | 0:9d17e4342598 | 936 | } |
wolfSSL | 0:9d17e4342598 | 937 | |
wolfSSL | 0:9d17e4342598 | 938 | /* 6. if u >= v then */ |
wolfSSL | 0:9d17e4342598 | 939 | if (mp_cmp (&u, &v) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 940 | /* u = u - v, B = B - D */ |
wolfSSL | 0:9d17e4342598 | 941 | if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 942 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 943 | } |
wolfSSL | 0:9d17e4342598 | 944 | |
wolfSSL | 0:9d17e4342598 | 945 | if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 946 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 947 | } |
wolfSSL | 0:9d17e4342598 | 948 | } else { |
wolfSSL | 0:9d17e4342598 | 949 | /* v - v - u, D = D - B */ |
wolfSSL | 0:9d17e4342598 | 950 | if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 951 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 952 | } |
wolfSSL | 0:9d17e4342598 | 953 | |
wolfSSL | 0:9d17e4342598 | 954 | if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 955 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 956 | } |
wolfSSL | 0:9d17e4342598 | 957 | } |
wolfSSL | 0:9d17e4342598 | 958 | |
wolfSSL | 0:9d17e4342598 | 959 | /* if not zero goto step 4 */ |
wolfSSL | 0:9d17e4342598 | 960 | if (mp_iszero (&u) == 0) { |
wolfSSL | 0:9d17e4342598 | 961 | goto top; |
wolfSSL | 0:9d17e4342598 | 962 | } |
wolfSSL | 0:9d17e4342598 | 963 | |
wolfSSL | 0:9d17e4342598 | 964 | /* now a = C, b = D, gcd == g*v */ |
wolfSSL | 0:9d17e4342598 | 965 | |
wolfSSL | 0:9d17e4342598 | 966 | /* if v != 1 then there is no inverse */ |
wolfSSL | 0:9d17e4342598 | 967 | if (mp_cmp_d (&v, 1) != MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 968 | res = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 969 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 970 | } |
wolfSSL | 0:9d17e4342598 | 971 | |
wolfSSL | 0:9d17e4342598 | 972 | /* b is now the inverse */ |
wolfSSL | 0:9d17e4342598 | 973 | neg = a->sign; |
wolfSSL | 0:9d17e4342598 | 974 | while (D.sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 975 | if ((res = mp_add (&D, b, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 976 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 977 | } |
wolfSSL | 0:9d17e4342598 | 978 | } |
wolfSSL | 0:9d17e4342598 | 979 | mp_exch (&D, c); |
wolfSSL | 0:9d17e4342598 | 980 | c->sign = neg; |
wolfSSL | 0:9d17e4342598 | 981 | res = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 982 | |
wolfSSL | 0:9d17e4342598 | 983 | LBL_ERR:mp_clear(&x); |
wolfSSL | 0:9d17e4342598 | 984 | mp_clear(&y); |
wolfSSL | 0:9d17e4342598 | 985 | mp_clear(&u); |
wolfSSL | 0:9d17e4342598 | 986 | mp_clear(&v); |
wolfSSL | 0:9d17e4342598 | 987 | mp_clear(&B); |
wolfSSL | 0:9d17e4342598 | 988 | mp_clear(&D); |
wolfSSL | 0:9d17e4342598 | 989 | return res; |
wolfSSL | 0:9d17e4342598 | 990 | } |
wolfSSL | 0:9d17e4342598 | 991 | |
wolfSSL | 0:9d17e4342598 | 992 | |
wolfSSL | 0:9d17e4342598 | 993 | /* hac 14.61, pp608 */ |
wolfSSL | 0:9d17e4342598 | 994 | int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 995 | { |
wolfSSL | 0:9d17e4342598 | 996 | mp_int x, y, u, v, A, B, C, D; |
wolfSSL | 0:9d17e4342598 | 997 | int res; |
wolfSSL | 0:9d17e4342598 | 998 | |
wolfSSL | 0:9d17e4342598 | 999 | /* b cannot be negative */ |
wolfSSL | 0:9d17e4342598 | 1000 | if (b->sign == MP_NEG || mp_iszero(b) == 1) { |
wolfSSL | 0:9d17e4342598 | 1001 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1002 | } |
wolfSSL | 0:9d17e4342598 | 1003 | |
wolfSSL | 0:9d17e4342598 | 1004 | /* init temps */ |
wolfSSL | 0:9d17e4342598 | 1005 | if ((res = mp_init_multi(&x, &y, &u, &v, |
wolfSSL | 0:9d17e4342598 | 1006 | &A, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1007 | return res; |
wolfSSL | 0:9d17e4342598 | 1008 | } |
wolfSSL | 0:9d17e4342598 | 1009 | |
wolfSSL | 0:9d17e4342598 | 1010 | /* init rest of tmps temps */ |
wolfSSL | 0:9d17e4342598 | 1011 | if ((res = mp_init_multi(&C, &D, 0, 0, 0, 0)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1012 | return res; |
wolfSSL | 0:9d17e4342598 | 1013 | } |
wolfSSL | 0:9d17e4342598 | 1014 | |
wolfSSL | 0:9d17e4342598 | 1015 | /* x = a, y = b */ |
wolfSSL | 0:9d17e4342598 | 1016 | if ((res = mp_mod(a, b, &x)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1017 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1018 | } |
wolfSSL | 0:9d17e4342598 | 1019 | if ((res = mp_copy (b, &y)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1020 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1021 | } |
wolfSSL | 0:9d17e4342598 | 1022 | |
wolfSSL | 0:9d17e4342598 | 1023 | /* 2. [modified] if x,y are both even then return an error! */ |
wolfSSL | 0:9d17e4342598 | 1024 | if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) { |
wolfSSL | 0:9d17e4342598 | 1025 | res = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1026 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1027 | } |
wolfSSL | 0:9d17e4342598 | 1028 | |
wolfSSL | 0:9d17e4342598 | 1029 | /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ |
wolfSSL | 0:9d17e4342598 | 1030 | if ((res = mp_copy (&x, &u)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1031 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1032 | } |
wolfSSL | 0:9d17e4342598 | 1033 | if ((res = mp_copy (&y, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1034 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1035 | } |
wolfSSL | 0:9d17e4342598 | 1036 | mp_set (&A, 1); |
wolfSSL | 0:9d17e4342598 | 1037 | mp_set (&D, 1); |
wolfSSL | 0:9d17e4342598 | 1038 | |
wolfSSL | 0:9d17e4342598 | 1039 | top: |
wolfSSL | 0:9d17e4342598 | 1040 | /* 4. while u is even do */ |
wolfSSL | 0:9d17e4342598 | 1041 | while (mp_iseven (&u) == 1) { |
wolfSSL | 0:9d17e4342598 | 1042 | /* 4.1 u = u/2 */ |
wolfSSL | 0:9d17e4342598 | 1043 | if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1044 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1045 | } |
wolfSSL | 0:9d17e4342598 | 1046 | /* 4.2 if A or B is odd then */ |
wolfSSL | 0:9d17e4342598 | 1047 | if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) { |
wolfSSL | 0:9d17e4342598 | 1048 | /* A = (A+y)/2, B = (B-x)/2 */ |
wolfSSL | 0:9d17e4342598 | 1049 | if ((res = mp_add (&A, &y, &A)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1050 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1051 | } |
wolfSSL | 0:9d17e4342598 | 1052 | if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1053 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1054 | } |
wolfSSL | 0:9d17e4342598 | 1055 | } |
wolfSSL | 0:9d17e4342598 | 1056 | /* A = A/2, B = B/2 */ |
wolfSSL | 0:9d17e4342598 | 1057 | if ((res = mp_div_2 (&A, &A)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1058 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1059 | } |
wolfSSL | 0:9d17e4342598 | 1060 | if ((res = mp_div_2 (&B, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1061 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1062 | } |
wolfSSL | 0:9d17e4342598 | 1063 | } |
wolfSSL | 0:9d17e4342598 | 1064 | |
wolfSSL | 0:9d17e4342598 | 1065 | /* 5. while v is even do */ |
wolfSSL | 0:9d17e4342598 | 1066 | while (mp_iseven (&v) == 1) { |
wolfSSL | 0:9d17e4342598 | 1067 | /* 5.1 v = v/2 */ |
wolfSSL | 0:9d17e4342598 | 1068 | if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1069 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1070 | } |
wolfSSL | 0:9d17e4342598 | 1071 | /* 5.2 if C or D is odd then */ |
wolfSSL | 0:9d17e4342598 | 1072 | if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) { |
wolfSSL | 0:9d17e4342598 | 1073 | /* C = (C+y)/2, D = (D-x)/2 */ |
wolfSSL | 0:9d17e4342598 | 1074 | if ((res = mp_add (&C, &y, &C)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1075 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1076 | } |
wolfSSL | 0:9d17e4342598 | 1077 | if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1078 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1079 | } |
wolfSSL | 0:9d17e4342598 | 1080 | } |
wolfSSL | 0:9d17e4342598 | 1081 | /* C = C/2, D = D/2 */ |
wolfSSL | 0:9d17e4342598 | 1082 | if ((res = mp_div_2 (&C, &C)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1083 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1084 | } |
wolfSSL | 0:9d17e4342598 | 1085 | if ((res = mp_div_2 (&D, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1086 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1087 | } |
wolfSSL | 0:9d17e4342598 | 1088 | } |
wolfSSL | 0:9d17e4342598 | 1089 | |
wolfSSL | 0:9d17e4342598 | 1090 | /* 6. if u >= v then */ |
wolfSSL | 0:9d17e4342598 | 1091 | if (mp_cmp (&u, &v) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 1092 | /* u = u - v, A = A - C, B = B - D */ |
wolfSSL | 0:9d17e4342598 | 1093 | if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1094 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1095 | } |
wolfSSL | 0:9d17e4342598 | 1096 | |
wolfSSL | 0:9d17e4342598 | 1097 | if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1098 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1099 | } |
wolfSSL | 0:9d17e4342598 | 1100 | |
wolfSSL | 0:9d17e4342598 | 1101 | if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1102 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1103 | } |
wolfSSL | 0:9d17e4342598 | 1104 | } else { |
wolfSSL | 0:9d17e4342598 | 1105 | /* v - v - u, C = C - A, D = D - B */ |
wolfSSL | 0:9d17e4342598 | 1106 | if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1107 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1108 | } |
wolfSSL | 0:9d17e4342598 | 1109 | |
wolfSSL | 0:9d17e4342598 | 1110 | if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1111 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1112 | } |
wolfSSL | 0:9d17e4342598 | 1113 | |
wolfSSL | 0:9d17e4342598 | 1114 | if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1115 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1116 | } |
wolfSSL | 0:9d17e4342598 | 1117 | } |
wolfSSL | 0:9d17e4342598 | 1118 | |
wolfSSL | 0:9d17e4342598 | 1119 | /* if not zero goto step 4 */ |
wolfSSL | 0:9d17e4342598 | 1120 | if (mp_iszero (&u) == 0) |
wolfSSL | 0:9d17e4342598 | 1121 | goto top; |
wolfSSL | 0:9d17e4342598 | 1122 | |
wolfSSL | 0:9d17e4342598 | 1123 | /* now a = C, b = D, gcd == g*v */ |
wolfSSL | 0:9d17e4342598 | 1124 | |
wolfSSL | 0:9d17e4342598 | 1125 | /* if v != 1 then there is no inverse */ |
wolfSSL | 0:9d17e4342598 | 1126 | if (mp_cmp_d (&v, 1) != MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 1127 | res = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1128 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1129 | } |
wolfSSL | 0:9d17e4342598 | 1130 | |
wolfSSL | 0:9d17e4342598 | 1131 | /* if its too low */ |
wolfSSL | 0:9d17e4342598 | 1132 | while (mp_cmp_d(&C, 0) == MP_LT) { |
wolfSSL | 0:9d17e4342598 | 1133 | if ((res = mp_add(&C, b, &C)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1134 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1135 | } |
wolfSSL | 0:9d17e4342598 | 1136 | } |
wolfSSL | 0:9d17e4342598 | 1137 | |
wolfSSL | 0:9d17e4342598 | 1138 | /* too big */ |
wolfSSL | 0:9d17e4342598 | 1139 | while (mp_cmp_mag(&C, b) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 1140 | if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1141 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1142 | } |
wolfSSL | 0:9d17e4342598 | 1143 | } |
wolfSSL | 0:9d17e4342598 | 1144 | |
wolfSSL | 0:9d17e4342598 | 1145 | /* C is now the inverse */ |
wolfSSL | 0:9d17e4342598 | 1146 | mp_exch (&C, c); |
wolfSSL | 0:9d17e4342598 | 1147 | res = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1148 | LBL_ERR:mp_clear(&x); |
wolfSSL | 0:9d17e4342598 | 1149 | mp_clear(&y); |
wolfSSL | 0:9d17e4342598 | 1150 | mp_clear(&u); |
wolfSSL | 0:9d17e4342598 | 1151 | mp_clear(&v); |
wolfSSL | 0:9d17e4342598 | 1152 | mp_clear(&A); |
wolfSSL | 0:9d17e4342598 | 1153 | mp_clear(&B); |
wolfSSL | 0:9d17e4342598 | 1154 | mp_clear(&C); |
wolfSSL | 0:9d17e4342598 | 1155 | mp_clear(&D); |
wolfSSL | 0:9d17e4342598 | 1156 | return res; |
wolfSSL | 0:9d17e4342598 | 1157 | } |
wolfSSL | 0:9d17e4342598 | 1158 | |
wolfSSL | 0:9d17e4342598 | 1159 | |
wolfSSL | 0:9d17e4342598 | 1160 | /* compare maginitude of two ints (unsigned) */ |
wolfSSL | 0:9d17e4342598 | 1161 | int mp_cmp_mag (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 1162 | { |
wolfSSL | 0:9d17e4342598 | 1163 | int n; |
wolfSSL | 0:9d17e4342598 | 1164 | mp_digit *tmpa, *tmpb; |
wolfSSL | 0:9d17e4342598 | 1165 | |
wolfSSL | 0:9d17e4342598 | 1166 | /* compare based on # of non-zero digits */ |
wolfSSL | 0:9d17e4342598 | 1167 | if (a->used > b->used) { |
wolfSSL | 0:9d17e4342598 | 1168 | return MP_GT; |
wolfSSL | 0:9d17e4342598 | 1169 | } |
wolfSSL | 0:9d17e4342598 | 1170 | |
wolfSSL | 0:9d17e4342598 | 1171 | if (a->used < b->used) { |
wolfSSL | 0:9d17e4342598 | 1172 | return MP_LT; |
wolfSSL | 0:9d17e4342598 | 1173 | } |
wolfSSL | 0:9d17e4342598 | 1174 | |
wolfSSL | 0:9d17e4342598 | 1175 | /* alias for a */ |
wolfSSL | 0:9d17e4342598 | 1176 | tmpa = a->dp + (a->used - 1); |
wolfSSL | 0:9d17e4342598 | 1177 | |
wolfSSL | 0:9d17e4342598 | 1178 | /* alias for b */ |
wolfSSL | 0:9d17e4342598 | 1179 | tmpb = b->dp + (a->used - 1); |
wolfSSL | 0:9d17e4342598 | 1180 | |
wolfSSL | 0:9d17e4342598 | 1181 | /* compare based on digits */ |
wolfSSL | 0:9d17e4342598 | 1182 | for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { |
wolfSSL | 0:9d17e4342598 | 1183 | if (*tmpa > *tmpb) { |
wolfSSL | 0:9d17e4342598 | 1184 | return MP_GT; |
wolfSSL | 0:9d17e4342598 | 1185 | } |
wolfSSL | 0:9d17e4342598 | 1186 | |
wolfSSL | 0:9d17e4342598 | 1187 | if (*tmpa < *tmpb) { |
wolfSSL | 0:9d17e4342598 | 1188 | return MP_LT; |
wolfSSL | 0:9d17e4342598 | 1189 | } |
wolfSSL | 0:9d17e4342598 | 1190 | } |
wolfSSL | 0:9d17e4342598 | 1191 | return MP_EQ; |
wolfSSL | 0:9d17e4342598 | 1192 | } |
wolfSSL | 0:9d17e4342598 | 1193 | |
wolfSSL | 0:9d17e4342598 | 1194 | |
wolfSSL | 0:9d17e4342598 | 1195 | /* compare two ints (signed)*/ |
wolfSSL | 0:9d17e4342598 | 1196 | int |
wolfSSL | 0:9d17e4342598 | 1197 | mp_cmp (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 1198 | { |
wolfSSL | 0:9d17e4342598 | 1199 | /* compare based on sign */ |
wolfSSL | 0:9d17e4342598 | 1200 | if (a->sign != b->sign) { |
wolfSSL | 0:9d17e4342598 | 1201 | if (a->sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 1202 | return MP_LT; |
wolfSSL | 0:9d17e4342598 | 1203 | } else { |
wolfSSL | 0:9d17e4342598 | 1204 | return MP_GT; |
wolfSSL | 0:9d17e4342598 | 1205 | } |
wolfSSL | 0:9d17e4342598 | 1206 | } |
wolfSSL | 0:9d17e4342598 | 1207 | |
wolfSSL | 0:9d17e4342598 | 1208 | /* compare digits */ |
wolfSSL | 0:9d17e4342598 | 1209 | if (a->sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 1210 | /* if negative compare opposite direction */ |
wolfSSL | 0:9d17e4342598 | 1211 | return mp_cmp_mag(b, a); |
wolfSSL | 0:9d17e4342598 | 1212 | } else { |
wolfSSL | 0:9d17e4342598 | 1213 | return mp_cmp_mag(a, b); |
wolfSSL | 0:9d17e4342598 | 1214 | } |
wolfSSL | 0:9d17e4342598 | 1215 | } |
wolfSSL | 0:9d17e4342598 | 1216 | |
wolfSSL | 0:9d17e4342598 | 1217 | |
wolfSSL | 0:9d17e4342598 | 1218 | /* compare a digit */ |
wolfSSL | 0:9d17e4342598 | 1219 | int mp_cmp_d(mp_int * a, mp_digit b) |
wolfSSL | 0:9d17e4342598 | 1220 | { |
wolfSSL | 0:9d17e4342598 | 1221 | /* compare based on sign */ |
wolfSSL | 0:9d17e4342598 | 1222 | if (a->sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 1223 | return MP_LT; |
wolfSSL | 0:9d17e4342598 | 1224 | } |
wolfSSL | 0:9d17e4342598 | 1225 | |
wolfSSL | 0:9d17e4342598 | 1226 | /* compare based on magnitude */ |
wolfSSL | 0:9d17e4342598 | 1227 | if (a->used > 1) { |
wolfSSL | 0:9d17e4342598 | 1228 | return MP_GT; |
wolfSSL | 0:9d17e4342598 | 1229 | } |
wolfSSL | 0:9d17e4342598 | 1230 | |
wolfSSL | 0:9d17e4342598 | 1231 | /* compare the only digit of a to b */ |
wolfSSL | 0:9d17e4342598 | 1232 | if (a->dp[0] > b) { |
wolfSSL | 0:9d17e4342598 | 1233 | return MP_GT; |
wolfSSL | 0:9d17e4342598 | 1234 | } else if (a->dp[0] < b) { |
wolfSSL | 0:9d17e4342598 | 1235 | return MP_LT; |
wolfSSL | 0:9d17e4342598 | 1236 | } else { |
wolfSSL | 0:9d17e4342598 | 1237 | return MP_EQ; |
wolfSSL | 0:9d17e4342598 | 1238 | } |
wolfSSL | 0:9d17e4342598 | 1239 | } |
wolfSSL | 0:9d17e4342598 | 1240 | |
wolfSSL | 0:9d17e4342598 | 1241 | |
wolfSSL | 0:9d17e4342598 | 1242 | /* set to a digit */ |
wolfSSL | 0:9d17e4342598 | 1243 | void mp_set (mp_int * a, mp_digit b) |
wolfSSL | 0:9d17e4342598 | 1244 | { |
wolfSSL | 0:9d17e4342598 | 1245 | mp_zero (a); |
wolfSSL | 0:9d17e4342598 | 1246 | a->dp[0] = b & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 1247 | a->used = (a->dp[0] != 0) ? 1 : 0; |
wolfSSL | 0:9d17e4342598 | 1248 | } |
wolfSSL | 0:9d17e4342598 | 1249 | |
wolfSSL | 0:9d17e4342598 | 1250 | |
wolfSSL | 0:9d17e4342598 | 1251 | /* c = a mod b, 0 <= c < b */ |
wolfSSL | 0:9d17e4342598 | 1252 | int |
wolfSSL | 0:9d17e4342598 | 1253 | mp_mod (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 1254 | { |
wolfSSL | 0:9d17e4342598 | 1255 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 1256 | int res; |
wolfSSL | 0:9d17e4342598 | 1257 | |
wolfSSL | 0:9d17e4342598 | 1258 | if ((res = mp_init (&t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1259 | return res; |
wolfSSL | 0:9d17e4342598 | 1260 | } |
wolfSSL | 0:9d17e4342598 | 1261 | |
wolfSSL | 0:9d17e4342598 | 1262 | if ((res = mp_div (a, b, NULL, &t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1263 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 1264 | return res; |
wolfSSL | 0:9d17e4342598 | 1265 | } |
wolfSSL | 0:9d17e4342598 | 1266 | |
wolfSSL | 0:9d17e4342598 | 1267 | if (t.sign != b->sign) { |
wolfSSL | 0:9d17e4342598 | 1268 | res = mp_add (b, &t, c); |
wolfSSL | 0:9d17e4342598 | 1269 | } else { |
wolfSSL | 0:9d17e4342598 | 1270 | res = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1271 | mp_exch (&t, c); |
wolfSSL | 0:9d17e4342598 | 1272 | } |
wolfSSL | 0:9d17e4342598 | 1273 | |
wolfSSL | 0:9d17e4342598 | 1274 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 1275 | return res; |
wolfSSL | 0:9d17e4342598 | 1276 | } |
wolfSSL | 0:9d17e4342598 | 1277 | |
wolfSSL | 0:9d17e4342598 | 1278 | |
wolfSSL | 0:9d17e4342598 | 1279 | /* slower bit-bang division... also smaller */ |
wolfSSL | 0:9d17e4342598 | 1280 | int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
wolfSSL | 0:9d17e4342598 | 1281 | { |
wolfSSL | 0:9d17e4342598 | 1282 | mp_int ta, tb, tq, q; |
wolfSSL | 0:9d17e4342598 | 1283 | int res, n, n2; |
wolfSSL | 0:9d17e4342598 | 1284 | |
wolfSSL | 0:9d17e4342598 | 1285 | /* is divisor zero ? */ |
wolfSSL | 0:9d17e4342598 | 1286 | if (mp_iszero (b) == 1) { |
wolfSSL | 0:9d17e4342598 | 1287 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1288 | } |
wolfSSL | 0:9d17e4342598 | 1289 | |
wolfSSL | 0:9d17e4342598 | 1290 | /* if a < b then q=0, r = a */ |
wolfSSL | 0:9d17e4342598 | 1291 | if (mp_cmp_mag (a, b) == MP_LT) { |
wolfSSL | 0:9d17e4342598 | 1292 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 1293 | res = mp_copy (a, d); |
wolfSSL | 0:9d17e4342598 | 1294 | } else { |
wolfSSL | 0:9d17e4342598 | 1295 | res = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1296 | } |
wolfSSL | 0:9d17e4342598 | 1297 | if (c != NULL) { |
wolfSSL | 0:9d17e4342598 | 1298 | mp_zero (c); |
wolfSSL | 0:9d17e4342598 | 1299 | } |
wolfSSL | 0:9d17e4342598 | 1300 | return res; |
wolfSSL | 0:9d17e4342598 | 1301 | } |
wolfSSL | 0:9d17e4342598 | 1302 | |
wolfSSL | 0:9d17e4342598 | 1303 | /* init our temps */ |
wolfSSL | 0:9d17e4342598 | 1304 | if ((res = mp_init_multi(&ta, &tb, &tq, &q, 0, 0)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1305 | return res; |
wolfSSL | 0:9d17e4342598 | 1306 | } |
wolfSSL | 0:9d17e4342598 | 1307 | |
wolfSSL | 0:9d17e4342598 | 1308 | |
wolfSSL | 0:9d17e4342598 | 1309 | mp_set(&tq, 1); |
wolfSSL | 0:9d17e4342598 | 1310 | n = mp_count_bits(a) - mp_count_bits(b); |
wolfSSL | 0:9d17e4342598 | 1311 | if (((res = mp_abs(a, &ta)) != MP_OKAY) || |
wolfSSL | 0:9d17e4342598 | 1312 | ((res = mp_abs(b, &tb)) != MP_OKAY) || |
wolfSSL | 0:9d17e4342598 | 1313 | ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || |
wolfSSL | 0:9d17e4342598 | 1314 | ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 1315 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1316 | } |
wolfSSL | 0:9d17e4342598 | 1317 | |
wolfSSL | 0:9d17e4342598 | 1318 | while (n-- >= 0) { |
wolfSSL | 0:9d17e4342598 | 1319 | if (mp_cmp(&tb, &ta) != MP_GT) { |
wolfSSL | 0:9d17e4342598 | 1320 | if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) || |
wolfSSL | 0:9d17e4342598 | 1321 | ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 1322 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1323 | } |
wolfSSL | 0:9d17e4342598 | 1324 | } |
wolfSSL | 0:9d17e4342598 | 1325 | if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) || |
wolfSSL | 0:9d17e4342598 | 1326 | ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) { |
wolfSSL | 0:9d17e4342598 | 1327 | goto LBL_ERR; |
wolfSSL | 0:9d17e4342598 | 1328 | } |
wolfSSL | 0:9d17e4342598 | 1329 | } |
wolfSSL | 0:9d17e4342598 | 1330 | |
wolfSSL | 0:9d17e4342598 | 1331 | /* now q == quotient and ta == remainder */ |
wolfSSL | 0:9d17e4342598 | 1332 | n = a->sign; |
wolfSSL | 0:9d17e4342598 | 1333 | n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); |
wolfSSL | 0:9d17e4342598 | 1334 | if (c != NULL) { |
wolfSSL | 0:9d17e4342598 | 1335 | mp_exch(c, &q); |
wolfSSL | 0:9d17e4342598 | 1336 | c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; |
wolfSSL | 0:9d17e4342598 | 1337 | } |
wolfSSL | 0:9d17e4342598 | 1338 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 1339 | mp_exch(d, &ta); |
wolfSSL | 0:9d17e4342598 | 1340 | d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n; |
wolfSSL | 0:9d17e4342598 | 1341 | } |
wolfSSL | 0:9d17e4342598 | 1342 | LBL_ERR: |
wolfSSL | 0:9d17e4342598 | 1343 | mp_clear(&ta); |
wolfSSL | 0:9d17e4342598 | 1344 | mp_clear(&tb); |
wolfSSL | 0:9d17e4342598 | 1345 | mp_clear(&tq); |
wolfSSL | 0:9d17e4342598 | 1346 | mp_clear(&q); |
wolfSSL | 0:9d17e4342598 | 1347 | return res; |
wolfSSL | 0:9d17e4342598 | 1348 | } |
wolfSSL | 0:9d17e4342598 | 1349 | |
wolfSSL | 0:9d17e4342598 | 1350 | |
wolfSSL | 0:9d17e4342598 | 1351 | /* b = a/2 */ |
wolfSSL | 0:9d17e4342598 | 1352 | int mp_div_2(mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 1353 | { |
wolfSSL | 0:9d17e4342598 | 1354 | int x, res, oldused; |
wolfSSL | 0:9d17e4342598 | 1355 | |
wolfSSL | 0:9d17e4342598 | 1356 | /* copy */ |
wolfSSL | 0:9d17e4342598 | 1357 | if (b->alloc < a->used) { |
wolfSSL | 0:9d17e4342598 | 1358 | if ((res = mp_grow (b, a->used)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1359 | return res; |
wolfSSL | 0:9d17e4342598 | 1360 | } |
wolfSSL | 0:9d17e4342598 | 1361 | } |
wolfSSL | 0:9d17e4342598 | 1362 | |
wolfSSL | 0:9d17e4342598 | 1363 | oldused = b->used; |
wolfSSL | 0:9d17e4342598 | 1364 | b->used = a->used; |
wolfSSL | 0:9d17e4342598 | 1365 | { |
wolfSSL | 0:9d17e4342598 | 1366 | register mp_digit r, rr, *tmpa, *tmpb; |
wolfSSL | 0:9d17e4342598 | 1367 | |
wolfSSL | 0:9d17e4342598 | 1368 | /* source alias */ |
wolfSSL | 0:9d17e4342598 | 1369 | tmpa = a->dp + b->used - 1; |
wolfSSL | 0:9d17e4342598 | 1370 | |
wolfSSL | 0:9d17e4342598 | 1371 | /* dest alias */ |
wolfSSL | 0:9d17e4342598 | 1372 | tmpb = b->dp + b->used - 1; |
wolfSSL | 0:9d17e4342598 | 1373 | |
wolfSSL | 0:9d17e4342598 | 1374 | /* carry */ |
wolfSSL | 0:9d17e4342598 | 1375 | r = 0; |
wolfSSL | 0:9d17e4342598 | 1376 | for (x = b->used - 1; x >= 0; x--) { |
wolfSSL | 0:9d17e4342598 | 1377 | /* get the carry for the next iteration */ |
wolfSSL | 0:9d17e4342598 | 1378 | rr = *tmpa & 1; |
wolfSSL | 0:9d17e4342598 | 1379 | |
wolfSSL | 0:9d17e4342598 | 1380 | /* shift the current digit, add in carry and store */ |
wolfSSL | 0:9d17e4342598 | 1381 | *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); |
wolfSSL | 0:9d17e4342598 | 1382 | |
wolfSSL | 0:9d17e4342598 | 1383 | /* forward carry to next iteration */ |
wolfSSL | 0:9d17e4342598 | 1384 | r = rr; |
wolfSSL | 0:9d17e4342598 | 1385 | } |
wolfSSL | 0:9d17e4342598 | 1386 | |
wolfSSL | 0:9d17e4342598 | 1387 | /* zero excess digits */ |
wolfSSL | 0:9d17e4342598 | 1388 | tmpb = b->dp + b->used; |
wolfSSL | 0:9d17e4342598 | 1389 | for (x = b->used; x < oldused; x++) { |
wolfSSL | 0:9d17e4342598 | 1390 | *tmpb++ = 0; |
wolfSSL | 0:9d17e4342598 | 1391 | } |
wolfSSL | 0:9d17e4342598 | 1392 | } |
wolfSSL | 0:9d17e4342598 | 1393 | b->sign = a->sign; |
wolfSSL | 0:9d17e4342598 | 1394 | mp_clamp (b); |
wolfSSL | 0:9d17e4342598 | 1395 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1396 | } |
wolfSSL | 0:9d17e4342598 | 1397 | |
wolfSSL | 0:9d17e4342598 | 1398 | |
wolfSSL | 0:9d17e4342598 | 1399 | /* high level addition (handles signs) */ |
wolfSSL | 0:9d17e4342598 | 1400 | int mp_add (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 1401 | { |
wolfSSL | 0:9d17e4342598 | 1402 | int sa, sb, res; |
wolfSSL | 0:9d17e4342598 | 1403 | |
wolfSSL | 0:9d17e4342598 | 1404 | /* get sign of both inputs */ |
wolfSSL | 0:9d17e4342598 | 1405 | sa = a->sign; |
wolfSSL | 0:9d17e4342598 | 1406 | sb = b->sign; |
wolfSSL | 0:9d17e4342598 | 1407 | |
wolfSSL | 0:9d17e4342598 | 1408 | /* handle two cases, not four */ |
wolfSSL | 0:9d17e4342598 | 1409 | if (sa == sb) { |
wolfSSL | 0:9d17e4342598 | 1410 | /* both positive or both negative */ |
wolfSSL | 0:9d17e4342598 | 1411 | /* add their magnitudes, copy the sign */ |
wolfSSL | 0:9d17e4342598 | 1412 | c->sign = sa; |
wolfSSL | 0:9d17e4342598 | 1413 | res = s_mp_add (a, b, c); |
wolfSSL | 0:9d17e4342598 | 1414 | } else { |
wolfSSL | 0:9d17e4342598 | 1415 | /* one positive, the other negative */ |
wolfSSL | 0:9d17e4342598 | 1416 | /* subtract the one with the greater magnitude from */ |
wolfSSL | 0:9d17e4342598 | 1417 | /* the one of the lesser magnitude. The result gets */ |
wolfSSL | 0:9d17e4342598 | 1418 | /* the sign of the one with the greater magnitude. */ |
wolfSSL | 0:9d17e4342598 | 1419 | if (mp_cmp_mag (a, b) == MP_LT) { |
wolfSSL | 0:9d17e4342598 | 1420 | c->sign = sb; |
wolfSSL | 0:9d17e4342598 | 1421 | res = s_mp_sub (b, a, c); |
wolfSSL | 0:9d17e4342598 | 1422 | } else { |
wolfSSL | 0:9d17e4342598 | 1423 | c->sign = sa; |
wolfSSL | 0:9d17e4342598 | 1424 | res = s_mp_sub (a, b, c); |
wolfSSL | 0:9d17e4342598 | 1425 | } |
wolfSSL | 0:9d17e4342598 | 1426 | } |
wolfSSL | 0:9d17e4342598 | 1427 | return res; |
wolfSSL | 0:9d17e4342598 | 1428 | } |
wolfSSL | 0:9d17e4342598 | 1429 | |
wolfSSL | 0:9d17e4342598 | 1430 | |
wolfSSL | 0:9d17e4342598 | 1431 | /* low level addition, based on HAC pp.594, Algorithm 14.7 */ |
wolfSSL | 0:9d17e4342598 | 1432 | int |
wolfSSL | 0:9d17e4342598 | 1433 | s_mp_add (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 1434 | { |
wolfSSL | 0:9d17e4342598 | 1435 | mp_int *x; |
wolfSSL | 0:9d17e4342598 | 1436 | int olduse, res, min, max; |
wolfSSL | 0:9d17e4342598 | 1437 | |
wolfSSL | 0:9d17e4342598 | 1438 | /* find sizes, we let |a| <= |b| which means we have to sort |
wolfSSL | 0:9d17e4342598 | 1439 | * them. "x" will point to the input with the most digits |
wolfSSL | 0:9d17e4342598 | 1440 | */ |
wolfSSL | 0:9d17e4342598 | 1441 | if (a->used > b->used) { |
wolfSSL | 0:9d17e4342598 | 1442 | min = b->used; |
wolfSSL | 0:9d17e4342598 | 1443 | max = a->used; |
wolfSSL | 0:9d17e4342598 | 1444 | x = a; |
wolfSSL | 0:9d17e4342598 | 1445 | } else { |
wolfSSL | 0:9d17e4342598 | 1446 | min = a->used; |
wolfSSL | 0:9d17e4342598 | 1447 | max = b->used; |
wolfSSL | 0:9d17e4342598 | 1448 | x = b; |
wolfSSL | 0:9d17e4342598 | 1449 | } |
wolfSSL | 0:9d17e4342598 | 1450 | |
wolfSSL | 0:9d17e4342598 | 1451 | /* init result */ |
wolfSSL | 0:9d17e4342598 | 1452 | if (c->alloc < max + 1) { |
wolfSSL | 0:9d17e4342598 | 1453 | if ((res = mp_grow (c, max + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1454 | return res; |
wolfSSL | 0:9d17e4342598 | 1455 | } |
wolfSSL | 0:9d17e4342598 | 1456 | } |
wolfSSL | 0:9d17e4342598 | 1457 | |
wolfSSL | 0:9d17e4342598 | 1458 | /* get old used digit count and set new one */ |
wolfSSL | 0:9d17e4342598 | 1459 | olduse = c->used; |
wolfSSL | 0:9d17e4342598 | 1460 | c->used = max + 1; |
wolfSSL | 0:9d17e4342598 | 1461 | |
wolfSSL | 0:9d17e4342598 | 1462 | { |
wolfSSL | 0:9d17e4342598 | 1463 | register mp_digit u, *tmpa, *tmpb, *tmpc; |
wolfSSL | 0:9d17e4342598 | 1464 | register int i; |
wolfSSL | 0:9d17e4342598 | 1465 | |
wolfSSL | 0:9d17e4342598 | 1466 | /* alias for digit pointers */ |
wolfSSL | 0:9d17e4342598 | 1467 | |
wolfSSL | 0:9d17e4342598 | 1468 | /* first input */ |
wolfSSL | 0:9d17e4342598 | 1469 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 1470 | |
wolfSSL | 0:9d17e4342598 | 1471 | /* second input */ |
wolfSSL | 0:9d17e4342598 | 1472 | tmpb = b->dp; |
wolfSSL | 0:9d17e4342598 | 1473 | |
wolfSSL | 0:9d17e4342598 | 1474 | /* destination */ |
wolfSSL | 0:9d17e4342598 | 1475 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 1476 | |
wolfSSL | 0:9d17e4342598 | 1477 | /* zero the carry */ |
wolfSSL | 0:9d17e4342598 | 1478 | u = 0; |
wolfSSL | 0:9d17e4342598 | 1479 | for (i = 0; i < min; i++) { |
wolfSSL | 0:9d17e4342598 | 1480 | /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */ |
wolfSSL | 0:9d17e4342598 | 1481 | *tmpc = *tmpa++ + *tmpb++ + u; |
wolfSSL | 0:9d17e4342598 | 1482 | |
wolfSSL | 0:9d17e4342598 | 1483 | /* U = carry bit of T[i] */ |
wolfSSL | 0:9d17e4342598 | 1484 | u = *tmpc >> ((mp_digit)DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 1485 | |
wolfSSL | 0:9d17e4342598 | 1486 | /* take away carry bit from T[i] */ |
wolfSSL | 0:9d17e4342598 | 1487 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 1488 | } |
wolfSSL | 0:9d17e4342598 | 1489 | |
wolfSSL | 0:9d17e4342598 | 1490 | /* now copy higher words if any, that is in A+B |
wolfSSL | 0:9d17e4342598 | 1491 | * if A or B has more digits add those in |
wolfSSL | 0:9d17e4342598 | 1492 | */ |
wolfSSL | 0:9d17e4342598 | 1493 | if (min != max) { |
wolfSSL | 0:9d17e4342598 | 1494 | for (; i < max; i++) { |
wolfSSL | 0:9d17e4342598 | 1495 | /* T[i] = X[i] + U */ |
wolfSSL | 0:9d17e4342598 | 1496 | *tmpc = x->dp[i] + u; |
wolfSSL | 0:9d17e4342598 | 1497 | |
wolfSSL | 0:9d17e4342598 | 1498 | /* U = carry bit of T[i] */ |
wolfSSL | 0:9d17e4342598 | 1499 | u = *tmpc >> ((mp_digit)DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 1500 | |
wolfSSL | 0:9d17e4342598 | 1501 | /* take away carry bit from T[i] */ |
wolfSSL | 0:9d17e4342598 | 1502 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 1503 | } |
wolfSSL | 0:9d17e4342598 | 1504 | } |
wolfSSL | 0:9d17e4342598 | 1505 | |
wolfSSL | 0:9d17e4342598 | 1506 | /* add carry */ |
wolfSSL | 0:9d17e4342598 | 1507 | *tmpc++ = u; |
wolfSSL | 0:9d17e4342598 | 1508 | |
wolfSSL | 0:9d17e4342598 | 1509 | /* clear digits above oldused */ |
wolfSSL | 0:9d17e4342598 | 1510 | for (i = c->used; i < olduse; i++) { |
wolfSSL | 0:9d17e4342598 | 1511 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 1512 | } |
wolfSSL | 0:9d17e4342598 | 1513 | } |
wolfSSL | 0:9d17e4342598 | 1514 | |
wolfSSL | 0:9d17e4342598 | 1515 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 1516 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1517 | } |
wolfSSL | 0:9d17e4342598 | 1518 | |
wolfSSL | 0:9d17e4342598 | 1519 | |
wolfSSL | 0:9d17e4342598 | 1520 | /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ |
wolfSSL | 0:9d17e4342598 | 1521 | int |
wolfSSL | 0:9d17e4342598 | 1522 | s_mp_sub (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 1523 | { |
wolfSSL | 0:9d17e4342598 | 1524 | int olduse, res, min, max; |
wolfSSL | 0:9d17e4342598 | 1525 | |
wolfSSL | 0:9d17e4342598 | 1526 | /* find sizes */ |
wolfSSL | 0:9d17e4342598 | 1527 | min = b->used; |
wolfSSL | 0:9d17e4342598 | 1528 | max = a->used; |
wolfSSL | 0:9d17e4342598 | 1529 | |
wolfSSL | 0:9d17e4342598 | 1530 | /* init result */ |
wolfSSL | 0:9d17e4342598 | 1531 | if (c->alloc < max) { |
wolfSSL | 0:9d17e4342598 | 1532 | if ((res = mp_grow (c, max)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1533 | return res; |
wolfSSL | 0:9d17e4342598 | 1534 | } |
wolfSSL | 0:9d17e4342598 | 1535 | } |
wolfSSL | 0:9d17e4342598 | 1536 | olduse = c->used; |
wolfSSL | 0:9d17e4342598 | 1537 | c->used = max; |
wolfSSL | 0:9d17e4342598 | 1538 | |
wolfSSL | 0:9d17e4342598 | 1539 | { |
wolfSSL | 0:9d17e4342598 | 1540 | register mp_digit u, *tmpa, *tmpb, *tmpc; |
wolfSSL | 0:9d17e4342598 | 1541 | register int i; |
wolfSSL | 0:9d17e4342598 | 1542 | |
wolfSSL | 0:9d17e4342598 | 1543 | /* alias for digit pointers */ |
wolfSSL | 0:9d17e4342598 | 1544 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 1545 | tmpb = b->dp; |
wolfSSL | 0:9d17e4342598 | 1546 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 1547 | |
wolfSSL | 0:9d17e4342598 | 1548 | /* set carry to zero */ |
wolfSSL | 0:9d17e4342598 | 1549 | u = 0; |
wolfSSL | 0:9d17e4342598 | 1550 | for (i = 0; i < min; i++) { |
wolfSSL | 0:9d17e4342598 | 1551 | /* T[i] = A[i] - B[i] - U */ |
wolfSSL | 0:9d17e4342598 | 1552 | *tmpc = *tmpa++ - *tmpb++ - u; |
wolfSSL | 0:9d17e4342598 | 1553 | |
wolfSSL | 0:9d17e4342598 | 1554 | /* U = carry bit of T[i] |
wolfSSL | 0:9d17e4342598 | 1555 | * Note this saves performing an AND operation since |
wolfSSL | 0:9d17e4342598 | 1556 | * if a carry does occur it will propagate all the way to the |
wolfSSL | 0:9d17e4342598 | 1557 | * MSB. As a result a single shift is enough to get the carry |
wolfSSL | 0:9d17e4342598 | 1558 | */ |
wolfSSL | 0:9d17e4342598 | 1559 | u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); |
wolfSSL | 0:9d17e4342598 | 1560 | |
wolfSSL | 0:9d17e4342598 | 1561 | /* Clear carry from T[i] */ |
wolfSSL | 0:9d17e4342598 | 1562 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 1563 | } |
wolfSSL | 0:9d17e4342598 | 1564 | |
wolfSSL | 0:9d17e4342598 | 1565 | /* now copy higher words if any, e.g. if A has more digits than B */ |
wolfSSL | 0:9d17e4342598 | 1566 | for (; i < max; i++) { |
wolfSSL | 0:9d17e4342598 | 1567 | /* T[i] = A[i] - U */ |
wolfSSL | 0:9d17e4342598 | 1568 | *tmpc = *tmpa++ - u; |
wolfSSL | 0:9d17e4342598 | 1569 | |
wolfSSL | 0:9d17e4342598 | 1570 | /* U = carry bit of T[i] */ |
wolfSSL | 0:9d17e4342598 | 1571 | u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); |
wolfSSL | 0:9d17e4342598 | 1572 | |
wolfSSL | 0:9d17e4342598 | 1573 | /* Clear carry from T[i] */ |
wolfSSL | 0:9d17e4342598 | 1574 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 1575 | } |
wolfSSL | 0:9d17e4342598 | 1576 | |
wolfSSL | 0:9d17e4342598 | 1577 | /* clear digits above used (since we may not have grown result above) */ |
wolfSSL | 0:9d17e4342598 | 1578 | for (i = c->used; i < olduse; i++) { |
wolfSSL | 0:9d17e4342598 | 1579 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 1580 | } |
wolfSSL | 0:9d17e4342598 | 1581 | } |
wolfSSL | 0:9d17e4342598 | 1582 | |
wolfSSL | 0:9d17e4342598 | 1583 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 1584 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1585 | } |
wolfSSL | 0:9d17e4342598 | 1586 | |
wolfSSL | 0:9d17e4342598 | 1587 | |
wolfSSL | 0:9d17e4342598 | 1588 | /* high level subtraction (handles signs) */ |
wolfSSL | 0:9d17e4342598 | 1589 | int |
wolfSSL | 0:9d17e4342598 | 1590 | mp_sub (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 1591 | { |
wolfSSL | 0:9d17e4342598 | 1592 | int sa, sb, res; |
wolfSSL | 0:9d17e4342598 | 1593 | |
wolfSSL | 0:9d17e4342598 | 1594 | sa = a->sign; |
wolfSSL | 0:9d17e4342598 | 1595 | sb = b->sign; |
wolfSSL | 0:9d17e4342598 | 1596 | |
wolfSSL | 0:9d17e4342598 | 1597 | if (sa != sb) { |
wolfSSL | 0:9d17e4342598 | 1598 | /* subtract a negative from a positive, OR */ |
wolfSSL | 0:9d17e4342598 | 1599 | /* subtract a positive from a negative. */ |
wolfSSL | 0:9d17e4342598 | 1600 | /* In either case, ADD their magnitudes, */ |
wolfSSL | 0:9d17e4342598 | 1601 | /* and use the sign of the first number. */ |
wolfSSL | 0:9d17e4342598 | 1602 | c->sign = sa; |
wolfSSL | 0:9d17e4342598 | 1603 | res = s_mp_add (a, b, c); |
wolfSSL | 0:9d17e4342598 | 1604 | } else { |
wolfSSL | 0:9d17e4342598 | 1605 | /* subtract a positive from a positive, OR */ |
wolfSSL | 0:9d17e4342598 | 1606 | /* subtract a negative from a negative. */ |
wolfSSL | 0:9d17e4342598 | 1607 | /* First, take the difference between their */ |
wolfSSL | 0:9d17e4342598 | 1608 | /* magnitudes, then... */ |
wolfSSL | 0:9d17e4342598 | 1609 | if (mp_cmp_mag (a, b) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 1610 | /* Copy the sign from the first */ |
wolfSSL | 0:9d17e4342598 | 1611 | c->sign = sa; |
wolfSSL | 0:9d17e4342598 | 1612 | /* The first has a larger or equal magnitude */ |
wolfSSL | 0:9d17e4342598 | 1613 | res = s_mp_sub (a, b, c); |
wolfSSL | 0:9d17e4342598 | 1614 | } else { |
wolfSSL | 0:9d17e4342598 | 1615 | /* The result has the *opposite* sign from */ |
wolfSSL | 0:9d17e4342598 | 1616 | /* the first number. */ |
wolfSSL | 0:9d17e4342598 | 1617 | c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 1618 | /* The second has a larger magnitude */ |
wolfSSL | 0:9d17e4342598 | 1619 | res = s_mp_sub (b, a, c); |
wolfSSL | 0:9d17e4342598 | 1620 | } |
wolfSSL | 0:9d17e4342598 | 1621 | } |
wolfSSL | 0:9d17e4342598 | 1622 | return res; |
wolfSSL | 0:9d17e4342598 | 1623 | } |
wolfSSL | 0:9d17e4342598 | 1624 | |
wolfSSL | 0:9d17e4342598 | 1625 | |
wolfSSL | 0:9d17e4342598 | 1626 | /* determines if reduce_2k_l can be used */ |
wolfSSL | 0:9d17e4342598 | 1627 | int mp_reduce_is_2k_l(mp_int *a) |
wolfSSL | 0:9d17e4342598 | 1628 | { |
wolfSSL | 0:9d17e4342598 | 1629 | int ix, iy; |
wolfSSL | 0:9d17e4342598 | 1630 | |
wolfSSL | 0:9d17e4342598 | 1631 | if (a->used == 0) { |
wolfSSL | 0:9d17e4342598 | 1632 | return MP_NO; |
wolfSSL | 0:9d17e4342598 | 1633 | } else if (a->used == 1) { |
wolfSSL | 0:9d17e4342598 | 1634 | return MP_YES; |
wolfSSL | 0:9d17e4342598 | 1635 | } else if (a->used > 1) { |
wolfSSL | 0:9d17e4342598 | 1636 | /* if more than half of the digits are -1 we're sold */ |
wolfSSL | 0:9d17e4342598 | 1637 | for (iy = ix = 0; ix < a->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 1638 | if (a->dp[ix] == MP_MASK) { |
wolfSSL | 0:9d17e4342598 | 1639 | ++iy; |
wolfSSL | 0:9d17e4342598 | 1640 | } |
wolfSSL | 0:9d17e4342598 | 1641 | } |
wolfSSL | 0:9d17e4342598 | 1642 | return (iy >= (a->used/2)) ? MP_YES : MP_NO; |
wolfSSL | 0:9d17e4342598 | 1643 | |
wolfSSL | 0:9d17e4342598 | 1644 | } |
wolfSSL | 0:9d17e4342598 | 1645 | return MP_NO; |
wolfSSL | 0:9d17e4342598 | 1646 | } |
wolfSSL | 0:9d17e4342598 | 1647 | |
wolfSSL | 0:9d17e4342598 | 1648 | |
wolfSSL | 0:9d17e4342598 | 1649 | /* determines if mp_reduce_2k can be used */ |
wolfSSL | 0:9d17e4342598 | 1650 | int mp_reduce_is_2k(mp_int *a) |
wolfSSL | 0:9d17e4342598 | 1651 | { |
wolfSSL | 0:9d17e4342598 | 1652 | int ix, iy, iw; |
wolfSSL | 0:9d17e4342598 | 1653 | mp_digit iz; |
wolfSSL | 0:9d17e4342598 | 1654 | |
wolfSSL | 0:9d17e4342598 | 1655 | if (a->used == 0) { |
wolfSSL | 0:9d17e4342598 | 1656 | return MP_NO; |
wolfSSL | 0:9d17e4342598 | 1657 | } else if (a->used == 1) { |
wolfSSL | 0:9d17e4342598 | 1658 | return MP_YES; |
wolfSSL | 0:9d17e4342598 | 1659 | } else if (a->used > 1) { |
wolfSSL | 0:9d17e4342598 | 1660 | iy = mp_count_bits(a); |
wolfSSL | 0:9d17e4342598 | 1661 | iz = 1; |
wolfSSL | 0:9d17e4342598 | 1662 | iw = 1; |
wolfSSL | 0:9d17e4342598 | 1663 | |
wolfSSL | 0:9d17e4342598 | 1664 | /* Test every bit from the second digit up, must be 1 */ |
wolfSSL | 0:9d17e4342598 | 1665 | for (ix = DIGIT_BIT; ix < iy; ix++) { |
wolfSSL | 0:9d17e4342598 | 1666 | if ((a->dp[iw] & iz) == 0) { |
wolfSSL | 0:9d17e4342598 | 1667 | return MP_NO; |
wolfSSL | 0:9d17e4342598 | 1668 | } |
wolfSSL | 0:9d17e4342598 | 1669 | iz <<= 1; |
wolfSSL | 0:9d17e4342598 | 1670 | if (iz > (mp_digit)MP_MASK) { |
wolfSSL | 0:9d17e4342598 | 1671 | ++iw; |
wolfSSL | 0:9d17e4342598 | 1672 | iz = 1; |
wolfSSL | 0:9d17e4342598 | 1673 | } |
wolfSSL | 0:9d17e4342598 | 1674 | } |
wolfSSL | 0:9d17e4342598 | 1675 | } |
wolfSSL | 0:9d17e4342598 | 1676 | return MP_YES; |
wolfSSL | 0:9d17e4342598 | 1677 | } |
wolfSSL | 0:9d17e4342598 | 1678 | |
wolfSSL | 0:9d17e4342598 | 1679 | |
wolfSSL | 0:9d17e4342598 | 1680 | /* determines if a number is a valid DR modulus */ |
wolfSSL | 0:9d17e4342598 | 1681 | int mp_dr_is_modulus(mp_int *a) |
wolfSSL | 0:9d17e4342598 | 1682 | { |
wolfSSL | 0:9d17e4342598 | 1683 | int ix; |
wolfSSL | 0:9d17e4342598 | 1684 | |
wolfSSL | 0:9d17e4342598 | 1685 | /* must be at least two digits */ |
wolfSSL | 0:9d17e4342598 | 1686 | if (a->used < 2) { |
wolfSSL | 0:9d17e4342598 | 1687 | return 0; |
wolfSSL | 0:9d17e4342598 | 1688 | } |
wolfSSL | 0:9d17e4342598 | 1689 | |
wolfSSL | 0:9d17e4342598 | 1690 | /* must be of the form b**k - a [a <= b] so all |
wolfSSL | 0:9d17e4342598 | 1691 | * but the first digit must be equal to -1 (mod b). |
wolfSSL | 0:9d17e4342598 | 1692 | */ |
wolfSSL | 0:9d17e4342598 | 1693 | for (ix = 1; ix < a->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 1694 | if (a->dp[ix] != MP_MASK) { |
wolfSSL | 0:9d17e4342598 | 1695 | return 0; |
wolfSSL | 0:9d17e4342598 | 1696 | } |
wolfSSL | 0:9d17e4342598 | 1697 | } |
wolfSSL | 0:9d17e4342598 | 1698 | return 1; |
wolfSSL | 0:9d17e4342598 | 1699 | } |
wolfSSL | 0:9d17e4342598 | 1700 | |
wolfSSL | 0:9d17e4342598 | 1701 | |
wolfSSL | 0:9d17e4342598 | 1702 | /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85 |
wolfSSL | 0:9d17e4342598 | 1703 | * |
wolfSSL | 0:9d17e4342598 | 1704 | * Uses a left-to-right k-ary sliding window to compute the modular |
wolfSSL | 0:9d17e4342598 | 1705 | * exponentiation. |
wolfSSL | 0:9d17e4342598 | 1706 | * The value of k changes based on the size of the exponent. |
wolfSSL | 0:9d17e4342598 | 1707 | * |
wolfSSL | 0:9d17e4342598 | 1708 | * Uses Montgomery or Diminished Radix reduction [whichever appropriate] |
wolfSSL | 0:9d17e4342598 | 1709 | */ |
wolfSSL | 0:9d17e4342598 | 1710 | |
wolfSSL | 0:9d17e4342598 | 1711 | #ifdef MP_LOW_MEM |
wolfSSL | 0:9d17e4342598 | 1712 | #define TAB_SIZE 32 |
wolfSSL | 0:9d17e4342598 | 1713 | #else |
wolfSSL | 0:9d17e4342598 | 1714 | #define TAB_SIZE 256 |
wolfSSL | 0:9d17e4342598 | 1715 | #endif |
wolfSSL | 0:9d17e4342598 | 1716 | |
wolfSSL | 0:9d17e4342598 | 1717 | int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, |
wolfSSL | 0:9d17e4342598 | 1718 | int redmode) |
wolfSSL | 0:9d17e4342598 | 1719 | { |
wolfSSL | 0:9d17e4342598 | 1720 | mp_int M[TAB_SIZE], res; |
wolfSSL | 0:9d17e4342598 | 1721 | mp_digit buf, mp; |
wolfSSL | 0:9d17e4342598 | 1722 | int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; |
wolfSSL | 0:9d17e4342598 | 1723 | |
wolfSSL | 0:9d17e4342598 | 1724 | /* use a pointer to the reduction algorithm. This allows us to use |
wolfSSL | 0:9d17e4342598 | 1725 | * one of many reduction algorithms without modding the guts of |
wolfSSL | 0:9d17e4342598 | 1726 | * the code with if statements everywhere. |
wolfSSL | 0:9d17e4342598 | 1727 | */ |
wolfSSL | 0:9d17e4342598 | 1728 | int (*redux)(mp_int*,mp_int*,mp_digit); |
wolfSSL | 0:9d17e4342598 | 1729 | |
wolfSSL | 0:9d17e4342598 | 1730 | /* find window size */ |
wolfSSL | 0:9d17e4342598 | 1731 | x = mp_count_bits (X); |
wolfSSL | 0:9d17e4342598 | 1732 | if (x <= 7) { |
wolfSSL | 0:9d17e4342598 | 1733 | winsize = 2; |
wolfSSL | 0:9d17e4342598 | 1734 | } else if (x <= 36) { |
wolfSSL | 0:9d17e4342598 | 1735 | winsize = 3; |
wolfSSL | 0:9d17e4342598 | 1736 | } else if (x <= 140) { |
wolfSSL | 0:9d17e4342598 | 1737 | winsize = 4; |
wolfSSL | 0:9d17e4342598 | 1738 | } else if (x <= 450) { |
wolfSSL | 0:9d17e4342598 | 1739 | winsize = 5; |
wolfSSL | 0:9d17e4342598 | 1740 | } else if (x <= 1303) { |
wolfSSL | 0:9d17e4342598 | 1741 | winsize = 6; |
wolfSSL | 0:9d17e4342598 | 1742 | } else if (x <= 3529) { |
wolfSSL | 0:9d17e4342598 | 1743 | winsize = 7; |
wolfSSL | 0:9d17e4342598 | 1744 | } else { |
wolfSSL | 0:9d17e4342598 | 1745 | winsize = 8; |
wolfSSL | 0:9d17e4342598 | 1746 | } |
wolfSSL | 0:9d17e4342598 | 1747 | |
wolfSSL | 0:9d17e4342598 | 1748 | #ifdef MP_LOW_MEM |
wolfSSL | 0:9d17e4342598 | 1749 | if (winsize > 5) { |
wolfSSL | 0:9d17e4342598 | 1750 | winsize = 5; |
wolfSSL | 0:9d17e4342598 | 1751 | } |
wolfSSL | 0:9d17e4342598 | 1752 | #endif |
wolfSSL | 0:9d17e4342598 | 1753 | |
wolfSSL | 0:9d17e4342598 | 1754 | /* init M array */ |
wolfSSL | 0:9d17e4342598 | 1755 | /* init first cell */ |
wolfSSL | 0:9d17e4342598 | 1756 | if ((err = mp_init(&M[1])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1757 | return err; |
wolfSSL | 0:9d17e4342598 | 1758 | } |
wolfSSL | 0:9d17e4342598 | 1759 | |
wolfSSL | 0:9d17e4342598 | 1760 | /* now init the second half of the array */ |
wolfSSL | 0:9d17e4342598 | 1761 | for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
wolfSSL | 0:9d17e4342598 | 1762 | if ((err = mp_init(&M[x])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1763 | for (y = 1<<(winsize-1); y < x; y++) { |
wolfSSL | 0:9d17e4342598 | 1764 | mp_clear (&M[y]); |
wolfSSL | 0:9d17e4342598 | 1765 | } |
wolfSSL | 0:9d17e4342598 | 1766 | mp_clear(&M[1]); |
wolfSSL | 0:9d17e4342598 | 1767 | return err; |
wolfSSL | 0:9d17e4342598 | 1768 | } |
wolfSSL | 0:9d17e4342598 | 1769 | } |
wolfSSL | 0:9d17e4342598 | 1770 | |
wolfSSL | 0:9d17e4342598 | 1771 | /* determine and setup reduction code */ |
wolfSSL | 0:9d17e4342598 | 1772 | if (redmode == 0) { |
wolfSSL | 0:9d17e4342598 | 1773 | #ifdef BN_MP_MONTGOMERY_SETUP_C |
wolfSSL | 0:9d17e4342598 | 1774 | /* now setup montgomery */ |
wolfSSL | 0:9d17e4342598 | 1775 | if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1776 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1777 | } |
wolfSSL | 0:9d17e4342598 | 1778 | #else |
wolfSSL | 0:9d17e4342598 | 1779 | err = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1780 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1781 | #endif |
wolfSSL | 0:9d17e4342598 | 1782 | |
wolfSSL | 0:9d17e4342598 | 1783 | /* automatically pick the comba one if available (saves quite a few |
wolfSSL | 0:9d17e4342598 | 1784 | calls/ifs) */ |
wolfSSL | 0:9d17e4342598 | 1785 | #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C |
wolfSSL | 0:9d17e4342598 | 1786 | if (((P->used * 2 + 1) < MP_WARRAY) && |
wolfSSL | 0:9d17e4342598 | 1787 | P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
wolfSSL | 0:9d17e4342598 | 1788 | redux = fast_mp_montgomery_reduce; |
wolfSSL | 0:9d17e4342598 | 1789 | } else |
wolfSSL | 0:9d17e4342598 | 1790 | #endif |
wolfSSL | 0:9d17e4342598 | 1791 | { |
wolfSSL | 0:9d17e4342598 | 1792 | #ifdef BN_MP_MONTGOMERY_REDUCE_C |
wolfSSL | 0:9d17e4342598 | 1793 | /* use slower baseline Montgomery method */ |
wolfSSL | 0:9d17e4342598 | 1794 | redux = mp_montgomery_reduce; |
wolfSSL | 0:9d17e4342598 | 1795 | #else |
wolfSSL | 0:9d17e4342598 | 1796 | err = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1797 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1798 | #endif |
wolfSSL | 0:9d17e4342598 | 1799 | } |
wolfSSL | 0:9d17e4342598 | 1800 | } else if (redmode == 1) { |
wolfSSL | 0:9d17e4342598 | 1801 | #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C) |
wolfSSL | 0:9d17e4342598 | 1802 | /* setup DR reduction for moduli of the form B**k - b */ |
wolfSSL | 0:9d17e4342598 | 1803 | mp_dr_setup(P, &mp); |
wolfSSL | 0:9d17e4342598 | 1804 | redux = mp_dr_reduce; |
wolfSSL | 0:9d17e4342598 | 1805 | #else |
wolfSSL | 0:9d17e4342598 | 1806 | err = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1807 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1808 | #endif |
wolfSSL | 0:9d17e4342598 | 1809 | } else { |
wolfSSL | 0:9d17e4342598 | 1810 | #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C) |
wolfSSL | 0:9d17e4342598 | 1811 | /* setup DR reduction for moduli of the form 2**k - b */ |
wolfSSL | 0:9d17e4342598 | 1812 | if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1813 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1814 | } |
wolfSSL | 0:9d17e4342598 | 1815 | redux = mp_reduce_2k; |
wolfSSL | 0:9d17e4342598 | 1816 | #else |
wolfSSL | 0:9d17e4342598 | 1817 | err = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1818 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1819 | #endif |
wolfSSL | 0:9d17e4342598 | 1820 | } |
wolfSSL | 0:9d17e4342598 | 1821 | |
wolfSSL | 0:9d17e4342598 | 1822 | /* setup result */ |
wolfSSL | 0:9d17e4342598 | 1823 | if ((err = mp_init (&res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1824 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 1825 | } |
wolfSSL | 0:9d17e4342598 | 1826 | |
wolfSSL | 0:9d17e4342598 | 1827 | /* create M table |
wolfSSL | 0:9d17e4342598 | 1828 | * |
wolfSSL | 0:9d17e4342598 | 1829 | |
wolfSSL | 0:9d17e4342598 | 1830 | * |
wolfSSL | 0:9d17e4342598 | 1831 | * The first half of the table is not computed though accept for M[0] and M[1] |
wolfSSL | 0:9d17e4342598 | 1832 | */ |
wolfSSL | 0:9d17e4342598 | 1833 | |
wolfSSL | 0:9d17e4342598 | 1834 | if (redmode == 0) { |
wolfSSL | 0:9d17e4342598 | 1835 | #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C |
wolfSSL | 0:9d17e4342598 | 1836 | /* now we need R mod m */ |
wolfSSL | 0:9d17e4342598 | 1837 | if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1838 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1839 | } |
wolfSSL | 0:9d17e4342598 | 1840 | #else |
wolfSSL | 0:9d17e4342598 | 1841 | err = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 1842 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1843 | #endif |
wolfSSL | 0:9d17e4342598 | 1844 | |
wolfSSL | 0:9d17e4342598 | 1845 | /* now set M[1] to G * R mod m */ |
wolfSSL | 0:9d17e4342598 | 1846 | if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1847 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1848 | } |
wolfSSL | 0:9d17e4342598 | 1849 | } else { |
wolfSSL | 0:9d17e4342598 | 1850 | mp_set(&res, 1); |
wolfSSL | 0:9d17e4342598 | 1851 | if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1852 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1853 | } |
wolfSSL | 0:9d17e4342598 | 1854 | } |
wolfSSL | 0:9d17e4342598 | 1855 | |
wolfSSL | 0:9d17e4342598 | 1856 | /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times*/ |
wolfSSL | 0:9d17e4342598 | 1857 | if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1858 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1859 | } |
wolfSSL | 0:9d17e4342598 | 1860 | |
wolfSSL | 0:9d17e4342598 | 1861 | for (x = 0; x < (winsize - 1); x++) { |
wolfSSL | 0:9d17e4342598 | 1862 | if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1863 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1864 | } |
wolfSSL | 0:9d17e4342598 | 1865 | if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1866 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1867 | } |
wolfSSL | 0:9d17e4342598 | 1868 | } |
wolfSSL | 0:9d17e4342598 | 1869 | |
wolfSSL | 0:9d17e4342598 | 1870 | /* create upper table */ |
wolfSSL | 0:9d17e4342598 | 1871 | for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { |
wolfSSL | 0:9d17e4342598 | 1872 | if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1873 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1874 | } |
wolfSSL | 0:9d17e4342598 | 1875 | if ((err = redux (&M[x], P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1876 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1877 | } |
wolfSSL | 0:9d17e4342598 | 1878 | } |
wolfSSL | 0:9d17e4342598 | 1879 | |
wolfSSL | 0:9d17e4342598 | 1880 | /* set initial mode and bit cnt */ |
wolfSSL | 0:9d17e4342598 | 1881 | mode = 0; |
wolfSSL | 0:9d17e4342598 | 1882 | bitcnt = 1; |
wolfSSL | 0:9d17e4342598 | 1883 | buf = 0; |
wolfSSL | 0:9d17e4342598 | 1884 | digidx = X->used - 1; |
wolfSSL | 0:9d17e4342598 | 1885 | bitcpy = 0; |
wolfSSL | 0:9d17e4342598 | 1886 | bitbuf = 0; |
wolfSSL | 0:9d17e4342598 | 1887 | |
wolfSSL | 0:9d17e4342598 | 1888 | for (;;) { |
wolfSSL | 0:9d17e4342598 | 1889 | /* grab next digit as required */ |
wolfSSL | 0:9d17e4342598 | 1890 | if (--bitcnt == 0) { |
wolfSSL | 0:9d17e4342598 | 1891 | /* if digidx == -1 we are out of digits so break */ |
wolfSSL | 0:9d17e4342598 | 1892 | if (digidx == -1) { |
wolfSSL | 0:9d17e4342598 | 1893 | break; |
wolfSSL | 0:9d17e4342598 | 1894 | } |
wolfSSL | 0:9d17e4342598 | 1895 | /* read next digit and reset bitcnt */ |
wolfSSL | 0:9d17e4342598 | 1896 | buf = X->dp[digidx--]; |
wolfSSL | 0:9d17e4342598 | 1897 | bitcnt = (int)DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 1898 | } |
wolfSSL | 0:9d17e4342598 | 1899 | |
wolfSSL | 0:9d17e4342598 | 1900 | /* grab the next msb from the exponent */ |
wolfSSL | 0:9d17e4342598 | 1901 | y = (int)(buf >> (DIGIT_BIT - 1)) & 1; |
wolfSSL | 0:9d17e4342598 | 1902 | buf <<= (mp_digit)1; |
wolfSSL | 0:9d17e4342598 | 1903 | |
wolfSSL | 0:9d17e4342598 | 1904 | /* if the bit is zero and mode == 0 then we ignore it |
wolfSSL | 0:9d17e4342598 | 1905 | * These represent the leading zero bits before the first 1 bit |
wolfSSL | 0:9d17e4342598 | 1906 | * in the exponent. Technically this opt is not required but it |
wolfSSL | 0:9d17e4342598 | 1907 | * does lower the # of trivial squaring/reductions used |
wolfSSL | 0:9d17e4342598 | 1908 | */ |
wolfSSL | 0:9d17e4342598 | 1909 | if (mode == 0 && y == 0) { |
wolfSSL | 0:9d17e4342598 | 1910 | continue; |
wolfSSL | 0:9d17e4342598 | 1911 | } |
wolfSSL | 0:9d17e4342598 | 1912 | |
wolfSSL | 0:9d17e4342598 | 1913 | /* if the bit is zero and mode == 1 then we square */ |
wolfSSL | 0:9d17e4342598 | 1914 | if (mode == 1 && y == 0) { |
wolfSSL | 0:9d17e4342598 | 1915 | if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1916 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1917 | } |
wolfSSL | 0:9d17e4342598 | 1918 | if ((err = redux (&res, P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1919 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1920 | } |
wolfSSL | 0:9d17e4342598 | 1921 | continue; |
wolfSSL | 0:9d17e4342598 | 1922 | } |
wolfSSL | 0:9d17e4342598 | 1923 | |
wolfSSL | 0:9d17e4342598 | 1924 | /* else we add it to the window */ |
wolfSSL | 0:9d17e4342598 | 1925 | bitbuf |= (y << (winsize - ++bitcpy)); |
wolfSSL | 0:9d17e4342598 | 1926 | mode = 2; |
wolfSSL | 0:9d17e4342598 | 1927 | |
wolfSSL | 0:9d17e4342598 | 1928 | if (bitcpy == winsize) { |
wolfSSL | 0:9d17e4342598 | 1929 | /* ok window is filled so square as required and multiply */ |
wolfSSL | 0:9d17e4342598 | 1930 | /* square first */ |
wolfSSL | 0:9d17e4342598 | 1931 | for (x = 0; x < winsize; x++) { |
wolfSSL | 0:9d17e4342598 | 1932 | if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1933 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1934 | } |
wolfSSL | 0:9d17e4342598 | 1935 | if ((err = redux (&res, P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1936 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1937 | } |
wolfSSL | 0:9d17e4342598 | 1938 | } |
wolfSSL | 0:9d17e4342598 | 1939 | |
wolfSSL | 0:9d17e4342598 | 1940 | /* then multiply */ |
wolfSSL | 0:9d17e4342598 | 1941 | if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1942 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1943 | } |
wolfSSL | 0:9d17e4342598 | 1944 | if ((err = redux (&res, P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1945 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1946 | } |
wolfSSL | 0:9d17e4342598 | 1947 | |
wolfSSL | 0:9d17e4342598 | 1948 | /* empty window and reset */ |
wolfSSL | 0:9d17e4342598 | 1949 | bitcpy = 0; |
wolfSSL | 0:9d17e4342598 | 1950 | bitbuf = 0; |
wolfSSL | 0:9d17e4342598 | 1951 | mode = 1; |
wolfSSL | 0:9d17e4342598 | 1952 | } |
wolfSSL | 0:9d17e4342598 | 1953 | } |
wolfSSL | 0:9d17e4342598 | 1954 | |
wolfSSL | 0:9d17e4342598 | 1955 | /* if bits remain then square/multiply */ |
wolfSSL | 0:9d17e4342598 | 1956 | if (mode == 2 && bitcpy > 0) { |
wolfSSL | 0:9d17e4342598 | 1957 | /* square then multiply if the bit is set */ |
wolfSSL | 0:9d17e4342598 | 1958 | for (x = 0; x < bitcpy; x++) { |
wolfSSL | 0:9d17e4342598 | 1959 | if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1960 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1961 | } |
wolfSSL | 0:9d17e4342598 | 1962 | if ((err = redux (&res, P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1963 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1964 | } |
wolfSSL | 0:9d17e4342598 | 1965 | |
wolfSSL | 0:9d17e4342598 | 1966 | /* get next bit of the window */ |
wolfSSL | 0:9d17e4342598 | 1967 | bitbuf <<= 1; |
wolfSSL | 0:9d17e4342598 | 1968 | if ((bitbuf & (1 << winsize)) != 0) { |
wolfSSL | 0:9d17e4342598 | 1969 | /* then multiply */ |
wolfSSL | 0:9d17e4342598 | 1970 | if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1971 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1972 | } |
wolfSSL | 0:9d17e4342598 | 1973 | if ((err = redux (&res, P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1974 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1975 | } |
wolfSSL | 0:9d17e4342598 | 1976 | } |
wolfSSL | 0:9d17e4342598 | 1977 | } |
wolfSSL | 0:9d17e4342598 | 1978 | } |
wolfSSL | 0:9d17e4342598 | 1979 | |
wolfSSL | 0:9d17e4342598 | 1980 | if (redmode == 0) { |
wolfSSL | 0:9d17e4342598 | 1981 | /* fixup result if Montgomery reduction is used |
wolfSSL | 0:9d17e4342598 | 1982 | * recall that any value in a Montgomery system is |
wolfSSL | 0:9d17e4342598 | 1983 | * actually multiplied by R mod n. So we have |
wolfSSL | 0:9d17e4342598 | 1984 | * to reduce one more time to cancel out the factor |
wolfSSL | 0:9d17e4342598 | 1985 | * of R. |
wolfSSL | 0:9d17e4342598 | 1986 | */ |
wolfSSL | 0:9d17e4342598 | 1987 | if ((err = redux(&res, P, mp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1988 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 1989 | } |
wolfSSL | 0:9d17e4342598 | 1990 | } |
wolfSSL | 0:9d17e4342598 | 1991 | |
wolfSSL | 0:9d17e4342598 | 1992 | /* swap res with Y */ |
wolfSSL | 0:9d17e4342598 | 1993 | mp_exch (&res, Y); |
wolfSSL | 0:9d17e4342598 | 1994 | err = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 1995 | LBL_RES:mp_clear (&res); |
wolfSSL | 0:9d17e4342598 | 1996 | LBL_M: |
wolfSSL | 0:9d17e4342598 | 1997 | mp_clear(&M[1]); |
wolfSSL | 0:9d17e4342598 | 1998 | for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
wolfSSL | 0:9d17e4342598 | 1999 | mp_clear (&M[x]); |
wolfSSL | 0:9d17e4342598 | 2000 | } |
wolfSSL | 0:9d17e4342598 | 2001 | return err; |
wolfSSL | 0:9d17e4342598 | 2002 | } |
wolfSSL | 0:9d17e4342598 | 2003 | |
wolfSSL | 0:9d17e4342598 | 2004 | |
wolfSSL | 0:9d17e4342598 | 2005 | /* setups the montgomery reduction stuff */ |
wolfSSL | 0:9d17e4342598 | 2006 | int |
wolfSSL | 0:9d17e4342598 | 2007 | mp_montgomery_setup (mp_int * n, mp_digit * rho) |
wolfSSL | 0:9d17e4342598 | 2008 | { |
wolfSSL | 0:9d17e4342598 | 2009 | mp_digit x, b; |
wolfSSL | 0:9d17e4342598 | 2010 | |
wolfSSL | 0:9d17e4342598 | 2011 | /* fast inversion mod 2**k |
wolfSSL | 0:9d17e4342598 | 2012 | * |
wolfSSL | 0:9d17e4342598 | 2013 | * Based on the fact that |
wolfSSL | 0:9d17e4342598 | 2014 | * |
wolfSSL | 0:9d17e4342598 | 2015 | * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) |
wolfSSL | 0:9d17e4342598 | 2016 | * => 2*X*A - X*X*A*A = 1 |
wolfSSL | 0:9d17e4342598 | 2017 | * => 2*(1) - (1) = 1 |
wolfSSL | 0:9d17e4342598 | 2018 | */ |
wolfSSL | 0:9d17e4342598 | 2019 | b = n->dp[0]; |
wolfSSL | 0:9d17e4342598 | 2020 | |
wolfSSL | 0:9d17e4342598 | 2021 | if ((b & 1) == 0) { |
wolfSSL | 0:9d17e4342598 | 2022 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 2023 | } |
wolfSSL | 0:9d17e4342598 | 2024 | |
wolfSSL | 0:9d17e4342598 | 2025 | x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ |
wolfSSL | 0:9d17e4342598 | 2026 | x *= 2 - b * x; /* here x*a==1 mod 2**8 */ |
wolfSSL | 0:9d17e4342598 | 2027 | #if !defined(MP_8BIT) |
wolfSSL | 0:9d17e4342598 | 2028 | x *= 2 - b * x; /* here x*a==1 mod 2**16 */ |
wolfSSL | 0:9d17e4342598 | 2029 | #endif |
wolfSSL | 0:9d17e4342598 | 2030 | #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) |
wolfSSL | 0:9d17e4342598 | 2031 | x *= 2 - b * x; /* here x*a==1 mod 2**32 */ |
wolfSSL | 0:9d17e4342598 | 2032 | #endif |
wolfSSL | 0:9d17e4342598 | 2033 | #ifdef MP_64BIT |
wolfSSL | 0:9d17e4342598 | 2034 | x *= 2 - b * x; /* here x*a==1 mod 2**64 */ |
wolfSSL | 0:9d17e4342598 | 2035 | #endif |
wolfSSL | 0:9d17e4342598 | 2036 | |
wolfSSL | 0:9d17e4342598 | 2037 | /* rho = -1/m mod b */ |
wolfSSL | 0:9d17e4342598 | 2038 | /* TAO, switched mp_word casts to mp_digit to shut up compiler */ |
wolfSSL | 0:9d17e4342598 | 2039 | *rho = (((mp_digit)1 << ((mp_digit) DIGIT_BIT)) - x) & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 2040 | |
wolfSSL | 0:9d17e4342598 | 2041 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2042 | } |
wolfSSL | 0:9d17e4342598 | 2043 | |
wolfSSL | 0:9d17e4342598 | 2044 | |
wolfSSL | 0:9d17e4342598 | 2045 | /* computes xR**-1 == x (mod N) via Montgomery Reduction |
wolfSSL | 0:9d17e4342598 | 2046 | * |
wolfSSL | 0:9d17e4342598 | 2047 | * This is an optimized implementation of montgomery_reduce |
wolfSSL | 0:9d17e4342598 | 2048 | * which uses the comba method to quickly calculate the columns of the |
wolfSSL | 0:9d17e4342598 | 2049 | * reduction. |
wolfSSL | 0:9d17e4342598 | 2050 | * |
wolfSSL | 0:9d17e4342598 | 2051 | * Based on Algorithm 14.32 on pp.601 of HAC. |
wolfSSL | 0:9d17e4342598 | 2052 | */ |
wolfSSL | 0:9d17e4342598 | 2053 | int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) |
wolfSSL | 0:9d17e4342598 | 2054 | { |
wolfSSL | 0:9d17e4342598 | 2055 | int ix, res, olduse; |
wolfSSL | 0:9d17e4342598 | 2056 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2057 | mp_word* W; /* uses dynamic memory and slower */ |
wolfSSL | 0:9d17e4342598 | 2058 | #else |
wolfSSL | 0:9d17e4342598 | 2059 | mp_word W[MP_WARRAY]; |
wolfSSL | 0:9d17e4342598 | 2060 | #endif |
wolfSSL | 0:9d17e4342598 | 2061 | |
wolfSSL | 0:9d17e4342598 | 2062 | /* get old used count */ |
wolfSSL | 0:9d17e4342598 | 2063 | olduse = x->used; |
wolfSSL | 0:9d17e4342598 | 2064 | |
wolfSSL | 0:9d17e4342598 | 2065 | /* grow a as required */ |
wolfSSL | 0:9d17e4342598 | 2066 | if (x->alloc < n->used + 1) { |
wolfSSL | 0:9d17e4342598 | 2067 | if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2068 | return res; |
wolfSSL | 0:9d17e4342598 | 2069 | } |
wolfSSL | 0:9d17e4342598 | 2070 | } |
wolfSSL | 0:9d17e4342598 | 2071 | |
wolfSSL | 0:9d17e4342598 | 2072 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2073 | W = (mp_word*)XMALLOC(sizeof(mp_word) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2074 | if (W == NULL) |
wolfSSL | 0:9d17e4342598 | 2075 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 2076 | #endif |
wolfSSL | 0:9d17e4342598 | 2077 | |
wolfSSL | 0:9d17e4342598 | 2078 | /* first we have to get the digits of the input into |
wolfSSL | 0:9d17e4342598 | 2079 | * an array of double precision words W[...] |
wolfSSL | 0:9d17e4342598 | 2080 | */ |
wolfSSL | 0:9d17e4342598 | 2081 | { |
wolfSSL | 0:9d17e4342598 | 2082 | register mp_word *_W; |
wolfSSL | 0:9d17e4342598 | 2083 | register mp_digit *tmpx; |
wolfSSL | 0:9d17e4342598 | 2084 | |
wolfSSL | 0:9d17e4342598 | 2085 | /* alias for the W[] array */ |
wolfSSL | 0:9d17e4342598 | 2086 | _W = W; |
wolfSSL | 0:9d17e4342598 | 2087 | |
wolfSSL | 0:9d17e4342598 | 2088 | /* alias for the digits of x*/ |
wolfSSL | 0:9d17e4342598 | 2089 | tmpx = x->dp; |
wolfSSL | 0:9d17e4342598 | 2090 | |
wolfSSL | 0:9d17e4342598 | 2091 | /* copy the digits of a into W[0..a->used-1] */ |
wolfSSL | 0:9d17e4342598 | 2092 | for (ix = 0; ix < x->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 2093 | *_W++ = *tmpx++; |
wolfSSL | 0:9d17e4342598 | 2094 | } |
wolfSSL | 0:9d17e4342598 | 2095 | |
wolfSSL | 0:9d17e4342598 | 2096 | /* zero the high words of W[a->used..m->used*2] */ |
wolfSSL | 0:9d17e4342598 | 2097 | for (; ix < n->used * 2 + 1; ix++) { |
wolfSSL | 0:9d17e4342598 | 2098 | *_W++ = 0; |
wolfSSL | 0:9d17e4342598 | 2099 | } |
wolfSSL | 0:9d17e4342598 | 2100 | } |
wolfSSL | 0:9d17e4342598 | 2101 | |
wolfSSL | 0:9d17e4342598 | 2102 | /* now we proceed to zero successive digits |
wolfSSL | 0:9d17e4342598 | 2103 | * from the least significant upwards |
wolfSSL | 0:9d17e4342598 | 2104 | */ |
wolfSSL | 0:9d17e4342598 | 2105 | for (ix = 0; ix < n->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 2106 | /* mu = ai * m' mod b |
wolfSSL | 0:9d17e4342598 | 2107 | * |
wolfSSL | 0:9d17e4342598 | 2108 | * We avoid a double precision multiplication (which isn't required) |
wolfSSL | 0:9d17e4342598 | 2109 | * by casting the value down to a mp_digit. Note this requires |
wolfSSL | 0:9d17e4342598 | 2110 | * that W[ix-1] have the carry cleared (see after the inner loop) |
wolfSSL | 0:9d17e4342598 | 2111 | */ |
wolfSSL | 0:9d17e4342598 | 2112 | register mp_digit mu; |
wolfSSL | 0:9d17e4342598 | 2113 | mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK); |
wolfSSL | 0:9d17e4342598 | 2114 | |
wolfSSL | 0:9d17e4342598 | 2115 | /* a = a + mu * m * b**i |
wolfSSL | 0:9d17e4342598 | 2116 | * |
wolfSSL | 0:9d17e4342598 | 2117 | * This is computed in place and on the fly. The multiplication |
wolfSSL | 0:9d17e4342598 | 2118 | * by b**i is handled by offseting which columns the results |
wolfSSL | 0:9d17e4342598 | 2119 | * are added to. |
wolfSSL | 0:9d17e4342598 | 2120 | * |
wolfSSL | 0:9d17e4342598 | 2121 | * Note the comba method normally doesn't handle carries in the |
wolfSSL | 0:9d17e4342598 | 2122 | * inner loop In this case we fix the carry from the previous |
wolfSSL | 0:9d17e4342598 | 2123 | * column since the Montgomery reduction requires digits of the |
wolfSSL | 0:9d17e4342598 | 2124 | * result (so far) [see above] to work. This is |
wolfSSL | 0:9d17e4342598 | 2125 | * handled by fixing up one carry after the inner loop. The |
wolfSSL | 0:9d17e4342598 | 2126 | * carry fixups are done in order so after these loops the |
wolfSSL | 0:9d17e4342598 | 2127 | * first m->used words of W[] have the carries fixed |
wolfSSL | 0:9d17e4342598 | 2128 | */ |
wolfSSL | 0:9d17e4342598 | 2129 | { |
wolfSSL | 0:9d17e4342598 | 2130 | register int iy; |
wolfSSL | 0:9d17e4342598 | 2131 | register mp_digit *tmpn; |
wolfSSL | 0:9d17e4342598 | 2132 | register mp_word *_W; |
wolfSSL | 0:9d17e4342598 | 2133 | |
wolfSSL | 0:9d17e4342598 | 2134 | /* alias for the digits of the modulus */ |
wolfSSL | 0:9d17e4342598 | 2135 | tmpn = n->dp; |
wolfSSL | 0:9d17e4342598 | 2136 | |
wolfSSL | 0:9d17e4342598 | 2137 | /* Alias for the columns set by an offset of ix */ |
wolfSSL | 0:9d17e4342598 | 2138 | _W = W + ix; |
wolfSSL | 0:9d17e4342598 | 2139 | |
wolfSSL | 0:9d17e4342598 | 2140 | /* inner loop */ |
wolfSSL | 0:9d17e4342598 | 2141 | for (iy = 0; iy < n->used; iy++) { |
wolfSSL | 0:9d17e4342598 | 2142 | *_W++ += ((mp_word)mu) * ((mp_word)*tmpn++); |
wolfSSL | 0:9d17e4342598 | 2143 | } |
wolfSSL | 0:9d17e4342598 | 2144 | } |
wolfSSL | 0:9d17e4342598 | 2145 | |
wolfSSL | 0:9d17e4342598 | 2146 | /* now fix carry for next digit, W[ix+1] */ |
wolfSSL | 0:9d17e4342598 | 2147 | W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 2148 | } |
wolfSSL | 0:9d17e4342598 | 2149 | |
wolfSSL | 0:9d17e4342598 | 2150 | /* now we have to propagate the carries and |
wolfSSL | 0:9d17e4342598 | 2151 | * shift the words downward [all those least |
wolfSSL | 0:9d17e4342598 | 2152 | * significant digits we zeroed]. |
wolfSSL | 0:9d17e4342598 | 2153 | */ |
wolfSSL | 0:9d17e4342598 | 2154 | { |
wolfSSL | 0:9d17e4342598 | 2155 | register mp_digit *tmpx; |
wolfSSL | 0:9d17e4342598 | 2156 | register mp_word *_W, *_W1; |
wolfSSL | 0:9d17e4342598 | 2157 | |
wolfSSL | 0:9d17e4342598 | 2158 | /* nox fix rest of carries */ |
wolfSSL | 0:9d17e4342598 | 2159 | |
wolfSSL | 0:9d17e4342598 | 2160 | /* alias for current word */ |
wolfSSL | 0:9d17e4342598 | 2161 | _W1 = W + ix; |
wolfSSL | 0:9d17e4342598 | 2162 | |
wolfSSL | 0:9d17e4342598 | 2163 | /* alias for next word, where the carry goes */ |
wolfSSL | 0:9d17e4342598 | 2164 | _W = W + ++ix; |
wolfSSL | 0:9d17e4342598 | 2165 | |
wolfSSL | 0:9d17e4342598 | 2166 | for (; ix <= n->used * 2 + 1; ix++) { |
wolfSSL | 0:9d17e4342598 | 2167 | *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 2168 | } |
wolfSSL | 0:9d17e4342598 | 2169 | |
wolfSSL | 0:9d17e4342598 | 2170 | /* copy out, A = A/b**n |
wolfSSL | 0:9d17e4342598 | 2171 | * |
wolfSSL | 0:9d17e4342598 | 2172 | * The result is A/b**n but instead of converting from an |
wolfSSL | 0:9d17e4342598 | 2173 | * array of mp_word to mp_digit than calling mp_rshd |
wolfSSL | 0:9d17e4342598 | 2174 | * we just copy them in the right order |
wolfSSL | 0:9d17e4342598 | 2175 | */ |
wolfSSL | 0:9d17e4342598 | 2176 | |
wolfSSL | 0:9d17e4342598 | 2177 | /* alias for destination word */ |
wolfSSL | 0:9d17e4342598 | 2178 | tmpx = x->dp; |
wolfSSL | 0:9d17e4342598 | 2179 | |
wolfSSL | 0:9d17e4342598 | 2180 | /* alias for shifted double precision result */ |
wolfSSL | 0:9d17e4342598 | 2181 | _W = W + n->used; |
wolfSSL | 0:9d17e4342598 | 2182 | |
wolfSSL | 0:9d17e4342598 | 2183 | for (ix = 0; ix < n->used + 1; ix++) { |
wolfSSL | 0:9d17e4342598 | 2184 | *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 2185 | } |
wolfSSL | 0:9d17e4342598 | 2186 | |
wolfSSL | 0:9d17e4342598 | 2187 | /* zero oldused digits, if the input a was larger than |
wolfSSL | 0:9d17e4342598 | 2188 | * m->used+1 we'll have to clear the digits |
wolfSSL | 0:9d17e4342598 | 2189 | */ |
wolfSSL | 0:9d17e4342598 | 2190 | for (; ix < olduse; ix++) { |
wolfSSL | 0:9d17e4342598 | 2191 | *tmpx++ = 0; |
wolfSSL | 0:9d17e4342598 | 2192 | } |
wolfSSL | 0:9d17e4342598 | 2193 | } |
wolfSSL | 0:9d17e4342598 | 2194 | |
wolfSSL | 0:9d17e4342598 | 2195 | /* set the max used and clamp */ |
wolfSSL | 0:9d17e4342598 | 2196 | x->used = n->used + 1; |
wolfSSL | 0:9d17e4342598 | 2197 | mp_clamp (x); |
wolfSSL | 0:9d17e4342598 | 2198 | |
wolfSSL | 0:9d17e4342598 | 2199 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2200 | XFREE(W, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2201 | #endif |
wolfSSL | 0:9d17e4342598 | 2202 | |
wolfSSL | 0:9d17e4342598 | 2203 | /* if A >= m then A = A - m */ |
wolfSSL | 0:9d17e4342598 | 2204 | if (mp_cmp_mag (x, n) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 2205 | return s_mp_sub (x, n, x); |
wolfSSL | 0:9d17e4342598 | 2206 | } |
wolfSSL | 0:9d17e4342598 | 2207 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2208 | } |
wolfSSL | 0:9d17e4342598 | 2209 | |
wolfSSL | 0:9d17e4342598 | 2210 | |
wolfSSL | 0:9d17e4342598 | 2211 | /* computes xR**-1 == x (mod N) via Montgomery Reduction */ |
wolfSSL | 0:9d17e4342598 | 2212 | int |
wolfSSL | 0:9d17e4342598 | 2213 | mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) |
wolfSSL | 0:9d17e4342598 | 2214 | { |
wolfSSL | 0:9d17e4342598 | 2215 | int ix, res, digs; |
wolfSSL | 0:9d17e4342598 | 2216 | mp_digit mu; |
wolfSSL | 0:9d17e4342598 | 2217 | |
wolfSSL | 0:9d17e4342598 | 2218 | /* can the fast reduction [comba] method be used? |
wolfSSL | 0:9d17e4342598 | 2219 | * |
wolfSSL | 0:9d17e4342598 | 2220 | * Note that unlike in mul you're safely allowed *less* |
wolfSSL | 0:9d17e4342598 | 2221 | * than the available columns [255 per default] since carries |
wolfSSL | 0:9d17e4342598 | 2222 | * are fixed up in the inner loop. |
wolfSSL | 0:9d17e4342598 | 2223 | */ |
wolfSSL | 0:9d17e4342598 | 2224 | digs = n->used * 2 + 1; |
wolfSSL | 0:9d17e4342598 | 2225 | if ((digs < MP_WARRAY) && |
wolfSSL | 0:9d17e4342598 | 2226 | n->used < |
wolfSSL | 0:9d17e4342598 | 2227 | (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
wolfSSL | 0:9d17e4342598 | 2228 | return fast_mp_montgomery_reduce (x, n, rho); |
wolfSSL | 0:9d17e4342598 | 2229 | } |
wolfSSL | 0:9d17e4342598 | 2230 | |
wolfSSL | 0:9d17e4342598 | 2231 | /* grow the input as required */ |
wolfSSL | 0:9d17e4342598 | 2232 | if (x->alloc < digs) { |
wolfSSL | 0:9d17e4342598 | 2233 | if ((res = mp_grow (x, digs)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2234 | return res; |
wolfSSL | 0:9d17e4342598 | 2235 | } |
wolfSSL | 0:9d17e4342598 | 2236 | } |
wolfSSL | 0:9d17e4342598 | 2237 | x->used = digs; |
wolfSSL | 0:9d17e4342598 | 2238 | |
wolfSSL | 0:9d17e4342598 | 2239 | for (ix = 0; ix < n->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 2240 | /* mu = ai * rho mod b |
wolfSSL | 0:9d17e4342598 | 2241 | * |
wolfSSL | 0:9d17e4342598 | 2242 | * The value of rho must be precalculated via |
wolfSSL | 0:9d17e4342598 | 2243 | * montgomery_setup() such that |
wolfSSL | 0:9d17e4342598 | 2244 | * it equals -1/n0 mod b this allows the |
wolfSSL | 0:9d17e4342598 | 2245 | * following inner loop to reduce the |
wolfSSL | 0:9d17e4342598 | 2246 | * input one digit at a time |
wolfSSL | 0:9d17e4342598 | 2247 | */ |
wolfSSL | 0:9d17e4342598 | 2248 | mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK); |
wolfSSL | 0:9d17e4342598 | 2249 | |
wolfSSL | 0:9d17e4342598 | 2250 | /* a = a + mu * m * b**i */ |
wolfSSL | 0:9d17e4342598 | 2251 | { |
wolfSSL | 0:9d17e4342598 | 2252 | register int iy; |
wolfSSL | 0:9d17e4342598 | 2253 | register mp_digit *tmpn, *tmpx, u; |
wolfSSL | 0:9d17e4342598 | 2254 | register mp_word r; |
wolfSSL | 0:9d17e4342598 | 2255 | |
wolfSSL | 0:9d17e4342598 | 2256 | /* alias for digits of the modulus */ |
wolfSSL | 0:9d17e4342598 | 2257 | tmpn = n->dp; |
wolfSSL | 0:9d17e4342598 | 2258 | |
wolfSSL | 0:9d17e4342598 | 2259 | /* alias for the digits of x [the input] */ |
wolfSSL | 0:9d17e4342598 | 2260 | tmpx = x->dp + ix; |
wolfSSL | 0:9d17e4342598 | 2261 | |
wolfSSL | 0:9d17e4342598 | 2262 | /* set the carry to zero */ |
wolfSSL | 0:9d17e4342598 | 2263 | u = 0; |
wolfSSL | 0:9d17e4342598 | 2264 | |
wolfSSL | 0:9d17e4342598 | 2265 | /* Multiply and add in place */ |
wolfSSL | 0:9d17e4342598 | 2266 | for (iy = 0; iy < n->used; iy++) { |
wolfSSL | 0:9d17e4342598 | 2267 | /* compute product and sum */ |
wolfSSL | 0:9d17e4342598 | 2268 | r = ((mp_word)mu) * ((mp_word)*tmpn++) + |
wolfSSL | 0:9d17e4342598 | 2269 | ((mp_word) u) + ((mp_word) * tmpx); |
wolfSSL | 0:9d17e4342598 | 2270 | |
wolfSSL | 0:9d17e4342598 | 2271 | /* get carry */ |
wolfSSL | 0:9d17e4342598 | 2272 | u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 2273 | |
wolfSSL | 0:9d17e4342598 | 2274 | /* fix digit */ |
wolfSSL | 0:9d17e4342598 | 2275 | *tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 2276 | } |
wolfSSL | 0:9d17e4342598 | 2277 | /* At this point the ix'th digit of x should be zero */ |
wolfSSL | 0:9d17e4342598 | 2278 | |
wolfSSL | 0:9d17e4342598 | 2279 | |
wolfSSL | 0:9d17e4342598 | 2280 | /* propagate carries upwards as required*/ |
wolfSSL | 0:9d17e4342598 | 2281 | while (u) { |
wolfSSL | 0:9d17e4342598 | 2282 | *tmpx += u; |
wolfSSL | 0:9d17e4342598 | 2283 | u = *tmpx >> DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 2284 | *tmpx++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 2285 | } |
wolfSSL | 0:9d17e4342598 | 2286 | } |
wolfSSL | 0:9d17e4342598 | 2287 | } |
wolfSSL | 0:9d17e4342598 | 2288 | |
wolfSSL | 0:9d17e4342598 | 2289 | /* at this point the n.used'th least |
wolfSSL | 0:9d17e4342598 | 2290 | * significant digits of x are all zero |
wolfSSL | 0:9d17e4342598 | 2291 | * which means we can shift x to the |
wolfSSL | 0:9d17e4342598 | 2292 | * right by n.used digits and the |
wolfSSL | 0:9d17e4342598 | 2293 | * residue is unchanged. |
wolfSSL | 0:9d17e4342598 | 2294 | */ |
wolfSSL | 0:9d17e4342598 | 2295 | |
wolfSSL | 0:9d17e4342598 | 2296 | /* x = x/b**n.used */ |
wolfSSL | 0:9d17e4342598 | 2297 | mp_clamp(x); |
wolfSSL | 0:9d17e4342598 | 2298 | mp_rshd (x, n->used); |
wolfSSL | 0:9d17e4342598 | 2299 | |
wolfSSL | 0:9d17e4342598 | 2300 | /* if x >= n then x = x - n */ |
wolfSSL | 0:9d17e4342598 | 2301 | if (mp_cmp_mag (x, n) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 2302 | return s_mp_sub (x, n, x); |
wolfSSL | 0:9d17e4342598 | 2303 | } |
wolfSSL | 0:9d17e4342598 | 2304 | |
wolfSSL | 0:9d17e4342598 | 2305 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2306 | } |
wolfSSL | 0:9d17e4342598 | 2307 | |
wolfSSL | 0:9d17e4342598 | 2308 | |
wolfSSL | 0:9d17e4342598 | 2309 | /* determines the setup value */ |
wolfSSL | 0:9d17e4342598 | 2310 | void mp_dr_setup(mp_int *a, mp_digit *d) |
wolfSSL | 0:9d17e4342598 | 2311 | { |
wolfSSL | 0:9d17e4342598 | 2312 | /* the casts are required if DIGIT_BIT is one less than |
wolfSSL | 0:9d17e4342598 | 2313 | * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] |
wolfSSL | 0:9d17e4342598 | 2314 | */ |
wolfSSL | 0:9d17e4342598 | 2315 | *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - |
wolfSSL | 0:9d17e4342598 | 2316 | ((mp_word)a->dp[0])); |
wolfSSL | 0:9d17e4342598 | 2317 | } |
wolfSSL | 0:9d17e4342598 | 2318 | |
wolfSSL | 0:9d17e4342598 | 2319 | |
wolfSSL | 0:9d17e4342598 | 2320 | /* reduce "x" in place modulo "n" using the Diminished Radix algorithm. |
wolfSSL | 0:9d17e4342598 | 2321 | * |
wolfSSL | 0:9d17e4342598 | 2322 | * Based on algorithm from the paper |
wolfSSL | 0:9d17e4342598 | 2323 | * |
wolfSSL | 0:9d17e4342598 | 2324 | * "Generating Efficient Primes for Discrete Log Cryptosystems" |
wolfSSL | 0:9d17e4342598 | 2325 | * Chae Hoon Lim, Pil Joong Lee, |
wolfSSL | 0:9d17e4342598 | 2326 | * POSTECH Information Research Laboratories |
wolfSSL | 0:9d17e4342598 | 2327 | * |
wolfSSL | 0:9d17e4342598 | 2328 | * The modulus must be of a special format [see manual] |
wolfSSL | 0:9d17e4342598 | 2329 | * |
wolfSSL | 0:9d17e4342598 | 2330 | * Has been modified to use algorithm 7.10 from the LTM book instead |
wolfSSL | 0:9d17e4342598 | 2331 | * |
wolfSSL | 0:9d17e4342598 | 2332 | * Input x must be in the range 0 <= x <= (n-1)**2 |
wolfSSL | 0:9d17e4342598 | 2333 | */ |
wolfSSL | 0:9d17e4342598 | 2334 | int |
wolfSSL | 0:9d17e4342598 | 2335 | mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k) |
wolfSSL | 0:9d17e4342598 | 2336 | { |
wolfSSL | 0:9d17e4342598 | 2337 | int err, i, m; |
wolfSSL | 0:9d17e4342598 | 2338 | mp_word r; |
wolfSSL | 0:9d17e4342598 | 2339 | mp_digit mu, *tmpx1, *tmpx2; |
wolfSSL | 0:9d17e4342598 | 2340 | |
wolfSSL | 0:9d17e4342598 | 2341 | /* m = digits in modulus */ |
wolfSSL | 0:9d17e4342598 | 2342 | m = n->used; |
wolfSSL | 0:9d17e4342598 | 2343 | |
wolfSSL | 0:9d17e4342598 | 2344 | /* ensure that "x" has at least 2m digits */ |
wolfSSL | 0:9d17e4342598 | 2345 | if (x->alloc < m + m) { |
wolfSSL | 0:9d17e4342598 | 2346 | if ((err = mp_grow (x, m + m)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2347 | return err; |
wolfSSL | 0:9d17e4342598 | 2348 | } |
wolfSSL | 0:9d17e4342598 | 2349 | } |
wolfSSL | 0:9d17e4342598 | 2350 | |
wolfSSL | 0:9d17e4342598 | 2351 | /* top of loop, this is where the code resumes if |
wolfSSL | 0:9d17e4342598 | 2352 | * another reduction pass is required. |
wolfSSL | 0:9d17e4342598 | 2353 | */ |
wolfSSL | 0:9d17e4342598 | 2354 | top: |
wolfSSL | 0:9d17e4342598 | 2355 | /* aliases for digits */ |
wolfSSL | 0:9d17e4342598 | 2356 | /* alias for lower half of x */ |
wolfSSL | 0:9d17e4342598 | 2357 | tmpx1 = x->dp; |
wolfSSL | 0:9d17e4342598 | 2358 | |
wolfSSL | 0:9d17e4342598 | 2359 | /* alias for upper half of x, or x/B**m */ |
wolfSSL | 0:9d17e4342598 | 2360 | tmpx2 = x->dp + m; |
wolfSSL | 0:9d17e4342598 | 2361 | |
wolfSSL | 0:9d17e4342598 | 2362 | /* set carry to zero */ |
wolfSSL | 0:9d17e4342598 | 2363 | mu = 0; |
wolfSSL | 0:9d17e4342598 | 2364 | |
wolfSSL | 0:9d17e4342598 | 2365 | /* compute (x mod B**m) + k * [x/B**m] inline and inplace */ |
wolfSSL | 0:9d17e4342598 | 2366 | for (i = 0; i < m; i++) { |
wolfSSL | 0:9d17e4342598 | 2367 | r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu; |
wolfSSL | 0:9d17e4342598 | 2368 | *tmpx1++ = (mp_digit)(r & MP_MASK); |
wolfSSL | 0:9d17e4342598 | 2369 | mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 2370 | } |
wolfSSL | 0:9d17e4342598 | 2371 | |
wolfSSL | 0:9d17e4342598 | 2372 | /* set final carry */ |
wolfSSL | 0:9d17e4342598 | 2373 | *tmpx1++ = mu; |
wolfSSL | 0:9d17e4342598 | 2374 | |
wolfSSL | 0:9d17e4342598 | 2375 | /* zero words above m */ |
wolfSSL | 0:9d17e4342598 | 2376 | for (i = m + 1; i < x->used; i++) { |
wolfSSL | 0:9d17e4342598 | 2377 | *tmpx1++ = 0; |
wolfSSL | 0:9d17e4342598 | 2378 | } |
wolfSSL | 0:9d17e4342598 | 2379 | |
wolfSSL | 0:9d17e4342598 | 2380 | /* clamp, sub and return */ |
wolfSSL | 0:9d17e4342598 | 2381 | mp_clamp (x); |
wolfSSL | 0:9d17e4342598 | 2382 | |
wolfSSL | 0:9d17e4342598 | 2383 | /* if x >= n then subtract and reduce again |
wolfSSL | 0:9d17e4342598 | 2384 | * Each successive "recursion" makes the input smaller and smaller. |
wolfSSL | 0:9d17e4342598 | 2385 | */ |
wolfSSL | 0:9d17e4342598 | 2386 | if (mp_cmp_mag (x, n) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 2387 | s_mp_sub(x, n, x); |
wolfSSL | 0:9d17e4342598 | 2388 | goto top; |
wolfSSL | 0:9d17e4342598 | 2389 | } |
wolfSSL | 0:9d17e4342598 | 2390 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2391 | } |
wolfSSL | 0:9d17e4342598 | 2392 | |
wolfSSL | 0:9d17e4342598 | 2393 | |
wolfSSL | 0:9d17e4342598 | 2394 | /* reduces a modulo n where n is of the form 2**p - d */ |
wolfSSL | 0:9d17e4342598 | 2395 | int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d) |
wolfSSL | 0:9d17e4342598 | 2396 | { |
wolfSSL | 0:9d17e4342598 | 2397 | mp_int q; |
wolfSSL | 0:9d17e4342598 | 2398 | int p, res; |
wolfSSL | 0:9d17e4342598 | 2399 | |
wolfSSL | 0:9d17e4342598 | 2400 | if ((res = mp_init(&q)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2401 | return res; |
wolfSSL | 0:9d17e4342598 | 2402 | } |
wolfSSL | 0:9d17e4342598 | 2403 | |
wolfSSL | 0:9d17e4342598 | 2404 | p = mp_count_bits(n); |
wolfSSL | 0:9d17e4342598 | 2405 | top: |
wolfSSL | 0:9d17e4342598 | 2406 | /* q = a/2**p, a = a mod 2**p */ |
wolfSSL | 0:9d17e4342598 | 2407 | if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2408 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 2409 | } |
wolfSSL | 0:9d17e4342598 | 2410 | |
wolfSSL | 0:9d17e4342598 | 2411 | if (d != 1) { |
wolfSSL | 0:9d17e4342598 | 2412 | /* q = q * d */ |
wolfSSL | 0:9d17e4342598 | 2413 | if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2414 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 2415 | } |
wolfSSL | 0:9d17e4342598 | 2416 | } |
wolfSSL | 0:9d17e4342598 | 2417 | |
wolfSSL | 0:9d17e4342598 | 2418 | /* a = a + q */ |
wolfSSL | 0:9d17e4342598 | 2419 | if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2420 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 2421 | } |
wolfSSL | 0:9d17e4342598 | 2422 | |
wolfSSL | 0:9d17e4342598 | 2423 | if (mp_cmp_mag(a, n) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 2424 | s_mp_sub(a, n, a); |
wolfSSL | 0:9d17e4342598 | 2425 | goto top; |
wolfSSL | 0:9d17e4342598 | 2426 | } |
wolfSSL | 0:9d17e4342598 | 2427 | |
wolfSSL | 0:9d17e4342598 | 2428 | ERR: |
wolfSSL | 0:9d17e4342598 | 2429 | mp_clear(&q); |
wolfSSL | 0:9d17e4342598 | 2430 | return res; |
wolfSSL | 0:9d17e4342598 | 2431 | } |
wolfSSL | 0:9d17e4342598 | 2432 | |
wolfSSL | 0:9d17e4342598 | 2433 | |
wolfSSL | 0:9d17e4342598 | 2434 | /* determines the setup value */ |
wolfSSL | 0:9d17e4342598 | 2435 | int mp_reduce_2k_setup(mp_int *a, mp_digit *d) |
wolfSSL | 0:9d17e4342598 | 2436 | { |
wolfSSL | 0:9d17e4342598 | 2437 | int res, p; |
wolfSSL | 0:9d17e4342598 | 2438 | mp_int tmp; |
wolfSSL | 0:9d17e4342598 | 2439 | |
wolfSSL | 0:9d17e4342598 | 2440 | if ((res = mp_init(&tmp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2441 | return res; |
wolfSSL | 0:9d17e4342598 | 2442 | } |
wolfSSL | 0:9d17e4342598 | 2443 | |
wolfSSL | 0:9d17e4342598 | 2444 | p = mp_count_bits(a); |
wolfSSL | 0:9d17e4342598 | 2445 | if ((res = mp_2expt(&tmp, p)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2446 | mp_clear(&tmp); |
wolfSSL | 0:9d17e4342598 | 2447 | return res; |
wolfSSL | 0:9d17e4342598 | 2448 | } |
wolfSSL | 0:9d17e4342598 | 2449 | |
wolfSSL | 0:9d17e4342598 | 2450 | if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2451 | mp_clear(&tmp); |
wolfSSL | 0:9d17e4342598 | 2452 | return res; |
wolfSSL | 0:9d17e4342598 | 2453 | } |
wolfSSL | 0:9d17e4342598 | 2454 | |
wolfSSL | 0:9d17e4342598 | 2455 | *d = tmp.dp[0]; |
wolfSSL | 0:9d17e4342598 | 2456 | mp_clear(&tmp); |
wolfSSL | 0:9d17e4342598 | 2457 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2458 | } |
wolfSSL | 0:9d17e4342598 | 2459 | |
wolfSSL | 0:9d17e4342598 | 2460 | |
wolfSSL | 0:9d17e4342598 | 2461 | /* computes a = 2**b |
wolfSSL | 0:9d17e4342598 | 2462 | * |
wolfSSL | 0:9d17e4342598 | 2463 | * Simple algorithm which zeroes the int, grows it then just sets one bit |
wolfSSL | 0:9d17e4342598 | 2464 | * as required. |
wolfSSL | 0:9d17e4342598 | 2465 | */ |
wolfSSL | 0:9d17e4342598 | 2466 | int |
wolfSSL | 0:9d17e4342598 | 2467 | mp_2expt (mp_int * a, int b) |
wolfSSL | 0:9d17e4342598 | 2468 | { |
wolfSSL | 0:9d17e4342598 | 2469 | int res; |
wolfSSL | 0:9d17e4342598 | 2470 | |
wolfSSL | 0:9d17e4342598 | 2471 | /* zero a as per default */ |
wolfSSL | 0:9d17e4342598 | 2472 | mp_zero (a); |
wolfSSL | 0:9d17e4342598 | 2473 | |
wolfSSL | 0:9d17e4342598 | 2474 | /* grow a to accomodate the single bit */ |
wolfSSL | 0:9d17e4342598 | 2475 | if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2476 | return res; |
wolfSSL | 0:9d17e4342598 | 2477 | } |
wolfSSL | 0:9d17e4342598 | 2478 | |
wolfSSL | 0:9d17e4342598 | 2479 | /* set the used count of where the bit will go */ |
wolfSSL | 0:9d17e4342598 | 2480 | a->used = b / DIGIT_BIT + 1; |
wolfSSL | 0:9d17e4342598 | 2481 | |
wolfSSL | 0:9d17e4342598 | 2482 | /* put the single bit in its place */ |
wolfSSL | 0:9d17e4342598 | 2483 | a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 2484 | |
wolfSSL | 0:9d17e4342598 | 2485 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2486 | } |
wolfSSL | 0:9d17e4342598 | 2487 | |
wolfSSL | 0:9d17e4342598 | 2488 | |
wolfSSL | 0:9d17e4342598 | 2489 | /* multiply by a digit */ |
wolfSSL | 0:9d17e4342598 | 2490 | int |
wolfSSL | 0:9d17e4342598 | 2491 | mp_mul_d (mp_int * a, mp_digit b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 2492 | { |
wolfSSL | 0:9d17e4342598 | 2493 | mp_digit u, *tmpa, *tmpc; |
wolfSSL | 0:9d17e4342598 | 2494 | mp_word r; |
wolfSSL | 0:9d17e4342598 | 2495 | int ix, res, olduse; |
wolfSSL | 0:9d17e4342598 | 2496 | |
wolfSSL | 0:9d17e4342598 | 2497 | /* make sure c is big enough to hold a*b */ |
wolfSSL | 0:9d17e4342598 | 2498 | if (c->alloc < a->used + 1) { |
wolfSSL | 0:9d17e4342598 | 2499 | if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2500 | return res; |
wolfSSL | 0:9d17e4342598 | 2501 | } |
wolfSSL | 0:9d17e4342598 | 2502 | } |
wolfSSL | 0:9d17e4342598 | 2503 | |
wolfSSL | 0:9d17e4342598 | 2504 | /* get the original destinations used count */ |
wolfSSL | 0:9d17e4342598 | 2505 | olduse = c->used; |
wolfSSL | 0:9d17e4342598 | 2506 | |
wolfSSL | 0:9d17e4342598 | 2507 | /* set the sign */ |
wolfSSL | 0:9d17e4342598 | 2508 | c->sign = a->sign; |
wolfSSL | 0:9d17e4342598 | 2509 | |
wolfSSL | 0:9d17e4342598 | 2510 | /* alias for a->dp [source] */ |
wolfSSL | 0:9d17e4342598 | 2511 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 2512 | |
wolfSSL | 0:9d17e4342598 | 2513 | /* alias for c->dp [dest] */ |
wolfSSL | 0:9d17e4342598 | 2514 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 2515 | |
wolfSSL | 0:9d17e4342598 | 2516 | /* zero carry */ |
wolfSSL | 0:9d17e4342598 | 2517 | u = 0; |
wolfSSL | 0:9d17e4342598 | 2518 | |
wolfSSL | 0:9d17e4342598 | 2519 | /* compute columns */ |
wolfSSL | 0:9d17e4342598 | 2520 | for (ix = 0; ix < a->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 2521 | /* compute product and carry sum for this term */ |
wolfSSL | 0:9d17e4342598 | 2522 | r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b); |
wolfSSL | 0:9d17e4342598 | 2523 | |
wolfSSL | 0:9d17e4342598 | 2524 | /* mask off higher bits to get a single digit */ |
wolfSSL | 0:9d17e4342598 | 2525 | *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 2526 | |
wolfSSL | 0:9d17e4342598 | 2527 | /* send carry into next iteration */ |
wolfSSL | 0:9d17e4342598 | 2528 | u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 2529 | } |
wolfSSL | 0:9d17e4342598 | 2530 | |
wolfSSL | 0:9d17e4342598 | 2531 | /* store final carry [if any] and increment ix offset */ |
wolfSSL | 0:9d17e4342598 | 2532 | *tmpc++ = u; |
wolfSSL | 0:9d17e4342598 | 2533 | ++ix; |
wolfSSL | 0:9d17e4342598 | 2534 | |
wolfSSL | 0:9d17e4342598 | 2535 | /* now zero digits above the top */ |
wolfSSL | 0:9d17e4342598 | 2536 | while (ix++ < olduse) { |
wolfSSL | 0:9d17e4342598 | 2537 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 2538 | } |
wolfSSL | 0:9d17e4342598 | 2539 | |
wolfSSL | 0:9d17e4342598 | 2540 | /* set used count */ |
wolfSSL | 0:9d17e4342598 | 2541 | c->used = a->used + 1; |
wolfSSL | 0:9d17e4342598 | 2542 | mp_clamp(c); |
wolfSSL | 0:9d17e4342598 | 2543 | |
wolfSSL | 0:9d17e4342598 | 2544 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2545 | } |
wolfSSL | 0:9d17e4342598 | 2546 | |
wolfSSL | 0:9d17e4342598 | 2547 | |
wolfSSL | 0:9d17e4342598 | 2548 | /* d = a * b (mod c) */ |
wolfSSL | 0:9d17e4342598 | 2549 | int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) |
wolfSSL | 0:9d17e4342598 | 2550 | { |
wolfSSL | 0:9d17e4342598 | 2551 | int res; |
wolfSSL | 0:9d17e4342598 | 2552 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 2553 | |
wolfSSL | 0:9d17e4342598 | 2554 | if ((res = mp_init (&t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2555 | return res; |
wolfSSL | 0:9d17e4342598 | 2556 | } |
wolfSSL | 0:9d17e4342598 | 2557 | |
wolfSSL | 0:9d17e4342598 | 2558 | if ((res = mp_mul (a, b, &t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2559 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 2560 | return res; |
wolfSSL | 0:9d17e4342598 | 2561 | } |
wolfSSL | 0:9d17e4342598 | 2562 | res = mp_mod (&t, c, d); |
wolfSSL | 0:9d17e4342598 | 2563 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 2564 | return res; |
wolfSSL | 0:9d17e4342598 | 2565 | } |
wolfSSL | 0:9d17e4342598 | 2566 | |
wolfSSL | 0:9d17e4342598 | 2567 | |
wolfSSL | 0:9d17e4342598 | 2568 | /* computes b = a*a */ |
wolfSSL | 0:9d17e4342598 | 2569 | int |
wolfSSL | 0:9d17e4342598 | 2570 | mp_sqr (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 2571 | { |
wolfSSL | 0:9d17e4342598 | 2572 | int res; |
wolfSSL | 0:9d17e4342598 | 2573 | |
wolfSSL | 0:9d17e4342598 | 2574 | { |
wolfSSL | 0:9d17e4342598 | 2575 | #ifdef BN_FAST_S_MP_SQR_C |
wolfSSL | 0:9d17e4342598 | 2576 | /* can we use the fast comba multiplier? */ |
wolfSSL | 0:9d17e4342598 | 2577 | if ((a->used * 2 + 1) < MP_WARRAY && |
wolfSSL | 0:9d17e4342598 | 2578 | a->used < |
wolfSSL | 0:9d17e4342598 | 2579 | (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) { |
wolfSSL | 0:9d17e4342598 | 2580 | res = fast_s_mp_sqr (a, b); |
wolfSSL | 0:9d17e4342598 | 2581 | } else |
wolfSSL | 0:9d17e4342598 | 2582 | #endif |
wolfSSL | 0:9d17e4342598 | 2583 | #ifdef BN_S_MP_SQR_C |
wolfSSL | 0:9d17e4342598 | 2584 | res = s_mp_sqr (a, b); |
wolfSSL | 0:9d17e4342598 | 2585 | #else |
wolfSSL | 0:9d17e4342598 | 2586 | res = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 2587 | #endif |
wolfSSL | 0:9d17e4342598 | 2588 | } |
wolfSSL | 0:9d17e4342598 | 2589 | b->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 2590 | return res; |
wolfSSL | 0:9d17e4342598 | 2591 | } |
wolfSSL | 0:9d17e4342598 | 2592 | |
wolfSSL | 0:9d17e4342598 | 2593 | |
wolfSSL | 0:9d17e4342598 | 2594 | /* high level multiplication (handles sign) */ |
wolfSSL | 0:9d17e4342598 | 2595 | int mp_mul (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 2596 | { |
wolfSSL | 0:9d17e4342598 | 2597 | int res, neg; |
wolfSSL | 0:9d17e4342598 | 2598 | neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; |
wolfSSL | 0:9d17e4342598 | 2599 | |
wolfSSL | 0:9d17e4342598 | 2600 | { |
wolfSSL | 0:9d17e4342598 | 2601 | /* can we use the fast multiplier? |
wolfSSL | 0:9d17e4342598 | 2602 | * |
wolfSSL | 0:9d17e4342598 | 2603 | * The fast multiplier can be used if the output will |
wolfSSL | 0:9d17e4342598 | 2604 | * have less than MP_WARRAY digits and the number of |
wolfSSL | 0:9d17e4342598 | 2605 | * digits won't affect carry propagation |
wolfSSL | 0:9d17e4342598 | 2606 | */ |
wolfSSL | 0:9d17e4342598 | 2607 | int digs = a->used + b->used + 1; |
wolfSSL | 0:9d17e4342598 | 2608 | |
wolfSSL | 0:9d17e4342598 | 2609 | #ifdef BN_FAST_S_MP_MUL_DIGS_C |
wolfSSL | 0:9d17e4342598 | 2610 | if ((digs < MP_WARRAY) && |
wolfSSL | 0:9d17e4342598 | 2611 | MIN(a->used, b->used) <= |
wolfSSL | 0:9d17e4342598 | 2612 | (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
wolfSSL | 0:9d17e4342598 | 2613 | res = fast_s_mp_mul_digs (a, b, c, digs); |
wolfSSL | 0:9d17e4342598 | 2614 | } else |
wolfSSL | 0:9d17e4342598 | 2615 | #endif |
wolfSSL | 0:9d17e4342598 | 2616 | #ifdef BN_S_MP_MUL_DIGS_C |
wolfSSL | 0:9d17e4342598 | 2617 | res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */ |
wolfSSL | 0:9d17e4342598 | 2618 | #else |
wolfSSL | 0:9d17e4342598 | 2619 | res = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 2620 | #endif |
wolfSSL | 0:9d17e4342598 | 2621 | |
wolfSSL | 0:9d17e4342598 | 2622 | } |
wolfSSL | 0:9d17e4342598 | 2623 | c->sign = (c->used > 0) ? neg : MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 2624 | return res; |
wolfSSL | 0:9d17e4342598 | 2625 | } |
wolfSSL | 0:9d17e4342598 | 2626 | |
wolfSSL | 0:9d17e4342598 | 2627 | |
wolfSSL | 0:9d17e4342598 | 2628 | /* b = a*2 */ |
wolfSSL | 0:9d17e4342598 | 2629 | int mp_mul_2(mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 2630 | { |
wolfSSL | 0:9d17e4342598 | 2631 | int x, res, oldused; |
wolfSSL | 0:9d17e4342598 | 2632 | |
wolfSSL | 0:9d17e4342598 | 2633 | /* grow to accomodate result */ |
wolfSSL | 0:9d17e4342598 | 2634 | if (b->alloc < a->used + 1) { |
wolfSSL | 0:9d17e4342598 | 2635 | if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2636 | return res; |
wolfSSL | 0:9d17e4342598 | 2637 | } |
wolfSSL | 0:9d17e4342598 | 2638 | } |
wolfSSL | 0:9d17e4342598 | 2639 | |
wolfSSL | 0:9d17e4342598 | 2640 | oldused = b->used; |
wolfSSL | 0:9d17e4342598 | 2641 | b->used = a->used; |
wolfSSL | 0:9d17e4342598 | 2642 | |
wolfSSL | 0:9d17e4342598 | 2643 | { |
wolfSSL | 0:9d17e4342598 | 2644 | register mp_digit r, rr, *tmpa, *tmpb; |
wolfSSL | 0:9d17e4342598 | 2645 | |
wolfSSL | 0:9d17e4342598 | 2646 | /* alias for source */ |
wolfSSL | 0:9d17e4342598 | 2647 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 2648 | |
wolfSSL | 0:9d17e4342598 | 2649 | /* alias for dest */ |
wolfSSL | 0:9d17e4342598 | 2650 | tmpb = b->dp; |
wolfSSL | 0:9d17e4342598 | 2651 | |
wolfSSL | 0:9d17e4342598 | 2652 | /* carry */ |
wolfSSL | 0:9d17e4342598 | 2653 | r = 0; |
wolfSSL | 0:9d17e4342598 | 2654 | for (x = 0; x < a->used; x++) { |
wolfSSL | 0:9d17e4342598 | 2655 | |
wolfSSL | 0:9d17e4342598 | 2656 | /* get what will be the *next* carry bit from the |
wolfSSL | 0:9d17e4342598 | 2657 | * MSB of the current digit |
wolfSSL | 0:9d17e4342598 | 2658 | */ |
wolfSSL | 0:9d17e4342598 | 2659 | rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); |
wolfSSL | 0:9d17e4342598 | 2660 | |
wolfSSL | 0:9d17e4342598 | 2661 | /* now shift up this digit, add in the carry [from the previous] */ |
wolfSSL | 0:9d17e4342598 | 2662 | *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 2663 | |
wolfSSL | 0:9d17e4342598 | 2664 | /* copy the carry that would be from the source |
wolfSSL | 0:9d17e4342598 | 2665 | * digit into the next iteration |
wolfSSL | 0:9d17e4342598 | 2666 | */ |
wolfSSL | 0:9d17e4342598 | 2667 | r = rr; |
wolfSSL | 0:9d17e4342598 | 2668 | } |
wolfSSL | 0:9d17e4342598 | 2669 | |
wolfSSL | 0:9d17e4342598 | 2670 | /* new leading digit? */ |
wolfSSL | 0:9d17e4342598 | 2671 | if (r != 0) { |
wolfSSL | 0:9d17e4342598 | 2672 | /* add a MSB which is always 1 at this point */ |
wolfSSL | 0:9d17e4342598 | 2673 | *tmpb = 1; |
wolfSSL | 0:9d17e4342598 | 2674 | ++(b->used); |
wolfSSL | 0:9d17e4342598 | 2675 | } |
wolfSSL | 0:9d17e4342598 | 2676 | |
wolfSSL | 0:9d17e4342598 | 2677 | /* now zero any excess digits on the destination |
wolfSSL | 0:9d17e4342598 | 2678 | * that we didn't write to |
wolfSSL | 0:9d17e4342598 | 2679 | */ |
wolfSSL | 0:9d17e4342598 | 2680 | tmpb = b->dp + b->used; |
wolfSSL | 0:9d17e4342598 | 2681 | for (x = b->used; x < oldused; x++) { |
wolfSSL | 0:9d17e4342598 | 2682 | *tmpb++ = 0; |
wolfSSL | 0:9d17e4342598 | 2683 | } |
wolfSSL | 0:9d17e4342598 | 2684 | } |
wolfSSL | 0:9d17e4342598 | 2685 | b->sign = a->sign; |
wolfSSL | 0:9d17e4342598 | 2686 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2687 | } |
wolfSSL | 0:9d17e4342598 | 2688 | |
wolfSSL | 0:9d17e4342598 | 2689 | |
wolfSSL | 0:9d17e4342598 | 2690 | /* divide by three (based on routine from MPI and the GMP manual) */ |
wolfSSL | 0:9d17e4342598 | 2691 | int |
wolfSSL | 0:9d17e4342598 | 2692 | mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) |
wolfSSL | 0:9d17e4342598 | 2693 | { |
wolfSSL | 0:9d17e4342598 | 2694 | mp_int q; |
wolfSSL | 0:9d17e4342598 | 2695 | mp_word w, t; |
wolfSSL | 0:9d17e4342598 | 2696 | mp_digit b; |
wolfSSL | 0:9d17e4342598 | 2697 | int res, ix; |
wolfSSL | 0:9d17e4342598 | 2698 | |
wolfSSL | 0:9d17e4342598 | 2699 | /* b = 2**DIGIT_BIT / 3 */ |
wolfSSL | 0:9d17e4342598 | 2700 | b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); |
wolfSSL | 0:9d17e4342598 | 2701 | |
wolfSSL | 0:9d17e4342598 | 2702 | if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2703 | return res; |
wolfSSL | 0:9d17e4342598 | 2704 | } |
wolfSSL | 0:9d17e4342598 | 2705 | |
wolfSSL | 0:9d17e4342598 | 2706 | q.used = a->used; |
wolfSSL | 0:9d17e4342598 | 2707 | q.sign = a->sign; |
wolfSSL | 0:9d17e4342598 | 2708 | w = 0; |
wolfSSL | 0:9d17e4342598 | 2709 | for (ix = a->used - 1; ix >= 0; ix--) { |
wolfSSL | 0:9d17e4342598 | 2710 | w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |
wolfSSL | 0:9d17e4342598 | 2711 | |
wolfSSL | 0:9d17e4342598 | 2712 | if (w >= 3) { |
wolfSSL | 0:9d17e4342598 | 2713 | /* multiply w by [1/3] */ |
wolfSSL | 0:9d17e4342598 | 2714 | t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 2715 | |
wolfSSL | 0:9d17e4342598 | 2716 | /* now subtract 3 * [w/3] from w, to get the remainder */ |
wolfSSL | 0:9d17e4342598 | 2717 | w -= t+t+t; |
wolfSSL | 0:9d17e4342598 | 2718 | |
wolfSSL | 0:9d17e4342598 | 2719 | /* fixup the remainder as required since |
wolfSSL | 0:9d17e4342598 | 2720 | * the optimization is not exact. |
wolfSSL | 0:9d17e4342598 | 2721 | */ |
wolfSSL | 0:9d17e4342598 | 2722 | while (w >= 3) { |
wolfSSL | 0:9d17e4342598 | 2723 | t += 1; |
wolfSSL | 0:9d17e4342598 | 2724 | w -= 3; |
wolfSSL | 0:9d17e4342598 | 2725 | } |
wolfSSL | 0:9d17e4342598 | 2726 | } else { |
wolfSSL | 0:9d17e4342598 | 2727 | t = 0; |
wolfSSL | 0:9d17e4342598 | 2728 | } |
wolfSSL | 0:9d17e4342598 | 2729 | q.dp[ix] = (mp_digit)t; |
wolfSSL | 0:9d17e4342598 | 2730 | } |
wolfSSL | 0:9d17e4342598 | 2731 | |
wolfSSL | 0:9d17e4342598 | 2732 | /* [optional] store the remainder */ |
wolfSSL | 0:9d17e4342598 | 2733 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 2734 | *d = (mp_digit)w; |
wolfSSL | 0:9d17e4342598 | 2735 | } |
wolfSSL | 0:9d17e4342598 | 2736 | |
wolfSSL | 0:9d17e4342598 | 2737 | /* [optional] store the quotient */ |
wolfSSL | 0:9d17e4342598 | 2738 | if (c != NULL) { |
wolfSSL | 0:9d17e4342598 | 2739 | mp_clamp(&q); |
wolfSSL | 0:9d17e4342598 | 2740 | mp_exch(&q, c); |
wolfSSL | 0:9d17e4342598 | 2741 | } |
wolfSSL | 0:9d17e4342598 | 2742 | mp_clear(&q); |
wolfSSL | 0:9d17e4342598 | 2743 | |
wolfSSL | 0:9d17e4342598 | 2744 | return res; |
wolfSSL | 0:9d17e4342598 | 2745 | } |
wolfSSL | 0:9d17e4342598 | 2746 | |
wolfSSL | 0:9d17e4342598 | 2747 | |
wolfSSL | 0:9d17e4342598 | 2748 | /* init an mp_init for a given size */ |
wolfSSL | 0:9d17e4342598 | 2749 | int mp_init_size (mp_int * a, int size) |
wolfSSL | 0:9d17e4342598 | 2750 | { |
wolfSSL | 0:9d17e4342598 | 2751 | int x; |
wolfSSL | 0:9d17e4342598 | 2752 | |
wolfSSL | 0:9d17e4342598 | 2753 | /* pad size so there are always extra digits */ |
wolfSSL | 0:9d17e4342598 | 2754 | size += (MP_PREC * 2) - (size % MP_PREC); |
wolfSSL | 0:9d17e4342598 | 2755 | |
wolfSSL | 0:9d17e4342598 | 2756 | /* alloc mem */ |
wolfSSL | 0:9d17e4342598 | 2757 | a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size, 0, |
wolfSSL | 0:9d17e4342598 | 2758 | DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2759 | if (a->dp == NULL) { |
wolfSSL | 0:9d17e4342598 | 2760 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 2761 | } |
wolfSSL | 0:9d17e4342598 | 2762 | |
wolfSSL | 0:9d17e4342598 | 2763 | /* set the members */ |
wolfSSL | 0:9d17e4342598 | 2764 | a->used = 0; |
wolfSSL | 0:9d17e4342598 | 2765 | a->alloc = size; |
wolfSSL | 0:9d17e4342598 | 2766 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 2767 | |
wolfSSL | 0:9d17e4342598 | 2768 | /* zero the digits */ |
wolfSSL | 0:9d17e4342598 | 2769 | for (x = 0; x < size; x++) { |
wolfSSL | 0:9d17e4342598 | 2770 | a->dp[x] = 0; |
wolfSSL | 0:9d17e4342598 | 2771 | } |
wolfSSL | 0:9d17e4342598 | 2772 | |
wolfSSL | 0:9d17e4342598 | 2773 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2774 | } |
wolfSSL | 0:9d17e4342598 | 2775 | |
wolfSSL | 0:9d17e4342598 | 2776 | |
wolfSSL | 0:9d17e4342598 | 2777 | /* the jist of squaring... |
wolfSSL | 0:9d17e4342598 | 2778 | * you do like mult except the offset of the tmpx [one that |
wolfSSL | 0:9d17e4342598 | 2779 | * starts closer to zero] can't equal the offset of tmpy. |
wolfSSL | 0:9d17e4342598 | 2780 | * So basically you set up iy like before then you min it with |
wolfSSL | 0:9d17e4342598 | 2781 | * (ty-tx) so that it never happens. You double all those |
wolfSSL | 0:9d17e4342598 | 2782 | * you add in the inner loop |
wolfSSL | 0:9d17e4342598 | 2783 | |
wolfSSL | 0:9d17e4342598 | 2784 | After that loop you do the squares and add them in. |
wolfSSL | 0:9d17e4342598 | 2785 | */ |
wolfSSL | 0:9d17e4342598 | 2786 | |
wolfSSL | 0:9d17e4342598 | 2787 | int fast_s_mp_sqr (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 2788 | { |
wolfSSL | 0:9d17e4342598 | 2789 | int olduse, res, pa, ix, iz; |
wolfSSL | 0:9d17e4342598 | 2790 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2791 | mp_digit* W; /* uses dynamic memory and slower */ |
wolfSSL | 0:9d17e4342598 | 2792 | #else |
wolfSSL | 0:9d17e4342598 | 2793 | mp_digit W[MP_WARRAY]; |
wolfSSL | 0:9d17e4342598 | 2794 | #endif |
wolfSSL | 0:9d17e4342598 | 2795 | mp_digit *tmpx; |
wolfSSL | 0:9d17e4342598 | 2796 | mp_word W1; |
wolfSSL | 0:9d17e4342598 | 2797 | |
wolfSSL | 0:9d17e4342598 | 2798 | /* grow the destination as required */ |
wolfSSL | 0:9d17e4342598 | 2799 | pa = a->used + a->used; |
wolfSSL | 0:9d17e4342598 | 2800 | if (b->alloc < pa) { |
wolfSSL | 0:9d17e4342598 | 2801 | if ((res = mp_grow (b, pa)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2802 | return res; |
wolfSSL | 0:9d17e4342598 | 2803 | } |
wolfSSL | 0:9d17e4342598 | 2804 | } |
wolfSSL | 0:9d17e4342598 | 2805 | |
wolfSSL | 0:9d17e4342598 | 2806 | if (pa > MP_WARRAY) |
wolfSSL | 0:9d17e4342598 | 2807 | return MP_RANGE; /* TAO range check */ |
wolfSSL | 0:9d17e4342598 | 2808 | |
wolfSSL | 0:9d17e4342598 | 2809 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2810 | W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2811 | if (W == NULL) |
wolfSSL | 0:9d17e4342598 | 2812 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 2813 | #endif |
wolfSSL | 0:9d17e4342598 | 2814 | |
wolfSSL | 0:9d17e4342598 | 2815 | /* number of output digits to produce */ |
wolfSSL | 0:9d17e4342598 | 2816 | W1 = 0; |
wolfSSL | 0:9d17e4342598 | 2817 | for (ix = 0; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 2818 | int tx, ty, iy; |
wolfSSL | 0:9d17e4342598 | 2819 | mp_word _W; |
wolfSSL | 0:9d17e4342598 | 2820 | mp_digit *tmpy; |
wolfSSL | 0:9d17e4342598 | 2821 | |
wolfSSL | 0:9d17e4342598 | 2822 | /* clear counter */ |
wolfSSL | 0:9d17e4342598 | 2823 | _W = 0; |
wolfSSL | 0:9d17e4342598 | 2824 | |
wolfSSL | 0:9d17e4342598 | 2825 | /* get offsets into the two bignums */ |
wolfSSL | 0:9d17e4342598 | 2826 | ty = MIN(a->used-1, ix); |
wolfSSL | 0:9d17e4342598 | 2827 | tx = ix - ty; |
wolfSSL | 0:9d17e4342598 | 2828 | |
wolfSSL | 0:9d17e4342598 | 2829 | /* setup temp aliases */ |
wolfSSL | 0:9d17e4342598 | 2830 | tmpx = a->dp + tx; |
wolfSSL | 0:9d17e4342598 | 2831 | tmpy = a->dp + ty; |
wolfSSL | 0:9d17e4342598 | 2832 | |
wolfSSL | 0:9d17e4342598 | 2833 | /* this is the number of times the loop will iterrate, essentially |
wolfSSL | 0:9d17e4342598 | 2834 | while (tx++ < a->used && ty-- >= 0) { ... } |
wolfSSL | 0:9d17e4342598 | 2835 | */ |
wolfSSL | 0:9d17e4342598 | 2836 | iy = MIN(a->used-tx, ty+1); |
wolfSSL | 0:9d17e4342598 | 2837 | |
wolfSSL | 0:9d17e4342598 | 2838 | /* now for squaring tx can never equal ty |
wolfSSL | 0:9d17e4342598 | 2839 | * we halve the distance since they approach at a rate of 2x |
wolfSSL | 0:9d17e4342598 | 2840 | * and we have to round because odd cases need to be executed |
wolfSSL | 0:9d17e4342598 | 2841 | */ |
wolfSSL | 0:9d17e4342598 | 2842 | iy = MIN(iy, (ty-tx+1)>>1); |
wolfSSL | 0:9d17e4342598 | 2843 | |
wolfSSL | 0:9d17e4342598 | 2844 | /* execute loop */ |
wolfSSL | 0:9d17e4342598 | 2845 | for (iz = 0; iz < iy; iz++) { |
wolfSSL | 0:9d17e4342598 | 2846 | _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
wolfSSL | 0:9d17e4342598 | 2847 | } |
wolfSSL | 0:9d17e4342598 | 2848 | |
wolfSSL | 0:9d17e4342598 | 2849 | /* double the inner product and add carry */ |
wolfSSL | 0:9d17e4342598 | 2850 | _W = _W + _W + W1; |
wolfSSL | 0:9d17e4342598 | 2851 | |
wolfSSL | 0:9d17e4342598 | 2852 | /* even columns have the square term in them */ |
wolfSSL | 0:9d17e4342598 | 2853 | if ((ix&1) == 0) { |
wolfSSL | 0:9d17e4342598 | 2854 | _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]); |
wolfSSL | 0:9d17e4342598 | 2855 | } |
wolfSSL | 0:9d17e4342598 | 2856 | |
wolfSSL | 0:9d17e4342598 | 2857 | /* store it */ |
wolfSSL | 0:9d17e4342598 | 2858 | W[ix] = (mp_digit)(_W & MP_MASK); |
wolfSSL | 0:9d17e4342598 | 2859 | |
wolfSSL | 0:9d17e4342598 | 2860 | /* make next carry */ |
wolfSSL | 0:9d17e4342598 | 2861 | W1 = _W >> ((mp_word)DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 2862 | } |
wolfSSL | 0:9d17e4342598 | 2863 | |
wolfSSL | 0:9d17e4342598 | 2864 | /* setup dest */ |
wolfSSL | 0:9d17e4342598 | 2865 | olduse = b->used; |
wolfSSL | 0:9d17e4342598 | 2866 | b->used = a->used+a->used; |
wolfSSL | 0:9d17e4342598 | 2867 | |
wolfSSL | 0:9d17e4342598 | 2868 | { |
wolfSSL | 0:9d17e4342598 | 2869 | mp_digit *tmpb; |
wolfSSL | 0:9d17e4342598 | 2870 | tmpb = b->dp; |
wolfSSL | 0:9d17e4342598 | 2871 | for (ix = 0; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 2872 | *tmpb++ = W[ix] & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 2873 | } |
wolfSSL | 0:9d17e4342598 | 2874 | |
wolfSSL | 0:9d17e4342598 | 2875 | /* clear unused digits [that existed in the old copy of c] */ |
wolfSSL | 0:9d17e4342598 | 2876 | for (; ix < olduse; ix++) { |
wolfSSL | 0:9d17e4342598 | 2877 | *tmpb++ = 0; |
wolfSSL | 0:9d17e4342598 | 2878 | } |
wolfSSL | 0:9d17e4342598 | 2879 | } |
wolfSSL | 0:9d17e4342598 | 2880 | mp_clamp (b); |
wolfSSL | 0:9d17e4342598 | 2881 | |
wolfSSL | 0:9d17e4342598 | 2882 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2883 | XFREE(W, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2884 | #endif |
wolfSSL | 0:9d17e4342598 | 2885 | |
wolfSSL | 0:9d17e4342598 | 2886 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2887 | } |
wolfSSL | 0:9d17e4342598 | 2888 | |
wolfSSL | 0:9d17e4342598 | 2889 | |
wolfSSL | 0:9d17e4342598 | 2890 | /* Fast (comba) multiplier |
wolfSSL | 0:9d17e4342598 | 2891 | * |
wolfSSL | 0:9d17e4342598 | 2892 | * This is the fast column-array [comba] multiplier. It is |
wolfSSL | 0:9d17e4342598 | 2893 | * designed to compute the columns of the product first |
wolfSSL | 0:9d17e4342598 | 2894 | * then handle the carries afterwards. This has the effect |
wolfSSL | 0:9d17e4342598 | 2895 | * of making the nested loops that compute the columns very |
wolfSSL | 0:9d17e4342598 | 2896 | * simple and schedulable on super-scalar processors. |
wolfSSL | 0:9d17e4342598 | 2897 | * |
wolfSSL | 0:9d17e4342598 | 2898 | * This has been modified to produce a variable number of |
wolfSSL | 0:9d17e4342598 | 2899 | * digits of output so if say only a half-product is required |
wolfSSL | 0:9d17e4342598 | 2900 | * you don't have to compute the upper half (a feature |
wolfSSL | 0:9d17e4342598 | 2901 | * required for fast Barrett reduction). |
wolfSSL | 0:9d17e4342598 | 2902 | * |
wolfSSL | 0:9d17e4342598 | 2903 | * Based on Algorithm 14.12 on pp.595 of HAC. |
wolfSSL | 0:9d17e4342598 | 2904 | * |
wolfSSL | 0:9d17e4342598 | 2905 | */ |
wolfSSL | 0:9d17e4342598 | 2906 | int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
wolfSSL | 0:9d17e4342598 | 2907 | { |
wolfSSL | 0:9d17e4342598 | 2908 | int olduse, res, pa, ix, iz; |
wolfSSL | 0:9d17e4342598 | 2909 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2910 | mp_digit* W; /* uses dynamic memory and slower */ |
wolfSSL | 0:9d17e4342598 | 2911 | #else |
wolfSSL | 0:9d17e4342598 | 2912 | mp_digit W[MP_WARRAY]; |
wolfSSL | 0:9d17e4342598 | 2913 | #endif |
wolfSSL | 0:9d17e4342598 | 2914 | register mp_word _W; |
wolfSSL | 0:9d17e4342598 | 2915 | |
wolfSSL | 0:9d17e4342598 | 2916 | /* grow the destination as required */ |
wolfSSL | 0:9d17e4342598 | 2917 | if (c->alloc < digs) { |
wolfSSL | 0:9d17e4342598 | 2918 | if ((res = mp_grow (c, digs)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 2919 | return res; |
wolfSSL | 0:9d17e4342598 | 2920 | } |
wolfSSL | 0:9d17e4342598 | 2921 | } |
wolfSSL | 0:9d17e4342598 | 2922 | |
wolfSSL | 0:9d17e4342598 | 2923 | /* number of output digits to produce */ |
wolfSSL | 0:9d17e4342598 | 2924 | pa = MIN(digs, a->used + b->used); |
wolfSSL | 0:9d17e4342598 | 2925 | if (pa > MP_WARRAY) |
wolfSSL | 0:9d17e4342598 | 2926 | return MP_RANGE; /* TAO range check */ |
wolfSSL | 0:9d17e4342598 | 2927 | |
wolfSSL | 0:9d17e4342598 | 2928 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2929 | W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2930 | if (W == NULL) |
wolfSSL | 0:9d17e4342598 | 2931 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 2932 | #endif |
wolfSSL | 0:9d17e4342598 | 2933 | |
wolfSSL | 0:9d17e4342598 | 2934 | /* clear the carry */ |
wolfSSL | 0:9d17e4342598 | 2935 | _W = 0; |
wolfSSL | 0:9d17e4342598 | 2936 | for (ix = 0; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 2937 | int tx, ty; |
wolfSSL | 0:9d17e4342598 | 2938 | int iy; |
wolfSSL | 0:9d17e4342598 | 2939 | mp_digit *tmpx, *tmpy; |
wolfSSL | 0:9d17e4342598 | 2940 | |
wolfSSL | 0:9d17e4342598 | 2941 | /* get offsets into the two bignums */ |
wolfSSL | 0:9d17e4342598 | 2942 | ty = MIN(b->used-1, ix); |
wolfSSL | 0:9d17e4342598 | 2943 | tx = ix - ty; |
wolfSSL | 0:9d17e4342598 | 2944 | |
wolfSSL | 0:9d17e4342598 | 2945 | /* setup temp aliases */ |
wolfSSL | 0:9d17e4342598 | 2946 | tmpx = a->dp + tx; |
wolfSSL | 0:9d17e4342598 | 2947 | tmpy = b->dp + ty; |
wolfSSL | 0:9d17e4342598 | 2948 | |
wolfSSL | 0:9d17e4342598 | 2949 | /* this is the number of times the loop will iterrate, essentially |
wolfSSL | 0:9d17e4342598 | 2950 | while (tx++ < a->used && ty-- >= 0) { ... } |
wolfSSL | 0:9d17e4342598 | 2951 | */ |
wolfSSL | 0:9d17e4342598 | 2952 | iy = MIN(a->used-tx, ty+1); |
wolfSSL | 0:9d17e4342598 | 2953 | |
wolfSSL | 0:9d17e4342598 | 2954 | /* execute loop */ |
wolfSSL | 0:9d17e4342598 | 2955 | for (iz = 0; iz < iy; ++iz) { |
wolfSSL | 0:9d17e4342598 | 2956 | _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
wolfSSL | 0:9d17e4342598 | 2957 | |
wolfSSL | 0:9d17e4342598 | 2958 | } |
wolfSSL | 0:9d17e4342598 | 2959 | |
wolfSSL | 0:9d17e4342598 | 2960 | /* store term */ |
wolfSSL | 0:9d17e4342598 | 2961 | W[ix] = ((mp_digit)_W) & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 2962 | |
wolfSSL | 0:9d17e4342598 | 2963 | /* make next carry */ |
wolfSSL | 0:9d17e4342598 | 2964 | _W = _W >> ((mp_word)DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 2965 | } |
wolfSSL | 0:9d17e4342598 | 2966 | |
wolfSSL | 0:9d17e4342598 | 2967 | /* setup dest */ |
wolfSSL | 0:9d17e4342598 | 2968 | olduse = c->used; |
wolfSSL | 0:9d17e4342598 | 2969 | c->used = pa; |
wolfSSL | 0:9d17e4342598 | 2970 | |
wolfSSL | 0:9d17e4342598 | 2971 | { |
wolfSSL | 0:9d17e4342598 | 2972 | register mp_digit *tmpc; |
wolfSSL | 0:9d17e4342598 | 2973 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 2974 | for (ix = 0; ix < pa+1; ix++) { |
wolfSSL | 0:9d17e4342598 | 2975 | /* now extract the previous digit [below the carry] */ |
wolfSSL | 0:9d17e4342598 | 2976 | *tmpc++ = W[ix]; |
wolfSSL | 0:9d17e4342598 | 2977 | } |
wolfSSL | 0:9d17e4342598 | 2978 | |
wolfSSL | 0:9d17e4342598 | 2979 | /* clear unused digits [that existed in the old copy of c] */ |
wolfSSL | 0:9d17e4342598 | 2980 | for (; ix < olduse; ix++) { |
wolfSSL | 0:9d17e4342598 | 2981 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 2982 | } |
wolfSSL | 0:9d17e4342598 | 2983 | } |
wolfSSL | 0:9d17e4342598 | 2984 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 2985 | |
wolfSSL | 0:9d17e4342598 | 2986 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 2987 | XFREE(W, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 2988 | #endif |
wolfSSL | 0:9d17e4342598 | 2989 | |
wolfSSL | 0:9d17e4342598 | 2990 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 2991 | } |
wolfSSL | 0:9d17e4342598 | 2992 | |
wolfSSL | 0:9d17e4342598 | 2993 | |
wolfSSL | 0:9d17e4342598 | 2994 | /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ |
wolfSSL | 0:9d17e4342598 | 2995 | int s_mp_sqr (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 2996 | { |
wolfSSL | 0:9d17e4342598 | 2997 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 2998 | int res, ix, iy, pa; |
wolfSSL | 0:9d17e4342598 | 2999 | mp_word r; |
wolfSSL | 0:9d17e4342598 | 3000 | mp_digit u, tmpx, *tmpt; |
wolfSSL | 0:9d17e4342598 | 3001 | |
wolfSSL | 0:9d17e4342598 | 3002 | pa = a->used; |
wolfSSL | 0:9d17e4342598 | 3003 | if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3004 | return res; |
wolfSSL | 0:9d17e4342598 | 3005 | } |
wolfSSL | 0:9d17e4342598 | 3006 | |
wolfSSL | 0:9d17e4342598 | 3007 | /* default used is maximum possible size */ |
wolfSSL | 0:9d17e4342598 | 3008 | t.used = 2*pa + 1; |
wolfSSL | 0:9d17e4342598 | 3009 | |
wolfSSL | 0:9d17e4342598 | 3010 | for (ix = 0; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 3011 | /* first calculate the digit at 2*ix */ |
wolfSSL | 0:9d17e4342598 | 3012 | /* calculate double precision result */ |
wolfSSL | 0:9d17e4342598 | 3013 | r = ((mp_word) t.dp[2*ix]) + |
wolfSSL | 0:9d17e4342598 | 3014 | ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); |
wolfSSL | 0:9d17e4342598 | 3015 | |
wolfSSL | 0:9d17e4342598 | 3016 | /* store lower part in result */ |
wolfSSL | 0:9d17e4342598 | 3017 | t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 3018 | |
wolfSSL | 0:9d17e4342598 | 3019 | /* get the carry */ |
wolfSSL | 0:9d17e4342598 | 3020 | u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 3021 | |
wolfSSL | 0:9d17e4342598 | 3022 | /* left hand side of A[ix] * A[iy] */ |
wolfSSL | 0:9d17e4342598 | 3023 | tmpx = a->dp[ix]; |
wolfSSL | 0:9d17e4342598 | 3024 | |
wolfSSL | 0:9d17e4342598 | 3025 | /* alias for where to store the results */ |
wolfSSL | 0:9d17e4342598 | 3026 | tmpt = t.dp + (2*ix + 1); |
wolfSSL | 0:9d17e4342598 | 3027 | |
wolfSSL | 0:9d17e4342598 | 3028 | for (iy = ix + 1; iy < pa; iy++) { |
wolfSSL | 0:9d17e4342598 | 3029 | /* first calculate the product */ |
wolfSSL | 0:9d17e4342598 | 3030 | r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); |
wolfSSL | 0:9d17e4342598 | 3031 | |
wolfSSL | 0:9d17e4342598 | 3032 | /* now calculate the double precision result, note we use |
wolfSSL | 0:9d17e4342598 | 3033 | * addition instead of *2 since it's easier to optimize |
wolfSSL | 0:9d17e4342598 | 3034 | */ |
wolfSSL | 0:9d17e4342598 | 3035 | r = ((mp_word) *tmpt) + r + r + ((mp_word) u); |
wolfSSL | 0:9d17e4342598 | 3036 | |
wolfSSL | 0:9d17e4342598 | 3037 | /* store lower part */ |
wolfSSL | 0:9d17e4342598 | 3038 | *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 3039 | |
wolfSSL | 0:9d17e4342598 | 3040 | /* get carry */ |
wolfSSL | 0:9d17e4342598 | 3041 | u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 3042 | } |
wolfSSL | 0:9d17e4342598 | 3043 | /* propagate upwards */ |
wolfSSL | 0:9d17e4342598 | 3044 | while (u != ((mp_digit) 0)) { |
wolfSSL | 0:9d17e4342598 | 3045 | r = ((mp_word) *tmpt) + ((mp_word) u); |
wolfSSL | 0:9d17e4342598 | 3046 | *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 3047 | u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 3048 | } |
wolfSSL | 0:9d17e4342598 | 3049 | } |
wolfSSL | 0:9d17e4342598 | 3050 | |
wolfSSL | 0:9d17e4342598 | 3051 | mp_clamp (&t); |
wolfSSL | 0:9d17e4342598 | 3052 | mp_exch (&t, b); |
wolfSSL | 0:9d17e4342598 | 3053 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 3054 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3055 | } |
wolfSSL | 0:9d17e4342598 | 3056 | |
wolfSSL | 0:9d17e4342598 | 3057 | |
wolfSSL | 0:9d17e4342598 | 3058 | /* multiplies |a| * |b| and only computes upto digs digits of result |
wolfSSL | 0:9d17e4342598 | 3059 | * HAC pp. 595, Algorithm 14.12 Modified so you can control how |
wolfSSL | 0:9d17e4342598 | 3060 | * many digits of output are created. |
wolfSSL | 0:9d17e4342598 | 3061 | */ |
wolfSSL | 0:9d17e4342598 | 3062 | int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
wolfSSL | 0:9d17e4342598 | 3063 | { |
wolfSSL | 0:9d17e4342598 | 3064 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 3065 | int res, pa, pb, ix, iy; |
wolfSSL | 0:9d17e4342598 | 3066 | mp_digit u; |
wolfSSL | 0:9d17e4342598 | 3067 | mp_word r; |
wolfSSL | 0:9d17e4342598 | 3068 | mp_digit tmpx, *tmpt, *tmpy; |
wolfSSL | 0:9d17e4342598 | 3069 | |
wolfSSL | 0:9d17e4342598 | 3070 | /* can we use the fast multiplier? */ |
wolfSSL | 0:9d17e4342598 | 3071 | if (((digs) < MP_WARRAY) && |
wolfSSL | 0:9d17e4342598 | 3072 | MIN (a->used, b->used) < |
wolfSSL | 0:9d17e4342598 | 3073 | (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
wolfSSL | 0:9d17e4342598 | 3074 | return fast_s_mp_mul_digs (a, b, c, digs); |
wolfSSL | 0:9d17e4342598 | 3075 | } |
wolfSSL | 0:9d17e4342598 | 3076 | |
wolfSSL | 0:9d17e4342598 | 3077 | if ((res = mp_init_size (&t, digs)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3078 | return res; |
wolfSSL | 0:9d17e4342598 | 3079 | } |
wolfSSL | 0:9d17e4342598 | 3080 | t.used = digs; |
wolfSSL | 0:9d17e4342598 | 3081 | |
wolfSSL | 0:9d17e4342598 | 3082 | /* compute the digits of the product directly */ |
wolfSSL | 0:9d17e4342598 | 3083 | pa = a->used; |
wolfSSL | 0:9d17e4342598 | 3084 | for (ix = 0; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 3085 | /* set the carry to zero */ |
wolfSSL | 0:9d17e4342598 | 3086 | u = 0; |
wolfSSL | 0:9d17e4342598 | 3087 | |
wolfSSL | 0:9d17e4342598 | 3088 | /* limit ourselves to making digs digits of output */ |
wolfSSL | 0:9d17e4342598 | 3089 | pb = MIN (b->used, digs - ix); |
wolfSSL | 0:9d17e4342598 | 3090 | |
wolfSSL | 0:9d17e4342598 | 3091 | /* setup some aliases */ |
wolfSSL | 0:9d17e4342598 | 3092 | /* copy of the digit from a used within the nested loop */ |
wolfSSL | 0:9d17e4342598 | 3093 | tmpx = a->dp[ix]; |
wolfSSL | 0:9d17e4342598 | 3094 | |
wolfSSL | 0:9d17e4342598 | 3095 | /* an alias for the destination shifted ix places */ |
wolfSSL | 0:9d17e4342598 | 3096 | tmpt = t.dp + ix; |
wolfSSL | 0:9d17e4342598 | 3097 | |
wolfSSL | 0:9d17e4342598 | 3098 | /* an alias for the digits of b */ |
wolfSSL | 0:9d17e4342598 | 3099 | tmpy = b->dp; |
wolfSSL | 0:9d17e4342598 | 3100 | |
wolfSSL | 0:9d17e4342598 | 3101 | /* compute the columns of the output and propagate the carry */ |
wolfSSL | 0:9d17e4342598 | 3102 | for (iy = 0; iy < pb; iy++) { |
wolfSSL | 0:9d17e4342598 | 3103 | /* compute the column as a mp_word */ |
wolfSSL | 0:9d17e4342598 | 3104 | r = ((mp_word)*tmpt) + |
wolfSSL | 0:9d17e4342598 | 3105 | ((mp_word)tmpx) * ((mp_word)*tmpy++) + |
wolfSSL | 0:9d17e4342598 | 3106 | ((mp_word) u); |
wolfSSL | 0:9d17e4342598 | 3107 | |
wolfSSL | 0:9d17e4342598 | 3108 | /* the new column is the lower part of the result */ |
wolfSSL | 0:9d17e4342598 | 3109 | *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 3110 | |
wolfSSL | 0:9d17e4342598 | 3111 | /* get the carry word from the result */ |
wolfSSL | 0:9d17e4342598 | 3112 | u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 3113 | } |
wolfSSL | 0:9d17e4342598 | 3114 | /* set carry if it is placed below digs */ |
wolfSSL | 0:9d17e4342598 | 3115 | if (ix + iy < digs) { |
wolfSSL | 0:9d17e4342598 | 3116 | *tmpt = u; |
wolfSSL | 0:9d17e4342598 | 3117 | } |
wolfSSL | 0:9d17e4342598 | 3118 | } |
wolfSSL | 0:9d17e4342598 | 3119 | |
wolfSSL | 0:9d17e4342598 | 3120 | mp_clamp (&t); |
wolfSSL | 0:9d17e4342598 | 3121 | mp_exch (&t, c); |
wolfSSL | 0:9d17e4342598 | 3122 | |
wolfSSL | 0:9d17e4342598 | 3123 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 3124 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3125 | } |
wolfSSL | 0:9d17e4342598 | 3126 | |
wolfSSL | 0:9d17e4342598 | 3127 | |
wolfSSL | 0:9d17e4342598 | 3128 | /* |
wolfSSL | 0:9d17e4342598 | 3129 | * shifts with subtractions when the result is greater than b. |
wolfSSL | 0:9d17e4342598 | 3130 | * |
wolfSSL | 0:9d17e4342598 | 3131 | * The method is slightly modified to shift B unconditionally upto just under |
wolfSSL | 0:9d17e4342598 | 3132 | * the leading bit of b. This saves alot of multiple precision shifting. |
wolfSSL | 0:9d17e4342598 | 3133 | */ |
wolfSSL | 0:9d17e4342598 | 3134 | int mp_montgomery_calc_normalization (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 3135 | { |
wolfSSL | 0:9d17e4342598 | 3136 | int x, bits, res; |
wolfSSL | 0:9d17e4342598 | 3137 | |
wolfSSL | 0:9d17e4342598 | 3138 | /* how many bits of last digit does b use */ |
wolfSSL | 0:9d17e4342598 | 3139 | bits = mp_count_bits (b) % DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 3140 | |
wolfSSL | 0:9d17e4342598 | 3141 | if (b->used > 1) { |
wolfSSL | 0:9d17e4342598 | 3142 | if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3143 | return res; |
wolfSSL | 0:9d17e4342598 | 3144 | } |
wolfSSL | 0:9d17e4342598 | 3145 | } else { |
wolfSSL | 0:9d17e4342598 | 3146 | mp_set(a, 1); |
wolfSSL | 0:9d17e4342598 | 3147 | bits = 1; |
wolfSSL | 0:9d17e4342598 | 3148 | } |
wolfSSL | 0:9d17e4342598 | 3149 | |
wolfSSL | 0:9d17e4342598 | 3150 | |
wolfSSL | 0:9d17e4342598 | 3151 | /* now compute C = A * B mod b */ |
wolfSSL | 0:9d17e4342598 | 3152 | for (x = bits - 1; x < (int)DIGIT_BIT; x++) { |
wolfSSL | 0:9d17e4342598 | 3153 | if ((res = mp_mul_2 (a, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3154 | return res; |
wolfSSL | 0:9d17e4342598 | 3155 | } |
wolfSSL | 0:9d17e4342598 | 3156 | if (mp_cmp_mag (a, b) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 3157 | if ((res = s_mp_sub (a, b, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3158 | return res; |
wolfSSL | 0:9d17e4342598 | 3159 | } |
wolfSSL | 0:9d17e4342598 | 3160 | } |
wolfSSL | 0:9d17e4342598 | 3161 | } |
wolfSSL | 0:9d17e4342598 | 3162 | |
wolfSSL | 0:9d17e4342598 | 3163 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3164 | } |
wolfSSL | 0:9d17e4342598 | 3165 | |
wolfSSL | 0:9d17e4342598 | 3166 | |
wolfSSL | 0:9d17e4342598 | 3167 | #ifdef MP_LOW_MEM |
wolfSSL | 0:9d17e4342598 | 3168 | #define TAB_SIZE 32 |
wolfSSL | 0:9d17e4342598 | 3169 | #else |
wolfSSL | 0:9d17e4342598 | 3170 | #define TAB_SIZE 256 |
wolfSSL | 0:9d17e4342598 | 3171 | #endif |
wolfSSL | 0:9d17e4342598 | 3172 | |
wolfSSL | 0:9d17e4342598 | 3173 | int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) |
wolfSSL | 0:9d17e4342598 | 3174 | { |
wolfSSL | 0:9d17e4342598 | 3175 | mp_int M[TAB_SIZE], res, mu; |
wolfSSL | 0:9d17e4342598 | 3176 | mp_digit buf; |
wolfSSL | 0:9d17e4342598 | 3177 | int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; |
wolfSSL | 0:9d17e4342598 | 3178 | int (*redux)(mp_int*,mp_int*,mp_int*); |
wolfSSL | 0:9d17e4342598 | 3179 | |
wolfSSL | 0:9d17e4342598 | 3180 | /* find window size */ |
wolfSSL | 0:9d17e4342598 | 3181 | x = mp_count_bits (X); |
wolfSSL | 0:9d17e4342598 | 3182 | if (x <= 7) { |
wolfSSL | 0:9d17e4342598 | 3183 | winsize = 2; |
wolfSSL | 0:9d17e4342598 | 3184 | } else if (x <= 36) { |
wolfSSL | 0:9d17e4342598 | 3185 | winsize = 3; |
wolfSSL | 0:9d17e4342598 | 3186 | } else if (x <= 140) { |
wolfSSL | 0:9d17e4342598 | 3187 | winsize = 4; |
wolfSSL | 0:9d17e4342598 | 3188 | } else if (x <= 450) { |
wolfSSL | 0:9d17e4342598 | 3189 | winsize = 5; |
wolfSSL | 0:9d17e4342598 | 3190 | } else if (x <= 1303) { |
wolfSSL | 0:9d17e4342598 | 3191 | winsize = 6; |
wolfSSL | 0:9d17e4342598 | 3192 | } else if (x <= 3529) { |
wolfSSL | 0:9d17e4342598 | 3193 | winsize = 7; |
wolfSSL | 0:9d17e4342598 | 3194 | } else { |
wolfSSL | 0:9d17e4342598 | 3195 | winsize = 8; |
wolfSSL | 0:9d17e4342598 | 3196 | } |
wolfSSL | 0:9d17e4342598 | 3197 | |
wolfSSL | 0:9d17e4342598 | 3198 | #ifdef MP_LOW_MEM |
wolfSSL | 0:9d17e4342598 | 3199 | if (winsize > 5) { |
wolfSSL | 0:9d17e4342598 | 3200 | winsize = 5; |
wolfSSL | 0:9d17e4342598 | 3201 | } |
wolfSSL | 0:9d17e4342598 | 3202 | #endif |
wolfSSL | 0:9d17e4342598 | 3203 | |
wolfSSL | 0:9d17e4342598 | 3204 | /* init M array */ |
wolfSSL | 0:9d17e4342598 | 3205 | /* init first cell */ |
wolfSSL | 0:9d17e4342598 | 3206 | if ((err = mp_init(&M[1])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3207 | return err; |
wolfSSL | 0:9d17e4342598 | 3208 | } |
wolfSSL | 0:9d17e4342598 | 3209 | |
wolfSSL | 0:9d17e4342598 | 3210 | /* now init the second half of the array */ |
wolfSSL | 0:9d17e4342598 | 3211 | for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
wolfSSL | 0:9d17e4342598 | 3212 | if ((err = mp_init(&M[x])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3213 | for (y = 1<<(winsize-1); y < x; y++) { |
wolfSSL | 0:9d17e4342598 | 3214 | mp_clear (&M[y]); |
wolfSSL | 0:9d17e4342598 | 3215 | } |
wolfSSL | 0:9d17e4342598 | 3216 | mp_clear(&M[1]); |
wolfSSL | 0:9d17e4342598 | 3217 | return err; |
wolfSSL | 0:9d17e4342598 | 3218 | } |
wolfSSL | 0:9d17e4342598 | 3219 | } |
wolfSSL | 0:9d17e4342598 | 3220 | |
wolfSSL | 0:9d17e4342598 | 3221 | /* create mu, used for Barrett reduction */ |
wolfSSL | 0:9d17e4342598 | 3222 | if ((err = mp_init (&mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3223 | goto LBL_M; |
wolfSSL | 0:9d17e4342598 | 3224 | } |
wolfSSL | 0:9d17e4342598 | 3225 | |
wolfSSL | 0:9d17e4342598 | 3226 | if (redmode == 0) { |
wolfSSL | 0:9d17e4342598 | 3227 | if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3228 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3229 | } |
wolfSSL | 0:9d17e4342598 | 3230 | redux = mp_reduce; |
wolfSSL | 0:9d17e4342598 | 3231 | } else { |
wolfSSL | 0:9d17e4342598 | 3232 | if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3233 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3234 | } |
wolfSSL | 0:9d17e4342598 | 3235 | redux = mp_reduce_2k_l; |
wolfSSL | 0:9d17e4342598 | 3236 | } |
wolfSSL | 0:9d17e4342598 | 3237 | |
wolfSSL | 0:9d17e4342598 | 3238 | /* create M table |
wolfSSL | 0:9d17e4342598 | 3239 | * |
wolfSSL | 0:9d17e4342598 | 3240 | * The M table contains powers of the base, |
wolfSSL | 0:9d17e4342598 | 3241 | * e.g. M[x] = G**x mod P |
wolfSSL | 0:9d17e4342598 | 3242 | * |
wolfSSL | 0:9d17e4342598 | 3243 | * The first half of the table is not |
wolfSSL | 0:9d17e4342598 | 3244 | * computed though accept for M[0] and M[1] |
wolfSSL | 0:9d17e4342598 | 3245 | */ |
wolfSSL | 0:9d17e4342598 | 3246 | if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3247 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3248 | } |
wolfSSL | 0:9d17e4342598 | 3249 | |
wolfSSL | 0:9d17e4342598 | 3250 | /* compute the value at M[1<<(winsize-1)] by squaring |
wolfSSL | 0:9d17e4342598 | 3251 | * M[1] (winsize-1) times |
wolfSSL | 0:9d17e4342598 | 3252 | */ |
wolfSSL | 0:9d17e4342598 | 3253 | if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3254 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3255 | } |
wolfSSL | 0:9d17e4342598 | 3256 | |
wolfSSL | 0:9d17e4342598 | 3257 | for (x = 0; x < (winsize - 1); x++) { |
wolfSSL | 0:9d17e4342598 | 3258 | /* square it */ |
wolfSSL | 0:9d17e4342598 | 3259 | if ((err = mp_sqr (&M[1 << (winsize - 1)], |
wolfSSL | 0:9d17e4342598 | 3260 | &M[1 << (winsize - 1)])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3261 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3262 | } |
wolfSSL | 0:9d17e4342598 | 3263 | |
wolfSSL | 0:9d17e4342598 | 3264 | /* reduce modulo P */ |
wolfSSL | 0:9d17e4342598 | 3265 | if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3266 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3267 | } |
wolfSSL | 0:9d17e4342598 | 3268 | } |
wolfSSL | 0:9d17e4342598 | 3269 | |
wolfSSL | 0:9d17e4342598 | 3270 | /* create upper table, that is M[x] = M[x-1] * M[1] (mod P) |
wolfSSL | 0:9d17e4342598 | 3271 | * for x = (2**(winsize - 1) + 1) to (2**winsize - 1) |
wolfSSL | 0:9d17e4342598 | 3272 | */ |
wolfSSL | 0:9d17e4342598 | 3273 | for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { |
wolfSSL | 0:9d17e4342598 | 3274 | if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3275 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3276 | } |
wolfSSL | 0:9d17e4342598 | 3277 | if ((err = redux (&M[x], P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3278 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3279 | } |
wolfSSL | 0:9d17e4342598 | 3280 | } |
wolfSSL | 0:9d17e4342598 | 3281 | |
wolfSSL | 0:9d17e4342598 | 3282 | /* setup result */ |
wolfSSL | 0:9d17e4342598 | 3283 | if ((err = mp_init (&res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3284 | goto LBL_MU; |
wolfSSL | 0:9d17e4342598 | 3285 | } |
wolfSSL | 0:9d17e4342598 | 3286 | mp_set (&res, 1); |
wolfSSL | 0:9d17e4342598 | 3287 | |
wolfSSL | 0:9d17e4342598 | 3288 | /* set initial mode and bit cnt */ |
wolfSSL | 0:9d17e4342598 | 3289 | mode = 0; |
wolfSSL | 0:9d17e4342598 | 3290 | bitcnt = 1; |
wolfSSL | 0:9d17e4342598 | 3291 | buf = 0; |
wolfSSL | 0:9d17e4342598 | 3292 | digidx = X->used - 1; |
wolfSSL | 0:9d17e4342598 | 3293 | bitcpy = 0; |
wolfSSL | 0:9d17e4342598 | 3294 | bitbuf = 0; |
wolfSSL | 0:9d17e4342598 | 3295 | |
wolfSSL | 0:9d17e4342598 | 3296 | for (;;) { |
wolfSSL | 0:9d17e4342598 | 3297 | /* grab next digit as required */ |
wolfSSL | 0:9d17e4342598 | 3298 | if (--bitcnt == 0) { |
wolfSSL | 0:9d17e4342598 | 3299 | /* if digidx == -1 we are out of digits */ |
wolfSSL | 0:9d17e4342598 | 3300 | if (digidx == -1) { |
wolfSSL | 0:9d17e4342598 | 3301 | break; |
wolfSSL | 0:9d17e4342598 | 3302 | } |
wolfSSL | 0:9d17e4342598 | 3303 | /* read next digit and reset the bitcnt */ |
wolfSSL | 0:9d17e4342598 | 3304 | buf = X->dp[digidx--]; |
wolfSSL | 0:9d17e4342598 | 3305 | bitcnt = (int) DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 3306 | } |
wolfSSL | 0:9d17e4342598 | 3307 | |
wolfSSL | 0:9d17e4342598 | 3308 | /* grab the next msb from the exponent */ |
wolfSSL | 0:9d17e4342598 | 3309 | y = (int)(buf >> (mp_digit)(DIGIT_BIT - 1)) & 1; |
wolfSSL | 0:9d17e4342598 | 3310 | buf <<= (mp_digit)1; |
wolfSSL | 0:9d17e4342598 | 3311 | |
wolfSSL | 0:9d17e4342598 | 3312 | /* if the bit is zero and mode == 0 then we ignore it |
wolfSSL | 0:9d17e4342598 | 3313 | * These represent the leading zero bits before the first 1 bit |
wolfSSL | 0:9d17e4342598 | 3314 | * in the exponent. Technically this opt is not required but it |
wolfSSL | 0:9d17e4342598 | 3315 | * does lower the # of trivial squaring/reductions used |
wolfSSL | 0:9d17e4342598 | 3316 | */ |
wolfSSL | 0:9d17e4342598 | 3317 | if (mode == 0 && y == 0) { |
wolfSSL | 0:9d17e4342598 | 3318 | continue; |
wolfSSL | 0:9d17e4342598 | 3319 | } |
wolfSSL | 0:9d17e4342598 | 3320 | |
wolfSSL | 0:9d17e4342598 | 3321 | /* if the bit is zero and mode == 1 then we square */ |
wolfSSL | 0:9d17e4342598 | 3322 | if (mode == 1 && y == 0) { |
wolfSSL | 0:9d17e4342598 | 3323 | if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3324 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3325 | } |
wolfSSL | 0:9d17e4342598 | 3326 | if ((err = redux (&res, P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3327 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3328 | } |
wolfSSL | 0:9d17e4342598 | 3329 | continue; |
wolfSSL | 0:9d17e4342598 | 3330 | } |
wolfSSL | 0:9d17e4342598 | 3331 | |
wolfSSL | 0:9d17e4342598 | 3332 | /* else we add it to the window */ |
wolfSSL | 0:9d17e4342598 | 3333 | bitbuf |= (y << (winsize - ++bitcpy)); |
wolfSSL | 0:9d17e4342598 | 3334 | mode = 2; |
wolfSSL | 0:9d17e4342598 | 3335 | |
wolfSSL | 0:9d17e4342598 | 3336 | if (bitcpy == winsize) { |
wolfSSL | 0:9d17e4342598 | 3337 | /* ok window is filled so square as required and multiply */ |
wolfSSL | 0:9d17e4342598 | 3338 | /* square first */ |
wolfSSL | 0:9d17e4342598 | 3339 | for (x = 0; x < winsize; x++) { |
wolfSSL | 0:9d17e4342598 | 3340 | if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3341 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3342 | } |
wolfSSL | 0:9d17e4342598 | 3343 | if ((err = redux (&res, P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3344 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3345 | } |
wolfSSL | 0:9d17e4342598 | 3346 | } |
wolfSSL | 0:9d17e4342598 | 3347 | |
wolfSSL | 0:9d17e4342598 | 3348 | /* then multiply */ |
wolfSSL | 0:9d17e4342598 | 3349 | if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3350 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3351 | } |
wolfSSL | 0:9d17e4342598 | 3352 | if ((err = redux (&res, P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3353 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3354 | } |
wolfSSL | 0:9d17e4342598 | 3355 | |
wolfSSL | 0:9d17e4342598 | 3356 | /* empty window and reset */ |
wolfSSL | 0:9d17e4342598 | 3357 | bitcpy = 0; |
wolfSSL | 0:9d17e4342598 | 3358 | bitbuf = 0; |
wolfSSL | 0:9d17e4342598 | 3359 | mode = 1; |
wolfSSL | 0:9d17e4342598 | 3360 | } |
wolfSSL | 0:9d17e4342598 | 3361 | } |
wolfSSL | 0:9d17e4342598 | 3362 | |
wolfSSL | 0:9d17e4342598 | 3363 | /* if bits remain then square/multiply */ |
wolfSSL | 0:9d17e4342598 | 3364 | if (mode == 2 && bitcpy > 0) { |
wolfSSL | 0:9d17e4342598 | 3365 | /* square then multiply if the bit is set */ |
wolfSSL | 0:9d17e4342598 | 3366 | for (x = 0; x < bitcpy; x++) { |
wolfSSL | 0:9d17e4342598 | 3367 | if ((err = mp_sqr (&res, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3368 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3369 | } |
wolfSSL | 0:9d17e4342598 | 3370 | if ((err = redux (&res, P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3371 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3372 | } |
wolfSSL | 0:9d17e4342598 | 3373 | |
wolfSSL | 0:9d17e4342598 | 3374 | bitbuf <<= 1; |
wolfSSL | 0:9d17e4342598 | 3375 | if ((bitbuf & (1 << winsize)) != 0) { |
wolfSSL | 0:9d17e4342598 | 3376 | /* then multiply */ |
wolfSSL | 0:9d17e4342598 | 3377 | if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3378 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3379 | } |
wolfSSL | 0:9d17e4342598 | 3380 | if ((err = redux (&res, P, &mu)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3381 | goto LBL_RES; |
wolfSSL | 0:9d17e4342598 | 3382 | } |
wolfSSL | 0:9d17e4342598 | 3383 | } |
wolfSSL | 0:9d17e4342598 | 3384 | } |
wolfSSL | 0:9d17e4342598 | 3385 | } |
wolfSSL | 0:9d17e4342598 | 3386 | |
wolfSSL | 0:9d17e4342598 | 3387 | mp_exch (&res, Y); |
wolfSSL | 0:9d17e4342598 | 3388 | err = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3389 | LBL_RES:mp_clear (&res); |
wolfSSL | 0:9d17e4342598 | 3390 | LBL_MU:mp_clear (&mu); |
wolfSSL | 0:9d17e4342598 | 3391 | LBL_M: |
wolfSSL | 0:9d17e4342598 | 3392 | mp_clear(&M[1]); |
wolfSSL | 0:9d17e4342598 | 3393 | for (x = 1<<(winsize-1); x < (1 << winsize); x++) { |
wolfSSL | 0:9d17e4342598 | 3394 | mp_clear (&M[x]); |
wolfSSL | 0:9d17e4342598 | 3395 | } |
wolfSSL | 0:9d17e4342598 | 3396 | return err; |
wolfSSL | 0:9d17e4342598 | 3397 | } |
wolfSSL | 0:9d17e4342598 | 3398 | |
wolfSSL | 0:9d17e4342598 | 3399 | |
wolfSSL | 0:9d17e4342598 | 3400 | /* pre-calculate the value required for Barrett reduction |
wolfSSL | 0:9d17e4342598 | 3401 | * For a given modulus "b" it calulates the value required in "a" |
wolfSSL | 0:9d17e4342598 | 3402 | */ |
wolfSSL | 0:9d17e4342598 | 3403 | int mp_reduce_setup (mp_int * a, mp_int * b) |
wolfSSL | 0:9d17e4342598 | 3404 | { |
wolfSSL | 0:9d17e4342598 | 3405 | int res; |
wolfSSL | 0:9d17e4342598 | 3406 | |
wolfSSL | 0:9d17e4342598 | 3407 | if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3408 | return res; |
wolfSSL | 0:9d17e4342598 | 3409 | } |
wolfSSL | 0:9d17e4342598 | 3410 | return mp_div (a, b, a, NULL); |
wolfSSL | 0:9d17e4342598 | 3411 | } |
wolfSSL | 0:9d17e4342598 | 3412 | |
wolfSSL | 0:9d17e4342598 | 3413 | |
wolfSSL | 0:9d17e4342598 | 3414 | /* reduces x mod m, assumes 0 < x < m**2, mu is |
wolfSSL | 0:9d17e4342598 | 3415 | * precomputed via mp_reduce_setup. |
wolfSSL | 0:9d17e4342598 | 3416 | * From HAC pp.604 Algorithm 14.42 |
wolfSSL | 0:9d17e4342598 | 3417 | */ |
wolfSSL | 0:9d17e4342598 | 3418 | int mp_reduce (mp_int * x, mp_int * m, mp_int * mu) |
wolfSSL | 0:9d17e4342598 | 3419 | { |
wolfSSL | 0:9d17e4342598 | 3420 | mp_int q; |
wolfSSL | 0:9d17e4342598 | 3421 | int res, um = m->used; |
wolfSSL | 0:9d17e4342598 | 3422 | |
wolfSSL | 0:9d17e4342598 | 3423 | /* q = x */ |
wolfSSL | 0:9d17e4342598 | 3424 | if ((res = mp_init_copy (&q, x)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3425 | return res; |
wolfSSL | 0:9d17e4342598 | 3426 | } |
wolfSSL | 0:9d17e4342598 | 3427 | |
wolfSSL | 0:9d17e4342598 | 3428 | /* q1 = x / b**(k-1) */ |
wolfSSL | 0:9d17e4342598 | 3429 | mp_rshd (&q, um - 1); |
wolfSSL | 0:9d17e4342598 | 3430 | |
wolfSSL | 0:9d17e4342598 | 3431 | /* according to HAC this optimization is ok */ |
wolfSSL | 0:9d17e4342598 | 3432 | if (((mp_word) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { |
wolfSSL | 0:9d17e4342598 | 3433 | if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3434 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3435 | } |
wolfSSL | 0:9d17e4342598 | 3436 | } else { |
wolfSSL | 0:9d17e4342598 | 3437 | #ifdef BN_S_MP_MUL_HIGH_DIGS_C |
wolfSSL | 0:9d17e4342598 | 3438 | if ((res = s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3439 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3440 | } |
wolfSSL | 0:9d17e4342598 | 3441 | #elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C) |
wolfSSL | 0:9d17e4342598 | 3442 | if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3443 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3444 | } |
wolfSSL | 0:9d17e4342598 | 3445 | #else |
wolfSSL | 0:9d17e4342598 | 3446 | { |
wolfSSL | 0:9d17e4342598 | 3447 | res = MP_VAL; |
wolfSSL | 0:9d17e4342598 | 3448 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3449 | } |
wolfSSL | 0:9d17e4342598 | 3450 | #endif |
wolfSSL | 0:9d17e4342598 | 3451 | } |
wolfSSL | 0:9d17e4342598 | 3452 | |
wolfSSL | 0:9d17e4342598 | 3453 | /* q3 = q2 / b**(k+1) */ |
wolfSSL | 0:9d17e4342598 | 3454 | mp_rshd (&q, um + 1); |
wolfSSL | 0:9d17e4342598 | 3455 | |
wolfSSL | 0:9d17e4342598 | 3456 | /* x = x mod b**(k+1), quick (no division) */ |
wolfSSL | 0:9d17e4342598 | 3457 | if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3458 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3459 | } |
wolfSSL | 0:9d17e4342598 | 3460 | |
wolfSSL | 0:9d17e4342598 | 3461 | /* q = q * m mod b**(k+1), quick (no division) */ |
wolfSSL | 0:9d17e4342598 | 3462 | if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3463 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3464 | } |
wolfSSL | 0:9d17e4342598 | 3465 | |
wolfSSL | 0:9d17e4342598 | 3466 | /* x = x - q */ |
wolfSSL | 0:9d17e4342598 | 3467 | if ((res = mp_sub (x, &q, x)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3468 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3469 | } |
wolfSSL | 0:9d17e4342598 | 3470 | |
wolfSSL | 0:9d17e4342598 | 3471 | /* If x < 0, add b**(k+1) to it */ |
wolfSSL | 0:9d17e4342598 | 3472 | if (mp_cmp_d (x, 0) == MP_LT) { |
wolfSSL | 0:9d17e4342598 | 3473 | mp_set (&q, 1); |
wolfSSL | 0:9d17e4342598 | 3474 | if ((res = mp_lshd (&q, um + 1)) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 3475 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3476 | if ((res = mp_add (x, &q, x)) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 3477 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3478 | } |
wolfSSL | 0:9d17e4342598 | 3479 | |
wolfSSL | 0:9d17e4342598 | 3480 | /* Back off if it's too big */ |
wolfSSL | 0:9d17e4342598 | 3481 | while (mp_cmp (x, m) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 3482 | if ((res = s_mp_sub (x, m, x)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3483 | goto CLEANUP; |
wolfSSL | 0:9d17e4342598 | 3484 | } |
wolfSSL | 0:9d17e4342598 | 3485 | } |
wolfSSL | 0:9d17e4342598 | 3486 | |
wolfSSL | 0:9d17e4342598 | 3487 | CLEANUP: |
wolfSSL | 0:9d17e4342598 | 3488 | mp_clear (&q); |
wolfSSL | 0:9d17e4342598 | 3489 | |
wolfSSL | 0:9d17e4342598 | 3490 | return res; |
wolfSSL | 0:9d17e4342598 | 3491 | } |
wolfSSL | 0:9d17e4342598 | 3492 | |
wolfSSL | 0:9d17e4342598 | 3493 | |
wolfSSL | 0:9d17e4342598 | 3494 | /* reduces a modulo n where n is of the form 2**p - d |
wolfSSL | 0:9d17e4342598 | 3495 | This differs from reduce_2k since "d" can be larger |
wolfSSL | 0:9d17e4342598 | 3496 | than a single digit. |
wolfSSL | 0:9d17e4342598 | 3497 | */ |
wolfSSL | 0:9d17e4342598 | 3498 | int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d) |
wolfSSL | 0:9d17e4342598 | 3499 | { |
wolfSSL | 0:9d17e4342598 | 3500 | mp_int q; |
wolfSSL | 0:9d17e4342598 | 3501 | int p, res; |
wolfSSL | 0:9d17e4342598 | 3502 | |
wolfSSL | 0:9d17e4342598 | 3503 | if ((res = mp_init(&q)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3504 | return res; |
wolfSSL | 0:9d17e4342598 | 3505 | } |
wolfSSL | 0:9d17e4342598 | 3506 | |
wolfSSL | 0:9d17e4342598 | 3507 | p = mp_count_bits(n); |
wolfSSL | 0:9d17e4342598 | 3508 | top: |
wolfSSL | 0:9d17e4342598 | 3509 | /* q = a/2**p, a = a mod 2**p */ |
wolfSSL | 0:9d17e4342598 | 3510 | if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3511 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 3512 | } |
wolfSSL | 0:9d17e4342598 | 3513 | |
wolfSSL | 0:9d17e4342598 | 3514 | /* q = q * d */ |
wolfSSL | 0:9d17e4342598 | 3515 | if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3516 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 3517 | } |
wolfSSL | 0:9d17e4342598 | 3518 | |
wolfSSL | 0:9d17e4342598 | 3519 | /* a = a + q */ |
wolfSSL | 0:9d17e4342598 | 3520 | if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3521 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 3522 | } |
wolfSSL | 0:9d17e4342598 | 3523 | |
wolfSSL | 0:9d17e4342598 | 3524 | if (mp_cmp_mag(a, n) != MP_LT) { |
wolfSSL | 0:9d17e4342598 | 3525 | s_mp_sub(a, n, a); |
wolfSSL | 0:9d17e4342598 | 3526 | goto top; |
wolfSSL | 0:9d17e4342598 | 3527 | } |
wolfSSL | 0:9d17e4342598 | 3528 | |
wolfSSL | 0:9d17e4342598 | 3529 | ERR: |
wolfSSL | 0:9d17e4342598 | 3530 | mp_clear(&q); |
wolfSSL | 0:9d17e4342598 | 3531 | return res; |
wolfSSL | 0:9d17e4342598 | 3532 | } |
wolfSSL | 0:9d17e4342598 | 3533 | |
wolfSSL | 0:9d17e4342598 | 3534 | |
wolfSSL | 0:9d17e4342598 | 3535 | /* determines the setup value */ |
wolfSSL | 0:9d17e4342598 | 3536 | int mp_reduce_2k_setup_l(mp_int *a, mp_int *d) |
wolfSSL | 0:9d17e4342598 | 3537 | { |
wolfSSL | 0:9d17e4342598 | 3538 | int res; |
wolfSSL | 0:9d17e4342598 | 3539 | mp_int tmp; |
wolfSSL | 0:9d17e4342598 | 3540 | |
wolfSSL | 0:9d17e4342598 | 3541 | if ((res = mp_init(&tmp)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3542 | return res; |
wolfSSL | 0:9d17e4342598 | 3543 | } |
wolfSSL | 0:9d17e4342598 | 3544 | |
wolfSSL | 0:9d17e4342598 | 3545 | if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3546 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 3547 | } |
wolfSSL | 0:9d17e4342598 | 3548 | |
wolfSSL | 0:9d17e4342598 | 3549 | if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3550 | goto ERR; |
wolfSSL | 0:9d17e4342598 | 3551 | } |
wolfSSL | 0:9d17e4342598 | 3552 | |
wolfSSL | 0:9d17e4342598 | 3553 | ERR: |
wolfSSL | 0:9d17e4342598 | 3554 | mp_clear(&tmp); |
wolfSSL | 0:9d17e4342598 | 3555 | return res; |
wolfSSL | 0:9d17e4342598 | 3556 | } |
wolfSSL | 0:9d17e4342598 | 3557 | |
wolfSSL | 0:9d17e4342598 | 3558 | |
wolfSSL | 0:9d17e4342598 | 3559 | /* multiplies |a| * |b| and does not compute the lower digs digits |
wolfSSL | 0:9d17e4342598 | 3560 | * [meant to get the higher part of the product] |
wolfSSL | 0:9d17e4342598 | 3561 | */ |
wolfSSL | 0:9d17e4342598 | 3562 | int |
wolfSSL | 0:9d17e4342598 | 3563 | s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
wolfSSL | 0:9d17e4342598 | 3564 | { |
wolfSSL | 0:9d17e4342598 | 3565 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 3566 | int res, pa, pb, ix, iy; |
wolfSSL | 0:9d17e4342598 | 3567 | mp_digit u; |
wolfSSL | 0:9d17e4342598 | 3568 | mp_word r; |
wolfSSL | 0:9d17e4342598 | 3569 | mp_digit tmpx, *tmpt, *tmpy; |
wolfSSL | 0:9d17e4342598 | 3570 | |
wolfSSL | 0:9d17e4342598 | 3571 | /* can we use the fast multiplier? */ |
wolfSSL | 0:9d17e4342598 | 3572 | #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C |
wolfSSL | 0:9d17e4342598 | 3573 | if (((a->used + b->used + 1) < MP_WARRAY) |
wolfSSL | 0:9d17e4342598 | 3574 | && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { |
wolfSSL | 0:9d17e4342598 | 3575 | return fast_s_mp_mul_high_digs (a, b, c, digs); |
wolfSSL | 0:9d17e4342598 | 3576 | } |
wolfSSL | 0:9d17e4342598 | 3577 | #endif |
wolfSSL | 0:9d17e4342598 | 3578 | |
wolfSSL | 0:9d17e4342598 | 3579 | if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3580 | return res; |
wolfSSL | 0:9d17e4342598 | 3581 | } |
wolfSSL | 0:9d17e4342598 | 3582 | t.used = a->used + b->used + 1; |
wolfSSL | 0:9d17e4342598 | 3583 | |
wolfSSL | 0:9d17e4342598 | 3584 | pa = a->used; |
wolfSSL | 0:9d17e4342598 | 3585 | pb = b->used; |
wolfSSL | 0:9d17e4342598 | 3586 | for (ix = 0; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 3587 | /* clear the carry */ |
wolfSSL | 0:9d17e4342598 | 3588 | u = 0; |
wolfSSL | 0:9d17e4342598 | 3589 | |
wolfSSL | 0:9d17e4342598 | 3590 | /* left hand side of A[ix] * B[iy] */ |
wolfSSL | 0:9d17e4342598 | 3591 | tmpx = a->dp[ix]; |
wolfSSL | 0:9d17e4342598 | 3592 | |
wolfSSL | 0:9d17e4342598 | 3593 | /* alias to the address of where the digits will be stored */ |
wolfSSL | 0:9d17e4342598 | 3594 | tmpt = &(t.dp[digs]); |
wolfSSL | 0:9d17e4342598 | 3595 | |
wolfSSL | 0:9d17e4342598 | 3596 | /* alias for where to read the right hand side from */ |
wolfSSL | 0:9d17e4342598 | 3597 | tmpy = b->dp + (digs - ix); |
wolfSSL | 0:9d17e4342598 | 3598 | |
wolfSSL | 0:9d17e4342598 | 3599 | for (iy = digs - ix; iy < pb; iy++) { |
wolfSSL | 0:9d17e4342598 | 3600 | /* calculate the double precision result */ |
wolfSSL | 0:9d17e4342598 | 3601 | r = ((mp_word)*tmpt) + |
wolfSSL | 0:9d17e4342598 | 3602 | ((mp_word)tmpx) * ((mp_word)*tmpy++) + |
wolfSSL | 0:9d17e4342598 | 3603 | ((mp_word) u); |
wolfSSL | 0:9d17e4342598 | 3604 | |
wolfSSL | 0:9d17e4342598 | 3605 | /* get the lower part */ |
wolfSSL | 0:9d17e4342598 | 3606 | *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
wolfSSL | 0:9d17e4342598 | 3607 | |
wolfSSL | 0:9d17e4342598 | 3608 | /* carry the carry */ |
wolfSSL | 0:9d17e4342598 | 3609 | u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); |
wolfSSL | 0:9d17e4342598 | 3610 | } |
wolfSSL | 0:9d17e4342598 | 3611 | *tmpt = u; |
wolfSSL | 0:9d17e4342598 | 3612 | } |
wolfSSL | 0:9d17e4342598 | 3613 | mp_clamp (&t); |
wolfSSL | 0:9d17e4342598 | 3614 | mp_exch (&t, c); |
wolfSSL | 0:9d17e4342598 | 3615 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 3616 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3617 | } |
wolfSSL | 0:9d17e4342598 | 3618 | |
wolfSSL | 0:9d17e4342598 | 3619 | |
wolfSSL | 0:9d17e4342598 | 3620 | /* this is a modified version of fast_s_mul_digs that only produces |
wolfSSL | 0:9d17e4342598 | 3621 | * output digits *above* digs. See the comments for fast_s_mul_digs |
wolfSSL | 0:9d17e4342598 | 3622 | * to see how it works. |
wolfSSL | 0:9d17e4342598 | 3623 | * |
wolfSSL | 0:9d17e4342598 | 3624 | * This is used in the Barrett reduction since for one of the multiplications |
wolfSSL | 0:9d17e4342598 | 3625 | * only the higher digits were needed. This essentially halves the work. |
wolfSSL | 0:9d17e4342598 | 3626 | * |
wolfSSL | 0:9d17e4342598 | 3627 | * Based on Algorithm 14.12 on pp.595 of HAC. |
wolfSSL | 0:9d17e4342598 | 3628 | */ |
wolfSSL | 0:9d17e4342598 | 3629 | int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) |
wolfSSL | 0:9d17e4342598 | 3630 | { |
wolfSSL | 0:9d17e4342598 | 3631 | int olduse, res, pa, ix, iz; |
wolfSSL | 0:9d17e4342598 | 3632 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 3633 | mp_digit* W; /* uses dynamic memory and slower */ |
wolfSSL | 0:9d17e4342598 | 3634 | #else |
wolfSSL | 0:9d17e4342598 | 3635 | mp_digit W[MP_WARRAY]; |
wolfSSL | 0:9d17e4342598 | 3636 | #endif |
wolfSSL | 0:9d17e4342598 | 3637 | mp_word _W; |
wolfSSL | 0:9d17e4342598 | 3638 | |
wolfSSL | 0:9d17e4342598 | 3639 | /* grow the destination as required */ |
wolfSSL | 0:9d17e4342598 | 3640 | pa = a->used + b->used; |
wolfSSL | 0:9d17e4342598 | 3641 | if (c->alloc < pa) { |
wolfSSL | 0:9d17e4342598 | 3642 | if ((res = mp_grow (c, pa)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3643 | return res; |
wolfSSL | 0:9d17e4342598 | 3644 | } |
wolfSSL | 0:9d17e4342598 | 3645 | } |
wolfSSL | 0:9d17e4342598 | 3646 | |
wolfSSL | 0:9d17e4342598 | 3647 | if (pa > MP_WARRAY) |
wolfSSL | 0:9d17e4342598 | 3648 | return MP_RANGE; /* TAO range check */ |
wolfSSL | 0:9d17e4342598 | 3649 | |
wolfSSL | 0:9d17e4342598 | 3650 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 3651 | W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 3652 | if (W == NULL) |
wolfSSL | 0:9d17e4342598 | 3653 | return MP_MEM; |
wolfSSL | 0:9d17e4342598 | 3654 | #endif |
wolfSSL | 0:9d17e4342598 | 3655 | |
wolfSSL | 0:9d17e4342598 | 3656 | /* number of output digits to produce */ |
wolfSSL | 0:9d17e4342598 | 3657 | pa = a->used + b->used; |
wolfSSL | 0:9d17e4342598 | 3658 | _W = 0; |
wolfSSL | 0:9d17e4342598 | 3659 | for (ix = digs; ix < pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 3660 | int tx, ty, iy; |
wolfSSL | 0:9d17e4342598 | 3661 | mp_digit *tmpx, *tmpy; |
wolfSSL | 0:9d17e4342598 | 3662 | |
wolfSSL | 0:9d17e4342598 | 3663 | /* get offsets into the two bignums */ |
wolfSSL | 0:9d17e4342598 | 3664 | ty = MIN(b->used-1, ix); |
wolfSSL | 0:9d17e4342598 | 3665 | tx = ix - ty; |
wolfSSL | 0:9d17e4342598 | 3666 | |
wolfSSL | 0:9d17e4342598 | 3667 | /* setup temp aliases */ |
wolfSSL | 0:9d17e4342598 | 3668 | tmpx = a->dp + tx; |
wolfSSL | 0:9d17e4342598 | 3669 | tmpy = b->dp + ty; |
wolfSSL | 0:9d17e4342598 | 3670 | |
wolfSSL | 0:9d17e4342598 | 3671 | /* this is the number of times the loop will iterrate, essentially its |
wolfSSL | 0:9d17e4342598 | 3672 | while (tx++ < a->used && ty-- >= 0) { ... } |
wolfSSL | 0:9d17e4342598 | 3673 | */ |
wolfSSL | 0:9d17e4342598 | 3674 | iy = MIN(a->used-tx, ty+1); |
wolfSSL | 0:9d17e4342598 | 3675 | |
wolfSSL | 0:9d17e4342598 | 3676 | /* execute loop */ |
wolfSSL | 0:9d17e4342598 | 3677 | for (iz = 0; iz < iy; iz++) { |
wolfSSL | 0:9d17e4342598 | 3678 | _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |
wolfSSL | 0:9d17e4342598 | 3679 | } |
wolfSSL | 0:9d17e4342598 | 3680 | |
wolfSSL | 0:9d17e4342598 | 3681 | /* store term */ |
wolfSSL | 0:9d17e4342598 | 3682 | W[ix] = ((mp_digit)_W) & MP_MASK; |
wolfSSL | 0:9d17e4342598 | 3683 | |
wolfSSL | 0:9d17e4342598 | 3684 | /* make next carry */ |
wolfSSL | 0:9d17e4342598 | 3685 | _W = _W >> ((mp_word)DIGIT_BIT); |
wolfSSL | 0:9d17e4342598 | 3686 | } |
wolfSSL | 0:9d17e4342598 | 3687 | |
wolfSSL | 0:9d17e4342598 | 3688 | /* setup dest */ |
wolfSSL | 0:9d17e4342598 | 3689 | olduse = c->used; |
wolfSSL | 0:9d17e4342598 | 3690 | c->used = pa; |
wolfSSL | 0:9d17e4342598 | 3691 | |
wolfSSL | 0:9d17e4342598 | 3692 | { |
wolfSSL | 0:9d17e4342598 | 3693 | register mp_digit *tmpc; |
wolfSSL | 0:9d17e4342598 | 3694 | |
wolfSSL | 0:9d17e4342598 | 3695 | tmpc = c->dp + digs; |
wolfSSL | 0:9d17e4342598 | 3696 | for (ix = digs; ix <= pa; ix++) { |
wolfSSL | 0:9d17e4342598 | 3697 | /* now extract the previous digit [below the carry] */ |
wolfSSL | 0:9d17e4342598 | 3698 | *tmpc++ = W[ix]; |
wolfSSL | 0:9d17e4342598 | 3699 | } |
wolfSSL | 0:9d17e4342598 | 3700 | |
wolfSSL | 0:9d17e4342598 | 3701 | /* clear unused digits [that existed in the old copy of c] */ |
wolfSSL | 0:9d17e4342598 | 3702 | for (; ix < olduse; ix++) { |
wolfSSL | 0:9d17e4342598 | 3703 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 3704 | } |
wolfSSL | 0:9d17e4342598 | 3705 | } |
wolfSSL | 0:9d17e4342598 | 3706 | mp_clamp (c); |
wolfSSL | 0:9d17e4342598 | 3707 | |
wolfSSL | 0:9d17e4342598 | 3708 | #ifdef CYASSL_SMALL_STACK |
wolfSSL | 0:9d17e4342598 | 3709 | XFREE(W, 0, DYNAMIC_TYPE_BIGINT); |
wolfSSL | 0:9d17e4342598 | 3710 | #endif |
wolfSSL | 0:9d17e4342598 | 3711 | |
wolfSSL | 0:9d17e4342598 | 3712 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3713 | } |
wolfSSL | 0:9d17e4342598 | 3714 | |
wolfSSL | 0:9d17e4342598 | 3715 | |
wolfSSL | 0:9d17e4342598 | 3716 | /* set a 32-bit const */ |
wolfSSL | 0:9d17e4342598 | 3717 | int mp_set_int (mp_int * a, unsigned long b) |
wolfSSL | 0:9d17e4342598 | 3718 | { |
wolfSSL | 0:9d17e4342598 | 3719 | int x, res; |
wolfSSL | 0:9d17e4342598 | 3720 | |
wolfSSL | 0:9d17e4342598 | 3721 | mp_zero (a); |
wolfSSL | 0:9d17e4342598 | 3722 | |
wolfSSL | 0:9d17e4342598 | 3723 | /* set four bits at a time */ |
wolfSSL | 0:9d17e4342598 | 3724 | for (x = 0; x < 8; x++) { |
wolfSSL | 0:9d17e4342598 | 3725 | /* shift the number up four bits */ |
wolfSSL | 0:9d17e4342598 | 3726 | if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3727 | return res; |
wolfSSL | 0:9d17e4342598 | 3728 | } |
wolfSSL | 0:9d17e4342598 | 3729 | |
wolfSSL | 0:9d17e4342598 | 3730 | /* OR in the top four bits of the source */ |
wolfSSL | 0:9d17e4342598 | 3731 | a->dp[0] |= (b >> 28) & 15; |
wolfSSL | 0:9d17e4342598 | 3732 | |
wolfSSL | 0:9d17e4342598 | 3733 | /* shift the source up to the next four bits */ |
wolfSSL | 0:9d17e4342598 | 3734 | b <<= 4; |
wolfSSL | 0:9d17e4342598 | 3735 | |
wolfSSL | 0:9d17e4342598 | 3736 | /* ensure that digits are not clamped off */ |
wolfSSL | 0:9d17e4342598 | 3737 | a->used += 1; |
wolfSSL | 0:9d17e4342598 | 3738 | } |
wolfSSL | 0:9d17e4342598 | 3739 | mp_clamp (a); |
wolfSSL | 0:9d17e4342598 | 3740 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3741 | } |
wolfSSL | 0:9d17e4342598 | 3742 | |
wolfSSL | 0:9d17e4342598 | 3743 | |
wolfSSL | 0:9d17e4342598 | 3744 | #if defined(CYASSL_KEY_GEN) || defined(HAVE_ECC) |
wolfSSL | 0:9d17e4342598 | 3745 | |
wolfSSL | 0:9d17e4342598 | 3746 | /* c = a * a (mod b) */ |
wolfSSL | 0:9d17e4342598 | 3747 | int mp_sqrmod (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 3748 | { |
wolfSSL | 0:9d17e4342598 | 3749 | int res; |
wolfSSL | 0:9d17e4342598 | 3750 | mp_int t; |
wolfSSL | 0:9d17e4342598 | 3751 | |
wolfSSL | 0:9d17e4342598 | 3752 | if ((res = mp_init (&t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3753 | return res; |
wolfSSL | 0:9d17e4342598 | 3754 | } |
wolfSSL | 0:9d17e4342598 | 3755 | |
wolfSSL | 0:9d17e4342598 | 3756 | if ((res = mp_sqr (a, &t)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3757 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 3758 | return res; |
wolfSSL | 0:9d17e4342598 | 3759 | } |
wolfSSL | 0:9d17e4342598 | 3760 | res = mp_mod (&t, b, c); |
wolfSSL | 0:9d17e4342598 | 3761 | mp_clear (&t); |
wolfSSL | 0:9d17e4342598 | 3762 | return res; |
wolfSSL | 0:9d17e4342598 | 3763 | } |
wolfSSL | 0:9d17e4342598 | 3764 | |
wolfSSL | 0:9d17e4342598 | 3765 | #endif |
wolfSSL | 0:9d17e4342598 | 3766 | |
wolfSSL | 0:9d17e4342598 | 3767 | |
wolfSSL | 0:9d17e4342598 | 3768 | #if defined(HAVE_ECC) || !defined(NO_PWDBASED) || defined(CYASSL_SNIFFER) || defined(CYASSL_HAVE_WOLFSCEP) |
wolfSSL | 0:9d17e4342598 | 3769 | |
wolfSSL | 0:9d17e4342598 | 3770 | /* single digit addition */ |
wolfSSL | 0:9d17e4342598 | 3771 | int mp_add_d (mp_int* a, mp_digit b, mp_int* c) |
wolfSSL | 0:9d17e4342598 | 3772 | { |
wolfSSL | 0:9d17e4342598 | 3773 | int res, ix, oldused; |
wolfSSL | 0:9d17e4342598 | 3774 | mp_digit *tmpa, *tmpc, mu; |
wolfSSL | 0:9d17e4342598 | 3775 | |
wolfSSL | 0:9d17e4342598 | 3776 | /* grow c as required */ |
wolfSSL | 0:9d17e4342598 | 3777 | if (c->alloc < a->used + 1) { |
wolfSSL | 0:9d17e4342598 | 3778 | if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3779 | return res; |
wolfSSL | 0:9d17e4342598 | 3780 | } |
wolfSSL | 0:9d17e4342598 | 3781 | } |
wolfSSL | 0:9d17e4342598 | 3782 | |
wolfSSL | 0:9d17e4342598 | 3783 | /* if a is negative and |a| >= b, call c = |a| - b */ |
wolfSSL | 0:9d17e4342598 | 3784 | if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) { |
wolfSSL | 0:9d17e4342598 | 3785 | /* temporarily fix sign of a */ |
wolfSSL | 0:9d17e4342598 | 3786 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 3787 | |
wolfSSL | 0:9d17e4342598 | 3788 | /* c = |a| - b */ |
wolfSSL | 0:9d17e4342598 | 3789 | res = mp_sub_d(a, b, c); |
wolfSSL | 0:9d17e4342598 | 3790 | |
wolfSSL | 0:9d17e4342598 | 3791 | /* fix sign */ |
wolfSSL | 0:9d17e4342598 | 3792 | a->sign = c->sign = MP_NEG; |
wolfSSL | 0:9d17e4342598 | 3793 | |
wolfSSL | 0:9d17e4342598 | 3794 | /* clamp */ |
wolfSSL | 0:9d17e4342598 | 3795 | mp_clamp(c); |
wolfSSL | 0:9d17e4342598 | 3796 | |
wolfSSL | 0:9d17e4342598 | 3797 | return res; |
wolfSSL | 0:9d17e4342598 | 3798 | } |
wolfSSL | 0:9d17e4342598 | 3799 | |
wolfSSL | 0:9d17e4342598 | 3800 | /* old number of used digits in c */ |
wolfSSL | 0:9d17e4342598 | 3801 | oldused = c->used; |
wolfSSL | 0:9d17e4342598 | 3802 | |
wolfSSL | 0:9d17e4342598 | 3803 | /* sign always positive */ |
wolfSSL | 0:9d17e4342598 | 3804 | c->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 3805 | |
wolfSSL | 0:9d17e4342598 | 3806 | /* source alias */ |
wolfSSL | 0:9d17e4342598 | 3807 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 3808 | |
wolfSSL | 0:9d17e4342598 | 3809 | /* destination alias */ |
wolfSSL | 0:9d17e4342598 | 3810 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 3811 | |
wolfSSL | 0:9d17e4342598 | 3812 | /* if a is positive */ |
wolfSSL | 0:9d17e4342598 | 3813 | if (a->sign == MP_ZPOS) { |
wolfSSL | 0:9d17e4342598 | 3814 | /* add digit, after this we're propagating |
wolfSSL | 0:9d17e4342598 | 3815 | * the carry. |
wolfSSL | 0:9d17e4342598 | 3816 | */ |
wolfSSL | 0:9d17e4342598 | 3817 | *tmpc = *tmpa++ + b; |
wolfSSL | 0:9d17e4342598 | 3818 | mu = *tmpc >> DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 3819 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 3820 | |
wolfSSL | 0:9d17e4342598 | 3821 | /* now handle rest of the digits */ |
wolfSSL | 0:9d17e4342598 | 3822 | for (ix = 1; ix < a->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 3823 | *tmpc = *tmpa++ + mu; |
wolfSSL | 0:9d17e4342598 | 3824 | mu = *tmpc >> DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 3825 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 3826 | } |
wolfSSL | 0:9d17e4342598 | 3827 | /* set final carry */ |
wolfSSL | 0:9d17e4342598 | 3828 | if (mu != 0 && ix < c->alloc) { |
wolfSSL | 0:9d17e4342598 | 3829 | ix++; |
wolfSSL | 0:9d17e4342598 | 3830 | *tmpc++ = mu; |
wolfSSL | 0:9d17e4342598 | 3831 | } |
wolfSSL | 0:9d17e4342598 | 3832 | |
wolfSSL | 0:9d17e4342598 | 3833 | /* setup size */ |
wolfSSL | 0:9d17e4342598 | 3834 | c->used = a->used + 1; |
wolfSSL | 0:9d17e4342598 | 3835 | } else { |
wolfSSL | 0:9d17e4342598 | 3836 | /* a was negative and |a| < b */ |
wolfSSL | 0:9d17e4342598 | 3837 | c->used = 1; |
wolfSSL | 0:9d17e4342598 | 3838 | |
wolfSSL | 0:9d17e4342598 | 3839 | /* the result is a single digit */ |
wolfSSL | 0:9d17e4342598 | 3840 | if (a->used == 1) { |
wolfSSL | 0:9d17e4342598 | 3841 | *tmpc++ = b - a->dp[0]; |
wolfSSL | 0:9d17e4342598 | 3842 | } else { |
wolfSSL | 0:9d17e4342598 | 3843 | *tmpc++ = b; |
wolfSSL | 0:9d17e4342598 | 3844 | } |
wolfSSL | 0:9d17e4342598 | 3845 | |
wolfSSL | 0:9d17e4342598 | 3846 | /* setup count so the clearing of oldused |
wolfSSL | 0:9d17e4342598 | 3847 | * can fall through correctly |
wolfSSL | 0:9d17e4342598 | 3848 | */ |
wolfSSL | 0:9d17e4342598 | 3849 | ix = 1; |
wolfSSL | 0:9d17e4342598 | 3850 | } |
wolfSSL | 0:9d17e4342598 | 3851 | |
wolfSSL | 0:9d17e4342598 | 3852 | /* now zero to oldused */ |
wolfSSL | 0:9d17e4342598 | 3853 | while (ix++ < oldused) { |
wolfSSL | 0:9d17e4342598 | 3854 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 3855 | } |
wolfSSL | 0:9d17e4342598 | 3856 | mp_clamp(c); |
wolfSSL | 0:9d17e4342598 | 3857 | |
wolfSSL | 0:9d17e4342598 | 3858 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3859 | } |
wolfSSL | 0:9d17e4342598 | 3860 | |
wolfSSL | 0:9d17e4342598 | 3861 | |
wolfSSL | 0:9d17e4342598 | 3862 | /* single digit subtraction */ |
wolfSSL | 0:9d17e4342598 | 3863 | int mp_sub_d (mp_int * a, mp_digit b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 3864 | { |
wolfSSL | 0:9d17e4342598 | 3865 | mp_digit *tmpa, *tmpc, mu; |
wolfSSL | 0:9d17e4342598 | 3866 | int res, ix, oldused; |
wolfSSL | 0:9d17e4342598 | 3867 | |
wolfSSL | 0:9d17e4342598 | 3868 | /* grow c as required */ |
wolfSSL | 0:9d17e4342598 | 3869 | if (c->alloc < a->used + 1) { |
wolfSSL | 0:9d17e4342598 | 3870 | if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3871 | return res; |
wolfSSL | 0:9d17e4342598 | 3872 | } |
wolfSSL | 0:9d17e4342598 | 3873 | } |
wolfSSL | 0:9d17e4342598 | 3874 | |
wolfSSL | 0:9d17e4342598 | 3875 | /* if a is negative just do an unsigned |
wolfSSL | 0:9d17e4342598 | 3876 | * addition [with fudged signs] |
wolfSSL | 0:9d17e4342598 | 3877 | */ |
wolfSSL | 0:9d17e4342598 | 3878 | if (a->sign == MP_NEG) { |
wolfSSL | 0:9d17e4342598 | 3879 | a->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 3880 | res = mp_add_d(a, b, c); |
wolfSSL | 0:9d17e4342598 | 3881 | a->sign = c->sign = MP_NEG; |
wolfSSL | 0:9d17e4342598 | 3882 | |
wolfSSL | 0:9d17e4342598 | 3883 | /* clamp */ |
wolfSSL | 0:9d17e4342598 | 3884 | mp_clamp(c); |
wolfSSL | 0:9d17e4342598 | 3885 | |
wolfSSL | 0:9d17e4342598 | 3886 | return res; |
wolfSSL | 0:9d17e4342598 | 3887 | } |
wolfSSL | 0:9d17e4342598 | 3888 | |
wolfSSL | 0:9d17e4342598 | 3889 | /* setup regs */ |
wolfSSL | 0:9d17e4342598 | 3890 | oldused = c->used; |
wolfSSL | 0:9d17e4342598 | 3891 | tmpa = a->dp; |
wolfSSL | 0:9d17e4342598 | 3892 | tmpc = c->dp; |
wolfSSL | 0:9d17e4342598 | 3893 | |
wolfSSL | 0:9d17e4342598 | 3894 | /* if a <= b simply fix the single digit */ |
wolfSSL | 0:9d17e4342598 | 3895 | if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) { |
wolfSSL | 0:9d17e4342598 | 3896 | if (a->used == 1) { |
wolfSSL | 0:9d17e4342598 | 3897 | *tmpc++ = b - *tmpa; |
wolfSSL | 0:9d17e4342598 | 3898 | } else { |
wolfSSL | 0:9d17e4342598 | 3899 | *tmpc++ = b; |
wolfSSL | 0:9d17e4342598 | 3900 | } |
wolfSSL | 0:9d17e4342598 | 3901 | ix = 1; |
wolfSSL | 0:9d17e4342598 | 3902 | |
wolfSSL | 0:9d17e4342598 | 3903 | /* negative/1digit */ |
wolfSSL | 0:9d17e4342598 | 3904 | c->sign = MP_NEG; |
wolfSSL | 0:9d17e4342598 | 3905 | c->used = 1; |
wolfSSL | 0:9d17e4342598 | 3906 | } else { |
wolfSSL | 0:9d17e4342598 | 3907 | /* positive/size */ |
wolfSSL | 0:9d17e4342598 | 3908 | c->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 3909 | c->used = a->used; |
wolfSSL | 0:9d17e4342598 | 3910 | |
wolfSSL | 0:9d17e4342598 | 3911 | /* subtract first digit */ |
wolfSSL | 0:9d17e4342598 | 3912 | *tmpc = *tmpa++ - b; |
wolfSSL | 0:9d17e4342598 | 3913 | mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); |
wolfSSL | 0:9d17e4342598 | 3914 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 3915 | |
wolfSSL | 0:9d17e4342598 | 3916 | /* handle rest of the digits */ |
wolfSSL | 0:9d17e4342598 | 3917 | for (ix = 1; ix < a->used; ix++) { |
wolfSSL | 0:9d17e4342598 | 3918 | *tmpc = *tmpa++ - mu; |
wolfSSL | 0:9d17e4342598 | 3919 | mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); |
wolfSSL | 0:9d17e4342598 | 3920 | *tmpc++ &= MP_MASK; |
wolfSSL | 0:9d17e4342598 | 3921 | } |
wolfSSL | 0:9d17e4342598 | 3922 | } |
wolfSSL | 0:9d17e4342598 | 3923 | |
wolfSSL | 0:9d17e4342598 | 3924 | /* zero excess digits */ |
wolfSSL | 0:9d17e4342598 | 3925 | while (ix++ < oldused) { |
wolfSSL | 0:9d17e4342598 | 3926 | *tmpc++ = 0; |
wolfSSL | 0:9d17e4342598 | 3927 | } |
wolfSSL | 0:9d17e4342598 | 3928 | mp_clamp(c); |
wolfSSL | 0:9d17e4342598 | 3929 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3930 | } |
wolfSSL | 0:9d17e4342598 | 3931 | |
wolfSSL | 0:9d17e4342598 | 3932 | #endif /* defined(HAVE_ECC) || !defined(NO_PWDBASED) */ |
wolfSSL | 0:9d17e4342598 | 3933 | |
wolfSSL | 0:9d17e4342598 | 3934 | |
wolfSSL | 0:9d17e4342598 | 3935 | #ifdef CYASSL_KEY_GEN |
wolfSSL | 0:9d17e4342598 | 3936 | |
wolfSSL | 0:9d17e4342598 | 3937 | int mp_cnt_lsb(mp_int *a); |
wolfSSL | 0:9d17e4342598 | 3938 | |
wolfSSL | 0:9d17e4342598 | 3939 | static int s_is_power_of_two(mp_digit b, int *p) |
wolfSSL | 0:9d17e4342598 | 3940 | { |
wolfSSL | 0:9d17e4342598 | 3941 | int x; |
wolfSSL | 0:9d17e4342598 | 3942 | |
wolfSSL | 0:9d17e4342598 | 3943 | /* fast return if no power of two */ |
wolfSSL | 0:9d17e4342598 | 3944 | if ((b==0) || (b & (b-1))) { |
wolfSSL | 0:9d17e4342598 | 3945 | return 0; |
wolfSSL | 0:9d17e4342598 | 3946 | } |
wolfSSL | 0:9d17e4342598 | 3947 | |
wolfSSL | 0:9d17e4342598 | 3948 | for (x = 0; x < DIGIT_BIT; x++) { |
wolfSSL | 0:9d17e4342598 | 3949 | if (b == (((mp_digit)1)<<x)) { |
wolfSSL | 0:9d17e4342598 | 3950 | *p = x; |
wolfSSL | 0:9d17e4342598 | 3951 | return 1; |
wolfSSL | 0:9d17e4342598 | 3952 | } |
wolfSSL | 0:9d17e4342598 | 3953 | } |
wolfSSL | 0:9d17e4342598 | 3954 | return 0; |
wolfSSL | 0:9d17e4342598 | 3955 | } |
wolfSSL | 0:9d17e4342598 | 3956 | |
wolfSSL | 0:9d17e4342598 | 3957 | /* single digit division (based on routine from MPI) */ |
wolfSSL | 0:9d17e4342598 | 3958 | static int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) |
wolfSSL | 0:9d17e4342598 | 3959 | { |
wolfSSL | 0:9d17e4342598 | 3960 | mp_int q; |
wolfSSL | 0:9d17e4342598 | 3961 | mp_word w; |
wolfSSL | 0:9d17e4342598 | 3962 | mp_digit t; |
wolfSSL | 0:9d17e4342598 | 3963 | int res, ix; |
wolfSSL | 0:9d17e4342598 | 3964 | |
wolfSSL | 0:9d17e4342598 | 3965 | /* cannot divide by zero */ |
wolfSSL | 0:9d17e4342598 | 3966 | if (b == 0) { |
wolfSSL | 0:9d17e4342598 | 3967 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 3968 | } |
wolfSSL | 0:9d17e4342598 | 3969 | |
wolfSSL | 0:9d17e4342598 | 3970 | /* quick outs */ |
wolfSSL | 0:9d17e4342598 | 3971 | if (b == 1 || mp_iszero(a) == 1) { |
wolfSSL | 0:9d17e4342598 | 3972 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 3973 | *d = 0; |
wolfSSL | 0:9d17e4342598 | 3974 | } |
wolfSSL | 0:9d17e4342598 | 3975 | if (c != NULL) { |
wolfSSL | 0:9d17e4342598 | 3976 | return mp_copy(a, c); |
wolfSSL | 0:9d17e4342598 | 3977 | } |
wolfSSL | 0:9d17e4342598 | 3978 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3979 | } |
wolfSSL | 0:9d17e4342598 | 3980 | |
wolfSSL | 0:9d17e4342598 | 3981 | /* power of two ? */ |
wolfSSL | 0:9d17e4342598 | 3982 | if (s_is_power_of_two(b, &ix) == 1) { |
wolfSSL | 0:9d17e4342598 | 3983 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 3984 | *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1); |
wolfSSL | 0:9d17e4342598 | 3985 | } |
wolfSSL | 0:9d17e4342598 | 3986 | if (c != NULL) { |
wolfSSL | 0:9d17e4342598 | 3987 | return mp_div_2d(a, ix, c, NULL); |
wolfSSL | 0:9d17e4342598 | 3988 | } |
wolfSSL | 0:9d17e4342598 | 3989 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 3990 | } |
wolfSSL | 0:9d17e4342598 | 3991 | |
wolfSSL | 0:9d17e4342598 | 3992 | #ifdef BN_MP_DIV_3_C |
wolfSSL | 0:9d17e4342598 | 3993 | /* three? */ |
wolfSSL | 0:9d17e4342598 | 3994 | if (b == 3) { |
wolfSSL | 0:9d17e4342598 | 3995 | return mp_div_3(a, c, d); |
wolfSSL | 0:9d17e4342598 | 3996 | } |
wolfSSL | 0:9d17e4342598 | 3997 | #endif |
wolfSSL | 0:9d17e4342598 | 3998 | |
wolfSSL | 0:9d17e4342598 | 3999 | /* no easy answer [c'est la vie]. Just division */ |
wolfSSL | 0:9d17e4342598 | 4000 | if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4001 | return res; |
wolfSSL | 0:9d17e4342598 | 4002 | } |
wolfSSL | 0:9d17e4342598 | 4003 | |
wolfSSL | 0:9d17e4342598 | 4004 | q.used = a->used; |
wolfSSL | 0:9d17e4342598 | 4005 | q.sign = a->sign; |
wolfSSL | 0:9d17e4342598 | 4006 | w = 0; |
wolfSSL | 0:9d17e4342598 | 4007 | for (ix = a->used - 1; ix >= 0; ix--) { |
wolfSSL | 0:9d17e4342598 | 4008 | w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |
wolfSSL | 0:9d17e4342598 | 4009 | |
wolfSSL | 0:9d17e4342598 | 4010 | if (w >= b) { |
wolfSSL | 0:9d17e4342598 | 4011 | t = (mp_digit)(w / b); |
wolfSSL | 0:9d17e4342598 | 4012 | w -= ((mp_word)t) * ((mp_word)b); |
wolfSSL | 0:9d17e4342598 | 4013 | } else { |
wolfSSL | 0:9d17e4342598 | 4014 | t = 0; |
wolfSSL | 0:9d17e4342598 | 4015 | } |
wolfSSL | 0:9d17e4342598 | 4016 | q.dp[ix] = (mp_digit)t; |
wolfSSL | 0:9d17e4342598 | 4017 | } |
wolfSSL | 0:9d17e4342598 | 4018 | |
wolfSSL | 0:9d17e4342598 | 4019 | if (d != NULL) { |
wolfSSL | 0:9d17e4342598 | 4020 | *d = (mp_digit)w; |
wolfSSL | 0:9d17e4342598 | 4021 | } |
wolfSSL | 0:9d17e4342598 | 4022 | |
wolfSSL | 0:9d17e4342598 | 4023 | if (c != NULL) { |
wolfSSL | 0:9d17e4342598 | 4024 | mp_clamp(&q); |
wolfSSL | 0:9d17e4342598 | 4025 | mp_exch(&q, c); |
wolfSSL | 0:9d17e4342598 | 4026 | } |
wolfSSL | 0:9d17e4342598 | 4027 | mp_clear(&q); |
wolfSSL | 0:9d17e4342598 | 4028 | |
wolfSSL | 0:9d17e4342598 | 4029 | return res; |
wolfSSL | 0:9d17e4342598 | 4030 | } |
wolfSSL | 0:9d17e4342598 | 4031 | |
wolfSSL | 0:9d17e4342598 | 4032 | |
wolfSSL | 0:9d17e4342598 | 4033 | static int mp_mod_d (mp_int * a, mp_digit b, mp_digit * c) |
wolfSSL | 0:9d17e4342598 | 4034 | { |
wolfSSL | 0:9d17e4342598 | 4035 | return mp_div_d(a, b, NULL, c); |
wolfSSL | 0:9d17e4342598 | 4036 | } |
wolfSSL | 0:9d17e4342598 | 4037 | |
wolfSSL | 0:9d17e4342598 | 4038 | |
wolfSSL | 0:9d17e4342598 | 4039 | const mp_digit ltm_prime_tab[] = { |
wolfSSL | 0:9d17e4342598 | 4040 | 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013, |
wolfSSL | 0:9d17e4342598 | 4041 | 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035, |
wolfSSL | 0:9d17e4342598 | 4042 | 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059, |
wolfSSL | 0:9d17e4342598 | 4043 | 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, |
wolfSSL | 0:9d17e4342598 | 4044 | #ifndef MP_8BIT |
wolfSSL | 0:9d17e4342598 | 4045 | 0x0083, |
wolfSSL | 0:9d17e4342598 | 4046 | 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD, |
wolfSSL | 0:9d17e4342598 | 4047 | 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF, |
wolfSSL | 0:9d17e4342598 | 4048 | 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107, |
wolfSSL | 0:9d17e4342598 | 4049 | 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137, |
wolfSSL | 0:9d17e4342598 | 4050 | |
wolfSSL | 0:9d17e4342598 | 4051 | 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167, |
wolfSSL | 0:9d17e4342598 | 4052 | 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199, |
wolfSSL | 0:9d17e4342598 | 4053 | 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9, |
wolfSSL | 0:9d17e4342598 | 4054 | 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7, |
wolfSSL | 0:9d17e4342598 | 4055 | 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239, |
wolfSSL | 0:9d17e4342598 | 4056 | 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265, |
wolfSSL | 0:9d17e4342598 | 4057 | 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293, |
wolfSSL | 0:9d17e4342598 | 4058 | 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF, |
wolfSSL | 0:9d17e4342598 | 4059 | |
wolfSSL | 0:9d17e4342598 | 4060 | 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301, |
wolfSSL | 0:9d17e4342598 | 4061 | 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B, |
wolfSSL | 0:9d17e4342598 | 4062 | 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371, |
wolfSSL | 0:9d17e4342598 | 4063 | 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD, |
wolfSSL | 0:9d17e4342598 | 4064 | 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5, |
wolfSSL | 0:9d17e4342598 | 4065 | 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419, |
wolfSSL | 0:9d17e4342598 | 4066 | 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449, |
wolfSSL | 0:9d17e4342598 | 4067 | 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B, |
wolfSSL | 0:9d17e4342598 | 4068 | |
wolfSSL | 0:9d17e4342598 | 4069 | 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7, |
wolfSSL | 0:9d17e4342598 | 4070 | 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503, |
wolfSSL | 0:9d17e4342598 | 4071 | 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529, |
wolfSSL | 0:9d17e4342598 | 4072 | 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F, |
wolfSSL | 0:9d17e4342598 | 4073 | 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3, |
wolfSSL | 0:9d17e4342598 | 4074 | 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7, |
wolfSSL | 0:9d17e4342598 | 4075 | 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623, |
wolfSSL | 0:9d17e4342598 | 4076 | 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653 |
wolfSSL | 0:9d17e4342598 | 4077 | #endif |
wolfSSL | 0:9d17e4342598 | 4078 | }; |
wolfSSL | 0:9d17e4342598 | 4079 | |
wolfSSL | 0:9d17e4342598 | 4080 | |
wolfSSL | 0:9d17e4342598 | 4081 | /* Miller-Rabin test of "a" to the base of "b" as described in |
wolfSSL | 0:9d17e4342598 | 4082 | * HAC pp. 139 Algorithm 4.24 |
wolfSSL | 0:9d17e4342598 | 4083 | * |
wolfSSL | 0:9d17e4342598 | 4084 | * Sets result to 0 if definitely composite or 1 if probably prime. |
wolfSSL | 0:9d17e4342598 | 4085 | * Randomly the chance of error is no more than 1/4 and often |
wolfSSL | 0:9d17e4342598 | 4086 | * very much lower. |
wolfSSL | 0:9d17e4342598 | 4087 | */ |
wolfSSL | 0:9d17e4342598 | 4088 | static int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) |
wolfSSL | 0:9d17e4342598 | 4089 | { |
wolfSSL | 0:9d17e4342598 | 4090 | mp_int n1, y, r; |
wolfSSL | 0:9d17e4342598 | 4091 | int s, j, err; |
wolfSSL | 0:9d17e4342598 | 4092 | |
wolfSSL | 0:9d17e4342598 | 4093 | /* default */ |
wolfSSL | 0:9d17e4342598 | 4094 | *result = MP_NO; |
wolfSSL | 0:9d17e4342598 | 4095 | |
wolfSSL | 0:9d17e4342598 | 4096 | /* ensure b > 1 */ |
wolfSSL | 0:9d17e4342598 | 4097 | if (mp_cmp_d(b, 1) != MP_GT) { |
wolfSSL | 0:9d17e4342598 | 4098 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 4099 | } |
wolfSSL | 0:9d17e4342598 | 4100 | |
wolfSSL | 0:9d17e4342598 | 4101 | /* get n1 = a - 1 */ |
wolfSSL | 0:9d17e4342598 | 4102 | if ((err = mp_init_copy (&n1, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4103 | return err; |
wolfSSL | 0:9d17e4342598 | 4104 | } |
wolfSSL | 0:9d17e4342598 | 4105 | if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4106 | goto LBL_N1; |
wolfSSL | 0:9d17e4342598 | 4107 | } |
wolfSSL | 0:9d17e4342598 | 4108 | |
wolfSSL | 0:9d17e4342598 | 4109 | /* set 2**s * r = n1 */ |
wolfSSL | 0:9d17e4342598 | 4110 | if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4111 | goto LBL_N1; |
wolfSSL | 0:9d17e4342598 | 4112 | } |
wolfSSL | 0:9d17e4342598 | 4113 | |
wolfSSL | 0:9d17e4342598 | 4114 | /* count the number of least significant bits |
wolfSSL | 0:9d17e4342598 | 4115 | * which are zero |
wolfSSL | 0:9d17e4342598 | 4116 | */ |
wolfSSL | 0:9d17e4342598 | 4117 | s = mp_cnt_lsb(&r); |
wolfSSL | 0:9d17e4342598 | 4118 | |
wolfSSL | 0:9d17e4342598 | 4119 | /* now divide n - 1 by 2**s */ |
wolfSSL | 0:9d17e4342598 | 4120 | if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4121 | goto LBL_R; |
wolfSSL | 0:9d17e4342598 | 4122 | } |
wolfSSL | 0:9d17e4342598 | 4123 | |
wolfSSL | 0:9d17e4342598 | 4124 | /* compute y = b**r mod a */ |
wolfSSL | 0:9d17e4342598 | 4125 | if ((err = mp_init (&y)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4126 | goto LBL_R; |
wolfSSL | 0:9d17e4342598 | 4127 | } |
wolfSSL | 0:9d17e4342598 | 4128 | if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4129 | goto LBL_Y; |
wolfSSL | 0:9d17e4342598 | 4130 | } |
wolfSSL | 0:9d17e4342598 | 4131 | |
wolfSSL | 0:9d17e4342598 | 4132 | /* if y != 1 and y != n1 do */ |
wolfSSL | 0:9d17e4342598 | 4133 | if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 4134 | j = 1; |
wolfSSL | 0:9d17e4342598 | 4135 | /* while j <= s-1 and y != n1 */ |
wolfSSL | 0:9d17e4342598 | 4136 | while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 4137 | if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4138 | goto LBL_Y; |
wolfSSL | 0:9d17e4342598 | 4139 | } |
wolfSSL | 0:9d17e4342598 | 4140 | |
wolfSSL | 0:9d17e4342598 | 4141 | /* if y == 1 then composite */ |
wolfSSL | 0:9d17e4342598 | 4142 | if (mp_cmp_d (&y, 1) == MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 4143 | goto LBL_Y; |
wolfSSL | 0:9d17e4342598 | 4144 | } |
wolfSSL | 0:9d17e4342598 | 4145 | |
wolfSSL | 0:9d17e4342598 | 4146 | ++j; |
wolfSSL | 0:9d17e4342598 | 4147 | } |
wolfSSL | 0:9d17e4342598 | 4148 | |
wolfSSL | 0:9d17e4342598 | 4149 | /* if y != n1 then composite */ |
wolfSSL | 0:9d17e4342598 | 4150 | if (mp_cmp (&y, &n1) != MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 4151 | goto LBL_Y; |
wolfSSL | 0:9d17e4342598 | 4152 | } |
wolfSSL | 0:9d17e4342598 | 4153 | } |
wolfSSL | 0:9d17e4342598 | 4154 | |
wolfSSL | 0:9d17e4342598 | 4155 | /* probably prime now */ |
wolfSSL | 0:9d17e4342598 | 4156 | *result = MP_YES; |
wolfSSL | 0:9d17e4342598 | 4157 | LBL_Y:mp_clear (&y); |
wolfSSL | 0:9d17e4342598 | 4158 | LBL_R:mp_clear (&r); |
wolfSSL | 0:9d17e4342598 | 4159 | LBL_N1:mp_clear (&n1); |
wolfSSL | 0:9d17e4342598 | 4160 | return err; |
wolfSSL | 0:9d17e4342598 | 4161 | } |
wolfSSL | 0:9d17e4342598 | 4162 | |
wolfSSL | 0:9d17e4342598 | 4163 | |
wolfSSL | 0:9d17e4342598 | 4164 | /* determines if an integers is divisible by one |
wolfSSL | 0:9d17e4342598 | 4165 | * of the first PRIME_SIZE primes or not |
wolfSSL | 0:9d17e4342598 | 4166 | * |
wolfSSL | 0:9d17e4342598 | 4167 | * sets result to 0 if not, 1 if yes |
wolfSSL | 0:9d17e4342598 | 4168 | */ |
wolfSSL | 0:9d17e4342598 | 4169 | static int mp_prime_is_divisible (mp_int * a, int *result) |
wolfSSL | 0:9d17e4342598 | 4170 | { |
wolfSSL | 0:9d17e4342598 | 4171 | int err, ix; |
wolfSSL | 0:9d17e4342598 | 4172 | mp_digit res; |
wolfSSL | 0:9d17e4342598 | 4173 | |
wolfSSL | 0:9d17e4342598 | 4174 | /* default to not */ |
wolfSSL | 0:9d17e4342598 | 4175 | *result = MP_NO; |
wolfSSL | 0:9d17e4342598 | 4176 | |
wolfSSL | 0:9d17e4342598 | 4177 | for (ix = 0; ix < PRIME_SIZE; ix++) { |
wolfSSL | 0:9d17e4342598 | 4178 | /* what is a mod LBL_prime_tab[ix] */ |
wolfSSL | 0:9d17e4342598 | 4179 | if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4180 | return err; |
wolfSSL | 0:9d17e4342598 | 4181 | } |
wolfSSL | 0:9d17e4342598 | 4182 | |
wolfSSL | 0:9d17e4342598 | 4183 | /* is the residue zero? */ |
wolfSSL | 0:9d17e4342598 | 4184 | if (res == 0) { |
wolfSSL | 0:9d17e4342598 | 4185 | *result = MP_YES; |
wolfSSL | 0:9d17e4342598 | 4186 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 4187 | } |
wolfSSL | 0:9d17e4342598 | 4188 | } |
wolfSSL | 0:9d17e4342598 | 4189 | |
wolfSSL | 0:9d17e4342598 | 4190 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 4191 | } |
wolfSSL | 0:9d17e4342598 | 4192 | |
wolfSSL | 0:9d17e4342598 | 4193 | |
wolfSSL | 0:9d17e4342598 | 4194 | /* |
wolfSSL | 0:9d17e4342598 | 4195 | * Sets result to 1 if probably prime, 0 otherwise |
wolfSSL | 0:9d17e4342598 | 4196 | */ |
wolfSSL | 0:9d17e4342598 | 4197 | int mp_prime_is_prime (mp_int * a, int t, int *result) |
wolfSSL | 0:9d17e4342598 | 4198 | { |
wolfSSL | 0:9d17e4342598 | 4199 | mp_int b; |
wolfSSL | 0:9d17e4342598 | 4200 | int ix, err, res; |
wolfSSL | 0:9d17e4342598 | 4201 | |
wolfSSL | 0:9d17e4342598 | 4202 | /* default to no */ |
wolfSSL | 0:9d17e4342598 | 4203 | *result = MP_NO; |
wolfSSL | 0:9d17e4342598 | 4204 | |
wolfSSL | 0:9d17e4342598 | 4205 | /* valid value of t? */ |
wolfSSL | 0:9d17e4342598 | 4206 | if (t <= 0 || t > PRIME_SIZE) { |
wolfSSL | 0:9d17e4342598 | 4207 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 4208 | } |
wolfSSL | 0:9d17e4342598 | 4209 | |
wolfSSL | 0:9d17e4342598 | 4210 | /* is the input equal to one of the primes in the table? */ |
wolfSSL | 0:9d17e4342598 | 4211 | for (ix = 0; ix < PRIME_SIZE; ix++) { |
wolfSSL | 0:9d17e4342598 | 4212 | if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { |
wolfSSL | 0:9d17e4342598 | 4213 | *result = 1; |
wolfSSL | 0:9d17e4342598 | 4214 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 4215 | } |
wolfSSL | 0:9d17e4342598 | 4216 | } |
wolfSSL | 0:9d17e4342598 | 4217 | |
wolfSSL | 0:9d17e4342598 | 4218 | /* first perform trial division */ |
wolfSSL | 0:9d17e4342598 | 4219 | if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4220 | return err; |
wolfSSL | 0:9d17e4342598 | 4221 | } |
wolfSSL | 0:9d17e4342598 | 4222 | |
wolfSSL | 0:9d17e4342598 | 4223 | /* return if it was trivially divisible */ |
wolfSSL | 0:9d17e4342598 | 4224 | if (res == MP_YES) { |
wolfSSL | 0:9d17e4342598 | 4225 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 4226 | } |
wolfSSL | 0:9d17e4342598 | 4227 | |
wolfSSL | 0:9d17e4342598 | 4228 | /* now perform the miller-rabin rounds */ |
wolfSSL | 0:9d17e4342598 | 4229 | if ((err = mp_init (&b)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4230 | return err; |
wolfSSL | 0:9d17e4342598 | 4231 | } |
wolfSSL | 0:9d17e4342598 | 4232 | |
wolfSSL | 0:9d17e4342598 | 4233 | for (ix = 0; ix < t; ix++) { |
wolfSSL | 0:9d17e4342598 | 4234 | /* set the prime */ |
wolfSSL | 0:9d17e4342598 | 4235 | mp_set (&b, ltm_prime_tab[ix]); |
wolfSSL | 0:9d17e4342598 | 4236 | |
wolfSSL | 0:9d17e4342598 | 4237 | if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4238 | goto LBL_B; |
wolfSSL | 0:9d17e4342598 | 4239 | } |
wolfSSL | 0:9d17e4342598 | 4240 | |
wolfSSL | 0:9d17e4342598 | 4241 | if (res == MP_NO) { |
wolfSSL | 0:9d17e4342598 | 4242 | goto LBL_B; |
wolfSSL | 0:9d17e4342598 | 4243 | } |
wolfSSL | 0:9d17e4342598 | 4244 | } |
wolfSSL | 0:9d17e4342598 | 4245 | |
wolfSSL | 0:9d17e4342598 | 4246 | /* passed the test */ |
wolfSSL | 0:9d17e4342598 | 4247 | *result = MP_YES; |
wolfSSL | 0:9d17e4342598 | 4248 | LBL_B:mp_clear (&b); |
wolfSSL | 0:9d17e4342598 | 4249 | return err; |
wolfSSL | 0:9d17e4342598 | 4250 | } |
wolfSSL | 0:9d17e4342598 | 4251 | |
wolfSSL | 0:9d17e4342598 | 4252 | |
wolfSSL | 0:9d17e4342598 | 4253 | /* computes least common multiple as |a*b|/(a, b) */ |
wolfSSL | 0:9d17e4342598 | 4254 | int mp_lcm (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 4255 | { |
wolfSSL | 0:9d17e4342598 | 4256 | int res; |
wolfSSL | 0:9d17e4342598 | 4257 | mp_int t1, t2; |
wolfSSL | 0:9d17e4342598 | 4258 | |
wolfSSL | 0:9d17e4342598 | 4259 | |
wolfSSL | 0:9d17e4342598 | 4260 | if ((res = mp_init_multi (&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4261 | return res; |
wolfSSL | 0:9d17e4342598 | 4262 | } |
wolfSSL | 0:9d17e4342598 | 4263 | |
wolfSSL | 0:9d17e4342598 | 4264 | /* t1 = get the GCD of the two inputs */ |
wolfSSL | 0:9d17e4342598 | 4265 | if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4266 | goto LBL_T; |
wolfSSL | 0:9d17e4342598 | 4267 | } |
wolfSSL | 0:9d17e4342598 | 4268 | |
wolfSSL | 0:9d17e4342598 | 4269 | /* divide the smallest by the GCD */ |
wolfSSL | 0:9d17e4342598 | 4270 | if (mp_cmp_mag(a, b) == MP_LT) { |
wolfSSL | 0:9d17e4342598 | 4271 | /* store quotient in t2 such that t2 * b is the LCM */ |
wolfSSL | 0:9d17e4342598 | 4272 | if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4273 | goto LBL_T; |
wolfSSL | 0:9d17e4342598 | 4274 | } |
wolfSSL | 0:9d17e4342598 | 4275 | res = mp_mul(b, &t2, c); |
wolfSSL | 0:9d17e4342598 | 4276 | } else { |
wolfSSL | 0:9d17e4342598 | 4277 | /* store quotient in t2 such that t2 * a is the LCM */ |
wolfSSL | 0:9d17e4342598 | 4278 | if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4279 | goto LBL_T; |
wolfSSL | 0:9d17e4342598 | 4280 | } |
wolfSSL | 0:9d17e4342598 | 4281 | res = mp_mul(a, &t2, c); |
wolfSSL | 0:9d17e4342598 | 4282 | } |
wolfSSL | 0:9d17e4342598 | 4283 | |
wolfSSL | 0:9d17e4342598 | 4284 | /* fix the sign to positive */ |
wolfSSL | 0:9d17e4342598 | 4285 | c->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 4286 | |
wolfSSL | 0:9d17e4342598 | 4287 | LBL_T: |
wolfSSL | 0:9d17e4342598 | 4288 | mp_clear(&t1); |
wolfSSL | 0:9d17e4342598 | 4289 | mp_clear(&t2); |
wolfSSL | 0:9d17e4342598 | 4290 | return res; |
wolfSSL | 0:9d17e4342598 | 4291 | } |
wolfSSL | 0:9d17e4342598 | 4292 | |
wolfSSL | 0:9d17e4342598 | 4293 | |
wolfSSL | 0:9d17e4342598 | 4294 | static const int lnz[16] = { |
wolfSSL | 0:9d17e4342598 | 4295 | 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 |
wolfSSL | 0:9d17e4342598 | 4296 | }; |
wolfSSL | 0:9d17e4342598 | 4297 | |
wolfSSL | 0:9d17e4342598 | 4298 | /* Counts the number of lsbs which are zero before the first zero bit */ |
wolfSSL | 0:9d17e4342598 | 4299 | int mp_cnt_lsb(mp_int *a) |
wolfSSL | 0:9d17e4342598 | 4300 | { |
wolfSSL | 0:9d17e4342598 | 4301 | int x; |
wolfSSL | 0:9d17e4342598 | 4302 | mp_digit q, qq; |
wolfSSL | 0:9d17e4342598 | 4303 | |
wolfSSL | 0:9d17e4342598 | 4304 | /* easy out */ |
wolfSSL | 0:9d17e4342598 | 4305 | if (mp_iszero(a) == 1) { |
wolfSSL | 0:9d17e4342598 | 4306 | return 0; |
wolfSSL | 0:9d17e4342598 | 4307 | } |
wolfSSL | 0:9d17e4342598 | 4308 | |
wolfSSL | 0:9d17e4342598 | 4309 | /* scan lower digits until non-zero */ |
wolfSSL | 0:9d17e4342598 | 4310 | for (x = 0; x < a->used && a->dp[x] == 0; x++); |
wolfSSL | 0:9d17e4342598 | 4311 | q = a->dp[x]; |
wolfSSL | 0:9d17e4342598 | 4312 | x *= DIGIT_BIT; |
wolfSSL | 0:9d17e4342598 | 4313 | |
wolfSSL | 0:9d17e4342598 | 4314 | /* now scan this digit until a 1 is found */ |
wolfSSL | 0:9d17e4342598 | 4315 | if ((q & 1) == 0) { |
wolfSSL | 0:9d17e4342598 | 4316 | do { |
wolfSSL | 0:9d17e4342598 | 4317 | qq = q & 15; |
wolfSSL | 0:9d17e4342598 | 4318 | x += lnz[qq]; |
wolfSSL | 0:9d17e4342598 | 4319 | q >>= 4; |
wolfSSL | 0:9d17e4342598 | 4320 | } while (qq == 0); |
wolfSSL | 0:9d17e4342598 | 4321 | } |
wolfSSL | 0:9d17e4342598 | 4322 | return x; |
wolfSSL | 0:9d17e4342598 | 4323 | } |
wolfSSL | 0:9d17e4342598 | 4324 | |
wolfSSL | 0:9d17e4342598 | 4325 | |
wolfSSL | 0:9d17e4342598 | 4326 | /* Greatest Common Divisor using the binary method */ |
wolfSSL | 0:9d17e4342598 | 4327 | int mp_gcd (mp_int * a, mp_int * b, mp_int * c) |
wolfSSL | 0:9d17e4342598 | 4328 | { |
wolfSSL | 0:9d17e4342598 | 4329 | mp_int u, v; |
wolfSSL | 0:9d17e4342598 | 4330 | int k, u_lsb, v_lsb, res; |
wolfSSL | 0:9d17e4342598 | 4331 | |
wolfSSL | 0:9d17e4342598 | 4332 | /* either zero than gcd is the largest */ |
wolfSSL | 0:9d17e4342598 | 4333 | if (mp_iszero (a) == MP_YES) { |
wolfSSL | 0:9d17e4342598 | 4334 | return mp_abs (b, c); |
wolfSSL | 0:9d17e4342598 | 4335 | } |
wolfSSL | 0:9d17e4342598 | 4336 | if (mp_iszero (b) == MP_YES) { |
wolfSSL | 0:9d17e4342598 | 4337 | return mp_abs (a, c); |
wolfSSL | 0:9d17e4342598 | 4338 | } |
wolfSSL | 0:9d17e4342598 | 4339 | |
wolfSSL | 0:9d17e4342598 | 4340 | /* get copies of a and b we can modify */ |
wolfSSL | 0:9d17e4342598 | 4341 | if ((res = mp_init_copy (&u, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4342 | return res; |
wolfSSL | 0:9d17e4342598 | 4343 | } |
wolfSSL | 0:9d17e4342598 | 4344 | |
wolfSSL | 0:9d17e4342598 | 4345 | if ((res = mp_init_copy (&v, b)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4346 | goto LBL_U; |
wolfSSL | 0:9d17e4342598 | 4347 | } |
wolfSSL | 0:9d17e4342598 | 4348 | |
wolfSSL | 0:9d17e4342598 | 4349 | /* must be positive for the remainder of the algorithm */ |
wolfSSL | 0:9d17e4342598 | 4350 | u.sign = v.sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 4351 | |
wolfSSL | 0:9d17e4342598 | 4352 | /* B1. Find the common power of two for u and v */ |
wolfSSL | 0:9d17e4342598 | 4353 | u_lsb = mp_cnt_lsb(&u); |
wolfSSL | 0:9d17e4342598 | 4354 | v_lsb = mp_cnt_lsb(&v); |
wolfSSL | 0:9d17e4342598 | 4355 | k = MIN(u_lsb, v_lsb); |
wolfSSL | 0:9d17e4342598 | 4356 | |
wolfSSL | 0:9d17e4342598 | 4357 | if (k > 0) { |
wolfSSL | 0:9d17e4342598 | 4358 | /* divide the power of two out */ |
wolfSSL | 0:9d17e4342598 | 4359 | if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4360 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4361 | } |
wolfSSL | 0:9d17e4342598 | 4362 | |
wolfSSL | 0:9d17e4342598 | 4363 | if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4364 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4365 | } |
wolfSSL | 0:9d17e4342598 | 4366 | } |
wolfSSL | 0:9d17e4342598 | 4367 | |
wolfSSL | 0:9d17e4342598 | 4368 | /* divide any remaining factors of two out */ |
wolfSSL | 0:9d17e4342598 | 4369 | if (u_lsb != k) { |
wolfSSL | 0:9d17e4342598 | 4370 | if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4371 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4372 | } |
wolfSSL | 0:9d17e4342598 | 4373 | } |
wolfSSL | 0:9d17e4342598 | 4374 | |
wolfSSL | 0:9d17e4342598 | 4375 | if (v_lsb != k) { |
wolfSSL | 0:9d17e4342598 | 4376 | if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4377 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4378 | } |
wolfSSL | 0:9d17e4342598 | 4379 | } |
wolfSSL | 0:9d17e4342598 | 4380 | |
wolfSSL | 0:9d17e4342598 | 4381 | while (mp_iszero(&v) == 0) { |
wolfSSL | 0:9d17e4342598 | 4382 | /* make sure v is the largest */ |
wolfSSL | 0:9d17e4342598 | 4383 | if (mp_cmp_mag(&u, &v) == MP_GT) { |
wolfSSL | 0:9d17e4342598 | 4384 | /* swap u and v to make sure v is >= u */ |
wolfSSL | 0:9d17e4342598 | 4385 | mp_exch(&u, &v); |
wolfSSL | 0:9d17e4342598 | 4386 | } |
wolfSSL | 0:9d17e4342598 | 4387 | |
wolfSSL | 0:9d17e4342598 | 4388 | /* subtract smallest from largest */ |
wolfSSL | 0:9d17e4342598 | 4389 | if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4390 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4391 | } |
wolfSSL | 0:9d17e4342598 | 4392 | |
wolfSSL | 0:9d17e4342598 | 4393 | /* Divide out all factors of two */ |
wolfSSL | 0:9d17e4342598 | 4394 | if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4395 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4396 | } |
wolfSSL | 0:9d17e4342598 | 4397 | } |
wolfSSL | 0:9d17e4342598 | 4398 | |
wolfSSL | 0:9d17e4342598 | 4399 | /* multiply by 2**k which we divided out at the beginning */ |
wolfSSL | 0:9d17e4342598 | 4400 | if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4401 | goto LBL_V; |
wolfSSL | 0:9d17e4342598 | 4402 | } |
wolfSSL | 0:9d17e4342598 | 4403 | c->sign = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 4404 | res = MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 4405 | LBL_V:mp_clear (&u); |
wolfSSL | 0:9d17e4342598 | 4406 | LBL_U:mp_clear (&v); |
wolfSSL | 0:9d17e4342598 | 4407 | return res; |
wolfSSL | 0:9d17e4342598 | 4408 | } |
wolfSSL | 0:9d17e4342598 | 4409 | |
wolfSSL | 0:9d17e4342598 | 4410 | |
wolfSSL | 0:9d17e4342598 | 4411 | |
wolfSSL | 0:9d17e4342598 | 4412 | #endif /* CYASSL_KEY_GEN */ |
wolfSSL | 0:9d17e4342598 | 4413 | |
wolfSSL | 0:9d17e4342598 | 4414 | |
wolfSSL | 0:9d17e4342598 | 4415 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 4416 | |
wolfSSL | 0:9d17e4342598 | 4417 | /* chars used in radix conversions */ |
wolfSSL | 0:9d17e4342598 | 4418 | const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; |
wolfSSL | 0:9d17e4342598 | 4419 | |
wolfSSL | 0:9d17e4342598 | 4420 | /* read a string [ASCII] in a given radix */ |
wolfSSL | 0:9d17e4342598 | 4421 | int mp_read_radix (mp_int * a, const char *str, int radix) |
wolfSSL | 0:9d17e4342598 | 4422 | { |
wolfSSL | 0:9d17e4342598 | 4423 | int y, res, neg; |
wolfSSL | 0:9d17e4342598 | 4424 | char ch; |
wolfSSL | 0:9d17e4342598 | 4425 | |
wolfSSL | 0:9d17e4342598 | 4426 | /* zero the digit bignum */ |
wolfSSL | 0:9d17e4342598 | 4427 | mp_zero(a); |
wolfSSL | 0:9d17e4342598 | 4428 | |
wolfSSL | 0:9d17e4342598 | 4429 | /* make sure the radix is ok */ |
wolfSSL | 0:9d17e4342598 | 4430 | if (radix < 2 || radix > 64) { |
wolfSSL | 0:9d17e4342598 | 4431 | return MP_VAL; |
wolfSSL | 0:9d17e4342598 | 4432 | } |
wolfSSL | 0:9d17e4342598 | 4433 | |
wolfSSL | 0:9d17e4342598 | 4434 | /* if the leading digit is a |
wolfSSL | 0:9d17e4342598 | 4435 | * minus set the sign to negative. |
wolfSSL | 0:9d17e4342598 | 4436 | */ |
wolfSSL | 0:9d17e4342598 | 4437 | if (*str == '-') { |
wolfSSL | 0:9d17e4342598 | 4438 | ++str; |
wolfSSL | 0:9d17e4342598 | 4439 | neg = MP_NEG; |
wolfSSL | 0:9d17e4342598 | 4440 | } else { |
wolfSSL | 0:9d17e4342598 | 4441 | neg = MP_ZPOS; |
wolfSSL | 0:9d17e4342598 | 4442 | } |
wolfSSL | 0:9d17e4342598 | 4443 | |
wolfSSL | 0:9d17e4342598 | 4444 | /* set the integer to the default of zero */ |
wolfSSL | 0:9d17e4342598 | 4445 | mp_zero (a); |
wolfSSL | 0:9d17e4342598 | 4446 | |
wolfSSL | 0:9d17e4342598 | 4447 | /* process each digit of the string */ |
wolfSSL | 0:9d17e4342598 | 4448 | while (*str) { |
wolfSSL | 0:9d17e4342598 | 4449 | /* if the radix < 36 the conversion is case insensitive |
wolfSSL | 0:9d17e4342598 | 4450 | * this allows numbers like 1AB and 1ab to represent the same value |
wolfSSL | 0:9d17e4342598 | 4451 | * [e.g. in hex] |
wolfSSL | 0:9d17e4342598 | 4452 | */ |
wolfSSL | 0:9d17e4342598 | 4453 | ch = (char) ((radix < 36) ? XTOUPPER(*str) : *str); |
wolfSSL | 0:9d17e4342598 | 4454 | for (y = 0; y < 64; y++) { |
wolfSSL | 0:9d17e4342598 | 4455 | if (ch == mp_s_rmap[y]) { |
wolfSSL | 0:9d17e4342598 | 4456 | break; |
wolfSSL | 0:9d17e4342598 | 4457 | } |
wolfSSL | 0:9d17e4342598 | 4458 | } |
wolfSSL | 0:9d17e4342598 | 4459 | |
wolfSSL | 0:9d17e4342598 | 4460 | /* if the char was found in the map |
wolfSSL | 0:9d17e4342598 | 4461 | * and is less than the given radix add it |
wolfSSL | 0:9d17e4342598 | 4462 | * to the number, otherwise exit the loop. |
wolfSSL | 0:9d17e4342598 | 4463 | */ |
wolfSSL | 0:9d17e4342598 | 4464 | if (y < radix) { |
wolfSSL | 0:9d17e4342598 | 4465 | if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4466 | return res; |
wolfSSL | 0:9d17e4342598 | 4467 | } |
wolfSSL | 0:9d17e4342598 | 4468 | if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 4469 | return res; |
wolfSSL | 0:9d17e4342598 | 4470 | } |
wolfSSL | 0:9d17e4342598 | 4471 | } else { |
wolfSSL | 0:9d17e4342598 | 4472 | break; |
wolfSSL | 0:9d17e4342598 | 4473 | } |
wolfSSL | 0:9d17e4342598 | 4474 | ++str; |
wolfSSL | 0:9d17e4342598 | 4475 | } |
wolfSSL | 0:9d17e4342598 | 4476 | |
wolfSSL | 0:9d17e4342598 | 4477 | /* set the sign only if a != 0 */ |
wolfSSL | 0:9d17e4342598 | 4478 | if (mp_iszero(a) != 1) { |
wolfSSL | 0:9d17e4342598 | 4479 | a->sign = neg; |
wolfSSL | 0:9d17e4342598 | 4480 | } |
wolfSSL | 0:9d17e4342598 | 4481 | return MP_OKAY; |
wolfSSL | 0:9d17e4342598 | 4482 | } |
wolfSSL | 0:9d17e4342598 | 4483 | |
wolfSSL | 0:9d17e4342598 | 4484 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 4485 | |
wolfSSL | 0:9d17e4342598 | 4486 | #endif /* USE_FAST_MATH */ |
wolfSSL | 0:9d17e4342598 | 4487 | |
wolfSSL | 0:9d17e4342598 | 4488 | #endif /* NO_BIG_INT */ |