1 /* crypto/bn/bn_nist.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
59 #define BN_NIST_192_TOP (192+BN_BITS2-1)/BN_BITS2
60 #define BN_NIST_224_TOP (224+BN_BITS2-1)/BN_BITS2
61 #define BN_NIST_256_TOP (256+BN_BITS2-1)/BN_BITS2
62 #define BN_NIST_384_TOP (384+BN_BITS2-1)/BN_BITS2
63 #define BN_NIST_521_TOP (521+BN_BITS2-1)/BN_BITS2
66 const static BN_ULONG _nist_p_192[] =
67 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFEULL,
68 0xFFFFFFFFFFFFFFFFULL};
69 const static BN_ULONG _nist_p_224[] =
70 {0x0000000000000001ULL,0xFFFFFFFF00000000ULL,
71 0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL};
72 const static BN_ULONG _nist_p_256[] =
73 {0xFFFFFFFFFFFFFFFFULL,0x00000000FFFFFFFFULL,
74 0x0000000000000000ULL,0xFFFFFFFF00000001ULL};
75 const static BN_ULONG _nist_p_384[] =
76 {0x00000000FFFFFFFFULL,0xFFFFFFFF00000000ULL,
77 0xFFFFFFFFFFFFFFFEULL,0xFFFFFFFFFFFFFFFFULL,
78 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL};
79 const static BN_ULONG _nist_p_521[] =
80 {0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
81 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
82 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
83 0xFFFFFFFFFFFFFFFFULL,0xFFFFFFFFFFFFFFFFULL,
84 0x00000000000001FFULL};
86 const static BN_ULONG _nist_p_192[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFE,
87 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
88 const static BN_ULONG _nist_p_224[] = {0x00000001,0x00000000,0x00000000,
89 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
90 const static BN_ULONG _nist_p_256[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
91 0x00000000,0x00000000,0x00000000,0x00000001,0xFFFFFFFF};
92 const static BN_ULONG _nist_p_384[] = {0xFFFFFFFF,0x00000000,0x00000000,
93 0xFFFFFFFF,0xFFFFFFFE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
94 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
95 const static BN_ULONG _nist_p_521[] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
96 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
97 0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
98 0xFFFFFFFF,0x000001FF};
100 const static BN_ULONG _nist_p_192[] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFE,
101 0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF};
102 const static BN_ULONG _nist_p_224[] = {0x0001,0x0000,0x0000,0x0000,0x0000,
103 0x0000,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF};
104 const static BN_ULONG _nist_p_256[] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
105 0xFFFF,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0001,0x0000,0xFFFF,
107 const static BN_ULONG _nist_p_384[] = {0xFFFF,0xFFFF,0x0000,0x0000,0x0000,
108 0x0000,0xFFFF,0xFFFF,0xFFFE,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
109 0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF};
110 const static BN_ULONG _nist_p_521[] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
111 0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
112 0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,
113 0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x01FF};
115 const static BN_ULONG _nist_p_192[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
116 0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
118 const static BN_ULONG _nist_p_224[] = {0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
119 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
120 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
121 const static BN_ULONG _nist_p_256[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
122 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
123 0x00,0x00,0x01,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF};
124 const static BN_ULONG _nist_p_384[] = {0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
125 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
126 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
127 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
128 const static BN_ULONG _nist_p_521[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
129 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
130 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
131 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
132 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
136 const BIGNUM *BN_get0_nist_prime_192(void)
138 static BIGNUM const_nist_192 = { (BN_ULONG *)_nist_p_192,
139 BN_NIST_192_TOP, BN_NIST_192_TOP, 0, BN_FLG_STATIC_DATA };
140 return &const_nist_192;
143 const BIGNUM *BN_get0_nist_prime_224(void)
145 static BIGNUM const_nist_224 = { (BN_ULONG *)_nist_p_224,
146 BN_NIST_224_TOP, BN_NIST_224_TOP, 0, BN_FLG_STATIC_DATA };
147 return &const_nist_224;
150 const BIGNUM *BN_get0_nist_prime_256(void)
152 static BIGNUM const_nist_256 = { (BN_ULONG *)_nist_p_256,
153 BN_NIST_256_TOP, BN_NIST_256_TOP, 0, BN_FLG_STATIC_DATA };
154 return &const_nist_256;
157 const BIGNUM *BN_get0_nist_prime_384(void)
159 static BIGNUM const_nist_384 = { (BN_ULONG *)_nist_p_384,
160 BN_NIST_384_TOP, BN_NIST_384_TOP, 0, BN_FLG_STATIC_DATA };
161 return &const_nist_384;
164 const BIGNUM *BN_get0_nist_prime_521(void)
166 static BIGNUM const_nist_521 = { (BN_ULONG *)_nist_p_521,
167 BN_NIST_521_TOP, BN_NIST_521_TOP, 0, BN_FLG_STATIC_DATA };
168 return &const_nist_521;
171 /* some misc internal functions */
172 static BN_ULONG _256_data[BN_NIST_256_TOP*6];
173 static int _is_set_256_data = 0;
174 static void _init_256_data(void);
176 static BN_ULONG _384_data[BN_NIST_384_TOP*8];
177 static int _is_set_384_data = 0;
178 static void _init_384_data(void);
180 #define BN_NIST_ADD_ONE(a) while (!(++(*(a)))) ++(a);
181 #define __buf_0 (BN_ULONG)0
182 #define __buf_0_1 (BN_ULONG)0
183 #define __buf_0_2 (BN_ULONG)0
185 #define BN_64_BIT_BUF(n) BN_ULONG __buf_##n = (BN_ULONG)0;
186 #define BN_CP_64_TO_BUF(n) __buf_##n = (a)[(n)];
187 #define BN_CP_64_FROM_BUF(a,n) *(a)++ = __buf_##n;
188 #define BN_CASE_64_BIT(n,a) case (n): __buf_##n = (a)[(n)];
189 #if UINT_MAX == 4294967295UL
190 #define nist32 unsigned int
191 #define BN_32_BIT_BUF(n) nist32 __buf_##n = (nist32)0;
192 #define BN_CP_32_TO_BUF(n) __buf_##n = ((nist32 *)(a))[(n)];
193 #define BN_CP_32_FROM_BUF(a,n) *((nist32)(a))++ = __buf_##n;
194 #define BN_CASE_32_BIT(n,a) case (n): __buf_##n = ((nist32)(a))[(n)];
195 #elif ULONG_MAX == 4294967295UL
196 #define nist32 unsigned long
197 #define BN_32_BIT_BUF(n) nist32 __buf_##n = (nist32)0;
198 #define BN_CP_32_TO_BUF(n) __buf_##n = ((nist32 *)(a))[(n)];
199 #define BN_CP_32_FROM_BUF(a,n) *((nist32)(a))++ = __buf_##n;
200 #define BN_CASE_32_BIT(n,a) case (n): __buf_##n = ((nist32)(a))[(n)];
202 #define NO_32_BIT_TYPE
205 #define BN_64_BIT_BUF(n) BN_ULONG __buf_##n##_1 = (BN_ULONG)0;\
206 BN_ULONG __buf_##n##_2 = (BN_ULONG)0;
207 #define BN_CP_64_TO_BUF(n) __buf_##n##_2 = (a)[2*(n)+1];\
208 __buf_##n##_1 = (a)[2*(n)];
209 #define BN_CP_64_FROM_BUF(a,n) *(a)++ = __buf_##n##_1;\
210 *(a)++ = __buf_##n##_2;
211 #define BN_CASE_64_BIT(n,a) case 2*(n)+1: __buf_##n##_2 = (a)[2*(n)+1];\
212 case 2*(n): __buf_##n##_1 = (a)[2*(n)];
214 #define BN_32_BIT_BUF(n) BN_ULONG __buf_##n = (BN_ULONG)0;
215 #define BN_CP_32_TO_BUF(n) __buf_##n = (a)[(n)];
216 #define BN_CP_32_FROM_BUF(a,n) *(a)++ = __buf_##n;
217 #define BN_CASE_32_BIT(n,a) case (n): __buf_##n = (a)[(n)];
219 #define __buf_0_3 (BN_ULONG)0
220 #define __buf_0_4 (BN_ULONG)0
221 #define BN_64_BIT_BUF(n) BN_ULONG __buf_##n##_1 = (BN_ULONG)0;\
222 BN_ULONG __buf_##n##_2 = (BN_ULONG)0;\
223 BN_ULONG __buf_##n##_3 = (BN_ULONG)0;\
224 BN_ULONG __buf_##n##_4 = (BN_ULONG)0;
225 #define BN_CP_64_TO_BUF(n) __buf_##n##_4 = (a)[4*(n)+3];\
226 __buf_##n##_3 = (a)[4*(n)+2];\
227 __buf_##n##_2 = (a)[4*(n)+1];\
228 __buf_##n##_1 = (a)[4*(n)];
229 #define BN_CP_64_FROM_BUF(a,n) *(a)++ = __buf_##n##_1;\
230 *(a)++ = __buf_##n##_2;\
231 *(a)++ = __buf_##n##_3;\
232 *(a)++ = __buf_##n##_4;
233 #define BN_CASE_64_BIT(n,a) case 4*(n)+3: __buf_##n##_4 = (a)[4*(n)+3];\
234 case 4*(n)+2: __buf_##n##_3 = (a)[4*(n)+2];\
235 case 4*(n)+1: __buf_##n##_2 = (a)[4*(n)+1];\
236 case 4*(n): __buf_##n##_1 = (a)[4*(n)];
237 #define BN_32_BIT_BUF(n) BN_ULONG __buf_##n##_1 = (BN_ULONG)0;\
238 BN_ULONG __buf_##n##_2 = (BN_ULONG)0;
239 #define BN_CP_32_TO_BUF(n) __buf_##n##_1 = (a)[2*(n)];\
240 __buf_##n##_2 = (a)[2*(n)+1];
241 #define BN_CP_32_FROM_BUF(a,n) *(a)++ = __buf_##n##_1;\
242 *(a)++ = __buf_##n##_2;
243 #define BN_CASE_32_BIT(n,a) case 2*(n)+1: __buf_##n##_2 = (a)[2*(n)+1];\
244 case 2*(n): __buf_##n##_1 = (a)[2*(n)];
246 #define __buf_0_3 (BN_ULONG)0
247 #define __buf_0_4 (BN_ULONG)0
248 #define __buf_0_5 (BN_ULONG)0
249 #define __buf_0_6 (BN_ULONG)0
250 #define __buf_0_7 (BN_ULONG)0
251 #define __buf_0_8 (BN_ULONG)0
252 #define BN_64_BIT_BUF(n) BN_ULONG __buf_##n##_1 = (BN_ULONG)0;\
253 BN_ULONG __buf_##n##_2 = (BN_ULONG)0;\
254 BN_ULONG __buf_##n##_3 = (BN_ULONG)0;\
255 BN_ULONG __buf_##n##_4 = (BN_ULONG)0;\
256 BN_ULONG __buf_##n##_5 = (BN_ULONG)0;\
257 BN_ULONG __buf_##n##_6 = (BN_ULONG)0;\
258 BN_ULONG __buf_##n##_7 = (BN_ULONG)0;\
259 BN_ULONG __buf_##n##_8 = (BN_ULONG)0;
260 #define BN_CP_64_TO_BUF(n) __buf_##n##_8 = (a)[8*(n)+7];\
261 __buf_##n##_7 = (a)[8*(n)+6];\
262 __buf_##n##_6 = (a)[8*(n)+5];\
263 __buf_##n##_5 = (a)[8*(n)+4];\
264 __buf_##n##_4 = (a)[8*(n)+3];\
265 __buf_##n##_3 = (a)[8*(n)+2];\
266 __buf_##n##_2 = (a)[8*(n)+1];\
267 __buf_##n##_1 = (a)[8*(n)];
268 #define BN_CP_64_FROM_BUF(a,n) *(a)++ = __buf_##n##_1;\
269 *(a)++ = __buf_##n##_2;\
270 *(a)++ = __buf_##n##_3;\
271 *(a)++ = __buf_##n##_4;\
272 *(a)++ = __buf_##n##_5;\
273 *(a)++ = __buf_##n##_6;\
274 *(a)++ = __buf_##n##_7;\
275 *(a)++ = __buf_##n##_8;
276 #define BN_CASE_64_BIT(n,a) case 8*(n)+7: __buf_##n##_8 = (a)[8*(n)+7];\
277 case 8*(n)+6: __buf_##n##_7 = (a)[8*(n)+6];\
278 case 8*(n)+5: __buf_##n##_6 = (a)[8*(n)+5];\
279 case 8*(n)+4: __buf_##n##_5 = (a)[8*(n)+4];\
280 case 8*(n)+3: __buf_##n##_4 = (a)[8*(n)+3];\
281 case 8*(n)+2: __buf_##n##_3 = (a)[8*(n)+2];\
282 case 8*(n)+1: __buf_##n##_2 = (a)[8*(n)+1];\
283 case 8*(n): __buf_##n##_1 = (a)[8*(n)];
284 #define BN_32_BIT_BUF(n) BN_ULONG __buf_##n##_1 = (BN_ULONG)0;\
285 BN_ULONG __buf_##n##_2 = (BN_ULONG)0;\
286 BN_ULONG __buf_##n##_3 = (BN_ULONG)0;\
287 BN_ULONG __buf_##n##_4 = (BN_ULONG)0;
288 #define BN_CP_32_TO_BUF(n) __buf_##n##_1 = (a)[4*(n)];\
289 __buf_##n##_2 = (a)[4*(n)+1];\
290 __buf_##n##_3 = (a)[4*(n)+2];\
291 __buf_##n##_4 = (a)[4*(n)+3];
292 #define BN_CP_32_FROM_BUF(a,n) *(a)++ = __buf_##n##_1;\
293 *(a)++ = __buf_##n##_2;\
294 *(a)++ = __buf_##n##_3;\
295 *(a)++ = __buf_##n##_4;
296 #define BN_CASE_32_BIT(n,a) case 4*(n)+3: __buf_##n##_4 = (a)[4*(n)+3];\
297 case 4*(n)+2: __buf_##n##_3 = (a)[4*(n)+2];\
298 case 4*(n)+1: __buf_##n##_2 = (a)[4*(n)+1];\
299 case 4*(n): __buf_##n##_1 = (a)[4*(n)];
303 #define BN_192_SET(d,a1,a2,a3) \
305 register BN_ULONG *td = (d);\
306 BN_CP_64_FROM_BUF(td,a3); BN_CP_64_FROM_BUF(td,a2);\
307 BN_CP_64_FROM_BUF(td,a1);\
310 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
315 register BN_ULONG *r_d, *a_d;
316 BN_ULONG t_d[BN_NIST_192_TOP];
317 BN_64_BIT_BUF(3) BN_64_BIT_BUF(4)
320 top = BN_ucmp(field, a);
324 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
327 if (!BN_ncopy(r, a, BN_NIST_192_TOP))
336 BN_CASE_64_BIT(5, a_d)
337 BN_CASE_64_BIT(4, a_d)
338 BN_CASE_64_BIT(3, a_d)
340 default: /* a->top == field->top */
341 return BN_usub(r, a, field);
344 BN_192_SET(t_d,0,3,3)
345 if (bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP))
348 BN_192_SET(t_d,4,4,0)
349 if (bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP))
352 BN_192_SET(t_d,5,5,5)
353 if (bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP))
358 if (bn_sub_words(r_d, r_d, _nist_p_192, BN_NIST_192_TOP))
361 r->top = BN_NIST_192_TOP;
363 if (BN_ucmp(r, field) >= 0)
365 bn_sub_words(r_d, r_d, _nist_p_192, BN_NIST_192_TOP);
373 #define BN_224_SET(d,a1,a2,a3,a4,a5,a6,a7) \
375 register BN_ULONG *td = (d);\
376 BN_CP_32_FROM_BUF(td,a7); BN_CP_32_FROM_BUF(td,a6);\
377 BN_CP_32_FROM_BUF(td,a5); BN_CP_32_FROM_BUF(td,a4);\
378 BN_CP_32_FROM_BUF(td,a3); BN_CP_32_FROM_BUF(td,a2);\
379 BN_CP_32_FROM_BUF(td,a1);\
382 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
385 #ifndef NO_32_BIT_TYPE
389 BN_ULONG t_d[BN_NIST_224_TOP];
390 BN_32_BIT_BUF(7) BN_32_BIT_BUF(8)
391 BN_32_BIT_BUF(9) BN_32_BIT_BUF(10)
392 BN_32_BIT_BUF(11) BN_32_BIT_BUF(12)
395 tmp_int = BN_ucmp(field, a);
398 else if (tmp_int > 0)
399 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
402 if (!BN_ncopy(r, a, BN_NIST_224_TOP))
412 BN_CASE_32_BIT(13, a_d)
413 BN_CASE_32_BIT(12, a_d)
414 BN_CASE_32_BIT(11, a_d)
415 BN_CASE_32_BIT(10, a_d)
416 BN_CASE_32_BIT(9, a_d)
417 BN_CASE_32_BIT(8, a_d)
418 BN_CASE_32_BIT(7, a_d)
420 default: /* a->top == field->top */
421 return BN_usub(r, a, field);
424 BN_224_SET(t_d,10,9,8,7,0,0,0)
425 if (bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP))
427 BN_224_SET(t_d,0,13,12,11,0,0,0)
428 if (bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP))
430 BN_224_SET(t_d,13,12,11,10,9,8,7)
431 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP))
433 BN_224_SET(t_d,0,0,0,0,13,12,11)
434 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP))
440 if (bn_sub_words(r_d,r_d,_nist_p_224,BN_NIST_224_TOP))
446 if (bn_add_words(r_d,r_d,_nist_p_224,BN_NIST_224_TOP))
450 r->top = BN_NIST_224_TOP;
452 if (BN_ucmp(r, field) >= 0)
454 bn_sub_words(r_d, r_d, _nist_p_224, BN_NIST_224_TOP);
464 static void _init_256_data(void)
467 BN_ULONG *tmp1 = _256_data;
468 const BN_ULONG *tmp2 = tmp1;
470 memcpy(tmp1, _nist_p_256, BN_NIST_256_TOP * sizeof(BN_ULONG));
471 tmp1 += BN_NIST_256_TOP;
475 bn_add_words(tmp1, _nist_p_256, tmp2, BN_NIST_256_TOP);
477 tmp1 += BN_NIST_256_TOP;
479 _is_set_256_data = 1;
482 #define BN_256_SET(d,a1,a2,a3,a4,a5,a6,a7,a8) \
484 register BN_ULONG *td = (d);\
485 BN_CP_32_FROM_BUF(td,a8); BN_CP_32_FROM_BUF(td,a7);\
486 BN_CP_32_FROM_BUF(td,a6); BN_CP_32_FROM_BUF(td,a5);\
487 BN_CP_32_FROM_BUF(td,a4); BN_CP_32_FROM_BUF(td,a3);\
488 BN_CP_32_FROM_BUF(td,a2); BN_CP_32_FROM_BUF(td,a1);\
491 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
494 #ifndef NO_32_BIT_TYPE
497 register BN_ULONG *a_d, *r_d;
498 BN_ULONG t_d[BN_NIST_256_TOP];
499 BN_ULONG t_d2[BN_NIST_256_TOP];
500 BN_32_BIT_BUF(8) BN_32_BIT_BUF(9)
501 BN_32_BIT_BUF(10) BN_32_BIT_BUF(11)
502 BN_32_BIT_BUF(12) BN_32_BIT_BUF(13)
503 BN_32_BIT_BUF(14) BN_32_BIT_BUF(15)
505 if (!_is_set_256_data)
507 CRYPTO_w_lock(CRYPTO_LOCK_BN);
509 if (!_is_set_256_data)
512 CRYPTO_w_unlock(CRYPTO_LOCK_BN);
515 tmp_int = BN_ucmp(field, a);
518 else if (tmp_int > 0)
519 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
522 if (!BN_ncopy(r, a, BN_NIST_256_TOP))
531 BN_CASE_32_BIT(15, a_d)
532 BN_CASE_32_BIT(14, a_d)
533 BN_CASE_32_BIT(13, a_d)
534 BN_CASE_32_BIT(12, a_d)
535 BN_CASE_32_BIT(11, a_d)
536 BN_CASE_32_BIT(10, a_d)
537 BN_CASE_32_BIT(9, a_d)
538 BN_CASE_32_BIT(8, a_d)
540 default: /* a->top == field->top */
541 return BN_usub(r, a, field);
545 BN_256_SET(t_d,15,14,13,12,11,0,0,0)
547 BN_256_SET(t_d2,0,15,14,13,12,0,0,0)
548 if (bn_add_words(t_d, t_d, t_d2, BN_NIST_256_TOP))
552 register BN_ULONG *ap,t,c;
555 for (tmp_int=BN_NIST_256_TOP; tmp_int != 0; --tmp_int)
558 *(ap++)=((t<<1)|c)&BN_MASK2;
565 if (bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP))
568 BN_256_SET(t_d,15,14,0,0,0,10,9,8)
569 if (bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP))
572 BN_256_SET(t_d,8,13,15,14,13,11,10,9)
573 if (bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP))
576 BN_256_SET(t_d,10,8,0,0,0,13,12,11)
577 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
580 BN_256_SET(t_d,11,9,0,0,15,14,13,12)
581 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
584 BN_256_SET(t_d,12,0,10,9,8,15,14,13)
585 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
588 BN_256_SET(t_d,13,0,11,10,9,0,15,14)
589 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP))
595 bn_sub_words(r_d, r_d, _256_data + BN_NIST_256_TOP *
596 --carry, BN_NIST_256_TOP);
600 bn_add_words(r_d, r_d, _256_data + BN_NIST_256_TOP *
601 --carry, BN_NIST_256_TOP);
605 r->top = BN_NIST_256_TOP;
607 if (BN_ucmp(r, field) >= 0)
609 bn_sub_words(r_d, r_d, _nist_p_256, BN_NIST_256_TOP);
619 static void _init_384_data(void)
622 BN_ULONG *tmp1 = _384_data;
623 const BN_ULONG *tmp2 = tmp1;
625 memcpy(tmp1, _nist_p_384, BN_NIST_384_TOP * sizeof(BN_ULONG));
626 tmp1 += BN_NIST_384_TOP;
630 bn_add_words(tmp1, _nist_p_384, tmp2, BN_NIST_384_TOP);
632 tmp1 += BN_NIST_384_TOP;
634 _is_set_384_data = 1;
637 #define BN_384_SET(d,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
639 register BN_ULONG *td = (d);\
640 BN_CP_32_FROM_BUF(td,a12); BN_CP_32_FROM_BUF(td,a11);\
641 BN_CP_32_FROM_BUF(td,a10); BN_CP_32_FROM_BUF(td,a9);\
642 BN_CP_32_FROM_BUF(td,a8); BN_CP_32_FROM_BUF(td,a7);\
643 BN_CP_32_FROM_BUF(td,a6); BN_CP_32_FROM_BUF(td,a5);\
644 BN_CP_32_FROM_BUF(td,a4); BN_CP_32_FROM_BUF(td,a3);\
645 BN_CP_32_FROM_BUF(td,a2); BN_CP_32_FROM_BUF(td,a1);\
648 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
651 #ifndef NO_32_BIT_TYPE
654 register BN_ULONG *r_d, *a_d;
655 BN_ULONG t_d[BN_NIST_384_TOP];
656 BN_32_BIT_BUF(12) BN_32_BIT_BUF(13)
657 BN_32_BIT_BUF(14) BN_32_BIT_BUF(15)
658 BN_32_BIT_BUF(16) BN_32_BIT_BUF(17)
659 BN_32_BIT_BUF(18) BN_32_BIT_BUF(19)
660 BN_32_BIT_BUF(20) BN_32_BIT_BUF(21)
661 BN_32_BIT_BUF(22) BN_32_BIT_BUF(23)
663 if (!_is_set_384_data)
665 CRYPTO_w_lock(CRYPTO_LOCK_BN);
667 if (!_is_set_384_data)
670 CRYPTO_w_unlock(CRYPTO_LOCK_BN);
673 tmp_int = BN_ucmp(field, a);
676 else if (tmp_int > 0)
677 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
680 if (!BN_ncopy(r, a, BN_NIST_384_TOP))
689 BN_CASE_32_BIT(23, a_d)
690 BN_CASE_32_BIT(22, a_d)
691 BN_CASE_32_BIT(21, a_d)
692 BN_CASE_32_BIT(20, a_d)
693 BN_CASE_32_BIT(19, a_d)
694 BN_CASE_32_BIT(18, a_d)
695 BN_CASE_32_BIT(17, a_d)
696 BN_CASE_32_BIT(16, a_d)
697 BN_CASE_32_BIT(15, a_d)
698 BN_CASE_32_BIT(14, a_d)
699 BN_CASE_32_BIT(13, a_d)
700 BN_CASE_32_BIT(12, a_d)
702 default: /* a->top == field->top */
703 return BN_usub(r, a, field);
707 BN_256_SET(t_d,0,0,0,0,0,23,22,21)
710 register BN_ULONG *ap,t,c;
713 for (tmp_int=BN_NIST_256_TOP; tmp_int != 0; --tmp_int)
716 *(ap++)=((t<<1)|c)&BN_MASK2;
720 if (bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
721 t_d, BN_NIST_256_TOP))
724 BN_384_SET(t_d,23,22,21,20,19,18,17,16,15,14,13,12)
725 if (bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP))
728 BN_384_SET(t_d,20,19,18,17,16,15,14,13,12,23,22,21)
729 if (bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP))
732 BN_384_SET(t_d,19,18,17,16,15,14,13,12,20,0,23,0)
733 if (bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP))
736 BN_256_SET(t_d,0,0,0,0,23,22,21,20)
737 if (bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2),
738 t_d, BN_NIST_256_TOP))
741 BN_384_SET(t_d,0,0,0,0,0,0,23,22,21,0,0,20)
742 if (bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP))
745 BN_384_SET(t_d,22,21,20,19,18,17,16,15,14,13,12,23)
746 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
749 BN_384_SET(t_d,0,0,0,0,0,0,0,23,22,21,20,0)
750 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
753 BN_384_SET(t_d,0,0,0,0,0,0,0,23,23,0,0,0)
754 if (bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP))
760 bn_sub_words(r_d, r_d, _384_data + BN_NIST_384_TOP *
761 --carry, BN_NIST_384_TOP);
765 bn_add_words(r_d, r_d, _384_data + BN_NIST_384_TOP *
766 --carry, BN_NIST_384_TOP);
770 r->top = BN_NIST_384_TOP;
772 if (BN_ucmp(r, field) >= 0)
774 bn_sub_words(r_d, r_d, _nist_p_384, BN_NIST_384_TOP);
784 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
788 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
790 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
792 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1FF
794 #define BN_NIST_521_TOP_MASK (BN_ULONG)0x1
800 /* check whether a reduction is necessary */
802 if (top < BN_NIST_521_TOP || ( top == BN_NIST_521_TOP &&
803 (!(a->d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))))
804 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
807 tmp = BN_CTX_get(ctx);
811 if (!BN_ncopy(tmp, a, BN_NIST_521_TOP))
813 if (!BN_rshift(r, a, 521))
816 if (tmp->top == BN_NIST_521_TOP)
817 tmp->d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK;
820 if (!BN_uadd(r, tmp, r))
824 if (top == BN_NIST_521_TOP &&
825 (r_d[BN_NIST_521_TOP-1] & ~(BN_NIST_521_TOP_MASK)))
828 r_d[BN_NIST_521_TOP-1] &= BN_NIST_521_TOP_MASK;