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 | |
Mateusz Starzyk | b198272 | 2021-05-27 14:46:48 +0200 | [diff] [blame] | 28 | /* Most fields of publicly available structs are private and are wrapped with |
| 29 | * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields |
| 30 | * directly (without using the MBEDTLS_PRIVATE wrapper). */ |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 31 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 32 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 33 | #include "mbedtls/build_info.h" |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 34 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 35 | #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \ |
| 36 | defined(MBEDTLS_TEST_HOOKS) |
| 37 | #define MBEDTLS_TEST_MUTEX_USAGE |
| 38 | #endif |
| 39 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_PLATFORM_C) |
| 41 | #include "mbedtls/platform.h" |
| 42 | #else |
| 43 | #include <stdio.h> |
| 44 | #define mbedtls_fprintf fprintf |
| 45 | #define mbedtls_snprintf snprintf |
| 46 | #define mbedtls_calloc calloc |
| 47 | #define mbedtls_free free |
| 48 | #define mbedtls_exit exit |
| 49 | #define mbedtls_time time |
| 50 | #define mbedtls_time_t time_t |
| 51 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 52 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 53 | #endif |
| 54 | |
| 55 | #include <stddef.h> |
| 56 | #include <stdint.h> |
| 57 | |
Gilles Peskine | ebc49e5 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_BIGNUM_C) |
| 59 | #include "mbedtls/bignum.h" |
| 60 | #endif |
| 61 | |
Gilles Peskine | 571576f | 2022-09-20 21:37:56 +0200 | [diff] [blame] | 62 | /** The type of test case arguments that contain binary data. */ |
| 63 | typedef struct data_tag |
| 64 | { |
| 65 | uint8_t * x; |
| 66 | uint32_t len; |
| 67 | } data_t; |
| 68 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 69 | typedef enum |
| 70 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 71 | MBEDTLS_TEST_RESULT_SUCCESS = 0, |
| 72 | MBEDTLS_TEST_RESULT_FAILED, |
| 73 | MBEDTLS_TEST_RESULT_SKIPPED |
| 74 | } mbedtls_test_result_t; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 75 | |
| 76 | typedef struct |
| 77 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 78 | mbedtls_test_result_t result; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 79 | const char *test; |
| 80 | const char *filename; |
| 81 | int line_no; |
| 82 | unsigned long step; |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 83 | char line1[76]; |
| 84 | char line2[76]; |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 85 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 86 | const char *mutex_usage_error; |
| 87 | #endif |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 88 | } |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 89 | mbedtls_test_info_t; |
| 90 | extern mbedtls_test_info_t mbedtls_test_info; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 91 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 92 | int mbedtls_test_platform_setup( void ); |
| 93 | void mbedtls_test_platform_teardown( void ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 94 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 95 | /** |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 96 | * \brief Record the current test case as a failure. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 97 | * |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 98 | * This function can be called directly however it is usually |
| 99 | * called via macros such as TEST_ASSERT, TEST_EQUAL, |
| 100 | * PSA_ASSERT, etc... |
| 101 | * |
| 102 | * \note If the test case was already marked as failed, calling |
| 103 | * `mbedtls_test_fail( )` again will not overwrite any |
| 104 | * previous information about the failure. |
| 105 | * |
| 106 | * \param test Description of the failure or assertion that failed. This |
| 107 | * MUST be a string literal. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 108 | * \param line_no Line number where the failure originated. |
| 109 | * \param filename Filename where the failure originated. |
| 110 | */ |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 111 | void mbedtls_test_fail( const char *test, int line_no, const char* filename ); |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 112 | |
| 113 | /** |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 114 | * \brief Record the current test case as skipped. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 115 | * |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 116 | * This function can be called directly however it is usually |
| 117 | * called via the TEST_ASSUME macro. |
| 118 | * |
| 119 | * \param test Description of the assumption that caused the test case to |
| 120 | * be skipped. This MUST be a string literal. |
| 121 | * \param line_no Line number where the test case was skipped. |
| 122 | * \param filename Filename where the test case was skipped. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 123 | */ |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 124 | void mbedtls_test_skip( const char *test, int line_no, const char* filename ); |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 125 | |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 126 | /** |
| 127 | * \brief Set the test step number for failure reports. |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 128 | * |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 129 | * Call this function to display "step NNN" in addition to the |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 130 | * line number and file name if a test fails. Typically the "step |
| 131 | * number" is the index of a for loop but it can be whatever you |
| 132 | * want. |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 133 | * |
| 134 | * \param step The step number to report. |
| 135 | */ |
| 136 | void mbedtls_test_set_step( unsigned long step ); |
| 137 | |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 138 | /** |
| 139 | * \brief Reset mbedtls_test_info to a ready/starting state. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 140 | */ |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 141 | void mbedtls_test_info_reset( void ); |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 142 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 143 | /** |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 144 | * \brief Record the current test case as a failure if two integers |
| 145 | * have a different value. |
| 146 | * |
| 147 | * This function is usually called via the macro |
| 148 | * #TEST_EQUAL. |
| 149 | * |
| 150 | * \param test Description of the failure or assertion that failed. This |
| 151 | * MUST be a string literal. This normally has the form |
| 152 | * "EXPR1 == EXPR2" where EXPR1 has the value \p value1 |
| 153 | * and EXPR2 has the value \p value2. |
| 154 | * \param line_no Line number where the failure originated. |
| 155 | * \param filename Filename where the failure originated. |
| 156 | * \param value1 The first value to compare. |
| 157 | * \param value2 The second value to compare. |
| 158 | * |
| 159 | * \return \c 1 if the values are equal, otherwise \c 0. |
| 160 | */ |
| 161 | int mbedtls_test_equal( const char *test, int line_no, const char* filename, |
| 162 | unsigned long long value1, unsigned long long value2 ); |
| 163 | |
| 164 | /** |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 165 | * \brief Record the current test case as a failure based |
| 166 | * on comparing two unsigned integers. |
| 167 | * |
| 168 | * This function is usually called via the macro |
| 169 | * #TEST_LE_U. |
| 170 | * |
| 171 | * \param test Description of the failure or assertion that failed. This |
| 172 | * MUST be a string literal. This normally has the form |
| 173 | * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1 |
| 174 | * and EXPR2 has the value \p value2. |
| 175 | * \param line_no Line number where the failure originated. |
| 176 | * \param filename Filename where the failure originated. |
| 177 | * \param value1 The first value to compare. |
| 178 | * \param value2 The second value to compare. |
| 179 | * |
| 180 | * \return \c 1 if \p value1 <= \p value2, otherwise \c 0. |
| 181 | */ |
| 182 | int mbedtls_test_le_u( const char *test, int line_no, const char* filename, |
| 183 | unsigned long long value1, unsigned long long value2 ); |
| 184 | |
| 185 | /** |
| 186 | * \brief Record the current test case as a failure based |
| 187 | * on comparing two signed integers. |
| 188 | * |
| 189 | * This function is usually called via the macro |
| 190 | * #TEST_LE_S. |
| 191 | * |
| 192 | * \param test Description of the failure or assertion that failed. This |
| 193 | * MUST be a string literal. This normally has the form |
| 194 | * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1 |
| 195 | * and EXPR2 has the value \p value2. |
| 196 | * \param line_no Line number where the failure originated. |
| 197 | * \param filename Filename where the failure originated. |
| 198 | * \param value1 The first value to compare. |
| 199 | * \param value2 The second value to compare. |
| 200 | * |
| 201 | * \return \c 1 if \p value1 <= \p value2, otherwise \c 0. |
| 202 | */ |
| 203 | int mbedtls_test_le_s( const char *test, int line_no, const char* filename, |
| 204 | long long value1, long long value2 ); |
| 205 | |
| 206 | /** |
Ronald Cron | ab500cb | 2020-07-01 17:09:10 +0200 | [diff] [blame] | 207 | * \brief This function decodes the hexadecimal representation of |
| 208 | * data. |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 209 | * |
| 210 | * \note The output buffer can be the same as the input buffer. For |
| 211 | * any other overlapping of the input and output buffers, the |
| 212 | * behavior is undefined. |
| 213 | * |
| 214 | * \param obuf Output buffer. |
| 215 | * \param obufmax Size in number of bytes of \p obuf. |
| 216 | * \param ibuf Input buffer. |
| 217 | * \param len The number of unsigned char written in \p obuf. This must |
| 218 | * not be \c NULL. |
| 219 | * |
| 220 | * \return \c 0 on success. |
| 221 | * \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] | 222 | * is not a valid hexadecimal representation. |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 223 | */ |
| 224 | int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax, |
| 225 | const char *ibuf, size_t *len ); |
| 226 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 227 | void mbedtls_test_hexify( unsigned char *obuf, |
| 228 | const unsigned char *ibuf, |
| 229 | int len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Allocate and zeroize a buffer. |
| 233 | * |
| 234 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 235 | * |
| 236 | * For convenience, dies if allocation fails. |
| 237 | */ |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 238 | unsigned char *mbedtls_test_zero_alloc( size_t len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * Allocate and fill a buffer from hex data. |
| 242 | * |
| 243 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 244 | * overruns (including overreads) when running the test suite under valgrind. |
| 245 | * |
| 246 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 247 | * |
| 248 | * For convenience, dies if allocation fails. |
| 249 | */ |
Ronald Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 250 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 251 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 252 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 253 | uint32_t a_len, uint32_t b_len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 254 | |
Gilles Peskine | 1dc19ff | 2021-02-08 20:59:39 +0100 | [diff] [blame] | 255 | #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1af872d | 2021-01-20 20:02:01 +0100 | [diff] [blame] | 256 | #include "test/fake_external_rng_for_test.h" |
| 257 | #endif |
| 258 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 259 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 260 | /** Permanently activate the mutex usage verification framework. See |
| 261 | * threading_helpers.c for information. */ |
| 262 | void mbedtls_test_mutex_usage_init( void ); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 263 | |
| 264 | /** Call this function after executing a test case to check for mutex usage |
| 265 | * errors. */ |
| 266 | void mbedtls_test_mutex_usage_check( void ); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 267 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |
| 268 | |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 269 | #if defined(MBEDTLS_TEST_HOOKS) |
| 270 | /** |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 271 | * \brief Check that only a pure high-level error code is being combined with |
| 272 | * a pure low-level error code as otherwise the resultant error code |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 273 | * would be corrupted. |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 274 | * |
| 275 | * \note Both high-level and low-level error codes cannot be greater than |
| 276 | * zero however can be zero. If one error code is zero then the |
| 277 | * other error code is returned even if both codes are zero. |
| 278 | * |
| 279 | * \note If the check fails, fail the test currently being run. |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 280 | */ |
| 281 | void mbedtls_test_err_add_check( int high, int low, |
| 282 | const char *file, int line); |
| 283 | #endif |
| 284 | |
Gilles Peskine | ebc49e5 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 285 | #if defined(MBEDTLS_BIGNUM_C) |
Gilles Peskine | 3aae4e8 | 2022-09-20 21:38:33 +0200 | [diff] [blame] | 286 | /** Allocate and populate a core MPI from a test case argument. |
| 287 | * |
| 288 | * This function allocates exactly as many limbs as necessary to fit |
| 289 | * the length of the input. In other words, it preserves leading zeros. |
| 290 | * |
| 291 | * The limb array is allocated with mbedtls_calloc() and must later be |
| 292 | * freed with mbedtls_free(). |
| 293 | * |
| 294 | * \param[in,out] pX The address where a pointer to the allocated limb |
| 295 | * array will be stored. |
| 296 | * \c *pX must be null on entry. |
| 297 | * On exit, \c *pX is null on error or if the number |
| 298 | * of limbs is 0. |
| 299 | * \param[out] plimbs The address where the number of limbs will be stored. |
| 300 | * \param[in] input The test argument to read. |
| 301 | * It is interpreted as a big-endian integer in base 256. |
| 302 | * |
| 303 | * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise. |
| 304 | */ |
| 305 | int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs, |
| 306 | const data_t *input ); |
| 307 | |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 308 | /** Read an MPI from a hexadecimal string. |
Gilles Peskine | ebc49e5 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 309 | * |
| 310 | * Like mbedtls_mpi_read_string(), but size the resulting bignum based |
| 311 | * on the number of digits in the string. In particular, construct a |
| 312 | * bignum with 0 limbs for an empty string, and a bignum with leading 0 |
| 313 | * limbs if the string has sufficiently many leading 0 digits. |
| 314 | * |
| 315 | * This is important so that the "0 (null)" and "0 (1 limb)" and |
| 316 | * "leading zeros" test cases do what they claim. |
| 317 | * |
| 318 | * \param[out] X The MPI object to populate. It must be initialized. |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 319 | * \param[in] s The null-terminated hexadecimal string to read from. |
Gilles Peskine | ebc49e5 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 320 | * |
| 321 | * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise. |
| 322 | */ |
Werner Lewis | 19b4cd8 | 2022-07-07 11:02:27 +0100 | [diff] [blame] | 323 | int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s ); |
Gilles Peskine | ebc49e5 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 324 | #endif /* MBEDTLS_BIGNUM_C */ |
| 325 | |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 326 | #endif /* TEST_HELPERS_H */ |