rsa_verify_hash.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, v2.0) de-sign 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. /* default to invalid */
  28. *stat = 0;
  29. /* valid hash ? */
  30. if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
  31. return err;
  32. }
  33. if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
  34. return err;
  35. }
  36. /* get modulus len in bits */
  37. modulus_bitlen = mp_count_bits(&(key->N));
  38. /* outlen must be at least the size of the modulus */
  39. modulus_bytelen = mp_unsigned_bin_size(&(key->N));
  40. if (modulus_bytelen != siglen) {
  41. return CRYPT_INVALID_PACKET;
  42. }
  43. /* allocate temp buffer for decoded sig */
  44. tmpbuf = XMALLOC(siglen);
  45. if (tmpbuf == NULL) {
  46. return CRYPT_MEM;
  47. }
  48. /* RSA decode it */
  49. x = siglen;
  50. if ((err = rsa_exptmod(sig, siglen, tmpbuf, &x, PK_PUBLIC, prng, prng_idx, key)) != CRYPT_OK) {
  51. XFREE(tmpbuf);
  52. return err;
  53. }
  54. /* PSS decode it */
  55. err = pkcs_1_pss_decode(msghash, msghashlen, tmpbuf, x, saltlen, hash_idx, modulus_bitlen, stat);
  56. XFREE(tmpbuf);
  57. return err;
  58. }
  59. #endif /* MRSA */