Skip to content

Commit

Permalink
improve tests and timing in regards to MPI provider selection
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaeckel committed Oct 18, 2017
1 parent 442bb90 commit fdc6cd2
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 73 deletions.
92 changes: 69 additions & 23 deletions demos/timing.c
Expand Up @@ -520,20 +520,15 @@ static void time_hash(void)
}

/*#warning you need an mp_rand!!!*/
#if !defined(USE_LTM) && !defined(USE_TFM) && !defined(USE_GMP) && !defined(EXT_MATH_LIB)
#undef LTC_MPI
#undef LTC_TEST_MPI
#else
#define LTC_TEST_MPI
#endif

#ifdef LTC_MPI
static void time_mult(void)
{
ulong64 t1, t2;
unsigned long x, y;
void *a, *b, *c;

if (ltc_mp.name == NULL) return;

fprintf(stderr, "Timing Multiplying:\n");
mp_init_multi(&a,&b,&c,NULL);
for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
Expand Down Expand Up @@ -565,6 +560,8 @@ static void time_sqr(void)
unsigned long x, y;
void *a, *b;

if (ltc_mp.name == NULL) return;

fprintf(stderr, "Timing Squaring:\n");
mp_init_multi(&a,&b,NULL);
for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
Expand All @@ -588,10 +585,6 @@ static void time_sqr(void)
#undef DO1
#undef DO2
}
#else
static void time_mult(void) { fprintf(stderr, "NO MULT\n"); }
static void time_sqr(void) { fprintf(stderr, "NO SQR\n"); }
#endif

static void time_prng(void)
{
Expand Down Expand Up @@ -645,7 +638,7 @@ static void time_prng(void)
}
}

#if defined(LTC_MDSA) && defined(LTC_TEST_MPI)
#if defined(LTC_MDSA)
/* time various DSA operations */
static void time_dsa(void)
{
Expand All @@ -665,6 +658,8 @@ static const struct {
#endif
};

if (ltc_mp.name == NULL) return;

for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
t2 = 0;
for (y = 0; y < 4; y++) {
Expand Down Expand Up @@ -700,7 +695,7 @@ static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); }
#endif


