1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file rsa_verify_hash.c
14 RSA PKCS #1 v1.5 or v2 PSS signature verification, Tom St Denis and Andreas Lange
15 */
16
17 #ifdef LTC_MRSA
18
19 /**
20 PKCS #1 de-sign then v1.5 or PSS depad
21 @param sig The signature data
22 @param siglen The length of the signature data (octets)
23 @param hash The hash of the message that was signed
24 @param hashlen The length of the hash of the message that was signed (octets)
25 @param padding Type of padding (LTC_PKCS_1_PSS, LTC_PKCS_1_V1_5 or LTC_PKCS_1_V1_5_NA1)
26 @param hash_idx The index of the desired hash
27 @param saltlen The length of the salt used during signature
28 @param stat [out] The result of the signature comparison, 1==valid, 0==invalid
29 @param key The public RSA key corresponding to the key that performed the signature
30 @return CRYPT_OK on success (even if the signature is invalid)
31 */
rsa_verify_hash_ex(const unsigned char * sig,unsigned long siglen,const unsigned char * hash,unsigned long hashlen,int padding,int hash_idx,unsigned long saltlen,int * stat,const rsa_key * key)32 int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
33 const unsigned char *hash, unsigned long hashlen,
34 int padding,
35 int hash_idx, unsigned long saltlen,
36 int *stat, const rsa_key *key)
37 {
38 unsigned long modulus_bitlen, modulus_bytelen, x;
39 int err;
40 unsigned char *tmpbuf;
41
42 LTC_ARGCHK(hash != NULL);
43 LTC_ARGCHK(sig != NULL);
44 LTC_ARGCHK(stat != NULL);
45 LTC_ARGCHK(key != NULL);
46
47 /* default to invalid */
48 *stat = 0;
49
50 /* valid padding? */
51
52 if ((padding != LTC_PKCS_1_V1_5) &&
53 (padding != LTC_PKCS_1_PSS) &&
54 (padding != LTC_PKCS_1_V1_5_NA1)) {
55 return CRYPT_PK_INVALID_PADDING;
56 }
57
58 if (padding != LTC_PKCS_1_V1_5_NA1) {
59 /* valid hash ? */
60 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
61 return err;
62 }
63 }
64
65 /* get modulus len in bits */
66 modulus_bitlen = mp_count_bits( (key->N));
67
68 /* outlen must be at least the size of the modulus */
69 modulus_bytelen = mp_unsigned_bin_size( (key->N));
70 if (modulus_bytelen != siglen) {
71 return CRYPT_INVALID_PACKET;
72 }
73
74 /* allocate temp buffer for decoded sig */
75 tmpbuf = XMALLOC(siglen);
76 if (tmpbuf == NULL) {
77 return CRYPT_MEM;
78 }
79
80 /* RSA decode it */
81 x = siglen;
82 if ((err = ltc_mp.rsa_me(sig, siglen, tmpbuf, &x, PK_PUBLIC, key)) != CRYPT_OK) {
83 XFREE(tmpbuf);
84 return err;
85 }
86
87 /* make sure the output is the right size */
88 if (x != siglen) {
89 XFREE(tmpbuf);
90 return CRYPT_INVALID_PACKET;
91 }
92
93 if (padding == LTC_PKCS_1_PSS) {
94 /* PSS decode and verify it */
95
96 if(modulus_bitlen%8 == 1){
97 err = pkcs_1_pss_decode(hash, hashlen, tmpbuf+1, x-1, saltlen, hash_idx, modulus_bitlen, stat);
98 }
99 else{
100 err = pkcs_1_pss_decode(hash, hashlen, tmpbuf, x, saltlen, hash_idx, modulus_bitlen, stat);
101 }
102
103 } else {
104 /* PKCS #1 v1.5 decode it */
105 unsigned char *out;
106 unsigned long outlen;
107 int decoded;
108
109 /* allocate temp buffer for decoded hash */
110 outlen = ((modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0)) - 3;
111 out = XMALLOC(outlen);
112 if (out == NULL) {
113 err = CRYPT_MEM;
114 goto bail_2;
115 }
116
117 if ((err = pkcs_1_v1_5_decode(tmpbuf, x, LTC_PKCS_1_EMSA, modulus_bitlen, out, &outlen, &decoded)) != CRYPT_OK) {
118 XFREE(out);
119 goto bail_2;
120 }
121
122 if (padding == LTC_PKCS_1_V1_5) {
123 unsigned long loid[16], reallen;
124 ltc_asn1_list digestinfo[2], siginfo[2];
125
126 /* not all hashes have OIDs... so sad */
127 if (hash_descriptor[hash_idx]->OIDlen == 0) {
128 err = CRYPT_INVALID_ARG;
129 goto bail_2;
130 }
131
132 /* now we must decode out[0...outlen-1] using ASN.1, test the OID and then test the hash */
133 /* construct the SEQUENCE
134 SEQUENCE {
135 SEQUENCE {hashoid OID
136 blah NULL
137 }
138 hash OCTET STRING
139 }
140 */
141 LTC_SET_ASN1(digestinfo, 0, LTC_ASN1_OBJECT_IDENTIFIER, loid, sizeof(loid)/sizeof(loid[0]));
142 LTC_SET_ASN1(digestinfo, 1, LTC_ASN1_NULL, NULL, 0);
143 LTC_SET_ASN1(siginfo, 0, LTC_ASN1_SEQUENCE, digestinfo, 2);
144 LTC_SET_ASN1(siginfo, 1, LTC_ASN1_OCTET_STRING, tmpbuf, siglen);
145
146 if ((err = der_decode_sequence_strict(out, outlen, siginfo, 2)) != CRYPT_OK) {
147 /* fallback to Legacy:missing NULL */
148 LTC_SET_ASN1(siginfo, 0, LTC_ASN1_SEQUENCE, digestinfo, 1);
149 if ((err = der_decode_sequence_strict(out, outlen, siginfo, 2)) != CRYPT_OK) {
150 XFREE(out);
151 goto bail_2;
152 }
153 }
154
155 if ((err = der_length_sequence(siginfo, 2, &reallen)) != CRYPT_OK) {
156 XFREE(out);
157 goto bail_2;
158 }
159
160 /* test OID */
161 if ((reallen == outlen) &&
162 (digestinfo[0].size == hash_descriptor[hash_idx]->OIDlen) &&
163 (XMEMCMP(digestinfo[0].data, hash_descriptor[hash_idx]->OID, sizeof(unsigned long) * hash_descriptor[hash_idx]->OIDlen) == 0) &&
164 (siginfo[1].size == hashlen) &&
165 (XMEMCMP(siginfo[1].data, hash, hashlen) == 0)) {
166 *stat = 1;
167 }
168 } else {
169 /* only check if the hash is equal */
170 if ((hashlen == outlen) &&
171 (XMEMCMP(out, hash, hashlen) == 0)) {
172 *stat = 1;
173 }
174 }
175
176 #ifdef LTC_CLEAN_STACK
177 zeromem(out, outlen);
178 #endif
179 XFREE(out);
180 }
181
182 bail_2:
183 #ifdef LTC_CLEAN_STACK
184 zeromem(tmpbuf, siglen);
185 #endif
186 XFREE(tmpbuf);
187 return err;
188 }
189
190 #endif /* LTC_MRSA */
191
192 /* ref: $Format:%D$ */
193 /* git commit: $Format:%H$ */
194 /* commit time: $Format:%ai$ */
195