rsa_sign_hash.c 1.7 KB

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