rsa_v15_sign_hash.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #ifdef MRSA
  13. /* PKCS #1 v1.5 pad then sign */
  14. int rsa_v15_sign_hash(const unsigned char *msghash, unsigned long msghashlen,
  15. unsigned char *sig, unsigned long *siglen,
  16. prng_state *prng, int prng_idx,
  17. int hash_idx, rsa_key *key)
  18. {
  19. unsigned long modulus_bitlen, modulus_bytelen, x;
  20. int err;
  21. _ARGCHK(msghash != NULL);
  22. _ARGCHK(sig != NULL);
  23. _ARGCHK(siglen != NULL);
  24. _ARGCHK(key != NULL);
  25. /* valid prng and hash ? */
  26. if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
  27. return err;
  28. }
  29. if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
  30. return err;
  31. }
  32. /* get modulus len in bits */
  33. modulus_bitlen = mp_count_bits(&(key->N));
  34. /* outlen must be at least the size of the modulus */
  35. modulus_bytelen = mp_unsigned_bin_size(&(key->N));
  36. if (modulus_bytelen > *siglen) {
  37. return CRYPT_BUFFER_OVERFLOW;
  38. }
  39. /* PKCS #1 v1.5 pad the key */
  40. x = *siglen;
  41. if ((err = pkcs_1_v15_sa_encode(msghash, msghashlen, hash_idx, modulus_bitlen, sig, &x)) != CRYPT_OK) {
  42. return err;
  43. }
  44. /* RSA encode it */
  45. return rsa_exptmod(sig, x, sig, siglen, PK_PRIVATE, prng, prng_idx, key);
  46. }
  47. #endif