rsa_verify_hash.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. /* design then PSS depad */
  14. int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
  15. const unsigned char *msghash, unsigned long msghashlen,
  16. prng_state *prng, int prng_idx,
  17. int hash_idx, unsigned long saltlen,
  18. int *stat, rsa_key *key)
  19. {
  20. unsigned long modulus_bitlen, modulus_bytelen, x;
  21. int err;
  22. unsigned char *tmpbuf;
  23. _ARGCHK(msghash != NULL);
  24. _ARGCHK(sig != NULL);
  25. _ARGCHK(stat != NULL);
  26. _ARGCHK(key != NULL);
  27. /* valid hash ? */
  28. if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
  29. return err;
  30. }
  31. if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
  32. return err;
  33. }
  34. /* get modulus len in bits */
  35. modulus_bitlen = mp_count_bits(&(key->N));
  36. /* outlen must be at least the size of the modulus */
  37. modulus_bytelen = mp_unsigned_bin_size(&(key->N));
  38. if (modulus_bytelen != siglen) {
  39. return CRYPT_INVALID_PACKET;
  40. }
  41. /* allocate temp buffer for decoded sig */
  42. tmpbuf = XMALLOC(siglen);
  43. if (tmpbuf == NULL) {
  44. return CRYPT_MEM;
  45. }
  46. /* RSA decode it */
  47. x = siglen;
  48. if ((err = rsa_exptmod(sig, siglen, tmpbuf, &x, PK_PUBLIC, prng, prng_idx, key)) != CRYPT_OK) {
  49. XFREE(tmpbuf);
  50. return err;
  51. }
  52. /* PSS decode it */
  53. err = pkcs_1_pss_decode(msghash, msghashlen, tmpbuf, x, saltlen, hash_idx, modulus_bitlen, stat);
  54. XFREE(tmpbuf);
  55. return err;
  56. }
  57. #endif /* MRSA */