xref: /freebsd/crypto/openssl/crypto/bn/bn_div.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <assert.h>
11 #include <openssl/bn.h>
12 #include "internal/cryptlib.h"
13 #include "bn_local.h"
14 
15 /* The old slow way */
16 #if 0
17 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
18            BN_CTX *ctx)
19 {
20     int i, nm, nd;
21     int ret = 0;
22     BIGNUM *D;
23 
24     bn_check_top(m);
25     bn_check_top(d);
26     if (BN_is_zero(d)) {
27         ERR_raise(ERR_LIB_BN, BN_R_DIV_BY_ZERO);
28         return 0;
29     }
30 
31     if (BN_ucmp(m, d) < 0) {
32         if (rem != NULL) {
33             if (BN_copy(rem, m) == NULL)
34                 return 0;
35         }
36         if (dv != NULL)
37             BN_zero(dv);
38         return 1;
39     }
40 
41     BN_CTX_start(ctx);
42     D = BN_CTX_get(ctx);
43     if (dv == NULL)
44         dv = BN_CTX_get(ctx);
45     if (rem == NULL)
46         rem = BN_CTX_get(ctx);
47     if (D == NULL || dv == NULL || rem == NULL)
48         goto end;
49 
50     nd = BN_num_bits(d);
51     nm = BN_num_bits(m);
52     if (BN_copy(D, d) == NULL)
53         goto end;
54     if (BN_copy(rem, m) == NULL)
55         goto end;
56 
57     /*
58      * The next 2 are needed so we can do a dv->d[0]|=1 later since
59      * BN_lshift1 will only work once there is a value :-)
60      */
61     BN_zero(dv);
62     if (bn_wexpand(dv, 1) == NULL)
63         goto end;
64     dv->top = 1;
65 
66     if (!BN_lshift(D, D, nm - nd))
67         goto end;
68     for (i = nm - nd; i >= 0; i--) {
69         if (!BN_lshift1(dv, dv))
70             goto end;
71         if (BN_ucmp(rem, D) >= 0) {
72             dv->d[0] |= 1;
73             if (!BN_usub(rem, rem, D))
74                 goto end;
75         }
76 /* CAN IMPROVE (and have now :=) */
77         if (!BN_rshift1(D, D))
78             goto end;
79     }
80     rem->neg = BN_is_zero(rem) ? 0 : m->neg;
81     dv->neg = m->neg ^ d->neg;
82     ret = 1;
83  end:
84     BN_CTX_end(ctx);
85     return ret;
86 }
87 
88 #else
89 
90 #if defined(BN_DIV3W)
91 BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0);
92 #elif 0
93 /*
94  * This is #if-ed away, because it's a reference for assembly implementations,
95  * where it can and should be made constant-time. But if you want to test it,
96  * just replace 0 with 1.
97  */
98 #if BN_BITS2 == 64 && defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16
99 #undef BN_ULLONG
100 #define BN_ULLONG uint128_t
101 #define BN_LLONG
102 #endif
103 
104 #ifdef BN_LLONG
105 #define BN_DIV3W
106 /*
107  * Interface is somewhat quirky, |m| is pointer to most significant limb,
108  * and less significant limb is referred at |m[-1]|. This means that caller
109  * is responsible for ensuring that |m[-1]| is valid. Second condition that
110  * has to be met is that |d0|'s most significant bit has to be set. Or in
111  * other words divisor has to be "bit-aligned to the left." bn_div_fixed_top
112  * does all this. The subroutine considers four limbs, two of which are
113  * "overlapping," hence the name...
114  */
115 static BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0)
116 {
117     BN_ULLONG R = ((BN_ULLONG)m[0] << BN_BITS2) | m[-1];
118     BN_ULLONG D = ((BN_ULLONG)d0 << BN_BITS2) | d1;
119     BN_ULONG Q = 0, mask;
120     int i;
121 
122     for (i = 0; i < BN_BITS2; i++) {
123         Q <<= 1;
124         if (R >= D) {
125             Q |= 1;
126             R -= D;
127         }
128         D >>= 1;
129     }
130 
131     mask = 0 - (Q >> (BN_BITS2 - 1)); /* does it overflow? */
132 
133     Q <<= 1;
134     Q |= (R >= D);
135 
136     return (Q | mask) & BN_MASK2;
137 }
138 #endif
139 #endif
140 
141 static int bn_left_align(BIGNUM *num)
142 {
143     BN_ULONG *d = num->d, n, m, rmask;
144     int top = num->top;
145     int rshift = BN_num_bits_word(d[top - 1]), lshift, i;
146 
147     lshift = BN_BITS2 - rshift;
148     rshift %= BN_BITS2; /* say no to undefined behaviour */
149     rmask = (BN_ULONG)0 - rshift; /* rmask = 0 - (rshift != 0) */
150     rmask |= rmask >> 8;
151 
152     for (i = 0, m = 0; i < top; i++) {
153         n = d[i];
154         d[i] = ((n << lshift) | m) & BN_MASK2;
155         m = (n >> rshift) & rmask;
156     }
157 
158     return lshift;
159 }
160 
161 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
162     && !defined(PEDANTIC) && !defined(BN_DIV3W)
163 #if defined(__GNUC__) && __GNUC__ >= 2
164 #if defined(__i386) || defined(__i386__)
165 /*-
166  * There were two reasons for implementing this template:
167  * - GNU C generates a call to a function (__udivdi3 to be exact)
168  *   in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
169  *   understand why...);
170  * - divl doesn't only calculate quotient, but also leaves
171  *   remainder in %edx which we can definitely use here:-)
172  */
173 #undef bn_div_words
174 #define bn_div_words(n0, n1, d0)        \
175     ({                                  \
176         asm volatile(                   \
177             "divl   %4"                 \
178             : "=a"(q), "=d"(rem)        \
179             : "a"(n1), "d"(n0), "r"(d0) \
180             : "cc");                    \
181         q;                              \
182     })
183 #define REMAINDER_IS_ALREADY_CALCULATED
184 #elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
185 /*
186  * Same story here, but it's 128-bit by 64-bit division. Wow!
187  */
188 #undef bn_div_words
189 #define bn_div_words(n0, n1, d0)        \
190     ({                                  \
191         asm volatile(                   \
192             "divq   %4"                 \
193             : "=a"(q), "=d"(rem)        \
194             : "a"(n1), "d"(n0), "r"(d0) \
195             : "cc");                    \
196         q;                              \
197     })
198 #define REMAINDER_IS_ALREADY_CALCULATED
199 #endif /* __<cpu> */
200 #endif /* __GNUC__ */
201 #endif /* OPENSSL_NO_ASM */
202 
203 /*-
204  * BN_div computes  dv := num / divisor, rounding towards
205  * zero, and sets up rm  such that  dv*divisor + rm = num  holds.
206  * Thus:
207  *     dv->neg == num->neg ^ divisor->neg  (unless the result is zero)
208  *     rm->neg == num->neg                 (unless the remainder is zero)
209  * If 'dv' or 'rm' is NULL, the respective value is not returned.
210  */
211 int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
212     BN_CTX *ctx)
213 {
214     int ret;
215 
216     if (BN_is_zero(divisor)) {
217         ERR_raise(ERR_LIB_BN, BN_R_DIV_BY_ZERO);
218         return 0;
219     }
220 
221     /*
222      * Invalid zero-padding would have particularly bad consequences so don't
223      * just rely on bn_check_top() here (bn_check_top() works only for
224      * BN_DEBUG builds)
225      */
226     if (divisor->d[divisor->top - 1] == 0) {
227         ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
228         return 0;
229     }
230 
231     ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);
232 
233     if (ret) {
234         if (dv != NULL)
235             bn_correct_top(dv);
236         if (rm != NULL)
237             bn_correct_top(rm);
238     }
239 
240     return ret;
241 }
242 
243 /*
244  * It's argued that *length* of *significant* part of divisor is public.
245  * Even if it's private modulus that is. Again, *length* is assumed
246  * public, but not *value*. Former is likely to be pre-defined by
247  * algorithm with bit granularity, though below subroutine is invariant
248  * of limb length. Thanks to this assumption we can require that |divisor|
249  * may not be zero-padded, yet claim this subroutine "constant-time"(*).
250  * This is because zero-padded dividend, |num|, is tolerated, so that
251  * caller can pass dividend of public length(*), but with smaller amount
252  * of significant limbs. This naturally means that quotient, |dv|, would
253  * contain correspongly less significant limbs as well, and will be zero-
254  * padded accordingly. Returned remainder, |rm|, will have same bit length
255  * as divisor, also zero-padded if needed. These actually leave sign bits
256  * in ambiguous state. In sense that we try to avoid negative zeros, while
257  * zero-padded zeros would retain sign.
258  *
259  * (*) "Constant-time-ness" has two pre-conditions:
260  *
261  *     - availability of constant-time bn_div_3_words;
262  *     - dividend is at least as "wide" as divisor, limb-wise, zero-padded
263  *       if so required, which shouldn't be a privacy problem, because
264  *       divisor's length is considered public;
265  */
266 int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
267     const BIGNUM *divisor, BN_CTX *ctx)
268 {
269     int norm_shift, i, j, loop;
270     BIGNUM *tmp, *snum, *sdiv, *res;
271     BN_ULONG *resp, *wnum, *wnumtop;
272     BN_ULONG d0, d1;
273     int num_n, div_n, num_neg;
274 
275     assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);
276 
277     bn_check_top(num);
278     bn_check_top(divisor);
279     bn_check_top(dv);
280     bn_check_top(rm);
281 
282     BN_CTX_start(ctx);
283     res = (dv == NULL) ? BN_CTX_get(ctx) : dv;
284     tmp = BN_CTX_get(ctx);
285     snum = BN_CTX_get(ctx);
286     sdiv = BN_CTX_get(ctx);
287     if (sdiv == NULL)
288         goto err;
289 
290     /* First we normalise the numbers */
291     if (!BN_copy(sdiv, divisor))
292         goto err;
293     norm_shift = bn_left_align(sdiv);
294     sdiv->neg = 0;
295     /*
296      * Note that bn_lshift_fixed_top's output is always one limb longer
297      * than input, even when norm_shift is zero. This means that amount of
298      * inner loop iterations is invariant of dividend value, and that one
299      * doesn't need to compare dividend and divisor if they were originally
300      * of the same bit length.
301      */
302     if (!(bn_lshift_fixed_top(snum, num, norm_shift)))
303         goto err;
304 
305     div_n = sdiv->top;
306     num_n = snum->top;
307 
308     if (num_n <= div_n) {
309         /* caller didn't pad dividend -> no constant-time guarantee... */
310         if (bn_wexpand(snum, div_n + 1) == NULL)
311             goto err;
312         memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));
313         snum->top = num_n = div_n + 1;
314     }
315 
316     loop = num_n - div_n;
317     /*
318      * Lets setup a 'window' into snum This is the part that corresponds to
319      * the current 'area' being divided
320      */
321     wnum = &(snum->d[loop]);
322     wnumtop = &(snum->d[num_n - 1]);
323 
324     /* Get the top 2 words of sdiv */
325     d0 = sdiv->d[div_n - 1];
326     d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
327 
328     /* Setup quotient */
329     if (!bn_wexpand(res, loop))
330         goto err;
331     num_neg = num->neg;
332     res->neg = (num_neg ^ divisor->neg);
333     res->top = loop;
334     res->flags |= BN_FLG_FIXED_TOP;
335     resp = &(res->d[loop]);
336 
337     /* space for temp */
338     if (!bn_wexpand(tmp, (div_n + 1)))
339         goto err;
340 
341     for (i = 0; i < loop; i++, wnumtop--) {
342         BN_ULONG q, l0;
343         /*
344          * the first part of the loop uses the top two words of snum and sdiv
345          * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
346          */
347 #if defined(BN_DIV3W)
348         q = bn_div_3_words(wnumtop, d1, d0);
349 #else
350         BN_ULONG n0, n1, rem = 0;
351 
352         n0 = wnumtop[0];
353         n1 = wnumtop[-1];
354         if (n0 == d0)
355             q = BN_MASK2;
356         else { /* n0 < d0 */
357             BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];
358 #ifdef BN_LLONG
359             BN_ULLONG t2;
360 
361 #if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
362             q = (BN_ULONG)(((((BN_ULLONG)n0) << BN_BITS2) | n1) / d0);
363 #else
364             q = bn_div_words(n0, n1, d0);
365 #endif
366 
367 #ifndef REMAINDER_IS_ALREADY_CALCULATED
368             /*
369              * rem doesn't have to be BN_ULLONG. The least we
370              * know it's less that d0, isn't it?
371              */
372             rem = (n1 - q * d0) & BN_MASK2;
373 #endif
374             t2 = (BN_ULLONG)d1 * q;
375 
376             for (;;) {
377                 if (t2 <= ((((BN_ULLONG)rem) << BN_BITS2) | n2))
378                     break;
379                 q--;
380                 rem += d0;
381                 if (rem < d0)
382                     break; /* don't let rem overflow */
383                 t2 -= d1;
384             }
385 #else /* !BN_LLONG */
386             BN_ULONG t2l, t2h;
387 
388             q = bn_div_words(n0, n1, d0);
389 #ifndef REMAINDER_IS_ALREADY_CALCULATED
390             rem = (n1 - q * d0) & BN_MASK2;
391 #endif
392 
393 #if defined(BN_UMULT_LOHI)
394             BN_UMULT_LOHI(t2l, t2h, d1, q);
395 #elif defined(BN_UMULT_HIGH)
396             t2l = d1 * q;
397             t2h = BN_UMULT_HIGH(d1, q);
398 #else
399             {
400                 BN_ULONG ql, qh;
401                 t2l = LBITS(d1);
402                 t2h = HBITS(d1);
403                 ql = LBITS(q);
404                 qh = HBITS(q);
405                 mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
406             }
407 #endif
408 
409             for (;;) {
410                 if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))
411                     break;
412                 q--;
413                 rem += d0;
414                 if (rem < d0)
415                     break; /* don't let rem overflow */
416                 if (t2l < d1)
417                     t2h--;
418                 t2l -= d1;
419             }
420 #endif /* !BN_LLONG */
421         }
422 #endif /* !BN_DIV3W */
423 
424         l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
425         tmp->d[div_n] = l0;
426         wnum--;
427         /*
428          * ignore top values of the bignums just sub the two BN_ULONG arrays
429          * with bn_sub_words
430          */
431         l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);
432         q -= l0;
433         /*
434          * Note: As we have considered only the leading two BN_ULONGs in
435          * the calculation of q, sdiv * q might be greater than wnum (but
436          * then (q-1) * sdiv is less or equal than wnum)
437          */
438         for (l0 = 0 - l0, j = 0; j < div_n; j++)
439             tmp->d[j] = sdiv->d[j] & l0;
440         l0 = bn_add_words(wnum, wnum, tmp->d, div_n);
441         (*wnumtop) += l0;
442         assert((*wnumtop) == 0);
443 
444         /* store part of the result */
445         *--resp = q;
446     }
447     /* snum holds remainder, it's as wide as divisor */
448     snum->neg = num_neg;
449     snum->top = div_n;
450     snum->flags |= BN_FLG_FIXED_TOP;
451 
452     if (rm != NULL && bn_rshift_fixed_top(rm, snum, norm_shift) == 0)
453         goto err;
454 
455     BN_CTX_end(ctx);
456     return 1;
457 err:
458     bn_check_top(rm);
459     BN_CTX_end(ctx);
460     return 0;
461 }
462 #endif
463