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).
62 #define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
63 #define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
64 #define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
65 #define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
66 #define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
68 /* pre-computed tables are "carry-less" values of modulus*(i+1) */
70 static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
71 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFFULL},
72 {0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFFULL},
73 {0xFFFFFFFFFFFFFFFDULL,0xFFFFFFFFFFFFFFFCULL,0xFFFFFFFFFFFFFFFFULL}
75 static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
76 {0x0000000000000001ULL,0xFFFFFFFF00000000ULL,
77 0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL},
78 {0x0000000000000002ULL,0xFFFFFFFE00000000ULL,
79 0xFFFFFFFFFFFFFFFFULL,0x00000001FFFFFFFFULL} /* this one is "carry-full" */
81 static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
82 {0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL,
83 0x0000000000000000ULL,0xFFFFFFFF00000001ULL},
84 {0xFFFFFFFFFFFFFFFEULL,0x00000001FFFFFFFFULL,
85 0x0000000000000000ULL,0xFFFFFFFE00000002ULL},
86 {0xFFFFFFFFFFFFFFFDULL,0x00000002FFFFFFFFULL,
87 0x0000000000000000ULL,0xFFFFFFFD00000003ULL},
88 {0xFFFFFFFFFFFFFFFCULL,0x00000003FFFFFFFFULL,
89 0x0000000000000000ULL,0xFFFFFFFC00000004ULL},
90 {0xFFFFFFFFFFFFFFFBULL,0x00000004FFFFFFFFULL,
91 0x0000000000000000ULL,0xFFFFFFFB00000005ULL},
93 static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
94 {0x00000000FFFFFFFFULL,0xFFFFFFFF00000000ULL,0xFFFFFFFFFFFFFFFEULL,
95 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
96 {0x00000001FFFFFFFEULL,0xFFFFFFFE00000000ULL,0xFFFFFFFFFFFFFFFDULL,
97 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
98 {0x00000002FFFFFFFDULL,0xFFFFFFFD00000000ULL,0xFFFFFFFFFFFFFFFCULL,
99 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
100 {0x00000003FFFFFFFCULL,0xFFFFFFFC00000000ULL,0xFFFFFFFFFFFFFFFBULL,
101 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
102 {0x00000004FFFFFFFBULL,0xFFFFFFFB00000000ULL,0xFFFFFFFFFFFFFFFAULL,
103 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL},
105 static const BN_ULONG _nist_p_521[] =
106 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
107 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
108 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
109 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
110 0x00000000000001FFULL};
112 static const BN_ULONG _nist_p_192[][BN_NIST_192_TOP] = {
113 {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
114 {0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
115 {0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFC,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}
117 static const BN_ULONG _nist_p_224[][BN_NIST_224_TOP] = {
118 {0x00000001,0x00000000,0x00000000,0xFFFFFFFF,
119 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
120 {0x00000002,0x00000000,0x00000000,0xFFFFFFFE,
121 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF}
123 static const BN_ULONG _nist_p_256[][BN_NIST_256_TOP] = {
124 {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0x00000000,
125 0x00000000,0x00000000,0x00000001,0xFFFFFFFF},
126 {0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0x00000001,
127 0x00000000,0x00000000,0x00000002,0xFFFFFFFE},
128 {0xFFFFFFFD,0xFFFFFFFF,0xFFFFFFFF,0x00000002,
129 0x00000000,0x00000000,0x00000003,0xFFFFFFFD},
130 {0xFFFFFFFC,0xFFFFFFFF,0xFFFFFFFF,0x00000003,
131 0x00000000,0x00000000,0x00000004,0xFFFFFFFC},
132 {0xFFFFFFFB,0xFFFFFFFF,0xFFFFFFFF,0x00000004,
133 0x00000000,0x00000000,0x00000005,0xFFFFFFFB},
135 static const BN_ULONG _nist_p_384[][BN_NIST_384_TOP] = {
136 {0xFFFFFFFF,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,
137 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
138 {0xFFFFFFFE,0x00000001,0x00000000,0xFFFFFFFE,0xFFFFFFFD,0xFFFFFFFF,
139 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
140 {0xFFFFFFFD,0x00000002,0x00000000,0xFFFFFFFD,0xFFFFFFFC,0xFFFFFFFF,
141 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
142 {0xFFFFFFFC,0x00000003,0x00000000,0xFFFFFFFC,0xFFFFFFFB,0xFFFFFFFF,
143 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
144 {0xFFFFFFFB,0x00000004,0x00000000,0xFFFFFFFB,0xFFFFFFFA,0xFFFFFFFF,
145 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
147 static const BN_ULONG _nist_p_521[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
148 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
149 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
150 0xFFFFFFFF,0x000001FF};
152 #error "unsupported BN_BITS2"
155 const BIGNUM *BN_get0_nist_prime_192(void)
157 static BIGNUM const_nist_192 = { (BN_ULONG *)_nist_p_192[0],
158 BN_NIST_192_TOP, BN_NIST_192_TOP, 0, BN_FLG_STATIC_DATA };
159 return &const_nist_192;
162 const BIGNUM *BN_get0_nist_prime_224(void)
164 static BIGNUM const_nist_224 = { (BN_ULONG *)_nist_p_224[0],
165 BN_NIST_224_TOP, BN_NIST_224_TOP, 0, BN_FLG_STATIC_DATA };
166 return &const_nist_224;
169 const BIGNUM *BN_get0_nist_prime_256(void)
171 static BIGNUM const_nist_256 = { (BN_ULONG *)_nist_p_256[0],
172 BN_NIST_256_TOP, BN_NIST_256_TOP, 0, BN_FLG_STATIC_DATA };
173 return &const_nist_256;
176 const BIGNUM *BN_get0_nist_prime_384(void)
178 static BIGNUM const_nist_384 = { (BN_ULONG *)_nist_p_384[0],
179 BN_NIST_384_TOP, BN_NIST_384_TOP, 0, BN_FLG_STATIC_DATA };
180 return &const_nist_384;
183 const BIGNUM *BN_get0_nist_prime_521(void)
185 static BIGNUM const_nist_521 = { (BN_ULONG *)_nist_p_521,
186 BN_NIST_521_TOP, BN_NIST_521_TOP, 0, BN_FLG_STATIC_DATA };
187 return &const_nist_521;
190 #define BN_NIST_ADD_ONE(a) while (!(*(a)=(*(a)+1)&BN_MASK2)) ++(a);
192 static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max)
195 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
196 for (i = (top); i != 0; i--)
198 for (i = (max) - (top); i != 0; i--)
199 *_tmp1++ = (BN_ULONG) 0;
202 static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
205 BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
206 for (i = (top); i != 0; i--)
211 #define bn_cp_64(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
212 #define bn_64_set_0(to, n) (to)[n] = (BN_ULONG)0;
214 * two following macros are implemented under assumption that they
215 * are called in a sequence with *ascending* n, i.e. as they are...
217 #define bn_cp_32_naked(to, n, from, m) (((n)&1)?(to[(n)/2]|=((m)&1)?(from[(m)/2]&BN_MASK2h):(from[(m)/2]<<32))\
218 :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
219 #define bn_32_set_0(to, n) (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
220 #define bn_cp_32(to,n,from,m) ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
222 #define bn_cp_64(to, n, from, m) \
224 bn_cp_32(to, (n)*2, from, (m)*2); \
225 bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
227 #define bn_64_set_0(to, n) \
229 bn_32_set_0(to, (n)*2); \
230 bn_32_set_0(to, (n)*2+1); \
233 #define bn_cp_32(to, n, from, m) (to)[n] = (m>=0)?((from)[m]):0;
234 #define bn_32_set_0(to, n) (to)[n] = (BN_ULONG)0;
236 #endif /* BN_BITS2 != 64 */
239 #define nist_set_192(to, from, a1, a2, a3) \
241 bn_cp_64(to, 0, from, (a3) - 3) \
242 bn_cp_64(to, 1, from, (a2) - 3) \
243 bn_cp_64(to, 2, from, (a1) - 3) \
246 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
251 register BN_ULONG *r_d, *a_d = a->d;
252 BN_ULONG t_d[BN_NIST_192_TOP],
253 buf[BN_NIST_192_TOP],
254 c_d[BN_NIST_192_TOP],
258 i = BN_ucmp(field, a);
265 return (r == a) ? 1 : (BN_copy(r ,a) != NULL);
267 if (top == BN_NIST_192_TOP)
268 return BN_usub(r, a, field);
272 if (!bn_wexpand(r, BN_NIST_192_TOP))
275 nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
280 nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
282 nist_set_192(t_d, buf, 0, 3, 3);
283 carry = bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
284 nist_set_192(t_d, buf, 4, 4, 0);
285 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
286 nist_set_192(t_d, buf, 5, 5, 5)
287 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
290 carry = bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP);
295 * we need 'if (carry==0 || result>=modulus) result-=modulus;'
296 * as comparison implies subtraction, we can write
297 * 'tmp=result-modulus; if (!carry || !borrow) result=tmp;'
298 * this is what happens below, but without explicit if:-) a.
300 mask = 0-(size_t)bn_sub_words(c_d,r_d,_nist_p_192[0],BN_NIST_192_TOP);
301 mask &= 0-(size_t)carry;
302 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
303 nist_cp_bn(r_d, res, BN_NIST_192_TOP);
304 r->top = BN_NIST_192_TOP;
310 typedef BN_ULONG (*bn_addsub_f)(BN_ULONG *,const BN_ULONG *,const BN_ULONG *,int);
312 #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
314 bn_cp_32(to, 0, from, (a7) - 7) \
315 bn_cp_32(to, 1, from, (a6) - 7) \
316 bn_cp_32(to, 2, from, (a5) - 7) \
317 bn_cp_32(to, 3, from, (a4) - 7) \
318 bn_cp_32(to, 4, from, (a3) - 7) \
319 bn_cp_32(to, 5, from, (a2) - 7) \
320 bn_cp_32(to, 6, from, (a1) - 7) \
323 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
328 BN_ULONG *r_d, *a_d = a->d;
329 BN_ULONG t_d[BN_NIST_224_TOP],
330 buf[BN_NIST_224_TOP],
331 c_d[BN_NIST_224_TOP],
334 union { bn_addsub_f f; size_t p; } u;
336 i = BN_ucmp(field, a);
343 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
345 if (top == BN_NIST_224_TOP)
346 return BN_usub(r, a, field);
350 if (!bn_wexpand(r, BN_NIST_224_TOP))
353 nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
359 /* copy upper 256 bits of 448 bit number ... */
360 nist_cp_bn_0(t_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);
361 /* ... and right shift by 32 to obtain upper 224 bits */
362 nist_set_224(buf, t_d, 14, 13, 12, 11, 10, 9, 8);
363 /* truncate lower part to 224 bits too */
364 r_d[BN_NIST_224_TOP-1] &= BN_MASK2l;
366 nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
368 nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
369 carry = bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
370 nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
371 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
372 nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
373 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
374 nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
375 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
378 carry = (int)(r_d[BN_NIST_224_TOP-1]>>32);
383 carry = bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);
385 carry=(int)(~(r_d[BN_NIST_224_TOP-1]>>32))&1;
390 /* it's a bit more comlicated logic in this case.
391 * if bn_add_words yields no carry, then result
392 * has to be adjusted by unconditionally *adding*
393 * the modulus. but if it does, then result has
394 * to be compared to the modulus and conditionally
395 * adjusted by *subtracting* the latter. */
396 carry = bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);
397 mask = 0-(size_t)carry;
398 u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
403 /* otherwise it's effectively same as in BN_nist_mod_192... */
404 mask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);
405 mask &= 0-(size_t)carry;
406 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
407 nist_cp_bn(r_d, res, BN_NIST_224_TOP);
408 r->top = BN_NIST_224_TOP;
414 #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
416 bn_cp_32(to, 0, from, (a8) - 8) \
417 bn_cp_32(to, 1, from, (a7) - 8) \
418 bn_cp_32(to, 2, from, (a6) - 8) \
419 bn_cp_32(to, 3, from, (a5) - 8) \
420 bn_cp_32(to, 4, from, (a4) - 8) \
421 bn_cp_32(to, 5, from, (a3) - 8) \
422 bn_cp_32(to, 6, from, (a2) - 8) \
423 bn_cp_32(to, 7, from, (a1) - 8) \
426 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
431 register BN_ULONG *a_d = a->d, *r_d;
432 BN_ULONG t_d[BN_NIST_256_TOP],
433 buf[BN_NIST_256_TOP],
434 c_d[BN_NIST_256_TOP],
437 union { bn_addsub_f f; size_t p; } u;
439 i = BN_ucmp(field, a);
446 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
448 if (top == BN_NIST_256_TOP)
449 return BN_usub(r, a, field);
453 if (!bn_wexpand(r, BN_NIST_256_TOP))
456 nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
461 nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP);
464 nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
466 nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0);
467 carry = bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
470 register BN_ULONG *ap,t,c;
473 for (i = BN_NIST_256_TOP; i != 0; --i)
476 *(ap++)=((t<<1)|c)&BN_MASK2;
482 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
484 nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
485 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
487 nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
488 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
490 nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
491 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
493 nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
494 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
496 nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
497 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
499 nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
500 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
502 /* see BN_nist_mod_224 for explanation */
505 carry = bn_sub_words(r_d,r_d,_nist_p_256[carry-1],BN_NIST_256_TOP);
508 carry = bn_add_words(r_d,r_d,_nist_p_256[-carry-1],BN_NIST_256_TOP);
509 mask = 0-(size_t)carry;
510 u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
515 mask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_256[0],BN_NIST_256_TOP);
516 mask &= 0-(size_t)carry;
517 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
518 nist_cp_bn(r_d, res, BN_NIST_256_TOP);
519 r->top = BN_NIST_256_TOP;
525 #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
527 bn_cp_32(to, 0, from, (a12) - 12) \
528 bn_cp_32(to, 1, from, (a11) - 12) \
529 bn_cp_32(to, 2, from, (a10) - 12) \
530 bn_cp_32(to, 3, from, (a9) - 12) \
531 bn_cp_32(to, 4, from, (a8) - 12) \
532 bn_cp_32(to, 5, from, (a7) - 12) \
533 bn_cp_32(to, 6, from, (a6) - 12) \
534 bn_cp_32(to, 7, from, (a5) - 12) \
535 bn_cp_32(to, 8, from, (a4) - 12) \
536 bn_cp_32(to, 9, from, (a3) - 12) \
537 bn_cp_32(to, 10, from, (a2) - 12) \
538 bn_cp_32(to, 11, from, (a1) - 12) \
541 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
546 register BN_ULONG *r_d, *a_d = a->d;
547 BN_ULONG t_d[BN_NIST_384_TOP],
548 buf[BN_NIST_384_TOP],
549 c_d[BN_NIST_384_TOP],
552 union { bn_addsub_f f; size_t p; } u;
554 i = BN_ucmp(field, a);
561 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
563 if (top == BN_NIST_384_TOP)
564 return BN_usub(r, a, field);
568 if (!bn_wexpand(r, BN_NIST_384_TOP))
571 nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
576 nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP);
579 nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4);
582 register BN_ULONG *ap,t,c;
585 for (i = 3; i != 0; --i)
588 *(ap++)=((t<<1)|c)&BN_MASK2;
593 carry = bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
594 t_d, BN_NIST_256_TOP);
596 carry += bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
598 nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21);
599 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
601 nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0);
602 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
604 nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0);
605 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
607 nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20);
608 carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
610 nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
611 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
613 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
614 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
616 nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
617 carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
619 /* see BN_nist_mod_224 for explanation */
622 carry = bn_sub_words(r_d,r_d,_nist_p_384[carry-1],BN_NIST_384_TOP);
625 carry = bn_add_words(r_d,r_d,_nist_p_384[-carry-1],BN_NIST_384_TOP);
626 mask = 0-(size_t)carry;
627 u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
632 mask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_384[0],BN_NIST_384_TOP);
633 mask &= 0-(size_t)carry;
634 res = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));
635 nist_cp_bn(r_d, res, BN_NIST_384_TOP);
636 r->top = BN_NIST_384_TOP;
642 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
646 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
648 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
654 /* check whether a reduction is necessary */
656 if (top < BN_NIST_521_TOP || ( top == BN_NIST_521_TOP &&
657 (!(a->d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))))
658 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
661 tmp = BN_CTX_get(ctx);
665 if (!bn_wexpand(tmp, BN_NIST_521_TOP))
667 nist_cp_bn(tmp->d, a->d, BN_NIST_521_TOP);
669 tmp->top = BN_NIST_521_TOP;
670 tmp->d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK;
673 if (!BN_rshift(r, a, 521))
676 if (!BN_uadd(r, tmp, r))
680 if (top == BN_NIST_521_TOP &&
681 (r_d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))
684 r->d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK;