Skip to content

Commit d6b5249

Browse files
committed
Refactor
1 parent 2b2faf5 commit d6b5249

File tree

7 files changed

+59
-63
lines changed

7 files changed

+59
-63
lines changed

crypto/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ add_library(
475475
rand_extra/deterministic.c
476476
rand_extra/entropy_passive.c
477477
rand_extra/forkunsafe.c
478+
rand_extra/getentropy.c
478479
rand_extra/rand_extra.c
479480
rand_extra/pq_custom_randombytes.c
480481
rand_extra/windows.c

crypto/fipsmodule/rand/getrandom_fillin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <openssl/base.h>
1919

2020

21-
#if defined(OPENSSL_LINUX)
21+
#if defined(OPENSSL_RAND_URANDOM)
2222

2323
#include <sys/syscall.h>
2424

crypto/fipsmodule/rand/internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
extern "C" {
2626
#endif
2727

28-
2928
#if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
3029
#define OPENSSL_RAND_DETERMINISTIC
3130
#elif defined(OPENSSL_WINDOWS)
3231
#define OPENSSL_RAND_WINDOWS
32+
#elif defined(OPENSSL_MACOS) || defined(OPENSSL_OPENBSD) || defined(OPENSSL_FREEBSD)
33+
#define OPENSSL_RAND_GETENTROPY
3334
#else
3435
#define OPENSSL_RAND_URANDOM
3536
#endif

crypto/fipsmodule/rand/urandom.c

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@
6969
#include <CommonCrypto/CommonRandom.h>
7070
#endif
7171

72-
#if defined(OPENSSL_FREEBSD)
73-
#define URANDOM_BLOCKS_FOR_ENTROPY
74-
#include <sys/param.h>
75-
#if __FreeBSD_version >= 1200000
76-
// getrandom is supported in FreeBSD 12 and up.
77-
#define FREEBSD_GETRANDOM
78-
#include <sys/random.h>
79-
#endif
80-
#endif
81-
82-
#if defined(OPENSSL_OPENBSD)
83-
#include <stdlib.h>
84-
#endif
85-
8672
#include <openssl/thread.h>
8773
#include <openssl/mem.h>
8874

@@ -250,22 +236,9 @@ static void init_once(void) {
250236
}
251237
#endif // USE_NR_getrandom
252238

253-
#if defined(OPENSSL_APPLE)
254-
// To get system randomness on MacOS and iOS we use |CCRandomGenerateBytes|
255-
// function provided by Apple rather than /dev/urandom or |getentropy|
256-
// function which is available on MacOS but not on iOS.
257-
return;
258-
#endif
259-
260-
#if defined(OPENSSL_OPENBSD)
261-
// To get system randomness on OpenBSD we use |arc4random_buf| function
262-
// which is recommended to use for C APIs rather then /dev/urandom.
263-
// See https://man.openbsd.org/arc4random.3
264-
return;
265-
#endif
266-
267-
#if defined(FREEBSD_GETRANDOM)
268-
*urandom_fd_bss_get() = kHaveGetrandom;
239+
#if defined(OPENSSL_IOS)
240+
// To get system randomness on iOS we use |CCRandomGenerateBytes| because
241+
// |getentroopy| is not available.
269242
return;
270243
#endif
271244

@@ -350,7 +323,7 @@ static void wait_for_entropy(void) {
350323
}
351324

352325
#if defined(BORINGSSL_FIPS) && !defined(URANDOM_BLOCKS_FOR_ENTROPY) && \
353-
!(defined(OPENSSL_APPLE) || defined(OPENSSL_OPENBSD)) // On MacOS, iOS, and OpenBSD we don't use /dev/urandom.
326+
!defined(OPENSSL_IOS) // On iOS we don't use /dev/urandom.
354327

355328
// In FIPS mode on platforms where urandom doesn't block at startup, we ensure
356329
// that the kernel has sufficient entropy before continuing. This is
@@ -399,30 +372,7 @@ static int fill_with_entropy(uint8_t *out, size_t len, int block, int seed) {
399372
}
400373
#endif
401374

402-
#if defined(OPENSSL_MACOS)
403-
// POSIX 2024 says unistd, but man page for macos says the former
404-
#include <sys/random.h>
405-
// To get system randomness on MacOS and iOS we use |CCRandomGenerateBytes|
406-
// rather than |getentropy| and /dev/urandom.
407-
// TODO at most can do 256 bytes
408-
if (getentropy(out, len) == 0) {
409-
return 1;
410-
} else {
411-
fprintf(stderr, "getentropy failed.\n");
412-
abort();
413-
}
414-
#endif
415-
416-
#if defined(OPENSSL_OPENBSD)
417-
#include <unistd.h>
418-
// POSIX 2024 says the latter, but man page for macos says the former
419-
// Return value is void, no error to check
420-
// TODO at most can do 256 bytes
421-
getentropy(out, len);
422-
return 1;
423-
#endif
424-
425-
#if defined(USE_NR_getrandom) || defined(FREEBSD_GETRANDOM)
375+
#if defined(USE_NR_getrandom)
426376
int getrandom_flags = 0;
427377
if (!block) {
428378
getrandom_flags |= GRND_NONBLOCK;
@@ -449,8 +399,6 @@ static int fill_with_entropy(uint8_t *out, size_t len, int block, int seed) {
449399
if (*urandom_fd_bss_get() == kHaveGetrandom) {
450400
#if defined(USE_NR_getrandom)
451401
r = boringssl_getrandom(out, len, getrandom_flags);
452-
#elif defined(FREEBSD_GETRANDOM)
453-
r = getrandom(out, len, getrandom_flags);
454402
#else // USE_NR_getrandom
455403
fprintf(stderr, "urandom fd corrupt.\n");
456404
abort();

crypto/fipsmodule/rand/urandom_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#include "internal.h"
2323
#include "../../ube/snapsafe_detect.h"
2424

25-
#if defined(OPENSSL_X86_64) && !defined(BORINGSSL_SHARED_LIBRARY) && \
25+
#if defined(OPENSSL_RAND_URANDOM) && defined(OPENSSL_X86_64) && \
26+
!defined(BORINGSSL_SHARED_LIBRARY) && \
2627
!defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && \
2728
defined(USE_NR_getrandom) && !defined(AWSLC_SNAPSAFE_TESTING)
2829

crypto/rand_extra/getentropy.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC
3+
4+
#if !defined(_DEFAULT_SOURCE)
5+
// Needed for getentropy on musl and glibc per man pages.
6+
#define _DEFAULT_SOURCE
7+
#endif
8+
9+
#include <openssl/rand.h>
10+
11+
#include "../fipsmodule/rand/internal.h"
12+
13+
#if defined(OPENSSL_RAND_GETENTROPY)
14+
15+
#include <limits.h>
16+
#include <stdio.h>
17+
#include <stdlib.h>
18+
#include <unistd.h>
19+
20+
#if defined(OPENSSL_MACOS)
21+
// MacOS does not declare getentropy in uinstd.h like other OS's.
22+
#include <sys/random.h>
23+
#endif
24+
25+
#if !defined(GETENTROPY_MAX)
26+
// Per POSIX 2024
27+
// https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html
28+
#define GETENTROPY_MAX 256
29+
#endif
30+
31+
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
32+
// getentropy max request size is GETENTROPY_MAX.
33+
while (requested > 0) {
34+
size_t request_chunk = (requested > GETENTROPY_MAX) ? GETENTROPY_MAX : requested;
35+
if (getentropy(out, request_chunk) != 0) {
36+
fprintf(stderr, "getentropy failed.\n");
37+
abort();
38+
}
39+
requested -= request_chunk;
40+
out += request_chunk;
41+
}
42+
}
43+
44+
void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) {
45+
CRYPTO_sysrand(out, requested);
46+
}
47+
48+
#endif

include/openssl/target.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@
8080

8181
#if defined(__APPLE__)
8282
#define OPENSSL_APPLE
83-
#endif
84-
85-
#if defined(__APPLE__)
8683
// Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX|
8784
// targets macOS specifically.
8885
#if defined(TARGET_OS_OSX) && TARGET_OS_OSX

0 commit comments

Comments
 (0)