Skip to content

Commit 5bc034f

Browse files
committed
re-factor some tests
1 parent ddf1b63 commit 5bc034f

5 files changed

Lines changed: 43 additions & 91 deletions

File tree

src/misc/compare_testvector.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ int compare_testvector(const void* is, const unsigned long is_len, const void* s
6464
}
6565
#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
6666
if (res != 0) {
67-
fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
67+
fprintf(stderr, "Testvector #%i(0x%x) of %s failed:\n", which, which, what);
6868
s_print_hex("SHOULD", should, should_len);
6969
s_print_hex("IS ", is, is_len);
7070
#if LTC_TEST_DBG > 1
7171
} else {
72-
fprintf(stderr, "Testvector #%i of %s passed!\n", which, what);
72+
fprintf(stderr, "Testvector #%i(0x%x) of %s passed!\n", which, which, what);
7373
#endif
7474
}
7575
#else

tests/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ extern prng_state yarrow_prng;
1313
#define SHOULD_FAIL(x) do { fprintf(stderr, "%s:\n", #x); run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
1414
#define SHOULD_FAIL_WITH(x, e) do { fprintf(stderr, "%s:\n", #x); run_cmd((x) == (e) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
1515
#define ENSURE(x) do { fprintf(stderr, "%s:\n", #x); run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
16+
#define ENSUREX(x, str) do { fprintf(stderr, "%s:\n", #x); run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, (str)); } while (0)
1617
#else
1718
#define DO(x) do { run_cmd((x), __LINE__, __FILE__, #x, NULL); } while (0)
1819
#define DOX(x, str) do { run_cmd((x), __LINE__, __FILE__, #x, (str)); } while (0)
1920
#define SHOULD_FAIL(x) do { run_cmd((x) != CRYPT_OK ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
2021
#define SHOULD_FAIL_WITH(x, e) do { run_cmd((x) == (e) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
2122
#define ENSURE(x) do { run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, NULL); } while (0)
23+
#define ENSUREX(x, str) do { run_cmd(((x)) ? CRYPT_OK : CRYPT_FAIL_TESTVECTOR, __LINE__, __FILE__, #x, (str)); } while (0)
2224
#endif
2325

2426
#define COMPARE_TESTVECTOR(i, il, s, sl, wa, wi) do { DO(do_compare_testvector((i), (il), (s), (sl), (wa), (wi))); } while(0)

tests/ecc_test.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,7 @@ static int s_ecc_old_api(void)
373373
y = sizeof(buf[1]);
374374
DO(ecc_shared_secret (&userb, &usera, buf[1], &y));
375375

376-
if (y != x) {
377-
fprintf(stderr, "ecc Shared keys are not same size.");
378-
return 1;
379-
}
380-
381-
if (memcmp (buf[0], buf[1], x)) {
382-
fprintf(stderr, "ecc Shared keys not same contents.");
383-
return 1;
384-
}
376+
DO(do_compare_testvector(buf[0], x, buf[1], y, "ecc Shared keys", s));
385377

386378
/* now export userb */
387379
y = sizeof(buf[0]);

