2020-06-03 10:11:18 +02:00
|
|
|
/**
|
|
|
|
* \file helpers.h
|
|
|
|
*
|
|
|
|
* \brief This file contains the prototypes of helper functions for the
|
|
|
|
* purpose of testing.
|
|
|
|
*/
|
|
|
|
|
2020-06-15 11:59:37 +02:00
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2020-06-03 10:11:18 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TEST_HELPERS_H
|
|
|
|
#define TEST_HELPERS_H
|
|
|
|
|
2021-05-27 14:46:48 +02:00
|
|
|
/* Most fields of publicly available structs are private and are wrapped with
|
|
|
|
* MBEDTLS_PRIVATE macro. This define allows tests to access the private fields
|
|
|
|
* directly (without using the MBEDTLS_PRIVATE wrapper). */
|
2021-05-14 22:20:10 +02:00
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2020-06-03 10:11:18 +02:00
|
|
|
|
2021-01-29 21:18:09 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
|
|
|
|
defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
#define MBEDTLS_TEST_MUTEX_USAGE
|
|
|
|
#endif
|
|
|
|
|
2020-06-09 16:27:37 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
#define mbedtls_fprintf fprintf
|
|
|
|
#define mbedtls_snprintf snprintf
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#define mbedtls_exit exit
|
|
|
|
#define mbedtls_time time
|
|
|
|
#define mbedtls_time_t time_t
|
|
|
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
|
|
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2021-06-11 14:13:53 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#endif
|
|
|
|
|
2021-01-20 16:56:42 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
2021-01-20 18:51:47 +01:00
|
|
|
MBEDTLS_TEST_RESULT_SUCCESS = 0,
|
|
|
|
MBEDTLS_TEST_RESULT_FAILED,
|
|
|
|
MBEDTLS_TEST_RESULT_SKIPPED
|
|
|
|
} mbedtls_test_result_t;
|
2021-01-20 16:56:42 +01:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2021-01-20 18:51:47 +01:00
|
|
|
mbedtls_test_result_t result;
|
2021-01-20 16:56:42 +01:00
|
|
|
const char *test;
|
|
|
|
const char *filename;
|
|
|
|
int line_no;
|
|
|
|
unsigned long step;
|
2021-04-29 20:28:54 +02:00
|
|
|
char line1[76];
|
|
|
|
char line2[76];
|
2021-01-29 21:18:09 +01:00
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
|
|
|
const char *mutex_usage_error;
|
|
|
|
#endif
|
2021-01-20 16:56:42 +01:00
|
|
|
}
|
2021-01-20 18:51:47 +01:00
|
|
|
mbedtls_test_info_t;
|
|
|
|
extern mbedtls_test_info_t mbedtls_test_info;
|
2021-01-20 16:56:42 +01:00
|
|
|
|
2020-06-08 16:44:58 +02:00
|
|
|
int mbedtls_test_platform_setup( void );
|
|
|
|
void mbedtls_test_platform_teardown( void );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
2021-02-03 13:07:01 +01:00
|
|
|
/**
|
2021-02-03 17:15:00 +01:00
|
|
|
* \brief Record the current test case as a failure.
|
2021-02-03 13:07:01 +01:00
|
|
|
*
|
2021-02-03 17:15:00 +01:00
|
|
|
* This function can be called directly however it is usually
|
|
|
|
* called via macros such as TEST_ASSERT, TEST_EQUAL,
|
|
|
|
* PSA_ASSERT, etc...
|
|
|
|
*
|
|
|
|
* \note If the test case was already marked as failed, calling
|
|
|
|
* `mbedtls_test_fail( )` again will not overwrite any
|
|
|
|
* previous information about the failure.
|
|
|
|
*
|
|
|
|
* \param test Description of the failure or assertion that failed. This
|
|
|
|
* MUST be a string literal.
|
2021-02-03 13:07:01 +01:00
|
|
|
* \param line_no Line number where the failure originated.
|
|
|
|
* \param filename Filename where the failure originated.
|
|
|
|
*/
|
2021-01-20 16:56:42 +01:00
|
|
|
void mbedtls_test_fail( const char *test, int line_no, const char* filename );
|
2021-02-03 13:07:01 +01:00
|
|
|
|
|
|
|
/**
|
2021-02-03 17:15:00 +01:00
|
|
|
* \brief Record the current test case as skipped.
|
2021-02-03 13:07:01 +01:00
|
|
|
*
|
2021-02-03 17:15:00 +01:00
|
|
|
* This function can be called directly however it is usually
|
|
|
|
* called via the TEST_ASSUME macro.
|
|
|
|
*
|
|
|
|
* \param test Description of the assumption that caused the test case to
|
|
|
|
* be skipped. This MUST be a string literal.
|
|
|
|
* \param line_no Line number where the test case was skipped.
|
|
|
|
* \param filename Filename where the test case was skipped.
|
2021-02-03 13:07:01 +01:00
|
|
|
*/
|
2021-02-02 17:20:45 +01:00
|
|
|
void mbedtls_test_skip( const char *test, int line_no, const char* filename );
|
2021-01-20 16:56:42 +01:00
|
|
|
|
2021-02-03 13:07:01 +01:00
|
|
|
/**
|
|
|
|
* \brief Set the test step number for failure reports.
|
2021-01-20 16:56:42 +01:00
|
|
|
*
|
2021-02-03 17:15:00 +01:00
|
|
|
* Call this function to display "step NNN" in addition to the
|
2021-02-03 13:07:01 +01:00
|
|
|
* line number and file name if a test fails. Typically the "step
|
|
|
|
* number" is the index of a for loop but it can be whatever you
|
|
|
|
* want.
|
2021-01-20 16:56:42 +01:00
|
|
|
*
|
|
|
|
* \param step The step number to report.
|
|
|
|
*/
|
|
|
|
void mbedtls_test_set_step( unsigned long step );
|
|
|
|
|
2021-02-03 13:07:01 +01:00
|
|
|
/**
|
|
|
|
* \brief Reset mbedtls_test_info to a ready/starting state.
|
|
|
|
*/
|
2021-02-02 17:20:45 +01:00
|
|
|
void mbedtls_test_info_reset( void );
|
2021-01-20 16:56:42 +01:00
|
|
|
|
2021-04-29 20:28:54 +02:00
|
|
|
/**
|
|
|
|
* \brief Record the current test case as a failure if two integers
|
|
|
|
* have a different value.
|
|
|
|
*
|
|
|
|
* This function is usually called via the macro
|
|
|
|
* #TEST_EQUAL.
|
|
|
|
*
|
|
|
|
* \param test Description of the failure or assertion that failed. This
|
|
|
|
* MUST be a string literal. This normally has the form
|
|
|
|
* "EXPR1 == EXPR2" where EXPR1 has the value \p value1
|
|
|
|
* and EXPR2 has the value \p value2.
|
|
|
|
* \param line_no Line number where the failure originated.
|
|
|
|
* \param filename Filename where the failure originated.
|
|
|
|
* \param value1 The first value to compare.
|
|
|
|
* \param value2 The second value to compare.
|
|
|
|
*
|
|
|
|
* \return \c 1 if the values are equal, otherwise \c 0.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_equal( const char *test, int line_no, const char* filename,
|
|
|
|
unsigned long long value1, unsigned long long value2 );
|
|
|
|
|
2022-04-13 23:59:52 +02:00
|
|
|
/**
|
|
|
|
* \brief Record the current test case as a failure based
|
|
|
|
* on comparing two unsigned integers.
|
|
|
|
*
|
|
|
|
* This function is usually called via the macro
|
|
|
|
* #TEST_LE_U.
|
|
|
|
*
|
|
|
|
* \param test Description of the failure or assertion that failed. This
|
|
|
|
* MUST be a string literal. This normally has the form
|
|
|
|
* "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
|
|
|
|
* and EXPR2 has the value \p value2.
|
|
|
|
* \param line_no Line number where the failure originated.
|
|
|
|
* \param filename Filename where the failure originated.
|
|
|
|
* \param value1 The first value to compare.
|
|
|
|
* \param value2 The second value to compare.
|
|
|
|
*
|
|
|
|
* \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
|
|
|
|
unsigned long long value1, unsigned long long value2 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Record the current test case as a failure based
|
|
|
|
* on comparing two signed integers.
|
|
|
|
*
|
|
|
|
* This function is usually called via the macro
|
|
|
|
* #TEST_LE_S.
|
|
|
|
*
|
|
|
|
* \param test Description of the failure or assertion that failed. This
|
|
|
|
* MUST be a string literal. This normally has the form
|
|
|
|
* "EXPR1 <= EXPR2" where EXPR1 has the value \p value1
|
|
|
|
* and EXPR2 has the value \p value2.
|
|
|
|
* \param line_no Line number where the failure originated.
|
|
|
|
* \param filename Filename where the failure originated.
|
|
|
|
* \param value1 The first value to compare.
|
|
|
|
* \param value2 The second value to compare.
|
|
|
|
*
|
|
|
|
* \return \c 1 if \p value1 <= \p value2, otherwise \c 0.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
|
|
|
|
long long value1, long long value2 );
|
|
|
|
|
2020-06-18 10:10:46 +02:00
|
|
|
/**
|
2020-07-01 17:09:10 +02:00
|
|
|
* \brief This function decodes the hexadecimal representation of
|
|
|
|
* data.
|
2020-06-18 10:10:46 +02:00
|
|
|
*
|
|
|
|
* \note The output buffer can be the same as the input buffer. For
|
|
|
|
* any other overlapping of the input and output buffers, the
|
|
|
|
* behavior is undefined.
|
|
|
|
*
|
|
|
|
* \param obuf Output buffer.
|
|
|
|
* \param obufmax Size in number of bytes of \p obuf.
|
|
|
|
* \param ibuf Input buffer.
|
|
|
|
* \param len The number of unsigned char written in \p obuf. This must
|
|
|
|
* not be \c NULL.
|
|
|
|
*
|
|
|
|
* \return \c 0 on success.
|
|
|
|
* \return \c -1 if the output buffer is too small or the input string
|
2020-07-01 17:09:10 +02:00
|
|
|
* is not a valid hexadecimal representation.
|
2020-06-18 10:10:46 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax,
|
|
|
|
const char *ibuf, size_t *len );
|
|
|
|
|
2020-06-08 17:05:57 +02:00
|
|
|
void mbedtls_test_hexify( unsigned char *obuf,
|
|
|
|
const unsigned char *ibuf,
|
|
|
|
int len );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate and zeroize a buffer.
|
|
|
|
*
|
|
|
|
* If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
|
|
|
|
*
|
|
|
|
* For convenience, dies if allocation fails.
|
|
|
|
*/
|
2020-06-10 10:42:18 +02:00
|
|
|
unsigned char *mbedtls_test_zero_alloc( size_t len );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate and fill a buffer from hex data.
|
|
|
|
*
|
|
|
|
* The buffer is sized exactly as needed. This allows to detect buffer
|
|
|
|
* overruns (including overreads) when running the test suite under valgrind.
|
|
|
|
*
|
|
|
|
* If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
|
|
|
|
*
|
|
|
|
* For convenience, dies if allocation fails.
|
|
|
|
*/
|
2020-06-10 10:53:11 +02:00
|
|
|
unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
2020-06-10 11:03:08 +02:00
|
|
|
int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
|
|
|
|
uint32_t a_len, uint32_t b_len );
|
2020-06-09 16:27:37 +02:00
|
|
|
|
2021-02-08 20:59:39 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
2021-01-20 20:02:01 +01:00
|
|
|
#include "test/fake_external_rng_for_test.h"
|
|
|
|
#endif
|
|
|
|
|
2021-01-29 21:18:09 +01:00
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
2021-01-29 21:17:11 +01:00
|
|
|
/** Permanently activate the mutex usage verification framework. See
|
|
|
|
* threading_helpers.c for information. */
|
|
|
|
void mbedtls_test_mutex_usage_init( void );
|
2021-01-29 21:18:09 +01:00
|
|
|
|
|
|
|
/** Call this function after executing a test case to check for mutex usage
|
|
|
|
* errors. */
|
|
|
|
void mbedtls_test_mutex_usage_check( void );
|
2021-01-29 21:17:11 +01:00
|
|
|
#endif /* MBEDTLS_TEST_MUTEX_USAGE */
|
|
|
|
|
2021-01-08 18:04:59 +01:00
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
/**
|
2021-03-31 10:34:22 +02:00
|
|
|
* \brief Check that only a pure high-level error code is being combined with
|
|
|
|
* a pure low-level error code as otherwise the resultant error code
|
2021-01-12 16:21:57 +01:00
|
|
|
* would be corrupted.
|
2021-03-31 10:34:22 +02:00
|
|
|
*
|
|
|
|
* \note Both high-level and low-level error codes cannot be greater than
|
|
|
|
* zero however can be zero. If one error code is zero then the
|
|
|
|
* other error code is returned even if both codes are zero.
|
|
|
|
*
|
|
|
|
* \note If the check fails, fail the test currently being run.
|
2021-01-08 18:04:59 +01:00
|
|
|
*/
|
|
|
|
void mbedtls_test_err_add_check( int high, int low,
|
|
|
|
const char *file, int line);
|
|
|
|
#endif
|
|
|
|
|
2021-06-11 14:13:53 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2022-07-07 12:02:27 +02:00
|
|
|
/** Read an MPI from a hexadecimal string.
|
2021-06-11 14:13:53 +02:00
|
|
|
*
|
|
|
|
* Like mbedtls_mpi_read_string(), but size the resulting bignum based
|
|
|
|
* on the number of digits in the string. In particular, construct a
|
|
|
|
* bignum with 0 limbs for an empty string, and a bignum with leading 0
|
|
|
|
* limbs if the string has sufficiently many leading 0 digits.
|
|
|
|
*
|
|
|
|
* This is important so that the "0 (null)" and "0 (1 limb)" and
|
|
|
|
* "leading zeros" test cases do what they claim.
|
|
|
|
*
|
|
|
|
* \param[out] X The MPI object to populate. It must be initialized.
|
2022-07-07 12:02:27 +02:00
|
|
|
* \param[in] s The null-terminated hexadecimal string to read from.
|
2021-06-11 14:13:53 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 on success, an \c MBEDTLS_ERR_MPI_xxx error code otherwise.
|
|
|
|
*/
|
|
|
|
/* Since the library has exactly the desired behavior, this is trivial. */
|
2022-07-07 12:02:27 +02:00
|
|
|
int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s );
|
2021-06-11 14:13:53 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
|
2020-06-03 10:11:18 +02:00
|
|
|
#endif /* TEST_HELPERS_H */
|