From 4cc03987b1465f200dc6e99c8c9163eed91d76f4 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 19 Mar 2020 14:57:18 -0400 Subject: [PATCH 01/10] Run the test_kem and test_sig tests on a thread when possible --- .CMake/compiler_flags.cmake | 8 ++++++++ src/oqsconfig.h.cmake | 2 ++ tests/test_kem.c | 27 ++++++++++++++++++++++++--- tests/test_sig.c | 27 ++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/.CMake/compiler_flags.cmake b/.CMake/compiler_flags.cmake index 5e48080ee..237e7a8f0 100644 --- a/.CMake/compiler_flags.cmake +++ b/.CMake/compiler_flags.cmake @@ -37,6 +37,10 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang") add_compile_options(-fomit-frame-pointer) endif() + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + set(OQS_USE_PTHREADS_IN_TESTS 1) + elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") add_compile_options(-Werror) add_compile_options(-Wall) @@ -75,6 +79,10 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") endif () endif() + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + set(OQS_USE_PTHREADS_IN_TESTS 1) + elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") # Warning C4146 is raised when a unary minus operator is applied to an # unsigned type; this has nonetheless been standard and portable for as diff --git a/src/oqsconfig.h.cmake b/src/oqsconfig.h.cmake index 58686b4e8..043bfa351 100644 --- a/src/oqsconfig.h.cmake +++ b/src/oqsconfig.h.cmake @@ -9,6 +9,8 @@ #cmakedefine OQS_USE_SHA2_OPENSSL 1 #cmakedefine OQS_USE_SHA3_OPENSSL 1 +#cmakedefine OQS_USE_PTHREADS_IN_TESTS 1 + #cmakedefine OQS_USE_AES_INSTRUCTIONS 1 #cmakedefine OQS_USE_AVX_INSTRUCTIONS 1 #cmakedefine OQS_USE_AVX2_INSTRUCTIONS 1 diff --git a/tests/test_kem.c b/tests/test_kem.c index a9421c4d1..8c748cabe 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -5,6 +5,10 @@ #include +#if OQS_USE_PTHREADS_IN_TESTS +#include +#endif + #include "system_info.c" /* Displays hexadecimal strings */ @@ -43,8 +47,8 @@ static OQS_STATUS kem_test_correctness(const char *method_name) { kem = OQS_KEM_new(method_name); if (kem == NULL) { - // should always succeed since we don't call this function on KEMs that aren't enabled - return OQS_ERROR; + fprintf(stderr, "ERROR: OQS_KEM_new failed\n"); + goto err; } printf("================================================================================\n"); @@ -123,6 +127,10 @@ cleanup: OQS_MEM_insecure_free(ciphertext); OQS_KEM_free(kem); +#ifdef OQS_USE_PTHREADS_IN_TESTS + pthread_exit((void *) ret); +#endif + return ret; } @@ -150,7 +158,20 @@ int main(int argc, char **argv) { if (!OQS_KEM_alg_is_enabled(alg_name)) { return EXIT_FAILURE; } - OQS_STATUS rc = kem_test_correctness(alg_name); + OQS_STATUS rc; +#if OQS_USE_PTHREADS_IN_TESTS + pthread_t thread; + void *status; + int trc = pthread_create(&thread, NULL, (void *(*)(void *)) &kem_test_correctness, alg_name); + if (trc) { + fprintf(stderr, "ERROR: Creating pthread\n"); + return EXIT_FAILURE; + } + pthread_join(thread, &status); + rc = (OQS_STATUS) status; +#else + rc = kem_test_correctness(alg_name); +#endif if (rc != OQS_SUCCESS) { return EXIT_FAILURE; } diff --git a/tests/test_sig.c b/tests/test_sig.c index 03d70438e..41ed23d93 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -8,6 +8,10 @@ #include +#if OQS_USE_PTHREADS_IN_TESTS +#include +#endif + #include "system_info.c" static OQS_STATUS sig_test_correctness(const char *method_name) { @@ -23,8 +27,8 @@ static OQS_STATUS sig_test_correctness(const char *method_name) { sig = OQS_SIG_new(method_name); if (sig == NULL) { - // should always succeed since we don't call this function on KEMs that aren't enabled - return OQS_ERROR; + fprintf(stderr, "ERROR: OQS_SIG_new failed\n"); + goto err; } printf("================================================================================\n"); @@ -84,6 +88,10 @@ cleanup: OQS_MEM_insecure_free(signature); OQS_SIG_free(sig); +#ifdef OQS_USE_PTHREADS_IN_TESTS + pthread_exit((void *) ret); +#endif + return ret; } @@ -111,7 +119,20 @@ int main(int argc, char **argv) { if (!OQS_SIG_alg_is_enabled(alg_name)) { return EXIT_FAILURE; } - OQS_STATUS rc = sig_test_correctness(alg_name); + OQS_STATUS rc; +#if OQS_USE_PTHREADS_IN_TESTS + pthread_t thread; + void *status; + int trc = pthread_create(&thread, NULL, (void *(*)(void *)) &sig_test_correctness, alg_name); + if (trc) { + fprintf(stderr, "ERROR: Creating pthread\n"); + return EXIT_FAILURE; + } + pthread_join(thread, &status); + rc = (OQS_STATUS) status; +#else + rc = sig_test_correctness(alg_name); +#endif if (rc != OQS_SUCCESS) { return EXIT_FAILURE; } From 2a2393d0978fcd0032390a4d051e8cdf8162d824 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 19 Mar 2020 15:23:20 -0400 Subject: [PATCH 02/10] Fix compilation warnings under gcc --- tests/test_kem.c | 16 +++++++++++----- tests/test_sig.c | 15 ++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/test_kem.c b/tests/test_kem.c index 8c748cabe..1a434ade5 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -127,13 +127,19 @@ cleanup: OQS_MEM_insecure_free(ciphertext); OQS_KEM_free(kem); -#ifdef OQS_USE_PTHREADS_IN_TESTS - pthread_exit((void *) ret); -#endif - return ret; } +#if OQS_USE_PTHREADS_IN_TESTS + +void *test_wrapper(void *arg) { + OQS_STATUS rc = kem_test_correctness((char *) arg); + long lrc = (long) rc; + pthread_exit((void *) lrc); + return (void *) lrc; +} +#endif + int main(int argc, char **argv) { if (argc != 2) { @@ -162,7 +168,7 @@ int main(int argc, char **argv) { #if OQS_USE_PTHREADS_IN_TESTS pthread_t thread; void *status; - int trc = pthread_create(&thread, NULL, (void *(*)(void *)) &kem_test_correctness, alg_name); + int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); if (trc) { fprintf(stderr, "ERROR: Creating pthread\n"); return EXIT_FAILURE; diff --git a/tests/test_sig.c b/tests/test_sig.c index 41ed23d93..458e1611a 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -88,13 +88,18 @@ cleanup: OQS_MEM_insecure_free(signature); OQS_SIG_free(sig); -#ifdef OQS_USE_PTHREADS_IN_TESTS - pthread_exit((void *) ret); -#endif - return ret; } +#if OQS_USE_PTHREADS_IN_TESTS +void *test_wrapper(void *arg) { + OQS_STATUS rc = sig_test_correctness((char *) arg); + long lrc = (long) rc; + pthread_exit((void *) lrc); + return (void *) lrc; +} +#endif + int main(int argc, char **argv) { if (argc != 2) { @@ -123,7 +128,7 @@ int main(int argc, char **argv) { #if OQS_USE_PTHREADS_IN_TESTS pthread_t thread; void *status; - int trc = pthread_create(&thread, NULL, (void *(*)(void *)) &sig_test_correctness, alg_name); + int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); if (trc) { fprintf(stderr, "ERROR: Creating pthread\n"); return EXIT_FAILURE; From f437663bd0ec412538eb2c1eaeca36a81f78dd07 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Wed, 25 Mar 2020 17:23:31 -0400 Subject: [PATCH 03/10] Run big stack usage tests on main thread --- README.md | 8 +++++--- tests/test_kem.c | 21 +++++++++++++-------- tests/test_sig.c | 21 +++++++++++++-------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 5c09f8273..4b4a21de4 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,10 @@ More information on OQS can be found [here](https://openquantumsafe.org/) and in #### Key encapsulation mechanisms - **BIKE**: BIKE1-L1-CPA, BIKE1-L3-CPA, BIKE1-L1-FO, BIKE1-L3-FO -- **Classic McEliece**: Classic-McEliece-348864, Classic-McEliece-348864f, Classic-McEliece-460896, Classic-McEliece-460896f, Classic-McEliece-6688128, Classic-McEliece-6688128f, Classic-McEliece-6960119, Classic-McEliece-6960119f, Classic-McEliece-8192128, Classic-McEliece-8192128f +- **Classic McEliece**: Classic-McEliece-348864†, Classic-McEliece-348864f†, Classic-McEliece-460896†, Classic-McEliece-460896f†, Classic-McEliece-6688128†, Classic-McEliece-6688128f†, Classic-McEliece-6960119†, Classic-McEliece-6960119f†, Classic-McEliece-8192128†, Classic-McEliece-8192128f† - **FrodoKEM**: FrodoKEM-640-AES, FrodoKEM-640-SHAKE, FrodoKEM-976-AES, FrodoKEM-976-SHAKE, FrodoKEM-1344-AES, FrodoKEM-1344-SHAKE - **Kyber**: Kyber512, Kyber768, Kyber1024, Kyber512-90s, Kyber768-90s, Kyber1024-90s -- **LEDAcrypt**: LEDAcryptKEM-LT12, LEDAcryptKEM-LT32, LEDAcryptKEM-LT52 +- **LEDAcrypt**: LEDAcryptKEM-LT12, LEDAcryptKEM-LT32, LEDAcryptKEM-LT52† - **NewHope**: NewHope-512-CCA, NewHope-1024-CCA - **NTRU**: NTRU-HPS-2048-509, NTRU-HPS-2048-677, NTRU-HPS-4096-821, NTRU-HRSS-701 - **SABER**: LightSaber-KEM, Saber-KEM, FireSaber-KEM @@ -54,11 +54,13 @@ More information on OQS can be found [here](https://openquantumsafe.org/) and in - **MQDSS**: MQDSS-31-48, MQDSS-31-64 - **Picnic**: Picnic-L1-FS, Picnic-L1-UR, Picnic-L3-FS, Picnic-L3-UR, Picnic-L5-FS, Picnic-L5-UR, Picnic2-L1-FS, Picnic2-L3-FS, Picnic2-L5-FS - **qTesla**: qTesla-p-I, qTesla-p-III -- **Rainbow**: Rainbow-Ia-Classic, Rainbow-Ia-Cyclic, Rainbow-Ia-Cyclic-Compressed, Rainbow-IIIc-Classic, Rainbow-IIIc-Cyclic, Rainbow-IIIc-Cyclic-Compressed, Rainbow-Vc-Classic, Rainbow-Vc-Cyclic, Rainbow-Vc-Cyclic-Compressed +- **Rainbow**: Rainbow-Ia-Classic, Rainbow-Ia-Cyclic, Rainbow-Ia-Cyclic-Compressed, Rainbow-IIIc-Classic†, Rainbow-IIIc-Cyclic†, Rainbow-IIIc-Cyclic-Compressed†, Rainbow-Vc-Classic†, Rainbow-Vc-Cyclic†, Rainbow-Vc-Cyclic-Compressed† - **SPHINCS+-Haraka**: SPHINCS+-Haraka-128f-robust, SPHINCS+-Haraka-128f-simple, SPHINCS+-Haraka-128s-robust, SPHINCS+-Haraka-128s-simple, SPHINCS+-Haraka-192f-robust, SPHINCS+-Haraka-192f-simple, SPHINCS+-Haraka-192s-robust, SPHINCS+-Haraka-192s-simple, SPHINCS+-Haraka-256f-robust, SPHINCS+-Haraka-256f-simple, SPHINCS+-Haraka-256s-robust, SPHINCS+-Haraka-256s-simple - **SPHINCS+-SHA256**: SPHINCS+-SHA256-128f-robust, SPHINCS+-SHA256-128f-simple, SPHINCS+-SHA256-128s-robust, SPHINCS+-SHA256-128s-simple, SPHINCS+-SHA256-192f-robust, SPHINCS+-SHA256-192f-simple, SPHINCS+-SHA256-192s-robust, SPHINCS+-SHA256-192s-simple, SPHINCS+-SHA256-256f-robust, SPHINCS+-SHA256-256f-simple, SPHINCS+-SHA256-256s-robust, SPHINCS+-SHA256-256s-simple - **SPHINCS+-SHAKE256**: SPHINCS+-SHAKE256-128f-robust, SPHINCS+-SHAKE256-128f-simple, SPHINCS+-SHAKE256-128s-robust, SPHINCS+-SHAKE256-128s-simple, SPHINCS+-SHAKE256-192f-robust, SPHINCS+-SHAKE256-192f-simple, SPHINCS+-SHAKE256-192s-robust, SPHINCS+-SHAKE256-192s-simple, SPHINCS+-SHAKE256-256f-robust, SPHINCS+-SHAKE256-256f-simple, SPHINCS+-SHAKE256-256s-robust, SPHINCS+-SHAKE256-256s-simple +Note that algorithms marked with a dagger (†) have large stack usage and may cause failures when run on threads or in constrained environments. + ### Limitations and Security As research advances, the supported algorithms may see rapid changes in their security, and may even prove insecure against both classical and quantum computers. diff --git a/tests/test_kem.c b/tests/test_kem.c index 1a434ade5..d2e649875 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -166,15 +166,20 @@ int main(int argc, char **argv) { } OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS - pthread_t thread; - void *status; - int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); - if (trc) { - fprintf(stderr, "ERROR: Creating pthread\n"); - return EXIT_FAILURE; + // don't run Classic McEliece or LEDAcryptKEM-LT52 in threads because of large stack usage + if ((strnstr(alg_name, "Classic-McEliece", 16) == NULL) && (strnstr(alg_name, "LEDAcryptKEM-LT52", 17) == NULL)) { + pthread_t thread; + void *status; + int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); + if (trc) { + fprintf(stderr, "ERROR: Creating pthread\n"); + return EXIT_FAILURE; + } + pthread_join(thread, &status); + rc = (OQS_STATUS) status; + } else { + rc = kem_test_correctness(alg_name); } - pthread_join(thread, &status); - rc = (OQS_STATUS) status; #else rc = kem_test_correctness(alg_name); #endif diff --git a/tests/test_sig.c b/tests/test_sig.c index 458e1611a..10b5e9309 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -126,15 +126,20 @@ int main(int argc, char **argv) { } OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS - pthread_t thread; - void *status; - int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); - if (trc) { - fprintf(stderr, "ERROR: Creating pthread\n"); - return EXIT_FAILURE; + // don't run Rainbow IIIc and Vc in threads because of large stack usage + if ((strnstr(alg_name, "Rainbow-IIIc", 12) == NULL) && (strnstr(alg_name, "Rainbow-Vc", 10) == NULL)) { + pthread_t thread; + void *status; + int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); + if (trc) { + fprintf(stderr, "ERROR: Creating pthread\n"); + return EXIT_FAILURE; + } + pthread_join(thread, &status); + rc = (OQS_STATUS) status; + } else { + rc = sig_test_correctness(alg_name); } - pthread_join(thread, &status); - rc = (OQS_STATUS) status; #else rc = sig_test_correctness(alg_name); #endif From efdbed4d67fa6bb9c0df0a9dc874a2b422085ae9 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 26 Mar 2020 09:44:17 -0400 Subject: [PATCH 04/10] Use strstr instead of strnstr --- tests/test_kem.c | 2 +- tests/test_sig.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_kem.c b/tests/test_kem.c index d2e649875..d6f480253 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -167,7 +167,7 @@ int main(int argc, char **argv) { OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS // don't run Classic McEliece or LEDAcryptKEM-LT52 in threads because of large stack usage - if ((strnstr(alg_name, "Classic-McEliece", 16) == NULL) && (strnstr(alg_name, "LEDAcryptKEM-LT52", 17) == NULL)) { + if ((strstr(alg_name, "Classic-McEliece") == NULL) && (strstr(alg_name, "LEDAcryptKEM-LT52") == NULL)) { pthread_t thread; void *status; int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); diff --git a/tests/test_sig.c b/tests/test_sig.c index 10b5e9309..308c28661 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -127,7 +127,7 @@ int main(int argc, char **argv) { OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS // don't run Rainbow IIIc and Vc in threads because of large stack usage - if ((strnstr(alg_name, "Rainbow-IIIc", 12) == NULL) && (strnstr(alg_name, "Rainbow-Vc", 10) == NULL)) { + if ((strstr(alg_name, "Rainbow-IIIc") == NULL) && (strstr(alg_name, "Rainbow-Vc") == NULL)) { pthread_t thread; void *status; int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); From 306d5f974754f1ddde35d362e425636b3a39b630 Mon Sep 17 00:00:00 2001 From: Vlad Gheorghiu Date: Thu, 26 Mar 2020 10:21:37 -0400 Subject: [PATCH 05/10] put no-thread kems/sigs in array --- .gitignore | 3 +++ tests/test_kem.c | 11 ++++++++++- tests/test_sig.c | 11 ++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4b51935bc..d350b8163 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ tags # CMake /build* +# CLion +/cmake-build* + # Misc __pycache__ .pytest_cache diff --git a/tests/test_kem.c b/tests/test_kem.c index d6f480253..a7a4c452b 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -166,8 +166,17 @@ int main(int argc, char **argv) { } OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS +#define MAX_LEN_KEM_NAME_ 64 // don't run Classic McEliece or LEDAcryptKEM-LT52 in threads because of large stack usage - if ((strstr(alg_name, "Classic-McEliece") == NULL) && (strstr(alg_name, "LEDAcryptKEM-LT52") == NULL)) { + char not_thread_kems[][MAX_LEN_KEM_NAME_] = {"Classic-McEliece", "LEDAcryptKEM-LT52"}; + int test_in_thread = 1; + for (size_t i = 0 ; i < sizeof(not_thread_kems) / MAX_LEN_KEM_NAME_; ++i) { + if (strstr(alg_name, not_thread_kems[i]) != NULL) { + test_in_thread = 0; + break; + } + } + if (test_in_thread) { pthread_t thread; void *status; int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); diff --git a/tests/test_sig.c b/tests/test_sig.c index 308c28661..2cbe931f2 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -126,8 +126,17 @@ int main(int argc, char **argv) { } OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS +#define MAX_LEN_SIG_NAME_ 64 // don't run Rainbow IIIc and Vc in threads because of large stack usage - if ((strstr(alg_name, "Rainbow-IIIc") == NULL) && (strstr(alg_name, "Rainbow-Vc") == NULL)) { + char not_thread_sigs[][MAX_LEN_SIG_NAME_] = {"Rainbow-IIIc", "Rainbow-Vc"}; + int test_in_thread = 1; + for (size_t i = 0 ; i < sizeof(not_thread_sigs) / MAX_LEN_SIG_NAME_; ++i) { + if (strstr(alg_name, not_thread_sigs[i]) != NULL) { + test_in_thread = 0; + break; + } + } + if (test_in_thread) { pthread_t thread; void *status; int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); From 88693c2f92eafedf5a1f03a1a0ed9cd81f5bebe3 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 26 Mar 2020 10:44:07 -0400 Subject: [PATCH 06/10] Link threads for tests --- tests/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8e47bcb8d..1d84e9db5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -62,7 +62,7 @@ add_executable(kat_kem kat_kem.c) target_link_libraries(kat_kem PRIVATE ${API_TEST_DEPS}) add_executable(test_kem test_kem.c) -target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS}) +target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS} Threads::Threads) add_executable(speed_kem speed_kem.c) target_link_libraries(speed_kem PRIVATE ${API_TEST_DEPS}) @@ -75,7 +75,7 @@ add_executable(kat_sig kat_sig.c) target_link_libraries(kat_sig PRIVATE ${API_TEST_DEPS}) add_executable(test_sig test_sig.c) -target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS}) +target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS} Threads::Threads) add_executable(speed_sig speed_sig.c) target_link_libraries(speed_sig PRIVATE ${API_TEST_DEPS}) From 532d0cb36b6b7c86fd27d19c703d771970ddfdac Mon Sep 17 00:00:00 2001 From: Vlad Gheorghiu Date: Thu, 26 Mar 2020 10:57:58 -0400 Subject: [PATCH 07/10] minor cosmetic change --- tests/test_kem.c | 16 +++++++++------- tests/test_sig.c | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/test_kem.c b/tests/test_kem.c index a7a4c452b..b86da2ffe 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -157,21 +157,23 @@ int main(int argc, char **argv) { print_system_info(); + char *alg_name = argv[1]; + if (!OQS_KEM_alg_is_enabled(alg_name)) { + printf("KEM algorithm %s not enabled!\n", alg_name); + return EXIT_FAILURE; + } + // Use system RNG in this program OQS_randombytes_switch_algorithm(OQS_RAND_alg_system); - char *alg_name = argv[1]; - if (!OQS_KEM_alg_is_enabled(alg_name)) { - return EXIT_FAILURE; - } OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS #define MAX_LEN_KEM_NAME_ 64 // don't run Classic McEliece or LEDAcryptKEM-LT52 in threads because of large stack usage - char not_thread_kems[][MAX_LEN_KEM_NAME_] = {"Classic-McEliece", "LEDAcryptKEM-LT52"}; + char no_thread_kem_patterns[][MAX_LEN_KEM_NAME_] = {"Classic-McEliece", "LEDAcryptKEM-LT52"}; int test_in_thread = 1; - for (size_t i = 0 ; i < sizeof(not_thread_kems) / MAX_LEN_KEM_NAME_; ++i) { - if (strstr(alg_name, not_thread_kems[i]) != NULL) { + for (size_t i = 0 ; i < sizeof(no_thread_kem_patterns) / MAX_LEN_KEM_NAME_; ++i) { + if (strstr(alg_name, no_thread_kem_patterns[i]) != NULL) { test_in_thread = 0; break; } diff --git a/tests/test_sig.c b/tests/test_sig.c index 2cbe931f2..f45817536 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -117,21 +117,23 @@ int main(int argc, char **argv) { print_system_info(); + char *alg_name = argv[1]; + if (!OQS_SIG_alg_is_enabled(alg_name)) { + printf("Signature algorithm %s not enabled!\n", alg_name); + return EXIT_FAILURE; + } + // Use system RNG in this program OQS_randombytes_switch_algorithm(OQS_RAND_alg_system); - char *alg_name = argv[1]; - if (!OQS_SIG_alg_is_enabled(alg_name)) { - return EXIT_FAILURE; - } OQS_STATUS rc; #if OQS_USE_PTHREADS_IN_TESTS #define MAX_LEN_SIG_NAME_ 64 // don't run Rainbow IIIc and Vc in threads because of large stack usage - char not_thread_sigs[][MAX_LEN_SIG_NAME_] = {"Rainbow-IIIc", "Rainbow-Vc"}; + char no_thread_sig_patterns[][MAX_LEN_SIG_NAME_] = {"Rainbow-IIIc", "Rainbow-Vc"}; int test_in_thread = 1; - for (size_t i = 0 ; i < sizeof(not_thread_sigs) / MAX_LEN_SIG_NAME_; ++i) { - if (strstr(alg_name, not_thread_sigs[i]) != NULL) { + for (size_t i = 0 ; i < sizeof(no_thread_sig_patterns) / MAX_LEN_SIG_NAME_; ++i) { + if (strstr(alg_name, no_thread_sig_patterns[i]) != NULL) { test_in_thread = 0; break; } From 8e1bd2327eee8ab7f2d7fc5512b8b1317e6dccab Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 26 Mar 2020 17:36:14 -0400 Subject: [PATCH 08/10] Only link against threads on clang/gcc --- tests/CMakeLists.txt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1d84e9db5..97d060e3d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -62,7 +62,11 @@ add_executable(kat_kem kat_kem.c) target_link_libraries(kat_kem PRIVATE ${API_TEST_DEPS}) add_executable(test_kem test_kem.c) -target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS} Threads::Threads) +if((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_C_COMPILER_ID STREQUAL "GNU")) + target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS} Threads::Threads) +else () + target_link_libraries(test_kem PRIVATE ${API_TEST_DEPS}) +endif() add_executable(speed_kem speed_kem.c) target_link_libraries(speed_kem PRIVATE ${API_TEST_DEPS}) @@ -75,7 +79,11 @@ add_executable(kat_sig kat_sig.c) target_link_libraries(kat_sig PRIVATE ${API_TEST_DEPS}) add_executable(test_sig test_sig.c) -target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS} Threads::Threads) +if((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_C_COMPILER_ID STREQUAL "GNU")) + target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS} Threads::Threads) +else () + target_link_libraries(test_sig PRIVATE ${API_TEST_DEPS}) +endif() add_executable(speed_sig speed_sig.c) target_link_libraries(speed_sig PRIVATE ${API_TEST_DEPS}) From 1ff23237525d4957f0d1e048b3ab4f9ee95438f9 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 26 Mar 2020 20:08:56 -0400 Subject: [PATCH 09/10] Properly pass data to/from threads --- .circleci/config.yml | 2 +- tests/test_kem.c | 20 ++++++++++++-------- tests/test_sig.c | 21 +++++++++++++-------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3baa96e71..761501628 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,7 +14,7 @@ localCheckout: &localCheckout docker: - image: ${IMAGE} steps: - - checkout # change this from "checkout" to "*localCheckout" when running CircleCI locally + - *localCheckout # change this from "checkout" to "*localCheckout" when running CircleCI locally - run: name: Configure command: scripts/git_no_checkin_in_last_day.sh || (mkdir build && cd build && cmake -GNinja ${CONFIGURE_ARGS} ..) diff --git a/tests/test_kem.c b/tests/test_kem.c index b86da2ffe..903f3c524 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -131,12 +131,15 @@ cleanup: } #if OQS_USE_PTHREADS_IN_TESTS +struct thread_data { + char *alg_name; + OQS_STATUS rc; +}; void *test_wrapper(void *arg) { - OQS_STATUS rc = kem_test_correctness((char *) arg); - long lrc = (long) rc; - pthread_exit((void *) lrc); - return (void *) lrc; + struct thread_data *td = arg; + td->rc = kem_test_correctness(td->alg_name); + return NULL; } #endif @@ -180,14 +183,15 @@ int main(int argc, char **argv) { } if (test_in_thread) { pthread_t thread; - void *status; - int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); + struct thread_data td; + td.alg_name = alg_name; + int trc = pthread_create(&thread, NULL, test_wrapper, &td); if (trc) { fprintf(stderr, "ERROR: Creating pthread\n"); return EXIT_FAILURE; } - pthread_join(thread, &status); - rc = (OQS_STATUS) status; + pthread_join(thread, NULL); + rc = td.rc; } else { rc = kem_test_correctness(alg_name); } diff --git a/tests/test_sig.c b/tests/test_sig.c index f45817536..733c6096b 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -92,11 +92,15 @@ cleanup: } #if OQS_USE_PTHREADS_IN_TESTS +struct thread_data { + char *alg_name; + OQS_STATUS rc; +}; + void *test_wrapper(void *arg) { - OQS_STATUS rc = sig_test_correctness((char *) arg); - long lrc = (long) rc; - pthread_exit((void *) lrc); - return (void *) lrc; + struct thread_data *td = arg; + td->rc = sig_test_correctness(td->alg_name); + return NULL; } #endif @@ -140,14 +144,15 @@ int main(int argc, char **argv) { } if (test_in_thread) { pthread_t thread; - void *status; - int trc = pthread_create(&thread, NULL, test_wrapper, alg_name); + struct thread_data td; + td.alg_name = alg_name; + int trc = pthread_create(&thread, NULL, test_wrapper, &td); if (trc) { fprintf(stderr, "ERROR: Creating pthread\n"); return EXIT_FAILURE; } - pthread_join(thread, &status); - rc = (OQS_STATUS) status; + pthread_join(thread, NULL); + rc = td.rc; } else { rc = sig_test_correctness(alg_name); } From fc69fa35b706add7d22d77a9db9f8d00db59b0d1 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 26 Mar 2020 21:05:04 -0400 Subject: [PATCH 10/10] Revert localCheckout --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 761501628..3baa96e71 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -14,7 +14,7 @@ localCheckout: &localCheckout docker: - image: ${IMAGE} steps: - - *localCheckout # change this from "checkout" to "*localCheckout" when running CircleCI locally + - checkout # change this from "checkout" to "*localCheckout" when running CircleCI locally - run: name: Configure command: scripts/git_no_checkin_in_last_day.sh || (mkdir build && cd build && cmake -GNinja ${CONFIGURE_ARGS} ..)