blob: b64a072b3e0655d9630f906c7753deddb5ac824e [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
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "mbedtls/config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Ronald Cron2058d562020-06-09 17:11:47 +020034#include <stddef.h>
35#include <stdint.h>
36
37typedef struct
38{
Gilles Peskineecacc3c2021-03-24 00:48:57 +010039 unsigned char *buf; /* Pointer to a buffer of length bytes. */
Ronald Cron2058d562020-06-09 17:11:47 +020040 size_t length;
Gilles Peskineecacc3c2021-03-24 00:48:57 +010041 /* If fallback_f_rng is NULL, fail after delivering length bytes. */
42 int ( *fallback_f_rng )( void*, unsigned char *, size_t );
43 void *fallback_p_rng;
Ronald Cron351f0ee2020-06-10 12:12:18 +020044} mbedtls_test_rnd_buf_info;
Ronald Cron2058d562020-06-09 17:11:47 +020045
46/**
47 * Info structure for the pseudo random function
48 *
49 * Key should be set at the start to a test-unique value.
50 * Do not forget endianness!
51 * State( v0, v1 ) should be set to zero.
52 */
53typedef struct
54{
55 uint32_t key[16];
56 uint32_t v0, v1;
Ronald Cron351f0ee2020-06-10 12:12:18 +020057} mbedtls_test_rnd_pseudo_info;
Ronald Cron2058d562020-06-09 17:11:47 +020058
59/**
60 * This function just returns data from rand().
61 * Although predictable and often similar on multiple
62 * runs, this does not result in identical random on
63 * each run. So do not use this if the results of a
64 * test depend on the random data that is generated.
65 *
66 * rng_state shall be NULL.
67 */
Ronald Cron351f0ee2020-06-10 12:12:18 +020068int mbedtls_test_rnd_std_rand( void *rng_state,
69 unsigned char *output,
70 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +020071
72/**
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010073 * This function only returns zeros.
Ronald Cron2058d562020-06-09 17:11:47 +020074 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010075 * \p rng_state shall be \c NULL.
Ronald Cron2058d562020-06-09 17:11:47 +020076 */
Ronald Cron351f0ee2020-06-10 12:12:18 +020077int mbedtls_test_rnd_zero_rand( void *rng_state,
78 unsigned char *output,
79 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +020080
81/**
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010082 * This function returns random data based on a buffer it receives.
Ronald Cron2058d562020-06-09 17:11:47 +020083 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +010084 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure.
Ronald Cron2058d562020-06-09 17:11:47 +020085 *
86 * The number of bytes released from the buffer on each call to
87 * the random function is specified by per_call. (Can be between
88 * 1 and 4)
89 *
Gilles Peskineecacc3c2021-03-24 00:48:57 +010090 * After the buffer is empty, this function will call the fallback RNG in the
91 * #mbedtls_test_rnd_buf_info structure if there is one, and
92 * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise.
Ronald Cron2058d562020-06-09 17:11:47 +020093 */
Ronald Cron351f0ee2020-06-10 12:12:18 +020094int mbedtls_test_rnd_buffer_rand( void *rng_state,
95 unsigned char *output,
96 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +020097
98/**
99 * This function returns random based on a pseudo random function.
100 * This means the results should be identical on all systems.
101 * Pseudo random is based on the XTEA encryption algorithm to
102 * generate pseudorandom.
103 *
Gilles Peskine0b1b0ab2021-03-24 00:14:53 +0100104 * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure.
Ronald Cron2058d562020-06-09 17:11:47 +0200105 */
Ronald Cron351f0ee2020-06-10 12:12:18 +0200106int mbedtls_test_rnd_pseudo_rand( void *rng_state,
107 unsigned char *output,
108 size_t len );
Ronald Cron2058d562020-06-09 17:11:47 +0200109
Ronald Cronb7eb67f2020-06-09 16:57:42 +0200110#endif /* TEST_RANDOM_H */