blob: c5572088ac4e37873d18304b92e3de568b7f92ab [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
Ronald Cronb7eb67f2020-06-09 16:57:42 +020010 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
Ronald Cronb7eb67f2020-06-09 16:57:42 +020023 */
24
25#ifndef TEST_RANDOM_H
26#define TEST_RANDOM_H
27
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Ronald Cronb7eb67f2020-06-09 16:57:42 +020029
Ronald Cron2058d562020-06-09 17:11:47 +020030#include <stddef.h>
31#include <stdint.h>
32
Gilles Peskine449bd832023-01-11 14:50:10 +010033typedef struct {
Gilles Peskineecacc3c2021-03-24 00:48:57 +010034 unsigned char *buf; /* Pointer to a buffer of length bytes. */
Ronald Cron2058d562020-06-09 17:11:47 +020035 size_t length;
Gilles Peskineecacc3c2021-03-24 00:48:57 +010036 /* If fallback_f_rng is NULL, fail after delivering length bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +010037 int (*fallback_f_rng)(void *, unsigned char *, size_t);
Gilles Peskineecacc3c2021-03-24 00:48:57 +010038 void *fallback_p_rng;
Ronald Cron351f0ee2020-06-10 12:12:18 +020039} mbedtls_test_rnd_buf_info;
Ronald Cron2058d562020-06-09 17:11:47 +020040
41/**
42 * Info structure for the pseudo random function
43 *
44 * Key should be set at the start to a test-unique value.
45 * Do not forget endianness!
46 * State( v0, v1 ) should be set to zero.
47 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048typedef struct {
Ronald Cron2058d562020-06-09 17:11:47 +020049 uint32_t key[16];
50 uint32_t v0, v1;
Ronald Cron351f0ee2020-06-10 12:12:18 +020051} mbedtls_test_rnd_pseudo_info;
Ronald Cron2058d562020-06-09 17:11:47 +020052
53/**
54 * This function just returns data from rand().
55 * Although predictable and often similar on multiple
56 * runs, this does not result in identical random on
57 * each run. So do not use this if the results of a
58 * test depend on the random data that is generated.
59 *
60 * rng_state shall be NULL.
61 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_test_rnd_std_rand(void *rng_state,
63 unsigned char *output,
64 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020065
66/**
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010067 * This function only returns zeros.
Ronald Cron2058d562020-06-09 17:11:47 +020068 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010069 * \p rng_state shall be \c NULL.
Ronald Cron2058d562020-06-09 17:11:47 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_test_rnd_zero_rand(void *rng_state,
72 unsigned char *output,
73 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020074
75/**
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010076 * This function returns random data based on a buffer it receives.
Ronald Cron2058d562020-06-09 17:11:47 +020077 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010078 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure.
Ronald Cron2058d562020-06-09 17:11:47 +020079 *
80 * The number of bytes released from the buffer on each call to
Gilles Peskinec7eeeb12021-06-02 21:17:36 +020081 * the random function is specified by \p len.
Ronald Cron2058d562020-06-09 17:11:47 +020082 *
Gilles Peskineecacc3c2021-03-24 00:48:57 +010083 * After the buffer is empty, this function will call the fallback RNG in the
84 * #mbedtls_test_rnd_buf_info structure if there is one, and
85 * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise.
Ronald Cron2058d562020-06-09 17:11:47 +020086 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087int mbedtls_test_rnd_buffer_rand(void *rng_state,
88 unsigned char *output,
89 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +020090
91/**
92 * This function returns random based on a pseudo random function.
93 * This means the results should be identical on all systems.
94 * Pseudo random is based on the XTEA encryption algorithm to
95 * generate pseudorandom.
96 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010097 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure.
Ronald Cron2058d562020-06-09 17:11:47 +020098 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099int mbedtls_test_rnd_pseudo_rand(void *rng_state,
100 unsigned char *output,
101 size_t len);
Ronald Cron2058d562020-06-09 17:11:47 +0200102
Ronald Cronb7eb67f2020-06-09 16:57:42 +0200103#endif /* TEST_RANDOM_H */