bn_mp_to_sbin.c 606 B

12345678910111213141516171819202122
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_TO_SBIN_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* store in signed [big endian] format */
  6. mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
  7. {
  8. mp_err err;
  9. if (maxlen == 0u) {
  10. return MP_BUF;
  11. }
  12. if ((err = mp_to_ubin(a, buf + 1, maxlen - 1u, written)) != MP_OKAY) {
  13. return err;
  14. }
  15. if (written != NULL) {
  16. (*written)++;
  17. }
  18. buf[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
  19. return MP_OKAY;
  20. }
  21. #endif