Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Hacl_AES_128_GCM_NI and Hacl_AES_128_GCM_M32 #418

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
146 changes: 97 additions & 49 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,42 @@ set(CMAKE_C_STANDARD_REQUIRED True)
# Read config from file
include(build/config.cmake)

# x64
# Set the architecture here. These come from the CMAKE_TOOLCHAIN_FILE
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
message(STATUS "Detected an x64 architecture")
set(ARCHITECTURE intel)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_X64})

# x86
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i386|i586|i686|i86pc|ia32|x86")
message(STATUS "Detected an x86 architecture")
set(ARCHITECTURE intel)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_X86})

# arm64
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8")
message(STATUS "Detected an arm64 architecture")
set(ARCHITECTURE arm)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_ARM64})

# arm32
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armel|armhf|armv7|arm32v7")
message(STATUS "Detected an arm32 architecture")
set(ARCHITECTURE arm)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_ARM32})

# s390x
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "s390x")
message(STATUS "Detected an s390x (systemz) architecture")
set(ARCHITECTURE arm)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_SYSTEMZ})

# unsupported architecture
else()
message(FATAL_ERROR "Unsupported architecture ${CMAKE_SYSTEM_PROCESSOR}")
endif()

# Configure different targets
# TODO: Set flags for MSVC
if(NOT MSVC)
Expand Down Expand Up @@ -175,6 +211,7 @@ endif()
# - SOURCES_std: All regular files
# - SOURCES_vec128: Files that require vec128 hardware
# - SOURCES_vec256: Files that require vec256 hardware
# - SOURCES_aesni_pclmul: Files that require aes-ni/pclmul hardware

# Remove files that require missing toolchain features
# and enable the features for compilation that are available.
Expand All @@ -196,7 +233,7 @@ if(TOOLCHAIN_CAN_COMPILE_VEC128)
add_library(hacl_vec128 OBJECT ${SOURCES_vec128})
target_include_directories(hacl_vec128 PRIVATE)

if(CMAKE_SYSTEM_PROCESSOR MATCHES "i386|i586|i686|i86pc|ia32|x86_64|amd64|AMD64")
if(ARCHITECTURE MATCHES intel)
if(MSVC)
# Nothing to do here. MSVC has it covered
else()
Expand All @@ -207,7 +244,7 @@ if(TOOLCHAIN_CAN_COMPILE_VEC128)
-msse4.2
)
endif(MSVC)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8")
elseif(HACL_TARGET_ARCHITECTURE MATCHES ${HACL_ARCHITECTURE_ARM64})
target_compile_options(hacl_vec128 PRIVATE
-march=armv8-a+simd
)
Expand Down Expand Up @@ -251,7 +288,7 @@ if(TOOLCHAIN_CAN_COMPILE_VEC256)
target_include_directories(hacl_vec256 PRIVATE)

# We really should only get here on x86 architectures. But let's make sure.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "i386|i586|i686|i86pc|ia32|x86|x86_64|amd64|AMD64")
if(ARCHITECTURE MATCHES intel)
if(MSVC)
target_compile_options(hacl_vec256 PRIVATE
/arch:AVX
Expand Down Expand Up @@ -284,6 +321,39 @@ if(TOOLCHAIN_CAN_COMPILE_VALE)
set(HACL_CAN_COMPILE_VALE 1)
endif()

if(TOOLCHAIN_CAN_COMPILE_AESNI_PCLMUL AND TOOLCHAIN_CAN_COMPILE_VEC128 AND
((ARCHITECTURE MATCHES intel AND TOOLCHAIN_CAN_COMPILE_VEC256) OR
HACL_TARGET_ARCHITECTURE MATCHES ${HACL_ARCHITECTURE_ARM64}))
set(HACL_CAN_COMPILE_AESNI_PCLMUL 1)

# # We make separate compilation units (objects) for each hardware feature
list(LENGTH SOURCES_aesni_pclmul SOURCES_AESNI_PCLMUL_LEN)

if(NOT SOURCES_AESNI_PCLMUL_LEN EQUAL 0)
set(HACL_AESNI_PCLMUL_O ON)
add_library(hacl_aesni_pclmul OBJECT ${SOURCES_aesni_pclmul})
target_include_directories(hacl_aesni_pclmul PRIVATE)
if(ARCHITECTURE MATCHES intel)
if(MSVC)
# Nothing to do here. MSVC has it covered
else()
target_compile_options(hacl_aesni_pclmul PRIVATE
-msse2
-msse3
-msse4.1
-msse4.2
-maes
-mpclmul
)
endif(MSVC)
elseif(HACL_TARGET_ARCHITECTURE MATCHES ${HACL_ARCHITECTURE_ARM64})
target_compile_options(hacl_aesni_pclmul PRIVATE
-march=armv8-a+crypto
)
endif()
endif()
endif()

if(TOOLCHAIN_CAN_COMPILE_INLINE_ASM)
message(STATUS "Detected inline assembly support")
set(HACL_CAN_COMPILE_INLINE_ASM 1)
Expand All @@ -294,42 +364,6 @@ if(TOOLCHAIN_CAN_COMPILE_INTRINSICS)
set(HACL_CAN_COMPILE_INTRINSICS 1)
endif()

# x64
# Set the architecture here. These come from the CMAKE_TOOLCHAIN_FILE
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
message(STATUS "Detected an x64 architecture")
set(ARCHITECTURE intel)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_X64})

