| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#if !defined(S2N_CLOEXEC_SUPPORTED) && defined(S2N_CLOEXEC_XOPEN_SUPPORTED) && !defined(_XOPEN_SOURCE) |
| 42 |
#define _XOPEN_SOURCE 700 |
| 43 |
#include <fcntl.h> |
| 44 |
#undef _XOPEN_SOURCE |
| 45 |
#else |
| 46 |
#include <fcntl.h> |
| 47 |
#endif |
| 48 |
#include <errno.h> |
| 49 |
#include <limits.h> |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#include <openssl/rand.h> |
| 54 |
#include <stdint.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <sys/stat.h> |
| 57 |
#include <sys/types.h> |
| 58 |
#include <time.h> |
| 59 |
#include <unistd.h> |
| 60 |
|
| 61 |
#include "api/s2n.h" |
| 62 |
#include "crypto/s2n_fips.h" |
| 63 |
#include "crypto/s2n_libcrypto.h" |
| 64 |
#include "error/s2n_errno.h" |
| 65 |
#include "s2n_io.h" |
| 66 |
#include "utils/s2n_init.h" |
| 67 |
#include "utils/s2n_mem.h" |
| 68 |
#include "utils/s2n_random.h" |
| 69 |
#include "utils/s2n_result.h" |
| 70 |
#include "utils/s2n_safety.h" |
| 71 |
|
| 72 |
#if defined(O_CLOEXEC) |
| 73 |
#define ENTROPY_FLAGS O_RDONLY | O_CLOEXEC |
| 74 |
#else |
| 75 |
#define ENTROPY_FLAGS O_RDONLY |
| 76 |
#endif |
| 77 |
|
| 78 |
|
| 79 |
#define ONE_S INT64_C(1000000000) |
| 80 |
|
| 81 |
|
| 82 |
#define UNINITIALIZED_ENTROPY_FD -1 |
| 83 |
|
| 84 |
static struct s2n_rand_device s2n_dev_urandom = { |
| 85 |
.source = "/dev/urandom", |
| 86 |
.fd = UNINITIALIZED_ENTROPY_FD, |
| 87 |
}; |
| 88 |
|
| 89 |
static int s2n_rand_init_cb_impl(void); |
| 90 |
static int s2n_rand_cleanup_cb_impl(void); |
| 91 |
static int s2n_rand_get_entropy_from_urandom(void *ptr, uint32_t size); |
| 92 |
|
| 93 |
static int s2n_rand_entropy_fd_close_ptr(int *fd) |
| 94 |
{ |
| 95 |
if (fd && *fd != UNINITIALIZED_ENTROPY_FD) { |
| 96 |
close(*fd); |
| 97 |
} |
| 98 |
return S2N_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
bool s2n_use_libcrypto_rand(void) |
| 110 |
{ |
| 111 |
#if defined(S2N_LIBCRYPTO_SUPPORTS_PRIVATE_RAND) || defined(S2N_LIBCRYPTO_SUPPORTS_PUBLIC_RAND) |
| 112 |
return true; |
| 113 |
#elif defined(OPENSSL_IS_AWSLC) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
return true; |
| 119 |
#else |
| 120 |
return false; |
| 121 |
#endif |
| 122 |
} |
| 123 |
|
| 124 |
static S2N_RESULT s2n_get_libcrypto_private_random_data(struct s2n_blob *out_blob) |
| 125 |
{ |
| 126 |
RESULT_GUARD_PTR(out_blob); |
| 127 |
RESULT_ENSURE_REF(out_blob->data); |
| 128 |
#if S2N_LIBCRYPTO_SUPPORTS_PRIVATE_RAND |
| 129 |
RESULT_GUARD_OSSL(RAND_priv_bytes(out_blob->data, out_blob->size), S2N_ERR_RANDOM); |
| 130 |
#else |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
RESULT_GUARD_OSSL(RAND_bytes(out_blob->data, out_blob->size), S2N_ERR_RANDOM); |
| 137 |
#endif |
| 138 |
return S2N_RESULT_OK; |
| 139 |
} |
| 140 |
|
| 141 |
static S2N_RESULT s2n_get_libcrypto_public_random_data(struct s2n_blob *out_blob) |
| 142 |
{ |
| 143 |
RESULT_GUARD_PTR(out_blob); |
| 144 |
RESULT_ENSURE_REF(out_blob->data); |
| 145 |
#if S2N_LIBCRYPTO_SUPPORTS_PUBLIC_RAND |
| 146 |
RESULT_GUARD_OSSL(RAND_public_bytes(out_blob->data, out_blob->size), S2N_ERR_RANDOM); |
| 147 |
#else |
| 148 |
RESULT_GUARD_OSSL(RAND_bytes(out_blob->data, out_blob->size), S2N_ERR_RANDOM); |
| 149 |
#endif |
| 150 |
return S2N_RESULT_OK; |
| 151 |
} |
| 152 |
|
| 153 |
static S2N_RESULT s2n_get_system_random_data(struct s2n_blob *blob) |
| 154 |
{ |
| 155 |
RESULT_GUARD_PTR(blob); |
| 156 |
RESULT_GUARD_PTR(blob->data); |
| 157 |
|
| 158 |
|
| 159 |
RESULT_GUARD_POSIX(s2n_rand_get_entropy_from_urandom(blob->data, blob->size)); |
| 160 |
|
| 161 |
return S2N_RESULT_OK; |
| 162 |
} |
| 163 |
|
| 164 |
S2N_RESULT s2n_get_public_random_data(struct s2n_blob *blob) |
| 165 |
{ |
| 166 |
if (s2n_use_libcrypto_rand()) { |
| 167 |
RESULT_GUARD(s2n_get_libcrypto_public_random_data(blob)); |
| 168 |
} else { |
| 169 |
RESULT_GUARD(s2n_get_system_random_data(blob)); |
| 170 |
} |
| 171 |
return S2N_RESULT_OK; |
| 172 |
} |
| 173 |
|
| 174 |
S2N_RESULT s2n_get_private_random_data(struct s2n_blob *blob) |
| 175 |
{ |
| 176 |
if (s2n_use_libcrypto_rand()) { |
| 177 |
RESULT_GUARD(s2n_get_libcrypto_private_random_data(blob)); |
| 178 |
} else { |
| 179 |
RESULT_GUARD(s2n_get_system_random_data(blob)); |
| 180 |
} |
| 181 |
return S2N_RESULT_OK; |
| 182 |
} |
| 183 |
|
| 184 |
S2N_RESULT s2n_rand_get_urandom_for_test(struct s2n_rand_device **device) |
| 185 |
{ |
| 186 |
RESULT_ENSURE_REF(device); |
| 187 |
RESULT_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST); |
| 188 |
*device = &s2n_dev_urandom; |
| 189 |
return S2N_RESULT_OK; |
| 190 |
} |
| 191 |
|
| 192 |
static S2N_RESULT s2n_rand_device_open(struct s2n_rand_device *device) |
| 193 |
{ |
| 194 |
RESULT_ENSURE_REF(device); |
| 195 |
RESULT_ENSURE_REF(device->source); |
| 196 |
|
| 197 |
DEFER_CLEANUP(int fd = -1, s2n_rand_entropy_fd_close_ptr); |
| 198 |
S2N_IO_RETRY_EINTR(fd, open(device->source, ENTROPY_FLAGS)); |
| 199 |
RESULT_ENSURE(fd >= 0, S2N_ERR_OPEN_RANDOM); |
| 200 |
|
| 201 |
struct stat st = { 0 }; |
| 202 |
RESULT_ENSURE(fstat(fd, &st) == 0, S2N_ERR_OPEN_RANDOM); |
| 203 |
device->dev = st.st_dev; |
| 204 |
device->ino = st.st_ino; |
| 205 |
device->mode = st.st_mode; |
| 206 |
device->rdev = st.st_rdev; |
| 207 |
|
| 208 |
device->fd = fd; |
| 209 |
|
| 210 |
|
| 211 |
fd = UNINITIALIZED_ENTROPY_FD; |
| 212 |
|
| 213 |
return S2N_RESULT_OK; |
| 214 |
} |
| 215 |
|
| 216 |
S2N_RESULT s2n_rand_device_validate(struct s2n_rand_device *device) |
| 217 |
{ |
| 218 |
RESULT_ENSURE_REF(device); |
| 219 |
RESULT_ENSURE_NE(device->fd, UNINITIALIZED_ENTROPY_FD); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
struct stat st = { 0 }; |
| 226 |
RESULT_ENSURE(fstat(device->fd, &st) == 0, S2N_ERR_OPEN_RANDOM); |
| 227 |
RESULT_ENSURE_EQ(device->dev, st.st_dev); |
| 228 |
RESULT_ENSURE_EQ(device->ino, st.st_ino); |
| 229 |
RESULT_ENSURE_EQ(device->rdev, st.st_rdev); |
| 230 |
|
| 231 |
|
| 232 |
mode_t permission_mask = ~(S_IRWXU | S_IRWXG | S_IRWXO); |
| 233 |
RESULT_ENSURE_EQ((device->mode ^ st.st_mode) & permission_mask, 0); |
| 234 |
|
| 235 |
return S2N_RESULT_OK; |
| 236 |
} |
| 237 |
|
| 238 |
static int s2n_rand_get_entropy_from_urandom(void *ptr, uint32_t size) |
| 239 |
{ |
| 240 |
POSIX_ENSURE_REF(ptr); |
| 241 |
POSIX_ENSURE(s2n_dev_urandom.fd != UNINITIALIZED_ENTROPY_FD, S2N_ERR_NOT_INITIALIZED); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (s2n_result_is_error(s2n_rand_device_validate(&s2n_dev_urandom))) { |
| 252 |
POSIX_GUARD_RESULT(s2n_rand_device_open(&s2n_dev_urandom)); |
| 253 |
} |
| 254 |
|
| 255 |
uint8_t *data = ptr; |
| 256 |
uint32_t n = size; |
| 257 |
struct timespec sleep_time = { .tv_sec = 0, .tv_nsec = 0 }; |
| 258 |
long backoff = 1; |
| 259 |
|
| 260 |
while (n) { |
| 261 |
errno = 0; |
| 262 |
int r = read(s2n_dev_urandom.fd, data, n); |
| 263 |
if (r <= 0) { |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if (errno != EINTR) { |
| 283 |
backoff = S2N_MIN(backoff * 10, ONE_S - 1); |
| 284 |
sleep_time.tv_nsec = backoff; |
| 285 |
do { |
| 286 |
r = nanosleep(&sleep_time, &sleep_time); |
| 287 |
} while (r != 0); |
| 288 |
} |
| 289 |
|
| 290 |
continue; |
| 291 |
} |
| 292 |
|
| 293 |
data += r; |
| 294 |
n -= r; |
| 295 |
} |
| 296 |
|
| 297 |
return S2N_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
S2N_RESULT s2n_public_random(int64_t bound, uint64_t *output) |
| 304 |
{ |
| 305 |
uint64_t r = 0; |
| 306 |
|
| 307 |
RESULT_ENSURE_GT(bound, 0); |
| 308 |
|
| 309 |
while (1) { |
| 310 |
struct s2n_blob blob = { 0 }; |
| 311 |
RESULT_GUARD_POSIX(s2n_blob_init(&blob, (void *) &r, sizeof(r))); |
| 312 |
RESULT_GUARD(s2n_get_public_random_data(&blob)); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if (r < (UINT64_MAX - (UINT64_MAX % bound))) { |
| 328 |
*output = r % bound; |
| 329 |
return S2N_RESULT_OK; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
static int s2n_rand_init_cb_impl(void) |
| 335 |
{ |
| 336 |
POSIX_GUARD_RESULT(s2n_rand_device_open(&s2n_dev_urandom)); |
| 337 |
|
| 338 |
return S2N_SUCCESS; |
| 339 |
} |
| 340 |
|
| 341 |
S2N_RESULT s2n_rand_init(void) |
| 342 |
{ |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if (!s2n_use_libcrypto_rand()) { |
| 347 |
RESULT_ENSURE(s2n_rand_init_cb_impl() >= S2N_SUCCESS, S2N_ERR_CANCELLED); |
| 348 |
} |
| 349 |
|
| 350 |
return S2N_RESULT_OK; |
| 351 |
} |
| 352 |
|
| 353 |
static int s2n_rand_cleanup_cb_impl(void) |
| 354 |
{ |
| 355 |
if (s2n_dev_urandom.fd == UNINITIALIZED_ENTROPY_FD) { |
| 356 |
return S2N_SUCCESS; |
| 357 |
} |
| 358 |
|
| 359 |
if (s2n_result_is_ok(s2n_rand_device_validate(&s2n_dev_urandom))) { |
| 360 |
POSIX_GUARD(close(s2n_dev_urandom.fd)); |
| 361 |
} |
| 362 |
s2n_dev_urandom.fd = UNINITIALIZED_ENTROPY_FD; |
| 363 |
|
| 364 |
return S2N_SUCCESS; |
| 365 |
} |
| 366 |
|
| 367 |
S2N_RESULT s2n_rand_cleanup(void) |
| 368 |
{ |
| 369 |
if (!s2n_use_libcrypto_rand()) { |
| 370 |
RESULT_ENSURE(s2n_rand_cleanup_cb_impl() >= S2N_SUCCESS, S2N_ERR_CANCELLED); |
| 371 |
} |
| 372 |
|
| 373 |
return S2N_RESULT_OK; |
| 374 |
} |
| 375 |
|
| 376 |
int s2n_rand_set_callbacks(s2n_rand_init_callback rand_init_callback, |
| 377 |
s2n_rand_cleanup_callback rand_cleanup_callback, |
| 378 |
s2n_rand_seed_callback rand_seed_callback, |
| 379 |
s2n_rand_mix_callback rand_mix_callback) |
| 380 |
{ |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
(void) rand_init_callback; |
| 386 |
(void) rand_cleanup_callback; |
| 387 |
(void) rand_seed_callback; |
| 388 |
(void) rand_mix_callback; |
| 389 |
return S2N_SUCCESS; |
| 390 |
} |