1 /* crypto/bn/bn_nist.c */
3 * Written by Nils Larsch for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
64 #define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
65 #define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
66 #define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
67 #define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
70 static const BN_ULONG _nist_p_192[] =
71 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFEULL,
72 0xFFFFFFFFFFFFFFFFULL};
73 static const BN_ULONG _nist_p_224[] =
74 {0x0000000000000001ULL,0xFFFFFFFF00000000ULL,
75 0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL};
76 static const BN_ULONG _nist_p_256[] =
77 {0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL,
78 0x0000000000000000ULL,0xFFFFFFFF00000001ULL};
79 static const BN_ULONG _nist_p_384[] =
80 {0x00000000FFFFFFFFULL,0xFFFFFFFF00000000ULL,
81 0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFFULL,
82 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL};
83 static const BN_ULONG _nist_p_521[] =
84 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
85 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
86 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
87 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
88 0x00000000000001FFULL};
90 static const BN_ULONG _nist_p_192[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,
91 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
92 static const BN_ULONG _nist_p_224[] = {0x00000001,0x00000000,0x00000000,
93 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
94 static const BN_ULONG _nist_p_256[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
95 0x00000000,0x00000000,0x00000000,0x00000001,0xFFFFFFFF};
96 static const BN_ULONG _nist_p_384[] = {0xFFFFFFFF,0x00000000,0x00000000,
97 0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
98 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
99 static const BN_ULONG _nist_p_521[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
100 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
101 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
102 0xFFFFFFFF,0x000001FF};
106 static const BIGNUM _bignum_nist_p_192 =
108 (BN_ULONG *)_nist_p_192,
115 static const BIGNUM _bignum_nist_p_224 =
117 (BN_ULONG *)_nist_p_224,
124 static const BIGNUM _bignum_nist_p_256 =
126 (BN_ULONG *)_nist_p_256,
133 static const BIGNUM _bignum_nist_p_384 =
135 (BN_ULONG *)_nist_p_384,
142 static const BIGNUM _bignum_nist_p_521 =
144 (BN_ULONG *)_nist_p_521,
152 const BIGNUM *BN_get0_nist_prime_192(void)
154 return &_bignum_nist_p_192;
157 const BIGNUM *BN_get0_nist_prime_224(void)
159 return &_bignum_nist_p_224;
162 const BIGNUM *BN_get0_nist_prime_256(void)
164 return &_bignum_nist_p_256;
167 const BIGNUM *BN_get0_nist_prime_384(void)
169 return &_bignum_nist_p_384;
172 const BIGNUM *BN_get0_nist_prime_521(void)
174 return &_bignum_nist_p_521;
178 static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max)
181 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
183 OPENSSL_assert(top <= max);
184 for (i = (top); i != 0; i--)
186 for (i = (max) - (top); i != 0; i--)
187 *_tmp1++ = (BN_ULONG) 0;
190 static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
193 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
194 for (i = (top); i != 0; i--)
199 #define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
200 #define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
202 #define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
203 #define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
205 #define bn_cp_64(to, n, from, m) \
207 bn_cp_32(to, (n)*2, from, (m)*2); \
208 bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
210 #define bn_64_set_0(to, n) \
212 bn_32_set_0(to, (n)*2); \
213 bn_32_set_0(to, (n)*2+1); \
216 #define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
217 #define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
219 #endif /* BN_BITS2 != 64 */
222 #define nist_set_192(to, from, a1, a2, a3) \
224 if (a3 != 0) bn_cp_64(to, 0, from, (a3) - 3) else bn_64_set_0(to, 0)\
225 bn_cp_64(to, 1, from, (a2) - 3) \
226 if (a1 != 0) bn_cp_64(to, 2, from, (a1) - 3) else bn_64_set_0(to, 2)\
229 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
234 register BN_ULONG *r_d, *a_d = a->d;
235 BN_ULONG t_d[BN_NIST_192_TOP],
236 buf[BN_NIST_192_TOP],
237 c_d[BN_NIST_192_TOP],
241 field = &_bignum_nist_p_192; /* just to make sure */
243 if (BN_is_negative(a) || a->top > 2*BN_NIST_192_TOP)
244 return BN_nnmod(r, field, a, ctx);
246 i = BN_ucmp(field, a);
253 return (r == a) ? 1 : (BN_copy(r ,a) != NULL);
257 if (!bn_wexpand(r, BN_NIST_192_TOP))
260 nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
265 nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
267 nist_set_192(t_d, buf, 0, 3, 3);
268 carry = bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
269 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192,BN_NIST_192_TOP);
270 mask = ~mask | (0-(size_t)carry);
271 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
273 nist_set_192(t_d, buf, 4, 4, 0);
274 carry = bn_add_words(r_d, res, t_d, BN_NIST_192_TOP);
275 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192,BN_NIST_192_TOP);
276 mask = ~mask | (0-(size_t)carry);
277 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
279 nist_set_192(t_d, buf, 5, 5, 5)
280 carry = bn_add_words(r_d, res, t_d, BN_NIST_192_TOP);
281 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192,BN_NIST_192_TOP);
282 mask = ~mask | (0-(size_t)carry);
283 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
285 nist_cp_bn(r_d, res, BN_NIST_192_TOP);
286 r->top = BN_NIST_192_TOP;
289 if (BN_ucmp(field, r) <= 0)
291 if (!BN_usub(r, r, field)) return 0;
297 #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
299 if (a7 != 0) bn_cp_32(to, 0, from, (a7) - 7) else bn_32_set_0(to, 0)\
300 if (a6 != 0) bn_cp_32(to, 1, from, (a6) - 7) else bn_32_set_0(to, 1)\
301 if (a5 != 0) bn_cp_32(to, 2, from, (a5) - 7) else bn_32_set_0(to, 2)\
302 if (a4 != 0) bn_cp_32(to, 3, from, (a4) - 7) else bn_32_set_0(to, 3)\
303 if (a3 != 0) bn_cp_32(to, 4, from, (a3) - 7) else bn_32_set_0(to, 4)\
304 if (a2 != 0) bn_cp_32(to, 5, from, (a2) - 7) else bn_32_set_0(to, 5)\
305 if (a1 != 0) bn_cp_32(to, 6, from, (a1) - 7) else bn_32_set_0(to, 6)\
308 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
314 BN_ULONG *r_d, *a_d = a->d;
315 BN_ULONG t_d[BN_NIST_224_TOP],
316 buf[BN_NIST_224_TOP],
317 c_d[BN_NIST_224_TOP],
321 field = &_bignum_nist_p_224; /* just to make sure */
323 if (BN_is_negative(a) || a->top > 2*BN_NIST_224_TOP)
324 return BN_nnmod(r, field, a, ctx);
326 i = BN_ucmp(field, a);
333 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
337 if (!bn_wexpand(r, BN_NIST_224_TOP))
340 nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
345 nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
347 nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
348 carry = bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
349 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
350 mask = ~mask | (0-(size_t)carry);
351 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
353 nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
354 carry = bn_add_words(r_d, res, t_d, BN_NIST_224_TOP);
355 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
356 mask = ~mask | (0-(size_t)carry);
357 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
359 nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
361 carry = bn_sub_words(r_d, res, t_d, BN_NIST_224_TOP);
362 bn_add_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
363 mask = 0-(size_t)carry;
364 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
366 if (bn_sub_words(r_d, res, t_d, BN_NIST_224_TOP))
367 bn_add_words(r_d,r_d,_nist_p_224,BN_NIST_224_TOP);
369 nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
371 carry = bn_sub_words(r_d, res, t_d, BN_NIST_224_TOP);
372 bn_add_words(c_d,r_d,_nist_p_224,BN_NIST_224_TOP);
373 mask = 0-(size_t)carry;
374 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
376 nist_cp_bn(r_d, res, BN_NIST_224_TOP);
378 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP))
379 bn_add_words(r_d,r_d,_nist_p_224,BN_NIST_224_TOP);
381 r->top = BN_NIST_224_TOP;
384 if (BN_ucmp(field, r) <= 0)
386 if (!BN_usub(r, r, field)) return 0;
390 #else /* BN_BITS!=32 */
395 #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
397 if (a8 != 0) bn_cp_32(to, 0, from, (a8) - 8) else bn_32_set_0(to, 0)\
398 if (a7 != 0) bn_cp_32(to, 1, from, (a7) - 8) else bn_32_set_0(to, 1)\
399 if (a6 != 0) bn_cp_32(to, 2, from, (a6) - 8) else bn_32_set_0(to, 2)\
400 if (a5 != 0) bn_cp_32(to, 3, from, (a5) - 8) else bn_32_set_0(to, 3)\
401 if (a4 != 0) bn_cp_32(to, 4, from, (a4) - 8) else bn_32_set_0(to, 4)\
402 if (a3 != 0) bn_cp_32(to, 5, from, (a3) - 8) else bn_32_set_0(to, 5)\
403 if (a2 != 0) bn_cp_32(to, 6, from, (a2) - 8) else bn_32_set_0(to, 6)\
404 if (a1 != 0) bn_cp_32(to, 7, from, (a1) - 8) else bn_32_set_0(to, 7)\
407 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
413 register BN_ULONG *a_d = a->d, *r_d;
414 BN_ULONG t_d[BN_NIST_256_TOP],
415 buf[BN_NIST_256_TOP],
416 c_d[BN_NIST_256_TOP],
420 field = &_bignum_nist_p_256; /* just to make sure */
422 if (BN_is_negative(a) || a->top > 2*BN_NIST_256_TOP)
423 return BN_nnmod(r, field, a, ctx);
425 i = BN_ucmp(field, a);
432 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
436 if (!bn_wexpand(r, BN_NIST_256_TOP))
439 nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
444 nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP);
447 nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
449 nist_set_256(c_d,buf, 0, 15, 14, 13, 12, 0, 0, 0);
450 carry = bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
451 mask = 0-(size_t)bn_sub_words(c_d,t_d,_nist_p_256,BN_NIST_256_TOP);
452 mask = ~mask | (0-(size_t)carry);
453 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)t_d&~mask));
455 carry = bn_add_words(t_d, res, res, BN_NIST_256_TOP);
456 mask = 0-(size_t)bn_sub_words(c_d,t_d,_nist_p_256,BN_NIST_256_TOP);
457 mask = ~mask | (0-(size_t)carry);
458 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)t_d&~mask));
460 carry = bn_add_words(r_d, r_d, res, BN_NIST_256_TOP);
461 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
462 mask = ~mask | (0-(size_t)carry);
463 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
466 nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
467 carry = bn_add_words(r_d, res, t_d, BN_NIST_256_TOP);
468 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
469 mask = ~mask | (0-(size_t)carry);
470 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
473 nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
474 carry = bn_add_words(r_d, res, t_d, BN_NIST_256_TOP);
475 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
476 mask = ~mask | (0-(size_t)carry);
477 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
480 nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
482 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
483 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
484 mask = 0-(size_t)carry;
485 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
487 if (bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP))
488 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
491 nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
493 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
494 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
495 mask = 0-(size_t)carry;
496 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
498 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
499 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
502 nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
504 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
505 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
506 mask = 0-(size_t)carry;
507 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
509 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
510 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
513 nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
515 carry = bn_sub_words(r_d, res, t_d, BN_NIST_256_TOP);
516 bn_add_words(c_d,r_d,_nist_p_256,BN_NIST_256_TOP);
517 mask = 0-(size_t)carry;
518 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
520 nist_cp_bn(r_d, res, BN_NIST_384_TOP);
522 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
523 bn_add_words(r_d,r_d,_nist_p_256,BN_NIST_256_TOP);
525 r->top = BN_NIST_256_TOP;
528 if (BN_ucmp(field, r) <= 0)
530 if (!BN_usub(r, r, field)) return 0;
534 #else /* BN_BITS!=32 */
539 #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
541 if (a12 != 0) bn_cp_32(to, 0, from, (a12) - 12) else bn_32_set_0(to, 0)\
542 if (a11 != 0) bn_cp_32(to, 1, from, (a11) - 12) else bn_32_set_0(to, 1)\
543 if (a10 != 0) bn_cp_32(to, 2, from, (a10) - 12) else bn_32_set_0(to, 2)\
544 if (a9 != 0) bn_cp_32(to, 3, from, (a9) - 12) else bn_32_set_0(to, 3)\
545 if (a8 != 0) bn_cp_32(to, 4, from, (a8) - 12) else bn_32_set_0(to, 4)\
546 if (a7 != 0) bn_cp_32(to, 5, from, (a7) - 12) else bn_32_set_0(to, 5)\
547 if (a6 != 0) bn_cp_32(to, 6, from, (a6) - 12) else bn_32_set_0(to, 6)\
548 if (a5 != 0) bn_cp_32(to, 7, from, (a5) - 12) else bn_32_set_0(to, 7)\
549 if (a4 != 0) bn_cp_32(to, 8, from, (a4) - 12) else bn_32_set_0(to, 8)\
550 if (a3 != 0) bn_cp_32(to, 9, from, (a3) - 12) else bn_32_set_0(to, 9)\
551 if (a2 != 0) bn_cp_32(to, 10, from, (a2) - 12) else bn_32_set_0(to, 10)\
552 if (a1 != 0) bn_cp_32(to, 11, from, (a1) - 12) else bn_32_set_0(to, 11)\
555 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
561 register BN_ULONG *r_d, *a_d = a->d;
562 BN_ULONG t_d[BN_NIST_384_TOP],
563 buf[BN_NIST_384_TOP],
564 c_d[BN_NIST_384_TOP],
568 field = &_bignum_nist_p_384; /* just to make sure */
570 if (BN_is_negative(a) || a->top > 2*BN_NIST_384_TOP)
571 return BN_nnmod(r, field, a, ctx);
573 i = BN_ucmp(field, a);
580 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
584 if (!bn_wexpand(r, BN_NIST_384_TOP))
587 nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
592 nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP);
595 nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4);
598 register BN_ULONG *ap,t,c;
601 for (i = 3; i != 0; --i)
604 *(ap++)=((t<<1)|c)&BN_MASK2;
609 carry = bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
610 t_d, BN_NIST_256_TOP);
612 * we need if (result>=modulus) subtract(result,modulus);
613 * in n-bit space this can be expressed as
614 * if (carry || result>=modulus) subtract(result,modulus);
615 * the catch is that comparison implies subtraction and
616 * therefore one can write tmp=subtract(result,modulus);
617 * and then if(carry || !borrow) result=tmp; this's what
618 * happens below, but without explicit if:-) a.
620 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
621 mask = ~mask | (0-(size_t)carry);
622 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
625 carry = bn_add_words(r_d, res, buf, BN_NIST_384_TOP);
626 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
627 mask = ~mask | (0-(size_t)carry);
628 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
631 nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21);
632 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP);
633 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
634 mask = ~mask | (0-(size_t)carry);
635 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
638 nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0);
639 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP);
640 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
641 mask = ~mask | (0-(size_t)carry);
642 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
645 nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0);
646 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP);
647 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
648 mask = ~mask | (0-(size_t)carry);
649 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
652 nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20);
653 carry = bn_add_words(r_d, res, t_d, BN_NIST_384_TOP);
654 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
655 mask = ~mask | (0-(size_t)carry);
656 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
659 nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
661 carry = bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP);
662 bn_add_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
663 mask = 0-(size_t)carry;
664 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
666 if (bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP))
667 bn_add_words(r_d,r_d,_nist_p_384,BN_NIST_384_TOP);
670 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
672 carry = bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP);
673 bn_add_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
674 mask = 0-(size_t)carry;
675 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
677 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
678 bn_add_words(r_d,r_d,_nist_p_384,BN_NIST_384_TOP);
681 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
683 carry = bn_sub_words(r_d, res, t_d, BN_NIST_384_TOP);
684 bn_add_words(c_d,r_d,_nist_p_384,BN_NIST_384_TOP);
685 mask = 0-(size_t)carry;
686 res = (BN_ULONG *)(((size_t)c_d&mask) | ((size_t)r_d&~mask));
688 nist_cp_bn(r_d, res, BN_NIST_384_TOP);
690 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
691 bn_add_words(r_d,r_d,_nist_p_384,BN_NIST_384_TOP);
693 r->top = BN_NIST_384_TOP;
696 if (BN_ucmp(field, r) <= 0)
698 if (!BN_usub(r, r, field)) return 0;
702 #else /* BN_BITS!=32 */
707 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
711 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
713 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
718 field = &_bignum_nist_p_521; /* just to make sure */
720 if (BN_is_negative(a))
721 return BN_nnmod(r, field, a, ctx);
723 /* check whether a reduction is necessary */
725 if (top < BN_NIST_521_TOP || ( top == BN_NIST_521_TOP &&
726 (!(a->d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))))
728 int i = BN_ucmp(field, a);
737 OPENSSL_assert(i > 0); /* because 'field' is 1111...1111 */
739 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
743 if (BN_num_bits(a) > 2*521)
744 return BN_nnmod(r, field, a, ctx);
747 tmp = BN_CTX_get(ctx);
751 if (!bn_wexpand(tmp, BN_NIST_521_TOP))
753 nist_cp_bn(tmp->d, a->d, BN_NIST_521_TOP);
755 tmp->top = BN_NIST_521_TOP;
756 tmp->d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK;
759 if (!BN_rshift(r, a, 521))
762 if (!BN_uadd(r, tmp, r))
765 if (BN_ucmp(field, r) <= 0)
767 if (!BN_usub(r, r, field)) goto err;