blob: fde8cc5c2ae56ff99631e368abc047f1e589bbfc [file] [log] [blame]
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02001/**
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úti86974652020-06-15 11:59:37 +02008/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020011 */
12
13#ifndef TEST_HELPERS_H
14#define TEST_HELPERS_H
15
16#if !defined(MBEDTLS_CONFIG_FILE)
17#include "mbedtls/config.h"
18#else
19#include MBEDTLS_CONFIG_FILE
20#endif
21
Gilles Peskine2a4c5982021-01-29 21:18:09 +010022#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
23 defined(MBEDTLS_TEST_HOOKS)
24#define MBEDTLS_TEST_MUTEX_USAGE
25#endif
26
Ronald Cronf40529d2020-06-09 16:27:37 +020027#include "mbedtls/platform.h"
Ronald Cronf40529d2020-06-09 16:27:37 +020028
29#include <stddef.h>
30#include <stdint.h>
31
Gilles Peskinedb479712021-06-11 14:13:53 +020032#if defined(MBEDTLS_BIGNUM_C)
33#include "mbedtls/bignum.h"
34#endif
35
Gilles Peskine34cb4622022-09-20 21:37:56 +020036/** The type of test case arguments that contain binary data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037typedef struct data_tag {
38 uint8_t *x;
Gilles Peskine34cb4622022-09-20 21:37:56 +020039 uint32_t len;
40} data_t;
41
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042typedef enum {
Chris Jonese60e2ae2021-01-20 17:51:47 +000043 MBEDTLS_TEST_RESULT_SUCCESS = 0,
44 MBEDTLS_TEST_RESULT_FAILED,
45 MBEDTLS_TEST_RESULT_SKIPPED
46} mbedtls_test_result_t;
Chris Jones9634bb12021-01-20 15:56:42 +000047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048typedef struct {
Chris Jonese60e2ae2021-01-20 17:51:47 +000049 mbedtls_test_result_t result;
Chris Jones9634bb12021-01-20 15:56:42 +000050 const char *test;
51 const char *filename;
52 int line_no;
53 unsigned long step;
Gilles Peskineb4366492021-04-29 20:28:54 +020054 char line1[76];
55 char line2[76];
Gilles Peskine2a4c5982021-01-29 21:18:09 +010056#if defined(MBEDTLS_TEST_MUTEX_USAGE)
57 const char *mutex_usage_error;
58#endif
Chris Jones9634bb12021-01-20 15:56:42 +000059}
Chris Jonese60e2ae2021-01-20 17:51:47 +000060mbedtls_test_info_t;
61extern mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063int mbedtls_test_platform_setup(void);
64void mbedtls_test_platform_teardown(void);
Ronald Cronf40529d2020-06-09 16:27:37 +020065
Ronald Crona0c25392020-06-18 10:10:46 +020066/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000067 * \brief Record the current test case as a failure.
Chris Jones567e0ad2021-02-03 12:07:01 +000068 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000069 * This function can be called directly however it is usually
70 * called via macros such as TEST_ASSERT, TEST_EQUAL,
71 * PSA_ASSERT, etc...
72 *
73 * \note If the test case was already marked as failed, calling
74 * `mbedtls_test_fail( )` again will not overwrite any
75 * previous information about the failure.
76 *
77 * \param test Description of the failure or assertion that failed. This
78 * MUST be a string literal.
Chris Jones567e0ad2021-02-03 12:07:01 +000079 * \param line_no Line number where the failure originated.
80 * \param filename Filename where the failure originated.
81 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082void mbedtls_test_fail(const char *test, int line_no, const char *filename);
Chris Jones567e0ad2021-02-03 12:07:01 +000083
84/**
Chris Jones39ddb0a2021-02-03 16:15:00 +000085 * \brief Record the current test case as skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000086 *
Chris Jones39ddb0a2021-02-03 16:15:00 +000087 * This function can be called directly however it is usually
88 * called via the TEST_ASSUME macro.
89 *
90 * \param test Description of the assumption that caused the test case to
91 * be skipped. This MUST be a string literal.
92 * \param line_no Line number where the test case was skipped.
93 * \param filename Filename where the test case was skipped.
Chris Jones567e0ad2021-02-03 12:07:01 +000094 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095void mbedtls_test_skip(const char *test, int line_no, const char *filename);
Chris Jones9634bb12021-01-20 15:56:42 +000096
Chris Jones567e0ad2021-02-03 12:07:01 +000097/**
98 * \brief Set the test step number for failure reports.
Chris Jones9634bb12021-01-20 15:56:42 +000099 *
Chris Jones39ddb0a2021-02-03 16:15:00 +0000100 * Call this function to display "step NNN" in addition to the
Chris Jones567e0ad2021-02-03 12:07:01 +0000101 * line number and file name if a test fails. Typically the "step
102 * number" is the index of a for loop but it can be whatever you
103 * want.
Chris Jones9634bb12021-01-20 15:56:42 +0000104 *
105 * \param step The step number to report.
106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107void mbedtls_test_set_step(unsigned long step);
Chris Jones9634bb12021-01-20 15:56:42 +0000108
Chris Jones567e0ad2021-02-03 12:07:01 +0000109/**
110 * \brief Reset mbedtls_test_info to a ready/starting state.
Chris Jones567e0ad2021-02-03 12:07:01 +0000111 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112void mbedtls_test_info_reset(void);
Chris Jones9634bb12021-01-20 15:56:42 +0000113
Ronald Crona0c25392020-06-18 10:10:46 +0200114/**
Gilles Peskineb4366492021-04-29 20:28:54 +0200115 * \brief Record the current test case as a failure if two integers
116 * have a different value.
117 *
118 * This function is usually called via the macro
119 * #TEST_EQUAL.
120 *
121 * \param test Description of the failure or assertion that failed. This
122 * MUST be a string literal. This normally has the form
123 * "EXPR1 == EXPR2" where EXPR1 has the value \p value1
124 * and EXPR2 has the value \p value2.
125 * \param line_no Line number where the failure originated.
126 * \param filename Filename where the failure originated.
127 * \param value1 The first value to compare.
128 * \param value2 The second value to compare.
129 *
130 * \return \c 1 if the values are equal, otherwise \c 0.
131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132int mbedtls_test_equal(const char *test, int line_no, const char *filename,
133 unsigned long long value1, unsigned long long value2);
Gilles Peskineb4366492021-04-29 20:28:54 +0200134
135/**
Gilles Peskine063700d2022-04-13 23:59:52 +0200136 * \brief Record the current test case as a failure based
137 * on comparing two unsigned integers.
138 *
139 * This function is usually called via the macro
140 * #TEST_LE_U.
141 *
142 * \param test Description of the failure or assertion that failed. This
143 * MUST be a string literal. This normally has the form
144 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
145 * and EXPR2 has the value \p value2.
146 * \param line_no Line number where the failure originated.
147 * \param filename Filename where the failure originated.
148 * \param value1 The first value to compare.
149 * \param value2 The second value to compare.
150 *
151 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
152 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
154 unsigned long long value1, unsigned long long value2);
Gilles Peskine063700d2022-04-13 23:59:52 +0200155
156/**
157 * \brief Record the current test case as a failure based
158 * on comparing two signed integers.
159 *
160 * This function is usually called via the macro
161 * #TEST_LE_S.
162 *
163 * \param test Description of the failure or assertion that failed. This
164 * MUST be a string literal. This normally has the form
165 * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
166 * and EXPR2 has the value \p value2.
167 * \param line_no Line number where the failure originated.
168 * \param filename Filename where the failure originated.
169 * \param value1 The first value to compare.
170 * \param value2 The second value to compare.
171 *
172 * \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
175 long long value1, long long value2);
Gilles Peskine063700d2022-04-13 23:59:52 +0200176
177/**
Ronald Cronab500cb2020-07-01 17:09:10 +0200178 * \brief This function decodes the hexadecimal representation of
179 * data.
Ronald Crona0c25392020-06-18 10:10:46 +0200180 *
181 * \note The output buffer can be the same as the input buffer. For
182 * any other overlapping of the input and output buffers, the
183 * behavior is undefined.
184 *
185 * \param obuf Output buffer.
186 * \param obufmax Size in number of bytes of \p obuf.
187 * \param ibuf Input buffer.
188 * \param len The number of unsigned char written in \p obuf. This must
189 * not be \c NULL.
190 *
191 * \return \c 0 on success.
192 * \return \c -1 if the output buffer is too small or the input string
Ronald Cronab500cb2020-07-01 17:09:10 +0200193 * is not a valid hexadecimal representation.
Ronald Crona0c25392020-06-18 10:10:46 +0200194 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax,
196 const char *ibuf, size_t *len);
Ronald Crona0c25392020-06-18 10:10:46 +0200197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198void mbedtls_test_hexify(unsigned char *obuf,
199 const unsigned char *ibuf,
200 int len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200201
202/**
203 * Allocate and zeroize a buffer.
204 *
205 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
206 *
207 * For convenience, dies if allocation fails.
208 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209unsigned char *mbedtls_test_zero_alloc(size_t len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200210
211/**
212 * Allocate and fill a buffer from hex data.
213 *
214 * The buffer is sized exactly as needed. This allows to detect buffer
215 * overruns (including overreads) when running the test suite under valgrind.
216 *
217 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
218 *
219 * For convenience, dies if allocation fails.
220 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen);
Ronald Cronf40529d2020-06-09 16:27:37 +0200222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
224 uint32_t a_len, uint32_t b_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200225
Ronald Crona1236142020-07-01 16:01:21 +0200226#if defined(MBEDTLS_CHECK_PARAMS)
227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228typedef struct {
Ronald Crona1236142020-07-01 16:01:21 +0200229 const char *failure_condition;
230 const char *file;
231 int line;
232}
233mbedtls_test_param_failed_location_record_t;
234
235/**
236 * \brief Get the location record of the last call to
237 * mbedtls_test_param_failed().
238 *
239 * \note The call expectation is set up and active until the next call to
240 * mbedtls_test_param_failed_check_expected_call() or
241 * mbedtls_param_failed() that cancels it.
242 */
243void mbedtls_test_param_failed_get_location_record(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 mbedtls_test_param_failed_location_record_t *location_record);
Ronald Crona1236142020-07-01 16:01:21 +0200245
246/**
247 * \brief State that a call to mbedtls_param_failed() is expected.
248 *
249 * \note The call expectation is set up and active until the next call to
250 * mbedtls_test_param_failed_check_expected_call() or
251 * mbedtls_param_failed that cancel it.
252 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253void mbedtls_test_param_failed_expect_call(void);
Ronald Crona1236142020-07-01 16:01:21 +0200254
255/**
256 * \brief Check whether mbedtls_param_failed() has been called as expected.
257 *
258 * \note Check whether mbedtls_param_failed() has been called between the
259 * last call to mbedtls_test_param_failed_expect_call() and the call
260 * to this function.
261 *
262 * \return \c 0 Since the last call to mbedtls_param_failed_expect_call(),
263 * mbedtls_param_failed() has been called.
264 * \c -1 Otherwise.
265 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266int mbedtls_test_param_failed_check_expected_call(void);
Ronald Crona1236142020-07-01 16:01:21 +0200267
268/**
Ronald Cronbf4f4082020-09-25 10:45:06 +0200269 * \brief Get the address of the object of type jmp_buf holding the execution
Ronald Crona1236142020-07-01 16:01:21 +0200270 * state information used by mbedtls_param_failed() to do a long jump.
271 *
272 * \note If a call to mbedtls_param_failed() is not expected in the sense
273 * that there is no call to mbedtls_test_param_failed_expect_call()
274 * preceding it, then mbedtls_param_failed() will try to restore the
275 * execution to the state stored in the jmp_buf object whose address
276 * is returned by the present function.
277 *
Ronald Cronbf4f4082020-09-25 10:45:06 +0200278 * \note This function is intended to provide the parameter of the
279 * setjmp() function to set-up where mbedtls_param_failed() should
280 * long-jump if it has to. It is foreseen to be used as:
281 *
282 * setjmp( mbedtls_test_param_failed_get_state_buf() ).
283 *
284 * \note The type of the returned value is not jmp_buf as jmp_buf is an
285 * an array type (C specification) and a function cannot return an
286 * array type.
287 *
288 * \note The type of the returned value is not jmp_buf* as then the return
289 * value couldn't be used by setjmp(), as its parameter's type is
290 * jmp_buf.
Ronald Crona1236142020-07-01 16:01:21 +0200291 *
292 * \return Address of the object of type jmp_buf holding the execution state
293 * information used by mbedtls_param_failed() to do a long jump.
294 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295void *mbedtls_test_param_failed_get_state_buf(void);
Ronald Crona1236142020-07-01 16:01:21 +0200296
297/**
298 * \brief Reset the execution state used by mbedtls_param_failed() to do a
299 * long jump.
300 *
301 * \note If a call to mbedtls_param_failed() is not expected in the sense
302 * that there is no call to mbedtls_test_param_failed_expect_call()
303 * preceding it, then mbedtls_param_failed() will try to restore the
304 * execution state that this function reset.
305 *
306 * \note It is recommended to reset the execution state when the state
307 * is not relevant anymore. That way an unexpected call to
308 * mbedtls_param_failed() will not trigger a long jump with
309 * undefined behavior but rather a long jump that will rather fault.
310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311void mbedtls_test_param_failed_reset_state(void);
Ronald Crona1236142020-07-01 16:01:21 +0200312#endif /* MBEDTLS_CHECK_PARAMS */
313
Gilles Peskine1dc19ff2021-02-08 20:59:39 +0100314#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine1af872d2021-01-20 20:02:01 +0100315#include "test/fake_external_rng_for_test.h"
316#endif
317
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100318#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine1061ec62021-01-29 21:17:11 +0100319/** Permanently activate the mutex usage verification framework. See
320 * threading_helpers.c for information. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321void mbedtls_test_mutex_usage_init(void);
Gilles Peskine2a4c5982021-01-29 21:18:09 +0100322
323/** Call this function after executing a test case to check for mutex usage
324 * errors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325void mbedtls_test_mutex_usage_check(void);
Gilles Peskine1061ec62021-01-29 21:17:11 +0100326#endif /* MBEDTLS_TEST_MUTEX_USAGE */
327
Chris Jones96ae73b2021-01-08 17:04:59 +0000328#if defined(MBEDTLS_TEST_HOOKS)
329/**
Chris Jones3f613c12021-03-31 09:34:22 +0100330 * \brief Check that only a pure high-level error code is being combined with
331 * a pure low-level error code as otherwise the resultant error code
Chris Jones5e8805a2021-01-12 15:21:57 +0000332 * would be corrupted.
Chris Jones3f613c12021-03-31 09:34:22 +0100333 *
334 * \note Both high-level and low-level error codes cannot be greater than
335 * zero however can be zero. If one error code is zero then the
336 * other error code is returned even if both codes are zero.
337 *
338 * \note If the check fails, fail the test currently being run.
Chris Jones96ae73b2021-01-08 17:04:59 +0000339 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340void mbedtls_test_err_add_check(int high, int low,
341 const char *file, int line);
Chris Jones96ae73b2021-01-08 17:04:59 +0000342#endif
343
Gilles Peskinedb479712021-06-11 14:13:53 +0200344#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100345/** Read an MPI from a hexadecimal string.
Gilles Peskinedb479712021-06-11 14:13:53 +0200346 *
Gilles Peskine53a72062022-11-09 21:08:44 +0100347 * Like mbedtls_mpi_read_string(), but with tighter guarantees around
348 * edge cases.
Gilles Peskinedb479712021-06-11 14:13:53 +0200349 *
Gilles Peskine53a72062022-11-09 21:08:44 +0100350 * - This function guarantees that if \p s begins with '-' then the sign
351 * bit of the result will be negative, even if the value is 0.
352 * When this function encounters such a "negative 0", it
Gilles Peskined64123a2022-11-11 15:59:51 +0100353 * increments #mbedtls_test_case_uses_negative_0.
Gilles Peskine53a72062022-11-09 21:08:44 +0100354 * - The size of the result is exactly the minimum number of limbs needed
355 * to fit the digits in the input. In particular, this function constructs
356 * a bignum with 0 limbs for an empty string, and a bignum with leading 0
357 * limbs if the string has sufficiently many leading 0 digits.
358 * This is important so that the "0 (null)" and "0 (1 limb)" and
359 * "leading zeros" test cases do what they claim.
Gilles Peskinedb479712021-06-11 14:13:53 +0200360 *
361 * \param[out] X The MPI object to populate. It must be initialized.
Werner Lewis24b60782022-07-07 15:08:17 +0100362 * \param[in] s The null-terminated hexadecimal string to read from.
Gilles Peskinedb479712021-06-11 14:13:53 +0200363 *
364 * \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
365 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s);
Gilles Peskine53a72062022-11-09 21:08:44 +0100367
368/** Nonzero if the current test case had an input parsed with
369 * mbedtls_test_read_mpi() that is a negative 0 (`"-"`, `"-0"`, `"-00"`, etc.,
370 * constructing a result with the sign bit set to -1 and the value being
371 * all-limbs-0, which is not a valid representation in #mbedtls_mpi but is
372 * tested for robustness).
373 */
374extern unsigned mbedtls_test_case_uses_negative_0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200375#endif /* MBEDTLS_BIGNUM_C */
376
Ronald Cronb6d6d4c2020-06-03 10:11:18 +0200377#endif /* TEST_HELPERS_H */