Mercurial > dropbear
comparison libtomcrypt/src/pk/pkcs1/pkcs_1_pss_decode.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 | 10 |
13 /** | 11 /** |
14 @file pkcs_1_pss_decode.c | 12 @file pkcs_1_pss_decode.c |
15 LTC_PKCS #1 PSS Signature Padding, Tom St Denis | 13 PKCS #1 PSS Signature Padding, Tom St Denis |
16 */ | 14 */ |
17 | 15 |
18 #ifdef LTC_PKCS_1 | 16 #ifdef LTC_PKCS_1 |
19 | 17 |
20 /** | 18 /** |
21 LTC_PKCS #1 v2.00 PSS decode | 19 PKCS #1 v2.00 PSS decode |
22 @param msghash The hash to verify | 20 @param msghash The hash to verify |
23 @param msghashlen The length of the hash (octets) | 21 @param msghashlen The length of the hash (octets) |
24 @param sig The signature data (encoded data) | 22 @param sig The signature data (encoded data) |
25 @param siglen The length of the signature data (octets) | 23 @param siglen The length of the signature data (octets) |
26 @param saltlen The length of the salt used (octets) | 24 @param saltlen The length of the salt used (octets) |
49 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { | 47 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
50 return err; | 48 return err; |
51 } | 49 } |
52 | 50 |
53 hLen = hash_descriptor[hash_idx].hashsize; | 51 hLen = hash_descriptor[hash_idx].hashsize; |
52 modulus_bitlen--; | |
54 modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); | 53 modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); |
55 | 54 |
56 /* check sizes */ | 55 /* check sizes */ |
57 if ((saltlen > modulus_len) || | 56 if ((saltlen > modulus_len) || |
58 (modulus_len < hLen + saltlen + 2) || (siglen != modulus_len)) { | 57 (modulus_len < hLen + saltlen + 2)) { |
59 return CRYPT_PK_INVALID_SIZE; | 58 return CRYPT_PK_INVALID_SIZE; |
60 } | 59 } |
61 | 60 |
62 /* allocate ram for DB/mask/salt/hash of size modulus_len */ | 61 /* allocate ram for DB/mask/salt/hash of size modulus_len */ |
63 DB = XMALLOC(modulus_len); | 62 DB = XMALLOC(modulus_len); |
91 XMEMCPY(DB, sig + x, modulus_len - hLen - 1); | 90 XMEMCPY(DB, sig + x, modulus_len - hLen - 1); |
92 x += modulus_len - hLen - 1; | 91 x += modulus_len - hLen - 1; |
93 | 92 |
94 /* copy out the hash */ | 93 /* copy out the hash */ |
95 XMEMCPY(hash, sig + x, hLen); | 94 XMEMCPY(hash, sig + x, hLen); |
96 x += hLen; | 95 /* x += hLen; */ |
97 | 96 |
98 /* check the MSB */ | 97 /* check the MSB */ |
99 if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen-1)))) != 0) { | 98 if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen)))) != 0) { |
100 err = CRYPT_INVALID_PACKET; | 99 err = CRYPT_INVALID_PACKET; |
101 goto LBL_ERR; | 100 goto LBL_ERR; |
102 } | 101 } |
103 | 102 |
104 /* generate mask of length modulus_len - hLen - 1 from hash */ | 103 /* generate mask of length modulus_len - hLen - 1 from hash */ |
108 | 107 |
109 /* xor against DB */ | 108 /* xor against DB */ |
110 for (y = 0; y < (modulus_len - hLen - 1); y++) { | 109 for (y = 0; y < (modulus_len - hLen - 1); y++) { |
111 DB[y] ^= mask[y]; | 110 DB[y] ^= mask[y]; |
112 } | 111 } |
113 | 112 |
114 /* now clear the first byte [make sure smaller than modulus] */ | 113 /* now clear the first byte [make sure smaller than modulus] */ |
115 DB[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen-1)); | 114 DB[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen)); |
116 | 115 |
117 /* DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */ | 116 /* DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */ |
118 | 117 |
119 /* check for zeroes and 0x01 */ | 118 /* check for zeroes and 0x01 */ |
120 for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) { | 119 for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) { |
147 if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) { | 146 if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) { |
148 goto LBL_ERR; | 147 goto LBL_ERR; |
149 } | 148 } |
150 | 149 |
151 /* mask == hash means valid signature */ | 150 /* mask == hash means valid signature */ |
152 if (XMEMCMP(mask, hash, hLen) == 0) { | 151 if (XMEM_NEQ(mask, hash, hLen) == 0) { |
153 *res = 1; | 152 *res = 1; |
154 } | 153 } |
155 | 154 |
156 err = CRYPT_OK; | 155 err = CRYPT_OK; |
157 LBL_ERR: | 156 LBL_ERR: |
158 #ifdef LTC_CLEAN_STACK | 157 #ifdef LTC_CLEAN_STACK |
159 zeromem(DB, modulus_len); | 158 zeromem(DB, modulus_len); |
160 zeromem(mask, modulus_len); | 159 zeromem(mask, modulus_len); |
161 zeromem(salt, modulus_len); | 160 zeromem(salt, modulus_len); |
162 zeromem(hash, modulus_len); | 161 zeromem(hash, modulus_len); |
163 #endif | 162 #endif |
164 | 163 |
165 XFREE(hash); | 164 XFREE(hash); |
166 XFREE(salt); | 165 XFREE(salt); |
167 XFREE(mask); | 166 XFREE(mask); |
170 return err; | 169 return err; |
171 } | 170 } |
172 | 171 |
173 #endif /* LTC_PKCS_1 */ | 172 #endif /* LTC_PKCS_1 */ |
174 | 173 |
175 /* $Source$ */ | 174 /* ref: $Format:%D$ */ |
176 /* $Revision$ */ | 175 /* git commit: $Format:%H$ */ |
177 /* $Date$ */ | 176 /* commit time: $Format:%ai$ */ |