Remove email addresses from source code.
[openssl.git] / crypto / bn / bn_div.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/bn.h>
11 #include "internal/cryptlib.h"
12 #include "bn_lcl.h"
13
14 /* The old slow way */
15 #if 0
16 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
17            BN_CTX *ctx)
18 {
19     int i, nm, nd;
20     int ret = 0;
21     BIGNUM *D;
22
23     bn_check_top(m);
24     bn_check_top(d);
25     if (BN_is_zero(d)) {
26         BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
27         return (0);
28     }
29
30     if (BN_ucmp(m, d) < 0) {
31         if (rem != NULL) {
32             if (BN_copy(rem, m) == NULL)
33                 return (0);
34         }
35         if (dv != NULL)
36             BN_zero(dv);
37         return 1;
38     }
39
40     BN_CTX_start(ctx);
41     D = BN_CTX_get(ctx);
42     if (dv == NULL)
43         dv = BN_CTX_get(ctx);
44     if (rem == NULL)
45         rem = BN_CTX_get(ctx);
46     if (D == NULL || dv == NULL || rem == NULL)
47         goto end;
48
49     nd = BN_num_bits(d);
50     nm = BN_num_bits(m);
51     if (BN_copy(D, d) == NULL)
52         goto end;
53     if (BN_copy(rem, m) == NULL)
54         goto end;
55
56     /*
57      * The next 2 are needed so we can do a dv->d[0]|=1 later since
58      * BN_lshift1 will only work once there is a value :-)
59      */
60     BN_zero(dv);
61     if (bn_wexpand(dv, 1) == NULL)
62         goto end;
63     dv->top = 1;
64
65     if (!BN_lshift(D, D, nm - nd))
66         goto end;
67     for (i = nm - nd; i >= 0; i--) {
68         if (!BN_lshift1(dv, dv))
69             goto end;
70         if (BN_ucmp(rem, D) >= 0) {
71             dv->d[0] |= 1;
72             if (!BN_usub(rem, rem, D))
73                 goto end;
74         }
75 /* CAN IMPROVE (and have now :=) */
76         if (!BN_rshift1(D, D))
77             goto end;
78     }
79     rem->neg = BN_is_zero(rem) ? 0 : m->neg;
80     dv->neg = m->neg ^ d->neg;
81     ret = 1;
82  end:
83     BN_CTX_end(ctx);
84     return (ret);
85 }
86
87 #else
88
89 # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
90     && !defined(PEDANTIC) && !defined(BN_DIV3W)
91 #  if defined(__GNUC__) && __GNUC__>=2
92 #   if defined(__i386) || defined (__i386__)
93    /*-
94     * There were two reasons for implementing this template:
95     * - GNU C generates a call to a function (__udivdi3 to be exact)
96     *   in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
97     *   understand why...);
98     * - divl doesn't only calculate quotient, but also leaves
99     *   remainder in %edx which we can definitely use here:-)
100     */
101 #    undef bn_div_words
102 #    define bn_div_words(n0,n1,d0)                \
103         ({  asm volatile (                      \
104                 "divl   %4"                     \
105                 : "=a"(q), "=d"(rem)            \
106                 : "a"(n1), "d"(n0), "r"(d0)     \
107                 : "cc");                        \
108             q;                                  \
109         })
110 #    define REMAINDER_IS_ALREADY_CALCULATED
111 #   elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
112    /*
113     * Same story here, but it's 128-bit by 64-bit division. Wow!
114     */
115 #    undef bn_div_words
116 #    define bn_div_words(n0,n1,d0)                \
117         ({  asm volatile (                      \
118                 "divq   %4"                     \
119                 : "=a"(q), "=d"(rem)            \
120                 : "a"(n1), "d"(n0), "r"(d0)     \
121                 : "cc");                        \
122             q;                                  \
123         })
124 #    define REMAINDER_IS_ALREADY_CALCULATED
125 #   endif                       /* __<cpu> */
126 #  endif                        /* __GNUC__ */
127 # endif                         /* OPENSSL_NO_ASM */
128
129 /*-
130  * BN_div computes  dv := num / divisor, rounding towards
131  * zero, and sets up rm  such that  dv*divisor + rm = num  holds.
132  * Thus:
133  *     dv->neg == num->neg ^ divisor->neg  (unless the result is zero)
134  *     rm->neg == num->neg                 (unless the remainder is zero)
135  * If 'dv' or 'rm' is NULL, the respective value is not returned.
136  */
137 int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
138            BN_CTX *ctx)
139 {
140     int norm_shift, i, loop;
141     BIGNUM *tmp, wnum, *snum, *sdiv, *res;
142     BN_ULONG *resp, *wnump;
143     BN_ULONG d0, d1;
144     int num_n, div_n;
145     int no_branch = 0;
146
147     /*
148      * Invalid zero-padding would have particularly bad consequences so don't
149      * just rely on bn_check_top() here (bn_check_top() works only for
150      * BN_DEBUG builds)
151      */
152     if ((num->top > 0 && num->d[num->top - 1] == 0) ||
153         (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
154         BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
155         return 0;
156     }
157
158     bn_check_top(num);
159     bn_check_top(divisor);
160
161     if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)
162         || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {
163         no_branch = 1;
164     }
165
166     bn_check_top(dv);
167     bn_check_top(rm);
168     /*- bn_check_top(num); *//*
169      * 'num' has been checked already
170      */
171     /*- bn_check_top(divisor); *//*
172      * 'divisor' has been checked already
173      */
174
175     if (BN_is_zero(divisor)) {
176         BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
177         return (0);
178     }
179
180     if (!no_branch && BN_ucmp(num, divisor) < 0) {
181         if (rm != NULL) {
182             if (BN_copy(rm, num) == NULL)
183                 return (0);
184         }
185         if (dv != NULL)
186             BN_zero(dv);
187         return 1;
188     }
189
190     BN_CTX_start(ctx);
191     res = (dv == NULL) ? BN_CTX_get(ctx) : dv;
192     tmp = BN_CTX_get(ctx);
193     snum = BN_CTX_get(ctx);
194     sdiv = BN_CTX_get(ctx);
195     if (sdiv == NULL)
196         goto err;
197
198     /* First we normalise the numbers */
199     norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
200     if (!(BN_lshift(sdiv, divisor, norm_shift)))
201         goto err;
202     sdiv->neg = 0;
203     norm_shift += BN_BITS2;
204     if (!(BN_lshift(snum, num, norm_shift)))
205         goto err;
206     snum->neg = 0;
207
208     if (no_branch) {
209         /*
210          * Since we don't know whether snum is larger than sdiv, we pad snum
211          * with enough zeroes without changing its value.
212          */
213         if (snum->top <= sdiv->top + 1) {
214             if (bn_wexpand(snum, sdiv->top + 2) == NULL)
215                 goto err;
216             for (i = snum->top; i < sdiv->top + 2; i++)
217                 snum->d[i] = 0;
218             snum->top = sdiv->top + 2;
219         } else {
220             if (bn_wexpand(snum, snum->top + 1) == NULL)
221                 goto err;
222             snum->d[snum->top] = 0;
223             snum->top++;
224         }
225     }
226
227     div_n = sdiv->top;
228     num_n = snum->top;
229     loop = num_n - div_n;
230     /*
231      * Lets setup a 'window' into snum This is the part that corresponds to
232      * the current 'area' being divided
233      */
234     wnum.neg = 0;
235     wnum.d = &(snum->d[loop]);
236     wnum.top = div_n;
237     /*
238      * only needed when BN_ucmp messes up the values between top and max
239      */
240     wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
241
242     /* Get the top 2 words of sdiv */
243     /* div_n=sdiv->top; */
244     d0 = sdiv->d[div_n - 1];
245     d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
246
247     /* pointer to the 'top' of snum */
248     wnump = &(snum->d[num_n - 1]);
249
250     /* Setup to 'res' */
251     if (!bn_wexpand(res, (loop + 1)))
252         goto err;
253     res->neg = (num->neg ^ divisor->neg);
254     res->top = loop - no_branch;
255     resp = &(res->d[loop - 1]);
256
257     /* space for temp */
258     if (!bn_wexpand(tmp, (div_n + 1)))
259         goto err;
260
261     if (!no_branch) {
262         if (BN_ucmp(&wnum, sdiv) >= 0) {
263             /*
264              * If BN_DEBUG_RAND is defined BN_ucmp changes (via bn_pollute)
265              * the const bignum arguments => clean the values between top and
266              * max again
267              */
268             bn_clear_top2max(&wnum);
269             bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
270             *resp = 1;
271         } else
272             res->top--;
273     }
274
275     /* Increase the resp pointer so that we never create an invalid pointer. */
276     resp++;
277
278     /*
279      * if res->top == 0 then clear the neg value otherwise decrease the resp
280      * pointer
281      */
282     if (res->top == 0)
283         res->neg = 0;
284     else
285         resp--;
286
287     for (i = 0; i < loop - 1; i++, wnump--) {
288         BN_ULONG q, l0;
289         /*
290          * the first part of the loop uses the top two words of snum and sdiv
291          * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
292          */
293 # if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
294         BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);
295         q = bn_div_3_words(wnump, d1, d0);
296 # else
297         BN_ULONG n0, n1, rem = 0;
298
299         n0 = wnump[0];
300         n1 = wnump[-1];
301         if (n0 == d0)
302             q = BN_MASK2;
303         else {                  /* n0 < d0 */
304
305 #  ifdef BN_LLONG
306             BN_ULLONG t2;
307
308 #   if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
309             q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);
310 #   else
311             q = bn_div_words(n0, n1, d0);
312 #   endif
313
314 #   ifndef REMAINDER_IS_ALREADY_CALCULATED
315             /*
316              * rem doesn't have to be BN_ULLONG. The least we
317              * know it's less that d0, isn't it?
318              */
319             rem = (n1 - q * d0) & BN_MASK2;
320 #   endif
321             t2 = (BN_ULLONG) d1 *q;
322
323             for (;;) {
324                 if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))
325                     break;
326                 q--;
327                 rem += d0;
328                 if (rem < d0)
329                     break;      /* don't let rem overflow */
330                 t2 -= d1;
331             }
332 #  else                         /* !BN_LLONG */
333             BN_ULONG t2l, t2h;
334
335             q = bn_div_words(n0, n1, d0);
336 #   ifndef REMAINDER_IS_ALREADY_CALCULATED
337             rem = (n1 - q * d0) & BN_MASK2;
338 #   endif
339
340 #   if defined(BN_UMULT_LOHI)
341             BN_UMULT_LOHI(t2l, t2h, d1, q);
342 #   elif defined(BN_UMULT_HIGH)
343             t2l = d1 * q;
344             t2h = BN_UMULT_HIGH(d1, q);
345 #   else
346             {
347                 BN_ULONG ql, qh;
348                 t2l = LBITS(d1);
349                 t2h = HBITS(d1);
350                 ql = LBITS(q);
351                 qh = HBITS(q);
352                 mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
353             }
354 #   endif
355
356             for (;;) {
357                 if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))
358                     break;
359                 q--;
360                 rem += d0;
361                 if (rem < d0)
362                     break;      /* don't let rem overflow */
363                 if (t2l < d1)
364                     t2h--;
365                 t2l -= d1;
366             }
367 #  endif                        /* !BN_LLONG */
368         }
369 # endif                         /* !BN_DIV3W */
370
371         l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
372         tmp->d[div_n] = l0;
373         wnum.d--;
374         /*
375          * ingore top values of the bignums just sub the two BN_ULONG arrays
376          * with bn_sub_words
377          */
378         if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
379             /*
380              * Note: As we have considered only the leading two BN_ULONGs in
381              * the calculation of q, sdiv * q might be greater than wnum (but
382              * then (q-1) * sdiv is less or equal than wnum)
383              */
384             q--;
385             if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))
386                 /*
387                  * we can't have an overflow here (assuming that q != 0, but
388                  * if q == 0 then tmp is zero anyway)
389                  */
390                 (*wnump)++;
391         }
392         /* store part of the result */
393         resp--;
394         *resp = q;
395     }
396     bn_correct_top(snum);
397     if (rm != NULL) {
398         /*
399          * Keep a copy of the neg flag in num because if rm==num BN_rshift()
400          * will overwrite it.
401          */
402         int neg = num->neg;
403         BN_rshift(rm, snum, norm_shift);
404         if (!BN_is_zero(rm))
405             rm->neg = neg;
406         bn_check_top(rm);
407     }
408     if (no_branch)
409         bn_correct_top(res);
410     BN_CTX_end(ctx);
411     return 1;
412  err:
413     bn_check_top(rm);
414     BN_CTX_end(ctx);
415     return (0);
416 }
417 #endif