tests/mpi_test.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@
66
static int s_radix_to_bin_test(void)
77
{
88
/* RADIX 16 */
9-
const char *ghex = "2";
10-
const char *phex = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22"
9+
const char ghex[] = "2";
10+
const char phex[] = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22"
1111
"514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6"
1212
"F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
1313
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB"
1414
"9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E8603"
1515
"9B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
1616
"15728E5A8AACAA68FFFFFFFFFFFFFFFF";
17-
const char *xhex = "A6681ADC386CE944C3DED9A7301DCC9C518250E3EDB62F959198F8DC0057DD6FB57ABAFD788198B1";
18-
const char *yhex = "39046632C834418DFA07B3091538B614D1FB5DBB785C0FBEA3B98B295BC0CD076A88D9452141A269"
17+
const char xhex[] = "A6681ADC386CE944C3DED9A7301DCC9C518250E3EDB62F959198F8DC0057DD6FB57ABAFD788198B1";
18+
const char yhex[] = "39046632C834418DFA07B3091538B614D1FB5DBB785C0FBEA3B98B295BC0CD076A88D9452141A269"
1919
"E8BAEB1DD654EBA03A5705318D129754CDF4003A8C399240FBB8F162490F6F0DC70E414B6FEE8808"
2020
"6AFAA48E9F3A248EDC093452663D34E0E809D4F6BADBB36F80B6813EBF7C3281B862209E5604BDEA"
2121
"8B8F5F7BFDC3EEB7ADB73048289BCEA0F5A5CDEE7DF91CD1F0BA632F06DBE9BA7EF014B84B02D497"
2222
"CA7D0C60F734752A649DA496946B4E531B30D9F82EDD855636C0B0F2AE232E4186454E8887BB423E"
2323
"32A5A2495EACBA99620ACD03A38345EBB6735E62330A8EE9AA6C8370410F5CD45AF37EE90A0DA95B"
2424
"E96FC939E88FE0BD2CD09FC8F524208C";
2525
/* RADIX 47 */
26-
const char *gr47 = "2";
27-
const char *pr47 = "F27Mg1SadOFIRbDOJ5dHgHiVF02Z1LHHQ6G5SLG2U8aTdfH1ETk4GARRE7WW99dBUBLb9e2OHFIaSM1A"
26+
const char gr47[] = "2";
27+
const char pr47[] = "F27Mg1SadOFIRbDOJ5dHgHiVF02Z1LHHQ6G5SLG2U8aTdfH1ETk4GARRE7WW99dBUBLb9e2OHFIaSM1A"
2828
"ag2LNNjgYa9I9CjQGJihL3J7A2SGQe8j5Ch8EHMj5jVbAYDiQKhhPhM6Hc56fKS40GUfJkGO7KJ6EXZQ"
2929
"VgbSa2AkPC65F91g0PaYie8AGNVaFKaV9HOQf3ia1iW4i6eCOB9CcBbH7TbQij8AEgjZ0VRBcLKc6UYO"
3030
"1Zc3I2Jc0h1H2HBEH8ONI3OYBbaPV6XhAd8WCc60D0RDBU3H9U7cWL28a0c90XNO0dh5RXEFBbUCE2ZG"
3131
"gh9XQSVIHkVbFIS5F5IGVOkiWAVc9i8BHB2V0UbGW6UdRTZVV";
32-
const char *xr47 = "6bhO7O9NWFRgEMjdU0Y5POj3c1JP15MYEdIg3FO1PEjUY2aGYNSXcaF01R";
33-
const char *yr47 = "3GNPNWEYfKML1cIbI7Cc1Z0O7aQLJgB734dO2i56LLYDdI4gHYk2GAbQH2WI97hNeC7dj3fPEH8I9gV9"
32+
const char xr47[] = "6bhO7O9NWFRgEMjdU0Y5POj3c1JP15MYEdIg3FO1PEjUY2aGYNSXcaF01R";
33+
const char yr47[] = "3GNPNWEYfKML1cIbI7Cc1Z0O7aQLJgB734dO2i56LLYDdI4gHYk2GAbQH2WI97hNeC7dj3fPEH8I9gV9"
3434
"U323AXj1AJXbFPFIHGOTdC29QUUeH2SSc6NWhfQDDXd5Q5iXCKEAUGX3SKcNFIfVOYJgZCLjfHYQdgOQ"
3535
"GCjKNgbEV7Hj34MU3b79iANX2DbMYfb9iGi78BWH2HYAd7IAhk7U0OYGHKJX1bIUUj1KBLhAUg46GaER"
3636
"G9W3ARMfBCj6kSdDF9TdkWAjWTDj722IeVJERC4bKU2VDFG20kDhCMF985efD1SS8DfXcdCHF1kDUkSA"
3737
"884FHYiFEPkaagQOBQaN9BNaEHNbbd002DCIIX5eMP4HgPJPF";
3838
/* RADIX 64 */
39-
const char *gr64 = "2";
40-
const char *pr64 = "3//////////yaFsg8XQC8qnCPYYu3S7D4f0au8YcVCT08BlgOx4viYKKe8UOuq1DtlbHcppJf36p0h2c"
39+
const char gr64[] = "2";
40+
const char pr64[] = "3//////////yaFsg8XQC8qnCPYYu3S7D4f0au8YcVCT08BlgOx4viYKKe8UOuq1DtlbHcppJf36p0h2c"
4141
"toNnGtJ+4rRMrHmaNaXRLsObv+nlHCGkccD+rh2/zSjlG6j+tkE6lxMecVfQwV915yIn/cIIXcKUpaMp"
4242
"t207oueME/1PZQI3OSLTEQQHO/gFqapr+3PLqZtAEjbXnYyrOWXLAxdjKf1t2Mbcrd33LEIhoO1F5qR0"
4343
"ZA625yCf1UHYuspZlZddSi60w60vidWwBi1wAFjSLTy6zCKidUAylsbLWN63cLINpgbMhb5T8c69Zw1H"
4444
"0LSevQYgogQF//////////";
45-
const char *xr64 = "2cQ1hSE6pfHCFUsQSm7SoSKO9Gu+ssBvMHcFZS05VTRxLwklruWPYn";
46-
const char *yr64 = "v16Ooo3H1ZVe7imaLEBOKqVjTktXS3xwZkOifMy3D1sg8sKKXGQ9fwBhh7TPKww0wLmKnZHANLCtq03g"
45+
const char xr64[] = "2cQ1hSE6pfHCFUsQSm7SoSKO9Gu+ssBvMHcFZS05VTRxLwklruWPYn";
46+
const char yr64[] = "v16Ooo3H1ZVe7imaLEBOKqVjTktXS3xwZkOifMy3D1sg8sKKXGQ9fwBhh7TPKww0wLmKnZHANLCtq03g"
4747
"CEP90+xZnOaaFRmt73a5BR+w826hwf8wVEYIEt0aqKcOzDE3e2TJskjkpRu2sWJw/V3A1k68WdbO4lUg"
4848
"BZrzx/SFkjwstC4WecywWzQNDxdtv7D7mkcCl1jlfkdxm5BXB0jINodqCOFSqTIfadQIMb6jEKnimsVW"
4949
"ktOLMDi2myguZBa66HKw8Xxj2FZAbeabUhBgPOWhD0wE3HUksSrvYCmgEwQfiWt113rpKMlD+wGeDgLl"
5050
"fRyavw8/WlIpGdyZr922C";
5151
/* RADIX 256 */
52-
unsigned char gbin[] = { 0x02 };
53-
unsigned char pbin[] = {
52+
const unsigned char gbin[] = { 0x02 };
53+
const unsigned char pbin[] = {
5454
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34,
5555
0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74,
5656
0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
@@ -68,12 +68,12 @@ static int s_radix_to_bin_test(void)
6868
0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
6969
0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
7070
};
71-
unsigned char xbin[] = {
71+
const unsigned char xbin[] = {
7272
0xA6, 0x68, 0x1A, 0xDC, 0x38, 0x6C, 0xE9, 0x44, 0xC3, 0xDE, 0xD9, 0xA7, 0x30, 0x1D, 0xCC, 0x9C,
7373
0x51, 0x82, 0x50, 0xE3, 0xED, 0xB6, 0x2F, 0x95, 0x91, 0x98, 0xF8, 0xDC, 0x00, 0x57, 0xDD, 0x6F,
7474
0xB5, 0x7A, 0xBA, 0xFD, 0x78, 0x81, 0x98, 0xB1
7575
};
76-
unsigned char ybin[] = {
76+
const unsigned char ybin[] = {
7777
0x39, 0x04, 0x66, 0x32, 0xC8, 0x34, 0x41, 0x8D, 0xFA, 0x07, 0xB3, 0x09, 0x15, 0x38, 0xB6, 0x14,
7878
0xD1, 0xFB, 0x5D, 0xBB, 0x78, 0x5C, 0x0F, 0xBE, 0xA3, 0xB9, 0x8B, 0x29, 0x5B, 0xC0, 0xCD, 0x07,
7979
0x6A, 0x88, 0xD9, 0x45, 0x21, 0x41, 0xA2, 0x69, 0xE8, 0xBA, 0xEB, 0x1D, 0xD6, 0x54, 0xEB, 0xA0,
@@ -92,18 +92,24 @@ static int s_radix_to_bin_test(void)
9292
0xE9, 0x6F, 0xC9, 0x39, 0xE8, 0x8F, 0xE0, 0xBD, 0x2C, 0xD0, 0x9F, 0xC8, 0xF5, 0x24, 0x20, 0x8C
9393
};
9494

95+
#define MPI_TEST(n) (n), sizeof(n)
96+
#define MPI_TESTSET(t) MPI_TEST(g ## t), MPI_TEST(p ## t), MPI_TEST(x ## t), MPI_TEST(y ## t)
9597
struct {
9698
int radix;
9799
const void* g; int glen;
98100
const void* p; int plen;
99101
const void* x; int xlen;
100102
const void* y; int ylen;
101103
} test[4] = {
102-
{ 256, gbin, sizeof(gbin), pbin, sizeof(pbin), xbin, sizeof(xbin), ybin, sizeof(ybin) },
103-
{ 16, ghex, XSTRLEN(ghex)+1, phex, XSTRLEN(phex)+1, xhex, XSTRLEN(xhex)+1, yhex, XSTRLEN(yhex)+1 },
104-
{ 47, gr47, XSTRLEN(gr47)+1, pr47, XSTRLEN(pr47)+1, xr47, XSTRLEN(xr47)+1, yr47, XSTRLEN(yr47)+1 },
105-
{ 64, gr64, XSTRLEN(gr64)+1, pr64, XSTRLEN(pr64)+1, xr64, XSTRLEN(xr64)+1, yr64, XSTRLEN(yr64)+1 },
104+
/* ground-truth values in binary format */
105+
{ 256, MPI_TESTSET(bin) },
106+
/* test-cases in different radices */
107+
{ 16, MPI_TESTSET(hex) },
108+
{ 47, MPI_TESTSET(r47) },
109+
{ 64, MPI_TESTSET(r64) },
106110
};
111+
#undef MPI_TESTSET
112+
#undef MPI_TEST
107113
int i, j;
108114
unsigned char key_parts[4][256];
109115
unsigned long key_lens[4];

tests/rsa_test.c

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,7 @@ static int rsa_compat_test(void)
258258
DO(rsa_import(openssl_public_rsa, sizeof(openssl_public_rsa), &key));
259259
len = sizeof(buf);
260260
DO(rsa_export(buf, &len, PK_PUBLIC | PK_STD, &key));
261-
if (len != sizeof(openssl_public_rsa) || memcmp(buf, openssl_public_rsa, len)) {
262-
fprintf(stderr, "RSA(public) SSL public X.509 export failed to match OpenSSL output\n");
263-
print_hex("should", openssl_public_rsa, sizeof(openssl_public_rsa));
264-
print_hex("is", buf, len);
265-
return 1;
266-
}
261+
DO(do_compare_testvector(buf, len, openssl_public_rsa, sizeof(openssl_public_rsa), "RSA public export (X.509)", 0));
267262
rsa_free(&key);
268263

269264
return 0;
@@ -472,24 +467,12 @@ print_hex("q", tmp, len);
472467
SHOULD_FAIL(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat2, &key));
473468
/* change a byte back */
474469
out[8] ^= 1;
475-
if (len2 != rsa_msgsize) {
476-
fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
477-
return 1;
478-
}
470+
ENSURE(len2 == rsa_msgsize);
479471

480472
len2 = rsa_msgsize;
481473
DO(rsa_decrypt_key(out, len, tmp, &len2, NULL, 0, hash_idx, &stat, &key));
482-
if (!(stat == 1 && stat2 == 0)) {
483-
fprintf(stderr, "rsa_decrypt_key (without lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
484-
fprintf(stderr, "\n stat: %i stat2: %i", stat, stat2);
485-
return 1;
486-
}
487-
if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
488-
fprintf(stderr, "\nrsa_decrypt_key mismatch, len %lu (second decrypt)\n", len2);
489-
print_hex("Original", in, rsa_msgsize);
490-
print_hex("Output", tmp, len2);
491-
return 1;
492-
}
474+
ENSUREX(stat == 1 && stat2 == 0, "rsa_decrypt_key (without lparam)");
475+
DO(do_compare_testvector(tmp, len2, in, rsa_msgsize, "rsa_decrypt_key (without lparam)", cnt << 8 | rsa_msgsize));
493476
}
494477
}
495478

@@ -501,25 +484,15 @@ print_hex("q", tmp, len);
501484
/* change a byte */
502485
out[8] ^= 1;
503486
SHOULD_FAIL(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat2, &key));
504-
if (len2 != rsa_msgsize) {
505-
fprintf(stderr, "\n%i:rsa_decrypt_key mismatch len %lu (first decrypt)", __LINE__, len2);
506-
return 1;
507-
}
487+
ENSURE(len2 == rsa_msgsize);
488+
508489
/* change a byte back */
509490
out[8] ^= 1;
510491

