Skip to content

Commit 65a3384

Browse files
Simplify urandom and move iOS source into its own file (#2379)
Refactor urandom.c to be more readable: Introduces explicit states Factor out code into self-contained functions Removes if/defs, in particular, drops any FIPS macro's Factors out iOS source CCRandomGenerateBytes() into its own file.
1 parent 51233d4 commit 65a3384

File tree

6 files changed

+310
-198
lines changed

6 files changed

+310
-198
lines changed

crypto/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ add_library(
468468
poly1305/poly1305_arm.c
469469
poly1305/poly1305_vec.c
470470
pool/pool.c
471+
rand_extra/ccrandomgeneratebytes.c
471472
rand_extra/deterministic.c
472473
rand_extra/getentropy.c
473474
rand_extra/rand_extra.c
@@ -818,6 +819,7 @@ if(BUILD_TESTING)
818819
pkcs8/pkcs12_test.cc
819820
poly1305/poly1305_test.cc
820821
pool/pool_test.cc
822+
rand_extra/ccrandomgeneratebytes_test.cc
821823
rand_extra/getentropy_test.cc
822824
rand_extra/rand_test.cc
823825
refcount_test.cc

crypto/fipsmodule/rand/rand_isolated_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ TEST_F(randIsolatedTest, RngKatWithUbe) {
783783
GTEST_SKIP() << "Test not supported when UBE is not supported";
784784
}
785785

786+
if (runtimeEmulationIsIntelSde() && addressSanitizerIsEnabled()) {
787+
GTEST_SKIP() << "Test not supported under Intel SDE + ASAN";
788+
}
789+
786790
auto runTest = [](RngKatTestUtils::TestType type) {
787791
RngKatTestUtils::RngKatTestEnv env(type);
788792
exit(env.runTest() ? 0 : 1);
@@ -803,6 +807,10 @@ TEST_F(randIsolatedTest, RngKatNoUbe) {
803807
GTEST_SKIP() << "Test not supported when UBE is supported";
804808
}
805809

810+
if (runtimeEmulationIsIntelSde() && addressSanitizerIsEnabled()) {
811+
GTEST_SKIP() << "Test not supported under Intel SDE + ASAN";
812+
}
813+
806814
auto runTest = [](RngKatTestUtils::TestType type) {
807815
RngKatTestUtils::RngKatTestEnv env(type);
808816
exit(env.runTest() ? 0 : 1);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC
3+
4+
#include <openssl/rand.h>
5+
6+
#include "internal.h"
7+
8+
#if defined(OPENSSL_RAND_CCRANDOMGENERATEBYTES)
9+
10+
#include <CommonCrypto/CommonRandom.h>
11+
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
15+
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
16+
17+
if (requested == 0) {
18+
return;
19+
}
20+
21+
// To get system randomness on iOS we use |CCRandomGenerateBytes|. On MacOS we
22+
// use |getentropy| but iOS doesn't expose that.
23+
if (CCRandomGenerateBytes(out, requested) != kCCSuccess) {
24+
fprintf(stderr, "CCRandomGenerateBytes failed.\n");
25+
abort();
26+
}
27+
}
28+
29+
void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) {
30+
CRYPTO_sysrand(out, requested);
31+
}
32+
33+
#endif
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#if !defined(_DEFAULT_SOURCE)
2+
#define _DEFAULT_SOURCE // Needed for getentropy on musl and glibc
3+
#endif
4+
5+
#include <openssl/rand.h>
6+
7+
#include "internal.h"
8+
9+
#if defined(OPENSSL_RAND_CCRANDOMGENERATEBYTES)
10+
11+
#include <gtest/gtest.h>
12+
13+
#include <openssl/span.h>
14+
15+
#include <CommonCrypto/CommonRandom.h>
16+
17+
#include "../test/test_util.h"
18+
19+
// This test is, strictly speaking, flaky, but we use large enough buffers
20+
// that the probability of failing when we should pass is negligible.
21+
22+
TEST(CCRandomGenerateBytesTest, NotObviouslyBroken) {
23+
static const uint8_t kZeros[256] = {0};
24+
25+
uint8_t buf1[256] = {0}, buf2[256] = {0}, buf3[256] = {0};
26+
27+
EXPECT_EQ(CCRandomGenerateBytes(buf1, sizeof(buf1)), kCCSuccess);
28+
EXPECT_EQ(CCRandomGenerateBytes(buf2, sizeof(buf2)), kCCSuccess);
29+
EXPECT_NE(Bytes(buf1), Bytes(buf2));
30+
EXPECT_NE(Bytes(buf1), Bytes(kZeros));
31+
EXPECT_NE(Bytes(buf2), Bytes(kZeros));
32+
33+
// Ensure that the implementation is not simply returning the memory unchanged.
34+
memcpy(buf3, buf1, sizeof(buf3));
35+
EXPECT_EQ(CCRandomGenerateBytes(buf1, sizeof(buf1)), kCCSuccess);
36+
EXPECT_NE(Bytes(buf1), Bytes(buf3));
37+
38+
// |CCRandomGenerateBytes| supports bigger inputs.
39+
uint8_t buf4[1024] = {0}, buf5[1024] = {0};
40+
EXPECT_NE(Bytes(buf4), Bytes(buf5));
41+
}
42+
43+
#endif // defined(OPENSSL_RAND_CCRANDOMGENERATEBYTES)

crypto/rand_extra/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define OPENSSL_RAND_WINDOWS
1111
#elif defined(OPENSSL_MACOS) || defined(OPENSSL_OPENBSD) || defined(OPENSSL_FREEBSD)
1212
#define OPENSSL_RAND_GETENTROPY
13+
#elif defined(OPENSSL_IOS)
14+
#define OPENSSL_RAND_CCRANDOMGENERATEBYTES
1315
#else
1416
#define OPENSSL_RAND_URANDOM
1517
#endif

0 commit comments

Comments
 (0)