From 13b5bb58966330aa7084bfee3262b437d849e157 Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 20 Jun 2019 20:58:16 -0400 Subject: [PATCH 1/5] Fix macros so AES version compilation actually works --- Makefile.am | 9 ++++----- src/crypto/aes/aes.c | 22 ++++++++++++---------- src/crypto/aes/aes_ni.c | 2 +- tests/Makefile.am | 5 ++++- tests/test_aes.c | 8 ++++---- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Makefile.am b/Makefile.am index 638324f64..512541468 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,7 +11,7 @@ CLANGFORMAT ?= clang-format-3.9 SUBDIRS = ${SRCDIR} . tests -BUILT_SOURCES = oqsconfigh links +BUILT_SOURCES = links lib_LTLIBRARIES = liboqs.la liboqs_la_SOURCES = liboqs_la_LIBADD = src/common/libcommon.la @@ -55,9 +55,6 @@ if USE_OPENSSL liboqs_la_LIBADD += -L${OPENSSL_DIR}/lib -lcrypto endif -oqsconfigh: - grep OQS_ config.h > src/oqsconfig.h - installheaderdir=$(includedir)/oqs ##### OQS_COPY_FROM_PQCLEAN_FRAGMENT_INSTALLHEADER_START installheader_HEADERS= src/oqs.h \ @@ -94,9 +91,11 @@ kat: clean-kats check tests/kat_kem scripts/check_kats.sh -links: oqsconfigh +links: $(MKDIR_P) include/oqs cp -f src/oqs.h include/oqs + grep OQS_ config.h > src/oqsconfig.h + grep USE_ config.h >> src/oqsconfig.h cp -f src/oqsconfig.h include/oqs cp -f src/common/common.h include/oqs cp -f src/common/rand.h include/oqs diff --git a/src/crypto/aes/aes.c b/src/crypto/aes/aes.c index bb36c5161..b2d0efb3f 100644 --- a/src/crypto/aes/aes.c +++ b/src/crypto/aes/aes.c @@ -1,12 +1,14 @@ #include +#include + #include "aes.h" #include "aes_local.h" void OQS_AES128_load_schedule(const uint8_t *key, void **schedule, UNUSED int for_encryption) { #ifdef USE_OPENSSL oqs_aes128_load_schedule_ossl(key, schedule, for_encryption); -#elif defined(AES_ENABLE_NI) +#elif defined(USE_AES_NI) oqs_aes128_load_schedule_ni(key, schedule); #else oqs_aes128_load_schedule_c(key, schedule); @@ -16,7 +18,7 @@ void OQS_AES128_load_schedule(const uint8_t *key, void **schedule, UNUSED int fo void OQS_AES128_free_schedule(void *schedule) { #ifdef USE_OPENSSL oqs_aes128_free_schedule_ossl(schedule); -#elif defined(AES_ENABLE_NI) +#elif defined(USE_AES_NI) oqs_aes128_free_schedule_ni(schedule); #else oqs_aes128_free_schedule_c(schedule); @@ -26,7 +28,7 @@ void OQS_AES128_free_schedule(void *schedule) { void OQS_AES128_ECB_enc(const uint8_t *plaintext, const size_t plaintext_len, const uint8_t *key, uint8_t *ciphertext) { #ifdef USE_OPENSSL oqs_aes128_ecb_enc_ossl(plaintext, plaintext_len, key, ciphertext); -#elif defined(AES_ENABLE_NI) +#elif defined(USE_AES_NI) oqs_aes128_ecb_enc_ni(plaintext, plaintext_len, key, ciphertext); #else oqs_aes128_ecb_enc_c(plaintext, plaintext_len, key, ciphertext); @@ -36,7 +38,7 @@ void OQS_AES128_ECB_enc(const uint8_t *plaintext, const size_t plaintext_len, co void OQS_AES128_ECB_dec(const uint8_t *ciphertext, const size_t ciphertext_len, const uint8_t *key, uint8_t *plaintext) { #ifdef USE_OPENSSL oqs_aes128_ecb_dec_ossl(ciphertext, ciphertext_len, key, plaintext); -#elif defined(AES_ENABLE_NI) +#elif defined(USE_AES_NI) oqs_aes128_ecb_dec_ni(ciphertext, ciphertext_len, key, plaintext); #else oqs_aes128_ecb_dec_c(ciphertext, ciphertext_len, key, plaintext); @@ -46,7 +48,7 @@ void OQS_AES128_ECB_dec(const uint8_t *ciphertext, const size_t ciphertext_len, void OQS_AES128_ECB_enc_sch(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext) { #ifdef USE_OPENSSL oqs_aes128_ecb_enc_sch_ossl(plaintext, plaintext_len, schedule, ciphertext); -#elif defined(AES_ENABLE_NI) +#elif defined(USE_AES_NI) oqs_aes128_ecb_enc_sch_ni(plaintext, plaintext_len, schedule, ciphertext); #else oqs_aes128_ecb_enc_sch_c(plaintext, plaintext_len, schedule, ciphertext); @@ -56,14 +58,14 @@ void OQS_AES128_ECB_enc_sch(const uint8_t *plaintext, const size_t plaintext_len void OQS_AES128_ECB_dec_sch(const uint8_t *ciphertext, const size_t ciphertext_len, const void *schedule, uint8_t *plaintext) { #ifdef USE_OPENSSL oqs_aes128_ecb_dec_sch_ossl(ciphertext, ciphertext_len, schedule, plaintext); -#elif defined(AES_ENABLE_NI) +#elif defined(USE_AES_NI) oqs_aes128_ecb_dec_sch_ni(ciphertext, ciphertext_len, schedule, plaintext); #else oqs_aes128_ecb_dec_sch_c(ciphertext, ciphertext_len, schedule, plaintext); #endif } -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI void oqs_aes128_ecb_enc_ni(const uint8_t *plaintext, const size_t plaintext_len, const uint8_t *key, uint8_t *ciphertext) { void *schedule = NULL; oqs_aes128_load_schedule_ni(key, &schedule); @@ -79,7 +81,7 @@ void oqs_aes128_ecb_enc_c(const uint8_t *plaintext, const size_t plaintext_len, oqs_aes128_free_schedule_c(schedule); } -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI void oqs_aes128_ecb_enc_sch_ni(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext) { assert(plaintext_len % 16 == 0); for (size_t block = 0; block < plaintext_len / 16; block++) { @@ -95,7 +97,7 @@ void oqs_aes128_ecb_enc_sch_c(const uint8_t *plaintext, const size_t plaintext_l } } -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI void oqs_aes128_ecb_dec_ni(const uint8_t *ciphertext, const size_t ciphertext_len, const uint8_t *key, uint8_t *plaintext) { void *schedule = NULL; oqs_aes128_load_schedule_ni(key, &schedule); @@ -111,7 +113,7 @@ void oqs_aes128_ecb_dec_c(const uint8_t *ciphertext, const size_t ciphertext_len oqs_aes128_free_schedule_c(schedule); } -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI void oqs_aes128_ecb_dec_sch_ni(const uint8_t *ciphertext, const size_t ciphertext_len, const void *schedule, uint8_t *plaintext) { assert(ciphertext_len % 16 == 0); for (size_t block = 0; block < ciphertext_len / 16; block++) { diff --git a/src/crypto/aes/aes_ni.c b/src/crypto/aes/aes_ni.c index e70720e91..48a745ade 100644 --- a/src/crypto/aes/aes_ni.c +++ b/src/crypto/aes/aes_ni.c @@ -3,7 +3,7 @@ #include #include -#ifndef AES_ENABLE_NI +#ifndef USE_AES_NI #include void oqs_aes128_load_schedule_ni(UNUSED const uint8_t *key, UNUSED void **_schedule) { assert(0); diff --git a/tests/Makefile.am b/tests/Makefile.am index 2ea8342d0..ab4463b21 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,7 +5,10 @@ if !ENABLE_SHARED check_PROGRAMS += test_aes test_sha3 endif -LIB_FLAGS=../liboqs.la -lm -lcrypto +LIB_FLAGS=../liboqs.la -lm +if USE_OPENSSL +LIB_FLAGS += -L${OPENSSL_DIR}/lib -lcrypto +endif example_kem_SOURCES = example_kem.c example_sig_SOURCES = example_sig.c diff --git a/tests/test_aes.c b/tests/test_aes.c index 45ec96002..0d0481e69 100644 --- a/tests/test_aes.c +++ b/tests/test_aes.c @@ -72,7 +72,7 @@ static int test_aes256_correctness_c() { return EXIT_SUCCESS; } -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI static int test_aes128_correctness_ni() { uint8_t derived_plaintext[16], derived_ciphertext[16]; void *schedule = NULL; @@ -168,7 +168,7 @@ static void speed_aes256_c() { oqs_aes256_free_schedule_c(schedule); } -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI static void speed_aes128_ni() { uint8_t plaintext[16], ciphertext[16]; void *schedule = NULL; @@ -253,7 +253,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; if (test_aes256_correctness_c() != EXIT_SUCCESS) return EXIT_FAILURE; -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI if (test_aes128_correctness_ni() != EXIT_SUCCESS) return EXIT_FAILURE; #endif @@ -270,7 +270,7 @@ int main(int argc, char **argv) { PRINT_TIMER_HEADER speed_aes128_c(); speed_aes256_c(); -#ifdef AES_ENABLE_NI +#ifdef USE_AES_NI speed_aes128_ni(); #endif #ifdef USE_OPENSSL From 6d4cb08acd70ed5a4fec99185afbcf8f927d053b Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 20 Jun 2019 20:59:40 -0400 Subject: [PATCH 2/5] Update to PQClean AES release context --- src/common/pqclean_shims/aes.h | 18 ++++++++---------- .../pqclean_frodokem1344aes_clean/matrix_aes.c | 2 ++ .../pqclean_frodokem640aes_clean/matrix_aes.c | 2 ++ .../pqclean_frodokem976aes_clean/matrix_aes.c | 2 ++ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/common/pqclean_shims/aes.h b/src/common/pqclean_shims/aes.h index 70c116b8c..fa801fec5 100644 --- a/src/common/pqclean_shims/aes.h +++ b/src/common/pqclean_shims/aes.h @@ -14,16 +14,14 @@ typedef void * aes128ctx; -static void aes128_keyexp(aes128ctx *r, const unsigned char *key) { - OQS_AES128_load_schedule(key, r, 1); -} +#define aes128_keyexp(r, key) OQS_AES128_load_schedule((key), (r), 1); +#define aes128_ecb(out, in, nblocks, ctx) OQS_AES128_ECB_enc_sch((in), (nblocks) * AES_BLOCKBYTES, *(ctx), (out)); +#define aes128_ctx_release(ctx) OQS_AES128_free_schedule(*(ctx)); -static void aes128_ecb(unsigned char *out, const unsigned char *in, size_t nblocks, aes128ctx *ctx) { - OQS_AES128_ECB_enc_sch(in, nblocks * AES_BLOCKBYTES, *ctx, out); - OQS_AES128_free_schedule(*ctx); - // FIXME: PQClean AES API expects that aes128_ecb can be called multiple - // times with the same key schedule, but this instantiation does not, since - // it frees the key schedule immediately -} +typedef void * aes256ctx; + +#define aes256_keyexp(r, key) OQS_AES256_load_schedule((key), (r), 1); +#define aes256_ecb(out, in, nblocks, ctx) OQS_AES256_ECB_enc_sch((in), (nblocks) * AES_BLOCKBYTES, *(ctx), (out)); +#define aes256_ctx_release(ctx) OQS_AES256_free_schedule(*(ctx)); #endif diff --git a/src/kem/frodokem/pqclean_frodokem1344aes_clean/matrix_aes.c b/src/kem/frodokem/pqclean_frodokem1344aes_clean/matrix_aes.c index 645901b05..7b9a830a7 100644 --- a/src/kem/frodokem/pqclean_frodokem1344aes_clean/matrix_aes.c +++ b/src/kem/frodokem/pqclean_frodokem1344aes_clean/matrix_aes.c @@ -33,6 +33,7 @@ int PQCLEAN_FRODOKEM1344AES_CLEAN_mul_add_as_plus_e(uint16_t *out, const uint16_ } aes128_ecb((uint8_t *) A, (uint8_t *) A, PARAMS_N * PARAMS_N * sizeof(int16_t) / AES_BLOCKBYTES, &ctx128); + aes128_ctx_release(&ctx128); for (i = 0; i < PARAMS_N * PARAMS_N; i++) { A[i] = PQCLEAN_FRODOKEM1344AES_CLEAN_LE_TO_UINT16(A[i]); @@ -73,6 +74,7 @@ int PQCLEAN_FRODOKEM1344AES_CLEAN_mul_add_sa_plus_e(uint16_t *out, const uint16_ } aes128_ecb((uint8_t *) A, (uint8_t *) A, PARAMS_N * PARAMS_N * sizeof(int16_t) / AES_BLOCKBYTES, &ctx128); + aes128_ctx_release(&ctx128); for (i = 0; i < PARAMS_N * PARAMS_N; i++) { A[i] = PQCLEAN_FRODOKEM1344AES_CLEAN_LE_TO_UINT16(A[i]); diff --git a/src/kem/frodokem/pqclean_frodokem640aes_clean/matrix_aes.c b/src/kem/frodokem/pqclean_frodokem640aes_clean/matrix_aes.c index 65344e3d9..1858b7549 100644 --- a/src/kem/frodokem/pqclean_frodokem640aes_clean/matrix_aes.c +++ b/src/kem/frodokem/pqclean_frodokem640aes_clean/matrix_aes.c @@ -33,6 +33,7 @@ int PQCLEAN_FRODOKEM640AES_CLEAN_mul_add_as_plus_e(uint16_t *out, const uint16_t } aes128_ecb((uint8_t *) A, (uint8_t *) A, PARAMS_N * PARAMS_N * sizeof(int16_t) / AES_BLOCKBYTES, &ctx128); + aes128_ctx_release(&ctx128); for (i = 0; i < PARAMS_N * PARAMS_N; i++) { A[i] = PQCLEAN_FRODOKEM640AES_CLEAN_LE_TO_UINT16(A[i]); @@ -73,6 +74,7 @@ int PQCLEAN_FRODOKEM640AES_CLEAN_mul_add_sa_plus_e(uint16_t *out, const uint16_t } aes128_ecb((uint8_t *) A, (uint8_t *) A, PARAMS_N * PARAMS_N * sizeof(int16_t) / AES_BLOCKBYTES, &ctx128); + aes128_ctx_release(&ctx128); for (i = 0; i < PARAMS_N * PARAMS_N; i++) { A[i] = PQCLEAN_FRODOKEM640AES_CLEAN_LE_TO_UINT16(A[i]); diff --git a/src/kem/frodokem/pqclean_frodokem976aes_clean/matrix_aes.c b/src/kem/frodokem/pqclean_frodokem976aes_clean/matrix_aes.c index 2596fc25d..f02ffb708 100644 --- a/src/kem/frodokem/pqclean_frodokem976aes_clean/matrix_aes.c +++ b/src/kem/frodokem/pqclean_frodokem976aes_clean/matrix_aes.c @@ -33,6 +33,7 @@ int PQCLEAN_FRODOKEM976AES_CLEAN_mul_add_as_plus_e(uint16_t *out, const uint16_t } aes128_ecb((uint8_t *) A, (uint8_t *) A, PARAMS_N * PARAMS_N * sizeof(int16_t) / AES_BLOCKBYTES, &ctx128); + aes128_ctx_release(&ctx128); for (i = 0; i < PARAMS_N * PARAMS_N; i++) { A[i] = PQCLEAN_FRODOKEM976AES_CLEAN_LE_TO_UINT16(A[i]); @@ -73,6 +74,7 @@ int PQCLEAN_FRODOKEM976AES_CLEAN_mul_add_sa_plus_e(uint16_t *out, const uint16_t } aes128_ecb((uint8_t *) A, (uint8_t *) A, PARAMS_N * PARAMS_N * sizeof(int16_t) / AES_BLOCKBYTES, &ctx128); + aes128_ctx_release(&ctx128); for (i = 0; i < PARAMS_N * PARAMS_N; i++) { A[i] = PQCLEAN_FRODOKEM976AES_CLEAN_LE_TO_UINT16(A[i]); From e1d3335dd8026090a89968596ec41436bc95447a Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 20 Jun 2019 21:17:17 -0400 Subject: [PATCH 3/5] Use oqs.h since oqsconfig.h not present on Windows --- src/crypto/aes/aes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/aes/aes.c b/src/crypto/aes/aes.c index b2d0efb3f..8df52c376 100644 --- a/src/crypto/aes/aes.c +++ b/src/crypto/aes/aes.c @@ -1,6 +1,6 @@ #include -#include +#include #include "aes.h" #include "aes_local.h" From 158102c505e5c98ec9898e2e24a660efb85644fc Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 20 Jun 2019 21:17:17 -0400 Subject: [PATCH 4/5] Use oqs.h since oqsconfig.h not present on Windows --- src/crypto/aes/aes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/aes/aes.c b/src/crypto/aes/aes.c index b2d0efb3f..8df52c376 100644 --- a/src/crypto/aes/aes.c +++ b/src/crypto/aes/aes.c @@ -1,6 +1,6 @@ #include -#include +#include #include "aes.h" #include "aes_local.h" From e7a1b2d1350306b197c78bbd701f0a4ad0d278ad Mon Sep 17 00:00:00 2001 From: Douglas Stebila Date: Thu, 27 Jun 2019 14:16:05 -0400 Subject: [PATCH 5/5] Add Dilithium filters (#503) --- VisualStudio/oqs/oqs.vcxproj.filters | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/VisualStudio/oqs/oqs.vcxproj.filters b/VisualStudio/oqs/oqs.vcxproj.filters index d9b2b2c39..cc4c57a15 100644 --- a/VisualStudio/oqs/oqs.vcxproj.filters +++ b/VisualStudio/oqs/oqs.vcxproj.filters @@ -695,6 +695,18 @@ {143e4927-3f7d-449f-b1d9-669993470c2f} + + {de97684a-bf94-413e-ad0d-477202dedea6} + + + {cf8dbf1d-cf1a-4ce9-893d-85841bced3fb} + + + {c7244cdd-79f6-4b9a-9a4e-ed358dc0cd79} + + + {f379218e-10bf-4e7a-ac6e-7e283568ff68} + {38993d7a-4180-4824-9451-f118b8df2fbd}