s4:heimdal: import lorikeet-heimdal-202201172009 (commit 5a0b45cd723628b3690ea848548b...
[samba.git] / source4 / heimdal / lib / hcrypto / libtommath / bn_mp_grow.c
1 #include "tommath_private.h"
2 #ifdef BN_MP_GROW_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5
6 /* grow as required */
7 mp_err mp_grow(mp_int *a, int size)
8 {
9    int     i;
10    mp_digit *tmp;
11
12    /* if the alloc size is smaller alloc more ram */
13    if (a->alloc < size) {
14       /* reallocate the array a->dp
15        *
16        * We store the return in a temporary variable
17        * in case the operation failed we don't want
18        * to overwrite the dp member of a.
19        */
20       tmp = (mp_digit *) MP_REALLOC(a->dp,
21                                     (size_t)a->alloc * sizeof(mp_digit),
22                                     (size_t)size * sizeof(mp_digit));
23       if (tmp == NULL) {
24          /* reallocation failed but "a" is still valid [can be freed] */
25          return MP_MEM;
26       }
27
28       /* reallocation succeeded so set a->dp */
29       a->dp = tmp;
30
31       /* zero excess digits */
32       i        = a->alloc;
33       a->alloc = size;
34       MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
35    }
36    return MP_OKAY;
37 }
38 #endif