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 | |
| 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 29 | #include "mbedtls/config.h" |
| 30 | #else |
| 31 | #include MBEDTLS_CONFIG_FILE |
| 32 | #endif |
| 33 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
| 35 | #include "mbedtls/platform.h" |
| 36 | #else |
| 37 | #include <stdio.h> |
| 38 | #define mbedtls_fprintf fprintf |
| 39 | #define mbedtls_snprintf snprintf |
| 40 | #define mbedtls_calloc calloc |
| 41 | #define mbedtls_free free |
| 42 | #define mbedtls_exit exit |
| 43 | #define mbedtls_time time |
| 44 | #define mbedtls_time_t time_t |
| 45 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 46 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 47 | #endif |
| 48 | |
| 49 | #include <stddef.h> |
| 50 | #include <stdint.h> |
| 51 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 52 | int mbedtls_test_platform_setup( void ); |
| 53 | void mbedtls_test_platform_teardown( void ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 54 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 55 | /** |
| 56 | * \brief This function translates an ASCII string encoding an |
| 57 | * hexadecimal number into the encoded hexadecimal number. The |
| 58 | * hexadecimal number is represented as an array of |
| 59 | * unsigned char. |
| 60 | * |
| 61 | * \note The output buffer can be the same as the input buffer. For |
| 62 | * any other overlapping of the input and output buffers, the |
| 63 | * behavior is undefined. |
| 64 | * |
| 65 | * \param obuf Output buffer. |
| 66 | * \param obufmax Size in number of bytes of \p obuf. |
| 67 | * \param ibuf Input buffer. |
| 68 | * \param len The number of unsigned char written in \p obuf. This must |
| 69 | * not be \c NULL. |
| 70 | * |
| 71 | * \return \c 0 on success. |
| 72 | * \return \c -1 if the output buffer is too small or the input string |
| 73 | * is not a valid ASCII encoding of an hexadecimal number. |
| 74 | */ |
| 75 | int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax, |
| 76 | const char *ibuf, size_t *len ); |
| 77 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 78 | void mbedtls_test_hexify( unsigned char *obuf, |
| 79 | const unsigned char *ibuf, |
| 80 | int len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Allocate and zeroize a buffer. |
| 84 | * |
| 85 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 86 | * |
| 87 | * For convenience, dies if allocation fails. |
| 88 | */ |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 89 | unsigned char *mbedtls_test_zero_alloc( size_t len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Allocate and fill a buffer from hex data. |
| 93 | * |
| 94 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 95 | * overruns (including overreads) when running the test suite under valgrind. |
| 96 | * |
| 97 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 98 | * |
| 99 | * For convenience, dies if allocation fails. |
| 100 | */ |
Ronald Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 101 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 102 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 103 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 104 | uint32_t a_len, uint32_t b_len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 105 | |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 106 | #endif /* TEST_HELPERS_H */ |