• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* 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 #include <openssl/bn.h>
58 
59 #include <assert.h>
60 #include <limits.h>
61 
62 #include <openssl/err.h>
63 
64 #include "internal.h"
65 
66 
67 // bn_div_words divides a double-width |h|,|l| by |d| and returns the result,
68 // which must fit in a |BN_ULONG|.
bn_div_words(BN_ULONG h,BN_ULONG l,BN_ULONG d)69 OPENSSL_UNUSED static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l,
70                                             BN_ULONG d) {
71   BN_ULONG dh, dl, q, ret = 0, th, tl, t;
72   int i, count = 2;
73 
74   if (d == 0) {
75     return BN_MASK2;
76   }
77 
78   i = BN_num_bits_word(d);
79   assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
80 
81   i = BN_BITS2 - i;
82   if (h >= d) {
83     h -= d;
84   }
85 
86   if (i) {
87     d <<= i;
88     h = (h << i) | (l >> (BN_BITS2 - i));
89     l <<= i;
90   }
91   dh = (d & BN_MASK2h) >> BN_BITS4;
92   dl = (d & BN_MASK2l);
93   for (;;) {
94     if ((h >> BN_BITS4) == dh) {
95       q = BN_MASK2l;
96     } else {
97       q = h / dh;
98     }
99 
100     th = q * dh;
101     tl = dl * q;
102     for (;;) {
103       t = h - th;
104       if ((t & BN_MASK2h) ||
105           ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) {
106         break;
107       }
108       q--;
109       th -= dh;
110       tl -= dl;
111     }
112     t = (tl >> BN_BITS4);
113     tl = (tl << BN_BITS4) & BN_MASK2h;
114     th += t;
115 
116     if (l < tl) {
117       th++;
118     }
119     l -= tl;
120     if (h < th) {
121       h += d;
122       q--;
123     }
124     h -= th;
125 
126     if (--count == 0) {
127       break;
128     }
129 
130     ret = q << BN_BITS4;
131     h = (h << BN_BITS4) | (l >> BN_BITS4);
132     l = (l & BN_MASK2l) << BN_BITS4;
133   }
134 
135   ret |= q;
136   return ret;
137 }
138 
bn_div_rem_words(BN_ULONG * quotient_out,BN_ULONG * rem_out,BN_ULONG n0,BN_ULONG n1,BN_ULONG d0)139 static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
140                                     BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) {
141   // GCC and Clang generate function calls to |__udivdi3| and |__umoddi3| when
142   // the |BN_ULLONG|-based C code is used.
143   //
144   // GCC bugs:
145   //   * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
146   //   * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721
147   //   * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54183
148   //   * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58897
149   //   * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65668
150   //
151   // Clang bugs:
152   //   * https://llvm.org/bugs/show_bug.cgi?id=6397
153   //   * https://llvm.org/bugs/show_bug.cgi?id=12418
154   //
155   // These issues aren't specific to x86 and x86_64, so it might be worthwhile
156   // to add more assembly language implementations.
157 #if defined(BN_CAN_USE_INLINE_ASM) && defined(OPENSSL_X86)
158   __asm__ volatile("divl %4"
159                    : "=a"(*quotient_out), "=d"(*rem_out)
160                    : "a"(n1), "d"(n0), "rm"(d0)
161                    : "cc");
162 #elif defined(BN_CAN_USE_INLINE_ASM) && defined(OPENSSL_X86_64)
163   __asm__ volatile("divq %4"
164                    : "=a"(*quotient_out), "=d"(*rem_out)
165                    : "a"(n1), "d"(n0), "rm"(d0)
166                    : "cc");
167 #else
168 #if defined(BN_CAN_DIVIDE_ULLONG)
169   BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
170   *quotient_out = (BN_ULONG)(n / d0);
171 #else
172   *quotient_out = bn_div_words(n0, n1, d0);
173 #endif
174   *rem_out = n1 - (*quotient_out * d0);
175 #endif
176 }
177 
178 // BN_div computes "quotient := numerator / divisor", rounding towards zero,
179 // and sets up |rem| such that "quotient * divisor + rem = numerator" holds.
180 //
181 // Thus:
182 //
183 //     quotient->neg == numerator->neg ^ divisor->neg
184 //        (unless the result is zero)
185 //     rem->neg == numerator->neg
186 //        (unless the remainder is zero)
187 //
188 // If |quotient| or |rem| is NULL, the respective value is not returned.
189 //
190 // This was specifically designed to contain fewer branches that may leak
191 // sensitive information; see "New Branch Prediction Vulnerabilities in OpenSSL
192 // and Necessary Software Countermeasures" by Onur Acıçmez, Shay Gueron, and
193 // Jean-Pierre Seifert.
BN_div(BIGNUM * quotient,BIGNUM * rem,const BIGNUM * numerator,const BIGNUM * divisor,BN_CTX * ctx)194 int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
195            const BIGNUM *divisor, BN_CTX *ctx) {
196   int norm_shift, loop;
197   BIGNUM wnum;
198   BN_ULONG *resp, *wnump;
199   BN_ULONG d0, d1;
200   int num_n, div_n;
201 
202   // This function relies on the historical minimal-width |BIGNUM| invariant.
203   // It is already not constant-time (constant-time reductions should use
204   // Montgomery logic), so we shrink all inputs and intermediate values to
205   // retain the previous behavior.
206 
207   // Invalid zero-padding would have particularly bad consequences.
208   int numerator_width = bn_minimal_width(numerator);
209   int divisor_width = bn_minimal_width(divisor);
210   if ((numerator_width > 0 && numerator->d[numerator_width - 1] == 0) ||
211       (divisor_width > 0 && divisor->d[divisor_width - 1] == 0)) {
212     OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
213     return 0;
214   }
215 
216   if (BN_is_zero(divisor)) {
217     OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
218     return 0;
219   }
220 
221   BN_CTX_start(ctx);
222   BIGNUM *tmp = BN_CTX_get(ctx);
223   BIGNUM *snum = BN_CTX_get(ctx);
224   BIGNUM *sdiv = BN_CTX_get(ctx);
225   BIGNUM *res = NULL;
226   if (quotient == NULL) {
227     res = BN_CTX_get(ctx);
228   } else {
229     res = quotient;
230   }
231   if (sdiv == NULL || res == NULL) {
232     goto err;
233   }
234 
235   // First we normalise the numbers
236   norm_shift = BN_BITS2 - (BN_num_bits(divisor) % BN_BITS2);
237   if (!BN_lshift(sdiv, divisor, norm_shift)) {
238     goto err;
239   }
240   bn_set_minimal_width(sdiv);
241   sdiv->neg = 0;
242   norm_shift += BN_BITS2;
243   if (!BN_lshift(snum, numerator, norm_shift)) {
244     goto err;
245   }
246   bn_set_minimal_width(snum);
247   snum->neg = 0;
248 
249   // Since we don't want to have special-case logic for the case where snum is
250   // larger than sdiv, we pad snum with enough zeroes without changing its
251   // value.
252   if (snum->width <= sdiv->width + 1) {
253     if (!bn_wexpand(snum, sdiv->width + 2)) {
254       goto err;
255     }
256     for (int i = snum->width; i < sdiv->width + 2; i++) {
257       snum->d[i] = 0;
258     }
259     snum->width = sdiv->width + 2;
260   } else {
261     if (!bn_wexpand(snum, snum->width + 1)) {
262       goto err;
263     }
264     snum->d[snum->width] = 0;
265     snum->width++;
266   }
267 
268   div_n = sdiv->width;
269   num_n = snum->width;
270   loop = num_n - div_n;
271   // Lets setup a 'window' into snum
272   // This is the part that corresponds to the current
273   // 'area' being divided
274   wnum.neg = 0;
275   wnum.d = &(snum->d[loop]);
276   wnum.width = div_n;
277   // only needed when BN_ucmp messes up the values between width and max
278   wnum.dmax = snum->dmax - loop;  // so we don't step out of bounds
279 
280   // Get the top 2 words of sdiv
281   // div_n=sdiv->width;
282   d0 = sdiv->d[div_n - 1];
283   d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
284 
285   // pointer to the 'top' of snum
286   wnump = &(snum->d[num_n - 1]);
287 
288   // Setup |res|. |numerator| and |res| may alias, so we save |numerator->neg|
289   // for later.
290   const int numerator_neg = numerator->neg;
291   res->neg = (numerator_neg ^ divisor->neg);
292   if (!bn_wexpand(res, loop + 1)) {
293     goto err;
294   }
295   res->width = loop - 1;
296   resp = &(res->d[loop - 1]);
297 
298   // space for temp
299   if (!bn_wexpand(tmp, div_n + 1)) {
300     goto err;
301   }
302 
303   // if res->width == 0 then clear the neg value otherwise decrease
304   // the resp pointer
305   if (res->width == 0) {
306     res->neg = 0;
307   } else {
308     resp--;
309   }
310 
311   for (int i = 0; i < loop - 1; i++, wnump--, resp--) {
312     BN_ULONG q, l0;
313     // the first part of the loop uses the top two words of snum and sdiv to
314     // calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
315     BN_ULONG n0, n1, rm = 0;
316 
317     n0 = wnump[0];
318     n1 = wnump[-1];
319     if (n0 == d0) {
320       q = BN_MASK2;
321     } else {
322       // n0 < d0
323       bn_div_rem_words(&q, &rm, n0, n1, d0);
324 
325 #ifdef BN_ULLONG
326       BN_ULLONG t2 = (BN_ULLONG)d1 * q;
327       for (;;) {
328         if (t2 <= ((((BN_ULLONG)rm) << BN_BITS2) | wnump[-2])) {
329           break;
330         }
331         q--;
332         rm += d0;
333         if (rm < d0) {
334           break;  // don't let rm overflow
335         }
336         t2 -= d1;
337       }
338 #else  // !BN_ULLONG
339       BN_ULONG t2l, t2h;
340       BN_UMULT_LOHI(t2l, t2h, d1, q);
341       for (;;) {
342         if (t2h < rm ||
343             (t2h == rm && t2l <= wnump[-2])) {
344           break;
345         }
346         q--;
347         rm += d0;
348         if (rm < d0) {
349           break;  // don't let rm overflow
350         }
351         if (t2l < d1) {
352           t2h--;
353         }
354         t2l -= d1;
355       }
356 #endif  // !BN_ULLONG
357     }
358 
359     l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
360     tmp->d[div_n] = l0;
361     wnum.d--;
362     // ingore top values of the bignums just sub the two
363     // BN_ULONG arrays with bn_sub_words
364     if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
365       // Note: As we have considered only the leading
366       // two BN_ULONGs in the calculation of q, sdiv * q
367       // might be greater than wnum (but then (q-1) * sdiv
368       // is less or equal than wnum)
369       q--;
370       if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) {
371         // we can't have an overflow here (assuming
372         // that q != 0, but if q == 0 then tmp is
373         // zero anyway)
374         (*wnump)++;
375       }
376     }
377     // store part of the result
378     *resp = q;
379   }
380 
381   bn_set_minimal_width(snum);
382 
383   if (rem != NULL) {
384     if (!BN_rshift(rem, snum, norm_shift)) {
385       goto err;
386     }
387     if (!BN_is_zero(rem)) {
388       rem->neg = numerator_neg;
389     }
390   }
391 
392   bn_set_minimal_width(res);
393   BN_CTX_end(ctx);
394   return 1;
395 
396 err:
397   BN_CTX_end(ctx);
398   return 0;
399 }
400 
BN_nnmod(BIGNUM * r,const BIGNUM * m,const BIGNUM * d,BN_CTX * ctx)401 int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
402   if (!(BN_mod(r, m, d, ctx))) {
403     return 0;
404   }
405   if (!r->neg) {
406     return 1;
407   }
408 
409   // now -|d| < r < 0, so we have to set r := r + |d|.
410   return (d->neg ? BN_sub : BN_add)(r, r, d);
411 }
412 
bn_reduce_once(BN_ULONG * r,const BN_ULONG * a,BN_ULONG carry,const BN_ULONG * m,size_t num)413 BN_ULONG bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry,
414                         const BN_ULONG *m, size_t num) {
415   assert(r != a);
416   // |r| = |a| - |m|. |bn_sub_words| performs the bulk of the subtraction, and
417   // then we apply the borrow to |carry|.
418   carry -= bn_sub_words(r, a, m, num);
419   // We know 0 <= |a| < 2*|m|, so -|m| <= |r| < |m|.
420   //
421   // If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then
422   // wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to
423   // return |r| + |m|, or |a|. |carry| must then be -1 or all ones. In both
424   // cases, |carry| is a suitable input to |bn_select_words|.
425   //
426   // Although |carry| may be one if it was one on input and |bn_sub_words|
427   // returns zero, this would give |r| > |m|, violating our input assumptions.
428   assert(carry == 0 || carry == (BN_ULONG)-1);
429   bn_select_words(r, carry, a /* r < 0 */, r /* r >= 0 */, num);
430   return carry;
431 }
432 
bn_reduce_once_in_place(BN_ULONG * r,BN_ULONG carry,const BN_ULONG * m,BN_ULONG * tmp,size_t num)433 BN_ULONG bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m,
434                                  BN_ULONG *tmp, size_t num) {
435   // See |bn_reduce_once| for why this logic works.
436   carry -= bn_sub_words(tmp, r, m, num);
437   assert(carry == 0 || carry == (BN_ULONG)-1);
438   bn_select_words(r, carry, r /* tmp < 0 */, tmp /* tmp >= 0 */, num);
439   return carry;
440 }
441 
bn_mod_sub_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,const BN_ULONG * m,BN_ULONG * tmp,size_t num)442 void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
443                       const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
444   // r = a - b
445   BN_ULONG borrow = bn_sub_words(r, a, b, num);
446   // tmp = a - b + m
447   bn_add_words(tmp, r, m, num);
448   bn_select_words(r, 0 - borrow, tmp /* r < 0 */, r /* r >= 0 */, num);
449 }
450 
bn_mod_add_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,const BN_ULONG * m,BN_ULONG * tmp,size_t num)451 void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
452                       const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
453   BN_ULONG carry = bn_add_words(r, a, b, num);
454   bn_reduce_once_in_place(r, carry, m, tmp, num);
455 }
456 
bn_div_consttime(BIGNUM * quotient,BIGNUM * remainder,const BIGNUM * numerator,const BIGNUM * divisor,unsigned divisor_min_bits,BN_CTX * ctx)457 int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
458                      const BIGNUM *numerator, const BIGNUM *divisor,
459                      unsigned divisor_min_bits, BN_CTX *ctx) {
460   if (BN_is_negative(numerator) || BN_is_negative(divisor)) {
461     OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
462     return 0;
463   }
464   if (BN_is_zero(divisor)) {
465     OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
466     return 0;
467   }
468 
469   // This function implements long division in binary. It is not very efficient,
470   // but it is simple, easy to make constant-time, and performant enough for RSA
471   // key generation.
472 
473   int ret = 0;
474   BN_CTX_start(ctx);
475   BIGNUM *q = quotient, *r = remainder;
476   if (quotient == NULL || quotient == numerator || quotient == divisor) {
477     q = BN_CTX_get(ctx);
478   }
479   if (remainder == NULL || remainder == numerator || remainder == divisor) {
480     r = BN_CTX_get(ctx);
481   }
482   BIGNUM *tmp = BN_CTX_get(ctx);
483   if (q == NULL || r == NULL || tmp == NULL ||
484       !bn_wexpand(q, numerator->width) ||
485       !bn_wexpand(r, divisor->width) ||
486       !bn_wexpand(tmp, divisor->width)) {
487     goto err;
488   }
489 
490   OPENSSL_memset(q->d, 0, numerator->width * sizeof(BN_ULONG));
491   q->width = numerator->width;
492   q->neg = 0;
493 
494   OPENSSL_memset(r->d, 0, divisor->width * sizeof(BN_ULONG));
495   r->width = divisor->width;
496   r->neg = 0;
497 
498   // Incorporate |numerator| into |r|, one bit at a time, reducing after each
499   // step. We maintain the invariant that |0 <= r < divisor| and
500   // |q * divisor + r = n| where |n| is the portion of |numerator| incorporated
501   // so far.
502   //
503   // First, we short-circuit the loop: if we know |divisor| has at least
504   // |divisor_min_bits| bits, the top |divisor_min_bits - 1| can be incorporated
505   // without reductions. This significantly speeds up |RSA_check_key|. For
506   // simplicity, we round down to a whole number of words.
507   assert(divisor_min_bits <= BN_num_bits(divisor));
508   int initial_words = 0;
509   if (divisor_min_bits > 0) {
510     initial_words = (divisor_min_bits - 1) / BN_BITS2;
511     if (initial_words > numerator->width) {
512       initial_words = numerator->width;
513     }
514     OPENSSL_memcpy(r->d, numerator->d + numerator->width - initial_words,
515                    initial_words * sizeof(BN_ULONG));
516   }
517 
518   for (int i = numerator->width - initial_words - 1; i >= 0; i--) {
519     for (int bit = BN_BITS2 - 1; bit >= 0; bit--) {
520       // Incorporate the next bit of the numerator, by computing
521       // r = 2*r or 2*r + 1. Note the result fits in one more word. We store the
522       // extra word in |carry|.
523       BN_ULONG carry = bn_add_words(r->d, r->d, r->d, divisor->width);
524       r->d[0] |= (numerator->d[i] >> bit) & 1;
525       // |r| was previously fully-reduced, so we know:
526       //      2*0 <= r <= 2*(divisor-1) + 1
527       //        0 <= r <= 2*divisor - 1 < 2*divisor.
528       // Thus |r| satisfies the preconditions for |bn_reduce_once_in_place|.
529       BN_ULONG subtracted = bn_reduce_once_in_place(r->d, carry, divisor->d,
530                                                     tmp->d, divisor->width);
531       // The corresponding bit of the quotient is set iff we needed to subtract.
532       q->d[i] |= (~subtracted & 1) << bit;
533     }
534   }
535 
536   if ((quotient != NULL && !BN_copy(quotient, q)) ||
537       (remainder != NULL && !BN_copy(remainder, r))) {
538     goto err;
539   }
540 
541   ret = 1;
542 
543 err:
544   BN_CTX_end(ctx);
545   return ret;
546 }
547 
bn_scratch_space_from_ctx(size_t width,BN_CTX * ctx)548 static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) {
549   BIGNUM *ret = BN_CTX_get(ctx);
550   if (ret == NULL ||
551       !bn_wexpand(ret, width)) {
552     return NULL;
553   }
554   ret->neg = 0;
555   ret->width = width;
556   return ret;
557 }
558 
559 // bn_resized_from_ctx returns |bn| with width at least |width| or NULL on
560 // error. This is so it may be used with low-level "words" functions. If
561 // necessary, it allocates a new |BIGNUM| with a lifetime of the current scope
562 // in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in
563 // |width| words.
bn_resized_from_ctx(const BIGNUM * bn,size_t width,BN_CTX * ctx)564 static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width,
565                                          BN_CTX *ctx) {
566   if ((size_t)bn->width >= width) {
567     // Any excess words must be zero.
568     assert(bn_fits_in_words(bn, width));
569     return bn;
570   }
571   BIGNUM *ret = bn_scratch_space_from_ctx(width, ctx);
572   if (ret == NULL ||
573       !BN_copy(ret, bn) ||
574       !bn_resize_words(ret, width)) {
575     return NULL;
576   }
577   return ret;
578 }
579 
BN_mod_add(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)580 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
581                BN_CTX *ctx) {
582   if (!BN_add(r, a, b)) {
583     return 0;
584   }
585   return BN_nnmod(r, r, m, ctx);
586 }
587 
BN_mod_add_quick(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m)588 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
589                      const BIGNUM *m) {
590   BN_CTX *ctx = BN_CTX_new();
591   int ok = ctx != NULL &&
592            bn_mod_add_consttime(r, a, b, m, ctx);
593   BN_CTX_free(ctx);
594   return ok;
595 }
596 
bn_mod_add_consttime(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)597 int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
598                          const BIGNUM *m, BN_CTX *ctx) {
599   BN_CTX_start(ctx);
600   a = bn_resized_from_ctx(a, m->width, ctx);
601   b = bn_resized_from_ctx(b, m->width, ctx);
602   BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
603   int ok = a != NULL && b != NULL && tmp != NULL &&
604            bn_wexpand(r, m->width);
605   if (ok) {
606     bn_mod_add_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
607     r->width = m->width;
608     r->neg = 0;
609   }
610   BN_CTX_end(ctx);
611   return ok;
612 }
613 
BN_mod_sub(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)614 int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
615                BN_CTX *ctx) {
616   if (!BN_sub(r, a, b)) {
617     return 0;
618   }
619   return BN_nnmod(r, r, m, ctx);
620 }
621 
bn_mod_sub_consttime(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)622 int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
623                          const BIGNUM *m, BN_CTX *ctx) {
624   BN_CTX_start(ctx);
625   a = bn_resized_from_ctx(a, m->width, ctx);
626   b = bn_resized_from_ctx(b, m->width, ctx);
627   BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
628   int ok = a != NULL && b != NULL && tmp != NULL &&
629            bn_wexpand(r, m->width);
630   if (ok) {
631     bn_mod_sub_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
632     r->width = m->width;
633     r->neg = 0;
634   }
635   BN_CTX_end(ctx);
636   return ok;
637 }
638 
BN_mod_sub_quick(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m)639 int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
640                      const BIGNUM *m) {
641   BN_CTX *ctx = BN_CTX_new();
642   int ok = ctx != NULL &&
643            bn_mod_sub_consttime(r, a, b, m, ctx);
644   BN_CTX_free(ctx);
645   return ok;
646 }
647 
BN_mod_mul(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * m,BN_CTX * ctx)648 int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
649                BN_CTX *ctx) {
650   BIGNUM *t;
651   int ret = 0;
652 
653   BN_CTX_start(ctx);
654   t = BN_CTX_get(ctx);
655   if (t == NULL) {
656     goto err;
657   }
658 
659   if (a == b) {
660     if (!BN_sqr(t, a, ctx)) {
661       goto err;
662     }
663   } else {
664     if (!BN_mul(t, a, b, ctx)) {
665       goto err;
666     }
667   }
668 
669   if (!BN_nnmod(r, t, m, ctx)) {
670     goto err;
671   }
672 
673   ret = 1;
674 
675 err:
676   BN_CTX_end(ctx);
677   return ret;
678 }
679 
BN_mod_sqr(BIGNUM * r,const BIGNUM * a,const BIGNUM * m,BN_CTX * ctx)680 int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
681   if (!BN_sqr(r, a, ctx)) {
682     return 0;
683   }
684 
685   // r->neg == 0,  thus we don't need BN_nnmod
686   return BN_mod(r, r, m, ctx);
687 }
688 
BN_mod_lshift(BIGNUM * r,const BIGNUM * a,int n,const BIGNUM * m,BN_CTX * ctx)689 int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
690                   BN_CTX *ctx) {
691   BIGNUM *abs_m = NULL;
692   int ret;
693 
694   if (!BN_nnmod(r, a, m, ctx)) {
695     return 0;
696   }
697 
698   if (m->neg) {
699     abs_m = BN_dup(m);
700     if (abs_m == NULL) {
701       return 0;
702     }
703     abs_m->neg = 0;
704   }
705 
706   ret = bn_mod_lshift_consttime(r, r, n, (abs_m ? abs_m : m), ctx);
707 
708   BN_free(abs_m);
709   return ret;
710 }
711 
bn_mod_lshift_consttime(BIGNUM * r,const BIGNUM * a,int n,const BIGNUM * m,BN_CTX * ctx)712 int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
713                             BN_CTX *ctx) {
714   if (!BN_copy(r, a)) {
715     return 0;
716   }
717   for (int i = 0; i < n; i++) {
718     if (!bn_mod_lshift1_consttime(r, r, m, ctx)) {
719       return 0;
720     }
721   }
722   return 1;
723 }
724 
BN_mod_lshift_quick(BIGNUM * r,const BIGNUM * a,int n,const BIGNUM * m)725 int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
726   BN_CTX *ctx = BN_CTX_new();
727   int ok = ctx != NULL &&
728            bn_mod_lshift_consttime(r, a, n, m, ctx);
729   BN_CTX_free(ctx);
730   return ok;
731 }
732 
BN_mod_lshift1(BIGNUM * r,const BIGNUM * a,const BIGNUM * m,BN_CTX * ctx)733 int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
734   if (!BN_lshift1(r, a)) {
735     return 0;
736   }
737 
738   return BN_nnmod(r, r, m, ctx);
739 }
740 
bn_mod_lshift1_consttime(BIGNUM * r,const BIGNUM * a,const BIGNUM * m,BN_CTX * ctx)741 int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
742                              BN_CTX *ctx) {
743   return bn_mod_add_consttime(r, a, a, m, ctx);
744 }
745 
BN_mod_lshift1_quick(BIGNUM * r,const BIGNUM * a,const BIGNUM * m)746 int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
747   BN_CTX *ctx = BN_CTX_new();
748   int ok = ctx != NULL &&
749            bn_mod_lshift1_consttime(r, a, m, ctx);
750   BN_CTX_free(ctx);
751   return ok;
752 }
753 
BN_div_word(BIGNUM * a,BN_ULONG w)754 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
755   BN_ULONG ret = 0;
756   int i, j;
757 
758   if (!w) {
759     // actually this an error (division by zero)
760     return (BN_ULONG) - 1;
761   }
762 
763   if (a->width == 0) {
764     return 0;
765   }
766 
767   // normalize input for |bn_div_rem_words|.
768   j = BN_BITS2 - BN_num_bits_word(w);
769   w <<= j;
770   if (!BN_lshift(a, a, j)) {
771     return (BN_ULONG) - 1;
772   }
773 
774   for (i = a->width - 1; i >= 0; i--) {
775     BN_ULONG l = a->d[i];
776     BN_ULONG d;
777     BN_ULONG unused_rem;
778     bn_div_rem_words(&d, &unused_rem, ret, l, w);
779     ret = l - (d * w);
780     a->d[i] = d;
781   }
782 
783   bn_set_minimal_width(a);
784   ret >>= j;
785   return ret;
786 }
787 
BN_mod_word(const BIGNUM * a,BN_ULONG w)788 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
789 #ifndef BN_CAN_DIVIDE_ULLONG
790   BN_ULONG ret = 0;
791 #else
792   BN_ULLONG ret = 0;
793 #endif
794   int i;
795 
796   if (w == 0) {
797     return (BN_ULONG) -1;
798   }
799 
800 #ifndef BN_CAN_DIVIDE_ULLONG
801   // If |w| is too long and we don't have |BN_ULLONG| division then we need to
802   // fall back to using |BN_div_word|.
803   if (w > ((BN_ULONG)1 << BN_BITS4)) {
804     BIGNUM *tmp = BN_dup(a);
805     if (tmp == NULL) {
806       return (BN_ULONG)-1;
807     }
808     ret = BN_div_word(tmp, w);
809     BN_free(tmp);
810     return ret;
811   }
812 #endif
813 
814   for (i = a->width - 1; i >= 0; i--) {
815 #ifndef BN_CAN_DIVIDE_ULLONG
816     ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
817     ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
818 #else
819     ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w);
820 #endif
821   }
822   return (BN_ULONG)ret;
823 }
824 
BN_mod_pow2(BIGNUM * r,const BIGNUM * a,size_t e)825 int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
826   if (e == 0 || a->width == 0) {
827     BN_zero(r);
828     return 1;
829   }
830 
831   size_t num_words = 1 + ((e - 1) / BN_BITS2);
832 
833   // If |a| definitely has less than |e| bits, just BN_copy.
834   if ((size_t) a->width < num_words) {
835     return BN_copy(r, a) != NULL;
836   }
837 
838   // Otherwise, first make sure we have enough space in |r|.
839   // Note that this will fail if num_words > INT_MAX.
840   if (!bn_wexpand(r, num_words)) {
841     return 0;
842   }
843 
844   // Copy the content of |a| into |r|.
845   OPENSSL_memcpy(r->d, a->d, num_words * sizeof(BN_ULONG));
846 
847   // If |e| isn't word-aligned, we have to mask off some of our bits.
848   size_t top_word_exponent = e % (sizeof(BN_ULONG) * 8);
849   if (top_word_exponent != 0) {
850     r->d[num_words - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
851   }
852 
853   // Fill in the remaining fields of |r|.
854   r->neg = a->neg;
855   r->width = (int) num_words;
856   bn_set_minimal_width(r);
857   return 1;
858 }
859 
BN_nnmod_pow2(BIGNUM * r,const BIGNUM * a,size_t e)860 int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
861   if (!BN_mod_pow2(r, a, e)) {
862     return 0;
863   }
864 
865   // If the returned value was non-negative, we're done.
866   if (BN_is_zero(r) || !r->neg) {
867     return 1;
868   }
869 
870   size_t num_words = 1 + (e - 1) / BN_BITS2;
871 
872   // Expand |r| to the size of our modulus.
873   if (!bn_wexpand(r, num_words)) {
874     return 0;
875   }
876 
877   // Clear the upper words of |r|.
878   OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES);
879 
880   // Set parameters of |r|.
881   r->neg = 0;
882   r->width = (int) num_words;
883 
884   // Now, invert every word. The idea here is that we want to compute 2^e-|x|,
885   // which is actually equivalent to the twos-complement representation of |x|
886   // in |e| bits, which is -x = ~x + 1.
887   for (int i = 0; i < r->width; i++) {
888     r->d[i] = ~r->d[i];
889   }
890 
891   // If our exponent doesn't span the top word, we have to mask the rest.
892   size_t top_word_exponent = e % BN_BITS2;
893   if (top_word_exponent != 0) {
894     r->d[r->width - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
895   }
896 
897   // Keep the minimal-width invariant for |BIGNUM|.
898   bn_set_minimal_width(r);
899 
900   // Finally, add one, for the reason described above.
901   return BN_add(r, r, BN_value_one());
902 }
903