bn_mp_from_ubin.c 876 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_FROM_UBIN_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* reads a unsigned char array, assumes the msb is stored first [big endian] */
  6. mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size)
  7. {
  8. mp_err err;
  9. /* make sure there are at least two digits */
  10. if (a->alloc < 2) {
  11. if ((err = mp_grow(a, 2)) != MP_OKAY) {
  12. return err;
  13. }
  14. }
  15. /* zero the int */
  16. mp_zero(a);
  17. /* read the bytes in */
  18. while (size-- > 0u) {
  19. if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) {
  20. return err;
  21. }
  22. #ifndef MP_8BIT
  23. a->dp[0] |= *buf++;
  24. a->used += 1;
  25. #else
  26. a->dp[0] = (*buf & MP_MASK);
  27. a->dp[1] |= ((*buf++ >> 7) & 1u);
  28. a->used += 2;
  29. #endif
  30. }
  31. mp_clamp(a);
  32. return MP_OKAY;
  33. }
  34. #endif