Browse Source

Merge pull request #297 from libtom/improve/mpi_selection

Improve MPI provider selection
Steffen Jaeckel 8 years ago
parent
commit
49f732101c

+ 30 - 23
demos/timing.c

@@ -520,20 +520,15 @@ static void time_hash(void)
 }
 }
 
 
 /*#warning you need an mp_rand!!!*/
 /*#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)
 static void time_mult(void)
 {
 {
    ulong64 t1, t2;
    ulong64 t1, t2;
    unsigned long x, y;
    unsigned long x, y;
    void  *a, *b, *c;
    void  *a, *b, *c;
 
 
+   if (ltc_mp.name == NULL) return;
+
    fprintf(stderr, "Timing Multiplying:\n");
    fprintf(stderr, "Timing Multiplying:\n");
    mp_init_multi(&a,&b,&c,NULL);
    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) {
    for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
@@ -565,6 +560,8 @@ static void time_sqr(void)
    unsigned long x, y;
    unsigned long x, y;
    void *a, *b;
    void *a, *b;
 
 
+   if (ltc_mp.name == NULL) return;
+
    fprintf(stderr, "Timing Squaring:\n");
    fprintf(stderr, "Timing Squaring:\n");
    mp_init_multi(&a,&b,NULL);
    mp_init_multi(&a,&b,NULL);
    for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
    for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
@@ -588,10 +585,6 @@ static void time_sqr(void)
 #undef DO1
 #undef DO1
 #undef DO2
 #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)
 static void time_prng(void)
 {
 {
@@ -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 */
 /* time various DSA operations */
 static void time_dsa(void)
 static void time_dsa(void)
 {
 {
@@ -665,6 +658,8 @@ static const struct {
 #endif
 #endif
 };
 };
 
 
+   if (ltc_mp.name == NULL) return;
+
    for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
    for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
        t2 = 0;
        t2 = 0;
        for (y = 0; y < 4; y++) {
        for (y = 0; y < 4; y++) {
@@ -700,7 +695,7 @@ static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); }
 #endif
 #endif
 
 
 
 
-#if defined(LTC_MRSA) && defined(LTC_TEST_MPI)
+#if defined(LTC_MRSA)
 /* time various RSA operations */
 /* time various RSA operations */
 static void time_rsa(void)
 static void time_rsa(void)
 {
 {
@@ -710,6 +705,8 @@ static void time_rsa(void)
    unsigned long x, y, z, zzz;
    unsigned long x, y, z, zzz;
    int           err, zz, stat;
    int           err, zz, stat;
 
 
+   if (ltc_mp.name == NULL) return;
+
    for (x = 1024; x <= 2048; x += 256) {
    for (x = 1024; x <= 2048; x += 256) {
        t2 = 0;
        t2 = 0;
        for (y = 0; y < 4; y++) {
        for (y = 0; y < 4; y++) {
@@ -824,7 +821,7 @@ static void time_rsa(void)
 static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
 static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
 #endif
 #endif
 
 
-#if defined(LTC_MKAT) && defined(LTC_TEST_MPI)
+#if defined(LTC_MKAT)
 /* time various KAT operations */
 /* time various KAT operations */
 static void time_katja(void)
 static void time_katja(void)
 {
 {
@@ -834,6 +831,8 @@ static void time_katja(void)
    unsigned long x, y, z, zzz;
    unsigned long x, y, z, zzz;
    int           err, zz;
    int           err, zz;
 
 
+   if (ltc_mp.name == NULL) return;
+
    for (x = 1024; x <= 2048; x += 256) {
    for (x = 1024; x <= 2048; x += 256) {
        t2 = 0;
        t2 = 0;
        for (y = 0; y < 4; y++) {
        for (y = 0; y < 4; y++) {
@@ -894,7 +893,7 @@ static void time_katja(void)
 static void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
 static void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
 #endif
 #endif
 
 
-#if defined(LTC_MDH) && defined(LTC_TEST_MPI)
+#if defined(LTC_MDH)
 /* time various DH operations */
 /* time various DH operations */
 static void time_dh(void)
 static void time_dh(void)
 {
 {
@@ -909,6 +908,8 @@ static void time_dh(void)
                                    100000
                                    100000
    };
    };
 
 
+   if (ltc_mp.name == NULL) return;
+
    for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
    for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
        t2 = 0;
        t2 = 0;
        for (y = 0; y < 16; y++) {
        for (y = 0; y < 16; y++) {
@@ -936,7 +937,7 @@ static void time_dh(void)
 static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
 static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
 #endif
 #endif
 
 
-#if defined(LTC_MECC) && defined(LTC_TEST_MPI)
+#if defined(LTC_MECC)
 /* time various ECC operations */
 /* time various ECC operations */
 static void time_ecc(void)
 static void time_ecc(void)
 {
 {
@@ -972,6 +973,8 @@ static void time_ecc(void)
 #endif
 #endif
 100000};
 100000};
 
 
+   if (ltc_mp.name == NULL) return;
+
    for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
    for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
        t2 = 0;
        t2 = 0;
        for (y = 0; y < 256; y++) {
        for (y = 0; y < 256; y++) {
@@ -1425,6 +1428,7 @@ const struct
 };
 };
 char *single_test = NULL;
 char *single_test = NULL;
 unsigned int i;
 unsigned int i;
+const char* mpi_provider = NULL;
 
 
 init_timer();
 init_timer();
 register_all_ciphers();
 register_all_ciphers();
@@ -1432,18 +1436,21 @@ register_all_hashes();
 register_all_prngs();
 register_all_prngs();
 
 
 #ifdef USE_LTM
 #ifdef USE_LTM
-   ltc_mp = ltm_desc;
+   mpi_provider = "ltm";
 #elif defined(USE_TFM)
 #elif defined(USE_TFM)
-   ltc_mp = tfm_desc;
+   mpi_provider = "tfm";
 #elif defined(USE_GMP)
 #elif defined(USE_GMP)
-   ltc_mp = gmp_desc;
+   mpi_provider = "gmp";
 #elif defined(EXT_MATH_LIB)
 #elif defined(EXT_MATH_LIB)
-   {
-      extern ltc_math_descriptor EXT_MATH_LIB;
-      ltc_mp = EXT_MATH_LIB;
-   }
+   mpi_provider = "ext";
 #endif
 #endif
 
 
+   if (argc > 2) {
+      mpi_provider = argv[2];
+   }
+
+   crypt_mp_init(mpi_provider);
+
 if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
 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));
    fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
    exit(EXIT_FAILURE);
    exit(EXIT_FAILURE);

+ 33 - 12
doc/crypt.tex

@@ -6539,29 +6539,36 @@ int crypt_list_all_constants(        char *names_list,
                              unsigned int *names_list_size);
                              unsigned int *names_list_size);
 \end{verbatim}
 \end{verbatim}
 You may want to call these functions twice, first to get the amount
 You may want to call these functions twice, first to get the amount
-of memory to be allocated for the $names_list$, and a final time to
-actually populate $names_list$.  If $names_list$ is NULL,
-$names_list_size$ will be the minimum size needed to receive the
-complete $names_list$.  If $names_list$ is NOT NULL, $names_list$ must
-be a pointer to sufficient memory into which the $names_list$ will be
-written.  Also, the value in $names_list_size$ sets the upper bound of
+of memory to be allocated for the $names\_list$, and a final time to
+actually populate $names\_list$.  If $names\_list$ is NULL,
+$names\_list\_size$ will be the minimum size needed to receive the
+complete $names\_list$.  If $names\_list$ is NOT NULL, $names\_list$ must
+be a pointer to sufficient memory into which the $names\_list$ will be
+written.  Also, the value in $names\_list\_size$ sets the upper bound of
 the number of characters to be written.  A -1 return value signifies
 the number of characters to be written.  A -1 return value signifies
 insufficient space.
 insufficient space.
 
 
-The format of the $names_list$ string is a series of $name,value$ pairs
+The format of the $names\_list$ string is a series of $name,value$ pairs
 where each name and value is separated by a comma, the pairs are separated
 where each name and value is separated by a comma, the pairs are separated
 by newlines, and the list is null terminated.
 by newlines, and the list is null terminated.
 
 
-Calling either of these functions will initialize the respective
-math library.
+\index{crypt\_mp\_init()}
 \begin{verbatim}
 \begin{verbatim}
-void init_LTM(void);
-void init_TFM(void);
-void init_GMP(void);
+int crypt_mp_init(const char* mpi);
 \end{verbatim}
 \end{verbatim}
 
 
+To ease the setup of a specific math descriptor, in cases where the library was compiled with support for multiple MPI libraries,
+the function \textit{crypt\_mp\_init()} is provided.
+It takes a string to the desired MPI library to use as an argument.
+The three default MPI libraries are identified as follows, \textit{LibTomMath} as \texttt{"ltm"}, \textit{TomsFastmath} as \texttt{"tfm"}
+and the \textit{GNU Multi Precision Arithmetic Library} as \texttt{"gmp"}.
+The identification happens case-insensitive and only on the first character.
+
 Here is a Python program demonstrating how to call various LTC dynamic
 Here is a Python program demonstrating how to call various LTC dynamic
 language support functions.
 language support functions.
+
+A more detailed example is given in the library source in \texttt{demos/demo\_dynamic.py}.
+
 \begin{verbatim}
 \begin{verbatim}
 from ctypes import *
 from ctypes import *
 
 
@@ -8255,6 +8262,20 @@ for RSA--1024 the output is always 128 bytes regardless of how small the numeric
 
 
 Since the function is given the entire RSA key (for private keys only) CRT is possible as prescribed in the PKCS \#1 v2.1 specification.
 Since the function is given the entire RSA key (for private keys only) CRT is possible as prescribed in the PKCS \#1 v2.1 specification.
 
 
+
+\mysection{Deprecated API functions}
+
+\subsection{After v1.18.0}
+
+\index{init\_LTM()} \index{init\_TFM()} \index{init\_GMP()}
+\begin{verbatim}
+void init_LTM(void);
+void init_TFM(void);
+void init_GMP(void);
+\end{verbatim}
+
+These three MPI init functions have been introduced in version 1.18.0 and have been deprecated in the same version in favor of \textit{crypt\_mp\_init()}.
+
 \newpage
 \newpage
 \markboth{Index}{Index}
 \markboth{Index}{Index}
 \input{crypt.ind}
 \input{crypt.ind}

+ 7 - 0
src/headers/tomcrypt_cfg.h

@@ -277,6 +277,13 @@ typedef unsigned long ltc_mp_digit;
    #define LTC_HAVE_BSWAP_BUILTIN
    #define LTC_HAVE_BSWAP_BUILTIN
 #endif
 #endif
 
 
+#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301)
+   #define LTC_DEPRECATED __attribute__((deprecated))
+#elif defined(_MSC_VER)
+   #define LTC_DEPRECATED __declspec(deprecated)
+#else
+   #define LTC_DEPRECATED
+#endif
 
 
 /* ref:         $Format:%D$ */
 /* ref:         $Format:%D$ */
 /* git commit:  $Format:%H$ */
 /* git commit:  $Format:%H$ */

+ 4 - 3
src/headers/tomcrypt_misc.h

@@ -73,14 +73,15 @@ int crypt_get_size(const char* namein, unsigned int *sizeout);
 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size);
 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size);
 
 
 #ifdef LTM_DESC
 #ifdef LTM_DESC
-void init_LTM(void);
+LTC_DEPRECATED void init_LTM(void);
 #endif
 #endif
 #ifdef TFM_DESC
 #ifdef TFM_DESC
-void init_TFM(void);
+LTC_DEPRECATED void init_TFM(void);
 #endif
 #endif
 #ifdef GMP_DESC
 #ifdef GMP_DESC
-void init_GMP(void);
+LTC_DEPRECATED void init_GMP(void);
 #endif
 #endif
+int crypt_mp_init(const char* mpi);
 
 
 #ifdef LTC_ADLER32
 #ifdef LTC_ADLER32
 typedef struct adler32_state_s
 typedef struct adler32_state_s

+ 48 - 0
src/misc/crypt/crypt_inits.c

@@ -37,6 +37,54 @@ void init_GMP(void)
 }
 }
 #endif
 #endif
 
 
+int crypt_mp_init(const char* mpi)
+{
+   if (mpi == NULL) return CRYPT_ERROR;
+   switch (mpi[0]) {
+#ifdef LTM_DESC
+      case 'l':
+      case 'L':
+         ltc_mp = ltm_desc;
+         return CRYPT_OK;
+#endif
+#ifdef TFM_DESC
+      case 't':
+      case 'T':
+         ltc_mp = tfm_desc;
+         return CRYPT_OK;
+#endif
+#ifdef GMP_DESC
+      case 'g':
+      case 'G':
+         ltc_mp = gmp_desc;
+         return CRYPT_OK;
+#endif
+#ifdef EXT_MATH_LIB
+      case 'e':
+      case 'E':
+         {
+            extern ltc_math_descriptor EXT_MATH_LIB;
+            ltc_mp = EXT_MATH_LIB;
+         }
+
+#if defined(LTC_TEST_DBG)
+#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
+#endif
+
+         return CRYPT_OK;
+#endif
+      default:
+#if defined(LTC_TEST_DBG)
+         printf("Unknown/Invalid MPI provider: %s\n", mpi);
+#endif
+         return CRYPT_ERROR;
+   }
+}
+
 
 
 /* ref:         $Format:%D$ */
 /* ref:         $Format:%D$ */
 /* git commit:  $Format:%H$ */
 /* git commit:  $Format:%H$ */

+ 3 - 4
tests/der_test.c

@@ -7,11 +7,8 @@
  * guarantee it works.
  * guarantee it works.
  */
  */
 #include <tomcrypt_test.h>
 #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)
 int der_test(void)
 {
 {
@@ -1126,6 +1123,8 @@ int der_test(void)
    unsigned char utf8_buf[32];
    unsigned char utf8_buf[32];
    wchar_t utf8_out[32];
    wchar_t utf8_out[32];
 
 
+   if (ltc_mp.name == NULL) return CRYPT_NOP;
+
    der_cacert_test();
    der_cacert_test();
 
 
    DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));
    DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, NULL));

+ 4 - 1
tests/dh_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #include <tomcrypt_test.h>
 
 
-#if defined(LTC_MDH) && defined(LTC_TEST_MPI)
+#if defined(LTC_MDH)
 
 
 #ifdef LTC_DH4096
 #ifdef LTC_DH4096
 #define KEYSIZE 4096
 #define KEYSIZE 4096
@@ -433,6 +433,9 @@ static int _basic_test(void)
 int dh_test(void)
 int dh_test(void)
 {
 {
    int fails = 0;
    int fails = 0;
+
+   if (ltc_mp.name == NULL) return CRYPT_NOP;
+
    if (_prime_test() != CRYPT_OK) fails++;
    if (_prime_test() != CRYPT_OK) fails++;
    if (_basic_test() != CRYPT_OK) fails++;
    if (_basic_test() != CRYPT_OK) fails++;
    if (_dhparam_test() != CRYPT_OK) fails++;
    if (_dhparam_test() != CRYPT_OK) fails++;

+ 3 - 1
tests/dsa_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #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 */
 /* This is the private key from test_dsa.key */
 static const unsigned char openssl_priv_dsa[] = {
 static const unsigned char openssl_priv_dsa[] = {
@@ -324,6 +324,8 @@ int dsa_test(void)
    int stat1, stat2;
    int stat1, stat2;
    dsa_key key, key2;
    dsa_key key, key2;
 
 
+   if (ltc_mp.name == NULL) return CRYPT_NOP;
+
    DO(_dsa_compat_test());
    DO(_dsa_compat_test());
    DO(_dsa_wycheproof_test());
    DO(_dsa_wycheproof_test());
 
 

+ 3 - 1
tests/ecc_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #include <tomcrypt_test.h>
 
 
-#if defined(LTC_MECC) && defined(LTC_TEST_MPI)
+#if defined(LTC_MECC)
 
 
 static unsigned int sizes[] = {
 static unsigned int sizes[] = {
 #ifdef LTC_ECC112
 #ifdef LTC_ECC112
@@ -120,6 +120,8 @@ int ecc_tests (void)
   int           stat, stat2;
   int           stat, stat2;
   ecc_key usera, userb, pubKey, privKey;
   ecc_key usera, userb, pubKey, privKey;
 
 
+  if (ltc_mp.name == NULL) return CRYPT_NOP;
+
   DO(ecc_test ());
   DO(ecc_test ());
 
 
   for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {
   for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) {

+ 3 - 1
tests/katja_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #include <tomcrypt_test.h>
 
 
-#if defined(LTC_MKAT) && defined(LTC_TEST_MPI)
+#if defined(LTC_MKAT)
 
 
 int katja_test(void)
 int katja_test(void)
 {
 {
@@ -18,6 +18,8 @@ int katja_test(void)
    unsigned long kat_msgsize, len, len2, cnt;
    unsigned long kat_msgsize, len, len2, cnt;
    static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
    static unsigned char lparam[] = { 0x01, 0x02, 0x03, 0x04 };
 
 
+   if (ltc_mp.name == NULL) return CRYPT_NOP;
+
    hash_idx = find_hash("sha1");
    hash_idx = find_hash("sha1");
    prng_idx = find_prng("yarrow");
    prng_idx = find_prng("yarrow");
    if (hash_idx == -1 || prng_idx == -1) {
    if (hash_idx == -1 || prng_idx == -1) {

+ 2 - 1
tests/mpi_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include  <tomcrypt_test.h>
 #include  <tomcrypt_test.h>
 
 
-#if defined(LTC_MPI) && defined(LTC_TEST_MPI)
+#if defined(LTC_MPI)
 static int _radix_to_bin_test(void)
 static int _radix_to_bin_test(void)
 {
 {
    /* RADIX 16 */
    /* RADIX 16 */
@@ -133,6 +133,7 @@ static int _radix_to_bin_test(void)
 
 
 int mpi_test(void)
 int mpi_test(void)
 {
 {
+   if (ltc_mp.name == NULL) return CRYPT_NOP;
    return _radix_to_bin_test();
    return _radix_to_bin_test();
 }
 }
 #else
 #else

+ 3 - 1
tests/pkcs_1_eme_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #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"
 #include "../notes/rsa-testvectors/pkcs1v15crypt-vectors.c"
 
 
@@ -22,6 +22,8 @@ int pkcs_1_eme_test(void)
   unsigned int i;
   unsigned int i;
   unsigned int j;
   unsigned int j;
 
 
+  if (ltc_mp.name == NULL) return CRYPT_NOP;
+
   DO(prng_is_valid(prng_idx));
   DO(prng_is_valid(prng_idx));
   DO(hash_is_valid(hash_idx));
   DO(hash_is_valid(hash_idx));
 
 

+ 3 - 1
tests/pkcs_1_emsa_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #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"
 #include "../notes/rsa-testvectors/pkcs1v15sign-vectors.c"
 
 
@@ -20,6 +20,8 @@ int pkcs_1_emsa_test(void)
   unsigned int i;
   unsigned int i;
   unsigned int j;
   unsigned int j;
 
 
+  if (ltc_mp.name == NULL) return CRYPT_NOP;
+
   DO(hash_is_valid(hash_idx));
   DO(hash_is_valid(hash_idx));
 
 
   for (i = 0; i < sizeof(testcases_emsa)/sizeof(testcases_emsa[0]); ++i) {
   for (i = 0; i < sizeof(testcases_emsa)/sizeof(testcases_emsa[0]); ++i) {

+ 3 - 1
tests/pkcs_1_oaep_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #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"
 #include "../notes/rsa-testvectors/oaep-vect.c"
 
 
@@ -22,6 +22,8 @@ int pkcs_1_oaep_test(void)
   unsigned int i;
   unsigned int i;
   unsigned int j;
   unsigned int j;
 
 
+  if (ltc_mp.name == NULL) return CRYPT_NOP;
+
   DO(prng_is_valid(prng_idx));
   DO(prng_is_valid(prng_idx));
   DO(hash_is_valid(hash_idx));
   DO(hash_is_valid(hash_idx));
 
 

+ 3 - 1
tests/pkcs_1_pss_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #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"
 #include "../notes/rsa-testvectors/pss-vect.c"
 
 
@@ -22,6 +22,8 @@ int pkcs_1_pss_test(void)
   unsigned int i;
   unsigned int i;
   unsigned int j;
   unsigned int j;
 
 
+  if (ltc_mp.name == NULL) return CRYPT_NOP;
+
   DO(prng_is_valid(prng_idx));
   DO(prng_is_valid(prng_idx));
   DO(hash_is_valid(hash_idx));
   DO(hash_is_valid(hash_idx));
 
 

+ 3 - 1
tests/rsa_test.c

@@ -8,7 +8,7 @@
  */
  */
 #include <tomcrypt_test.h>
 #include <tomcrypt_test.h>
 
 
-#if defined(LTC_MRSA) && defined(LTC_TEST_MPI)
+#if defined(LTC_MRSA)
 
 
 #define RSA_MSGSIZE 78
 #define RSA_MSGSIZE 78
 
 
@@ -355,6 +355,8 @@ int rsa_test(void)
    unsigned char* p2;
    unsigned char* p2;
    unsigned char* p3;
    unsigned char* p3;
 
 
+   if (ltc_mp.name == NULL) return CRYPT_NOP;
+
    if (rsa_compat_test() != 0) {
    if (rsa_compat_test() != 0) {
       return 1;
       return 1;
    }
    }

+ 17 - 22
tests/test.c

@@ -299,6 +299,7 @@ int main(int argc, char **argv)
 #endif
 #endif
    int x, pass = 0, fail = 0, nop = 0;
    int x, pass = 0, fail = 0, nop = 0;
    size_t fn_len, i, dots;
    size_t fn_len, i, dots;
+   const char* mpi_provider = NULL;
    char *single_test = NULL;
    char *single_test = NULL;
    ulong64 ts;
    ulong64 ts;
    long delta, dur, real = 0;
    long delta, dur, real = 0;
@@ -307,34 +308,28 @@ int main(int argc, char **argv)
    printf("LTC_VERSION  = %s\n%s\n\n", GIT_VERSION, crypt_build_settings);
    printf("LTC_VERSION  = %s\n%s\n\n", GIT_VERSION, crypt_build_settings);
 
 
 #ifdef USE_LTM
 #ifdef USE_LTM
-   ltc_mp = ltm_desc;
-   printf("MP_PROVIDER  = LibTomMath\n");
+   mpi_provider = "ltm";
 #elif defined(USE_TFM)
 #elif defined(USE_TFM)
-   ltc_mp = tfm_desc;
-   printf("MP_PROVIDER  = TomsFastMath\n");
+   mpi_provider = "tfm";
 #elif defined(USE_GMP)
 #elif defined(USE_GMP)
-   ltc_mp = gmp_desc;
-   printf("MP_PROVIDER  = GnuMP\n");
+   mpi_provider = "gmp";
 #elif defined(EXT_MATH_LIB)
 #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];
    }
    }
 
 
-#define NAME_VALUE(s) #s"="NAME(s)
-#define NAME(s) #s
-   printf("MP_PROVIDER  = %s\n", NAME_VALUE(EXT_MATH_LIB));
-#undef NAME_VALUE
-#undef NAME
+   crypt_mp_init(mpi_provider);
 
 
-#endif
-#ifdef LTC_TEST_MPI
-   printf("MP_DIGIT_BIT = %d\n", MP_DIGIT_BIT);
-#else
-   printf("NO math provider selected, all tests requiring MPI were disabled and will 'nop'\n");
-#endif
-
-   printf("sizeof(ltc_mp_digit) = %d\n", (int)sizeof(ltc_mp_digit));
+   if (ltc_mp.name != NULL) {
+      printf("MP_PROVIDER  = %s\n", ltc_mp.name);
+      printf("MP_DIGIT_BIT = %d\n", MP_DIGIT_BIT);
+      printf("sizeof(ltc_mp_digit) = %d\n", (int)sizeof(ltc_mp_digit));
+   } else {
+      printf("NO math provider selected, all tests requiring MPI will 'nop'\n");
+   }
 
 
 #ifdef LTC_PTHREAD
 #ifdef LTC_PTHREAD
    tinfo = XCALLOC(sizeof(test_functions)/sizeof(test_functions[0]), sizeof(thread_info));
    tinfo = XCALLOC(sizeof(test_functions)/sizeof(test_functions[0]), sizeof(thread_info));

+ 0 - 14
tests/tomcrypt_test.h

@@ -14,20 +14,6 @@
 
 
 #include "common.h"
 #include "common.h"
 
 
-#ifdef USE_LTM
-/* Use libtommath as MPI provider */
-#define LTC_TEST_MPI
-#elif defined(USE_TFM)
-/* Use tomsfastmath as MPI provider */
-#define LTC_TEST_MPI
-#elif defined(USE_GMP)
-/* Use GNU Multiple Precision Arithmetic Library as MPI provider */
-#define LTC_TEST_MPI
-#elif defined(EXT_MATH_LIB)
-/* The user must define his own MPI provider! */
-#define LTC_TEST_MPI
-#endif
-
 typedef struct {
 typedef struct {
     char *name, *prov, *req;
     char *name, *prov, *req;
     int  (*entry)(void);
     int  (*entry)(void);