bn_mp_grow.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. /* grow as required */
  6. mp_err mp_grow(mp_int *a, int size)
  7. {
  8. int i;
  9. mp_digit *tmp;
  10. /* if the alloc size is smaller alloc more ram */
  11. if (a->alloc < size) {
  12. /* reallocate the array a->dp
  13. *
  14. * We store the return in a temporary variable
  15. * in case the operation failed we don't want
  16. * to overwrite the dp member of a.
  17. */
  18. tmp = (mp_digit *) MP_REALLOC(a->dp,
  19. (size_t)a->alloc * sizeof(mp_digit),
  20. (size_t)size * sizeof(mp_digit));
  21. if (tmp == NULL) {
  22. /* reallocation failed but "a" is still valid [can be freed] */
  23. return MP_MEM;
  24. }
  25. /* reallocation succeeded so set a->dp */
  26. a->dp = tmp;
  27. /* zero excess digits */
  28. i = a->alloc;
  29. a->alloc = size;
  30. MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
  31. }
  32. return MP_OKAY;
  33. }
  34. #endif