From 5b79e3bc2070fa12e2ebf6df3a49d597dd918e40 Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Tue, 23 Mar 2021 09:25:18 +0000 Subject: [PATCH] adds C-API --- CMakeLists.txt | 73 +++++++++++++++++------------------ src/capi/pqapi.c | 69 +++++++++++++++++++++++++++++++++ test/Makefile.Microsoft_nmake | 73 ----------------------------------- 3 files changed, 104 insertions(+), 111 deletions(-) create mode 100644 src/capi/pqapi.c delete mode 100644 test/Makefile.Microsoft_nmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a9c41a9..76a7ce9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,7 +86,6 @@ set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -D${ARCH}") set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}") # Define sources of the components - add_subdirectory(src/sign/dilithium/dilithium2/clean) add_subdirectory(src/sign/dilithium/dilithium3/clean) add_subdirectory(src/sign/dilithium/dilithium5/clean) @@ -119,10 +118,33 @@ add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/clean) add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/clean) add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean) add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/clean) +add_subdirectory(src/kem/kyber/kyber512/clean) +add_subdirectory(src/kem/kyber/kyber768/clean) +add_subdirectory(src/kem/kyber/kyber1024/clean) +add_subdirectory(src/kem/saber/lightsaber/clean) +add_subdirectory(src/kem/saber/firesaber/clean) +add_subdirectory(src/kem/saber/saber/clean) +add_subdirectory(src/kem/frodo/frodokem640shake/clean) +add_subdirectory(src/kem/frodo/frodokem976shake/clean) +add_subdirectory(src/kem/frodo/frodokem1344shake/clean) +add_subdirectory(src/kem/ntru/ntruhps4096821/clean) +add_subdirectory(src/kem/ntru/ntruhps2048509/clean) +add_subdirectory(src/kem/ntru/ntruhrss701/clean) +add_subdirectory(src/kem/ntru/ntruhps2048677/clean) +add_subdirectory(src/kem/ntru_prime/ntrulpr761/clean) +add_subdirectory(src/kem/ntru_prime/ntrulpr653/clean) +add_subdirectory(src/kem/ntru_prime/ntrulpr857/clean) -# AVX2 targets +# Hardware optimized targets if(${ARCH} STREQUAL "ARCH_x86_64") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native -mtune=native") + +set(CMAKE_C_FLAGS + "${CMAKE_C_FLAGS} -march=native -mtune=native") +set(SRC_COMMON_AVX2 + src/common/keccak4x/KeccakP-1600-times4-SIMD256.c +) + +# Sign add_subdirectory(src/sign/falcon/falcon-512/avx2) add_subdirectory(src/sign/falcon/falcon-1024/avx2) add_subdirectory(src/sign/dilithium/dilithium2/avx2) @@ -140,7 +162,6 @@ add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-robust/avx2) add_subdirectory(src/sign/sphincs/sphincs-shake256-256f-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-robust/avx2) - add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-robust/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/avx2) @@ -149,28 +170,12 @@ add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/avx2) - add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-robust/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2) -endif() -# The rest of the library -set(SRC_COMMON_GENERIC - src/common/aes.c - src/common/fips202.c - src/common/sp800-185.c - src/common/randombytes.c - src/common/sha2.c -) - -if(${ARCH} STREQUAL "ARCH_x86_64") -set(SRC_COMMON_AVX2 - src/common/keccak4x/KeccakP-1600-times4-SIMD256.c -) - -# KEM - AVX2 +# KEMs add_subdirectory(src/kem/kyber/kyber512/avx2) add_subdirectory(src/kem/kyber/kyber768/avx2) add_subdirectory(src/kem/kyber/kyber1024/avx2) @@ -186,23 +191,15 @@ add_subdirectory(src/kem/ntru_prime/ntrulpr653/avx2) add_subdirectory(src/kem/ntru_prime/ntrulpr857/avx2) endif() -# KEM - clean -add_subdirectory(src/kem/kyber/kyber512/clean) -add_subdirectory(src/kem/kyber/kyber768/clean) -add_subdirectory(src/kem/kyber/kyber1024/clean) -add_subdirectory(src/kem/saber/lightsaber/clean) -add_subdirectory(src/kem/saber/firesaber/clean) -add_subdirectory(src/kem/saber/saber/clean) -add_subdirectory(src/kem/frodo/frodokem640shake/clean) -add_subdirectory(src/kem/frodo/frodokem976shake/clean) -add_subdirectory(src/kem/frodo/frodokem1344shake/clean) -add_subdirectory(src/kem/ntru/ntruhps4096821/clean) -add_subdirectory(src/kem/ntru/ntruhps2048509/clean) -add_subdirectory(src/kem/ntru/ntruhrss701/clean) -add_subdirectory(src/kem/ntru/ntruhps2048677/clean) -add_subdirectory(src/kem/ntru_prime/ntrulpr761/clean) -add_subdirectory(src/kem/ntru_prime/ntrulpr653/clean) -add_subdirectory(src/kem/ntru_prime/ntrulpr857/clean) +# The rest of the library +set(SRC_COMMON_GENERIC + src/common/aes.c + src/common/fips202.c + src/common/sp800-185.c + src/common/randombytes.c + src/common/sha2.c + src/capi/pqapi.c +) add_library( common diff --git a/src/capi/pqapi.c b/src/capi/pqapi.c new file mode 100644 index 00000000..c0a69db9 --- /dev/null +++ b/src/capi/pqapi.c @@ -0,0 +1,69 @@ +#include +#include "kem/kyber/kyber512/clean/api.h" + +// helpers +#define OPT_VERSION _CLEAN_ + + +// Helper to stringify constants +#define STR(x) STR_(x) +#define STR_(x) #x + +/* Concatenate tokens X and Y. Can be done by the "##" operator in + * simple cases, but has some side effects in more complicated cases. + */ +#define GLUE(a, b) GLUE_(a, b) +#define GLUE_(a, b) a##b + +// Returns prefix defined by PQClean, depending +// on OPT_VERSION setting. +// Something like: "PQCLEAN_KYBER512_CLEAN_" +#define A(x) \ + GLUE(PQCLEAN_, \ + GLUE(x, OPT_VERSION)) \ + +#define PQC_PUB_KEY_BSZ(x) GLUE(A(x), CRYPTO_PUBLICKEYBYTES) +#define PQC_PRV_KEY_BSZ(x) GLUE(A(x), CRYPTO_SECRETKEYBYTES) +#define PQC_KEM_BSZ(x) GLUE(A(x), CRYPTO_BYTES) +#define PQC_SIGN_BSZ(x) GLUE(A(x), CRYPTO_BYTES) +#define PQC_CT_BSZ(x) GLUE(A(x), CRYPTO_CIPHERTEXTBYTES) +#define PQC_NAME(x) GLUE(A(x), CRYPTO_ALGNAME) + + +// Parameters of the scheme +typedef struct params_t { + const uint8_t alg_id; + const char* alg_name; + const uint32_t prv_key_bsz; + const uint32_t pub_key_bsz; +} params_t; + +typedef struct kem_params_t { + params_t p; + const uint32_t ciphertext_bsz; + const uint32_t secret_bsz; +} kem_params_t; + +typedef struct sig_params_t { + params_t p; + const uint32_t sign_bsz; +} sig_params_t; + +// Macro magic needed to initialize parameters for a scheme +#define REG_KEM(ID) \ +{ \ + .p.alg_id = ID, \ + .p.alg_name = STR(ID), \ + .p.prv_key_bsz = PQC_PRV_KEY_BSZ(ID), \ + .p.pub_key_bsz = PQC_PUB_KEY_BSZ(ID), \ + .ciphertext_bsz = PQC_CT_BSZ(ID), \ + .secret_bsz = PQC_KEM_BSZ(ID) \ +} + +enum { + KYBER512 +}; + +const kem_params_t kems[] = { + REG_KEM(KYBER512) +}; diff --git a/test/Makefile.Microsoft_nmake b/test/Makefile.Microsoft_nmake deleted file mode 100644 index d5b1d191..00000000 --- a/test/Makefile.Microsoft_nmake +++ /dev/null @@ -1,73 +0,0 @@ -# This Makefile can be used with Microsoft Visual Studio's nmake using the command: -# nmake /f Makefile.Microsoft_nmake - -# override as desired, use /E -# vim: set ts=4 sw=4 et: -TYPE=kem -SCHEME=kyber768 -SCHEME_UPPERCASE=KYBER768 -IMPLEMENTATION=clean -IMPLEMENTATION_UPPERCASE=CLEAN - -SCHEME_DIR=..\crypto_$(TYPE)\$(SCHEME)\$(IMPLEMENTATION) - -TEST_COMMON_DIR=..\test\common -COMMON_DIR=..\common -COMMON_OBJECTS=$(COMMON_DIR)\aes.obj $(COMMON_DIR)\fips202.obj $(COMMON_DIR)\sha2.obj $(COMMON_DIR)\nistseedexpander.obj $(COMMON_DIR)\sp800-185.obj -COMMON_OBJECTS_NOPATH=aes.obj fips202.obj sha2.obj sp800-185.obj nistseedexpander.obj - -DEST_DIR=..\bin - -CFLAGS=/nologo /O2 /I $(COMMON_DIR) /W4 /WX - -all: $(DEST_DIR)\functest_$(SCHEME)_$(IMPLEMENTATION).EXE $(DEST_DIR)\testvectors_$(SCHEME)_$(IMPLEMENTATION).EXE - -build-scheme: - cd $(SCHEME_DIR) - nmake /f Makefile.Microsoft_nmake - cd ..\..\..\test - -clean-scheme: - cd $(SCHEME_DIR) - nmake /f Makefile.Microsoft_nmake clean - cd ..\..\..\test - -functest: $(DEST_DIR)\functest_$(SCHEME)_$(IMPLEMENTATION).exe - -testvectors: $(DEST_DIR)\testvectors_$(SCHEME)_$(IMPLEMENTATION).exe - -printparams: $(DEST_DIR)\printparams_$(SCHEME)_$(IMPLEMENTATION).exe - -nistkat: $(DEST_DIR)\nistkat_$(SCHEME)_$(IMPLEMENTATION).exe - -$(DEST_DIR)\functest_$(SCHEME)_$(IMPLEMENTATION).exe: build-scheme $(COMMON_OBJECTS) $(COMMON_DIR)\randombytes.obj - -MKDIR $(DEST_DIR) - -DEL functest.obj - $(CC) /c crypto_$(TYPE)\functest.c $(CFLAGS) /I $(SCHEME_DIR) /DPQCLEAN_NAMESPACE=PQCLEAN_$(SCHEME_UPPERCASE)_$(IMPLEMENTATION_UPPERCASE) - LINK.EXE /STACK:8192000 /OUT:$@ functest.obj $(COMMON_OBJECTS_NOPATH) randombytes.obj $(SCHEME_DIR)\lib$(SCHEME)_$(IMPLEMENTATION).lib Advapi32.lib - -$(DEST_DIR)\testvectors_$(SCHEME)_$(IMPLEMENTATION).exe: build-scheme $(COMMON_OBJECTS) $(TEST_COMMON_DIR)\notrandombytes.obj - -MKDIR $(DEST_DIR) - -DEL testvectors.obj - $(CC) /c crypto_$(TYPE)\testvectors.c $(CFLAGS) /I $(SCHEME_DIR) /DPQCLEAN_NAMESPACE=PQCLEAN_$(SCHEME_UPPERCASE)_$(IMPLEMENTATION_UPPERCASE) - LINK.EXE /STACK:8192000 /OUT:$@ testvectors.obj $(COMMON_OBJECTS_NOPATH) notrandombytes.obj $(SCHEME_DIR)\lib$(SCHEME)_$(IMPLEMENTATION).lib - -$(DEST_DIR)\printparams_$(SCHEME)_$(IMPLEMENTATION).exe: crypto_$(TYPE)\printparams.c $(SCHEME_DIR)\api.h - -MKDIR $(DEST_DIR) - -DEL printparams.obj - $(CC) /c crypto_$(TYPE)\printparams.c $(CFLAGS) /I $(SCHEME_DIR) /DPQCLEAN_NAMESPACE=PQCLEAN_$(SCHEME_UPPERCASE)_$(IMPLEMENTATION_UPPERCASE) - LINK.EXE /OUT:$@ printparams.obj - -$(DEST_DIR)\nistkat_$(SCHEME)_$(IMPLEMENTATION).exe: build-scheme $(COMMON_OBJECTS) $(TEST_COMMON_DIR)\nistkatrng.obj - -MKDIR $(DEST_DIR) - -DEL nistkat.obj - $(CC) /c crypto_$(TYPE)\nistkat.c $(CFLAGS) /I $(SCHEME_DIR) /DPQCLEAN_NAMESPACE=PQCLEAN_$(SCHEME_UPPERCASE)_$(IMPLEMENTATION_UPPERCASE) - LINK.EXE /STACK:8192000 /OUT:$@ nistkat.obj $(COMMON_OBJECTS_NOPATH) nistkatrng.obj $(SCHEME_DIR)\lib$(SCHEME)_$(IMPLEMENTATION).lib - -clean: - -DEL functest.obj testvectors.obj printparams.obj nistkat.obj - -DEL $(COMMON_OBJECTS_NOPATH) randombytes.obj notrandombytes.obj - -DEL $(DEST_DIR)\functest_$(SCHEME)_$(IMPLEMENTATION).exe - -DEL $(DEST_DIR)\testvectors_$(SCHEME)_$(IMPLEMENTATION).exe - -DEL $(DEST_DIR)\printparams_$(SCHEME)_$(IMPLEMENTATION).exe - -DEL $(DEST_DIR)\nistkat_$(SCHEME)_$(IMPLEMENTATION).exe