blob: 6304e05d7fa8ae6b43d31277d7ce0871c4b02cd7 [file] [log] [blame]
Ronald Cronb7eb67f2020-06-09 16:57:42 +02001/**
2 * \file random.h
3 *
4 * \brief This file contains the prototypes of helper functions to generate
5 * random numbers for the purpose of testing.
6 */
7
Bence Szépkúti86974652020-06-15 11:59:37 +02008/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cronb7eb67f2020-06-09 16:57:42 +020011 */
12
13#ifndef TEST_RANDOM_H
14#define TEST_RANDOM_H
15
Bence Szépkútic662b362021-05-27 11:25:03 +020016#include "mbedtls/build_info.h"
Ronald Cronb7eb67f2020-06-09 16:57:42 +020017
Ronald Cron2058d562020-06-09 17:11:47 +020018#include <stddef.h>
19#include <stdint.h>
20
Gilles Peskine449bd832023-01-11 14:50:10 +010021typedef struct {
Gilles Peskineecacc3c2021-03-24 00:48:57 +010022 unsigned char *buf; /* Pointer to a buffer of length bytes. */
Ronald Cron2058d562020-06-09 17:11:47 +020023 size_t length;
Gilles Peskineecacc3c2021-03-24 00:48:57 +010024 /* If fallback_f_rng is NULL, fail after delivering length bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +010025 int (*fallback_f_rng)(void *, unsigned char *, size_t);
Gilles Peskineecacc3c2021-03-24 00:48:57 +010026 void *fallback_p_rng;
Ronald Cron351f0ee2020-06-10 12:12:18 +020027} mbedtls_test_rnd_buf_info;
Ronald Cron2058d562020-06-09 17:11:47 +020028
29/**
30 * Info structure for the pseudo random function
31 *
32 * Key should be set at the start to a test-unique value.
33 * Do not forget endianness!
34 * State( v0, v1 ) should be set to zero.
35 */
Gilles Peskine449bd832023-01-11 14:50:10 +010036typedef struct {
Ronald Cron2058d562020-06-09 17:11:47 +020037 uint32_t key[16];
38 uint32_t v0, v1;
Ronald Cron351f0ee2020-06-10 12:12:18 +020039} mbedtls_test_rnd_pseudo_info;
Ronald Cron2058d562020-06-09 17:11:47 +020040
41/**
42 * This function just returns data from rand().
43 * Although predictable and often similar on multiple
44 * runs, this does not result in identical random on
45 * each run. So do not use this if the results of a
46 * test depend on the random data that is generated.
47 *
48 * rng_state shall be NULL.
49 */
Gilles Peskine449bd832023-01-11 14:50:10 +010050int mbedtls_test_rnd_std_rand(void *rng_state,
51 unsigned char *output,
52 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020053
54/**
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010055 * This function only returns zeros.
Ronald Cron2058d562020-06-09 17:11:47 +020056 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010057 * \p rng_state shall be \c NULL.
Ronald Cron2058d562020-06-09 17:11:47 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059int mbedtls_test_rnd_zero_rand(void *rng_state,
60 unsigned char *output,
61 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020062
63/**
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010064 * This function returns random data based on a buffer it receives.
Ronald Cron2058d562020-06-09 17:11:47 +020065 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010066 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure.
Ronald Cron2058d562020-06-09 17:11:47 +020067 *
68 * The number of bytes released from the buffer on each call to
Gilles Peskinec7eeeb12021-06-02 21:17:36 +020069 * the random function is specified by \p len.
Ronald Cron2058d562020-06-09 17:11:47 +020070 *
Gilles Peskineecacc3c2021-03-24 00:48:57 +010071 * After the buffer is empty, this function will call the fallback RNG in the
72 * #mbedtls_test_rnd_buf_info structure if there is one, and
73 * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise.
Ronald Cron2058d562020-06-09 17:11:47 +020074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075int mbedtls_test_rnd_buffer_rand(void *rng_state,
76 unsigned char *output,
77 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020078
79/**
80 * This function returns random based on a pseudo random function.
81 * This means the results should be identical on all systems.
82 * Pseudo random is based on the XTEA encryption algorithm to
83 * generate pseudorandom.
84 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010085 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure.
Ronald Cron2058d562020-06-09 17:11:47 +020086 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087int mbedtls_test_rnd_pseudo_rand(void *rng_state,
88 unsigned char *output,
89 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020090
Ronald Cronb7eb67f2020-06-09 16:57:42 +020091#endif /* TEST_RANDOM_H */