From 8539b7cf6c5db86973f4f74f92762de9011b968b Mon Sep 17 00:00:00 2001 From: Henning Westerholt Date: Mon, 7 Oct 2019 10:38:36 +0200 Subject: [PATCH] tls: add cryptorand support for TLS module, add some more comments to existing code --- src/modules/tls/tls_rand.c | 59 +++++++++++++++++++++++++++++++++++++- src/modules/tls/tls_rand.h | 1 + 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/modules/tls/tls_rand.c b/src/modules/tls/tls_rand.c index d5c29b845f2..a149b07d334 100644 --- a/src/modules/tls/tls_rand.c +++ b/src/modules/tls/tls_rand.c @@ -16,7 +16,11 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - +/* + * OpenSSL docs: + * https://www.openssl.org/docs/man1.1.1/man7/RAND.html + * https://www.openssl.org/docs/man1.1.1/man3/RAND_set_rand_method.html + */ #include #include @@ -28,7 +32,12 @@ #include "../../core/dprint.h" #include "../../core/rand/kam_rand.h" #include "../../core/rand/fastrand.h" +#include "../../core/rand/fortuna/random.h" +/* + * Implementation for tests with system library PNRG, + * do not use this in production. + */ static int ksr_krand_bytes(unsigned char *outdata, int size) { int r; @@ -76,6 +85,11 @@ const RAND_METHOD *RAND_ksr_krand_method(void) return &_ksr_krand_method; } +/* + * Implementation for tests with fastrand implementation, + * better as system library but still not secure enough. + * Do not use this in production.y + */ static int ksr_fastrand_bytes(unsigned char *outdata, int size) { int r; @@ -123,4 +137,47 @@ const RAND_METHOD *RAND_ksr_fastrand_method(void) return &_ksr_fastrand_method; } +/* + * Implementation with Fortuna cryptographic PRNG. + * We are not strictly implementing the OpenSSL API here - we will + * not return an error if the PRNG has not been seeded with enough + * randomness to ensure an unpredictable byte sequence. + */ +static int ksr_cryptorand_bytes(unsigned char *outdata, int size) +{ + if (size < 0) { + return 0; + } else if (size == 0) { + return 1; + } + + sr_get_pseudo_random_bytes(outdata, size); + return 1; +} + +static int ksr_cryptorand_status(void) +{ + return 1; +} + +/* + * We don't have a dedicated function for pseudo-random + * bytes, just use the secure version as well for it. + */ +const RAND_METHOD _ksr_cryptorand_method = { + NULL, + ksr_cryptorand_bytes, + NULL, + NULL, + ksr_cryptorand_bytes, + ksr_cryptorand_status +}; + +const RAND_METHOD *RAND_ksr_cryptorand_method(void) +{ + return &_ksr_cryptorand_method; +} + + + #endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */ diff --git a/src/modules/tls/tls_rand.h b/src/modules/tls/tls_rand.h index d1a3f0d37f8..c73d36b8d94 100644 --- a/src/modules/tls/tls_rand.h +++ b/src/modules/tls/tls_rand.h @@ -27,6 +27,7 @@ const RAND_METHOD *RAND_ksr_krand_method(void); const RAND_METHOD *RAND_ksr_fastrand_method(void); +const RAND_METHOD *RAND_ksr_cryptorand_method(void); #endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */ #endif