Skip to content

Commit ef8c3a1

Browse files
pks-tgitster
authored andcommitted
meson: make the CSPRNG backend configurable
The CSPRNG backend is not configurable in Meson and isn't quite discoverable, either. Make it configurable and add the actual backend used to the summary. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28911f7 commit ef8c3a1

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

meson.build

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ if not meson.is_cross_build() and fs.exists('/dev/tty')
13321332
libgit_c_args += '-DHAVE_DEV_TTY'
13331333
endif
13341334

1335+
csprng_backend = get_option('csprng_backend')
13351336
https_backend = get_option('https_backend')
13361337
sha1_backend = get_option('sha1_backend')
13371338
sha1_unsafe_backend = get_option('sha1_unsafe_backend')
@@ -1343,7 +1344,7 @@ if https_backend == 'auto' and security_framework.found()
13431344
https_backend = 'CommonCrypto'
13441345
endif
13451346

1346-
openssl_required = 'openssl' in [https_backend, sha1_backend, sha1_unsafe_backend, sha256_backend]
1347+
openssl_required = 'openssl' in [csprng_backend, https_backend, sha1_backend, sha1_unsafe_backend, sha256_backend]
13471348
openssl = dependency('openssl', required: openssl_required, default_options: ['default_library=static'])
13481349
if https_backend == 'auto' and openssl.found()
13491350
https_backend = 'openssl'
@@ -1428,18 +1429,30 @@ else
14281429
error('Unhandled SHA256 backend ' + sha256_backend)
14291430
endif
14301431

1431-
if compiler.has_header_symbol('stdlib.h', 'arc4random_buf')
1432+
# Backends are ordered to reflect our preference for more secure and faster
1433+
# ones over the ones that are less so.
1434+
if csprng_backend in ['auto', 'arc4random'] and compiler.has_header_symbol('stdlib.h', 'arc4random_buf', required: csprng_backend == 'arc4random')
14321435
libgit_c_args += '-DHAVE_ARC4RANDOM'
1433-
elif compiler.has_header_symbol('bsd/stdlib.h', 'arc4random_buf')
1436+
csprng_backend = 'arc4random'
1437+
elif csprng_backend in ['auto', 'arc4random_bsd'] and compiler.has_header_symbol('bsd/stdlib.h', 'arc4random_buf', required: csprng_backend == 'arc4random_bsd')
14341438
libgit_c_args += '-DHAVE_ARC4RANDOM_BSD'
1435-
elif compiler.has_function('getrandom', prefix: '#include <sys/random.h>')
1439+
csprng_backend = 'arc4random_bsd'
1440+
elif csprng_backend in ['auto', 'getrandom'] and compiler.has_header_symbol('sys/random.h', 'getrandom', required: csprng_backend == 'getrandom')
14361441
libgit_c_args += '-DHAVE_GETRANDOM'
1437-
elif compiler.has_function('getentropy', prefix: '#include <unistd.h>')
1442+
csprng_backend = 'getrandom'
1443+
elif csprng_backend in ['auto', 'getentropy'] and compiler.has_header_symbol('unistd.h', 'getentropy', required: csprng_backend == 'getentropy')
14381444
libgit_c_args += '-DHAVE_GETENTROPY'
1439-
elif compiler.has_function('RtlGenRandom', prefix: '#include <windows.h>\n#include <ntsecapi.h>')
1445+
csprng_backend = 'getentropy'
1446+
elif csprng_backend in ['auto', 'rtlgenrandom'] and compiler.has_header_symbol('ntsecapi.h', 'RtlGenRandom', prefix: '#include <windows.h>', required: csprng_backend == 'rtlgenrandom')
14401447
libgit_c_args += '-DHAVE_RTLGENRANDOM'
1441-
elif openssl.found()
1448+
csprng_backend = 'rtlgenrandom'
1449+
elif csprng_backend in ['auto', 'openssl'] and openssl.found()
14421450
libgit_c_args += '-DHAVE_OPENSSL_CSPRNG'
1451+
csprng_backend = 'openssl'
1452+
elif csprng_backend in ['auto', 'urandom']
1453+
csprng_backend = 'urandom'
1454+
else
1455+
error('Unsupported CSPRNG backend: ' + csprng_backend)
14431456
endif
14441457

14451458
if get_option('runtime_prefix')
@@ -1977,6 +1990,7 @@ summary({
19771990
}, section: 'Auto-detected features')
19781991

19791992
summary({
1993+
'csprng': csprng_backend,
19801994
'https': https_backend,
19811995
'sha1': sha1_backend,
19821996
'sha1_unsafe': sha1_unsafe_backend,

meson_options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ option('regex', type: 'feature', value: 'auto',
4747
description: 'Use the system-provided regex library instead of the bundled one.')
4848

4949
# Backends.
50+
option('csprng_backend', type: 'combo', value: 'auto', choices: ['auto', 'arc4random', 'arc4random_bsd', 'getrandom', 'getentropy', 'rtlgenrandom', 'openssl', 'urandom'],
51+
description: 'The backend to use for generating cryptographically-secure pseudo-random numbers.')
5052
option('https_backend', type: 'combo', value: 'auto', choices: ['auto', 'openssl', 'CommonCrypto', 'none'],
5153
description: 'The HTTPS backend to use when connecting to remotes.')
5254
option('sha1_backend', type: 'combo', choices: ['openssl', 'block', 'sha1dc', 'CommonCrypto'], value: 'sha1dc',

0 commit comments

Comments
 (0)