der_put_multi_integer.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include <stdarg.h>
  12. #include "mycrypt.h"
  13. /* store multiple mp_ints in DER INTEGER format to the dst, will not
  14. * overflow the length you give it [outlen] and store the number of
  15. * bytes used in [outlen]
  16. */
  17. int der_put_multi_integer(unsigned char *dst, unsigned long *outlen,
  18. mp_int *num, ...)
  19. {
  20. va_list args;
  21. mp_int *next;
  22. unsigned long wrote, len;
  23. int err;
  24. _ARGCHK(dst != NULL);
  25. _ARGCHK(outlen != NULL);
  26. /* setup va list */
  27. next = num;
  28. len = *outlen;
  29. wrote = 0;
  30. va_start(args, num);
  31. while (next != NULL) {
  32. if ((err = der_encode_integer(next, dst, outlen)) != CRYPT_OK) {
  33. va_end(args);
  34. return err;
  35. }
  36. wrote += *outlen;
  37. dst += *outlen;
  38. len -= *outlen;
  39. *outlen = len;
  40. next = va_arg(args, mp_int*);
  41. }
  42. va_end(args);
  43. *outlen = wrote;
  44. return CRYPT_OK;
  45. }