pkcs_1_pss_decode.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /* PKCS #1 PSS Signature Padding -- Tom St Denis */
  13. #ifdef PKCS_1
  14. int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
  15. const unsigned char *sig, unsigned long siglen,
  16. unsigned long saltlen, int hash_idx,
  17. unsigned long modulus_bitlen, int *res)
  18. {
  19. unsigned char *DB, *mask, *salt, *hash;
  20. unsigned long x, y, hLen, modulus_len;
  21. int err;
  22. hash_state md;
  23. _ARGCHK(msghash != NULL);
  24. _ARGCHK(res != NULL);
  25. /* default to invalid */
  26. *res = 0;
  27. /* ensure hash is valid */
  28. if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
  29. return err;
  30. }
  31. hLen = hash_descriptor[hash_idx].hashsize;
  32. modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
  33. /* allocate ram for DB/mask/salt/hash of size modulus_len */
  34. DB = XMALLOC(modulus_len);
  35. mask = XMALLOC(modulus_len);
  36. salt = XMALLOC(modulus_len);
  37. hash = XMALLOC(modulus_len);
  38. if (DB == NULL || mask == NULL || salt == NULL || hash == NULL) {
  39. if (DB != NULL) {
  40. XFREE(DB);
  41. }
  42. if (mask != NULL) {
  43. XFREE(mask);
  44. }
  45. if (salt != NULL) {
  46. XFREE(salt);
  47. }
  48. if (hash != NULL) {
  49. XFREE(hash);
  50. }
  51. return CRYPT_MEM;
  52. }
  53. /* check sizes */
  54. if ((saltlen > modulus_len) ||
  55. (modulus_len < hLen + saltlen + 2) || (siglen != modulus_len)) {
  56. err = CRYPT_INVALID_ARG;
  57. goto __ERR;
  58. }
  59. /* ensure the 0xBC byte */
  60. if (sig[siglen-1] != 0xBC) {
  61. err = CRYPT_OK;
  62. goto __ERR;
  63. }
  64. /* copy out the DB */
  65. for (x = 0; x < modulus_len - hLen - 1; x++) {
  66. DB[x] = sig[x];
  67. }
  68. /* copy out the hash */
  69. for (y = 0; y < hLen; y++) {
  70. hash[y] = sig[x++];
  71. }
  72. /* check the MSB */
  73. if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen-1)))) != 0) {
  74. err = CRYPT_OK;
  75. goto __ERR;
  76. }
  77. /* generate mask of length modulus_len - hLen - 1 from hash */
  78. if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
  79. goto __ERR;
  80. }
  81. /* xor against DB */
  82. for (y = 0; y < (modulus_len - hLen - 1); y++) {
  83. DB[y] ^= mask[y];
  84. }
  85. /* now clear the first byte [make sure smaller than modulus] */
  86. DB[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen-1));
  87. /* DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */
  88. /* check for zeroes and 0x01 */
  89. for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) {
  90. if (DB[x] != 0x00) {
  91. err = CRYPT_OK;
  92. goto __ERR;
  93. }
  94. }
  95. /* check for the 0x01 */
  96. if (DB[x++] != 0x01) {
  97. err = CRYPT_OK;
  98. goto __ERR;
  99. }
  100. /* M = (eight) 0x00 || msghash || salt, mask = H(M) */
  101. hash_descriptor[hash_idx].init(&md);
  102. zeromem(mask, 8);
  103. if ((err = hash_descriptor[hash_idx].process(&md, mask, 8)) != CRYPT_OK) {
  104. goto __ERR;
  105. }
  106. if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) {
  107. goto __ERR;
  108. }
  109. if ((err = hash_descriptor[hash_idx].process(&md, DB+x, saltlen)) != CRYPT_OK) {
  110. goto __ERR;
  111. }
  112. if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) {
  113. goto __ERR;
  114. }
  115. /* mask == hash means valid signature */
  116. if (memcmp(mask, hash, hLen) == 0) {
  117. *res = 1;
  118. }
  119. err = CRYPT_OK;
  120. __ERR:
  121. #ifdef CLEAN_STACK
  122. zeromem(DB, modulus_len);
  123. zeromem(mask, modulus_len);
  124. zeromem(salt, modulus_len);
  125. zeromem(hash, modulus_len);
  126. #endif
  127. XFREE(hash);
  128. XFREE(salt);
  129. XFREE(mask);
  130. XFREE(DB);
  131. return err;
  132. }
  133. #endif /* PKCS_1 */