libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / hcrypto / libtommath / bn_mp_neg.c
blob264d90097cf57ea9bc50a8f576c8b695fbd7d570
1 #include "tommath_private.h"
2 #ifdef BN_MP_NEG_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* b = -a */
7 mp_err mp_neg(const mp_int *a, mp_int *b)
9 mp_err err;
10 if (a != b) {
11 if ((err = mp_copy(a, b)) != MP_OKAY) {
12 return err;
16 if (!MP_IS_ZERO(b)) {
17 b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
18 } else {
19 b->sign = MP_ZPOS;
22 return MP_OKAY;
24 #endif