adds C-API

This commit is contained in:
Henry Case 2021-03-23 09:25:18 +00:00
parent 67ee416cb7
commit badd0c81a6
3 changed files with 104 additions and 111 deletions

View File

@ -86,7 +86,6 @@ set(CMAKE_C_FLAGS "${C_CXX_FLAGS} -D${ARCH}")
set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}") set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -D${ARCH}")
# Define sources of the components # Define sources of the components
add_subdirectory(src/sign/dilithium/dilithium2/clean) add_subdirectory(src/sign/dilithium/dilithium2/clean)
add_subdirectory(src/sign/dilithium/dilithium3/clean) add_subdirectory(src/sign/dilithium/dilithium3/clean)
add_subdirectory(src/sign/dilithium/dilithium5/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-128s-robust/clean)
add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean) add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-simple/clean)
add_subdirectory(src/sign/sphincs/sphincs-sha256-192f-robust/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") 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-512/avx2)
add_subdirectory(src/sign/falcon/falcon-1024/avx2) add_subdirectory(src/sign/falcon/falcon-1024/avx2)
add_subdirectory(src/sign/dilithium/dilithium2/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-256f-simple/avx2)
add_subdirectory(src/sign/sphincs/sphincs-shake256-256s-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-shake256-256s-robust/avx2)
add_subdirectory(src/sign/sphincs/sphincs-sha256-128f-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-simple/avx2)
add_subdirectory(src/sign/sphincs/sphincs-sha256-128s-robust/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-192f-simple/avx2)
add_subdirectory(src/sign/sphincs/sphincs-sha256-192s-robust/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-192f-robust/avx2)
add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-simple/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-robust/avx2)
add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-256f-simple/avx2)
add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2) add_subdirectory(src/sign/sphincs/sphincs-sha256-256s-robust/avx2)
endif()
# The rest of the library # KEMs
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
add_subdirectory(src/kem/kyber/kyber512/avx2) add_subdirectory(src/kem/kyber/kyber512/avx2)
add_subdirectory(src/kem/kyber/kyber768/avx2) add_subdirectory(src/kem/kyber/kyber768/avx2)
add_subdirectory(src/kem/kyber/kyber1024/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) add_subdirectory(src/kem/ntru_prime/ntrulpr857/avx2)
endif() endif()
# KEM - clean # The rest of the library
add_subdirectory(src/kem/kyber/kyber512/clean) set(SRC_COMMON_GENERIC
add_subdirectory(src/kem/kyber/kyber768/clean) src/common/aes.c
add_subdirectory(src/kem/kyber/kyber1024/clean) src/common/fips202.c
add_subdirectory(src/kem/saber/lightsaber/clean) src/common/sp800-185.c
add_subdirectory(src/kem/saber/firesaber/clean) src/common/randombytes.c
add_subdirectory(src/kem/saber/saber/clean) src/common/sha2.c
add_subdirectory(src/kem/frodo/frodokem640shake/clean) src/capi/pqapi.c
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)
add_library( add_library(
common common

69
src/capi/pqapi.c Normal file
View File

@ -0,0 +1,69 @@
#include <stdint.h>
#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)
};

View File

@ -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