bn_mp_from_sbin.c 635 B

12345678910111213141516171819202122232425
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_FROM_SBIN_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* read signed bin, big endian, first byte is 0==positive or 1==negative */
  6. mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size)
  7. {
  8. mp_err err;
  9. /* read magnitude */
  10. if ((err = mp_from_ubin(a, buf + 1, size - 1u)) != MP_OKAY) {
  11. return err;
  12. }
  13. /* first byte is 0 for positive, non-zero for negative */
  14. if (buf[0] == (unsigned char)0) {
  15. a->sign = MP_ZPOS;
  16. } else {
  17. a->sign = MP_NEG;
  18. }
  19. return MP_OKAY;
  20. }
  21. #endif