|
| 1 | +cmake_minimum_required(VERSION 3.16) |
| 2 | +project(nextssl VERSION 1.0.0 LANGUAGES C) |
| 3 | + |
| 4 | +set(CMAKE_C_STANDARD 11) |
| 5 | +set(CMAKE_C_STANDARD_REQUIRED ON) |
| 6 | + |
| 7 | +# ───────────────────────────────────────────────────────────────── |
| 8 | +# Feature flags (all default ON for full build, can be toggled) |
| 9 | +# ───────────────────────────────────────────────────────────────── |
| 10 | +option(ENABLE_ML_KEM "Enable ML-KEM (Kyber) KEM family" ON) |
| 11 | +option(ENABLE_ML_DSA "Enable ML-DSA (Dilithium) sign family" ON) |
| 12 | +option(ENABLE_FALCON "Enable Falcon sign family" ON) |
| 13 | +option(ENABLE_HQC "Enable HQC KEM family" ON) |
| 14 | +option(ENABLE_MCELIECE "Enable McEliece KEM family" ON) |
| 15 | +option(ENABLE_SPHINCS "Enable SPHINCS+ sign family" ON) |
| 16 | + |
| 17 | +option(ENABLE_GMSSL "Enable GmSSL (SM2/SM3/SM4)" OFF) |
| 18 | +option(ENABLE_ED448 "Enable Ed448 / X448" ON) |
| 19 | +option(ENABLE_POMELO "Enable Pomelo memory-hard hash" OFF) |
| 20 | +option(ENABLE_MAKWA "Enable Makwa memory-hard hash" OFF) |
| 21 | +option(ENABLE_BALLOON "Enable Balloon memory-hard hash" OFF) |
| 22 | +# scrypt, yescrypt, catena, lyra2 are always built (no opt-out). |
| 23 | + |
| 24 | +option(NEXTSSL_SHARED "Build as shared library (.dll/.so)" ON) |
| 25 | + |
| 26 | +# ───────────────────────────────────────────────────────────────── |
| 27 | +# Output directory: <root>/bin/<os>/<arch>/ |
| 28 | +# ───────────────────────────────────────────────────────────────── |
| 29 | +if(WIN32) |
| 30 | + set(_NEXTSSL_OS "win") |
| 31 | +elseif(APPLE) |
| 32 | + set(_NEXTSSL_OS "macos") |
| 33 | +else() |
| 34 | + set(_NEXTSSL_OS "linux") |
| 35 | +endif() |
| 36 | + |
| 37 | +if(CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64") |
| 38 | + set(_NEXTSSL_ARCH "x86_64") |
| 39 | +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64|aarch64") |
| 40 | + set(_NEXTSSL_ARCH "arm64") |
| 41 | +else() |
| 42 | + set(_NEXTSSL_ARCH "${CMAKE_SYSTEM_PROCESSOR}") |
| 43 | +endif() |
| 44 | + |
| 45 | +set(_BIN_DIR "${PROJECT_SOURCE_DIR}/bin/${_NEXTSSL_OS}/${_NEXTSSL_ARCH}") |
| 46 | + |
| 47 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${_BIN_DIR}") # .dll on Windows |
| 48 | +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${_BIN_DIR}") # .so on Linux/macOS |
| 49 | +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${_BIN_DIR}") # .a / .lib |
| 50 | + |
| 51 | +# ───────────────────────────────────────────────────────────────── |
| 52 | +# Compile definitions from feature flags |
| 53 | +# ───────────────────────────────────────────────────────────────── |
| 54 | +set(NEXTSSL_DEFS NEXTSSL_BUILDING_DLL) |
| 55 | + |
| 56 | +if(ENABLE_ML_KEM) |
| 57 | + list(APPEND NEXTSSL_DEFS ENABLE_ML_KEM) |
| 58 | +endif() |
| 59 | +if(ENABLE_ML_DSA) |
| 60 | + list(APPEND NEXTSSL_DEFS ENABLE_ML_DSA) |
| 61 | +endif() |
| 62 | +if(ENABLE_FALCON) |
| 63 | + list(APPEND NEXTSSL_DEFS ENABLE_FALCON) |
| 64 | +endif() |
| 65 | +if(ENABLE_HQC) |
| 66 | + list(APPEND NEXTSSL_DEFS ENABLE_HQC) |
| 67 | +endif() |
| 68 | +if(ENABLE_MCELIECE) |
| 69 | + list(APPEND NEXTSSL_DEFS ENABLE_MCELIECE) |
| 70 | +endif() |
| 71 | +if(ENABLE_SPHINCS) |
| 72 | + list(APPEND NEXTSSL_DEFS ENABLE_SPHINCS) |
| 73 | +endif() |
| 74 | +if(ENABLE_GMSSL) |
| 75 | + list(APPEND NEXTSSL_DEFS NEXTSSL_HAS_GMSSL) |
| 76 | +endif() |
| 77 | +if(ENABLE_ED448) |
| 78 | + list(APPEND NEXTSSL_DEFS HAVE_ED448 HAVE_CURVE448) |
| 79 | +endif() |
| 80 | +if(ENABLE_POMELO) |
| 81 | + list(APPEND NEXTSSL_DEFS NEXTSSL_HAS_POMELO) |
| 82 | +endif() |
| 83 | +if(ENABLE_MAKWA) |
| 84 | + list(APPEND NEXTSSL_DEFS NEXTSSL_HAS_MAKWA) |
| 85 | +endif() |
| 86 | +if(ENABLE_BALLOON) |
| 87 | + list(APPEND NEXTSSL_DEFS NEXTSSL_HAS_BALLOON) |
| 88 | +endif() |
| 89 | +list(APPEND NEXTSSL_DEFS ENABLE_SCRYPT ENABLE_YESCRYPT ENABLE_CATENA ENABLE_LYRA2) |
| 90 | + |
| 91 | +# ───────────────────────────────────────────────────────────────── |
| 92 | +# Source root |
| 93 | +# ───────────────────────────────────────────────────────────────── |
| 94 | +set(SRC "${CMAKE_SOURCE_DIR}/src") |
| 95 | + |
| 96 | +# ───────────────────────────────────────────────────────────────── |
| 97 | +# SOURCE COLLECTION — single glob over all of src/, then exclude |
| 98 | +# platform-specific, broken, or duplicate files so nothing is missed |
| 99 | +# when new subdirectories are added. |
| 100 | +# ───────────────────────────────────────────────────────────────── |
| 101 | +file(GLOB_RECURSE NEXTSSL_SOURCES "${SRC}/*.c") |
| 102 | + |
| 103 | +# ── ARM-only files (require arm_neon.h — not available on x86) ── |
| 104 | +if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64|aarch64|arm") |
| 105 | + list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/aarch64/") |
| 106 | + list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/pqc/common/keccak2x/") |
| 107 | +endif() |
| 108 | + |
| 109 | +# ── PQC avx2/ — pqc_main.c only uses ref/ APIs; avx2/ = dead code on Windows |
| 110 | +# McEliece avx2: uses ELF-only .hidden ASM directives (unsupported on COFF) |
| 111 | +# ML-KEM/ML-DSA/Falcon/SPHINCS+ avx2: build requires -mavx2 but all callers |
| 112 | +# use the ref/ namespace — exclude to avoid dead-code linker noise and errors |
| 113 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/pqc/.*/avx2/") |
| 114 | + |
| 115 | +# ── Duplicate definitions (only one variant needed) ───────────── |
| 116 | +# Argon2: opt.c is the optimised x86 version; ref.c is a duplicate |
| 117 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/ref\\.c$") |
| 118 | +# Skein: skein_block.c is the primary; skeinBlockNo3F.c is a duplicate |
| 119 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/skeinBlockNo3F\\.c$") |
| 120 | + |
| 121 | +# ── Memory-hard hashes with broken/platform deps on Windows ───── |
| 122 | +# These have missing headers, Linux-only syscalls, or incomplete |
| 123 | +# vendored code. Stub ops are provided in hash_ops_disabled_stubs.c |
| 124 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/balloon/") |
| 125 | + |
| 126 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/makwa/") |
| 127 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/pomelo/") |
| 128 | +# scrypt's original sha256.c requires cpusupport.h — use sha256_portable.c instead |
| 129 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/scrypt/sha256\\.c$") |
| 130 | +# yescrypt's sha256.c uses same libcperciva_ namespace — scrypt's portable covers both |
| 131 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/yescrypt/sha256\\.c$") |
| 132 | +# yescrypt's insecure_memzero.c duplicates scrypt's — share from scrypt |
| 133 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/memory_hard/yescrypt/insecure_memzero\\.c$") |
| 134 | +# yescrypt-platform.c is #include'd by yescrypt-opt.c, not compiled separately |
| 135 | +list(FILTER NEXTSSL_SOURCES EXCLUDE REGEX "/yescrypt-platform\\.c$") |
| 136 | + |
| 137 | + |
| 138 | +# ── Conditional re-inclusion of opted-in memory-hard algos ────── |
| 139 | +if(ENABLE_BALLOON) |
| 140 | + file(GLOB_RECURSE _extra "${SRC}/hash/memory_hard/balloon/*.c") |
| 141 | + list(APPEND NEXTSSL_SOURCES ${_extra}) |
| 142 | +endif() |
| 143 | +# scrypt/yescrypt/catena/lyra2 always included via main GLOB (no exclusion) |
| 144 | +if(ENABLE_MAKWA) |
| 145 | + file(GLOB_RECURSE _extra "${SRC}/hash/memory_hard/makwa/*.c") |
| 146 | + list(APPEND NEXTSSL_SOURCES ${_extra}) |
| 147 | +endif() |
| 148 | +if(ENABLE_POMELO) |
| 149 | + file(GLOB_RECURSE _extra "${SRC}/hash/memory_hard/pomelo/*.c") |
| 150 | + list(APPEND NEXTSSL_SOURCES ${_extra}) |
| 151 | +endif() |
| 152 | + |
| 153 | +# ───────────────────────────────────────────────────────────────── |
| 154 | +# Library target |
| 155 | +# ───────────────────────────────────────────────────────────────── |
| 156 | +if(NEXTSSL_SHARED) |
| 157 | + add_library(nextssl SHARED ${NEXTSSL_SOURCES}) |
| 158 | +else() |
| 159 | + add_library(nextssl STATIC ${NEXTSSL_SOURCES}) |
| 160 | +endif() |
| 161 | + |
| 162 | +target_compile_definitions(nextssl PRIVATE ${NEXTSSL_DEFS}) |
| 163 | + |
| 164 | +target_include_directories(nextssl PUBLIC |
| 165 | + "${SRC}/root" |
| 166 | +) |
| 167 | +target_include_directories(nextssl PRIVATE |
| 168 | + "${SRC}" |
| 169 | + "${SRC}/common" |
| 170 | + "${SRC}/common/encoding" |
| 171 | + "${SRC}/common/sanitizer" |
| 172 | + "${SRC}/hash" |
| 173 | + "${SRC}/hash/interface" |
| 174 | + "${SRC}/hash/fast" |
| 175 | + "${SRC}/hash/blake" |
| 176 | + "${SRC}/hash/legacy" |
| 177 | + "${SRC}/hash/memory_hard" |
| 178 | + "${SRC}/hash/skein" |
| 179 | + "${SRC}/hash/sponge" |
| 180 | + "${SRC}/seed" |
| 181 | + "${SRC}/seed/hash" |
| 182 | + "${SRC}/seed/random" |
| 183 | + "${SRC}/seed/rng" |
| 184 | + "${SRC}/seed/drbg" |
| 185 | + "${SRC}/seed/udbf" |
| 186 | + "${SRC}/modern" |
| 187 | + "${SRC}/modern/symmetric" |
| 188 | + "${SRC}/modern/aead" |
| 189 | + "${SRC}/modern/mac" |
| 190 | + "${SRC}/modern/kdf" |
| 191 | + "${SRC}/modern/encoding" |
| 192 | + "${SRC}/modern/curve_math" |
| 193 | + "${SRC}/modern/asymmetric" |
| 194 | + "${SRC}/modern/asymmetric/rsa" |
| 195 | + "${SRC}/modern/asymmetric/micro_ecc" |
| 196 | + "${SRC}/pow" |
| 197 | + "${SRC}/pow/core" |
| 198 | + "${SRC}/pow/client" |
| 199 | + "${SRC}/pow/server" |
| 200 | + "${SRC}/pow/dhcm" |
| 201 | + "${SRC}/pqc" |
| 202 | + "${SRC}/pqc/common" |
| 203 | + "${SRC}/root/hash" |
| 204 | + "${SRC}/root/seed" |
| 205 | + "${SRC}/root/modern" |
| 206 | + "${SRC}/root/pqc" |
| 207 | + "${SRC}/root/pow" |
| 208 | +) |
| 209 | + |
| 210 | +if(ENABLE_GMSSL) |
| 211 | + target_include_directories(nextssl PRIVATE "${SRC}/modern/asymmetric/sm2") |
| 212 | +endif() |
| 213 | +if(ENABLE_BALLOON) |
| 214 | + target_include_directories(nextssl PRIVATE "${SRC}/hash/memory_hard/balloon") |
| 215 | +endif() |
| 216 | + |
| 217 | +# ───────────────────────────────────────────────────────────────── |
| 218 | +# BLAKE3 SIMD — disable intrinsics on non-x86 or when unavailable |
| 219 | +# ───────────────────────────────────────────────────────────────── |
| 220 | +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|i686|i386") |
| 221 | + set_source_files_properties("${SRC}/hash/blake/blake3_avx2.c" PROPERTIES COMPILE_FLAGS "-mavx2") |
| 222 | + set_source_files_properties("${SRC}/hash/blake/blake3_avx512.c" PROPERTIES COMPILE_FLAGS "-mavx512f -mavx512vl") |
| 223 | + set_source_files_properties("${SRC}/hash/blake/blake3_sse2.c" PROPERTIES COMPILE_FLAGS "-msse2") |
| 224 | + set_source_files_properties("${SRC}/hash/blake/blake3_sse41.c" PROPERTIES COMPILE_FLAGS "-msse4.1") |
| 225 | + # PQC Keccak-4x AVX2 (used by ML-KEM, ML-DSA, Falcon, HQC, SPHINCS+ ref/) |
| 226 | + set_source_files_properties( |
| 227 | + "${SRC}/pqc/common/keccak4x/KeccakP-1600-times4-SIMD256.c" |
| 228 | + PROPERTIES COMPILE_FLAGS "-mavx2") |
| 229 | +else() |
| 230 | + # Non-x86: disable SIMD dispatch, use portable fallback only |
| 231 | + target_compile_definitions(nextssl PRIVATE |
| 232 | + BLAKE3_NO_AVX2 BLAKE3_NO_AVX512 BLAKE3_NO_SSE2 BLAKE3_NO_SSE41) |
| 233 | +endif() |
| 234 | + |
| 235 | +# ───────────────────────────────────────────────────────────────── |
| 236 | +# Compiler warnings / optimization |
| 237 | +# ───────────────────────────────────────────────────────────────── |
| 238 | +if(MSVC) |
| 239 | + target_compile_options(nextssl PRIVATE /W3 /wd4996) |
| 240 | +else() |
| 241 | + target_compile_options(nextssl PRIVATE |
| 242 | + -Wall -Wextra -Wno-unused-parameter -Wno-sign-compare |
| 243 | + -Wno-missing-field-initializers |
| 244 | + $<$<CONFIG:Release>:-O2> |
| 245 | + $<$<CONFIG:Debug>:-g -O0> |
| 246 | + ) |
| 247 | +endif() |
| 248 | + |
| 249 | +# ───────────────────────────────────────────────────────────────── |
| 250 | +# On Windows link ws2_32 (needed by some network/random code) |
| 251 | +# ───────────────────────────────────────────────────────────────── |
| 252 | +if(WIN32) |
| 253 | + target_link_libraries(nextssl PRIVATE ws2_32 bcrypt) |
| 254 | +endif() |
| 255 | + |
| 256 | +# ───────────────────────────────────────────────────────────────── |
| 257 | +# Install rules |
| 258 | +# ───────────────────────────────────────────────────────────────── |
| 259 | +install(TARGETS nextssl |
| 260 | + RUNTIME DESTINATION bin |
| 261 | + LIBRARY DESTINATION lib |
| 262 | + ARCHIVE DESTINATION lib |
| 263 | +) |
| 264 | +install(FILES "${SRC}/root/nextssl.h" "${SRC}/root/nextssl_export.h" |
| 265 | + DESTINATION include/nextssl |
| 266 | +) |
| 267 | + |
| 268 | +# ───────────────────────────────────────────────────────────────── |
| 269 | +# Summary |
| 270 | +# ───────────────────────────────────────────────────────────────── |
| 271 | +message(STATUS "NextSSL build configuration:") |
| 272 | +message(STATUS " Type : ${CMAKE_BUILD_TYPE}") |
| 273 | +message(STATUS " Shared lib : ${NEXTSSL_SHARED}") |
| 274 | +message(STATUS " ML-KEM : ${ENABLE_ML_KEM}") |
| 275 | +message(STATUS " ML-DSA : ${ENABLE_ML_DSA}") |
| 276 | +message(STATUS " Falcon : ${ENABLE_FALCON}") |
| 277 | +message(STATUS " HQC : ${ENABLE_HQC}") |
| 278 | +message(STATUS " McEliece : ${ENABLE_MCELIECE}") |
| 279 | +message(STATUS " SPHINCS+ : ${ENABLE_SPHINCS}") |
| 280 | +message(STATUS " GmSSL : ${ENABLE_GMSSL}") |
| 281 | +message(STATUS " Ed448/X448 : ${ENABLE_ED448}") |
0 commit comments