Skip to content

Commit 59bc3b5

Browse files
committed
make ecc_verify_hash_ex less strict (as it was before ecc_recover_key addition)
1 parent 4d6f973 commit 59bc3b5

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/pk/ecc/ecc_verify_hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
7676
}
7777
else if (sigformat == LTC_ECCSIG_RFC7518) {
7878
/* RFC7518 format - raw (r,s) */
79-
i = mp_unsigned_bin_size(key->dp.order);
80-
if (siglen != (2*i)) {
79+
if ((siglen % 2) == 1) {
8180
err = CRYPT_INVALID_PACKET;
8281
goto error;
8382
}
83+
i = siglen / 2;
8484
if ((err = mp_read_unsigned_bin(r, (unsigned char *)sig, i)) != CRYPT_OK) { goto error; }
8585
if ((err = mp_read_unsigned_bin(s, (unsigned char *)sig+i, i)) != CRYPT_OK) { goto error; }
8686
}

tests/ecc_test.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ static int _ecc_test_shamir(void)
117117
}
118118
#endif
119119

120+
/* https://github.com/libtom/libtomcrypt/issues/108 */
120121
static int _ecc_issue108(void)
121122
{
122123
void *a, *modulus, *order;
@@ -151,6 +152,57 @@ static int _ecc_issue108(void)
151152
return err;
152153
}
153154

155+
/* https://github.com/libtom/libtomcrypt/issues/443 */
156+
static int _ecc_issue443(void)
157+
{
158+
const ltc_ecc_curve* cu;
159+
ecc_key key;
160+
int stat = 0;
161+
unsigned char hash[64];
162+
unsigned long hashlen;
163+
const unsigned char msg[] = { 0x54,0x65,0x73,0x74 };
164+
/* msg+pub1+sig1 test vector is from wycheproof - ecdsa_webcrypto_test (incorrect size of signature) */
165+
const unsigned char pub1[] = {
166+
0x04,
167+
0x4a,0x03,0xef,0x9f,0x92,0xeb,0x26,0x8c,0xaf,0xa6,0x01,0x07,0x24,0x89,0xa5,0x63,
168+
0x80,0xfa,0x0d,0xc4,0x31,0x71,0xd7,0x71,0x28,0x13,0xb3,0xa1,0x9a,0x1e,0xb5,0xe5,
169+
0x3e,0x21,0x3e,0x28,0xa6,0x08,0xce,0x9a,0x2f,0x4a,0x17,0xfd,0x83,0x0c,0x66,0x54,
170+
0x01,0x8a,0x79,0xb3,0xe0,0x26,0x3d,0x91,0xa8,0xba,0x90,0x62,0x2d,0xf6,0xf2,0xf0
171+
};
172+
const unsigned char sig1[] = { 0x05, 0x01 };
173+
/* msg+pub2+sig2 test vector is from wycheproof - ecdsa_webcrypto_test (incorrect size of signature) */
174+
const unsigned char pub2[] = {
175+
0x04,
176+
0x00,0x5f,0x50,0x59,0x30,0x83,0x49,0xf9,0xeb,0xbb,0x4d,0x1c,0x55,0xc0,0xaf,0xcc,0xf6,0x21,0x62,0xec,0x1d,0xd1,
177+
0x2e,0xf3,0xed,0x90,0x66,0x56,0x92,0x4f,0xfd,0x99,0xca,0xb9,0xf0,0x6b,0x0e,0xb2,0x18,0xcf,0xf0,0x78,0xa4,0x67,
178+
0x7a,0x5c,0xe1,0xcc,0x07,0x65,0x2b,0xc9,0x76,0xae,0xfc,0x73,0x2c,0x28,0xf6,0x7e,0xf0,0x78,0xa4,0x34,0xe9,0x99,
179+
0x00,0xa5,0xd1,0x4d,0xf3,0x10,0x63,0x0d,0x76,0xec,0x03,0xcb,0x6f,0x9b,0x95,0xbf,0x1a,0x22,0x43,0x81,0x05,0xc8,
180+
0x8c,0xd9,0xfd,0x3d,0xac,0x80,0xf8,0x57,0xad,0xd3,0x82,0x71,0xd8,0xba,0x90,0x16,0x84,0xb2,0x6d,0x43,0x6d,0x4a,
181+
0x85,0x9a,0xd4,0xcd,0xa5,0xe9,0x67,0x7b,0x73,0xca,0xb3,0xf3,0xe5,0xe4,0x1a,0x3d,0x79,0x96,0x60,0x72,0x79,0xab
182+
};
183+
const unsigned char sig2[] = { 0x01, 0x01 };
184+
185+
hashlen = sizeof(hash);
186+
DO(hash_memory(find_hash("sha256"), msg, sizeof(msg), hash, &hashlen));
187+
DO(ecc_find_curve("secp256r1", &cu));
188+
DO(ecc_set_curve(cu, &key));
189+
DO(ecc_set_key(pub1, sizeof(pub1), PK_PUBLIC, &key));
190+
DO(ecc_verify_hash_rfc7518(sig1, sizeof(sig1), hash, hashlen, &stat, &key));
191+
ecc_free(&key);
192+
if (stat != 1) return CRYPT_FAIL_TESTVECTOR;
193+
194+
hashlen = sizeof(hash);
195+
DO(hash_memory(find_hash("sha512"), msg, sizeof(msg), hash, &hashlen));
196+
DO(ecc_find_curve("secp521r1", &cu));
197+
DO(ecc_set_curve(cu, &key));
198+
DO(ecc_set_key(pub2, sizeof(pub2), PK_PUBLIC, &key));
199+
DO(ecc_verify_hash_rfc7518(sig2, sizeof(sig2), hash, hashlen, &stat, &key));
200+
ecc_free(&key);
201+
if (stat != 1) return CRYPT_FAIL_TESTVECTOR;
202+
203+
return CRYPT_OK;
204+
}
205+
154206
static int _ecc_test_mp(void)
155207
{
156208
void *a, *modulus, *order;
@@ -1113,6 +1165,7 @@ int ecc_tests(void)
11131165
DO(_ecc_import_export());
11141166
DO(_ecc_test_mp());
11151167
DO(_ecc_issue108());
1168+
DO(_ecc_issue443());
11161169
#ifdef LTC_ECC_SHAMIR
11171170
DO(_ecc_test_shamir());
11181171
DO(_ecc_test_recovery());

0 commit comments

Comments
 (0)