1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file pkcs_1_pss_decode.c
13 PKCS #1 PSS Signature Padding, Tom St Denis
19 PKCS #1 v2.00 PSS decode
20 @param msghash The hash to verify
21 @param msghashlen The length of the hash (octets)
22 @param sig The signature data (encoded data)
23 @param siglen The length of the signature data (octets)
24 @param saltlen The length of the salt used (octets)
25 @param hash_idx The index of the hash desired
26 @param modulus_bitlen The bit length of the RSA modulus
27 @param res [out] The result of the comparison, 1==valid, 0==invalid
28 @return CRYPT_OK if successful (even if the comparison failed)
30 int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
31 const unsigned char *sig, unsigned long siglen,
32 unsigned long saltlen, int hash_idx,
33 unsigned long modulus_bitlen, int *res)
35 unsigned char *DB, *mask, *salt, *hash;
36 unsigned long x, y, hLen, modulus_len;
40 LTC_ARGCHK(msghash != NULL);
41 LTC_ARGCHK(res != NULL);
43 /* default to invalid */
46 /* ensure hash is valid */
47 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
51 hLen = hash_descriptor[hash_idx].hashsize;
53 modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
56 if ((saltlen > modulus_len) ||
57 (modulus_len < hLen + saltlen + 2)) {
58 return CRYPT_PK_INVALID_SIZE;
61 /* allocate ram for DB/mask/salt/hash of size modulus_len */
62 DB = XMALLOC(modulus_len);
63 mask = XMALLOC(modulus_len);
64 salt = XMALLOC(modulus_len);
65 hash = XMALLOC(modulus_len);
66 if (DB == NULL || mask == NULL || salt == NULL || hash == NULL) {
82 /* ensure the 0xBC byte */
83 if (sig[siglen-1] != 0xBC) {
84 err = CRYPT_INVALID_PACKET;
90 XMEMCPY(DB, sig + x, modulus_len - hLen - 1);
91 x += modulus_len - hLen - 1;
93 /* copy out the hash */
94 XMEMCPY(hash, sig + x, hLen);
98 if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen)))) != 0) {
99 err = CRYPT_INVALID_PACKET;
103 /* generate mask of length modulus_len - hLen - 1 from hash */
104 if ((err = pkcs_1_mgf1(hash_idx, hash, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
109 for (y = 0; y < (modulus_len - hLen - 1); y++) {
113 /* now clear the first byte [make sure smaller than modulus] */
114 DB[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen));
116 /* DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */
118 /* check for zeroes and 0x01 */
119 for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) {
121 err = CRYPT_INVALID_PACKET;
126 /* check for the 0x01 */
127 if (DB[x++] != 0x01) {
128 err = CRYPT_INVALID_PACKET;
132 /* M = (eight) 0x00 || msghash || salt, mask = H(M) */
133 if ((err = hash_descriptor[hash_idx].init(&md)) != CRYPT_OK) {
137 if ((err = hash_descriptor[hash_idx].process(&md, mask, 8)) != CRYPT_OK) {
140 if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) {
143 if ((err = hash_descriptor[hash_idx].process(&md, DB+x, saltlen)) != CRYPT_OK) {
146 if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) {
150 /* mask == hash means valid signature */
151 if (XMEM_NEQ(mask, hash, hLen) == 0) {
157 #ifdef LTC_CLEAN_STACK
158 zeromem(DB, modulus_len);
159 zeromem(mask, modulus_len);
160 zeromem(salt, modulus_len);
161 zeromem(hash, modulus_len);
172 #endif /* LTC_PKCS_1 */
174 /* ref: $Format:%D$ */
175 /* git commit: $Format:%H$ */
176 /* commit time: $Format:%ai$ */