Building OpenSSL with Determinisic Randomness

security

TLS, like most cryptographic protocols, depend on random numbers to generate keying material. These numbers should come from a trusted and truly random source. While this is necessary for production use, for testing purposes it is beneficial to use a pseudorandom number generator (PRNG). By seeding the generator with a static and not-random number, each execution of the protocol yields the same bytes which are sent over the network.

Furthermore, each execution gives the exactly same coverage, if the edges in the implementation of the protocol depend on random values. This is helpful for fuzzing, such that each run yields deterministic results.

OpenSSL internally uses an interface which is called rand.h. This API allows setting custom methods for generating random number.

I use this implementation of an OpenSSL random method which is based on the rand and srand functions of the C standard library.

 1// based on https://stackoverflow.com/a/7510354
 2#include <openssl/rand.h>
 3#include <stdlib.h>
 4
 5// Seed the RNG. srand() takes an unsigned int, so we just use the first
 6// sizeof(unsigned int) bytes in the buffer to seed the RNG.
 7static int stdlib_rand_seed(const void *buf, int num)
 8{
 9    if (num < 1)
10    {
11        srand(0);
12        return 0;
13    }
14    srand(*((unsigned int *) buf));
15    return 1;
16}
17
18// Fill the buffer with random bytes.  For each byte in the buffer, we generate
19// a random number and clamp it to the range of a byte, 0-255.
20static int stdlib_rand_bytes(unsigned char *buf, int num)
21{
22    for (int index = 0; index < num; ++index)
23    {
24        buf[index] = rand() % 256;
25    }
26    return 1;
27}
28
29static void stdlib_rand_cleanup() {}
30static int stdlib_rand_add(const void *buf, int num, double add_entropy)
31{
32    return 1;
33}
34static int stdlib_rand_status()
35{
36    return 1;
37}
38
39RAND_METHOD stdlib_rand_meth = { stdlib_rand_seed,
40                                 stdlib_rand_bytes,
41                                 stdlib_rand_cleanup,
42                                 stdlib_rand_add,
43                                 stdlib_rand_bytes,
44                                 stdlib_rand_status
45};

By utilizing this random number generator we are able to generate deterministic random numbers. To use this library from Rust we create a public library which sets the above stdlib_rand_method:

1void make_openssl_deterministic()
2{
3    RAND_set_rand_method(&stdlib_rand_meth);
4}

I’m statically linking OpenSSL against my fuzzer which is called tlspuffin. That means by utilizing extern "C", we can easily call into C code. We can call make_openssl_deterministic from Rust and directly seed our random number generator with the number 42:

 1extern "C" {
 2    pub fn make_openssl_deterministic();
 3    pub fn RAND_seed(buf: *mut u8, num: c_int);
 4}
 5
 6pub fn make_deterministic() {
 7    warn!("OpenSSL is no longer random!");
 8    unsafe {
 9        make_openssl_deterministic();
10        let mut seed: [u8; 4] = transmute(42u32.to_le());
11        let buf = seed.as_mut_ptr();
12        RAND_seed(buf, 4);
13    }
14}

I integrated this functionality into openssl-src-rs, which builds OpenSSL using the build system of Rust. If you are interested you can take a look here.

There is also the C compilation flag: FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION which is used for fuzzing. A quick search through the OpenSSL code reveals though that the goal of this flag is not to make random number generation deterministic, but change the behavior of OpenSSL for fuzzing. It mostly skips error messages like in cmp_msg.c. Sadly, there is no official documentation which does into detail what the benefits of using the flag are.

Do you have questions? Send an email to max@maxammann.org