Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 1 | /** |
| 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úti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 8 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 9 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 10 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef TEST_RANDOM_H |
| 14 | #define TEST_RANDOM_H |
| 15 | |
| 16 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 17 | #include "mbedtls/config.h" |
| 18 | #else |
| 19 | #include MBEDTLS_CONFIG_FILE |
| 20 | #endif |
| 21 | |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 25 | typedef struct { |
Gilles Peskine | bef3019 | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 26 | unsigned char *buf; /* Pointer to a buffer of length bytes. */ |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 27 | size_t length; |
Gilles Peskine | bef3019 | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 28 | /* If fallback_f_rng is NULL, fail after delivering length bytes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | int (*fallback_f_rng)(void *, unsigned char *, size_t); |
Gilles Peskine | bef3019 | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 30 | void *fallback_p_rng; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 31 | } mbedtls_test_rnd_buf_info; |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Info structure for the pseudo random function |
| 35 | * |
| 36 | * Key should be set at the start to a test-unique value. |
| 37 | * Do not forget endianness! |
| 38 | * State( v0, v1 ) should be set to zero. |
| 39 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 40 | typedef struct { |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 41 | uint32_t key[16]; |
| 42 | uint32_t v0, v1; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 43 | } mbedtls_test_rnd_pseudo_info; |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * This function just returns data from rand(). |
| 47 | * Although predictable and often similar on multiple |
| 48 | * runs, this does not result in identical random on |
| 49 | * each run. So do not use this if the results of a |
| 50 | * test depend on the random data that is generated. |
| 51 | * |
| 52 | * rng_state shall be NULL. |
| 53 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | int mbedtls_test_rnd_std_rand(void *rng_state, |
| 55 | unsigned char *output, |
| 56 | size_t len); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
Gilles Peskine | ebf3a4b | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 59 | * This function only returns zeros. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 60 | * |
Gilles Peskine | ebf3a4b | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 61 | * \p rng_state shall be \c NULL. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 62 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | int mbedtls_test_rnd_zero_rand(void *rng_state, |
| 64 | unsigned char *output, |
| 65 | size_t len); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 66 | |
| 67 | /** |
Gilles Peskine | ebf3a4b | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 68 | * This function returns random data based on a buffer it receives. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 69 | * |
Gilles Peskine | ebf3a4b | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 70 | * \p rng_state shall be a pointer to a #mbedtls_test_rnd_buf_info structure. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 71 | * |
| 72 | * The number of bytes released from the buffer on each call to |
Gilles Peskine | b72b7e6 | 2021-06-02 21:17:36 +0200 | [diff] [blame] | 73 | * the random function is specified by \p len. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 74 | * |
Gilles Peskine | bef3019 | 2021-03-24 00:48:57 +0100 | [diff] [blame] | 75 | * After the buffer is empty, this function will call the fallback RNG in the |
| 76 | * #mbedtls_test_rnd_buf_info structure if there is one, and |
| 77 | * will return #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 78 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | int mbedtls_test_rnd_buffer_rand(void *rng_state, |
| 80 | unsigned char *output, |
| 81 | size_t len); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * This function returns random based on a pseudo random function. |
| 85 | * This means the results should be identical on all systems. |
| 86 | * Pseudo random is based on the XTEA encryption algorithm to |
| 87 | * generate pseudorandom. |
| 88 | * |
Gilles Peskine | ebf3a4b | 2021-03-24 00:14:53 +0100 | [diff] [blame] | 89 | * \p rng_state shall be a pointer to a #mbedtls_test_rnd_pseudo_info structure. |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 90 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | int mbedtls_test_rnd_pseudo_rand(void *rng_state, |
| 92 | unsigned char *output, |
| 93 | size_t len); |
Ronald Cron | 2058d56 | 2020-06-09 17:11:47 +0200 | [diff] [blame] | 94 | |
Ronald Cron | b7eb67f | 2020-06-09 16:57:42 +0200 | [diff] [blame] | 95 | #endif /* TEST_RANDOM_H */ |