| 1 |
#include "random.h" |
| 2 |
#include "config.h" |
| 3 |
#include "log-private.h" |
| 4 |
|
| 5 |
#if HAVE_MBEDTLS |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <mbedtls/ctr_drbg.h> |
| 9 |
#include <mbedtls/entropy.h> |
| 10 |
|
| 11 |
#include "pthread-shim.h" |
| 12 |
#include "vcs_version.h" |
| 13 |
|
| 14 |
#ifdef _WIN32 |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <windows.h> |
| 21 |
#include <bcrypt.h> |
| 22 |
#ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG |
| 23 |
#define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002 |
| 24 |
#endif |
| 25 |
|
| 26 |
static int _librist_bcrypt_f_rng(void *data, unsigned char *output, size_t len) |
| 27 |
{ |
| 28 |
(void) data; |
| 29 |
NTSTATUS s = BCryptGenRandom(NULL, output, (ULONG) len, |
| 30 |
BCRYPT_USE_SYSTEM_PREFERRED_RNG); |
| 31 |
if (s != 0) |
| 32 |
return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; |
| 33 |
return 0; |
| 34 |
} |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifndef _WIN32 |
| 38 |
static mbedtls_entropy_context entropy_ctx; |
| 39 |
#endif |
| 40 |
static mbedtls_ctr_drbg_context ctr_drbg_ctx; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static int ctr_drbg_seed_ret = MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED; |
| 45 |
|
| 46 |
#if !defined(_WIN32) || HAVE_PTHREADS |
| 47 |
|
| 48 |
#if defined(__GNU__) |
| 49 |
static pthread_once_t entropy_init_once = {__PTHREAD_ONCE_INIT}; |
| 50 |
#else |
| 51 |
static pthread_once_t entropy_init_once = PTHREAD_ONCE_INIT; |
| 52 |
#endif |
| 53 |
#endif |
| 54 |
#if defined(_WIN32) && !HAVE_PTHREADS |
| 55 |
static INIT_ONCE entropy_init_once = INIT_ONCE_STATIC_INIT; |
| 56 |
#endif |
| 57 |
|
| 58 |
|
| 59 |
#if HAVE_PTHREADS |
| 60 |
static void _librist_crypto_random_init_func(void) |
| 61 |
#else |
| 62 |
static BOOL WINAPI librist_crypto_srp_init_random_func(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) |
| 63 |
#endif |
| 64 |
{ |
| 65 |
#if HAVE_MBEDTLS |
| 66 |
mbedtls_ctr_drbg_init(&ctr_drbg_ctx); |
| 67 |
|
| 68 |
const char user_custom[] = "libRIST librist_crypto_random_init_func "LIBRIST_VERSION; |
| 69 |
#ifdef _WIN32 |
| 70 |
ctr_drbg_seed_ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, _librist_bcrypt_f_rng, NULL, |
| 71 |
(const unsigned char *)user_custom, sizeof(user_custom)); |
| 72 |
#else |
| 73 |
mbedtls_entropy_init(&entropy_ctx); |
| 74 |
ctr_drbg_seed_ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx, |
| 75 |
(const unsigned char *)user_custom, sizeof(user_custom)); |
| 76 |
#endif |
| 77 |
if (ctr_drbg_seed_ret != 0) { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
rist_log_priv3(RIST_LOG_ERROR, |
| 83 |
"CSPRNG seeding failed (mbedtls_ctr_drbg_seed = -0x%04x); " |
| 84 |
"all crypto-random calls will fail until the entropy source is fixed\n", |
| 85 |
(unsigned)-ctr_drbg_seed_ret); |
| 86 |
} |
| 87 |
#endif |
| 88 |
#if !HAVE_PTHREADS |
| 89 |
return 1; |
| 90 |
#endif |
| 91 |
} |
| 92 |
|
| 93 |
static void _librist_crypto_random_init(void) { |
| 94 |
#if HAVE_PTHREADS |
| 95 |
pthread_once(&entropy_init_once, _librist_crypto_random_init_func); |
| 96 |
#else |
| 97 |
InitOnceExecuteOnce(&entropy_init_once, librist_crypto_srp_init_random_func, NULL, NULL); |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
#elif HAVE_NETTLE |
| 102 |
#include <gnutls/crypto.h> |
| 103 |
static void _librist_crypto_random_init(void) { |
| 104 |
return; |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
int _librist_crypto_ramdom_get_bytes(uint8_t buf[], size_t buflen) { |
| 109 |
#if HAVE_MBEDTLS || HAVE_NETTLE |
| 110 |
_librist_crypto_random_init(); |
| 111 |
int ret; |
| 112 |
#if HAVE_MBEDTLS |
| 113 |
if (ctr_drbg_seed_ret != 0) |
| 114 |
return ctr_drbg_seed_ret; |
| 115 |
ret = mbedtls_ctr_drbg_random(&ctr_drbg_ctx, buf, buflen); |
| 116 |
#elif HAVE_NETTLE |
| 117 |
int i=0; |
| 118 |
do { |
| 119 |
ret = gnutls_rnd(GNUTLS_RND_NONCE, buf, buflen); |
| 120 |
i++; |
| 121 |
} while (ret != 0 && i < 10); |
| 122 |
if (ret != 0) |
| 123 |
rist_log_priv3(RIST_LOG_ERROR, |
| 124 |
"CSPRNG (gnutls_rnd) failed %d times in a row, returning %d\n", |
| 125 |
i, ret); |
| 126 |
#endif |
| 127 |
return ret; |
| 128 |
#else |
| 129 |
(void)buf; (void)buflen; |
| 130 |
return -1; |
| 131 |
#endif |
| 132 |
} |
| 133 |
|
| 134 |
int _librist_crypto_random_get_string(char buf[], size_t len) { |
| 135 |
|
| 136 |
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?"; |
| 137 |
const size_t charset_n = sizeof(charset) - 1; |
| 138 |
uint8_t rand_buf[128]; |
| 139 |
if (len > sizeof(rand_buf)) |
| 140 |
return -1; |
| 141 |
int ret = _librist_crypto_ramdom_get_bytes(rand_buf, len); |
| 142 |
if (ret != 0) |
| 143 |
return ret; |
| 144 |
for (size_t i = 0; i < len; i++) |
| 145 |
buf[i] = charset[rand_buf[i] % charset_n]; |
| 146 |
return 0; |
| 147 |
} |