1*62c56f98SSadaf Ebrahimi /** 2*62c56f98SSadaf Ebrahimi * \file helpers.h 3*62c56f98SSadaf Ebrahimi * 4*62c56f98SSadaf Ebrahimi * \brief This file contains the prototypes of helper functions for the 5*62c56f98SSadaf Ebrahimi * purpose of testing. 6*62c56f98SSadaf Ebrahimi */ 7*62c56f98SSadaf Ebrahimi 8*62c56f98SSadaf Ebrahimi /* 9*62c56f98SSadaf Ebrahimi * Copyright The Mbed TLS Contributors 10*62c56f98SSadaf Ebrahimi * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 11*62c56f98SSadaf Ebrahimi */ 12*62c56f98SSadaf Ebrahimi 13*62c56f98SSadaf Ebrahimi #ifndef TEST_HELPERS_H 14*62c56f98SSadaf Ebrahimi #define TEST_HELPERS_H 15*62c56f98SSadaf Ebrahimi 16*62c56f98SSadaf Ebrahimi /* Most fields of publicly available structs are private and are wrapped with 17*62c56f98SSadaf Ebrahimi * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields 18*62c56f98SSadaf Ebrahimi * directly (without using the MBEDTLS_PRIVATE wrapper). */ 19*62c56f98SSadaf Ebrahimi #define MBEDTLS_ALLOW_PRIVATE_ACCESS 20*62c56f98SSadaf Ebrahimi 21*62c56f98SSadaf Ebrahimi #include "mbedtls/build_info.h" 22*62c56f98SSadaf Ebrahimi 23*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \ 24*62c56f98SSadaf Ebrahimi defined(MBEDTLS_TEST_HOOKS) 25*62c56f98SSadaf Ebrahimi #define MBEDTLS_TEST_MUTEX_USAGE 26*62c56f98SSadaf Ebrahimi #endif 27*62c56f98SSadaf Ebrahimi 28*62c56f98SSadaf Ebrahimi #include "mbedtls/platform.h" 29*62c56f98SSadaf Ebrahimi 30*62c56f98SSadaf Ebrahimi #include <stddef.h> 31*62c56f98SSadaf Ebrahimi #include <stdint.h> 32*62c56f98SSadaf Ebrahimi 33*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_BIGNUM_C) 34*62c56f98SSadaf Ebrahimi #include "mbedtls/bignum.h" 35*62c56f98SSadaf Ebrahimi #endif 36*62c56f98SSadaf Ebrahimi 37*62c56f98SSadaf Ebrahimi /** The type of test case arguments that contain binary data. */ 38*62c56f98SSadaf Ebrahimi typedef struct data_tag { 39*62c56f98SSadaf Ebrahimi uint8_t *x; 40*62c56f98SSadaf Ebrahimi uint32_t len; 41*62c56f98SSadaf Ebrahimi } data_t; 42*62c56f98SSadaf Ebrahimi 43*62c56f98SSadaf Ebrahimi typedef enum { 44*62c56f98SSadaf Ebrahimi MBEDTLS_TEST_RESULT_SUCCESS = 0, 45*62c56f98SSadaf Ebrahimi MBEDTLS_TEST_RESULT_FAILED, 46*62c56f98SSadaf Ebrahimi MBEDTLS_TEST_RESULT_SKIPPED 47*62c56f98SSadaf Ebrahimi } mbedtls_test_result_t; 48*62c56f98SSadaf Ebrahimi 49*62c56f98SSadaf Ebrahimi typedef struct { 50*62c56f98SSadaf Ebrahimi mbedtls_test_result_t result; 51*62c56f98SSadaf Ebrahimi const char *test; 52*62c56f98SSadaf Ebrahimi const char *filename; 53*62c56f98SSadaf Ebrahimi int line_no; 54*62c56f98SSadaf Ebrahimi unsigned long step; 55*62c56f98SSadaf Ebrahimi char line1[76]; 56*62c56f98SSadaf Ebrahimi char line2[76]; 57*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_TEST_MUTEX_USAGE) 58*62c56f98SSadaf Ebrahimi const char *mutex_usage_error; 59*62c56f98SSadaf Ebrahimi #endif 60*62c56f98SSadaf Ebrahimi } 61*62c56f98SSadaf Ebrahimi mbedtls_test_info_t; 62*62c56f98SSadaf Ebrahimi extern mbedtls_test_info_t mbedtls_test_info; 63*62c56f98SSadaf Ebrahimi 64*62c56f98SSadaf Ebrahimi int mbedtls_test_platform_setup(void); 65*62c56f98SSadaf Ebrahimi void mbedtls_test_platform_teardown(void); 66*62c56f98SSadaf Ebrahimi 67*62c56f98SSadaf Ebrahimi /** 68*62c56f98SSadaf Ebrahimi * \brief Record the current test case as a failure. 69*62c56f98SSadaf Ebrahimi * 70*62c56f98SSadaf Ebrahimi * This function can be called directly however it is usually 71*62c56f98SSadaf Ebrahimi * called via macros such as TEST_ASSERT, TEST_EQUAL, 72*62c56f98SSadaf Ebrahimi * PSA_ASSERT, etc... 73*62c56f98SSadaf Ebrahimi * 74*62c56f98SSadaf Ebrahimi * \note If the test case was already marked as failed, calling 75*62c56f98SSadaf Ebrahimi * `mbedtls_test_fail( )` again will not overwrite any 76*62c56f98SSadaf Ebrahimi * previous information about the failure. 77*62c56f98SSadaf Ebrahimi * 78*62c56f98SSadaf Ebrahimi * \param test Description of the failure or assertion that failed. This 79*62c56f98SSadaf Ebrahimi * MUST be a string literal. 80*62c56f98SSadaf Ebrahimi * \param line_no Line number where the failure originated. 81*62c56f98SSadaf Ebrahimi * \param filename Filename where the failure originated. 82*62c56f98SSadaf Ebrahimi */ 83*62c56f98SSadaf Ebrahimi void mbedtls_test_fail(const char *test, int line_no, const char *filename); 84*62c56f98SSadaf Ebrahimi 85*62c56f98SSadaf Ebrahimi /** 86*62c56f98SSadaf Ebrahimi * \brief Record the current test case as skipped. 87*62c56f98SSadaf Ebrahimi * 88*62c56f98SSadaf Ebrahimi * This function can be called directly however it is usually 89*62c56f98SSadaf Ebrahimi * called via the TEST_ASSUME macro. 90*62c56f98SSadaf Ebrahimi * 91*62c56f98SSadaf Ebrahimi * \param test Description of the assumption that caused the test case to 92*62c56f98SSadaf Ebrahimi * be skipped. This MUST be a string literal. 93*62c56f98SSadaf Ebrahimi * \param line_no Line number where the test case was skipped. 94*62c56f98SSadaf Ebrahimi * \param filename Filename where the test case was skipped. 95*62c56f98SSadaf Ebrahimi */ 96*62c56f98SSadaf Ebrahimi void mbedtls_test_skip(const char *test, int line_no, const char *filename); 97*62c56f98SSadaf Ebrahimi 98*62c56f98SSadaf Ebrahimi /** 99*62c56f98SSadaf Ebrahimi * \brief Set the test step number for failure reports. 100*62c56f98SSadaf Ebrahimi * 101*62c56f98SSadaf Ebrahimi * Call this function to display "step NNN" in addition to the 102*62c56f98SSadaf Ebrahimi * line number and file name if a test fails. Typically the "step 103*62c56f98SSadaf Ebrahimi * number" is the index of a for loop but it can be whatever you 104*62c56f98SSadaf Ebrahimi * want. 105*62c56f98SSadaf Ebrahimi * 106*62c56f98SSadaf Ebrahimi * \param step The step number to report. 107*62c56f98SSadaf Ebrahimi */ 108*62c56f98SSadaf Ebrahimi void mbedtls_test_set_step(unsigned long step); 109*62c56f98SSadaf Ebrahimi 110*62c56f98SSadaf Ebrahimi /** 111*62c56f98SSadaf Ebrahimi * \brief Reset mbedtls_test_info to a ready/starting state. 112*62c56f98SSadaf Ebrahimi */ 113*62c56f98SSadaf Ebrahimi void mbedtls_test_info_reset(void); 114*62c56f98SSadaf Ebrahimi 115*62c56f98SSadaf Ebrahimi /** 116*62c56f98SSadaf Ebrahimi * \brief Record the current test case as a failure if two integers 117*62c56f98SSadaf Ebrahimi * have a different value. 118*62c56f98SSadaf Ebrahimi * 119*62c56f98SSadaf Ebrahimi * This function is usually called via the macro 120*62c56f98SSadaf Ebrahimi * #TEST_EQUAL. 121*62c56f98SSadaf Ebrahimi * 122*62c56f98SSadaf Ebrahimi * \param test Description of the failure or assertion that failed. This 123*62c56f98SSadaf Ebrahimi * MUST be a string literal. This normally has the form 124*62c56f98SSadaf Ebrahimi * "EXPR1 == EXPR2" where EXPR1 has the value \p value1 125*62c56f98SSadaf Ebrahimi * and EXPR2 has the value \p value2. 126*62c56f98SSadaf Ebrahimi * \param line_no Line number where the failure originated. 127*62c56f98SSadaf Ebrahimi * \param filename Filename where the failure originated. 128*62c56f98SSadaf Ebrahimi * \param value1 The first value to compare. 129*62c56f98SSadaf Ebrahimi * \param value2 The second value to compare. 130*62c56f98SSadaf Ebrahimi * 131*62c56f98SSadaf Ebrahimi * \return \c 1 if the values are equal, otherwise \c 0. 132*62c56f98SSadaf Ebrahimi */ 133*62c56f98SSadaf Ebrahimi int mbedtls_test_equal(const char *test, int line_no, const char *filename, 134*62c56f98SSadaf Ebrahimi unsigned long long value1, unsigned long long value2); 135*62c56f98SSadaf Ebrahimi 136*62c56f98SSadaf Ebrahimi /** 137*62c56f98SSadaf Ebrahimi * \brief Record the current test case as a failure based 138*62c56f98SSadaf Ebrahimi * on comparing two unsigned integers. 139*62c56f98SSadaf Ebrahimi * 140*62c56f98SSadaf Ebrahimi * This function is usually called via the macro 141*62c56f98SSadaf Ebrahimi * #TEST_LE_U. 142*62c56f98SSadaf Ebrahimi * 143*62c56f98SSadaf Ebrahimi * \param test Description of the failure or assertion that failed. This 144*62c56f98SSadaf Ebrahimi * MUST be a string literal. This normally has the form 145*62c56f98SSadaf Ebrahimi * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1 146*62c56f98SSadaf Ebrahimi * and EXPR2 has the value \p value2. 147*62c56f98SSadaf Ebrahimi * \param line_no Line number where the failure originated. 148*62c56f98SSadaf Ebrahimi * \param filename Filename where the failure originated. 149*62c56f98SSadaf Ebrahimi * \param value1 The first value to compare. 150*62c56f98SSadaf Ebrahimi * \param value2 The second value to compare. 151*62c56f98SSadaf Ebrahimi * 152*62c56f98SSadaf Ebrahimi * \return \c 1 if \p value1 <= \p value2, otherwise \c 0. 153*62c56f98SSadaf Ebrahimi */ 154*62c56f98SSadaf Ebrahimi int mbedtls_test_le_u(const char *test, int line_no, const char *filename, 155*62c56f98SSadaf Ebrahimi unsigned long long value1, unsigned long long value2); 156*62c56f98SSadaf Ebrahimi 157*62c56f98SSadaf Ebrahimi /** 158*62c56f98SSadaf Ebrahimi * \brief Record the current test case as a failure based 159*62c56f98SSadaf Ebrahimi * on comparing two signed integers. 160*62c56f98SSadaf Ebrahimi * 161*62c56f98SSadaf Ebrahimi * This function is usually called via the macro 162*62c56f98SSadaf Ebrahimi * #TEST_LE_S. 163*62c56f98SSadaf Ebrahimi * 164*62c56f98SSadaf Ebrahimi * \param test Description of the failure or assertion that failed. This 165*62c56f98SSadaf Ebrahimi * MUST be a string literal. This normally has the form 166*62c56f98SSadaf Ebrahimi * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1 167*62c56f98SSadaf Ebrahimi * and EXPR2 has the value \p value2. 168*62c56f98SSadaf Ebrahimi * \param line_no Line number where the failure originated. 169*62c56f98SSadaf Ebrahimi * \param filename Filename where the failure originated. 170*62c56f98SSadaf Ebrahimi * \param value1 The first value to compare. 171*62c56f98SSadaf Ebrahimi * \param value2 The second value to compare. 172*62c56f98SSadaf Ebrahimi * 173*62c56f98SSadaf Ebrahimi * \return \c 1 if \p value1 <= \p value2, otherwise \c 0. 174*62c56f98SSadaf Ebrahimi */ 175*62c56f98SSadaf Ebrahimi int mbedtls_test_le_s(const char *test, int line_no, const char *filename, 176*62c56f98SSadaf Ebrahimi long long value1, long long value2); 177*62c56f98SSadaf Ebrahimi 178*62c56f98SSadaf Ebrahimi /** 179*62c56f98SSadaf Ebrahimi * \brief This function decodes the hexadecimal representation of 180*62c56f98SSadaf Ebrahimi * data. 181*62c56f98SSadaf Ebrahimi * 182*62c56f98SSadaf Ebrahimi * \note The output buffer can be the same as the input buffer. For 183*62c56f98SSadaf Ebrahimi * any other overlapping of the input and output buffers, the 184*62c56f98SSadaf Ebrahimi * behavior is undefined. 185*62c56f98SSadaf Ebrahimi * 186*62c56f98SSadaf Ebrahimi * \param obuf Output buffer. 187*62c56f98SSadaf Ebrahimi * \param obufmax Size in number of bytes of \p obuf. 188*62c56f98SSadaf Ebrahimi * \param ibuf Input buffer. 189*62c56f98SSadaf Ebrahimi * \param len The number of unsigned char written in \p obuf. This must 190*62c56f98SSadaf Ebrahimi * not be \c NULL. 191*62c56f98SSadaf Ebrahimi * 192*62c56f98SSadaf Ebrahimi * \return \c 0 on success. 193*62c56f98SSadaf Ebrahimi * \return \c -1 if the output buffer is too small or the input string 194*62c56f98SSadaf Ebrahimi * is not a valid hexadecimal representation. 195*62c56f98SSadaf Ebrahimi */ 196*62c56f98SSadaf Ebrahimi int mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax, 197*62c56f98SSadaf Ebrahimi const char *ibuf, size_t *len); 198*62c56f98SSadaf Ebrahimi 199*62c56f98SSadaf Ebrahimi void mbedtls_test_hexify(unsigned char *obuf, 200*62c56f98SSadaf Ebrahimi const unsigned char *ibuf, 201*62c56f98SSadaf Ebrahimi int len); 202*62c56f98SSadaf Ebrahimi 203*62c56f98SSadaf Ebrahimi /** 204*62c56f98SSadaf Ebrahimi * \brief Convert hexadecimal digit to an integer. 205*62c56f98SSadaf Ebrahimi * 206*62c56f98SSadaf Ebrahimi * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or 207*62c56f98SSadaf Ebrahimi * `'a'` to `'f'`). 208*62c56f98SSadaf Ebrahimi * \param[out] uc On success, the value of the digit (0 to 15). 209*62c56f98SSadaf Ebrahimi * 210*62c56f98SSadaf Ebrahimi * \return 0 on success, -1 if \p c is not a hexadecimal digit. 211*62c56f98SSadaf Ebrahimi */ 212*62c56f98SSadaf Ebrahimi int mbedtls_test_ascii2uc(const char c, unsigned char *uc); 213*62c56f98SSadaf Ebrahimi 214*62c56f98SSadaf Ebrahimi /** 215*62c56f98SSadaf Ebrahimi * Allocate and zeroize a buffer. 216*62c56f98SSadaf Ebrahimi * 217*62c56f98SSadaf Ebrahimi * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. 218*62c56f98SSadaf Ebrahimi * 219*62c56f98SSadaf Ebrahimi * For convenience, dies if allocation fails. 220*62c56f98SSadaf Ebrahimi */ 221*62c56f98SSadaf Ebrahimi unsigned char *mbedtls_test_zero_alloc(size_t len); 222*62c56f98SSadaf Ebrahimi 223*62c56f98SSadaf Ebrahimi /** 224*62c56f98SSadaf Ebrahimi * Allocate and fill a buffer from hex data. 225*62c56f98SSadaf Ebrahimi * 226*62c56f98SSadaf Ebrahimi * The buffer is sized exactly as needed. This allows to detect buffer 227*62c56f98SSadaf Ebrahimi * overruns (including overreads) when running the test suite under valgrind. 228*62c56f98SSadaf Ebrahimi * 229*62c56f98SSadaf Ebrahimi * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. 230*62c56f98SSadaf Ebrahimi * 231*62c56f98SSadaf Ebrahimi * For convenience, dies if allocation fails. 232*62c56f98SSadaf Ebrahimi */ 233*62c56f98SSadaf Ebrahimi unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen); 234*62c56f98SSadaf Ebrahimi 235*62c56f98SSadaf Ebrahimi int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, 236*62c56f98SSadaf Ebrahimi uint32_t a_len, uint32_t b_len); 237*62c56f98SSadaf Ebrahimi 238*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) 239*62c56f98SSadaf Ebrahimi #include "test/fake_external_rng_for_test.h" 240*62c56f98SSadaf Ebrahimi #endif 241*62c56f98SSadaf Ebrahimi 242*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_TEST_MUTEX_USAGE) 243*62c56f98SSadaf Ebrahimi /** Permanently activate the mutex usage verification framework. See 244*62c56f98SSadaf Ebrahimi * threading_helpers.c for information. */ 245*62c56f98SSadaf Ebrahimi void mbedtls_test_mutex_usage_init(void); 246*62c56f98SSadaf Ebrahimi 247*62c56f98SSadaf Ebrahimi /** Call this function after executing a test case to check for mutex usage 248*62c56f98SSadaf Ebrahimi * errors. */ 249*62c56f98SSadaf Ebrahimi void mbedtls_test_mutex_usage_check(void); 250*62c56f98SSadaf Ebrahimi #endif /* MBEDTLS_TEST_MUTEX_USAGE */ 251*62c56f98SSadaf Ebrahimi 252*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_TEST_HOOKS) 253*62c56f98SSadaf Ebrahimi /** 254*62c56f98SSadaf Ebrahimi * \brief Check that only a pure high-level error code is being combined with 255*62c56f98SSadaf Ebrahimi * a pure low-level error code as otherwise the resultant error code 256*62c56f98SSadaf Ebrahimi * would be corrupted. 257*62c56f98SSadaf Ebrahimi * 258*62c56f98SSadaf Ebrahimi * \note Both high-level and low-level error codes cannot be greater than 259*62c56f98SSadaf Ebrahimi * zero however can be zero. If one error code is zero then the 260*62c56f98SSadaf Ebrahimi * other error code is returned even if both codes are zero. 261*62c56f98SSadaf Ebrahimi * 262*62c56f98SSadaf Ebrahimi * \note If the check fails, fail the test currently being run. 263*62c56f98SSadaf Ebrahimi */ 264*62c56f98SSadaf Ebrahimi void mbedtls_test_err_add_check(int high, int low, 265*62c56f98SSadaf Ebrahimi const char *file, int line); 266*62c56f98SSadaf Ebrahimi #endif 267*62c56f98SSadaf Ebrahimi 268*62c56f98SSadaf Ebrahimi #endif /* TEST_HELPERS_H */ 269