diff --git a/.ci/meta_builds.sh b/.ci/meta_builds.sh index 4240c686f..d673ad058 100755 --- a/.ci/meta_builds.sh +++ b/.ci/meta_builds.sh @@ -80,7 +80,7 @@ make clean &>/dev/null EXTRALIBS="$5" -echo $2 | grep -q GMP && EXTRALIBS="$EXTRALIBS -lgmp" +echo $* | grep -q GMP && EXTRALIBS="$EXTRALIBS -lgmp" if [ -z "$(echo $CC | grep "clang")" ]; then run_gcc "$1" "$2" "$3" "$4" "$EXTRALIBS" @@ -90,11 +90,11 @@ fi make clean &>/dev/null -bash .ci/testbuild.sh "NOTEST" "-DLTC_NO_TEST" "$3" "$4" "$5" +bash .ci/testbuild.sh "NOTEST" "-DLTC_NO_TEST" "$3" "$4" "$EXTRALIBS" make clean &>/dev/null -bash .ci/testbuild.sh "NOFILE" "-DLTC_NO_FILE" "$3" "$4" "$5" +bash .ci/testbuild.sh "NOFILE" "-DLTC_NO_FILE" "$3" "$4" "$EXTRALIBS" make clean &>/dev/null diff --git a/.ci/testbuild.sh b/.ci/testbuild.sh index 96810f9da..0159774d6 100755 --- a/.ci/testbuild.sh +++ b/.ci/testbuild.sh @@ -14,7 +14,7 @@ echo "$1 (Build Only, $2, $3)..." make clean 1>/dev/null 2>/dev/null echo -n "building..." touch testok.txt -CFLAGS="$2 $CFLAGS $4" EXTRALIBS="$5" make -j$MAKE_JOBS -f $3 test tv_gen 1>gcc_1.txt 2>gcc_2.txt || (echo "build $1 failed see gcc_2.txt for more information" && cat gcc_2.txt && rm -f testok.txt && exit 1) +make -j$MAKE_JOBS -f $3 test tv_gen CFLAGS="$2 $CFLAGS $4" EXTRALIBS="$5" 1>gcc_1.txt 2>gcc_2.txt || (echo "build $1 failed see gcc_2.txt for more information" && cat gcc_2.txt && rm -f testok.txt && exit 1) if find testok.txt -type f 1>/dev/null 2>/dev/null ; then echo "successful" exit 0 diff --git a/.travis.yml b/.travis.yml index d8128f66e..19e7af068 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,8 +34,8 @@ compiler: - gcc - clang script: - - bash "${BUILDSCRIPT}" "${BUILDNAME}" "${BUILDOPTIONS}" "makefile V=1" "-DUSE_LTM -DLTM_DESC" "-ltommath" - - bash "${BUILDSCRIPT}" "${BUILDNAME}" "${BUILDOPTIONS}" "makefile.shared V=1" "-DUSE_TFM -DTFM_DESC" "-ltfm" + - bash "${BUILDSCRIPT}" "${BUILDNAME}" "-DUSE_LTM -DLTM_DESC" "makefile V=1" "${BUILDOPTIONS}" "-ltommath" && + bash "${BUILDSCRIPT}" "${BUILDNAME}" "-DUSE_TFM -DTFM_DESC" "makefile.shared V=1" "${BUILDOPTIONS}" "-ltfm" env: - | BUILDSCRIPT=".ci/meta_builds.sh" @@ -49,6 +49,10 @@ env: BUILDSCRIPT=".ci/run.sh" BUILDNAME="STOCK" BUILDOPTIONS=" " + - | + BUILDSCRIPT=".ci/run.sh" + BUILDNAME="STOCK-MPI" + BUILDOPTIONS="-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM" - | BUILDSCRIPT=".ci/run.sh" BUILDNAME="EASY" @@ -109,6 +113,22 @@ env: BUILDSCRIPT=".ci/run.sh" BUILDNAME="CLEANSTACK+NOTABLES+SMALL+NO_ASM+NO_TIMING_RESISTANCE+LTC_FORTUNA_RESEED_RATELIMIT_STATIC+PTHREAD" BUILDOPTIONS="-DLTC_CLEAN_STACK -DLTC_NO_TABLES -DLTC_SMALL_CODE -DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC -DLTC_PTHREAD" + - | + BUILDSCRIPT=".ci/run.sh" + BUILDNAME="STOCK+ARGTYPE=1" + BUILDOPTIONS="-DARGTYPE=1" + - | + BUILDSCRIPT=".ci/run.sh" + BUILDNAME="STOCK+ARGTYPE=2" + BUILDOPTIONS="-DARGTYPE=2" + - | + BUILDSCRIPT=".ci/run.sh" + BUILDNAME="STOCK+ARGTYPE=3" + BUILDOPTIONS="-DARGTYPE=3" + - | + BUILDSCRIPT=".ci/run.sh" + BUILDNAME="STOCK+ARGTYPE=4" + BUILDOPTIONS="-DARGTYPE=4" after_failure: - cat test_std.txt diff --git a/demos/gcm-file/gcm_file.c b/demos/gcm-file/gcm_file.c index ade1de364..e2a6c1247 100644 --- a/demos/gcm-file/gcm_file.c +++ b/demos/gcm-file/gcm_file.c @@ -37,6 +37,7 @@ @param out The output file @param taglen The MAC tag length @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT) + @param res [out] Result of the operation, 1==valid, 0==invalid @return CRYPT_OK on success */ int gcm_file( int cipher, @@ -56,6 +57,8 @@ int gcm_file( int cipher, LTC_ARGCHK(out != NULL); LTC_ARGCHK(res != NULL); + *res = 0; + f_in = fopen(in, "rb"); if (f_in == NULL) { err = CRYPT_FILE_NOTFOUND; diff --git a/demos/gcm-file/gcm_filehandle.c b/demos/gcm-file/gcm_filehandle.c index e30fdf498..1df76a0a7 100644 --- a/demos/gcm-file/gcm_filehandle.c +++ b/demos/gcm-file/gcm_filehandle.c @@ -55,6 +55,7 @@ @param out The output file @param taglen The MAC tag length @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT) + @param res [out] Result of the operation, 1==valid, 0==invalid @return CRYPT_OK on success */ int gcm_filehandle( int cipher, diff --git a/demos/ltcrypt.c b/demos/ltcrypt.c index 5e0831ed8..a8226adf9 100644 --- a/demos/ltcrypt.c +++ b/demos/ltcrypt.c @@ -18,7 +18,7 @@ #include -static int NORETURN usage(char *name) +static int LTC_NORETURN usage(char *name) { int x; diff --git a/demos/tv_gen.c b/demos/tv_gen.c index 82aa6df69..5b340094c 100644 --- a/demos/tv_gen.c +++ b/demos/tv_gen.c @@ -786,9 +786,6 @@ int main(void) #elif defined(EXT_MATH_LIB) extern ltc_math_descriptor EXT_MATH_LIB; ltc_mp = EXT_MATH_LIB; -#else - fprintf(stderr, "No MPI provider available\n"); - exit(EXIT_FAILURE); #endif printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n"); @@ -816,8 +813,10 @@ int main(void) printf("Generating GCM vectors..."); fflush(stdout); gcm_gen(); printf("done\n"); #endif printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); - printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n"); - printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n"); + if (ltc_mp.name != NULL) { + printf("Generating MATH vectors..."); fflush(stdout); math_gen(); printf("done\n"); + printf("Generating ECC vectors..."); fflush(stdout); ecc_gen(); printf("done\n"); + } #ifdef LTC_LRW_MODE printf("Generating LRW vectors..."); fflush(stdout); lrw_gen(); printf("done\n"); #endif diff --git a/makefile b/makefile index 1cdd3b7c7..0dfd4739d 100644 --- a/makefile +++ b/makefile @@ -71,7 +71,7 @@ $(1): $(call print-help,$(1),Builds the library and the '$(1)' demo) demos/$(1). ifneq ($V,1) @echo " * $${CC} $$@" endif - $${silent} $$(CC) $$< $$(LIB_PRE) $$(LIBNAME) $$(LIB_POST) $$(LTC_EXTRALIBS) -o $(1) + $${silent} $$(CC) $$(LTC_LDFLAGS) $$< $$(LIB_PRE) $$(LIBNAME) $$(LIB_POST) $$(LTC_EXTRALIBS) -o $(1) endef $(foreach demo, $(strip $(DEMOS)), $(eval $(call DEMO_template,$(demo)))) diff --git a/src/headers/tomcrypt_argchk.h b/src/headers/tomcrypt_argchk.h index be9ef0f52..8c3191920 100644 --- a/src/headers/tomcrypt_argchk.h +++ b/src/headers/tomcrypt_argchk.h @@ -9,18 +9,13 @@ /* Defines the LTC_ARGCHK macro used within the library */ /* ARGTYPE is defined in tomcrypt_cfg.h */ + +/* ARGTYPE is per default defined to 0 */ #if ARGTYPE == 0 #include -/* this is the default LibTomCrypt macro */ -#if defined(__clang__) || defined(__GNUC_MINOR__) -#define NORETURN __attribute__ ((noreturn)) -#else -#define NORETURN -#endif - -void crypt_argchk(const char *v, const char *s, int d) NORETURN; +void crypt_argchk(const char *v, const char *s, int d) LTC_NORETURN; #define LTC_ARGCHK(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0) #define LTC_ARGCHKVD(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0) @@ -37,7 +32,7 @@ void crypt_argchk(const char *v, const char *s, int d) NORETURN; #elif ARGTYPE == 3 -#define LTC_ARGCHK(x) +#define LTC_ARGCHK(x) LTC_UNUSED_PARAM(x) #define LTC_ARGCHKVD(x) LTC_ARGCHK(x) #elif ARGTYPE == 4 diff --git a/src/headers/tomcrypt_cfg.h b/src/headers/tomcrypt_cfg.h index 5d64ca7d4..116fd1c45 100644 --- a/src/headers/tomcrypt_cfg.h +++ b/src/headers/tomcrypt_cfg.h @@ -61,6 +61,14 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2); #define LTC_INLINE #endif +#if defined(__clang__) || defined(__GNUC_MINOR__) +#define LTC_NORETURN __attribute__ ((noreturn)) +#elif defined(_MSC_VER) +#define LTC_NORETURN __declspec(noreturn) +#else +#define LTC_NORETURN +#endif + /* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */ #ifndef ARGTYPE #define ARGTYPE 0 diff --git a/src/headers/tomcrypt_custom.h b/src/headers/tomcrypt_custom.h index aedf08bc3..d4b72a532 100644 --- a/src/headers/tomcrypt_custom.h +++ b/src/headers/tomcrypt_custom.h @@ -554,7 +554,7 @@ #endif #endif -#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) +#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_SSH) /* Include the MPI functionality? (required by the PK algorithms) */ #define LTC_MPI diff --git a/src/math/ltm_desc.c b/src/math/ltm_desc.c index 0ee7958dd..56b46ef08 100644 --- a/src/math/ltm_desc.c +++ b/src/math/ltm_desc.c @@ -412,9 +412,7 @@ static int isprime(void *a, int b, int *c) int err; LTC_ARGCHK(a != NULL); LTC_ARGCHK(c != NULL); - if (b == 0) { - b = LTC_MILLER_RABIN_REPS; - } /* if */ + b = mp_prime_rabin_miller_trials(mp_count_bits(a)); err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c)); *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO; return err; diff --git a/src/stream/salsa20/xsalsa20_test.c b/src/stream/salsa20/xsalsa20_test.c index 99616f393..8b9b93f71 100644 --- a/src/stream/salsa20/xsalsa20_test.c +++ b/src/stream/salsa20/xsalsa20_test.c @@ -18,7 +18,7 @@ #ifdef LTC_XSALSA20 -#ifdef LTC_SHA256 +#if defined(LTC_SHA256) && defined(LTC_TEST) static int _sha256(unsigned char *hash, const unsigned char *data, const int datalen) { hash_state md; sha256_init(&md); diff --git a/tests/ecc_test.c b/tests/ecc_test.c index 880ded55b..92b227115 100644 --- a/tests/ecc_test.c +++ b/tests/ecc_test.c @@ -296,10 +296,9 @@ static int _ecc_test_mp(void) void *a, *modulus, *order; ecc_point *G, *GG; int i, err, primality; + char buf[4096]; - if ((err = mp_init_multi(&modulus, &order, &a, NULL)) != CRYPT_OK) { - return err; - } + DO(mp_init_multi(&modulus, &order, &a, NULL)); G = ltc_ecc_new_point(); GG = ltc_ecc_new_point(); @@ -310,34 +309,40 @@ static int _ecc_test_mp(void) return CRYPT_MEM; } + err = CRYPT_OK; + for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) { - if ((err = mp_read_radix(a, (char *)ltc_ecc_curves[i].A, 16)) != CRYPT_OK) { goto done; } - if ((err = mp_read_radix(modulus, (char *)ltc_ecc_curves[i].prime, 16)) != CRYPT_OK) { goto done; } - if ((err = mp_read_radix(order, (char *)ltc_ecc_curves[i].order, 16)) != CRYPT_OK) { goto done; } + DO(mp_read_radix(a, (char *)ltc_ecc_curves[i].A, 16)); + DO(mp_read_radix(modulus, (char *)ltc_ecc_curves[i].prime, 16)); + DO(mp_read_radix(order, (char *)ltc_ecc_curves[i].order, 16)); /* is prime actually prime? */ - if ((err = mp_prime_is_prime(modulus, 8, &primality)) != CRYPT_OK) { goto done; } + DO(mp_prime_is_prime(modulus, 8, &primality)); if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; - goto done; + mp_tohex(modulus, buf); + printf("Modulus failed prime check: %s\n", buf); } /* is order prime ? */ - if ((err = mp_prime_is_prime(order, 8, &primality)) != CRYPT_OK) { goto done; } + DO(mp_prime_is_prime(order, 8, &primality)); if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; - goto done; + mp_tohex(order, buf); + printf("Order failed prime check: %s\n", buf); } - if ((err = mp_read_radix(G->x, (char *)ltc_ecc_curves[i].Gx, 16)) != CRYPT_OK) { goto done; } - if ((err = mp_read_radix(G->y, (char *)ltc_ecc_curves[i].Gy, 16)) != CRYPT_OK) { goto done; } + DO(mp_read_radix(G->x, (char *)ltc_ecc_curves[i].Gx, 16)); + DO(mp_read_radix(G->y, (char *)ltc_ecc_curves[i].Gy, 16)); mp_set(G->z, 1); /* then we should have G == (order + 1)G */ - if ((err = mp_add_d(order, 1, order)) != CRYPT_OK) { goto done; } - if ((err = ltc_mp.ecc_ptmul(order, G, GG, a, modulus, 1)) != CRYPT_OK) { goto done; } + DO(mp_add_d(order, 1, order)); + DO(ltc_mp.ecc_ptmul(order, G, GG, a, modulus, 1)); if (mp_cmp(G->x, GG->x) != LTC_MP_EQ || mp_cmp(G->y, GG->y) != LTC_MP_EQ) { err = CRYPT_FAIL_TESTVECTOR; + } + if (err != CRYPT_OK) { goto done; } } diff --git a/tests/misc_test.c b/tests/misc_test.c index 67648f973..2847af8a0 100644 --- a/tests/misc_test.c +++ b/tests/misc_test.c @@ -35,7 +35,7 @@ int misc_test(void) DO(crc32_test()); #endif #ifdef LTC_SSH - DO(ssh_test()); + ssh_test(); #endif return 0; } diff --git a/tests/no_prng.c b/tests/no_prng.c index 07e0b0fa4..834a0a638 100644 --- a/tests/no_prng.c +++ b/tests/no_prng.c @@ -163,10 +163,15 @@ static const struct ltc_prng_descriptor no_prng_desc = struct ltc_prng_descriptor* no_prng_desc_get(void) { + int ret; no_prng_desc_t* no_prng = XMALLOC(sizeof(*no_prng)); - LTC_ARGCHK(no_prng != NULL); + if (no_prng == NULL) return NULL; XMEMCPY(&no_prng->desc, &no_prng_desc, sizeof(no_prng_desc)); - LTC_ARGCHK(snprintf(no_prng->name, sizeof(no_prng->name), "no_prng@%p", no_prng) < (int)sizeof(no_prng->name)); + ret = snprintf(no_prng->name, sizeof(no_prng->name), "no_prng@%p", no_prng); + if((ret >= (int)sizeof(no_prng->name)) || (ret == -1)) { + XFREE(no_prng); + return NULL; + } no_prng->desc.name = no_prng->name; return &no_prng->desc; } @@ -174,8 +179,8 @@ struct ltc_prng_descriptor* no_prng_desc_get(void) void no_prng_desc_free(struct ltc_prng_descriptor* prng) { no_prng_desc_t *no_prng = (no_prng_desc_t*) prng; - LTC_ARGCHK(no_prng != NULL); - LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name)); + LTC_ARGCHKVD(no_prng != NULL); + LTC_ARGCHKVD(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name)); XFREE(no_prng); } diff --git a/tests/ssh_test.c b/tests/ssh_test.c index 942a745de..5ca3ddfbd 100644 --- a/tests/ssh_test.c +++ b/tests/ssh_test.c @@ -294,13 +294,20 @@ static int _ssh_decoding_test(void) */ int ssh_test(void) { + if (ltc_mp.name == NULL) return CRYPT_NOP; + DO(_ssh_encoding_test()); DO(_ssh_decoding_test()); return CRYPT_OK; } +#else +int ssh_test(void) +{ + return CRYPT_NOP; +} #endif diff --git a/tests/tomcrypt_test.h b/tests/tomcrypt_test.h index c89573c12..20c813e34 100644 --- a/tests/tomcrypt_test.h +++ b/tests/tomcrypt_test.h @@ -44,10 +44,7 @@ int multi_test(void); int prng_test(void); int mpi_test(void); int padding_test(void); - -#ifdef LTC_SSH int ssh_test(void); -#endif #ifdef LTC_PKCS_1 struct ltc_prng_descriptor* no_prng_desc_get(void);