From 6ec93afa3cef0c919dcc42e8afe2ab2f1e44b852 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 1 Sep 2015 10:09:41 +0200 Subject: [PATCH] clean-up test-build and extend tomcrypt_custom.h added LTC_MINIMAL to be able do a build without nearly any functionality :) make sure timing resistant RSA & ECC are enabled if not said otherwise --- src/ciphers/des.c | 8 ++-- src/headers/tomcrypt_custom.h | 53 +++++++++++++++++++------- testprof/base64_test.c | 2 + testprof/modes_test.c | 70 +++++++++++++++++++---------------- testprof/x86_prof.c | 16 +++++++- 5 files changed, 99 insertions(+), 50 deletions(-) diff --git a/src/ciphers/des.c b/src/ciphers/des.c index ab8004b..fbacf5c 100644 --- a/src/ciphers/des.c +++ b/src/ciphers/des.c @@ -12,7 +12,7 @@ /** @file des.c - LTC_DES code submitted by Dobes Vandermeer + DES code submitted by Dobes Vandermeer */ #ifdef LTC_DES @@ -1450,9 +1450,9 @@ static void _desfunc(ulong32 *block, const ulong32 *keys) for (cur_round = 0; cur_round < 8; cur_round++) { work = RORc(right, 4) ^ *keys++; leftt ^= SP7[work & 0x3fL] - ^ SP5[(work >> 8) & 0x3fL] - ^ SP3[(work >> 16) & 0x3fL] - ^ SP1[(work >> 24) & 0x3fL]; + ^ SP5[(work >> 8) & 0x3fL] + ^ SP3[(work >> 16) & 0x3fL] + ^ SP1[(work >> 24) & 0x3fL]; work = right ^ *keys++; leftt ^= SP8[ work & 0x3fL] ^ SP6[(work >> 8) & 0x3fL] diff --git a/src/headers/tomcrypt_custom.h b/src/headers/tomcrypt_custom.h index 42a3e6f..680a2ca 100644 --- a/src/headers/tomcrypt_custom.h +++ b/src/headers/tomcrypt_custom.h @@ -125,6 +125,19 @@ #define LTC_BASE64 #endif +/* The minimal set of functionality to run the tests */ +#ifdef LTC_MINIMAL + #define LTC_SHA256 + #define LTC_CTR_MODE + #define LTC_RNG_MAKE_PRNG + #define LTC_RNG_GET_BYTES + #define LTC_YARROW + #define LTC_DEVRANDOM + #define LTC_TRY_URANDOM_FIRST + + #undef LTC_NO_FILE +#endif + /* Enable self-test test vector checking */ #ifndef LTC_NO_TEST #define LTC_TEST @@ -252,6 +265,7 @@ #endif /* LTC_NO_HASHES */ + /* ---> MAC functions <--- */ #ifndef LTC_NO_MACS @@ -283,6 +297,7 @@ #endif /* LTC_NO_MACS */ + /* --> Pseudo Random Number Generators <--- */ #ifndef LTC_NO_PRNGS @@ -304,17 +319,14 @@ /* Fortuna PRNG */ #define LTC_FORTUNA -/* reseed every N calls to the read function */ -#define LTC_FORTUNA_WD 10 -/* number of pools (4..32) can save a bit of ram by lowering the count */ -#define LTC_FORTUNA_POOLS 32 /* Greg's LTC_SOBER128 PRNG ;-0 */ #define LTC_SOBER128 /* the *nix style /dev/random device */ #define LTC_DEVRANDOM -/* try /dev/urandom before trying /dev/random */ +/* try /dev/urandom before trying /dev/random + * are you sure you want to disable this? http://www.2uo.de/myths-about-urandom/ */ #define LTC_TRY_URANDOM_FIRST /* rng_get_bytes() */ #define LTC_RNG_GET_BYTES @@ -323,17 +335,27 @@ #endif /* LTC_NO_PRNGS */ +#ifdef LTC_FORTUNA + +#ifndef LTC_FORTUNA_WD +/* reseed every N calls to the read function */ +#define LTC_FORTUNA_WD 10 +#endif + +#ifndef LTC_FORTUNA_POOLS +/* number of pools (4..32) can save a bit of ram by lowering the count */ +#define LTC_FORTUNA_POOLS 32 +#endif + +#endif /* LTC_FORTUNA */ + + /* ---> Public Key Crypto <--- */ #ifndef LTC_NO_PK /* Include RSA support */ #define LTC_MRSA -#ifndef LTC_NO_RSA_BLINDING -/* Enable RSA blinding when doing private key operations by default */ -#define LTC_RSA_BLINDING -#endif /* LTC_NO_RSA_BLINDING */ - /* Include Diffie-Hellman support */ #ifndef GMP_DESC /* is_prime fails for GMP */ @@ -373,13 +395,18 @@ /* do we want fixed point ECC */ /* #define LTC_MECC_FP */ -#ifndef LTC_NO_ECC_TIMING_RESISTANT +#endif /* LTC_NO_PK */ + +#if defined(LTC_MRSA) && !defined(LTC_NO_RSA_BLINDING) +/* Enable RSA blinding when doing private key operations by default */ +#define LTC_RSA_BLINDING +#endif /* LTC_NO_RSA_BLINDING */ + +#if defined(LTC_MECC) && !defined(LTC_NO_ECC_TIMING_RESISTANT) /* Enable ECC timing resistant version by default */ #define LTC_ECC_TIMING_RESISTANT #endif -#endif /* LTC_NO_PK */ - /* define these PK sizes out of LTC_NO_PK * to have them always defined */ diff --git a/testprof/base64_test.c b/testprof/base64_test.c index e627864..8c15d3c 100644 --- a/testprof/base64_test.c +++ b/testprof/base64_test.c @@ -1,5 +1,6 @@ #include +#ifdef LTC_BASE64 int base64_test(void) { unsigned char in[64], out[256], tmp[64]; @@ -57,6 +58,7 @@ int base64_test(void) } return 0; } +#endif /* $Source$ */ /* $Revision$ */ diff --git a/testprof/modes_test.c b/testprof/modes_test.c index c1cd1c4..58a9fae 100644 --- a/testprof/modes_test.c +++ b/testprof/modes_test.c @@ -3,8 +3,7 @@ int modes_test(void) { - unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16]; - int cipher_idx; + int ret = CRYPT_NOP; #ifdef LTC_CBC_MODE symmetric_CBC cbc; #endif @@ -14,44 +13,48 @@ int modes_test(void) #ifdef LTC_OFB_MODE symmetric_OFB ofb; #endif +#if defined(LTC_CBC_MODE) || defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE) + unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16]; + int cipher_idx; unsigned long l; - + /* make a random pt, key and iv */ yarrow_read(pt, 64, &yarrow_prng); yarrow_read(key, 16, &yarrow_prng); yarrow_read(iv, 16, &yarrow_prng); - + /* get idx of AES handy */ cipher_idx = find_cipher("aes"); if (cipher_idx == -1) { fprintf(stderr, "test requires AES"); return 1; } - +#endif + #ifdef LTC_F8_MODE - DO(f8_test_mode()); -#endif - + DO(ret = f8_test_mode()); +#endif + #ifdef LTC_LRW_MODE - DO(lrw_test()); + DO(ret = lrw_test()); #endif #ifdef LTC_CBC_MODE /* test CBC mode */ /* encode the block */ - DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc)); + DO(ret = cbc_start(cipher_idx, iv, key, 16, 0, &cbc)); l = sizeof(iv2); - DO(cbc_getiv(iv2, &l, &cbc)); + DO(ret = cbc_getiv(iv2, &l, &cbc)); if (l != 16 || memcmp(iv2, iv, 16)) { fprintf(stderr, "cbc_getiv failed"); return 1; } - DO(cbc_encrypt(pt, ct, 64, &cbc)); - + DO(ret = cbc_encrypt(pt, ct, 64, &cbc)); + /* decode the block */ - DO(cbc_setiv(iv2, l, &cbc)); + DO(ret = cbc_setiv(iv2, l, &cbc)); zeromem(tmp, sizeof(tmp)); - DO(cbc_decrypt(ct, tmp, 64, &cbc)); + DO(ret = cbc_decrypt(ct, tmp, 64, &cbc)); if (memcmp(tmp, pt, 64) != 0) { fprintf(stderr, "CBC failed"); return 1; @@ -61,56 +64,59 @@ int modes_test(void) #ifdef LTC_CFB_MODE /* test CFB mode */ /* encode the block */ - DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb)); + DO(ret = cfb_start(cipher_idx, iv, key, 16, 0, &cfb)); l = sizeof(iv2); - DO(cfb_getiv(iv2, &l, &cfb)); + DO(ret = cfb_getiv(iv2, &l, &cfb)); /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */ if (l != 16) { fprintf(stderr, "cfb_getiv failed"); return 1; } - DO(cfb_encrypt(pt, ct, 64, &cfb)); - + DO(ret = cfb_encrypt(pt, ct, 64, &cfb)); + /* decode the block */ - DO(cfb_setiv(iv, l, &cfb)); + DO(ret = cfb_setiv(iv, l, &cfb)); zeromem(tmp, sizeof(tmp)); - DO(cfb_decrypt(ct, tmp, 64, &cfb)); + DO(ret = cfb_decrypt(ct, tmp, 64, &cfb)); if (memcmp(tmp, pt, 64) != 0) { fprintf(stderr, "CFB failed"); return 1; } #endif - + #ifdef LTC_OFB_MODE /* test OFB mode */ /* encode the block */ - DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb)); + DO(ret = ofb_start(cipher_idx, iv, key, 16, 0, &ofb)); l = sizeof(iv2); - DO(ofb_getiv(iv2, &l, &ofb)); + DO(ret = ofb_getiv(iv2, &l, &ofb)); if (l != 16 || memcmp(iv2, iv, 16)) { fprintf(stderr, "ofb_getiv failed"); return 1; } - DO(ofb_encrypt(pt, ct, 64, &ofb)); - + DO(ret = ofb_encrypt(pt, ct, 64, &ofb)); + /* decode the block */ - DO(ofb_setiv(iv2, l, &ofb)); + DO(ret = ofb_setiv(iv2, l, &ofb)); zeromem(tmp, sizeof(tmp)); - DO(ofb_decrypt(ct, tmp, 64, &ofb)); + DO(ret = ofb_decrypt(ct, tmp, 64, &ofb)); if (memcmp(tmp, pt, 64) != 0) { fprintf(stderr, "OFB failed"); return 1; } #endif -#ifdef LTC_CTR_MODE - DO(ctr_test()); +#if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL) + DO(ret = ctr_test()); #endif #ifdef LTC_XTS_MODE - DO(xts_test()); + DO(ret = xts_test()); #endif - + + if (ret == CRYPT_NOP) + fprintf(stderr, "NOP"); + return 0; } diff --git a/testprof/x86_prof.c b/testprof/x86_prof.c index aa7e86c..ba12db2 100644 --- a/testprof/x86_prof.c +++ b/testprof/x86_prof.c @@ -451,13 +451,14 @@ int time_keysched(void) int time_cipher(void) { + fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n"); +#ifdef LTC_ECB_MODE unsigned long x, y1; ulong64 t1, t2, c1, c2, a1, a2; symmetric_ECB ecb; unsigned char key[MAXBLOCKSIZE], pt[4096]; int err; - fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n"); no_results = 0; for (x = 0; cipher_descriptor[x].name != NULL; x++) { ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb); @@ -516,6 +517,9 @@ int time_cipher(void) #undef DO1 } tally_results(1); +#else + fprintf(stderr, "NOP"); +#endif return 0; } @@ -1312,6 +1316,7 @@ void time_ecc(void) { fprintf(stderr, "NO ECC\n"); } void time_macs_(unsigned long MAC_SIZE) { +#if defined(LTC_OMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_PMAC) || defined(LTC_PELICAN) || defined(LTC_HMAC) unsigned char *buf, key[16], tag[16]; ulong64 t1, t2; unsigned long x, z; @@ -1433,6 +1438,10 @@ void time_macs_(unsigned long MAC_SIZE) #endif XFREE(buf); +#else + LTC_UNUSED_PARAM(MAC_SIZE); + fprintf(stderr, "NO MACs\n"); +#endif } void time_macs(void) @@ -1444,6 +1453,7 @@ void time_macs(void) void time_encmacs_(unsigned long MAC_SIZE) { +#if defined(LTC_EAX_MODE) || defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE) unsigned char *buf, IV[16], key[16], tag[16]; ulong64 t1, t2; unsigned long x, z; @@ -1600,6 +1610,10 @@ __attribute__ ((aligned (16))) } #endif +#else + LTC_UNUSED_PARAM(MAC_SIZE); + fprintf(stderr, "NO ENCMACs\n"); +#endif }