blob: 65e177cabb818abcca232e306acebc0fda79be52 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <openssl/bn.h>
110
111#include <string.h>
112
113#include <openssl/mem.h>
114#include <openssl/thread.h>
115
116#include "internal.h"
117
118
119#if !defined(OPENSSL_NO_ASM) && \
120 (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
121#define OPENSSL_BN_ASM_MONT
122#endif
123
124BN_MONT_CTX *BN_MONT_CTX_new(void) {
125 BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
126
127 if (ret == NULL) {
128 return NULL;
129 }
130
131 BN_MONT_CTX_init(ret);
132 ret->flags = BN_FLG_MALLOCED;
133 return ret;
134}
135
136void BN_MONT_CTX_init(BN_MONT_CTX *mont) {
137 memset(mont, 0, sizeof(BN_MONT_CTX));
138 BN_init(&mont->RR);
139 BN_init(&mont->N);
140 BN_init(&mont->Ni);
141}
142
143void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
144 if (mont == NULL) {
145 return;
146 }
147
148 BN_free(&mont->RR);
149 BN_free(&mont->N);
150 BN_free(&mont->Ni);
151 if (mont->flags & BN_FLG_MALLOCED) {
152 OPENSSL_free(mont);
153 }
154}
155
156BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from) {
157 if (to == from) {
158 return to;
159 }
160
161 if (!BN_copy(&to->RR, &from->RR) ||
162 !BN_copy(&to->N, &from->N) ||
163 !BN_copy(&to->Ni, &from->Ni)) {
164 return NULL;
165 }
166 to->ri = from->ri;
167 to->n0[0] = from->n0[0];
168 to->n0[1] = from->n0[1];
169 return to;
170}
171
172int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
173 int ret = 0;
174 BIGNUM *Ri, *R;
175 BIGNUM tmod;
176 BN_ULONG buf[2];
177
178 BN_CTX_start(ctx);
179 Ri = BN_CTX_get(ctx);
180 if (Ri == NULL) {
181 goto err;
182 }
183 R = &mont->RR; /* grab RR as a temp */
184 if (!BN_copy(&mont->N, mod)) {
185 goto err; /* Set N */
186 }
187 mont->N.neg = 0;
188
189 BN_init(&tmod);
190 tmod.d = buf;
191 tmod.dmax = 2;
192 tmod.neg = 0;
193
194 mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
195
196#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2 <= 32)
197 /* Only certain BN_BITS2<=32 platforms actually make use of
198 * n0[1], and we could use the #else case (with a shorter R
199 * value) for the others. However, currently only the assembler
200 * files do know which is which. */
201
202 BN_zero(R);
203 if (!BN_set_bit(R, 2 * BN_BITS2)) {
204 goto err;
205 }
206
207 tmod.top = 0;
208 if ((buf[0] = mod->d[0])) {
209 tmod.top = 1;
210 }
211 if ((buf[1] = mod->top > 1 ? mod->d[1] : 0)) {
212 tmod.top = 2;
213 }
214
215 if (BN_mod_inverse(Ri, R, &tmod, ctx) == NULL) {
216 goto err;
217 }
218 if (!BN_lshift(Ri, Ri, 2 * BN_BITS2)) {
219 goto err; /* R*Ri */
220 }
221 if (!BN_is_zero(Ri)) {
222 if (!BN_sub_word(Ri, 1)) {
223 goto err;
224 }
225 } else {
226 /* if N mod word size == 1 */
227 if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL) {
228 goto err;
229 }
230 /* Ri-- (mod double word size) */
231 Ri->neg = 0;
232 Ri->d[0] = BN_MASK2;
233 Ri->d[1] = BN_MASK2;
234 Ri->top = 2;
235 }
236
237 if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) {
238 goto err;
239 }
240 /* Ni = (R*Ri-1)/N,
241 * keep only couple of least significant words: */
242 mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
243 mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
244#else
245 BN_zero(R);
246 if (!BN_set_bit(R, BN_BITS2)) {
247 goto err; /* R */
248 }
249
250 buf[0] = mod->d[0]; /* tmod = N mod word size */
251 buf[1] = 0;
252 tmod.top = buf[0] != 0 ? 1 : 0;
253 /* Ri = R^-1 mod N*/
254 if (BN_mod_inverse(Ri, R, &tmod, ctx) == NULL) {
255 goto err;
256 }
257 if (!BN_lshift(Ri, Ri, BN_BITS2)) {
258 goto err; /* R*Ri */
259 }
260 if (!BN_is_zero(Ri)) {
261 if (!BN_sub_word(Ri, 1)) {
262 goto err;
263 }
264 } else {
265 /* if N mod word size == 1 */
266 if (!BN_set_word(Ri, BN_MASK2)) {
267 goto err; /* Ri-- (mod word size) */
268 }
269 }
270 if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) {
271 goto err;
272 }
273 /* Ni = (R*Ri-1)/N,
274 * keep only least significant word: */
275 mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
276 mont->n0[1] = 0;
277#endif
278
279 /* setup RR for conversions */
280 BN_zero(&(mont->RR));
281 if (!BN_set_bit(&(mont->RR), mont->ri * 2)) {
282 goto err;
283 }
284 if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx)) {
285 goto err;
286 }
287
288 ret = 1;
289
290err:
291 BN_CTX_end(ctx);
292 return ret;
293}
294
295BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
296 const BIGNUM *mod, BN_CTX *ctx) {
297 BN_MONT_CTX *ret;
298
299 CRYPTO_r_lock(lock);
300 ret = *pmont;
301 CRYPTO_r_unlock(lock);
302 if (ret) {
303 return ret;
304 }
305
306 /* We don't want to serialise globally while doing our lazy-init math in
307 * BN_MONT_CTX_set. That punishes threads that are doing independent
308 * things. Instead, punish the case where more than one thread tries to
309 * lazy-init the same 'pmont', by having each do the lazy-init math work
310 * independently and only use the one from the thread that wins the race
311 * (the losers throw away the work they've done). */
312 ret = BN_MONT_CTX_new();
313 if (!ret) {
314 return NULL;
315 }
316 if (!BN_MONT_CTX_set(ret, mod, ctx)) {
317 BN_MONT_CTX_free(ret);
318 return NULL;
319 }
320
321 /* The locked compare-and-set, after the local work is done. */
322 CRYPTO_w_lock(lock);
323 if (*pmont) {
324 BN_MONT_CTX_free(ret);
325 ret = *pmont;
326 } else {
327 *pmont = ret;
328 }
329
330 CRYPTO_w_unlock(lock);
331
332 return ret;
333}
334
335int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
336 BN_CTX *ctx) {
337 return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
338}
339
340#if 0
341static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
342 const BN_MONT_CTX *mont) {
343 const BIGNUM *n;
344 BN_ULONG *ap, *np, *rp, n0, v, carry;
345 int nl, max, i;
346
347 n = &mont->N;
348 nl = n->top;
349 if (nl == 0) {
350 ret->top = 0;
351 return 1;
352 }
353
354 max = (2 * nl); /* carry is stored separately */
355 if (bn_wexpand(r, max) == NULL) {
356 return 0;
357 }
358
359 r->neg ^= n->neg;
360 np = n->d;
361 rp = r->d;
362
363 /* clear the top words of T */
364 if (max > r->top) {
365 memset(&rp[r->top], 0, (max - r->top) * sizeof(BN_ULONG));
366 }
367
368 r->top = max;
369 n0 = mont->n0[0];
370
371 for (carry = 0, i = 0; i < nl; i++, rp++) {
372 v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
373 v = (v + carry + rp[nl]) & BN_MASK2;
374 carry |= (v != rp[nl]);
375 carry &= (v <= rp[nl]);
376 rp[nl] = v;
377 }
378
379 if (bn_wexpand(ret, nl) == NULL) {
380 return 0;
381 }
382 ret->top = nl;
383 ret->neg = r->neg;
384
385 rp = ret->d;
386 ap = &(r->d[nl]);
387
388 {
389 BN_ULONG *nrp;
390 size_t m;
391
392 v = bn_sub_words(rp, ap, np, nl) - carry;
393 /* if subtraction result is real, then trick unconditional memcpy below to
394 * perform in-place "refresh" instead of actual copy. */
395 m = (0 - (size_t)v);
396 nrp = (BN_ULONG *)(((intptr_t)rp & ~m) | ((intptr_t)ap & m));
397
398 for (i = 0, nl -= 4; i < nl; i += 4) {
399 BN_ULONG t1, t2, t3, t4;
400
401 t1 = nrp[i + 0];
402 t2 = nrp[i + 1];
403 t3 = nrp[i + 2];
404 ap[i + 0] = 0;
405 t4 = nrp[i + 3];
406 ap[i + 1] = 0;
407 rp[i + 0] = t1;
408 ap[i + 2] = 0;
409 rp[i + 1] = t2;
410 ap[i + 3] = 0;
411 rp[i + 2] = t3;
412 rp[i + 3] = t4;
413 }
414
415 for (nl += 4; i < nl; i++) {
416 rp[i] = nrp[i], ap[i] = 0;
417 }
418 }
419
420 bn_correct_top(r);
421 bn_correct_top(ret);
422
423 return 1;
424}
425#endif
426
427#define PTR_SIZE_INT size_t
428
429static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, const BN_MONT_CTX *mont)
430 {
431 BIGNUM *n;
432 BN_ULONG *ap,*np,*rp,n0,v,carry;
433 int nl,max,i;
434
435 n= (BIGNUM*) &(mont->N);
436 nl=n->top;
437 if (nl == 0) { ret->top=0; return(1); }
438
439 max=(2*nl); /* carry is stored separately */
440 if (bn_wexpand(r,max) == NULL) return(0);
441
442 r->neg^=n->neg;
443 np=n->d;
444 rp=r->d;
445
446 /* clear the top words of T */
447#if 1
448 for (i=r->top; i<max; i++) /* memset? XXX */
449 rp[i]=0;
450#else
451 memset(&(rp[r->top]),0,(max-r->top)*sizeof(BN_ULONG));
452#endif
453
454 r->top=max;
455 n0=mont->n0[0];
456
457 for (carry=0, i=0; i<nl; i++, rp++)
458 {
459 v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
460 v = (v+carry+rp[nl])&BN_MASK2;
461 carry |= (v != rp[nl]);
462 carry &= (v <= rp[nl]);
463 rp[nl]=v;
464 }
465
466 if (bn_wexpand(ret,nl) == NULL) return(0);
467 ret->top=nl;
468 ret->neg=r->neg;
469
470 rp=ret->d;
471 ap=&(r->d[nl]);
472
473 {
474 BN_ULONG *nrp;
475 size_t m;
476
477 v=bn_sub_words(rp,ap,np,nl)-carry;
478 /* if subtraction result is real, then
479 * trick unconditional memcpy below to perform in-place
480 * "refresh" instead of actual copy. */
481 m=(0-(size_t)v);
482 nrp=(BN_ULONG *)(((PTR_SIZE_INT)rp&~m)|((PTR_SIZE_INT)ap&m));
483
484 for (i=0,nl-=4; i<nl; i+=4)
485 {
486 BN_ULONG t1,t2,t3,t4;
487
488 t1=nrp[i+0];
489 t2=nrp[i+1];
490 t3=nrp[i+2]; ap[i+0]=0;
491 t4=nrp[i+3]; ap[i+1]=0;
492 rp[i+0]=t1; ap[i+2]=0;
493 rp[i+1]=t2; ap[i+3]=0;
494 rp[i+2]=t3;
495 rp[i+3]=t4;
496 }
497 for (nl+=4; i<nl; i++)
498 rp[i]=nrp[i], ap[i]=0;
499 }
500 bn_correct_top(r);
501 bn_correct_top(ret);
502
503 return(1);
504 }
505
506int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
507 BN_CTX *ctx) {
508 int retn = 0;
509 BIGNUM *t;
510
511 BN_CTX_start(ctx);
512 t = BN_CTX_get(ctx);
513 if (t == NULL) {
514 return 0;
515 }
516
517 if (BN_copy(t, a))
518 retn = BN_from_montgomery_word(ret, t, mont);
519 BN_CTX_end(ctx);
520
521 return retn;
522}
523
524int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
525 const BN_MONT_CTX *mont, BN_CTX *ctx) {
526 BIGNUM *tmp;
527 int ret = 0;
528
529#if defined(OPENSSL_BN_ASM_MONT)
530 int num = mont->N.top;
531
532 if (num > 1 && a->top == num && b->top == num) {
533 if (bn_wexpand(r, num) == NULL) {
534 return 0;
535 }
536 if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
537 r->neg = a->neg ^ b->neg;
538 r->top = num;
539 bn_correct_top(r);
540 return 1;
541 }
542 }
543#endif
544
545 BN_CTX_start(ctx);
546 tmp = BN_CTX_get(ctx);
547 if (tmp == NULL) {
548 goto err;
549 }
550
551 if (a == b) {
552 if (!BN_sqr(tmp, a, ctx)) {
553 goto err;
554 }
555 } else {
556 if (!BN_mul(tmp, a, b, ctx)) {
557 goto err;
558 }
559 }
560
561 /* reduce from aRR to aR */
562 if (!BN_from_montgomery_word(r, tmp, mont)) {
563 goto err;
564 }
565
566 ret = 1;
567
568err:
569 BN_CTX_end(ctx);
570 return ret;
571}