| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <assert.h> |
| 33 |
|
| 34 |
#include "common.h" |
| 35 |
#include "endianess.h" |
| 36 |
#include "multiply.h" |
| 37 |
#include "mont.h" |
| 38 |
|
| 39 |
#if SYS_BITS == 32 |
| 40 |
#include "multiply_32.c" |
| 41 |
#else |
| 42 |
#if SYS_BITS == 64 |
| 43 |
#include "multiply_64.c" |
| 44 |
#else |
| 45 |
#error You must define the macro SYS_BITS |
| 46 |
#endif |
| 47 |
#endif |
| 48 |
|
| 49 |
#if defined(USE_SSE2) |
| 50 |
#if defined(HAVE_INTRIN_H) |
| 51 |
#include <intrin.h> |
| 52 |
#elif defined(HAVE_X86INTRIN_H) |
| 53 |
#include <x86intrin.h> |
| 54 |
#elif defined(HAVE_EMMINTRIN_H) |
| 55 |
#include <xmmintrin.h> |
| 56 |
#include <emmintrin.h> |
| 57 |
#endif |
| 58 |
#endif |
| 59 |
|
| 60 |
#include "modexp_utils.c" |
| 61 |
#include "bignum.c" |
| 62 |
|
| 63 |
void mont_printf(const char *prefix, const uint64_t *mont_number, const MontContext *ctx) |
| 64 |
{ |
| 65 |
uint8_t* number; |
| 66 |
unsigned i; |
| 67 |
|
| 68 |
number = (uint8_t*)calloc(1, ctx->modulus_len); |
| 69 |
|
| 70 |
if (NULL == mont_number || NULL == ctx || NULL == number) |
| 71 |
return; |
| 72 |
|
| 73 |
mont_to_bytes(number, ctx->modulus_len, mont_number, ctx); |
| 74 |
printf("%s", prefix); |
| 75 |
for (i=0; i<ctx->modulus_len; i++) { |
| 76 |
printf("%02X", number[i]); |
| 77 |
} |
| 78 |
printf("\n"); |
| 79 |
free(number); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
ger. |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
STATIC uint64_t inverse64(uint64_t a) |
| 88 |
{ |
| 89 |
uint64_t x; |
| 90 |
|
| 91 |
assert(1 & a); |
| 92 |
x = ((a << 1 ^ a) & 4) << 1 ^ a; |
| 93 |
x += x - a*x*x; |
| 94 |
x += x - a*x*x; |
| 95 |
x += x - a*x*x; |
| 96 |
x += x - a*x*x; |
| 97 |
assert((x*a & 0xFFFFFFFFFFFFFFFFULL) == 1); |
| 98 |
|
| 99 |
return x; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
an N. |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
STATIC void rsquare(uint64_t *r2_mod_n, uint64_t *n, size_t nw) |
| 110 |
{ |
| 111 |
size_t i; |
| 112 |
size_t R_bits; |
| 113 |
|
| 114 |
memset(r2_mod_n, 0, sizeof(uint64_t)*nw); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
r2_mod_n[0] = 1; |
| 121 |
R_bits = nw * sizeof(uint64_t) * 8; |
| 122 |
for (i=0; i<R_bits*2; i++) { |
| 123 |
unsigned overflow; |
| 124 |
size_t j; |
| 125 |
|
| 126 |
|
| 127 |
overflow = (unsigned)(r2_mod_n[nw-1] >> 63); |
| 128 |
for (j=nw-1; j>0; j--) { |
| 129 |
r2_mod_n[j] = (r2_mod_n[j] << 1) + (r2_mod_n[j-1] >> 63); |
| 130 |
} |
| 131 |
|
| 132 |
r2_mod_n[0] <<= 1; |
| 133 |
|
| 134 |
|
| 135 |
while (overflow || ge(r2_mod_n, n, nw)) { |
| 136 |
sub(r2_mod_n, r2_mod_n, n, nw); |
| 137 |
overflow = 0; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
d R |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#if SCRATCHPAD_NR < 7 |
| 158 |
#error Scratchpad is too small |
| 159 |
#endif |
| 160 |
STATIC void mont_mult_generic(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw) |
| 161 |
{ |
| 162 |
size_t i; |
| 163 |
uint64_t *t, *scratchpad, *t2; |
| 164 |
unsigned cond; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
t = tmp; |
| 174 |
scratchpad = tmp + 3*nw; |
| 175 |
t2 = scratchpad + 3*nw; |
| 176 |
|
| 177 |
if (a == b) { |
| 178 |
square(t, scratchpad, a, nw); |
| 179 |
} else { |
| 180 |
product(t, scratchpad, a, b, nw); |
| 181 |
} |
| 182 |
|
| 183 |
t[2*nw] = 0; |
| 184 |
|
| 185 |
|
| 186 |
for (i=0; i<(nw ^ (nw & 1)); i+=2) { |
| 187 |
uint64_t k0, k1, ti1, prod_lo, prod_hi; |
| 188 |
|
| 189 |
|
| 190 |
k0 = t[i] * m0; |
| 191 |
|
| 192 |
|
| 193 |
DP_MULT(k0, n[0], prod_lo, prod_hi); |
| 194 |
prod_lo += t[i]; |
| 195 |
prod_hi += prod_lo < t[i]; |
| 196 |
|
| 197 |
|
| 198 |
ti1 = t[i+1] + n[1]*k0 + prod_hi; |
| 199 |
|
| 200 |
|
| 201 |
k1 = ti1 * m0; |
| 202 |
|
| 203 |
addmul128(&t[i], scratchpad, n, k0, k1, 2*nw+1-i, nw); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
if (is_odd(nw)) { |
| 208 |
addmul(&t[nw-1], nw+2, n, nw, t[nw-1]*m0); |
| 209 |
} |
| 210 |
|
| 211 |
assert(t[2*nw] <= 1); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
sub(t2, &t[nw], n, nw); |
| 217 |
cond = (unsigned)(t[2*nw] | (uint64_t)ge(&t[nw], n, nw)); |
| 218 |
mod_select(out, t2, &t[nw], cond, (unsigned)nw); |
| 219 |
} |
| 220 |
|
| 221 |
STATIC void mont_mult_p256(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw) |
| 222 |
{ |
| 223 |
unsigned i; |
| 224 |
uint64_t *t, *scratchpad, *t2; |
| 225 |
unsigned cond; |
| 226 |
#define WORDS_64 4U |
| 227 |
#define PREDIV_WORDS_64 (2*WORDS_64+1) |
| 228 |
#define WORDS_32 (WORDS_64*2) |
| 229 |
#define PREDIV_WORDS_32 (2*PREDIV_WORDS_64) |
| 230 |
|
| 231 |
#if SYS_BITS == 32 |
| 232 |
uint32_t t32[18]; |
| 233 |
#endif |
| 234 |
|
| 235 |
assert(nw == 4); |
| 236 |
assert(m0 == 1); |
| 237 |
|
| 238 |
t = tmp; |
| 239 |
scratchpad = tmp + 3*nw; |
| 240 |
t2 = scratchpad + 3*nw; |
| 241 |
|
| 242 |
if (a == b) { |
| 243 |
square(t, scratchpad, a, WORDS_64); |
| 244 |
} else { |
| 245 |
product(t, scratchpad, a, b, WORDS_64); |
| 246 |
} |
| 247 |
|
| 248 |
t[PREDIV_WORDS_64-1] = 0; |
| 249 |
|
| 250 |
#if SYS_BITS == 32 |
| 251 |
for (i=0; i<PREDIV_WORDS_64; i++) { |
| 252 |
t32[2*i] = (uint32_t)t[i]; |
| 253 |
t32[2*i+1] = (uint32_t)(t[i] >> 32); |
| 254 |
} |
| 255 |
|
| 256 |
for (i=0; i<WORDS_32; i++) { |
| 257 |
uint32_t k, carry; |
| 258 |
uint64_t prod, k2; |
| 259 |
unsigned j; |
| 260 |
|
| 261 |
k = t32[i]; |
| 262 |
k2 = ((uint64_t)k<<32) - k; |
| 263 |
|
| 264 |
*/ |
| 265 |
prod = k2 + t32[i+0]; |
| 266 |
t32[i+0] = (uint32_t)prod; |
| 267 |
carry = (uint32_t)(prod >> 32); |
| 268 |
*/ |
| 269 |
prod = k2 + t32[i+1] + carry; |
| 270 |
t32[i+1] = (uint32_t)prod; |
| 271 |
carry = (uint32_t)(prod >> 32); |
| 272 |
*/ |
| 273 |
prod = k2 + t32[i+2] + carry; |
| 274 |
t32[i+2] = (uint32_t)prod; |
| 275 |
carry = (uint32_t)(prod >> 32); |
| 276 |
|
| 277 |
t32[i+3] += carry; |
| 278 |
carry = t32[i+3] < carry; |
| 279 |
|
| 280 |
t32[i+4] += carry; |
| 281 |
carry = t32[i+4] < carry; |
| 282 |
|
| 283 |
t32[i+5] += carry; |
| 284 |
carry = t32[i+5] < carry; |
| 285 |
|
| 286 |
t32[i+6] += carry; |
| 287 |
carry = t32[i+6] < carry; |
| 288 |
t32[i+6] += k; |
| 289 |
carry |= t32[i+6] < k; |
| 290 |
*/ |
| 291 |
prod = k2 + t32[i+7] + carry; |
| 292 |
t32[i+7] = (uint32_t)prod; |
| 293 |
carry = (uint32_t)(prod >> 32); |
| 294 |
|
| 295 |
for (j=WORDS_32; carry; j++) { |
| 296 |
t32[i+j] += carry; |
| 297 |
carry = t32[i+j] < carry; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
for (i=0; i<PREDIV_WORDS_64; i++) { |
| 302 |
t[i] = ((uint64_t)t32[2*i+1]<<32) + t32[2*i]; |
| 303 |
} |
| 304 |
|
| 305 |
#elif SYS_BITS == 64 |
| 306 |
|
| 307 |
for (i=0; i<WORDS_64; i++) { |
| 308 |
unsigned j; |
| 309 |
uint64_t carry, k; |
| 310 |
uint64_t prod_lo, prod_hi; |
| 311 |
|
| 312 |
k = t[i]; |
| 313 |
|
| 314 |
1 */ |
| 315 |
prod_lo = (uint64_t)(0 - k); |
| 316 |
prod_hi = k - (k!=0); |
| 317 |
t[i+0] += prod_lo; |
| 318 |
prod_hi += t[i+0] < prod_lo; |
| 319 |
carry = prod_hi; |
| 320 |
|
| 321 |
*/ |
| 322 |
DP_MULT(n[1], k, prod_lo, prod_hi); |
| 323 |
prod_lo += carry; |
| 324 |
prod_hi += prod_lo < carry; |
| 325 |
t[i+1] += prod_lo; |
| 326 |
prod_hi += t[i+1] < prod_lo; |
| 327 |
carry = prod_hi; |
| 328 |
|
| 329 |
|
| 330 |
t[i+2] += carry; |
| 331 |
carry = t[i+2] < carry; |
| 332 |
|
| 333 |
+ 1 */ |
| 334 |
DP_MULT(n[3], k, prod_lo, prod_hi); |
| 335 |
prod_lo += carry; |
| 336 |
prod_hi += prod_lo < carry; |
| 337 |
t[i+3] += prod_lo; |
| 338 |
prod_hi += t[i+3] < prod_lo; |
| 339 |
carry = prod_hi; |
| 340 |
|
| 341 |
for (j=WORDS_64; carry; j++) { |
| 342 |
t[i+j] += carry; |
| 343 |
carry = t[i+j] < carry; |
| 344 |
} |
| 345 |
} |
| 346 |
#else |
| 347 |
#error You must define the SYS_BITS macro |
| 348 |
#endif |
| 349 |
|
| 350 |
assert(t[PREDIV_WORDS_64-1] <= 1); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
sub(t2, &t[nw], n, WORDS_64); |
| 356 |
cond = (unsigned)(t[PREDIV_WORDS_64-1] | (uint64_t)ge(&t[WORDS_64], n, WORDS_64)); |
| 357 |
mod_select(out, t2, &t[WORDS_64], cond, WORDS_64); |
| 358 |
|
| 359 |
#undef WORDS_64 |
| 360 |
#undef PREDIV_WORDS_64 |
| 361 |
#undef WORDS_32 |
| 362 |
#undef PREDIV_WORDS_32 |
| 363 |
} |
| 364 |
|
| 365 |
STATIC void mont_mult_p384(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw) |
| 366 |
{ |
| 367 |
size_t i; |
| 368 |
uint64_t *t, *scratchpad, *t2; |
| 369 |
unsigned cond; |
| 370 |
#define WORDS_64 6U |
| 371 |
#define PREDIV_WORDS_64 (2*WORDS_64+1) |
| 372 |
#define WORDS_32 (WORDS_64*2) |
| 373 |
#define PREDIV_WORDS_32 (2*PREDIV_WORDS_64) |
| 374 |
|
| 375 |
#if SYS_BITS == 32 |
| 376 |
uint32_t t32[PREDIV_WORDS_32]; |
| 377 |
#endif |
| 378 |
|
| 379 |
assert(nw == WORDS_64); |
| 380 |
assert(m0 == 0x0000000100000001ULL); |
| 381 |
|
| 382 |
t = tmp; |
| 383 |
scratchpad = tmp + 3*nw; |
| 384 |
t2 = scratchpad + 3*nw; |
| 385 |
|
| 386 |
if (a == b) { |
| 387 |
square(t, scratchpad, a, WORDS_64); |
| 388 |
} else { |
| 389 |
product(t, scratchpad, a, b, WORDS_64); |
| 390 |
} |
| 391 |
|
| 392 |
t[PREDIV_WORDS_64-1] = 0; |
| 393 |
|
| 394 |
#if SYS_BITS == 32 |
| 395 |
for (i=0; i<PREDIV_WORDS_64; i++) { |
| 396 |
t32[2*i] = (uint32_t)t[i]; |
| 397 |
t32[2*i+1] = (uint32_t)(t[i] >> 32); |
| 398 |
} |
| 399 |
|
| 400 |
for (i=0; i<WORDS_32; i++) { |
| 401 |
uint32_t k, carry; |
| 402 |
uint64_t prod, k2, k3; |
| 403 |
unsigned j; |
| 404 |
|
| 405 |
k = t32[i]; |
| 406 |
k2 = ((uint64_t)k<<32) - k; |
| 407 |
k3 = k2 - k; |
| 408 |
|
| 409 |
*/ |
| 410 |
prod = k2 + t32[i+0]; |
| 411 |
t32[i+0] = (uint32_t)prod; |
| 412 |
carry = (uint32_t)(prod >> 32); |
| 413 |
|
| 414 |
prod = (uint64_t)t32[i+1] + carry; |
| 415 |
t32[i+1] = (uint32_t)prod; |
| 416 |
carry = (uint32_t)(prod >> 32); |
| 417 |
|
| 418 |
prod = (uint64_t)t32[i+2] + carry; |
| 419 |
t32[i+2] = (uint32_t)prod; |
| 420 |
carry = (uint32_t)(prod >> 32); |
| 421 |
*/ |
| 422 |
prod = k2 + t32[i+3] + carry; |
| 423 |
t32[i+3] = (uint32_t)prod; |
| 424 |
carry = (uint32_t)(prod >> 32); |
| 425 |
*/ |
| 426 |
prod = k3 + t32[i+4] + carry; |
| 427 |
t32[i+4] = (uint32_t)prod; |
| 428 |
carry = (uint32_t)(prod >> 32); |
| 429 |
*/ |
| 430 |
prod = k2 + t32[i+5] + carry; |
| 431 |
t32[i+5] = (uint32_t)prod; |
| 432 |
carry = (uint32_t)(prod >> 32); |
| 433 |
*/ |
| 434 |
prod = k2 + t32[i+6] + carry; |
| 435 |
t32[i+6] = (uint32_t)prod; |
| 436 |
carry = (uint32_t)(prod >> 32); |
| 437 |
*/ |
| 438 |
prod = k2 + t32[i+7] + carry; |
| 439 |
t32[i+7] = (uint32_t)prod; |
| 440 |
carry = (uint32_t)(prod >> 32); |
| 441 |
*/ |
| 442 |
prod = k2 + t32[i+8] + carry; |
| 443 |
t32[i+8] = (uint32_t)prod; |
| 444 |
carry = (uint32_t)(prod >> 32); |
| 445 |
*/ |
| 446 |
prod = k2 + t32[i+9] + carry; |
| 447 |
t32[i+9] = (uint32_t)prod; |
| 448 |
carry = (uint32_t)(prod >> 32); |
| 449 |
*/ |
| 450 |
prod = k2 + t32[i+10] + carry; |
| 451 |
t32[i+10] = (uint32_t)prod; |
| 452 |
carry = (uint32_t)(prod >> 32); |
| 453 |
*/ |
| 454 |
prod = k2 + t32[i+11] + carry; |
| 455 |
t32[i+11] = (uint32_t)prod; |
| 456 |
carry = (uint32_t)(prod >> 32); |
| 457 |
|
| 458 |
for (j=WORDS_32; carry; j++) { |
| 459 |
t32[i+j] += carry; |
| 460 |
carry = t32[i+j] < carry; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
for (i=0; i<PREDIV_WORDS_64; i++) { |
| 465 |
t[i] = ((uint64_t)t32[2*i+1]<<32) + t32[2*i]; |
| 466 |
} |
| 467 |
|
| 468 |
#elif SYS_BITS == 64 |
| 469 |
|
| 470 |
for (i=0; i<WORDS_64; i++) { |
| 471 |
unsigned j; |
| 472 |
uint64_t carry; |
| 473 |
uint64_t k, k2_lo, k2_hi; |
| 474 |
uint64_t prod_lo, prod_hi; |
| 475 |
|
| 476 |
k = t[i] + (t[i] << 32); |
| 477 |
k2_lo = (uint64_t)(0 - k); |
| 478 |
k2_hi = k - (k!=0); |
| 479 |
|
| 480 |
*/ |
| 481 |
DP_MULT(n[0], k, prod_lo, prod_hi); |
| 482 |
t[i+0] += prod_lo; |
| 483 |
prod_hi += t[i+0] < prod_lo; |
| 484 |
carry = prod_hi; |
| 485 |
³² */ |
| 486 |
DP_MULT(n[1], k, prod_lo, prod_hi); |
| 487 |
prod_lo += carry; |
| 488 |
prod_hi += prod_lo < carry; |
| 489 |
t[i+1] += prod_lo; |
| 490 |
prod_hi += t[i+1] < prod_lo; |
| 491 |
carry = prod_hi; |
| 492 |
2 */ |
| 493 |
DP_MULT(n[2], k, prod_lo, prod_hi); |
| 494 |
prod_lo += carry; |
| 495 |
prod_hi += prod_lo < carry; |
| 496 |
t[i+2] += prod_lo; |
| 497 |
prod_hi += t[i+2] < prod_lo; |
| 498 |
carry = prod_hi; |
| 499 |
1 */ |
| 500 |
prod_lo = k2_lo; |
| 501 |
prod_hi = k2_hi; |
| 502 |
prod_lo += carry; |
| 503 |
prod_hi += prod_lo < carry; |
| 504 |
t[i+3] += prod_lo; |
| 505 |
prod_hi += t[i+3] < prod_lo; |
| 506 |
carry = prod_hi; |
| 507 |
1 */ |
| 508 |
prod_lo = k2_lo; |
| 509 |
prod_hi = k2_hi; |
| 510 |
prod_lo += carry; |
| 511 |
prod_hi += prod_lo < carry; |
| 512 |
t[i+4] += prod_lo; |
| 513 |
prod_hi += t[i+4] < prod_lo; |
| 514 |
carry = prod_hi; |
| 515 |
1 */ |
| 516 |
prod_lo = k2_lo; |
| 517 |
prod_hi = k2_hi; |
| 518 |
prod_lo += carry; |
| 519 |
prod_hi += prod_lo < carry; |
| 520 |
t[i+5] += prod_lo; |
| 521 |
prod_hi += t[i+5] < prod_lo; |
| 522 |
carry = prod_hi; |
| 523 |
|
| 524 |
for (j=WORDS_64; carry; j++) { |
| 525 |
t[i+j] += carry; |
| 526 |
carry = t[i+j] < carry; |
| 527 |
} |
| 528 |
} |
| 529 |
#else |
| 530 |
#error You must define the SYS_BITS macro |
| 531 |
#endif |
| 532 |
|
| 533 |
assert(t[PREDIV_WORDS_64-1] <= 1); |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
sub(t2, &t[WORDS_64], n, WORDS_64); |
| 539 |
cond = (unsigned)(t[PREDIV_WORDS_64-1] | (uint64_t)ge(&t[WORDS_64], n, WORDS_64)); |
| 540 |
mod_select(out, t2, &t[WORDS_64], cond, WORDS_64); |
| 541 |
|
| 542 |
#undef WORDS_64 |
| 543 |
#undef PREDIV_WORDS_64 |
| 544 |
#undef WORDS_32 |
| 545 |
#undef PREDIV_WORDS_32 |
| 546 |
} |
| 547 |
|
| 548 |
STATIC void mont_mult_p521(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw) |
| 549 |
{ |
| 550 |
uint64_t *t, *scratchpad, *s, *tmp1, *tmp2; |
| 551 |
|
| 552 |
assert(nw == 9); |
| 553 |
assert(m0 == 1); |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
+ y |
| 558 |
to: |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
t = tmp; |
| 571 |
scratchpad = t + 2*nw; |
| 572 |
s = scratchpad + 3*nw; |
| 573 |
tmp1 = scratchpad; |
| 574 |
tmp2 = scratchpad + nw; |
| 575 |
|
| 576 |
if (a == b) { |
| 577 |
square(t, scratchpad, a, 9); |
| 578 |
} else { |
| 579 |
product(t, scratchpad, a, b, 9); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
s[0] = (t[8] >> 9) | (t[9] << 55); t[8] &= 0x1FF; |
| 584 |
s[1] = (t[9] >> 9) | (t[10] << 55); |
| 585 |
s[2] = (t[10] >> 9) | (t[11] << 55); |
| 586 |
s[3] = (t[11] >> 9) | (t[12] << 55); |
| 587 |
s[4] = (t[12] >> 9) | (t[13] << 55); |
| 588 |
s[5] = (t[13] >> 9) | (t[14] << 55); |
| 589 |
s[6] = (t[14] >> 9) | (t[15] << 55); |
| 590 |
s[7] = (t[15] >> 9) | (t[16] << 55); |
| 591 |
s[8] = t[16] >> 9; |
| 592 |
|
| 593 |
add_mod(out, t, s, n, tmp1, tmp2, nw); |
| 594 |
} |
| 595 |
|
| 596 |
STATIC void mont_mult_ed448(uint64_t *out, const uint64_t *a, const uint64_t *b, const uint64_t *n, uint64_t m0, uint64_t *tmp, size_t nw) |
| 597 |
{ |
| 598 |
size_t i; |
| 599 |
uint64_t *t, *scratchpad, *t2; |
| 600 |
unsigned cond; |
| 601 |
|
| 602 |
assert(nw == 7); |
| 603 |
assert(m0 == 1); |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
t = tmp; |
| 613 |
scratchpad = tmp + 3*nw; |
| 614 |
t2 = scratchpad + 3*nw; |
| 615 |
|
| 616 |
if (a == b) { |
| 617 |
square(t, scratchpad, a, nw); |
| 618 |
} else { |
| 619 |
product(t, scratchpad, a, b, nw); |
| 620 |
} |
| 621 |
|
| 622 |
t[2*nw] = 0; |
| 623 |
|
| 624 |
|
| 625 |
for (i=0; i<7; i++) { |
| 626 |
uint64_t k, k2_lo, k2_hi; |
| 627 |
uint64_t carry, j; |
| 628 |
uint64_t prod_lo, prod_hi; |
| 629 |
|
| 630 |
k = t[i]; |
| 631 |
k2_lo = (uint64_t)(0 - k); |
| 632 |
k2_hi = k - (k!=0); |
| 633 |
|
| 634 |
1 */ |
| 635 |
prod_lo = k2_lo; |
| 636 |
prod_hi = k2_hi; |
| 637 |
t[i+0] += prod_lo; |
| 638 |
prod_hi += t[i+0] < prod_lo; |
| 639 |
carry = prod_hi; |
| 640 |
|
| 641 |
1 */ |
| 642 |
prod_lo = k2_lo; |
| 643 |
prod_hi = k2_hi; |
| 644 |
prod_lo += carry; |
| 645 |
prod_hi += prod_lo < carry; |
| 646 |
t[i+1] += prod_lo; |
| 647 |
prod_hi += t[i+1] < prod_lo; |
| 648 |
carry = prod_hi; |
| 649 |
|
| 650 |
1 */ |
| 651 |
prod_lo = k2_lo; |
| 652 |
prod_hi = k2_hi; |
| 653 |
prod_lo += carry; |
| 654 |
prod_hi += prod_lo < carry; |
| 655 |
t[i+2] += prod_lo; |
| 656 |
prod_hi += t[i+2] < prod_lo; |
| 657 |
carry = prod_hi; |
| 658 |
|
| 659 |
- 1 */ |
| 660 |
DP_MULT(n[3], k, prod_lo, prod_hi); |
| 661 |
prod_lo += carry; |
| 662 |
prod_hi += prod_lo < carry; |
| 663 |
t[i+3] += prod_lo; |
| 664 |
prod_hi += t[i+3] < prod_lo; |
| 665 |
carry = prod_hi; |
| 666 |
|
| 667 |
1 */ |
| 668 |
prod_lo = k2_lo; |
| 669 |
prod_hi = k2_hi; |
| 670 |
prod_lo += carry; |
| 671 |
prod_hi += prod_lo < carry; |
| 672 |
t[i+4] += prod_lo; |
| 673 |
prod_hi += t[i+4] < prod_lo; |
| 674 |
carry = prod_hi; |
| 675 |
|
| 676 |
1 */ |
| 677 |
prod_lo = k2_lo; |
| 678 |
prod_hi = k2_hi; |
| 679 |
prod_lo += carry; |
| 680 |
prod_hi += prod_lo < carry; |
| 681 |
t[i+5] += prod_lo; |
| 682 |
prod_hi += t[i+5] < prod_lo; |
| 683 |
carry = prod_hi; |
| 684 |
|
| 685 |
1 */ |
| 686 |
prod_lo = k2_lo; |
| 687 |
prod_hi = k2_hi; |
| 688 |
prod_lo += carry; |
| 689 |
prod_hi += prod_lo < carry; |
| 690 |
t[i+6] += prod_lo; |
| 691 |
prod_hi += t[i+6] < prod_lo; |
| 692 |
carry = prod_hi; |
| 693 |
|
| 694 |
for (j=7; carry; j++) { |
| 695 |
t[i+j] += carry; |
| 696 |
carry = t[i+j] < carry; |
| 697 |
} |
| 698 |
|
| 699 |
assert(j <= (15-i)); |
| 700 |
} |
| 701 |
|
| 702 |
assert(t[2*nw] <= 1); |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
sub(t2, &t[nw], n, nw); |
| 708 |
cond = (unsigned)(t[2*nw] | (uint64_t)ge(&t[nw], n, nw)); |
| 709 |
mod_select(out, t2, &t[nw], cond, (unsigned)nw); |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
void mont_context_free(MontContext *ctx) |
| 716 |
{ |
| 717 |
if (NULL == ctx) |
| 718 |
return; |
| 719 |
free(ctx->one); |
| 720 |
free(ctx->r2_mod_n); |
| 721 |
free(ctx->r_mod_n); |
| 722 |
free(ctx->modulus); |
| 723 |
free(ctx->modulus_min_2); |
| 724 |
free(ctx); |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
size_t mont_bytes(const MontContext *ctx) |
| 731 |
{ |
| 732 |
if (NULL == ctx) |
| 733 |
return 0; |
| 734 |
return ctx->bytes; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
int mont_number(uint64_t **out, unsigned count, const MontContext *ctx) |
| 751 |
{ |
| 752 |
if (NULL == out || NULL == ctx) |
| 753 |
return ERR_NULL; |
| 754 |
|
| 755 |
*out = (uint64_t*)calloc(count * ctx->words, sizeof(uint64_t)); |
| 756 |
if (NULL == *out) |
| 757 |
return ERR_MEMORY; |
| 758 |
|
| 759 |
return 0; |
| 760 |
} |
| 761 |
|
| 762 |
int mont_random_number(uint64_t **out, unsigned count, uint64_t seed, const MontContext *ctx) |
| 763 |
{ |
| 764 |
int res; |
| 765 |
unsigned i; |
| 766 |
uint64_t *number; |
| 767 |
|
| 768 |
res = mont_number(out, count, ctx); |
| 769 |
if (res) |
| 770 |
return res; |
| 771 |
|
| 772 |
number = *out; |
| 773 |
expand_seed(seed, (uint8_t*)number, count * ctx->bytes); |
| 774 |
for (i=0; i<count; i++, number += ctx->words) { |
| 775 |
number[ctx->words-1] = 0; |
| 776 |
} |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
int mont_from_bytes(uint64_t **out, const uint8_t *number, size_t len, const MontContext *ctx) |
| 793 |
{ |
| 794 |
uint64_t *encoded = NULL; |
| 795 |
uint64_t *tmp1 = NULL; |
| 796 |
uint64_t *scratchpad = NULL; |
| 797 |
int res = 0; |
| 798 |
|
| 799 |
if (NULL == out || NULL == ctx || NULL == number) |
| 800 |
return ERR_NULL; |
| 801 |
|
| 802 |
*out = NULL; |
| 803 |
|
| 804 |
|
| 805 |
if (0 == len) |
| 806 |
return ERR_NOT_ENOUGH_DATA; |
| 807 |
while (len>1 && *number==0) { |
| 808 |
len--; |
| 809 |
number++; |
| 810 |
} |
| 811 |
|
| 812 |
if (ctx->bytes < len) |
| 813 |
return ERR_VALUE; |
| 814 |
|
| 815 |
|
| 816 |
*out = encoded = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 817 |
if (NULL == encoded) |
| 818 |
return ERR_MEMORY; |
| 819 |
|
| 820 |
|
| 821 |
tmp1 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 822 |
if (NULL == tmp1) { |
| 823 |
res = ERR_MEMORY; |
| 824 |
goto cleanup; |
| 825 |
} |
| 826 |
bytes_to_words(tmp1, ctx->words, number, len); |
| 827 |
|
| 828 |
|
| 829 |
if (ge(tmp1, ctx->modulus, ctx->words)) { |
| 830 |
res = ERR_VALUE; |
| 831 |
goto cleanup; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t)); |
| 836 |
if (NULL == scratchpad) { |
| 837 |
res = ERR_MEMORY; |
| 838 |
goto cleanup; |
| 839 |
} |
| 840 |
|
| 841 |
if (ctx->modulus_type != ModulusP521) |
| 842 |
mont_mult_generic(encoded, tmp1, ctx->r2_mod_n, ctx->modulus, ctx->m0, scratchpad, ctx->words); |
| 843 |
else |
| 844 |
mont_copy(encoded, tmp1, ctx); |
| 845 |
res = 0; |
| 846 |
|
| 847 |
cleanup: |
| 848 |
free(scratchpad); |
| 849 |
free(tmp1); |
| 850 |
if (res != 0) { |
| 851 |
free(encoded); |
| 852 |
*out = NULL; |
| 853 |
} |
| 854 |
return res; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
int mont_to_bytes(uint8_t *number, size_t len, const uint64_t* mont_number, const MontContext *ctx) |
| 868 |
{ |
| 869 |
uint64_t *tmp1 = NULL; |
| 870 |
uint64_t *scratchpad = NULL; |
| 871 |
int res; |
| 872 |
|
| 873 |
if (NULL == number || NULL == ctx || NULL == mont_number) |
| 874 |
return ERR_NULL; |
| 875 |
|
| 876 |
if (len < ctx->modulus_len) |
| 877 |
return ERR_NOT_ENOUGH_DATA; |
| 878 |
|
| 879 |
|
| 880 |
tmp1 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 881 |
if (NULL == tmp1) |
| 882 |
return ERR_MEMORY; |
| 883 |
|
| 884 |
|
| 885 |
scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t)); |
| 886 |
if (NULL == scratchpad) { |
| 887 |
free(tmp1); |
| 888 |
return ERR_MEMORY; |
| 889 |
} |
| 890 |
|
| 891 |
if (ctx->modulus_type != ModulusP521) |
| 892 |
mont_mult_generic(tmp1, mont_number, ctx->one, ctx->modulus, ctx->m0, scratchpad, ctx->words); |
| 893 |
else |
| 894 |
mont_copy(tmp1, mont_number, ctx); |
| 895 |
res = words_to_bytes(number, len, tmp1, ctx->words); |
| 896 |
|
| 897 |
free(scratchpad); |
| 898 |
free(tmp1); |
| 899 |
return res; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
int mont_add(uint64_t* out, const uint64_t* a, const uint64_t* b, uint64_t *tmp, const MontContext *ctx) |
| 913 |
{ |
| 914 |
if (NULL == out || NULL == a || NULL == b || NULL == tmp || NULL == ctx) |
| 915 |
return ERR_NULL; |
| 916 |
add_mod(out, a, b, ctx->modulus, tmp, tmp + ctx->words, ctx->words); |
| 917 |
return 0; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
int mont_mult(uint64_t* out, const uint64_t* a, const uint64_t *b, uint64_t *tmp, const MontContext *ctx) |
| 931 |
{ |
| 932 |
if (NULL == out || NULL == a || NULL == b || NULL == tmp || NULL == ctx) |
| 933 |
return ERR_NULL; |
| 934 |
|
| 935 |
switch (ctx->modulus_type) { |
| 936 |
case ModulusP256: |
| 937 |
mont_mult_p256(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words); |
| 938 |
break; |
| 939 |
case ModulusP384: |
| 940 |
mont_mult_p384(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words); |
| 941 |
break; |
| 942 |
case ModulusP521: |
| 943 |
mont_mult_p521(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words); |
| 944 |
break; |
| 945 |
case ModulusEd448: |
| 946 |
mont_mult_ed448(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words); |
| 947 |
break; |
| 948 |
case ModulusGeneric: |
| 949 |
mont_mult_generic(out, a, b, ctx->modulus, ctx->m0, tmp, ctx->words); |
| 950 |
break; |
| 951 |
default: |
| 952 |
return ERR_MODULUS; |
| 953 |
} |
| 954 |
|
| 955 |
return 0; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
int mont_sub(uint64_t *out, const uint64_t *a, const uint64_t *b, uint64_t *tmp, const MontContext *ctx) |
| 970 |
{ |
| 971 |
if (NULL == out || NULL == a || NULL == b || NULL == tmp || NULL == ctx) |
| 972 |
return ERR_NULL; |
| 973 |
|
| 974 |
return sub_mod(out, a, b, ctx->modulus, tmp, tmp + ctx->words, ctx->words); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
int mont_inv_prime(uint64_t *out, uint64_t *a, const MontContext *ctx) |
| 989 |
{ |
| 990 |
unsigned idx_word; |
| 991 |
uint64_t bit; |
| 992 |
uint64_t *tmp1 = NULL; |
| 993 |
uint64_t *scratchpad = NULL; |
| 994 |
uint64_t *exponent = NULL; |
| 995 |
int res; |
| 996 |
|
| 997 |
if (NULL == out || NULL == a || NULL == ctx) |
| 998 |
return ERR_NULL; |
| 999 |
|
| 1000 |
tmp1 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1001 |
if (NULL == tmp1) |
| 1002 |
return ERR_MEMORY; |
| 1003 |
|
| 1004 |
scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t)); |
| 1005 |
if (NULL == scratchpad) { |
| 1006 |
res = ERR_MEMORY; |
| 1007 |
goto cleanup; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
exponent = ctx->modulus_min_2; |
| 1012 |
|
| 1013 |
|
| 1014 |
idx_word = ctx->words-1; |
| 1015 |
for (;;) { |
| 1016 |
if (exponent[idx_word] != 0) |
| 1017 |
break; |
| 1018 |
if (idx_word-- == 0) |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
for (bit = (uint64_t)1U << 63; 0 == (exponent[idx_word] & bit); bit>>=1); |
| 1022 |
|
| 1023 |
|
| 1024 |
memcpy(out, ctx->r_mod_n, ctx->bytes); |
| 1025 |
|
| 1026 |
|
| 1027 |
for (;;) { |
| 1028 |
while (bit > 0) { |
| 1029 |
mont_mult(tmp1, out, out, scratchpad, ctx); |
| 1030 |
if (exponent[idx_word] & bit) { |
| 1031 |
mont_mult(out, tmp1, a, scratchpad, ctx); |
| 1032 |
} else { |
| 1033 |
memcpy(out, tmp1, ctx->bytes); |
| 1034 |
} |
| 1035 |
bit >>= 1; |
| 1036 |
} |
| 1037 |
if (idx_word-- == 0) |
| 1038 |
break; |
| 1039 |
bit = (uint64_t)1 << 63; |
| 1040 |
} |
| 1041 |
res = 0; |
| 1042 |
|
| 1043 |
cleanup: |
| 1044 |
free(tmp1); |
| 1045 |
free(scratchpad); |
| 1046 |
return res; |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
int mont_set(uint64_t *out, uint64_t x, const MontContext *ctx) |
| 1058 |
{ |
| 1059 |
uint64_t *tmp, *scratchpad; |
| 1060 |
|
| 1061 |
if (NULL == out || NULL == ctx) |
| 1062 |
return ERR_NULL; |
| 1063 |
|
| 1064 |
if (x == 0) { |
| 1065 |
memset(out, 0, ctx->bytes); |
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
if (x == 1) { |
| 1069 |
mont_copy(out, ctx->r_mod_n, ctx); |
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
|
| 1073 |
tmp = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1074 |
if (NULL == tmp) |
| 1075 |
return ERR_MEMORY; |
| 1076 |
tmp[0] = x; |
| 1077 |
|
| 1078 |
scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t)); |
| 1079 |
if (NULL == scratchpad) { |
| 1080 |
free(tmp); |
| 1081 |
return ERR_MEMORY; |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (ctx->modulus_type != ModulusP521) |
| 1085 |
mont_mult_generic(out, tmp, ctx->r2_mod_n, ctx->modulus, ctx->m0, scratchpad, ctx->words); |
| 1086 |
else |
| 1087 |
mont_copy(out, tmp, ctx); |
| 1088 |
|
| 1089 |
free(tmp); |
| 1090 |
free(scratchpad); |
| 1091 |
|
| 1092 |
return 0; |
| 1093 |
} |
| 1094 |
|
| 1095 |
static int cmp_modulus(const uint8_t *mod1, size_t mod1_len, const uint8_t *mod2, size_t mod2_len) |
| 1096 |
{ |
| 1097 |
size_t diff; |
| 1098 |
|
| 1099 |
if (mod1_len > mod2_len) { |
| 1100 |
diff = mod1_len - mod2_len; |
| 1101 |
if (0 != memcmp(mod1+diff, mod2, mod2_len)) |
| 1102 |
return -1; |
| 1103 |
if (NULL != memchr_not(mod1, 0, diff)) |
| 1104 |
return -1; |
| 1105 |
} else { |
| 1106 |
diff = mod2_len - mod1_len; |
| 1107 |
if (0 != memcmp(mod2+diff, mod1, mod1_len)) |
| 1108 |
return -1; |
| 1109 |
if (NULL != memchr_not(mod2, 0, diff)) |
| 1110 |
return -1; |
| 1111 |
} |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
int mont_context_init(MontContext **out, const uint8_t *modulus, size_t mod_len) |
| 1125 |
{ |
| 1126 |
const uint8_t p256_mod[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, |
| 1127 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1128 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1129 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
| 1130 |
const uint8_t p384_mod[48] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1131 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1132 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1133 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 1134 |
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, |
| 1135 |
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}; |
| 1136 |
const uint8_t p521_mod[66] = {0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1137 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1138 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1139 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1140 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1141 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1142 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1143 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1144 |
0xFF, 0xFF}; |
| 1145 |
const uint8_t ed448_mod[56] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1146 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1147 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1148 |
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1149 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1150 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1151 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
| 1152 |
uint64_t *scratchpad = NULL; |
| 1153 |
MontContext *ctx; |
| 1154 |
int res; |
| 1155 |
|
| 1156 |
if (NULL == out || NULL == modulus) |
| 1157 |
return ERR_NULL; |
| 1158 |
|
| 1159 |
|
| 1160 |
while (mod_len>0 && *modulus==0) { |
| 1161 |
modulus++; |
| 1162 |
mod_len--; |
| 1163 |
} |
| 1164 |
if (0 == mod_len) |
| 1165 |
return ERR_MODULUS; |
| 1166 |
|
| 1167 |
|
| 1168 |
if (is_even(modulus[mod_len-1])) |
| 1169 |
return ERR_MODULUS; |
| 1170 |
if (mod_len==1 && modulus[0]==1) |
| 1171 |
return ERR_MODULUS; |
| 1172 |
|
| 1173 |
*out = ctx = (MontContext*)calloc(1, sizeof(MontContext)); |
| 1174 |
if (NULL == ctx) |
| 1175 |
return ERR_MEMORY; |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
ctx->modulus_type = ModulusGeneric; |
| 1182 |
switch (mod_len) { |
| 1183 |
case sizeof(p256_mod): |
| 1184 |
if (0 == cmp_modulus(modulus, mod_len, p256_mod, sizeof(p256_mod))) { |
| 1185 |
ctx->modulus_type = ModulusP256; |
| 1186 |
} |
| 1187 |
break; |
| 1188 |
case sizeof(p384_mod): |
| 1189 |
if (0 == cmp_modulus(modulus, mod_len, p384_mod, sizeof(p384_mod))) { |
| 1190 |
ctx->modulus_type = ModulusP384; |
| 1191 |
} |
| 1192 |
break; |
| 1193 |
case sizeof(p521_mod): |
| 1194 |
if (0 == cmp_modulus(modulus, mod_len, p521_mod, sizeof(p521_mod))) { |
| 1195 |
ctx->modulus_type = ModulusP521; |
| 1196 |
} |
| 1197 |
break; |
| 1198 |
case sizeof(ed448_mod): |
| 1199 |
if (0 == cmp_modulus(modulus, mod_len, ed448_mod, sizeof(ed448_mod))) { |
| 1200 |
ctx->modulus_type = ModulusEd448; |
| 1201 |
} |
| 1202 |
break; |
| 1203 |
} |
| 1204 |
|
| 1205 |
ctx->words = ((unsigned)mod_len + 7) / 8; |
| 1206 |
ctx->bytes = (unsigned)(ctx->words * sizeof(uint64_t)); |
| 1207 |
ctx->modulus_len = (unsigned)mod_len; |
| 1208 |
|
| 1209 |
|
| 1210 |
ctx->modulus = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1211 |
if (0 == ctx->modulus) { |
| 1212 |
res = ERR_MEMORY; |
| 1213 |
goto cleanup; |
| 1214 |
} |
| 1215 |
bytes_to_words(ctx->modulus, ctx->words, modulus, mod_len); |
| 1216 |
|
| 1217 |
|
| 1218 |
ctx->one = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1219 |
if (NULL == ctx->one) { |
| 1220 |
res = ERR_MEMORY; |
| 1221 |
goto cleanup; |
| 1222 |
} |
| 1223 |
ctx->one[0] = 1; |
| 1224 |
|
| 1225 |
/ |
| 1226 |
**/ |
| 1227 |
ctx->r2_mod_n = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1228 |
if (0 == ctx->r2_mod_n) { |
| 1229 |
res = ERR_MEMORY; |
| 1230 |
goto cleanup; |
| 1231 |
} |
| 1232 |
if (ctx->modulus_type != ModulusP521) { |
| 1233 |
rsquare(ctx->r2_mod_n, ctx->modulus, ctx->words); |
| 1234 |
ctx->m0 = inverse64(~ctx->modulus[0]+1); |
| 1235 |
} else { |
| 1236 |
memcpy(ctx->r2_mod_n, ctx->one, ctx->words * sizeof(uint64_t)); |
| 1237 |
ctx->m0 = 1U; |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
ctx->r_mod_n = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1242 |
if (NULL == ctx->r_mod_n) { |
| 1243 |
res = ERR_MEMORY; |
| 1244 |
goto cleanup; |
| 1245 |
} |
| 1246 |
scratchpad = (uint64_t*)calloc(SCRATCHPAD_NR, ctx->words*sizeof(uint64_t)); |
| 1247 |
if (NULL == scratchpad) { |
| 1248 |
res = ERR_MEMORY; |
| 1249 |
goto cleanup; |
| 1250 |
} |
| 1251 |
if (ctx->modulus_type != ModulusP521) |
| 1252 |
mont_mult_generic(ctx->r_mod_n, ctx->one, ctx->r2_mod_n, ctx->modulus, ctx->m0, scratchpad, ctx->words); |
| 1253 |
else |
| 1254 |
memcpy(ctx->r_mod_n, ctx->one, ctx->words * sizeof(uint64_t)); |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
ctx->modulus_min_2 = (uint64_t*)calloc(ctx->words, sizeof(uint64_t)); |
| 1259 |
if (NULL == ctx->modulus_min_2) { |
| 1260 |
res = ERR_MEMORY; |
| 1261 |
goto cleanup; |
| 1262 |
} |
| 1263 |
sub(ctx->modulus_min_2, ctx->modulus, ctx->one, ctx->words); |
| 1264 |
sub(ctx->modulus_min_2, ctx->modulus_min_2, ctx->one, ctx->words); |
| 1265 |
|
| 1266 |
res = 0; |
| 1267 |
|
| 1268 |
cleanup: |
| 1269 |
free(scratchpad); |
| 1270 |
if (res != 0) { |
| 1271 |
mont_context_free(ctx); |
| 1272 |
} |
| 1273 |
return res; |
| 1274 |
} |
| 1275 |
|
| 1276 |
int mont_is_zero(const uint64_t *a, const MontContext *ctx) |
| 1277 |
{ |
| 1278 |
unsigned i; |
| 1279 |
uint64_t sum = 0; |
| 1280 |
|
| 1281 |
if (NULL == a || NULL == ctx) |
| 1282 |
return -1; |
| 1283 |
|
| 1284 |
for (i=0; i<ctx->words; i++) { |
| 1285 |
sum |= *a++; |
| 1286 |
} |
| 1287 |
|
| 1288 |
return (sum == 0); |
| 1289 |
} |
| 1290 |
|
| 1291 |
int mont_is_one(const uint64_t *a, const MontContext *ctx) |
| 1292 |
{ |
| 1293 |
unsigned i; |
| 1294 |
uint64_t sum = 0; |
| 1295 |
|
| 1296 |
if (NULL == a || NULL == ctx) |
| 1297 |
return -1; |
| 1298 |
|
| 1299 |
for (i=0; i<ctx->words; i++) { |
| 1300 |
sum |= a[i] ^ ctx->r_mod_n[i]; |
| 1301 |
} |
| 1302 |
|
| 1303 |
return (sum == 0); |
| 1304 |
} |
| 1305 |
|
| 1306 |
int mont_is_equal(const uint64_t *a, const uint64_t *b, const MontContext *ctx) |
| 1307 |
{ |
| 1308 |
unsigned i; |
| 1309 |
uint64_t result = 0; |
| 1310 |
|
| 1311 |
if (NULL == a || NULL == b || NULL == ctx) |
| 1312 |
return -1; |
| 1313 |
|
| 1314 |
for (i=0; i<ctx->words; i++) { |
| 1315 |
result |= *a++ ^ *b++; |
| 1316 |
} |
| 1317 |
|
| 1318 |
return (result == 0); |
| 1319 |
} |
| 1320 |
|
| 1321 |
int mont_copy(uint64_t *out, const uint64_t *a, const MontContext *ctx) |
| 1322 |
{ |
| 1323 |
unsigned i; |
| 1324 |
|
| 1325 |
if (NULL == out || NULL == a || NULL == ctx) |
| 1326 |
return ERR_NULL; |
| 1327 |
|
| 1328 |
for (i=0; i<ctx->words; i++) { |
| 1329 |
*out++ = *a++; |
| 1330 |
} |
| 1331 |
|
| 1332 |
return 0; |
| 1333 |
} |