Skip to content

Commit 632d2b7

Browse files
authored
Merge pull request #291 from libtom/pr/dsa-test-wycheproof
dsa_verify_hash fix + dsa_wycheproof_test
2 parents 4b36f06 + 66abefc commit 632d2b7

2 files changed

Lines changed: 80 additions & 6 deletions

File tree

src/pk/dsa/dsa_verify_hash.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
101101
ltc_asn1_list sig_seq[2];
102102
unsigned long reallen = 0;
103103

104+
LTC_ARGCHK(stat != NULL);
105+
*stat = 0; /* must be set before the first return */
106+
104107
if ((err = mp_init_multi(&r, &s, NULL)) != CRYPT_OK) {
105108
return err;
106109
}

tests/dsa_test.c

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,76 @@ static int _dsa_compat_test(void)
246246
return CRYPT_OK;
247247
}
248248

249+
static int _dsa_wycheproof_test(void)
250+
{
251+
/* test case from https://github.com/google/wycheproof/blob/master/testvectors/dsa_test.json
252+
*
253+
* "comment" : "appending unused 0's",
254+
* "message" : "48656c6c6f",
255+
* "result" : "invalid",
256+
* "sig" : "303d021c1e41b479ad576905b960fe14eadb91b0ccf34843dab916173bb8c9cd021d00ade65988d237d30f9ef41dd424a4e1c8f16967cf3365813fe87862360000",
257+
* "tcId" : 55
258+
*/
259+
unsigned char msg[] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f };
260+
unsigned char sig[] = { 0x30, 0x3d, 0x02, 0x1c, 0x1e, 0x41, 0xb4, 0x79, 0xad, 0x57, 0x69, 0x05, 0xb9, 0x60, 0xfe,
261+
0x14, 0xea, 0xdb, 0x91, 0xb0, 0xcc, 0xf3, 0x48, 0x43, 0xda, 0xb9, 0x16, 0x17, 0x3b, 0xb8,
262+
0xc9, 0xcd, 0x02, 0x1d, 0x00, 0xad, 0xe6, 0x59, 0x88, 0xd2, 0x37, 0xd3, 0x0f, 0x9e, 0xf4,
263+
0x1d, 0xd4, 0x24, 0xa4, 0xe1, 0xc8, 0xf1, 0x69, 0x67, 0xcf, 0x33, 0x65, 0x81, 0x3f, 0xe8,
264+
0x78, 0x62, 0x36, 0x00, 0x00 };
265+
const char* b64key =
266+
"MIIDQjCCAjUGByqGSM44BAEwggIoAoIBAQCPeTXZuarpv6vtiHrPSVG28y7FnjuvNxjo6sSWHz79"
267+
"NgbnQ1GpxBgzObgJ58KuHFObp0dbhdARrbi0eYd1SYRpXKwOjxSzNggooi/6JxEKPWKpk0U0CaD+"
268+
"aWxGWPhL3SCBnDcJoBBXsZWtzQAjPbpUhLYpH51kjviDRIZ3l5zsBLQ0pqwudemYXeI9sCkvwRGM"
269+
"n/qdgYHnM423krcw17njSVkvaAmYchU5Feo9a4tGU8YzRY+AOzKkwuDycpAlbk4/ijsIOKHEUOTh"
270+
"jBopo33fXqFD3ktm/wSQPtXPFiPhWNSHxgjpfyEc2B3KI8tuOAdl+CLjQr5ITAV2OTlgHNZnAh0A"
271+
"uvaWpoV499/e5/pnyXfHhe8ysjO65YDAvNVpXQKCAQAWplxYIEhQcE51AqOXVwQNNNo6NHjBVNTk"
272+
"pcAtJC7gT5bmHkvQkEq9rI837rHgnzGC0jyQQ8tkL4gAQWDt+coJsyB2p5wypifyRz6Rh5uixOdE"
273+
"vSCBVEy1W4AsNo0fqD7UielOD6BojjJCilx4xHjGjQUntxyaOrsLC+EsRGiWOefTznTbEBplqiuH"
274+
"9kxoJts+xy9LVZmDS7TtsC98kOmkltOlXVNb6/xF1PYZ9j897buHOSXC8iTgdzEpbaiH7B5HSPh+"
275+
"+1/et1SEMWsiMt7lU92vAhErDR8C2jCXMiT+J67ai51LKSLZuovjntnhA6Y8UoELxoi34u1DFuHv"
276+
"F9veA4IBBQACggEAHnf4QrGuD82ZKdOUFh1B4UYU/3UHqaMfSh8U0i4qYnofTllmJIg/GlsWjpQl"
277+
"FG8i1fbuKHV0FHFLuZS6ESnwFdbgSnF+35tTCl1cq5TxRjHotM95rrNYzHQYRVU4QeisRhYw6ASm"
278+
"L0Nna6Z5SvZomcN3uGnqYSp7n+ZhGqlr5S64tiyXkRe7vMqKfsHh/6scffz8cEhwDTrjhYE26Jdw"
279+
"HXwpIbXf7x0fiX9Q2WyhtcLtxYytoYkZ41ZC8IB+6/oAyZoy9NCVwxiPeO1UcRvgMlxLUyrszWVA"
280+
"pWfDJyJUQOoVMZveBlEEeaGGF5niW1fezHPANtdaBwK9NzyiMTSZMQ==";
281+
unsigned char derkey[838];
282+
unsigned long derlen = sizeof(derkey);
283+
unsigned char hash[32];
284+
unsigned long hashlen = sizeof(hash);
285+
dsa_key key;
286+
int stat;
287+
288+
DO(base64_decode((unsigned char*)b64key, strlen(b64key), derkey, &derlen));
289+
if (derlen != 838) {
290+
fprintf(stderr, "base64_decode failed, derlen=%lu (expected 838)\n", derlen);
291+
return CRYPT_FAIL_TESTVECTOR;
292+
}
293+
DO(dsa_import(derkey, derlen, &key));
294+
DO(hash_memory(find_hash("sha224"), msg, sizeof(msg), hash, &hashlen));
295+
if (hashlen != 28) {
296+
fprintf(stderr, "hash_memory failed, hashlen=%lu (expected 32)\n", hashlen);
297+
return CRYPT_FAIL_TESTVECTOR;
298+
}
299+
300+
stat = 666; /* intentionally not one, not zero */
301+
DO(dsa_verify_hash(sig, sizeof(sig)-2, hash, hashlen, &stat, &key));
302+
/* without the last two 0x00 bytes it is a valid signature */
303+
if (stat != 1) {
304+
fprintf(stderr, "dsa_verify_hash rejected valid signature\n");
305+
return CRYPT_FAIL_TESTVECTOR;
306+
}
307+
308+
stat = 666; /* intentionally not one, not zero */
309+
DO(dsa_verify_hash(sig, sizeof(sig), hash, hashlen, &stat, &key));
310+
/* this should be invalid */
311+
if (stat != 0) {
312+
fprintf(stderr, "dsa_verify_hash did not reject invalid signature\n");
313+
return CRYPT_FAIL_TESTVECTOR;
314+
}
315+
316+
return CRYPT_OK;
317+
}
318+
249319
int dsa_test(void)
250320
{
251321
unsigned char msg[16], out[1024], out2[1024], ch;
@@ -254,6 +324,7 @@ int dsa_test(void)
254324
dsa_key key, key2;
255325

256326
DO(_dsa_compat_test());
327+
DO(_dsa_wycheproof_test());
257328

258329
/* make a random key */
259330
DO(dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), 20, 128, &key));
@@ -291,20 +362,20 @@ int dsa_test(void)
291362
if (!(stat1 == 1 && stat2 == 0)) { fprintf(stderr, "dsa_verify %d %d", stat1, stat2); return 1; }
292363

293364
/* test exporting it */
294-
x = sizeof(out2);
295-
DO(dsa_export(out2, &x, PK_PRIVATE, &key));
296-
DO(dsa_import(out2, x, &key2));
365+
y = sizeof(out2);
366+
DO(dsa_export(out2, &y, PK_PRIVATE, &key));
367+
DO(dsa_import(out2, y, &key2));
297368

298369
/* verify a signature with it */
299370
DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
300371
if (stat1 == 0) { fprintf(stderr, "dsa_verify (import private) %d ", stat1); return 1; }
301372
dsa_free(&key2);
302373

303374
/* export as public now */
304-
x = sizeof(out2);
305-
DO(dsa_export(out2, &x, PK_PUBLIC, &key));
375+
y = sizeof(out2);
376+
DO(dsa_export(out2, &y, PK_PUBLIC, &key));
306377

307-
DO(dsa_import(out2, x, &key2));
378+
DO(dsa_import(out2, y, &key2));
308379
/* verify a signature with it */
309380
DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
310381
if (stat1 == 0) { fprintf(stderr, "dsa_verify (import public) %d ", stat1); return 1; }

0 commit comments

Comments
 (0)