511492
len2 = rsa_msgsize;
512493
DO(rsa_decrypt_key(out, len, tmp, &len2, lparam, sizeof(lparam), hash_idx, &stat, &key));
513-
if (!(stat == 1 && stat2 == 0)) {
514-
fprintf(stderr, "rsa_decrypt_key (with lparam) failed (rsa_msgsize = %lu)", rsa_msgsize);
515-
return 1;
516-
}
517-
if (len2 != rsa_msgsize || memcmp(tmp, in, rsa_msgsize)) {
518-
fprintf(stderr, "rsa_decrypt_key mismatch len %lu", len2);
519-
print_hex("Original", in, rsa_msgsize);
520-
print_hex("Output", tmp, len2);
521-
return 1;
522-
}
494+
ENSURE(stat == 1 && stat2 == 0);
495+
DO(do_compare_testvector(tmp, len2, in, rsa_msgsize, "rsa_decrypt_key (with lparam)", rsa_msgsize));
523496
}
524497

525498
/* encrypt the key PKCS #1 v1.5 (payload from 1 to 117 bytes) */
@@ -532,20 +505,8 @@ print_hex("q", tmp, len);
532505

533506
len2 = rsa_msgsize;
534507
DO(rsa_decrypt_key_ex(out, len, tmp, &len2, NULL, 0, 0, LTC_PKCS_1_V1_5, &stat, &key));
535-
if (stat != 1) {
536-
fprintf(stderr, "rsa_decrypt_key_ex failed, %d, %d", stat, stat2);
537-
return 1;
538-
}
539-
if (len2 != rsa_msgsize) {
540-
fprintf(stderr, "rsa_decrypt_key_ex mismatch len %lu", len2);
541-
return 1;
542-
}
543-
if (memcmp(tmp, in, rsa_msgsize)) {
544-
fprintf(stderr, "rsa_decrypt_key_ex mismatch data");
545-
print_hex("Original", in, rsa_msgsize);
546-
print_hex("Output", tmp, rsa_msgsize);
547-
return 1;
548-
}
508+
ENSURE(stat == 1);
509+
DO(do_compare_testvector(tmp, len2, in, rsa_msgsize, "rsa_decrypt_key_ex", rsa_msgsize));
549510
}
550511