# x86
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i386|i586|i686|i86pc|ia32|x86")
message(STATUS "Detected an x86 architecture")
set(ARCHITECTURE intel)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_X86})

# arm64
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8")
message(STATUS "Detected an arm64 architecture")
set(ARCHITECTURE arm)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_ARM64})

# arm32
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armel|armhf|armv7|arm32v7")
message(STATUS "Detected an arm32 architecture")
set(ARCHITECTURE arm)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_ARM32})

# s390x
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "s390x")
message(STATUS "Detected an s390x (systemz) architecture")
set(ARCHITECTURE arm)
set(HACL_TARGET_ARCHITECTURE ${HACL_ARCHITECTURE_SYSTEMZ})

# unsupported architecture
else()
message(FATAL_ERROR "Unsupported architecture ${CMAKE_SYSTEM_PROCESSOR}")
endif()

# Write configuration
configure_file(config/Config.h.in config.h)

Expand All @@ -348,6 +382,11 @@ if(TOOLCHAIN_CAN_COMPILE_VEC256 AND HACL_VEC256_O)
target_link_libraries(hacl PRIVATE $<TARGET_OBJECTS:hacl_vec256>)
endif()

if(TOOLCHAIN_CAN_COMPILE_AESNI_PCLMUL AND HACL_AESNI_PCLMUL_O)
add_dependencies(hacl hacl_aesni_pclmul)
target_link_libraries(hacl PRIVATE $<TARGET_OBJECTS:hacl_aesni_pclmul>)
endif()

# # Static library
add_library(hacl_static STATIC ${SOURCES_std} ${VALE_OBJECTS})

Expand All @@ -359,6 +398,10 @@ if(TOOLCHAIN_CAN_COMPILE_VEC256 AND HACL_VEC256_O)
target_sources(hacl_static PRIVATE $<TARGET_OBJECTS:hacl_vec256>)
endif()

if(TOOLCHAIN_CAN_COMPILE_AESNI_PCLMUL AND HACL_AESNI_PCLMUL_O)
target_sources(hacl_static PRIVATE $<TARGET_OBJECTS:hacl_aesni_pclmul>)
endif()

# Install
# # This allows package maintainers to control the install destination by setting
# # the appropriate cache variables.
Expand Down Expand Up @@ -399,12 +442,13 @@ install(DIRECTORY vale/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vale
# # Install config.h
install(FILES build/config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hacl)

# The CPU detection is used for testing and benchmarking
if(ENABLE_TESTS OR ENABLE_BENCHMARKS)
# CPU feature detection for tests
add_library(hacl_cpu_features OBJECT ${PROJECT_SOURCE_DIR}/cpu-features/src/cpu-features.c)
target_include_directories(hacl_cpu_features PUBLIC ${PROJECT_SOURCE_DIR}/cpu-features/include)
endif(ENABLE_TESTS OR ENABLE_BENCHMARKS)
# CPU feature detection
add_library(hacl_cpu_features OBJECT ${PROJECT_SOURCE_DIR}/cpu-features/src/cpu-features.c)
target_include_directories(hacl_cpu_features PUBLIC ${PROJECT_SOURCE_DIR}/cpu-features/include)
add_dependencies(hacl hacl_cpu_features)
target_link_libraries(hacl PRIVATE $<TARGET_OBJECTS:hacl_cpu_features>)
add_dependencies(hacl_static hacl_cpu_features)
target_link_libraries(hacl_static PRIVATE $<TARGET_OBJECTS:hacl_cpu_features>)

# Add ecckiila for benchmarks
if(ENABLE_BENCHMARKS)
Expand All @@ -426,6 +470,11 @@ if(ENABLE_BENCHMARKS)
target_include_directories(digestif PUBLIC ${PROJECT_SOURCE_DIR}/third-party/digestif)
endif(ENABLE_BENCHMARKS)

# Add bearssl for benchmarks
if(ENABLE_BENCHMARKS)
include(${PROJECT_SOURCE_DIR}/third-party/bearssl/config.cmake)
add_library(bearssl OBJECT ${SOURCES_bearssl})
endif(ENABLE_BENCHMARKS)

# Testing
# It's only one binary. Everything else is done with gtest arguments.
Expand Down Expand Up @@ -465,11 +514,10 @@ if(ENABLE_TESTS)
target_compile_options(${TEST_NAME} PRIVATE /std:c++20)
endif(MSVC)

add_dependencies(${TEST_NAME} hacl hacl_cpu_features)
add_dependencies(${TEST_NAME} hacl)
target_link_libraries(${TEST_NAME} PRIVATE
gtest_main
hacl_static
hacl_cpu_features
nlohmann_json::nlohmann_json
)
target_include_directories(${TEST_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/cpu-features/include)
Expand Down Expand Up @@ -542,13 +590,13 @@ if(ENABLE_BENCHMARKS)
target_compile_options(${BENCH_NAME} PRIVATE /std:c++20)
endif(NOT MSVC)

add_dependencies(${BENCH_NAME} hacl hacl_cpu_features)
add_dependencies(${BENCH_NAME} hacl)
target_link_libraries(${BENCH_NAME} PRIVATE
hacl_static
ecckiila
blake2
digestif
hacl_cpu_features
bearssl
benchmark::benchmark
)
endforeach()
Expand Down
Loading