Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file helpers.h |
| 3 | * |
| 4 | * \brief This file contains the prototypes of helper functions for the |
| 5 | * 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 |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 10 | * 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 Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #ifndef TEST_HELPERS_H |
| 26 | #define TEST_HELPERS_H |
| 27 | |
| 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 29 | #include "mbedtls/config.h" |
| 30 | #else |
| 31 | #include MBEDTLS_CONFIG_FILE |
| 32 | #endif |
| 33 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
| 35 | #include "mbedtls/platform.h" |
| 36 | #else |
| 37 | #include <stdio.h> |
| 38 | #define mbedtls_fprintf fprintf |
| 39 | #define mbedtls_snprintf snprintf |
| 40 | #define mbedtls_calloc calloc |
| 41 | #define mbedtls_free free |
| 42 | #define mbedtls_exit exit |
| 43 | #define mbedtls_time time |
| 44 | #define mbedtls_time_t time_t |
| 45 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 46 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 47 | #endif |
| 48 | |
| 49 | #include <stddef.h> |
| 50 | #include <stdint.h> |
| 51 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 52 | int mbedtls_test_platform_setup( void ); |
| 53 | void mbedtls_test_platform_teardown( void ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 54 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 55 | /** |
Ronald Cron | ab500cb | 2020-07-01 17:09:10 +0200 | [diff] [blame] | 56 | * \brief This function decodes the hexadecimal representation of |
| 57 | * data. |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 58 | * |
| 59 | * \note The output buffer can be the same as the input buffer. For |
| 60 | * any other overlapping of the input and output buffers, the |
| 61 | * behavior is undefined. |
| 62 | * |
| 63 | * \param obuf Output buffer. |
| 64 | * \param obufmax Size in number of bytes of \p obuf. |
| 65 | * \param ibuf Input buffer. |
| 66 | * \param len The number of unsigned char written in \p obuf. This must |
| 67 | * not be \c NULL. |
| 68 | * |
| 69 | * \return \c 0 on success. |
| 70 | * \return \c -1 if the output buffer is too small or the input string |
Ronald Cron | ab500cb | 2020-07-01 17:09:10 +0200 | [diff] [blame] | 71 | * is not a valid hexadecimal representation. |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 72 | */ |
| 73 | int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax, |
| 74 | const char *ibuf, size_t *len ); |
| 75 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 76 | void mbedtls_test_hexify( unsigned char *obuf, |
| 77 | const unsigned char *ibuf, |
| 78 | int len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Allocate and zeroize a buffer. |
| 82 | * |
| 83 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 84 | * |
| 85 | * For convenience, dies if allocation fails. |
| 86 | */ |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 87 | unsigned char *mbedtls_test_zero_alloc( size_t len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Allocate and fill a buffer from hex data. |
| 91 | * |
| 92 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 93 | * overruns (including overreads) when running the test suite under valgrind. |
| 94 | * |
| 95 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 96 | * |
| 97 | * For convenience, dies if allocation fails. |
| 98 | */ |
Ronald Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 99 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 100 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 101 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 102 | uint32_t a_len, uint32_t b_len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 103 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 104 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 105 | |
| 106 | typedef struct |
| 107 | { |
| 108 | const char *failure_condition; |
| 109 | const char *file; |
| 110 | int line; |
| 111 | } |
| 112 | mbedtls_test_param_failed_location_record_t; |
| 113 | |
| 114 | /** |
| 115 | * \brief Get the location record of the last call to |
| 116 | * mbedtls_test_param_failed(). |
| 117 | * |
| 118 | * \note The call expectation is set up and active until the next call to |
| 119 | * mbedtls_test_param_failed_check_expected_call() or |
| 120 | * mbedtls_param_failed() that cancels it. |
| 121 | */ |
| 122 | void mbedtls_test_param_failed_get_location_record( |
| 123 | mbedtls_test_param_failed_location_record_t *location_record ); |
| 124 | |
| 125 | /** |
| 126 | * \brief State that a call to mbedtls_param_failed() is expected. |
| 127 | * |
| 128 | * \note The call expectation is set up and active until the next call to |
| 129 | * mbedtls_test_param_failed_check_expected_call() or |
| 130 | * mbedtls_param_failed that cancel it. |
| 131 | */ |
| 132 | void mbedtls_test_param_failed_expect_call( void ); |
| 133 | |
| 134 | /** |
| 135 | * \brief Check whether mbedtls_param_failed() has been called as expected. |
| 136 | * |
| 137 | * \note Check whether mbedtls_param_failed() has been called between the |
| 138 | * last call to mbedtls_test_param_failed_expect_call() and the call |
| 139 | * to this function. |
| 140 | * |
| 141 | * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(), |
| 142 | * mbedtls_param_failed() has been called. |
| 143 | * \c -1 Otherwise. |
| 144 | */ |
| 145 | int mbedtls_test_param_failed_check_expected_call( void ); |
| 146 | |
| 147 | /** |
Ronald Cron | bf4f408 | 2020-09-25 10:45:06 +0200 | [diff] [blame] | 148 | * \brief Get the address of the object of type jmp_buf holding the execution |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 149 | * state information used by mbedtls_param_failed() to do a long jump. |
| 150 | * |
| 151 | * \note If a call to mbedtls_param_failed() is not expected in the sense |
| 152 | * that there is no call to mbedtls_test_param_failed_expect_call() |
| 153 | * preceding it, then mbedtls_param_failed() will try to restore the |
| 154 | * execution to the state stored in the jmp_buf object whose address |
| 155 | * is returned by the present function. |
| 156 | * |
Ronald Cron | bf4f408 | 2020-09-25 10:45:06 +0200 | [diff] [blame] | 157 | * \note This function is intended to provide the parameter of the |
| 158 | * setjmp() function to set-up where mbedtls_param_failed() should |
| 159 | * long-jump if it has to. It is foreseen to be used as: |
| 160 | * |
| 161 | * setjmp( mbedtls_test_param_failed_get_state_buf() ). |
| 162 | * |
| 163 | * \note The type of the returned value is not jmp_buf as jmp_buf is an |
| 164 | * an array type (C specification) and a function cannot return an |
| 165 | * array type. |
| 166 | * |
| 167 | * \note The type of the returned value is not jmp_buf* as then the return |
| 168 | * value couldn't be used by setjmp(), as its parameter's type is |
| 169 | * jmp_buf. |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 170 | * |
| 171 | * \return Address of the object of type jmp_buf holding the execution state |
| 172 | * information used by mbedtls_param_failed() to do a long jump. |
| 173 | */ |
| 174 | void* mbedtls_test_param_failed_get_state_buf( void ); |
| 175 | |
| 176 | /** |
| 177 | * \brief Reset the execution state used by mbedtls_param_failed() to do a |
| 178 | * long jump. |
| 179 | * |
| 180 | * \note If a call to mbedtls_param_failed() is not expected in the sense |
| 181 | * that there is no call to mbedtls_test_param_failed_expect_call() |
| 182 | * preceding it, then mbedtls_param_failed() will try to restore the |
| 183 | * execution state that this function reset. |
| 184 | * |
| 185 | * \note It is recommended to reset the execution state when the state |
| 186 | * is not relevant anymore. That way an unexpected call to |
| 187 | * mbedtls_param_failed() will not trigger a long jump with |
| 188 | * undefined behavior but rather a long jump that will rather fault. |
| 189 | */ |
| 190 | void mbedtls_test_param_failed_reset_state( void ); |
| 191 | #endif /* MBEDTLS_CHECK_PARAMS */ |
| 192 | |
Gilles Peskine | 1af872d | 2021-01-20 20:02:01 +0100 | [diff] [blame] | 193 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 194 | #include "test/fake_external_rng_for_test.h" |
| 195 | #endif |
| 196 | |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 197 | #endif /* TEST_HELPERS_H */ |