]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_div.c
2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/bn.h>
11 #include "internal/cryptlib.h"
14 /* The old slow way */
16 int BN_div(BIGNUM
*dv
, BIGNUM
*rem
, const BIGNUM
*m
, const BIGNUM
*d
,
26 BNerr(BN_F_BN_DIV
, BN_R_DIV_BY_ZERO
);
30 if (BN_ucmp(m
, d
) < 0) {
32 if (BN_copy(rem
, m
) == NULL
)
45 rem
= BN_CTX_get(ctx
);
46 if (D
== NULL
|| dv
== NULL
|| rem
== NULL
)
51 if (BN_copy(D
, d
) == NULL
)
53 if (BN_copy(rem
, m
) == NULL
)
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 :-)
61 if (bn_wexpand(dv
, 1) == NULL
)
65 if (!BN_lshift(D
, D
, nm
- nd
))
67 for (i
= nm
- nd
; i
>= 0; i
--) {
68 if (!BN_lshift1(dv
, dv
))
70 if (BN_ucmp(rem
, D
) >= 0) {
72 if (!BN_usub(rem
, rem
, D
))
75 /* CAN IMPROVE (and have now :=) */
76 if (!BN_rshift1(D
, D
))
79 rem
->neg
= BN_is_zero(rem
) ? 0 : m
->neg
;
80 dv
->neg
= m
->neg
^ d
->neg
;
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__)
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
98 * - divl doesn't only calculate quotient, but also leaves
99 * remainder in %edx which we can definitely use here:-)
101 * <appro@fy.chalmers.se>
104 # define bn_div_words(n0,n1,d0) \
107 : "=a"(q), "=d"(rem) \
108 : "a"(n1), "d"(n0), "r"(d0) \
112 # define REMAINDER_IS_ALREADY_CALCULATED
113 # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
115 * Same story here, but it's 128-bit by 64-bit division. Wow!
116 * <appro@fy.chalmers.se>
119 # define bn_div_words(n0,n1,d0) \
122 : "=a"(q), "=d"(rem) \
123 : "a"(n1), "d"(n0), "r"(d0) \
127 # define REMAINDER_IS_ALREADY_CALCULATED
128 # endif /* __<cpu> */
129 # endif /* __GNUC__ */
130 # endif /* OPENSSL_NO_ASM */
133 * BN_div computes dv := num / divisor, rounding towards
134 * zero, and sets up rm such that dv*divisor + rm = num holds.
136 * dv->neg == num->neg ^ divisor->neg (unless the result is zero)
137 * rm->neg == num->neg (unless the remainder is zero)
138 * If 'dv' or 'rm' is NULL, the respective value is not returned.
140 int BN_div(BIGNUM
*dv
, BIGNUM
*rm
, const BIGNUM
*num
, const BIGNUM
*divisor
,
143 int norm_shift
, i
, loop
;
144 BIGNUM
*tmp
, wnum
, *snum
, *sdiv
, *res
;
145 BN_ULONG
*resp
, *wnump
;
151 * Invalid zero-padding would have particularly bad consequences so don't
152 * just rely on bn_check_top() here (bn_check_top() works only for
155 if ((num
->top
> 0 && num
->d
[num
->top
- 1] == 0) ||
156 (divisor
->top
> 0 && divisor
->d
[divisor
->top
- 1] == 0)) {
157 BNerr(BN_F_BN_DIV
, BN_R_NOT_INITIALIZED
);
162 bn_check_top(divisor
);
164 if ((BN_get_flags(num
, BN_FLG_CONSTTIME
) != 0)
165 || (BN_get_flags(divisor
, BN_FLG_CONSTTIME
) != 0)) {
171 /*- bn_check_top(num); *//*
172 * 'num' has been checked already
174 /*- bn_check_top(divisor); *//*
175 * 'divisor' has been checked already
178 if (BN_is_zero(divisor
)) {
179 BNerr(BN_F_BN_DIV
, BN_R_DIV_BY_ZERO
);
183 if (!no_branch
&& BN_ucmp(num
, divisor
) < 0) {
185 if (BN_copy(rm
, num
) == NULL
)
194 res
= (dv
== NULL
) ? BN_CTX_get(ctx
) : dv
;
195 tmp
= BN_CTX_get(ctx
);
196 snum
= BN_CTX_get(ctx
);
197 sdiv
= BN_CTX_get(ctx
);
201 /* First we normalise the numbers */
202 norm_shift
= BN_BITS2
- ((BN_num_bits(divisor
)) % BN_BITS2
);
203 if (!(BN_lshift(sdiv
, divisor
, norm_shift
)))
206 norm_shift
+= BN_BITS2
;
207 if (!(BN_lshift(snum
, num
, norm_shift
)))
213 * Since we don't know whether snum is larger than sdiv, we pad snum
214 * with enough zeroes without changing its value.
216 if (snum
->top
<= sdiv
->top
+ 1) {
217 if (bn_wexpand(snum
, sdiv
->top
+ 2) == NULL
)
219 for (i
= snum
->top
; i
< sdiv
->top
+ 2; i
++)
221 snum
->top
= sdiv
->top
+ 2;
223 if (bn_wexpand(snum
, snum
->top
+ 1) == NULL
)
225 snum
->d
[snum
->top
] = 0;
232 loop
= num_n
- div_n
;
234 * Lets setup a 'window' into snum This is the part that corresponds to
235 * the current 'area' being divided
238 wnum
.d
= &(snum
->d
[loop
]);
241 * only needed when BN_ucmp messes up the values between top and max
243 wnum
.dmax
= snum
->dmax
- loop
; /* so we don't step out of bounds */
245 /* Get the top 2 words of sdiv */
246 /* div_n=sdiv->top; */
247 d0
= sdiv
->d
[div_n
- 1];
248 d1
= (div_n
== 1) ? 0 : sdiv
->d
[div_n
- 2];
250 /* pointer to the 'top' of snum */
251 wnump
= &(snum
->d
[num_n
- 1]);
254 if (!bn_wexpand(res
, (loop
+ 1)))
256 res
->neg
= (num
->neg
^ divisor
->neg
);
257 res
->top
= loop
- no_branch
;
258 resp
= &(res
->d
[loop
- 1]);
261 if (!bn_wexpand(tmp
, (div_n
+ 1)))
265 if (BN_ucmp(&wnum
, sdiv
) >= 0) {
267 * If BN_DEBUG_RAND is defined BN_ucmp changes (via bn_pollute)
268 * the const bignum arguments => clean the values between top and
271 bn_clear_top2max(&wnum
);
272 bn_sub_words(wnum
.d
, wnum
.d
, sdiv
->d
, div_n
);
278 /* Increase the resp pointer so that we never create an invalid pointer. */
282 * if res->top == 0 then clear the neg value otherwise decrease the resp
290 for (i
= 0; i
< loop
- 1; i
++, wnump
--) {
293 * the first part of the loop uses the top two words of snum and sdiv
294 * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
296 # if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
297 BN_ULONG
bn_div_3_words(BN_ULONG
*, BN_ULONG
, BN_ULONG
);
298 q
= bn_div_3_words(wnump
, d1
, d0
);
300 BN_ULONG n0
, n1
, rem
= 0;
311 # if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
312 q
= (BN_ULONG
)(((((BN_ULLONG
) n0
) << BN_BITS2
) | n1
) / d0
);
314 q
= bn_div_words(n0
, n1
, d0
);
317 # ifndef REMAINDER_IS_ALREADY_CALCULATED
319 * rem doesn't have to be BN_ULLONG. The least we
320 * know it's less that d0, isn't it?
322 rem
= (n1
- q
* d0
) & BN_MASK2
;
324 t2
= (BN_ULLONG
) d1
*q
;
327 if (t2
<= ((((BN_ULLONG
) rem
) << BN_BITS2
) | wnump
[-2]))
332 break; /* don't let rem overflow */
335 # else /* !BN_LLONG */
338 q
= bn_div_words(n0
, n1
, d0
);
339 # ifndef REMAINDER_IS_ALREADY_CALCULATED
340 rem
= (n1
- q
* d0
) & BN_MASK2
;
343 # if defined(BN_UMULT_LOHI)
344 BN_UMULT_LOHI(t2l
, t2h
, d1
, q
);
345 # elif defined(BN_UMULT_HIGH)
347 t2h
= BN_UMULT_HIGH(d1
, q
);
355 mul64(t2l
, t2h
, ql
, qh
); /* t2=(BN_ULLONG)d1*q; */
360 if ((t2h
< rem
) || ((t2h
== rem
) && (t2l
<= wnump
[-2])))
365 break; /* don't let rem overflow */
370 # endif /* !BN_LLONG */
372 # endif /* !BN_DIV3W */
374 l0
= bn_mul_words(tmp
->d
, sdiv
->d
, div_n
, q
);
378 * ingore top values of the bignums just sub the two BN_ULONG arrays
381 if (bn_sub_words(wnum
.d
, wnum
.d
, tmp
->d
, div_n
+ 1)) {
383 * Note: As we have considered only the leading two BN_ULONGs in
384 * the calculation of q, sdiv * q might be greater than wnum (but
385 * then (q-1) * sdiv is less or equal than wnum)
388 if (bn_add_words(wnum
.d
, wnum
.d
, sdiv
->d
, div_n
))
390 * we can't have an overflow here (assuming that q != 0, but
391 * if q == 0 then tmp is zero anyway)
395 /* store part of the result */
399 bn_correct_top(snum
);
402 * Keep a copy of the neg flag in num because if rm==num BN_rshift()
406 BN_rshift(rm
, snum
, norm_shift
);