2323 @param siglen The length of the signature data (octets)
2424 @param hash The hash of the message that was signed
2525 @param hashlen The length of the hash of the message that was signed (octets)
26- @param padding Type of padding (LTC_PKCS_1_PSS or LTC_PKCS_1_V1_5 )
26+ @param padding Type of padding (LTC_PKCS_1_PSS, LTC_PKCS_1_V1_5 or LTC_PKCS_1_V1_5_NA1 )
2727 @param hash_idx The index of the desired hash
2828 @param saltlen The length of the salt used during signature
2929 @param stat [out] The result of the signature comparison, 1==valid, 0==invalid
@@ -51,11 +51,12 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
5151 /* valid padding? */
5252
5353 if ((padding != LTC_PKCS_1_V1_5 ) &&
54- (padding != LTC_PKCS_1_PSS )) {
54+ (padding != LTC_PKCS_1_PSS ) &&
55+ (padding != LTC_PKCS_1_V1_5_NA1 )) {
5556 return CRYPT_PK_INVALID_PADDING ;
5657 }
5758
58- if (padding == LTC_PKCS_1_PSS ) {
59+ if (padding != LTC_PKCS_1_V1_5_NA1 ) {
5960 /* valid hash ? */
6061 if ((err = hash_is_valid (hash_idx )) != CRYPT_OK ) {
6162 return err ;
@@ -103,15 +104,8 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
103104 } else {
104105 /* PKCS #1 v1.5 decode it */
105106 unsigned char * out ;
106- unsigned long outlen , loid [ 16 ], reallen ;
107+ unsigned long outlen ;
107108 int decoded ;
108- ltc_asn1_list digestinfo [2 ], siginfo [2 ];
109-
110- /* not all hashes have OIDs... so sad */
111- if (hash_descriptor [hash_idx ].OIDlen == 0 ) {
112- err = CRYPT_INVALID_ARG ;
113- goto bail_2 ;
114- }
115109
116110 /* allocate temp buffer for decoded hash */
117111 outlen = ((modulus_bitlen >> 3 ) + (modulus_bitlen & 7 ? 1 : 0 )) - 3 ;
@@ -126,37 +120,54 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
126120 goto bail_2 ;
127121 }
128122
129- /* now we must decode out[0...outlen-1] using ASN.1, test the OID and then test the hash */
130- /* construct the SEQUENCE
131- SEQUENCE {
132- SEQUENCE {hashoid OID
133- blah NULL
134- }
135- hash OCTET STRING
123+ if (padding == LTC_PKCS_1_V1_5 ) {
124+ unsigned long loid [16 ], reallen ;
125+ ltc_asn1_list digestinfo [2 ], siginfo [2 ];
126+
127+ /* not all hashes have OIDs... so sad */
128+ if (hash_descriptor [hash_idx ].OIDlen == 0 ) {
129+ err = CRYPT_INVALID_ARG ;
130+ goto bail_2 ;
136131 }
137- */
138- LTC_SET_ASN1 (digestinfo , 0 , LTC_ASN1_OBJECT_IDENTIFIER , loid , sizeof (loid )/sizeof (loid [0 ]));
139- LTC_SET_ASN1 (digestinfo , 1 , LTC_ASN1_NULL , NULL , 0 );
140- LTC_SET_ASN1 (siginfo , 0 , LTC_ASN1_SEQUENCE , digestinfo , 2 );
141- LTC_SET_ASN1 (siginfo , 1 , LTC_ASN1_OCTET_STRING , tmpbuf , siglen );
142-
143- if ((err = der_decode_sequence (out , outlen , siginfo , 2 )) != CRYPT_OK ) {
144- XFREE (out );
145- goto bail_2 ;
146- }
147132
148- if ((err = der_length_sequence (siginfo , 2 , & reallen )) != CRYPT_OK ) {
149- XFREE (out );
150- goto bail_2 ;
151- }
133+ /* now we must decode out[0...outlen-1] using ASN.1, test the OID and then test the hash */
134+ /* construct the SEQUENCE
135+ SEQUENCE {
136+ SEQUENCE {hashoid OID
137+ blah NULL
138+ }
139+ hash OCTET STRING
140+ }
141+ */
142+ LTC_SET_ASN1 (digestinfo , 0 , LTC_ASN1_OBJECT_IDENTIFIER , loid , sizeof (loid )/sizeof (loid [0 ]));
143+ LTC_SET_ASN1 (digestinfo , 1 , LTC_ASN1_NULL , NULL , 0 );
144+ LTC_SET_ASN1 (siginfo , 0 , LTC_ASN1_SEQUENCE , digestinfo , 2 );
145+ LTC_SET_ASN1 (siginfo , 1 , LTC_ASN1_OCTET_STRING , tmpbuf , siglen );
146+
147+ if ((err = der_decode_sequence (out , outlen , siginfo , 2 )) != CRYPT_OK ) {
148+ XFREE (out );
149+ goto bail_2 ;
150+ }
151+
152+ if ((err = der_length_sequence (siginfo , 2 , & reallen )) != CRYPT_OK ) {
153+ XFREE (out );
154+ goto bail_2 ;
155+ }
152156
153- /* test OID */
154- if ((reallen == outlen ) &&
155- (digestinfo [0 ].size == hash_descriptor [hash_idx ].OIDlen ) &&
157+ /* test OID */
158+ if ((reallen == outlen ) &&
159+ (digestinfo [0 ].size == hash_descriptor [hash_idx ].OIDlen ) &&
156160 (XMEM_NEQ (digestinfo [0 ].data , hash_descriptor [hash_idx ].OID , sizeof (unsigned long ) * hash_descriptor [hash_idx ].OIDlen ) == 0 ) &&
157- (siginfo [1 ].size == hashlen ) &&
161+ (siginfo [1 ].size == hashlen ) &&
158162 (XMEM_NEQ (siginfo [1 ].data , hash , hashlen ) == 0 )) {
159- * stat = 1 ;
163+ * stat = 1 ;
164+ }
165+ } else {
166+ /* only check if the hash is equal */
167+ if ((hashlen == outlen ) &&
168+ (XMEMCMP (out , hash , hashlen ) == 0 )) {
169+ * stat = 1 ;
170+ }
160171 }
161172
162173#ifdef LTC_CLEAN_STACK
0 commit comments