1project('libecc', 'c', 2 meson_version: '>=1.1.0', 3 version: run_command('dunamai', 'from', 'git', '--style', 'semver', '--dirty', check: true).stdout().strip(), 4 default_options: ['c_std=gnu11', 'default_library=static', 'warning_level=3' ], 5 license: 'BSD-3-Clause OR GPL-2.0-or-later', 6 license_files: [ 'LICENSE' ], 7) 8 9top_dir = meson.current_source_dir() 10 11# dunamai is used for version sync with gconf (in the same way poetry-dynamic-versioning does) 12pymod = import('python') 13py3 = pymod.find_installation('python3', modules: ['dunamai']) 14 15# about libecc 16# below file listings should be moved in successive small 'meson.build' file in each subdir, that 17# only contains the file listing, replacing them with a lonely 'subdir('src/xxx') instead. 18 19ecc_inc = include_directories('include') 20subdir('include/libecc') 21 22# module declaration, each module files listing is declare in its own directory 23subdir('src/utils') 24subdir('src/nn') 25subdir('src/fp') 26subdir('src/curves') 27subdir('src/hash') 28subdir('src/sig') 29subdir('src/ecdh') 30subdir('src/external_deps') 31 32# detect if an external rand source implementation is passed 33use_external_rand_opt = get_option('use_external_rand') 34if use_external_rand_opt 35 rand_src = files(get_option('with_rand_source')) 36else 37 rand_src = files() 38endif 39# detect if an external print source implementation is passed 40use_external_print_opt = get_option('use_external_print') 41if use_external_print_opt 42 print_src = files(get_option('with_print_source')) 43else 44 print_src = files() 45endif 46# detect if an external time source implementation is passed 47use_external_time_opt = get_option('use_external_time') 48if use_external_time_opt 49 time_src = files(get_option('with_time_source')) 50else 51 time_src = files() 52endif 53 54 55# globally used build args, TODO: to make configurable 56build_args = [ 57 '-fno-builtin', 58 '-D_FORTIFY_SOURCE=2', 59 '-fstack-protector-strong', 60 '-DUSE_WARN_UNUSED_RET', 61 '-ffreestanding' 62] 63 64 65# About configurable options, based on makefiles 66 67# 1. build-relative options first 68 69# info compilers arguments (such as m32/m64 should be cross-file related. 70# just using a different cross-file (event for m32 on m64 arch for e.g. is enough 71# to change the behavior 72with_wordsize_opt = get_option('with_wordsize') 73if with_wordsize_opt == '16' 74 build_args += '-DWORDSIZE=16' 75elif with_wordsize_opt == '32' 76 build_args += '-DWORDSIZE=32' 77elif with_wordsize_opt == '64' 78 build_args += '-DWORDSIZE=64' 79endif 80 81with_stdlib_opt = get_option('with_stdlib') 82if with_stdlib_opt 83 build_args += '-DWITH_STDLIB' 84endif 85 86with_debug_opt = get_option('with_debug') 87if with_debug_opt 88 build_args += '-DDEBUG' 89endif 90 91use_cryptofuzz_opt = get_option('use_cryptofuzz') 92if use_cryptofuzz_opt 93 build_args += '-DUSE_CRYPTOFUZZ' 94endif 95 96assert_print_opt = get_option('assert_print') 97if assert_print_opt 98 build_args += '-DUSE_ASSERT_PRINT' 99endif 100 101no_warn_unused_ret_opt = get_option('no_warn_unused_ret') 102if not no_warn_unused_ret_opt 103 build_args += '-DUSE_WARN_UNUSED_RET' 104endif 105 106# 2. security relative options 107 108with_sig_blinding_opt = get_option('with_sig_blinding') 109if with_sig_blinding_opt 110 build_args += '-DUSE_SIG_BLINDING' 111endif 112 113with_complete_formulas_opt = get_option('with_complete_formulas') 114if with_complete_formulas_opt 115 build_args += '-DNO_USE_COMPLETE_FORMULAS' 116endif 117 118with_double_add_opt = get_option('with_double_add') 119if with_double_add_opt == 'true' 120 build_args += '-DUSE_DOUBLE_ADD_ALWAYS' 121elif with_double_add_opt == 'false' 122 build_args += '-DUSE_MONTY_LADDER' 123endif 124 125with_monty_ladder_opt = get_option('with_monty_ladder') 126if with_monty_ladder_opt == 'true' 127 build_args += '-DUSE_MONTY_LADDER' 128elif with_monty_ladder_opt == 'false' 129 build_args += '-DUSE_DOUBLE_ADD_ALWAYS' 130endif 131 132assert(not (with_double_add_opt == 'true' and with_monty_ladder_opt == 'true'), 'with_double_add and with_monty_ladder are incompatible options!') 133 134# 3. crypto engines relative options. It is possible to fully disable all 135# engines and manually select which one must be built 136 137with_override_ecc_config_opt = get_option('with_override_ecc_config') 138if with_override_ecc_config_opt 139 build_args += '-DWITH_LIBECC_CONFIG_OVERRIDE' 140endif 141 142if with_override_ecc_config_opt 143 # Handle the asked curves 144 with_curves_opt = get_option('with_curves') 145 assert(with_curves_opt.length() != 0, 'You have selected a libecc configuration override with no curve: please select at least one proper curve!') 146 foreach curve : with_curves_opt 147 build_args += '-DWITH_CURVE_'+curve.to_upper() 148 endforeach 149 # Handle the asked hashes 150 with_hashes_opt = get_option('with_hashes') 151 assert(with_hashes_opt.length() != 0, 'You have selected a libecc configuration override with no hash: please select at least one proper hash!') 152 foreach hash : with_hashes_opt 153 build_args += '-DWITH_HASH_'+hash.to_upper() 154 endforeach 155 # Handle the asked algorithms 156 with_algs_opt = get_option('with_algs') 157 assert(with_algs_opt.length() != 0, 'You have selected a libecc configuration override with no algorithm: please select at least one proper algorithm!') 158 foreach alg : with_algs_opt 159 build_args += '-DWITH_'+alg.to_upper() 160 endforeach 161endif 162 163# Small stack handling 164with_small_stack_opt = get_option('with_small_stack') 165if with_small_stack_opt 166 build_args += '-DUSE_SMALL_STACK' 167endif 168assert(not (with_small_stack_opt and with_sig_eddsa25519_opt), 'Small stack and EdDSA are incompatible options!') 169assert(not (with_small_stack_opt and with_sig_eddsa448_opt), 'Small stack and EdDSA are incompatible options!') 170assert(not (with_small_stack_opt and with_x25519_opt), 'Small stack and X25519 are incompatible options!') 171assert(not (with_small_stack_opt and with_x448_opt), 'Small stack and X448 are incompatible options!') 172 173 174# libecc libraries declaration. For each library, the library itself and the 175# corresponding dependency object (includedir and library to link with) is 176# also declared 177# 178# INFO: defaulting to static lib only (see project declaration). 179# to build both static and dynamic library, use -Ddefault_library=both option 180# 181libarith_lib = library('arith', 182 sources: [ 183 fp_mod_src, 184 nn_mod_src, 185 rand_src, 186 print_src, 187 time_src, 188 utils_arith_src, 189 ], 190 include_directories: ecc_inc, 191 install : true, 192 c_args: build_args, 193) 194libarith_dep = declare_dependency( 195 link_with: libarith_lib, 196 include_directories: ecc_inc, 197) 198 199libec_lib = library('ecc', 200 sources: [ 201 curves_mod_src, 202 utils_ec_src, 203 fp_mod_src, 204 nn_mod_src, 205 rand_src, 206 print_src, 207 time_src, 208 utils_arith_src 209 ], 210 include_directories: ecc_inc, 211 install : true, 212 c_args: build_args, 213) 214libec_dep = declare_dependency( 215 link_with: libec_lib, 216 include_directories: ecc_inc, 217) 218 219libsign_lib = library('sign', 220 sources: [ 221 hash_mod_src, 222 sig_mod_src, 223 key_mod_src, 224 utils_sign_src, 225 ecdh_mod_src, 226 curves_mod_src, 227 utils_ec_src, 228 fp_mod_src, 229 nn_mod_src, 230 rand_src, 231 print_src, 232 time_src, 233 utils_arith_src 234 ], 235 include_directories: ecc_inc, 236 install : true, 237 c_args: build_args, 238) 239libsign_dep = declare_dependency( 240 link_with: libsign_lib, 241 include_directories: ecc_inc, 242) 243 244# in order to build native tools that depends on libsign library, we should 245# check if the nominal library built has been made native or cross. If 246# cross, another build must be made natively for native tooling 247if meson.is_cross_build() 248 native_libsign_lib = library('sign_native', 249 sources: [ 250 hash_mod_src, 251 sig_mod_src, 252 key_mod_src, 253 utils_sign_src, 254 ecdh_mod_src, 255 curves_mod_src, 256 utils_ec_src, 257 fp_mod_src, 258 nn_mod_src, 259 rand_src, 260 print_src, 261 time_src, 262 utils_arith_src 263 ], 264 include_directories: ecc_inc, 265 install : false, 266 c_args: build_args, 267 native: true, 268 ) 269 native_libsign_dep = declare_dependency( 270 link_with: native_libsign_lib, 271 include_directories: ecc_inc, 272 ) 273else 274 native_libsign_lib = libsign_lib 275 native_libsign_dep = libsign_dep 276endif 277 278# About tests, see src/tests/meson.build. Enabled with -Dwith_tests=true 279with_tests_opt = get_option('with_tests') 280if with_tests_opt 281 subdir('src/tests') 282endif 283