From 4d492b9270a19cc962114856f20f40d10aefb744 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Fri, 2 Aug 2019 22:03:13 -0400 Subject: [PATCH] Add macros containing compilation information and print in test programs Fixes #331 --- VisualStudio/winconfig.h | 3 ++ configure.ac | 4 ++ scripts/git_commit.sh | 16 ++++++++ tests/Makefile.am | 29 ++++++++------- tests/kat_kem.c | 4 ++ tests/kat_sig.c | 4 ++ tests/speed_kem.c | 12 +----- tests/speed_sig.c | 12 +----- tests/system_info.c | 79 ++++++++++++++++++++++++++++++++++++++++ tests/test_aes.c | 4 ++ tests/test_hash.c | 4 ++ tests/test_kem.c | 4 ++ tests/test_sha3.c | 5 +++ tests/test_sig.c | 4 ++ 14 files changed, 151 insertions(+), 33 deletions(-) create mode 100755 scripts/git_commit.sh create mode 100644 tests/system_info.c diff --git a/VisualStudio/winconfig.h b/VisualStudio/winconfig.h index d77bf7cfe..aad3e9ab3 100644 --- a/VisualStudio/winconfig.h +++ b/VisualStudio/winconfig.h @@ -94,5 +94,8 @@ #define OQS_KEM_DEFAULT OQS_KEM_alg_sike_p434 #define OQS_SIG_DEFAULT OQS_SIG_alg_picnic_L1_FS +#define OQS_MASTER_BRANCH /**/ +#define OQS_VERSION_NUMBER 0x00200000L +#define OQS_VERSION_TEXT "0.2.0-dev" #endif diff --git a/configure.ac b/configure.ac index 83054b26f..47486e217 100644 --- a/configure.ac +++ b/configure.ac @@ -111,4 +111,8 @@ case "$SIG_DEFAULT" in ;; esac +AC_DEFINE_UNQUOTED([OQS_COMPILE_CFLAGS], ["$CFLAGS $AM_CFLAGS"], [CFLAGS set during ./configure]) +AC_DEFINE_UNQUOTED([OQS_COMPILE_LDFLAGS], ["$LDFLAGS"], [LDFLAGS set during ./configure]) +AC_DEFINE_UNQUOTED([OQS_COMPILE_BUILD_TARGET], ["$build"], [build set during ./configure]) + AC_OUTPUT diff --git a/scripts/git_commit.sh b/scripts/git_commit.sh new file mode 100755 index 000000000..47954c36c --- /dev/null +++ b/scripts/git_commit.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Get the SHA-1 hash of the current git commit, if any +# based on https://stackoverflow.com/questions/8215785/how-can-i-use-ac-revision-with-git + +changes=`git status -s 2>&1` +if [ $? -ne 0 ] ; then + # not a git repository + printf 'unknown' +elif [ "x$changes" == "x" ] ; then + # no changes since last commit + printf '%s' `git rev-parse HEAD` +else + # changes since last commit + printf '%s (+ local modifications)' `git rev-parse HEAD` +fi \ No newline at end of file diff --git a/tests/Makefile.am b/tests/Makefile.am index b0f036783..27e7ec7ce 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,11 +3,14 @@ check_PROGRAMS = example_kem speed_kem test_kem kat_kem \ example_sig speed_sig test_sig kat_sig \ test_aes test_sha3 test_hash -commonflags = -L../.libs -loqs -lm +commonldflags = -L../.libs -loqs -lm if USE_OPENSSL -commonflags += -L${OPENSSL_DIR}/lib -lcrypto +commonldflags += -L${OPENSSL_DIR}/lib -lcrypto endif +git_commit=$(shell ../scripts/git_commit.sh) +CFLAGS += '-DOQS_COMPILE_GIT_COMMIT="${git_commit}"' + example_kem_SOURCES = example_kem.c example_sig_SOURCES = example_sig.c speed_kem_SOURCES = speed_kem.c @@ -20,21 +23,21 @@ test_sig_SOURCES = test_sig.c kat_kem_SOURCES = kat_kem.c kat_sig_SOURCES = kat_sig.c -example_kem_LDFLAGS = ${commonflags} -example_sig_LDFLAGS = ${commonflags} -kat_kem_LDFLAGS = ${commonflags} -kat_sig_LDFLAGS = ${commonflags} -speed_kem_LDFLAGS = ${commonflags} -speed_sig_LDFLAGS = ${commonflags} -test_kem_LDFLAGS = ${commonflags} -test_sig_LDFLAGS = ${commonflags} +example_kem_LDFLAGS = ${commonldflags} +example_sig_LDFLAGS = ${commonldflags} +kat_kem_LDFLAGS = ${commonldflags} +kat_sig_LDFLAGS = ${commonldflags} +speed_kem_LDFLAGS = ${commonldflags} +speed_sig_LDFLAGS = ${commonldflags} +test_kem_LDFLAGS = ${commonldflags} +test_sig_LDFLAGS = ${commonldflags} test_aes_LDFLAGS = -L../src/crypto/aes/.libs -laes # required since OQS_AES symbols are not part of OQS public API -test_aes_LDFLAGS += ${commonflags} +test_aes_LDFLAGS += ${commonldflags} test_hash_LDFLAGS = -L../src/crypto/sha2/.libs -lsha2 # required since OQS_SHA2 symbols are not part of OQS public API test_hash_LDFLAGS += -L../src/crypto/sha3/.libs -lsha3 # required since OQS_SHA3 symbols are not part of OQS public API -test_hash_LDFLAGS += ${commonflags} +test_hash_LDFLAGS += ${commonldflags} test_sha3_LDFLAGS = -L../src/crypto/sha3/.libs -lsha3 # required since OQS_SHA3 symbols are not part of OQS public API -test_sha3_LDFLAGS += ${commonflags} +test_sha3_LDFLAGS += ${commonldflags} example_kem_DEPENDENCIES = ../liboqs.la example_sig_DEPENDENCIES = ../liboqs.la diff --git a/tests/kat_kem.c b/tests/kat_kem.c index 0a0198de9..91a9c5d50 100644 --- a/tests/kat_kem.c +++ b/tests/kat_kem.c @@ -12,6 +12,8 @@ #include +#include "system_info.c" + /* Displays hexadecimal strings */ void OQS_print_hex_string(const char *label, const uint8_t *str, size_t len) { printf("%-20s (%4zu bytes): ", label, len); @@ -144,6 +146,8 @@ int main(int argc, char **argv) { fprintf(stderr, "%s", OQS_KEM_alg_identifier(i)); } fprintf(stderr, "\n"); + printf("\n"); + print_system_info(); return EXIT_FAILURE; } diff --git a/tests/kat_sig.c b/tests/kat_sig.c index ec345ad54..39b14a91e 100644 --- a/tests/kat_sig.c +++ b/tests/kat_sig.c @@ -12,6 +12,8 @@ #include +#include "system_info.c" + /* Displays hexadecimal strings */ void OQS_print_hex_string(const char *label, const uint8_t *str, size_t len) { printf("%-20s (%4zu bytes): ", label, len); @@ -690,6 +692,8 @@ int main(int argc, char **argv) { fprintf(stderr, "%s", OQS_SIG_alg_identifier(i)); } fprintf(stderr, "\n"); + printf("\n"); + print_system_info(); return EXIT_FAILURE; } diff --git a/tests/speed_kem.c b/tests/speed_kem.c index 299a30bb3..a09f93537 100644 --- a/tests/speed_kem.c +++ b/tests/speed_kem.c @@ -7,6 +7,7 @@ #include #include "ds_benchmark.h" +#include "system_info.c" static OQS_STATUS kem_speed_wrapper(const char *method_name, int duration, bool printInfo) { @@ -135,16 +136,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - /* TODO: Make autoconf generate these variables */ - // printf("Compiler setup\n"); - // printf("==============\n"); - // printf("Date: %s\n", OQS_COMPILE_DATE); - // printf("Compiler: %s (%s)\n", OQS_COMPILE_CC, OQS_COMPILE_CC_VERSION); - // printf("OS: %s\n", OQS_COMPILE_UNAME); - // printf("CFLAGS: %s\n", OQS_COMPILE_CFLAGS); - // printf("LDFLAGS: %s\n", OQS_COMPILE_LDFLAGS); - // printf("RNG: OpenSSL\n"); - // printf("\n"); + print_system_info(); printf("Speed test\n"); printf("==========\n"); diff --git a/tests/speed_sig.c b/tests/speed_sig.c index 1c1fa2f2a..4b12a319f 100644 --- a/tests/speed_sig.c +++ b/tests/speed_sig.c @@ -7,6 +7,7 @@ #include #include "ds_benchmark.h" +#include "system_info.c" static OQS_STATUS sig_speed_wrapper(const char *method_name, int duration, bool printInfo) { @@ -136,16 +137,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - /* TODO: Make autoconf generate these variables */ - // printf("Compiler setup\n"); - // printf("==============\n"); - // printf("Date: %s\n", OQS_COMPILE_DATE); - // printf("Compiler: %s (%s)\n", OQS_COMPILE_CC, OQS_COMPILE_CC_VERSION); - // printf("OS: %s\n", OQS_COMPILE_UNAME); - // printf("CFLAGS: %s\n", OQS_COMPILE_CFLAGS); - // printf("LDFLAGS: %s\n", OQS_COMPILE_LDFLAGS); - // printf("RNG: OpenSSL\n"); - // printf("\n"); + print_system_info(); printf("Speed test\n"); printf("==========\n"); diff --git a/tests/system_info.c b/tests/system_info.c new file mode 100644 index 000000000..cefec3498 --- /dev/null +++ b/tests/system_info.c @@ -0,0 +1,79 @@ +#include + +#include + +// based on macros in https://sourceforge.net/p/predef/wiki/Compilers/ +static void print_compiler_info() { +#if defined(__clang__) + printf("Compiler: clang (%s)\n", __clang_version__); +#elif defined(__GNUC_PATCHLEVEL__) + printf("Compiler: gcc (%d.%d.%d)\n", __GNU_C__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); +#elif defined(__GNUC_MINOR__) + printf("Compiler: gcc (%d.%d)\n", __GNU_C__, __GNUC_MINOR__); +#elif defined(__INTEL_COMPILER) + printf("Compiler: Intel C/C++ (%d)\n", __INTEL_COMPILER); +#elif defined(_MSC_FULL_VER) + printf("Compiler: Microsoft C/C++ (%d)\n", _MSC_FULL_VER); +#else + printf("Compiler: Unknown"\n); +#endif +} + +// based on macros in https://sourceforge.net/p/predef/wiki/Architectures/ +static void print_platform_info() { +#if defined(OQS_COMPILE_BUILD_TARGET) + printf("Target platform: %s\n", OQS_COMPILE_BUILD_TARGET); +#elif defined(_WIN64) + printf("Target platform: Windows (64-bit)\n"); +#elif defined(_WIN32) + printf("Target platform: Windows (32-bit)\n"); +#else + printf("Target platform: Unknown\n"); +#endif +} + +#if defined(USE_OPENSSL) +#include +#endif + +static void print_oqs_configuration() { + printf("OQS version: %s\n", OQS_VERSION_TEXT); +#if defined(OQS_COMPILE_GIT_COMMIT) + printf("Git commit: %s\n", OQS_COMPILE_GIT_COMMIT); +#endif +#if defined(USE_OPENSSL) + printf("OpenSSL enabled: Yes (%08lx)\n", OpenSSL_version_num()); +#else + printf("OpenSSL enabled: No\n"); +#endif +#if defined(USE_AES_C) + printf("AES: C\n"); +#elif defined(USE_AES_OPENSSL) + printf("AES: OpenSSL\n"); +#endif +#if defined(USE_SHA2_C) + printf("SHA-2: C\n"); +#elif defined(USE_SHA2_OPENSSL) + printf("SHA-2: OpenSSL\n"); +#endif +#if defined(USE_SHA3_C) + printf("SHA-3: C\n"); +#elif defined(USE_SHA3_OPENSSL) + printf("SHA-3: OpenSSL\n"); +#endif +#if defined(OQS_COMPILE_CFLAGS) + printf("CFLAGS: %s\n", OQS_COMPILE_CFLAGS); +#endif +#if defined(OQS_COMPILE_LDFLAGS) + printf("LDFLAGS: %s\n", OQS_COMPILE_LDFLAGS); +#endif +} + +static void print_system_info() { + printf("Configuration info\n"); + printf("==================\n"); + print_platform_info(); + print_compiler_info(); + print_oqs_configuration(); + printf("\n"); +} \ No newline at end of file diff --git a/tests/test_aes.c b/tests/test_aes.c index cd385a0eb..13b341b47 100644 --- a/tests/test_aes.c +++ b/tests/test_aes.c @@ -6,6 +6,8 @@ #include #include "ds_benchmark.h" +#include "system_info.c" + #include /* Displays hexadecimal strings */ @@ -125,6 +127,8 @@ int main(int argc, char **argv) { } } } + + print_system_info(); printf("=== test_aes correctness ===\n"); if (test_aes128_correctness() != EXIT_SUCCESS) { diff --git a/tests/test_hash.c b/tests/test_hash.c index d71f8362e..9f929fb26 100644 --- a/tests/test_hash.c +++ b/tests/test_hash.c @@ -8,6 +8,8 @@ #include #include +#include "system_info.c" + #define BUFFER_SIZE 10000 static int read_stdin(uint8_t **msg, size_t *msg_len) { @@ -111,6 +113,8 @@ int main(int argc, char **argv) { fprintf(stderr, "Usage: test_hash algname\n"); fprintf(stderr, " algname: sha256, sha384, sha512\n"); fprintf(stderr, " test_hash reads input from stdin and outputs hash value as hex string to stdout"); + printf("\n"); + print_system_info(); return EXIT_FAILURE; } diff --git a/tests/test_kem.c b/tests/test_kem.c index a394f1afc..accc89191 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -5,6 +5,8 @@ #include +#include "system_info.c" + /* Displays hexadecimal strings */ void OQS_print_hex_string(const char *label, const uint8_t *str, size_t len) { printf("%-20s (%4zu bytes): ", label, len); @@ -134,6 +136,8 @@ int main(int argc, char **argv) { fprintf(stderr, "\n"); return EXIT_FAILURE; } + + print_system_info(); // Use system RNG in this program OQS_randombytes_switch_algorithm(OQS_RAND_alg_system); diff --git a/tests/test_sha3.c b/tests/test_sha3.c index 7adb221ef..c1b8d2f37 100644 --- a/tests/test_sha3.c +++ b/tests/test_sha3.c @@ -5,6 +5,8 @@ #include +#include "system_info.c" + #if defined(WINDOWS) #define UNUSED #else @@ -817,6 +819,9 @@ int cshake_simple_256_kat_test() { */ int main(UNUSED int argc, UNUSED char **argv) { int ret = EXIT_SUCCESS; + + print_system_info(); + if (sha3_256_kat_test() == EXIT_SUCCESS) { printf("Success! passed sha3-256 known answer tests \n"); } else { diff --git a/tests/test_sig.c b/tests/test_sig.c index c52d1dc2b..fb2a877c5 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -8,6 +8,8 @@ #include +#include "system_info.c" + static OQS_STATUS sig_test_correctness(const char *method_name) { OQS_SIG *sig = NULL; @@ -98,6 +100,8 @@ int main(int argc, char **argv) { fprintf(stderr, "\n"); return EXIT_FAILURE; } + + print_system_info(); // Use system RNG in this program OQS_randombytes_switch_algorithm(OQS_RAND_alg_system);