Move openssl-0.9/ to openssl/.
[dragonfly.git] / crypto / openssl / crypto / bn / bn_nist.c
1 /* crypto/bn/bn_nist.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include "bn_lcl.h"
60 #include "cryptlib.h"
61
62
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
68
69 #if BN_BITS2 == 64
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};
89 #elif BN_BITS2 == 32
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};
103 #endif
104
105
106 static const BIGNUM _bignum_nist_p_192 =
107         {
108         (BN_ULONG *)_nist_p_192,
109         BN_NIST_192_TOP,
110         BN_NIST_192_TOP,
111         0,
112         BN_FLG_STATIC_DATA
113         };
114
115 static const BIGNUM _bignum_nist_p_224 =
116         {
117         (BN_ULONG *)_nist_p_224,
118         BN_NIST_224_TOP,
119         BN_NIST_224_TOP,
120         0,
121         BN_FLG_STATIC_DATA
122         };
123
124 static const BIGNUM _bignum_nist_p_256 =
125         {
126         (BN_ULONG *)_nist_p_256,
127         BN_NIST_256_TOP,
128         BN_NIST_256_TOP,
129         0,
130         BN_FLG_STATIC_DATA
131         };
132
133 static const BIGNUM _bignum_nist_p_384 =
134         {
135         (BN_ULONG *)_nist_p_384,
136         BN_NIST_384_TOP,
137         BN_NIST_384_TOP,
138         0,
139         BN_FLG_STATIC_DATA
140         };
141
142 static const BIGNUM _bignum_nist_p_521 =
143         {
144         (BN_ULONG *)_nist_p_521,
145         BN_NIST_521_TOP,
146         BN_NIST_521_TOP,
147         0,
148         BN_FLG_STATIC_DATA
149         };
150
151
152 const BIGNUM *BN_get0_nist_prime_192(void)
153         {
154         return &_bignum_nist_p_192;
155         }
156
157 const BIGNUM *BN_get0_nist_prime_224(void)
158         {
159         return &_bignum_nist_p_224;
160         }
161
162 const BIGNUM *BN_get0_nist_prime_256(void)
163         {
164         return &_bignum_nist_p_256;
165         }
166
167 const BIGNUM *BN_get0_nist_prime_384(void)
168         {
169         return &_bignum_nist_p_384;
170         }
171
172 const BIGNUM *BN_get0_nist_prime_521(void)
173         {
174         return &_bignum_nist_p_521;
175         }
176
177
178 static void nist_cp_bn_0(BN_ULONG *buf, BN_ULONG *a, int top, int max)
179         {
180         int i;
181         BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
182
183         OPENSSL_assert(top <= max);
184         for (i = (top); i != 0; i--)
185                 *_tmp1++ = *_tmp2++;
186         for (i = (max) - (top); i != 0; i--)
187                 *_tmp1++ = (BN_ULONG) 0;
188         }
189
190 static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)
191         { 
192         int i;
193         BN_ULONG *_tmp1 = (buf), *_tmp2 = (a);
194         for (i = (top); i != 0; i--)
195                 *_tmp1++ = *_tmp2++;
196         }
197
198 #if BN_BITS2 == 64
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;
201 /* TBD */
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;
204 #else
205 #define bn_cp_64(to, n, from, m) \
206         { \
207         bn_cp_32(to, (n)*2, from, (m)*2); \
208         bn_cp_32(to, (n)*2+1, from, (m)*2+1); \
209         }
210 #define bn_64_set_0(to, n) \
211         { \
212         bn_32_set_0(to, (n)*2); \
213         bn_32_set_0(to, (n)*2+1); \
214         }
215 #if BN_BITS2 == 32
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;
218 #endif
219 #endif /* BN_BITS2 != 64 */
220
221
222 #define nist_set_192(to, from, a1, a2, a3) \
223         { \
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)\
227         }
228
229 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
230         BN_CTX *ctx)
231         {
232         int      top = a->top, i;
233         int      carry;
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],
238                 *res;
239         size_t   mask;
240
241         field = &_bignum_nist_p_192; /* just to make sure */
242
243         if (BN_is_negative(a) || a->top > 2*BN_NIST_192_TOP)
244                 return BN_nnmod(r, field, a, ctx);
245
246         i = BN_ucmp(field, a);
247         if (i == 0)
248                 {
249                 BN_zero(r);
250                 return 1;
251                 }
252         else if (i > 0)
253                 return (r == a) ? 1 : (BN_copy(r ,a) != NULL);
254
255         if (r != a)
256                 {
257                 if (!bn_wexpand(r, BN_NIST_192_TOP))
258                         return 0;
259                 r_d = r->d;
260                 nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
261                 }
262         else
263                 r_d = a_d;
264
265         nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
266
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));
272         
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));
278
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));
284
285         nist_cp_bn(r_d, res, BN_NIST_192_TOP);
286         r->top = BN_NIST_192_TOP;
287         bn_correct_top(r);
288
289         if (BN_ucmp(field, r) <= 0)
290                 {
291                 if (!BN_usub(r, r, field)) return 0;
292                 }
293
294         return 1;
295         }
296
297 #define nist_set_224(to, from, a1, a2, a3, a4, a5, a6, a7) \
298         { \
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)\
306         }
307
308 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
309         BN_CTX *ctx)
310         {
311 #if BN_BITS2 == 32
312         int     top = a->top, i;
313         int     carry;
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],
318                 *res;
319         size_t   mask;
320
321         field = &_bignum_nist_p_224; /* just to make sure */
322
323         if (BN_is_negative(a) || a->top > 2*BN_NIST_224_TOP)
324                 return BN_nnmod(r, field, a, ctx);
325
326         i = BN_ucmp(field, a);
327         if (i == 0)
328                 {
329                 BN_zero(r);
330                 return 1;
331                 }
332         else if (i > 0)
333                 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
334
335         if (r != a)
336                 {
337                 if (!bn_wexpand(r, BN_NIST_224_TOP))
338                         return 0;
339                 r_d = r->d;
340                 nist_cp_bn(r_d, a_d, BN_NIST_224_TOP);
341                 }
342         else
343                 r_d = a_d;
344
345         nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
346
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));
352         
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));
358
359         nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
360 #if BRANCH_FREE
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));
365 #else
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);
368 #endif
369         nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
370 #if BRANCH_FREE
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));
375
376         nist_cp_bn(r_d, res, BN_NIST_224_TOP);
377 #else
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);
380 #endif
381         r->top = BN_NIST_224_TOP;
382         bn_correct_top(r);
383
384         if (BN_ucmp(field, r) <= 0)
385                 {
386                 if (!BN_usub(r, r, field)) return 0;
387                 }
388
389         return 1;
390 #else   /* BN_BITS!=32 */
391         return 0;
392 #endif
393         }
394
395 #define nist_set_256(to, from, a1, a2, a3, a4, a5, a6, a7, a8) \
396         { \
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)\
405         }
406
407 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
408         BN_CTX *ctx)
409         {
410 #if BN_BITS2 == 32
411         int     i, top = a->top;
412         int     carry = 0;
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],
417                 *res;
418         size_t   mask;
419
420         field = &_bignum_nist_p_256; /* just to make sure */
421
422         if (BN_is_negative(a) || a->top > 2*BN_NIST_256_TOP)
423                 return BN_nnmod(r, field, a, ctx);
424
425         i = BN_ucmp(field, a);
426         if (i == 0)
427                 {
428                 BN_zero(r);
429                 return 1;
430                 }
431         else if (i > 0)
432                 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
433
434         if (r != a)
435                 {
436                 if (!bn_wexpand(r, BN_NIST_256_TOP))
437                         return 0;
438                 r_d = r->d;
439                 nist_cp_bn(r_d, a_d, BN_NIST_256_TOP);
440                 }
441         else
442                 r_d = a_d;
443
444         nist_cp_bn_0(buf, a_d + BN_NIST_256_TOP, top - BN_NIST_256_TOP, BN_NIST_256_TOP);
445
446         /*S1*/
447         nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
448         /*S2*/
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));
454
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));
459
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));
464
465         /*S3*/
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));
471
472         /*S4*/
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));
478
479         /*D1*/
480         nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
481 #if BRANCH_FREE
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));   
486 #else
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);
489 #endif
490         /*D2*/
491         nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
492 #if BRANCH_FREE
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));   
497 #else
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);
500 #endif
501         /*D3*/
502         nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
503 #if BRANCH_FREE
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));   
508 #else
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);
511 #endif
512         /*D4*/
513         nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
514 #if BRANCH_FREE
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));   
519
520         nist_cp_bn(r_d, res, BN_NIST_384_TOP);
521 #else
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);
524 #endif
525         r->top = BN_NIST_256_TOP;
526         bn_correct_top(r);
527
528         if (BN_ucmp(field, r) <= 0)
529                 {
530                 if (!BN_usub(r, r, field)) return 0;
531                 }
532
533         return 1;
534 #else   /* BN_BITS!=32 */
535         return 0;
536 #endif
537         }
538
539 #define nist_set_384(to,from,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) \
540         { \
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)\
553         }
554
555 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
556         BN_CTX *ctx)
557         {
558 #if BN_BITS2 == 32
559         int     i, top = a->top;
560         int     carry = 0;
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],
565                 *res;
566         size_t   mask;
567
568         field = &_bignum_nist_p_384; /* just to make sure */
569
570         if (BN_is_negative(a) || a->top > 2*BN_NIST_384_TOP)
571                 return BN_nnmod(r, field, a, ctx);
572
573         i = BN_ucmp(field, a);
574         if (i == 0)
575                 {
576                 BN_zero(r);
577                 return 1;
578                 }
579         else if (i > 0)
580                 return (r == a)? 1 : (BN_copy(r ,a) != NULL);
581
582         if (r != a)
583                 {
584                 if (!bn_wexpand(r, BN_NIST_384_TOP))
585                         return 0;
586                 r_d = r->d;
587                 nist_cp_bn(r_d, a_d, BN_NIST_384_TOP);
588                 }
589         else
590                 r_d = a_d;
591
592         nist_cp_bn_0(buf, a_d + BN_NIST_384_TOP, top - BN_NIST_384_TOP, BN_NIST_384_TOP);
593
594         /*S1*/
595         nist_set_256(t_d, buf, 0, 0, 0, 0, 0, 23-4, 22-4, 21-4);
596                 /* left shift */
597                 {
598                 register BN_ULONG *ap,t,c;
599                 ap = t_d;
600                 c=0;
601                 for (i = 3; i != 0; --i)
602                         {
603                         t= *ap;
604                         *(ap++)=((t<<1)|c)&BN_MASK2;
605                         c=(t & BN_TBIT)?1:0;
606                         }
607                 *ap=c;
608                 }
609         carry = bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2), 
610                 t_d, BN_NIST_256_TOP);
611         /*
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.
619          */
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));
623
624         /*S2 */
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));
629
630         /*S3*/
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));
636
637         /*S4*/
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));
643
644         /*S5*/
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));
650
651         /*S6*/
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));
657
658         /*D1*/
659         nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
660 #if BRANCH_FREE
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));
665 #else
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);
668 #endif
669         /*D2*/
670         nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
671 #if BRANCH_FREE
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));
676 #else
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);
679 #endif
680         /*D3*/
681         nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
682 #if BRANCH_FREE
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));
687
688         nist_cp_bn(r_d, res, BN_NIST_384_TOP);
689 #else
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);
692 #endif
693         r->top = BN_NIST_384_TOP;
694         bn_correct_top(r);
695
696         if (BN_ucmp(field, r) <= 0)
697                 {
698                 if (!BN_usub(r, r, field)) return 0;
699                 }
700
701         return 1;
702 #else   /* BN_BITS!=32 */
703         return 0;
704 #endif
705         }
706
707 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
708         BN_CTX *ctx)
709         {
710 #if BN_BITS2 == 64
711 #define BN_NIST_521_TOP_MASK    (BN_ULONG)0x1FF
712 #elif BN_BITS2 == 32
713 #define BN_NIST_521_TOP_MASK    (BN_ULONG)0x1FF
714 #endif
715         int     top, ret = 0;
716         BIGNUM  *tmp;
717
718         field = &_bignum_nist_p_521; /* just to make sure */
719
720         if (BN_is_negative(a))
721                 return BN_nnmod(r, field, a, ctx);
722
723         /* check whether a reduction is necessary */
724         top = a->top;
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)))))
727                 {
728                 int i = BN_ucmp(field, a);
729                 if (i == 0)
730                         {
731                         BN_zero(r);
732                         return 1;
733                         }
734                 else
735                         {
736 #ifdef BN_DEBUG
737                         OPENSSL_assert(i > 0); /* because 'field' is 1111...1111 */
738 #endif
739                         return (r == a)? 1 : (BN_copy(r ,a) != NULL);
740                         }
741                 }
742
743         if (BN_num_bits(a) > 2*521)
744                 return BN_nnmod(r, field, a, ctx);
745
746         BN_CTX_start(ctx);
747         tmp = BN_CTX_get(ctx);
748         if (!tmp)
749                 goto err;
750
751         if (!bn_wexpand(tmp, BN_NIST_521_TOP))
752                 goto err;
753         nist_cp_bn(tmp->d, a->d, BN_NIST_521_TOP);
754
755         tmp->top = BN_NIST_521_TOP;
756         tmp->d[BN_NIST_521_TOP-1]  &= BN_NIST_521_TOP_MASK;
757         bn_correct_top(tmp);
758
759         if (!BN_rshift(r, a, 521))
760                 goto err;
761
762         if (!BN_uadd(r, tmp, r))
763                 goto err;
764
765         if (BN_ucmp(field, r) <= 0)
766                 {
767                 if (!BN_usub(r, r, field)) goto err;
768                 }
769
770         ret = 1;
771 err:
772         BN_CTX_end(ctx);
773
774         bn_check_top(r);
775         return ret;
776         }