libtomcrypt/tests/test.c

436 lines
10 KiB
C
Raw Normal View History

2020-06-17 08:15:27 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2005-04-17 11:37:13 +00:00
#include <tomcrypt_test.h>
2017-05-02 08:35:00 +00:00
#ifndef GIT_VERSION
#define GIT_VERSION "Undefined version"
#endif
#define LTC_TEST_FN(f) { f, #f }
2017-05-11 12:49:57 +00:00
typedef struct {
int (*fn)(void);
const char* name;
2017-05-11 12:49:57 +00:00
} test_function;
static const test_function test_functions[] =
{
LTC_TEST_FN(store_test),
2017-03-27 17:32:05 +00:00
LTC_TEST_FN(rotate_test),
LTC_TEST_FN(misc_test),
2017-06-28 12:02:25 +00:00
LTC_TEST_FN(mpi_test),
LTC_TEST_FN(cipher_hash_test),
LTC_TEST_FN(mac_test),
LTC_TEST_FN(modes_test),
2017-06-08 11:25:40 +00:00
LTC_TEST_FN(der_test),
LTC_TEST_FN(pkcs_1_test),
LTC_TEST_FN(pkcs_1_pss_test),
LTC_TEST_FN(pkcs_1_oaep_test),
LTC_TEST_FN(pkcs_1_emsa_test),
LTC_TEST_FN(pkcs_1_eme_test),
LTC_TEST_FN(rsa_test),
LTC_TEST_FN(dh_test),
2018-11-07 08:25:08 +00:00
LTC_TEST_FN(ecc_test),
LTC_TEST_FN(dsa_test),
2018-02-20 15:50:55 +00:00
LTC_TEST_FN(ed25519_test),
LTC_TEST_FN(x25519_test),
2017-04-21 14:12:18 +00:00
LTC_TEST_FN(file_test),
2017-04-23 20:04:39 +00:00
LTC_TEST_FN(multi_test),
2017-06-09 11:34:26 +00:00
/* keep the prng_test always at the end as
* it has to be handled specially when
* testing with LTC_PTHREAD enabled
*/
2017-06-08 09:51:36 +00:00
LTC_TEST_FN(prng_test),
};
2017-05-11 12:49:57 +00:00
2017-05-24 19:16:43 +00:00
#if defined(_WIN32)
#include <windows.h> /* GetSystemTimeAsFileTime */
#else
#include <sys/time.h>
#endif
/* microseconds since 1970 (UNIX epoch) */
static ulong64 epoch_usec(void)
{
#if defined(LTC_NO_TEST_TIMING)
return 0;
#elif defined(_WIN32)
FILETIME CurrentTime;
ulong64 cur_time;
ULARGE_INTEGER ul;
GetSystemTimeAsFileTime(&CurrentTime);
ul.LowPart = CurrentTime.dwLowDateTime;
ul.HighPart = CurrentTime.dwHighDateTime;
cur_time = ul.QuadPart; /* now we have 100ns intervals since 1 January 1601 */
cur_time -= CONST64(116444736000000000); /* subtract 100ns intervals between 1601-1970 */
cur_time /= 10; /* 100ns intervals > microseconds */
2017-05-24 19:16:43 +00:00
return cur_time;
#elif defined(LTC_CLOCK_GETTIME)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ulong64)(ts.tv_sec) * 1000000 + (ulong64)(ts.tv_nsec) / 1000; /* get microseconds */
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (ulong64)(tv.tv_sec) * 1000000 + (ulong64)(tv.tv_usec); /* get microseconds */
2017-05-24 19:16:43 +00:00
#endif
}
2017-05-11 12:49:57 +00:00
#ifdef LTC_PTHREAD
typedef struct
{
pthread_t thread_id;
const test_function* t;
int err;
2017-06-09 11:34:26 +00:00
ulong64 delta;
2017-05-11 12:49:57 +00:00
} thread_info;
static void *run(void *arg)
{
thread_info *tinfo = arg;
ulong64 ts;
ts = epoch_usec();
tinfo->err = tinfo->t->fn();
2017-06-09 11:34:26 +00:00
tinfo->delta = epoch_usec() - ts;
2017-05-11 12:49:57 +00:00
return arg;
}
#endif
2017-06-08 09:51:36 +00:00
/*
* unregister ciphers, hashes & prngs
*/
static void s_unregister_all(void)
2017-06-08 09:51:36 +00:00
{
#ifdef LTC_RIJNDAEL
#ifdef ENCRYPT_ONLY
/* alternative would be
* unregister_cipher(&rijndael_enc_desc);
*/
unregister_cipher(&aes_enc_desc);
#else
/* alternative would be
* unregister_cipher(&rijndael_desc);
*/
unregister_cipher(&aes_desc);
#endif
2017-06-08 09:51:36 +00:00
#endif
#ifdef LTC_BLOWFISH
unregister_cipher(&blowfish_desc);
#endif
#ifdef LTC_XTEA
unregister_cipher(&xtea_desc);
#endif
#ifdef LTC_RC5
unregister_cipher(&rc5_desc);
#endif
#ifdef LTC_RC6
unregister_cipher(&rc6_desc);
#endif
#ifdef LTC_SAFERP
unregister_cipher(&saferp_desc);
#endif
#ifdef LTC_TWOFISH
unregister_cipher(&twofish_desc);
#endif
#ifdef LTC_SAFER
unregister_cipher(&safer_k64_desc);
unregister_cipher(&safer_sk64_desc);
unregister_cipher(&safer_k128_desc);
unregister_cipher(&safer_sk128_desc);
#endif
#ifdef LTC_RC2
unregister_cipher(&rc2_desc);
#endif
#ifdef LTC_DES
unregister_cipher(&des_desc);
unregister_cipher(&des3_desc);
#endif
#ifdef LTC_CAST5
unregister_cipher(&cast5_desc);
#endif
#ifdef LTC_NOEKEON
unregister_cipher(&noekeon_desc);
#endif
#ifdef LTC_SKIPJACK
unregister_cipher(&skipjack_desc);
#endif
#ifdef LTC_KHAZAD
unregister_cipher(&khazad_desc);
#endif
#ifdef LTC_ANUBIS
unregister_cipher(&anubis_desc);
#endif
#ifdef LTC_KSEED
unregister_cipher(&kseed_desc);
#endif
#ifdef LTC_KASUMI
unregister_cipher(&kasumi_desc);
#endif
#ifdef LTC_MULTI2
unregister_cipher(&multi2_desc);
#endif
#ifdef LTC_CAMELLIA
unregister_cipher(&camellia_desc);
#endif
2017-10-16 19:20:33 +00:00
#ifdef LTC_IDEA
unregister_cipher(&idea_desc);
#endif
2017-10-17 10:55:14 +00:00
#ifdef LTC_SERPENT
unregister_cipher(&serpent_desc);
#endif
2017-06-08 09:51:36 +00:00
#ifdef LTC_TIGER
unregister_hash(&tiger_desc);
#endif
#ifdef LTC_MD2
unregister_hash(&md2_desc);
#endif
#ifdef LTC_MD4
unregister_hash(&md4_desc);
#endif
#ifdef LTC_MD5
unregister_hash(&md5_desc);
#endif
#ifdef LTC_SHA1
unregister_hash(&sha1_desc);
#endif
#ifdef LTC_SHA224
unregister_hash(&sha224_desc);
#endif
#ifdef LTC_SHA256
unregister_hash(&sha256_desc);
#endif
#ifdef LTC_SHA384
unregister_hash(&sha384_desc);
#endif
#ifdef LTC_SHA512
unregister_hash(&sha512_desc);
#endif
#ifdef LTC_SHA512_224
unregister_hash(&sha512_224_desc);
#endif
#ifdef LTC_SHA512_256
unregister_hash(&sha512_256_desc);
#endif
#ifdef LTC_SHA3
unregister_hash(&sha3_224_desc);
unregister_hash(&sha3_256_desc);
unregister_hash(&sha3_384_desc);
unregister_hash(&sha3_512_desc);
#endif
2018-03-07 13:55:10 +00:00
#ifdef LTC_KECCAK
unregister_hash(&keccak_224_desc);
unregister_hash(&keccak_256_desc);
unregister_hash(&keccak_384_desc);
unregister_hash(&keccak_512_desc);
#endif
2017-06-08 09:51:36 +00:00
#ifdef LTC_RIPEMD128
unregister_hash(&rmd128_desc);
#endif
#ifdef LTC_RIPEMD160
unregister_hash(&rmd160_desc);
#endif
#ifdef LTC_RIPEMD256
unregister_hash(&rmd256_desc);
#endif
#ifdef LTC_RIPEMD320
unregister_hash(&rmd320_desc);
#endif
#ifdef LTC_WHIRLPOOL
unregister_hash(&whirlpool_desc);
#endif
#ifdef LTC_BLAKE2S
unregister_hash(&blake2s_128_desc);
unregister_hash(&blake2s_160_desc);
unregister_hash(&blake2s_224_desc);
unregister_hash(&blake2s_256_desc);
#endif
#ifdef LTC_BLAKE2B
unregister_hash(&blake2b_160_desc);
unregister_hash(&blake2b_256_desc);
unregister_hash(&blake2b_384_desc);
unregister_hash(&blake2b_512_desc);
#endif
#ifdef LTC_CHC_HASH
unregister_hash(&chc_desc);
#endif
unregister_prng(&yarrow_desc);
#ifdef LTC_FORTUNA
unregister_prng(&fortuna_desc);
#endif
#ifdef LTC_RC4
unregister_prng(&rc4_desc);
#endif
#ifdef LTC_CHACHA20_PRNG
unregister_prng(&chacha20_prng_desc);
#endif
#ifdef LTC_SOBER128
unregister_prng(&sober128_desc);
#endif
#ifdef LTC_SPRNG
unregister_prng(&sprng_desc);
#endif
} /* s_cleanup() */
2017-06-08 09:51:36 +00:00
static void register_algs(void)
{
int err;
atexit(s_unregister_all);
2017-06-08 09:51:36 +00:00
#ifndef LTC_YARROW
#error This demo requires Yarrow.
#endif
2017-10-18 20:35:16 +00:00
if ((err = register_all_ciphers()) != CRYPT_OK) {
fprintf(stderr, "register_all_ciphers err=%s\n", error_to_string(err));
exit(EXIT_FAILURE);
}
if ((err = register_all_hashes()) != CRYPT_OK) {
fprintf(stderr, "register_all_hashes err=%s\n", error_to_string(err));
exit(EXIT_FAILURE);
}
if ((err = register_all_prngs()) != CRYPT_OK) {
fprintf(stderr, "register_all_prngs err=%s\n", error_to_string(err));
exit(EXIT_FAILURE);
}
2017-06-08 09:51:36 +00:00
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);
}
if (strcmp("CRYPT_OK", error_to_string(err))) {
exit(EXIT_FAILURE);
}
}
2017-03-29 18:52:30 +00:00
int main(int argc, char **argv)
2005-04-17 11:37:13 +00:00
{
2017-05-11 12:49:57 +00:00
#ifdef LTC_PTHREAD
thread_info *tinfo, *res;
#endif
2017-03-29 18:52:30 +00:00
int x, pass = 0, fail = 0, nop = 0;
size_t fn_len, i, dots;
const char* mpi_provider = NULL;
2017-03-29 18:52:30 +00:00
char *single_test = NULL;
2017-05-09 16:31:14 +00:00
ulong64 ts;
2017-05-11 12:49:57 +00:00
long delta, dur, real = 0;
register_algs();
2005-08-01 16:36:47 +00:00
2017-09-19 16:51:47 +00:00
printf("LTC_VERSION = %s\n%s\n\n", GIT_VERSION, crypt_build_settings);
2017-03-26 13:46:41 +00:00
2005-08-01 16:36:47 +00:00
#ifdef USE_LTM
mpi_provider = "ltm";
2005-08-01 16:36:47 +00:00
#elif defined(USE_TFM)
mpi_provider = "tfm";
2006-04-06 19:48:32 +00:00
#elif defined(USE_GMP)
mpi_provider = "gmp";
#elif defined(EXT_MATH_LIB)
mpi_provider = "ext";
#endif
if (argc > 2) {
mpi_provider = argv[2];
}
2017-10-12 08:20:43 +00:00
crypt_mp_init(mpi_provider);
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");
}
2017-05-11 12:49:57 +00:00
#ifdef LTC_PTHREAD
tinfo = XCALLOC(sizeof(test_functions)/sizeof(test_functions[0]), sizeof(thread_info));
if (tinfo == NULL) {
printf("\n\nFAILURE: XCALLOC\n");
return EXIT_FAILURE;
}
#endif
fn_len = 0;
2017-05-11 12:49:57 +00:00
for (i = 0; i < sizeof(test_functions) / sizeof(test_functions[0]); ++i) {
2019-10-03 13:18:25 +00:00
size_t len = XSTRLEN(test_functions[i].name);
if (fn_len < len) fn_len = len;
2017-05-11 12:49:57 +00:00
#ifdef LTC_PTHREAD
2017-06-09 11:34:26 +00:00
if(test_functions[i].fn == prng_test) continue;
2017-05-11 12:49:57 +00:00
tinfo[i].t = &test_functions[i];
x = pthread_create(&tinfo[i].thread_id, NULL, run, &tinfo[i]);
if (x != 0) {
printf("\n\nFAILURE: pthread_create\n");
return EXIT_FAILURE;
}
#endif
}
fn_len = fn_len + (4 - (fn_len % 4));
2017-03-29 18:52:30 +00:00
/* single test name from commandline */
if (argc > 1) single_test = argv[1];
2017-05-11 12:49:57 +00:00
dur = epoch_usec();
for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
2017-03-29 18:52:30 +00:00
continue;
}
2019-10-03 13:18:25 +00:00
dots = fn_len - XSTRLEN(test_functions[i].name);
printf("\n%s", test_functions[i].name);
while(dots--) printf(".");
fflush(stdout);
2017-05-11 12:49:57 +00:00
#ifdef LTC_PTHREAD
2017-06-09 11:34:26 +00:00
if(test_functions[i].fn != prng_test) {
x = pthread_join(tinfo[i].thread_id, (void**)&res);
if (x != 0){
printf("\n\nFAILURE: pthread_join\n");
return EXIT_FAILURE;
}
x = res->err;
delta = res->delta;
}
else {
ts = epoch_usec();
x = test_functions[i].fn();
delta = (long)(epoch_usec() - ts);
2017-05-11 12:49:57 +00:00
}
#else
2017-03-29 18:52:30 +00:00
ts = epoch_usec();
x = test_functions[i].fn();
2017-05-09 16:31:14 +00:00
delta = (long)(epoch_usec() - ts);
2017-05-11 12:49:57 +00:00
#endif
2017-06-09 11:34:26 +00:00
real += delta;
2017-03-29 18:52:30 +00:00
if (x == CRYPT_OK) {
2017-05-09 16:31:14 +00:00
printf("passed %10.3fms", (double)(delta)/1000);
2017-03-29 18:52:30 +00:00
pass++;
}
else if (x == CRYPT_NOP) {
printf("nop");
nop++;
}
else {
2017-08-07 14:33:38 +00:00
printf("failed (%s) %10.3fms", error_to_string(x), (double)(delta)/1000);
2017-03-29 18:52:30 +00:00
fail++;
}
}
2017-05-11 12:49:57 +00:00
dur = epoch_usec() - dur;
2017-05-11 12:49:57 +00:00
#ifdef LTC_PTHREAD
XFREE(tinfo);
#endif
x = (fail > 0 || fail+pass+nop == 0) ? EXIT_FAILURE : EXIT_SUCCESS;
printf("\n\n%s: passed=%d failed=%d nop=%d duration=%.1fsec real=%.1fsec\n", x ? "FAILURE" : "SUCCESS", pass, fail, nop, (double)(dur)/(1000*1000), (double)(real)/(1000*1000));
return x;
2005-04-17 11:37:13 +00:00
}