/*
- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
#include <assert.h>
#include <openssl/bn.h>
#include "internal/cryptlib.h"
-#include "bn_lcl.h"
+#include "bn_local.h"
/* The old slow way */
#if 0
bn_check_top(m);
bn_check_top(d);
if (BN_is_zero(d)) {
- BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
+ ERR_raise(ERR_LIB_BN, BN_R_DIV_BY_ZERO);
return 0;
}
*/
# if BN_BITS2 == 64 && defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
# undef BN_ULLONG
-# define BN_ULLONG __uint128_t
+# define BN_ULLONG uint128_t
# define BN_LLONG
# endif
int ret;
if (BN_is_zero(divisor)) {
- BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
+ ERR_raise(ERR_LIB_BN, BN_R_DIV_BY_ZERO);
return 0;
}
* BN_DEBUG builds)
*/
if (divisor->d[divisor->top - 1] == 0) {
- BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
+ ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
return 0;
}
*
* - availability of constant-time bn_div_3_words;
* - dividend is at least as "wide" as divisor, limb-wise, zero-padded
- * if so requied, which shouldn't be a privacy problem, because
+ * if so required, which shouldn't be a privacy problem, because
* divisor's length is considered public;
*/
int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
BIGNUM *tmp, *snum, *sdiv, *res;
BN_ULONG *resp, *wnum, *wnumtop;
BN_ULONG d0, d1;
- int num_n, div_n;
+ int num_n, div_n, num_neg;
assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);
/* Setup quotient */
if (!bn_wexpand(res, loop))
goto err;
- res->neg = (num->neg ^ divisor->neg);
+ num_neg = num->neg;
+ res->neg = (num_neg ^ divisor->neg);
res->top = loop;
res->flags |= BN_FLG_FIXED_TOP;
resp = &(res->d[loop]);
*--resp = q;
}
/* snum holds remainder, it's as wide as divisor */
- snum->neg = num->neg;
+ snum->neg = num_neg;
snum->top = div_n;
snum->flags |= BN_FLG_FIXED_TOP;
- if (rm != NULL)
- bn_rshift_fixed_top(rm, snum, norm_shift);
+
+ if (rm != NULL && bn_rshift_fixed_top(rm, snum, norm_shift) == 0)
+ goto err;
+
BN_CTX_end(ctx);
return 1;
err: