Test the PSA RNG after fork()

Assert that two forked children have distinct RNG states, and also that the
state is distinct from their parent.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine
2026-01-16 19:05:52 +01:00
parent 0764c9348a
commit ce8a71c071
2 changed files with 105 additions and 0 deletions

View File

@@ -104,6 +104,18 @@ PSA external RNG failure: RSA PKCS#1v1.5 (software implementation)
depends_on:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_IMPORT:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_EXPORT:MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN
external_rng_failure_sign:PSA_KEY_TYPE_RSA_KEY_PAIR:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_ALG_RSA_PKCS1V15_SIGN_RAW:32
PSA RNG after fork: parent gets random never
psa_rng_fork:-1
PSA RNG after fork: parent gets random before
psa_rng_fork:0
PSA RNG after fork: parent gets random between
psa_rng_fork:1
PSA RNG after fork: parent gets random after
psa_rng_fork:2
PSA validate entropy injection: good, minimum size
validate_entropy_seed_injection:MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE:PSA_SUCCESS:MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE:PSA_ERROR_NOT_PERMITTED

View File

@@ -2,6 +2,10 @@
#include <stdint.h>
#include <string.h>
#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE)
#include <test/fork_helpers.h>
#endif
#include <psa/crypto.h>
/* Some tests in this module configure entropy sources. */
@@ -149,6 +153,20 @@ exit:
}
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
#if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) && \
!defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
static void child_psa_get_random(void *param,
uint8_t *output, size_t output_size,
size_t *output_length)
{
(void) param;
PSA_ASSERT(psa_generate_random(output, output_size));
*output_length = output_size;
exit:
;
}
#endif /* MBEDTLS_PLATFORM_IS_UNIXLIKE && !MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
/* Calculating the minimum allowed entropy size in bytes */
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, \
MBEDTLS_ENTROPY_BLOCK_SIZE)
@@ -662,6 +680,81 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE depends_on:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG:MBEDTLS_PLATFORM_IS_UNIXLIKE */
/* Test that if a program calls fork(), the PSA RNG returns different byte
* sequences in each child process, and that they're different from the
* parent process.
*
* The argument parent_when controls when the parent calls
* psa_generate_random(): -1 = never, 0 = before forking, >0 = after forking
* that many children.
*
* Note that passing tests don't mean that everything is fine, they only
* mean that things are not too obviously broken. It's possible to badly
* design the RNG so that, for example, different child processes will
* have the same RNG output sequence but at an offset, or so that a child
* process's RNG is seeded from RNG output of the parent (making it
* predictable if an adversary happens to be able to get the right chunk
* of RNG output from th parent), or different sequencing of forking
* grand^n-children ends up with them having identical RNG output sequences.
* These bad designs are practically impossible to detect through testing,
* and must be excluded by human reasoning on the RNG design.
*/
void psa_rng_fork(int parent_when)
{
struct {
/* We read 16 bytes from the RNG. This is large enough so that the
* probability of a coincidence is negligible, and small enough that
* the RNG won't spontaneously decide reseed to unless it has
* prediction resistance. */
unsigned char rng_output[16];
} child[2], parent;
memset(child, 0, sizeof(child));
memset(&parent, 0, sizeof(parent));
PSA_INIT();
/* Create some child processes, have them generate random data
* and report that data back to the original process. */
for (size_t i = 0; i < ARRAY_LENGTH(child); i++) {
mbedtls_test_set_step(i);
if ((size_t) parent_when == i) {
PSA_ASSERT(psa_generate_random(parent.rng_output,
sizeof(parent.rng_output)));
}
size_t length;
TEST_EQUAL(mbedtls_test_fork_run_child(
child_psa_get_random, NULL,
child[i].rng_output, sizeof(child[i].rng_output),
&length), 0);
TEST_EQUAL(length, sizeof(child[i].rng_output));
}
if (parent_when == ARRAY_LENGTH(child)) {
PSA_ASSERT(psa_generate_random(parent.rng_output,
sizeof(parent.rng_output)));
}
/* Did the children have different RNG states? */
TEST_ASSERT(memcmp(child[0].rng_output,
child[1].rng_output,
sizeof(parent.rng_output)) != 0);
/* If parent_when >= 0: did the children have different RNG states
* from the parent?
* If parent_when < 0: did the children get nonzero RNG output?
*/
for (size_t i = 0; i < ARRAY_LENGTH(child); i++) {
mbedtls_test_set_step(i);
TEST_ASSERT(memcmp(parent.rng_output,
child[i].rng_output,
sizeof(parent.rng_output)) != 0);
}
exit:
PSA_DONE();
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_PSA_INJECT_ENTROPY */
void validate_entropy_seed_injection(int seed_length_a,
int expected_status_a,