551512
/* sign a message (unsalted, lower cholestorol and Atkins approved) now */
@@ -574,13 +535,7 @@ print_hex("q", tmp, len);
574535
in[0] ^= 1;
575536
DO(rsa_verify_hash(out, len, in, 20, hash_idx, 0, &stat2, &key));
576537

577-
if (!(stat == 1 && stat2 == 0)) {
578-
fprintf(stderr, "rsa_verify_hash (unsalted, origKey) failed, %d, %d", stat, stat2);
579-
rsa_free(&key);
580-
rsa_free(&pubKey);
581-
rsa_free(&privKey);
582-
return 1;
583-
}
538+
ENSUREX(stat == 1 && stat2 == 0, "rsa_verify_hash (unsalted, origKey) failed");
584539

585540
/* verify with privKey */
586541
/* change byte back to original */
@@ -736,10 +691,7 @@ print_hex("q", tmp, len);
736691
DO(rsa_import_x509(tmp, len3, &key));
737692
len = sizeof(tmp);
738693
DO(rsa_export(tmp, &len, PK_PUBLIC, &key));
739-
if (len != sizeof(openssl_public_rsa_stripped) || memcmp(tmp, openssl_public_rsa_stripped, len)) {
740-
fprintf(stderr, "RSA public export failed to match rsa_import_x509\n");
741-
return 1;
742-
}
694+
DO(do_compare_testvector(tmp, len, openssl_public_rsa_stripped, sizeof(openssl_public_rsa_stripped), "RSA public export failed to match rsa_import_x509", 0));
743695
rsa_free(&key);
744696

745697
len3 = sizeof(tmp);

0 commit comments

Comments
 (0)