-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use getentropy for OpenBSD, FreeBSD, and MacOS (#2099)
Use getentropy for some operating systems. Namely, Apple MacOS, OpenBSD, and FreeBSD. getentropy is closer to the noise sources.
- Loading branch information
1 parent
19e9b93
commit 3da1ba6
Showing
8 changed files
with
68 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR ISC | ||
|
||
#if !defined(_DEFAULT_SOURCE) | ||
// Needed for getentropy on musl and glibc per man pages. | ||
#define _DEFAULT_SOURCE | ||
#endif | ||
|
||
#include <openssl/rand.h> | ||
|
||
#include "../fipsmodule/rand/internal.h" | ||
|
||
#if defined(OPENSSL_RAND_GETENTROPY) | ||
|
||
#include <limits.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#if defined(OPENSSL_MACOS) | ||
// MacOS does not declare getentropy in uinstd.h like other OS's. | ||
#include <sys/random.h> | ||
#endif | ||
|
||
#if !defined(GETENTROPY_MAX) | ||
// Per POSIX 2024 | ||
// https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html | ||
#define GETENTROPY_MAX 256 | ||
#endif | ||
|
||
void CRYPTO_sysrand(uint8_t *out, size_t requested) { | ||
// getentropy max request size is GETENTROPY_MAX. | ||
while (requested > 0) { | ||
size_t request_chunk = (requested > GETENTROPY_MAX) ? GETENTROPY_MAX : requested; | ||
if (getentropy(out, request_chunk) != 0) { | ||
fprintf(stderr, "getentropy failed.\n"); | ||
abort(); | ||
} | ||
requested -= request_chunk; | ||
out += request_chunk; | ||
} | ||
} | ||
|
||
void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) { | ||
CRYPTO_sysrand(out, requested); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters