1a8e1175bSopenharmony_ci/** 2a8e1175bSopenharmony_ci * \file helpers.h 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * \brief This file contains the prototypes of helper functions for the 5a8e1175bSopenharmony_ci * purpose of testing. 6a8e1175bSopenharmony_ci */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci/* 9a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 10a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 11a8e1175bSopenharmony_ci */ 12a8e1175bSopenharmony_ci 13a8e1175bSopenharmony_ci#ifndef TEST_HELPERS_H 14a8e1175bSopenharmony_ci#define TEST_HELPERS_H 15a8e1175bSopenharmony_ci 16a8e1175bSopenharmony_ci/* Most fields of publicly available structs are private and are wrapped with 17a8e1175bSopenharmony_ci * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields 18a8e1175bSopenharmony_ci * directly (without using the MBEDTLS_PRIVATE wrapper). */ 19a8e1175bSopenharmony_ci#define MBEDTLS_ALLOW_PRIVATE_ACCESS 20a8e1175bSopenharmony_ci 21a8e1175bSopenharmony_ci#include "mbedtls/build_info.h" 22a8e1175bSopenharmony_ci 23a8e1175bSopenharmony_ci#if defined(__SANITIZE_ADDRESS__) /* gcc -fsanitize=address */ 24a8e1175bSopenharmony_ci# define MBEDTLS_TEST_HAVE_ASAN 25a8e1175bSopenharmony_ci#endif 26a8e1175bSopenharmony_ci#if defined(__SANITIZE_THREAD__) /* gcc -fsanitize-thread */ 27a8e1175bSopenharmony_ci# define MBEDTLS_TEST_HAVE_TSAN 28a8e1175bSopenharmony_ci#endif 29a8e1175bSopenharmony_ci 30a8e1175bSopenharmony_ci#if defined(__has_feature) 31a8e1175bSopenharmony_ci# if __has_feature(address_sanitizer) /* clang -fsanitize=address */ 32a8e1175bSopenharmony_ci# define MBEDTLS_TEST_HAVE_ASAN 33a8e1175bSopenharmony_ci# endif 34a8e1175bSopenharmony_ci# if __has_feature(memory_sanitizer) /* clang -fsanitize=memory */ 35a8e1175bSopenharmony_ci# define MBEDTLS_TEST_HAVE_MSAN 36a8e1175bSopenharmony_ci# endif 37a8e1175bSopenharmony_ci# if __has_feature(thread_sanitizer) /* clang -fsanitize=thread */ 38a8e1175bSopenharmony_ci# define MBEDTLS_TEST_HAVE_TSAN 39a8e1175bSopenharmony_ci# endif 40a8e1175bSopenharmony_ci#endif 41a8e1175bSopenharmony_ci 42a8e1175bSopenharmony_ci#include "test/threading_helpers.h" 43a8e1175bSopenharmony_ci 44a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_MUTEX_USAGE) 45a8e1175bSopenharmony_ci#include "mbedtls/threading.h" 46a8e1175bSopenharmony_ci#endif 47a8e1175bSopenharmony_ci 48a8e1175bSopenharmony_ci#include "mbedtls/platform.h" 49a8e1175bSopenharmony_ci 50a8e1175bSopenharmony_ci#include <stddef.h> 51a8e1175bSopenharmony_ci#include <stdint.h> 52a8e1175bSopenharmony_ci 53a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) 54a8e1175bSopenharmony_ci#include "mbedtls/bignum.h" 55a8e1175bSopenharmony_ci#endif 56a8e1175bSopenharmony_ci 57a8e1175bSopenharmony_ci/** The type of test case arguments that contain binary data. */ 58a8e1175bSopenharmony_citypedef struct data_tag { 59a8e1175bSopenharmony_ci uint8_t *x; 60a8e1175bSopenharmony_ci uint32_t len; 61a8e1175bSopenharmony_ci} data_t; 62a8e1175bSopenharmony_ci 63a8e1175bSopenharmony_citypedef enum { 64a8e1175bSopenharmony_ci MBEDTLS_TEST_RESULT_SUCCESS = 0, 65a8e1175bSopenharmony_ci MBEDTLS_TEST_RESULT_FAILED, 66a8e1175bSopenharmony_ci MBEDTLS_TEST_RESULT_SKIPPED 67a8e1175bSopenharmony_ci} mbedtls_test_result_t; 68a8e1175bSopenharmony_ci 69a8e1175bSopenharmony_ci#define MBEDTLS_TEST_LINE_LENGTH 76 70a8e1175bSopenharmony_ci 71a8e1175bSopenharmony_citypedef struct { 72a8e1175bSopenharmony_ci mbedtls_test_result_t result; 73a8e1175bSopenharmony_ci const char *test; 74a8e1175bSopenharmony_ci const char *filename; 75a8e1175bSopenharmony_ci int line_no; 76a8e1175bSopenharmony_ci unsigned long step; 77a8e1175bSopenharmony_ci char line1[MBEDTLS_TEST_LINE_LENGTH]; 78a8e1175bSopenharmony_ci char line2[MBEDTLS_TEST_LINE_LENGTH]; 79a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_MUTEX_USAGE) 80a8e1175bSopenharmony_ci const char *mutex_usage_error; 81a8e1175bSopenharmony_ci#endif 82a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) 83a8e1175bSopenharmony_ci unsigned case_uses_negative_0; 84a8e1175bSopenharmony_ci#endif 85a8e1175bSopenharmony_ci} 86a8e1175bSopenharmony_cimbedtls_test_info_t; 87a8e1175bSopenharmony_ci 88a8e1175bSopenharmony_ci/** 89a8e1175bSopenharmony_ci * \brief Get the current test result status 90a8e1175bSopenharmony_ci * 91a8e1175bSopenharmony_ci * \return The current test result status 92a8e1175bSopenharmony_ci */ 93a8e1175bSopenharmony_cimbedtls_test_result_t mbedtls_test_get_result(void); 94a8e1175bSopenharmony_ci 95a8e1175bSopenharmony_ci/** 96a8e1175bSopenharmony_ci * \brief Get the current test name/description 97a8e1175bSopenharmony_ci * 98a8e1175bSopenharmony_ci * \return The current test name/description 99a8e1175bSopenharmony_ci */ 100a8e1175bSopenharmony_ciconst char *mbedtls_test_get_test(void); 101a8e1175bSopenharmony_ci 102a8e1175bSopenharmony_ci/** 103a8e1175bSopenharmony_ci * \brief Get the current test filename 104a8e1175bSopenharmony_ci * 105a8e1175bSopenharmony_ci * \return The current test filename 106a8e1175bSopenharmony_ci */ 107a8e1175bSopenharmony_ciconst char *mbedtls_get_test_filename(void); 108a8e1175bSopenharmony_ci 109a8e1175bSopenharmony_ci/** 110a8e1175bSopenharmony_ci * \brief Get the current test file line number (for failure / skip) 111a8e1175bSopenharmony_ci * 112a8e1175bSopenharmony_ci * \return The current test file line number (for failure / skip) 113a8e1175bSopenharmony_ci */ 114a8e1175bSopenharmony_ciint mbedtls_test_get_line_no(void); 115a8e1175bSopenharmony_ci 116a8e1175bSopenharmony_ci/** 117a8e1175bSopenharmony_ci * \brief Increment the current test step. 118a8e1175bSopenharmony_ci * 119a8e1175bSopenharmony_ci * \note It is not recommended for multiple threads to call this 120a8e1175bSopenharmony_ci * function concurrently - whilst it is entirely thread safe, 121a8e1175bSopenharmony_ci * the order of calls to this function can obviously not be 122a8e1175bSopenharmony_ci * ensured, so unexpected results may occur. 123a8e1175bSopenharmony_ci */ 124a8e1175bSopenharmony_civoid mbedtls_test_increment_step(void); 125a8e1175bSopenharmony_ci 126a8e1175bSopenharmony_ci/** 127a8e1175bSopenharmony_ci * \brief Get the current test step 128a8e1175bSopenharmony_ci * 129a8e1175bSopenharmony_ci * \return The current test step 130a8e1175bSopenharmony_ci */ 131a8e1175bSopenharmony_ciunsigned long mbedtls_test_get_step(void); 132a8e1175bSopenharmony_ci 133a8e1175bSopenharmony_ci/** 134a8e1175bSopenharmony_ci * \brief Get the current test line buffer 1 135a8e1175bSopenharmony_ci * 136a8e1175bSopenharmony_ci * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH, 137a8e1175bSopenharmony_ci * which will have line buffer 1 copied to it. 138a8e1175bSopenharmony_ci */ 139a8e1175bSopenharmony_civoid mbedtls_test_get_line1(char *line); 140a8e1175bSopenharmony_ci 141a8e1175bSopenharmony_ci/** 142a8e1175bSopenharmony_ci * \brief Get the current test line buffer 2 143a8e1175bSopenharmony_ci * 144a8e1175bSopenharmony_ci * \param line Buffer of minimum size \c MBEDTLS_TEST_LINE_LENGTH, 145a8e1175bSopenharmony_ci * which will have line buffer 1 copied to it. 146a8e1175bSopenharmony_ci */ 147a8e1175bSopenharmony_civoid mbedtls_test_get_line2(char *line); 148a8e1175bSopenharmony_ci 149a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_MUTEX_USAGE) 150a8e1175bSopenharmony_ci/** 151a8e1175bSopenharmony_ci * \brief Get the current mutex usage error message 152a8e1175bSopenharmony_ci * 153a8e1175bSopenharmony_ci * \return The current mutex error message (may be NULL if no error) 154a8e1175bSopenharmony_ci */ 155a8e1175bSopenharmony_ciconst char *mbedtls_test_get_mutex_usage_error(void); 156a8e1175bSopenharmony_ci 157a8e1175bSopenharmony_ci/** 158a8e1175bSopenharmony_ci * \brief Set the current mutex usage error message 159a8e1175bSopenharmony_ci * 160a8e1175bSopenharmony_ci * \note This will only set the mutex error message if one has not 161a8e1175bSopenharmony_ci * already been set, or if we are clearing the message (msg is 162a8e1175bSopenharmony_ci * NULL) 163a8e1175bSopenharmony_ci * 164a8e1175bSopenharmony_ci * \param msg Error message to set (can be NULL to clear) 165a8e1175bSopenharmony_ci */ 166a8e1175bSopenharmony_civoid mbedtls_test_set_mutex_usage_error(const char *msg); 167a8e1175bSopenharmony_ci#endif 168a8e1175bSopenharmony_ci 169a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) 170a8e1175bSopenharmony_ci 171a8e1175bSopenharmony_ci/** 172a8e1175bSopenharmony_ci * \brief Get whether the current test is a bignum test that uses 173a8e1175bSopenharmony_ci * negative zero. 174a8e1175bSopenharmony_ci * 175a8e1175bSopenharmony_ci * \return non zero if the current test uses bignum negative zero. 176a8e1175bSopenharmony_ci */ 177a8e1175bSopenharmony_ciunsigned mbedtls_test_get_case_uses_negative_0(void); 178a8e1175bSopenharmony_ci 179a8e1175bSopenharmony_ci/** 180a8e1175bSopenharmony_ci * \brief Indicate that the current test uses bignum negative zero. 181a8e1175bSopenharmony_ci * 182a8e1175bSopenharmony_ci * \note This function is called if the current test case had an 183a8e1175bSopenharmony_ci * input parsed with mbedtls_test_read_mpi() that is a negative 184a8e1175bSopenharmony_ci * 0 (`"-"`, `"-0"`, `"-00"`, etc., constructing a result with 185a8e1175bSopenharmony_ci * the sign bit set to -1 and the value being all-limbs-0, 186a8e1175bSopenharmony_ci * which is not a valid representation in #mbedtls_mpi but is 187a8e1175bSopenharmony_ci * tested for robustness). * 188a8e1175bSopenharmony_ci */ 189a8e1175bSopenharmony_civoid mbedtls_test_increment_case_uses_negative_0(void); 190a8e1175bSopenharmony_ci#endif 191a8e1175bSopenharmony_ci 192a8e1175bSopenharmony_ciint mbedtls_test_platform_setup(void); 193a8e1175bSopenharmony_civoid mbedtls_test_platform_teardown(void); 194a8e1175bSopenharmony_ci 195a8e1175bSopenharmony_ci/** 196a8e1175bSopenharmony_ci * \brief Record the current test case as a failure. 197a8e1175bSopenharmony_ci * 198a8e1175bSopenharmony_ci * This function can be called directly however it is usually 199a8e1175bSopenharmony_ci * called via macros such as TEST_ASSERT, TEST_EQUAL, 200a8e1175bSopenharmony_ci * PSA_ASSERT, etc... 201a8e1175bSopenharmony_ci * 202a8e1175bSopenharmony_ci * \note If the test case was already marked as failed, calling 203a8e1175bSopenharmony_ci * `mbedtls_test_fail( )` again will not overwrite any 204a8e1175bSopenharmony_ci * previous information about the failure. 205a8e1175bSopenharmony_ci * 206a8e1175bSopenharmony_ci * \param test Description of the failure or assertion that failed. This 207a8e1175bSopenharmony_ci * MUST be a string literal. 208a8e1175bSopenharmony_ci * \param line_no Line number where the failure originated. 209a8e1175bSopenharmony_ci * \param filename Filename where the failure originated. 210a8e1175bSopenharmony_ci */ 211a8e1175bSopenharmony_civoid mbedtls_test_fail(const char *test, int line_no, const char *filename); 212a8e1175bSopenharmony_ci 213a8e1175bSopenharmony_ci/** 214a8e1175bSopenharmony_ci * \brief Record the current test case as skipped. 215a8e1175bSopenharmony_ci * 216a8e1175bSopenharmony_ci * This function can be called directly however it is usually 217a8e1175bSopenharmony_ci * called via the TEST_ASSUME macro. 218a8e1175bSopenharmony_ci * 219a8e1175bSopenharmony_ci * \param test Description of the assumption that caused the test case to 220a8e1175bSopenharmony_ci * be skipped. This MUST be a string literal. 221a8e1175bSopenharmony_ci * \param line_no Line number where the test case was skipped. 222a8e1175bSopenharmony_ci * \param filename Filename where the test case was skipped. 223a8e1175bSopenharmony_ci */ 224a8e1175bSopenharmony_civoid mbedtls_test_skip(const char *test, int line_no, const char *filename); 225a8e1175bSopenharmony_ci 226a8e1175bSopenharmony_ci/** 227a8e1175bSopenharmony_ci * \brief Set the test step number for failure reports. 228a8e1175bSopenharmony_ci * 229a8e1175bSopenharmony_ci * Call this function to display "step NNN" in addition to the 230a8e1175bSopenharmony_ci * line number and file name if a test fails. Typically the 231a8e1175bSopenharmony_ci * "step number" is the index of a for loop but it can be 232a8e1175bSopenharmony_ci * whatever you want. 233a8e1175bSopenharmony_ci * 234a8e1175bSopenharmony_ci * \note It is not recommended for multiple threads to call this 235a8e1175bSopenharmony_ci * function concurrently - whilst it is entirely thread safe, 236a8e1175bSopenharmony_ci * the order of calls to this function can obviously not be 237a8e1175bSopenharmony_ci * ensured, so unexpected results may occur. 238a8e1175bSopenharmony_ci * 239a8e1175bSopenharmony_ci * \param step The step number to report. 240a8e1175bSopenharmony_ci */ 241a8e1175bSopenharmony_civoid mbedtls_test_set_step(unsigned long step); 242a8e1175bSopenharmony_ci 243a8e1175bSopenharmony_ci/** 244a8e1175bSopenharmony_ci * \brief Reset mbedtls_test_info to a ready/starting state. 245a8e1175bSopenharmony_ci */ 246a8e1175bSopenharmony_civoid mbedtls_test_info_reset(void); 247a8e1175bSopenharmony_ci 248a8e1175bSopenharmony_ci#ifdef MBEDTLS_TEST_MUTEX_USAGE 249a8e1175bSopenharmony_ci/** 250a8e1175bSopenharmony_ci * \brief Get the test info data mutex. 251a8e1175bSopenharmony_ci * 252a8e1175bSopenharmony_ci * \note This is designed only to be used by threading_helpers to 253a8e1175bSopenharmony_ci * avoid a deadlock, not for general access to this mutex. 254a8e1175bSopenharmony_ci * 255a8e1175bSopenharmony_ci * \return The test info data mutex. 256a8e1175bSopenharmony_ci */ 257a8e1175bSopenharmony_cimbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void); 258a8e1175bSopenharmony_ci 259a8e1175bSopenharmony_ci#endif /* MBEDTLS_TEST_MUTEX_USAGE */ 260a8e1175bSopenharmony_ci 261a8e1175bSopenharmony_ci/** 262a8e1175bSopenharmony_ci * \brief Record the current test case as a failure if two integers 263a8e1175bSopenharmony_ci * have a different value. 264a8e1175bSopenharmony_ci * 265a8e1175bSopenharmony_ci * This function is usually called via the macro 266a8e1175bSopenharmony_ci * #TEST_EQUAL. 267a8e1175bSopenharmony_ci * 268a8e1175bSopenharmony_ci * \param test Description of the failure or assertion that failed. This 269a8e1175bSopenharmony_ci * MUST be a string literal. This normally has the form 270a8e1175bSopenharmony_ci * "EXPR1 == EXPR2" where EXPR1 has the value \p value1 271a8e1175bSopenharmony_ci * and EXPR2 has the value \p value2. 272a8e1175bSopenharmony_ci * \param line_no Line number where the failure originated. 273a8e1175bSopenharmony_ci * \param filename Filename where the failure originated. 274a8e1175bSopenharmony_ci * \param value1 The first value to compare. 275a8e1175bSopenharmony_ci * \param value2 The second value to compare. 276a8e1175bSopenharmony_ci * 277a8e1175bSopenharmony_ci * \return \c 1 if the values are equal, otherwise \c 0. 278a8e1175bSopenharmony_ci */ 279a8e1175bSopenharmony_ciint mbedtls_test_equal(const char *test, int line_no, const char *filename, 280a8e1175bSopenharmony_ci unsigned long long value1, unsigned long long value2); 281a8e1175bSopenharmony_ci 282a8e1175bSopenharmony_ci/** 283a8e1175bSopenharmony_ci * \brief Record the current test case as a failure based 284a8e1175bSopenharmony_ci * on comparing two unsigned integers. 285a8e1175bSopenharmony_ci * 286a8e1175bSopenharmony_ci * This function is usually called via the macro 287a8e1175bSopenharmony_ci * #TEST_LE_U. 288a8e1175bSopenharmony_ci * 289a8e1175bSopenharmony_ci * \param test Description of the failure or assertion that failed. This 290a8e1175bSopenharmony_ci * MUST be a string literal. This normally has the form 291a8e1175bSopenharmony_ci * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1 292a8e1175bSopenharmony_ci * and EXPR2 has the value \p value2. 293a8e1175bSopenharmony_ci * \param line_no Line number where the failure originated. 294a8e1175bSopenharmony_ci * \param filename Filename where the failure originated. 295a8e1175bSopenharmony_ci * \param value1 The first value to compare. 296a8e1175bSopenharmony_ci * \param value2 The second value to compare. 297a8e1175bSopenharmony_ci * 298a8e1175bSopenharmony_ci * \return \c 1 if \p value1 <= \p value2, otherwise \c 0. 299a8e1175bSopenharmony_ci */ 300a8e1175bSopenharmony_ciint mbedtls_test_le_u(const char *test, int line_no, const char *filename, 301a8e1175bSopenharmony_ci unsigned long long value1, unsigned long long value2); 302a8e1175bSopenharmony_ci 303a8e1175bSopenharmony_ci/** 304a8e1175bSopenharmony_ci * \brief Record the current test case as a failure based 305a8e1175bSopenharmony_ci * on comparing two signed integers. 306a8e1175bSopenharmony_ci * 307a8e1175bSopenharmony_ci * This function is usually called via the macro 308a8e1175bSopenharmony_ci * #TEST_LE_S. 309a8e1175bSopenharmony_ci * 310a8e1175bSopenharmony_ci * \param test Description of the failure or assertion that failed. This 311a8e1175bSopenharmony_ci * MUST be a string literal. This normally has the form 312a8e1175bSopenharmony_ci * "EXPR1 <= EXPR2" where EXPR1 has the value \p value1 313a8e1175bSopenharmony_ci * and EXPR2 has the value \p value2. 314a8e1175bSopenharmony_ci * \param line_no Line number where the failure originated. 315a8e1175bSopenharmony_ci * \param filename Filename where the failure originated. 316a8e1175bSopenharmony_ci * \param value1 The first value to compare. 317a8e1175bSopenharmony_ci * \param value2 The second value to compare. 318a8e1175bSopenharmony_ci * 319a8e1175bSopenharmony_ci * \return \c 1 if \p value1 <= \p value2, otherwise \c 0. 320a8e1175bSopenharmony_ci */ 321a8e1175bSopenharmony_ciint mbedtls_test_le_s(const char *test, int line_no, const char *filename, 322a8e1175bSopenharmony_ci long long value1, long long value2); 323a8e1175bSopenharmony_ci 324a8e1175bSopenharmony_ci/** 325a8e1175bSopenharmony_ci * \brief This function decodes the hexadecimal representation of 326a8e1175bSopenharmony_ci * data. 327a8e1175bSopenharmony_ci * 328a8e1175bSopenharmony_ci * \note The output buffer can be the same as the input buffer. For 329a8e1175bSopenharmony_ci * any other overlapping of the input and output buffers, the 330a8e1175bSopenharmony_ci * behavior is undefined. 331a8e1175bSopenharmony_ci * 332a8e1175bSopenharmony_ci * \param obuf Output buffer. 333a8e1175bSopenharmony_ci * \param obufmax Size in number of bytes of \p obuf. 334a8e1175bSopenharmony_ci * \param ibuf Input buffer. 335a8e1175bSopenharmony_ci * \param len The number of unsigned char written in \p obuf. This must 336a8e1175bSopenharmony_ci * not be \c NULL. 337a8e1175bSopenharmony_ci * 338a8e1175bSopenharmony_ci * \return \c 0 on success. 339a8e1175bSopenharmony_ci * \return \c -1 if the output buffer is too small or the input string 340a8e1175bSopenharmony_ci * is not a valid hexadecimal representation. 341a8e1175bSopenharmony_ci */ 342a8e1175bSopenharmony_ciint mbedtls_test_unhexify(unsigned char *obuf, size_t obufmax, 343a8e1175bSopenharmony_ci const char *ibuf, size_t *len); 344a8e1175bSopenharmony_ci 345a8e1175bSopenharmony_civoid mbedtls_test_hexify(unsigned char *obuf, 346a8e1175bSopenharmony_ci const unsigned char *ibuf, 347a8e1175bSopenharmony_ci int len); 348a8e1175bSopenharmony_ci 349a8e1175bSopenharmony_ci/** 350a8e1175bSopenharmony_ci * \brief Convert hexadecimal digit to an integer. 351a8e1175bSopenharmony_ci * 352a8e1175bSopenharmony_ci * \param c The digit to convert (`'0'` to `'9'`, `'A'` to `'F'` or 353a8e1175bSopenharmony_ci * `'a'` to `'f'`). 354a8e1175bSopenharmony_ci * \param[out] uc On success, the value of the digit (0 to 15). 355a8e1175bSopenharmony_ci * 356a8e1175bSopenharmony_ci * \return 0 on success, -1 if \p c is not a hexadecimal digit. 357a8e1175bSopenharmony_ci */ 358a8e1175bSopenharmony_ciint mbedtls_test_ascii2uc(const char c, unsigned char *uc); 359a8e1175bSopenharmony_ci 360a8e1175bSopenharmony_ci/** 361a8e1175bSopenharmony_ci * Allocate and zeroize a buffer. 362a8e1175bSopenharmony_ci * 363a8e1175bSopenharmony_ci * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. 364a8e1175bSopenharmony_ci * 365a8e1175bSopenharmony_ci * For convenience, dies if allocation fails. 366a8e1175bSopenharmony_ci */ 367a8e1175bSopenharmony_ciunsigned char *mbedtls_test_zero_alloc(size_t len); 368a8e1175bSopenharmony_ci 369a8e1175bSopenharmony_ci/** 370a8e1175bSopenharmony_ci * Allocate and fill a buffer from hex data. 371a8e1175bSopenharmony_ci * 372a8e1175bSopenharmony_ci * The buffer is sized exactly as needed. This allows to detect buffer 373a8e1175bSopenharmony_ci * overruns (including overreads) when running the test suite under valgrind. 374a8e1175bSopenharmony_ci * 375a8e1175bSopenharmony_ci * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. 376a8e1175bSopenharmony_ci * 377a8e1175bSopenharmony_ci * For convenience, dies if allocation fails. 378a8e1175bSopenharmony_ci */ 379a8e1175bSopenharmony_ciunsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen); 380a8e1175bSopenharmony_ci 381a8e1175bSopenharmony_ciint mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, 382a8e1175bSopenharmony_ci uint32_t a_len, uint32_t b_len); 383a8e1175bSopenharmony_ci 384a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) 385a8e1175bSopenharmony_ci#include "test/fake_external_rng_for_test.h" 386a8e1175bSopenharmony_ci#endif 387a8e1175bSopenharmony_ci 388a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS) 389a8e1175bSopenharmony_ci/** 390a8e1175bSopenharmony_ci * \brief Check that only a pure high-level error code is being combined with 391a8e1175bSopenharmony_ci * a pure low-level error code as otherwise the resultant error code 392a8e1175bSopenharmony_ci * would be corrupted. 393a8e1175bSopenharmony_ci * 394a8e1175bSopenharmony_ci * \note Both high-level and low-level error codes cannot be greater than 395a8e1175bSopenharmony_ci * zero however can be zero. If one error code is zero then the 396a8e1175bSopenharmony_ci * other error code is returned even if both codes are zero. 397a8e1175bSopenharmony_ci * 398a8e1175bSopenharmony_ci * \note If the check fails, fail the test currently being run. 399a8e1175bSopenharmony_ci */ 400a8e1175bSopenharmony_civoid mbedtls_test_err_add_check(int high, int low, 401a8e1175bSopenharmony_ci const char *file, int line); 402a8e1175bSopenharmony_ci#endif 403a8e1175bSopenharmony_ci 404a8e1175bSopenharmony_ci#endif /* TEST_HELPERS_H */ 405