From 8be2b8385cd1cf1ccd53d941087101f4547670ff Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Tue, 20 Jun 2023 11:17:00 +0200 Subject: [PATCH] Port of the openss-poratble arc4random code to our build environment --- ext/arc4random/.gitignore | 5 +++++ ext/arc4random/Makefile.am | 6 ++++++ ext/arc4random/arc4random.c | 3 +++ ext/arc4random/arc4random.h | 4 ++-- ext/arc4random/arc4random.hh | 9 +++++++++ ext/arc4random/includes.h | 20 ++++++++++++++++++++ ext/arc4random/log.h | 1 + 7 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 ext/arc4random/.gitignore create mode 100644 ext/arc4random/Makefile.am create mode 100644 ext/arc4random/arc4random.hh create mode 100644 ext/arc4random/includes.h create mode 100644 ext/arc4random/log.h diff --git a/ext/arc4random/.gitignore b/ext/arc4random/.gitignore new file mode 100644 index 0000000000..24ad051c6e --- /dev/null +++ b/ext/arc4random/.gitignore @@ -0,0 +1,5 @@ +*.la +*.lo +*.o +Makefile +Makefile.in diff --git a/ext/arc4random/Makefile.am b/ext/arc4random/Makefile.am new file mode 100644 index 0000000000..ee46ad2a57 --- /dev/null +++ b/ext/arc4random/Makefile.am @@ -0,0 +1,6 @@ +noinst_LTLIBRARIES = libarc4random.la +libarc4random_la_SOURCES = arc4random.c arc4random.h \ + arc4random_uniform.c \ + bsd-getentropy.c \ + includes.h \ + log.h diff --git a/ext/arc4random/arc4random.c b/ext/arc4random/arc4random.c index ffd33734db..afcaa64720 100644 --- a/ext/arc4random/arc4random.c +++ b/ext/arc4random/arc4random.c @@ -205,6 +205,9 @@ _rs_random_u32(uint32_t *val) rs->rs_have -= sizeof(*val); } +#include +static pthread_mutex_t arc4mutex = PTHREAD_MUTEX_INITIALIZER; + uint32_t arc4random(void) { diff --git a/ext/arc4random/arc4random.h b/ext/arc4random/arc4random.h index 5af3a4492a..402b26312a 100644 --- a/ext/arc4random/arc4random.h +++ b/ext/arc4random/arc4random.h @@ -28,8 +28,8 @@ #include /* OpenSSH isn't multithreaded */ -#define _ARC4_LOCK() -#define _ARC4_UNLOCK() +#define _ARC4_LOCK() pthread_mutex_lock(&arc4mutex); +#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4mutex); #define _ARC4_ATFORK(f) static inline void diff --git a/ext/arc4random/arc4random.hh b/ext/arc4random/arc4random.hh new file mode 100644 index 0000000000..72449fb248 --- /dev/null +++ b/ext/arc4random/arc4random.hh @@ -0,0 +1,9 @@ +#pragma once + +#include + +extern "C" { +uint32_t arc4random(void); +void arc4random_buf(void *buf, size_t nbytes); +uint32_t arc4random_uniform(uint32_t upper_bound); +} diff --git a/ext/arc4random/includes.h b/ext/arc4random/includes.h new file mode 100644 index 0000000000..8914c12aa9 --- /dev/null +++ b/ext/arc4random/includes.h @@ -0,0 +1,20 @@ +#include "config.h" + +#ifdef HAVE_GETRANDOM +#include +#endif + +#include +#include +#include + +#include +#include + +#define seed_from_prngd(a, b) -1 + +uint32_t arc4random(void); +void arc4random_buf(void *buf, size_t nbytes); +uint32_t arc4random_uniform(uint32_t upper_bound); + +#define DEF_WEAK(x) diff --git a/ext/arc4random/log.h b/ext/arc4random/log.h new file mode 100644 index 0000000000..51d7af2799 --- /dev/null +++ b/ext/arc4random/log.h @@ -0,0 +1 @@ +#define fatal(...) do { fprintf(stderr, __VA_ARGS__); abort(); } while (0) -- 2.47.2