]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_div.c
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/bn.h>
12 #include "internal/cryptlib.h"
15 /* The old slow way */
17 int BN_div(BIGNUM
*dv
, BIGNUM
*rem
, const BIGNUM
*m
, const BIGNUM
*d
,
27 BNerr(BN_F_BN_DIV
, BN_R_DIV_BY_ZERO
);
31 if (BN_ucmp(m
, d
) < 0) {
33 if (BN_copy(rem
, m
) == NULL
)
46 rem
= BN_CTX_get(ctx
);
47 if (D
== NULL
|| dv
== NULL
|| rem
== NULL
)
52 if (BN_copy(D
, d
) == NULL
)
54 if (BN_copy(rem
, m
) == NULL
)
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 :-)
62 if (bn_wexpand(dv
, 1) == NULL
)
66 if (!BN_lshift(D
, D
, nm
- nd
))
68 for (i
= nm
- nd
; i
>= 0; i
--) {
69 if (!BN_lshift1(dv
, dv
))
71 if (BN_ucmp(rem
, D
) >= 0) {
73 if (!BN_usub(rem
, rem
, D
))
76 /* CAN IMPROVE (and have now :=) */
77 if (!BN_rshift1(D
, D
))
80 rem
->neg
= BN_is_zero(rem
) ? 0 : m
->neg
;
81 dv
->neg
= m
->neg
^ d
->neg
;
90 # if defined(BN_DIV3W)
91 BN_ULONG
bn_div_3_words(const BN_ULONG
*m
, BN_ULONG d1
, BN_ULONG d0
);
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.
98 # if BN_BITS2 == 64 && defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
100 # define BN_ULLONG __uint128_t
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...
115 static BN_ULONG
bn_div_3_words(const BN_ULONG
*m
, BN_ULONG d1
, BN_ULONG d0
)
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
;
122 for (i
= 0; i
< BN_BITS2
; i
++) {
131 mask
= 0 - (Q
>> (BN_BITS2
- 1)); /* does it overflow? */
136 return (Q
| mask
) & BN_MASK2
;
141 static int bn_left_align(BIGNUM
*num
)
143 BN_ULONG
*d
= num
->d
, n
, m
, rmask
;
145 int rshift
= BN_num_bits_word(d
[top
- 1]), lshift
, i
;
147 lshift
= BN_BITS2
- rshift
;
148 rshift
%= BN_BITS2
; /* say no to undefined behaviour */
149 rmask
= (BN_ULONG
)0 - rshift
; /* rmask = 0 - (rshift != 0) */
152 for (i
= 0, m
= 0; i
< top
; i
++) {
154 d
[i
] = ((n
<< lshift
) | m
) & BN_MASK2
;
155 m
= (n
>> rshift
) & rmask
;
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__)
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:-)
174 # define bn_div_words(n0,n1,d0) \
177 : "=a"(q), "=d"(rem) \
178 : "a"(n1), "d"(n0), "r"(d0) \
182 # define REMAINDER_IS_ALREADY_CALCULATED
183 # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
185 * Same story here, but it's 128-bit by 64-bit division. Wow!
188 # define bn_div_words(n0,n1,d0) \
191 : "=a"(q), "=d"(rem) \
192 : "a"(n1), "d"(n0), "r"(d0) \
196 # define REMAINDER_IS_ALREADY_CALCULATED
197 # endif /* __<cpu> */
198 # endif /* __GNUC__ */
199 # endif /* OPENSSL_NO_ASM */
202 * BN_div computes dv := num / divisor, rounding towards
203 * zero, and sets up rm such that dv*divisor + rm = num holds.
205 * dv->neg == num->neg ^ divisor->neg (unless the result is zero)
206 * rm->neg == num->neg (unless the remainder is zero)
207 * If 'dv' or 'rm' is NULL, the respective value is not returned.
209 int BN_div(BIGNUM
*dv
, BIGNUM
*rm
, const BIGNUM
*num
, const BIGNUM
*divisor
,
214 if (BN_is_zero(divisor
)) {
215 BNerr(BN_F_BN_DIV
, BN_R_DIV_BY_ZERO
);
220 * Invalid zero-padding would have particularly bad consequences so don't
221 * just rely on bn_check_top() here (bn_check_top() works only for
224 if (divisor
->d
[divisor
->top
- 1] == 0) {
225 BNerr(BN_F_BN_DIV
, BN_R_NOT_INITIALIZED
);
229 ret
= bn_div_fixed_top(dv
, rm
, num
, divisor
, ctx
);
242 * It's argued that *length* of *significant* part of divisor is public.
243 * Even if it's private modulus that is. Again, *length* is assumed
244 * public, but not *value*. Former is likely to be pre-defined by
245 * algorithm with bit granularity, though below subroutine is invariant
246 * of limb length. Thanks to this assumption we can require that |divisor|
247 * may not be zero-padded, yet claim this subroutine "constant-time"(*).
248 * This is because zero-padded dividend, |num|, is tolerated, so that
249 * caller can pass dividend of public length(*), but with smaller amount
250 * of significant limbs. This naturally means that quotient, |dv|, would
251 * contain correspongly less significant limbs as well, and will be zero-
252 * padded accordingly. Returned remainder, |rm|, will have same bit length
253 * as divisor, also zero-padded if needed. These actually leave sign bits
254 * in ambiguous state. In sense that we try to avoid negative zeros, while
255 * zero-padded zeros would retain sign.
257 * (*) "Constant-time-ness" has two pre-conditions:
259 * - availability of constant-time bn_div_3_words;
260 * - dividend is at least as "wide" as divisor, limb-wise, zero-padded
261 * if so required, which shouldn't be a privacy problem, because
262 * divisor's length is considered public;
264 int bn_div_fixed_top(BIGNUM
*dv
, BIGNUM
*rm
, const BIGNUM
*num
,
265 const BIGNUM
*divisor
, BN_CTX
*ctx
)
267 int norm_shift
, i
, j
, loop
;
268 BIGNUM
*tmp
, *snum
, *sdiv
, *res
;
269 BN_ULONG
*resp
, *wnum
, *wnumtop
;
273 assert(divisor
->top
> 0 && divisor
->d
[divisor
->top
- 1] != 0);
276 bn_check_top(divisor
);
281 res
= (dv
== NULL
) ? BN_CTX_get(ctx
) : dv
;
282 tmp
= BN_CTX_get(ctx
);
283 snum
= BN_CTX_get(ctx
);
284 sdiv
= BN_CTX_get(ctx
);
288 /* First we normalise the numbers */
289 if (!BN_copy(sdiv
, divisor
))
291 norm_shift
= bn_left_align(sdiv
);
294 * Note that bn_lshift_fixed_top's output is always one limb longer
295 * than input, even when norm_shift is zero. This means that amount of
296 * inner loop iterations is invariant of dividend value, and that one
297 * doesn't need to compare dividend and divisor if they were originally
298 * of the same bit length.
300 if (!(bn_lshift_fixed_top(snum
, num
, norm_shift
)))
306 if (num_n
<= div_n
) {
307 /* caller didn't pad dividend -> no constant-time guarantee... */
308 if (bn_wexpand(snum
, div_n
+ 1) == NULL
)
310 memset(&(snum
->d
[num_n
]), 0, (div_n
- num_n
+ 1) * sizeof(BN_ULONG
));
311 snum
->top
= num_n
= div_n
+ 1;
314 loop
= num_n
- div_n
;
316 * Lets setup a 'window' into snum This is the part that corresponds to
317 * the current 'area' being divided
319 wnum
= &(snum
->d
[loop
]);
320 wnumtop
= &(snum
->d
[num_n
- 1]);
322 /* Get the top 2 words of sdiv */
323 d0
= sdiv
->d
[div_n
- 1];
324 d1
= (div_n
== 1) ? 0 : sdiv
->d
[div_n
- 2];
327 if (!bn_wexpand(res
, loop
))
329 res
->neg
= (num
->neg
^ divisor
->neg
);
331 res
->flags
|= BN_FLG_FIXED_TOP
;
332 resp
= &(res
->d
[loop
]);
335 if (!bn_wexpand(tmp
, (div_n
+ 1)))
338 for (i
= 0; i
< loop
; i
++, wnumtop
--) {
341 * the first part of the loop uses the top two words of snum and sdiv
342 * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
344 # if defined(BN_DIV3W)
345 q
= bn_div_3_words(wnumtop
, d1
, d0
);
347 BN_ULONG n0
, n1
, rem
= 0;
354 BN_ULONG n2
= (wnumtop
== wnum
) ? 0 : wnumtop
[-2];
358 # if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
359 q
= (BN_ULONG
)(((((BN_ULLONG
) n0
) << BN_BITS2
) | n1
) / d0
);
361 q
= bn_div_words(n0
, n1
, d0
);
364 # ifndef REMAINDER_IS_ALREADY_CALCULATED
366 * rem doesn't have to be BN_ULLONG. The least we
367 * know it's less that d0, isn't it?
369 rem
= (n1
- q
* d0
) & BN_MASK2
;
371 t2
= (BN_ULLONG
) d1
*q
;
374 if (t2
<= ((((BN_ULLONG
) rem
) << BN_BITS2
) | n2
))
379 break; /* don't let rem overflow */
382 # else /* !BN_LLONG */
385 q
= bn_div_words(n0
, n1
, d0
);
386 # ifndef REMAINDER_IS_ALREADY_CALCULATED
387 rem
= (n1
- q
* d0
) & BN_MASK2
;
390 # if defined(BN_UMULT_LOHI)
391 BN_UMULT_LOHI(t2l
, t2h
, d1
, q
);
392 # elif defined(BN_UMULT_HIGH)
394 t2h
= BN_UMULT_HIGH(d1
, q
);
402 mul64(t2l
, t2h
, ql
, qh
); /* t2=(BN_ULLONG)d1*q; */
407 if ((t2h
< rem
) || ((t2h
== rem
) && (t2l
<= n2
)))
412 break; /* don't let rem overflow */
417 # endif /* !BN_LLONG */
419 # endif /* !BN_DIV3W */
421 l0
= bn_mul_words(tmp
->d
, sdiv
->d
, div_n
, q
);
425 * ignore top values of the bignums just sub the two BN_ULONG arrays
428 l0
= bn_sub_words(wnum
, wnum
, tmp
->d
, div_n
+ 1);
431 * Note: As we have considered only the leading two BN_ULONGs in
432 * the calculation of q, sdiv * q might be greater than wnum (but
433 * then (q-1) * sdiv is less or equal than wnum)
435 for (l0
= 0 - l0
, j
= 0; j
< div_n
; j
++)
436 tmp
->d
[j
] = sdiv
->d
[j
] & l0
;
437 l0
= bn_add_words(wnum
, wnum
, tmp
->d
, div_n
);
439 assert((*wnumtop
) == 0);
441 /* store part of the result */
444 /* snum holds remainder, it's as wide as divisor */
445 snum
->neg
= num
->neg
;
447 snum
->flags
|= BN_FLG_FIXED_TOP
;
449 bn_rshift_fixed_top(rm
, snum
, norm_shift
);