#if defined(LTC_MRSA) && defined(LTC_TEST_MPI)
#if defined(LTC_MRSA)
/* time various RSA operations */
static void time_rsa(void)
{
Expand All @@ -710,6 +705,8 @@ static void time_rsa(void)
unsigned long x, y, z, zzz;
int err, zz, stat;

if (ltc_mp.name == NULL) return;

for (x = 1024; x <= 2048; x += 256) {
t2 = 0;
for (y = 0; y < 4; y++) {
Expand Down Expand Up @@ -824,7 +821,7 @@ static void time_rsa(void)
static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
#endif

#if defined(LTC_MKAT) && defined(LTC_TEST_MPI)
#if defined(LTC_MKAT)
/* time various KAT operations */
static void time_katja(void)
{
Expand All @@ -834,6 +831,8 @@ static void time_katja(void)
unsigned long x, y, z, zzz;
int err, zz;

if (ltc_mp.name == NULL) return;

for (x = 1024; x <= 2048; x += 256) {
t2 = 0;
for (y = 0; y < 4; y++) {
Expand Down Expand Up @@ -894,7 +893,7 @@ static void time_katja(void)
static void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
#endif

#if defined(LTC_MDH) && defined(LTC_TEST_MPI)
#if defined(LTC_MDH)
/* time various DH operations */
static void time_dh(void)
{
Expand All @@ -909,6 +908,8 @@ static void time_dh(void)
100000
};

if (ltc_mp.name == NULL) return;

for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
t2 = 0;
for (y = 0; y < 16; y++) {
Expand Down Expand Up @@ -936,7 +937,7 @@ static void time_dh(void)
static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
#endif

#if defined(LTC_MECC) && defined(LTC_TEST_MPI)
#if defined(LTC_MECC)
/* time various ECC operations */
static void time_ecc(void)
{
Expand Down Expand Up @@ -972,6 +973,8 @@ static void time_ecc(void)
#endif
100000};

if (ltc_mp.name == NULL) return;

for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
t2 = 0;
for (y = 0; y < 256; y++) {
Expand Down Expand Up @@ -1396,6 +1399,45 @@ static void time_encmacs(void)
time_encmacs_(32);
}

static void init_mpi(const char* mpi)
{
switch (mpi[0]) {
#ifdef LTM_DESC
case 'l':
init_LTM();
break;
#endif
#ifdef TFM_DESC
case 't':
init_TFM();
break;
#endif
#ifdef GMP_DESC
case 'g':
init_GMP();
break;
#endif
#ifdef EXT_MATH_LIB
case 'e':
{
extern ltc_math_descriptor EXT_MATH_LIB;
ltc_mp = EXT_MATH_LIB;
}

#define NAME_VALUE(s) #s"="NAME(s)
#define NAME(s) #s
printf("EXT_MATH_LIB = %s\n", NAME_VALUE(EXT_MATH_LIB));
#undef NAME_VALUE
#undef NAME

break;
#endif
default:
printf("Unknown/Invalid MPI provider: %s\n", mpi);
break;
}
}

#define LTC_TEST_FN(f) { f, #f }
int main(int argc, char **argv)
{
Expand Down Expand Up @@ -1425,25 +1467,29 @@ const struct
};
char *single_test = NULL;
unsigned int i;
const char* mpi_provider = NULL;

init_timer();
register_all_ciphers();
register_all_hashes();
register_all_prngs();

#ifdef USE_LTM
ltc_mp = ltm_desc;
mpi_provider = "ltm";
#elif defined(USE_TFM)
ltc_mp = tfm_desc;
mpi_provider = "tfm";
#elif defined(USE_GMP)
ltc_mp = gmp_desc;
mpi_provider = "gmp";
#elif defined(EXT_MATH_LIB)
{
extern ltc_math_descriptor EXT_MATH_LIB;
ltc_mp = EXT_MATH_LIB;
}
mpi_provider = "ext";
#endif

if (argc > 2) {
mpi_provider = argv[2];
}

init_mpi(mpi_provider);

if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
exit(EXIT_FAILURE);
Expand Down
7 changes: 3 additions & 4 deletions tests/der_test.c
Expand Up @@ -7,11 +7,8 @@
* guarantee it works.
*/
#include <tomcrypt_test.h>
#if defined(GMP_LTC_DESC) || defined(USE_GMP)
#include <gmp.h>
#endif

#if !defined(LTC_DER) || !defined(LTC_TEST_MPI)
#if !defined(LTC_DER)

int der_test(void)
{
Expand Down Expand Up @@ -1126,6 +1123,8 @@ int der_test(void)
unsigned char utf8_buf[32];
wchar_t utf8_out[32];

if (ltc_mp.name == NULL) return CRYPT_NOP;

der_cacert_test();

DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
Expand Down
5 changes: 4 additions & 1 deletion tests/dh_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_MDH) && defined(LTC_TEST_MPI)
#if defined(LTC_MDH)

#ifdef LTC_DH4096
#define KEYSIZE 4096
Expand Down Expand Up @@ -433,6 +433,9 @@ static int _basic_test(void)
int dh_test(void)
{
int fails = 0;

if (ltc_mp.name == NULL) return CRYPT_NOP;

if (_prime_test() != CRYPT_OK) fails++;
if (_basic_test() != CRYPT_OK) fails++;
if (_dhparam_test() != CRYPT_OK) fails++;
Expand Down
4 changes: 3 additions & 1 deletion tests/dsa_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_MDSA) && defined(LTC_TEST_MPI)
#if defined(LTC_MDSA)

/* This is the private key from test_dsa.key */
static const unsigned char openssl_priv_dsa[] = {
Expand Down Expand Up @@ -324,6 +324,8 @@ int dsa_test(void)
int stat1, stat2;
dsa_key key, key2;

if (ltc_mp.name == NULL) return CRYPT_NOP;

DO(_dsa_compat_test());
DO(_dsa_wycheproof_test());

Expand Down
4 changes: 3 additions & 1 deletion tests/ecc_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_MECC) && defined(LTC_TEST_MPI)
#if defined(LTC_MECC)

static unsigned int sizes[] = {
#ifdef LTC_ECC112
Expand Down Expand Up @@ -120,6 +120,8 @@ int ecc_tests (void)
int stat, stat2;
ecc_key usera, userb, pubKey, privKey;

if (ltc_mp.name == NULL) return CRYPT_NOP;

DO(ecc_test ());

for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
Expand Down
4 changes: 3 additions & 1 deletion tests/katja_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_MKAT) && defined(LTC_TEST_MPI)
#if defined(LTC_MKAT)

int katja_test(void)
{
Expand All @@ -18,6 +18,8 @@ int katja_test(void)
unsigned long kat_msgsize, len, len2, cnt;
static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };

if (ltc_mp.name == NULL) return CRYPT_NOP;

hash_idx = find_hash("sha1");
prng_idx = find_prng("yarrow");
if (hash_idx == -1 || prng_idx == -1) {
Expand Down
3 changes: 2 additions & 1 deletion tests/mpi_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_MPI) && defined(LTC_TEST_MPI)
#if defined(LTC_MPI)
static int _radix_to_bin_test(void)
{
/* RADIX 16 */
Expand Down Expand Up @@ -133,6 +133,7 @@ static int _radix_to_bin_test(void)

int mpi_test(void)
{
if (ltc_mp.name == NULL) return CRYPT_NOP;
return _radix_to_bin_test();
}
#else
Expand Down
4 changes: 3 additions & 1 deletion tests/pkcs_1_eme_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_PKCS_1) && defined(LTC_TEST_MPI)
#if defined(LTC_PKCS_1)

#include "../notes/rsa-testvectors/pkcs1v15crypt-vectors.c"

Expand All @@ -22,6 +22,8 @@ int pkcs_1_eme_test(void)
unsigned int i;
unsigned int j;

if (ltc_mp.name == NULL) return CRYPT_NOP;

DO(prng_is_valid(prng_idx));
DO(hash_is_valid(hash_idx));

Expand Down
4 changes: 3 additions & 1 deletion tests/pkcs_1_emsa_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_PKCS_1) && defined(LTC_TEST_MPI)
#if defined(LTC_PKCS_1)

#include "../notes/rsa-testvectors/pkcs1v15sign-vectors.c"

Expand All @@ -20,6 +20,8 @@ int pkcs_1_emsa_test(void)
unsigned int i;
unsigned int j;

if (ltc_mp.name == NULL) return CRYPT_NOP;

DO(hash_is_valid(hash_idx));

for (i = 0; i < sizeof(testcases_emsa)/sizeof(testcases_emsa[0]); ++i) {
Expand Down
4 changes: 3 additions & 1 deletion tests/pkcs_1_oaep_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_PKCS_1) && defined(LTC_TEST_MPI)
#if defined(LTC_PKCS_1)

#include "../notes/rsa-testvectors/oaep-vect.c"

Expand All @@ -22,6 +22,8 @@ int pkcs_1_oaep_test(void)
unsigned int i;
unsigned int j;

if (ltc_mp.name == NULL) return CRYPT_NOP;

DO(prng_is_valid(prng_idx));
DO(hash_is_valid(hash_idx));

Expand Down
4 changes: 3 additions & 1 deletion tests/pkcs_1_pss_test.c
Expand Up @@ -8,7 +8,7 @@
*/
#include <tomcrypt_test.h>

#if defined(LTC_PKCS_1) && defined(LTC_TEST_MPI)
#if defined(LTC_PKCS_1)

#include "../notes/rsa-testvectors/pss-vect.c"

Expand All @@ -22,6 +22,8 @@ int pkcs_1_pss_test(void)
unsigned int i;
unsigned int j;

if (ltc_mp.name == NULL) return CRYPT_NOP;

DO(prng_is_valid(prng_idx));
DO(hash_is_valid(hash_idx));

Expand Down

0 comments on commit fdc6cd2

Please sign in to comment.