pkcs_1_i2osp.c 934 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 "mycrypt.h"
  12. /* Integer to Octet I2OSP -- Tom St Denis */
  13. #ifdef PKCS_1
  14. /* always stores the same # of bytes, pads with leading zero bytes
  15. as required
  16. */
  17. int pkcs_1_i2osp(mp_int *n, unsigned long modulus_len, unsigned char *out)
  18. {
  19. int err;
  20. unsigned long size;
  21. size = mp_unsigned_bin_size(n);
  22. if (size > modulus_len) {
  23. return CRYPT_BUFFER_OVERFLOW;
  24. }
  25. /* store it */
  26. zeromem(out, modulus_len);
  27. if ((err = mp_to_unsigned_bin(n, out+(modulus_len-size))) != MP_OKAY) {
  28. return mpi_to_ltc_error(err);
  29. }
  30. return CRYPT_OK;
  31. }
  32. #endif /* PKCS_1 */