Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file common.h |
| 3 | * |
| 4 | * \brief Utility macros for internal use in the library |
| 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef MBEDTLS_LIBRARY_COMMON_H |
| 24 | #define MBEDTLS_LIBRARY_COMMON_H |
| 25 | |
| 26 | #if defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include MBEDTLS_CONFIG_FILE |
| 28 | #else |
| 29 | #include "mbedtls/config.h" |
| 30 | #endif |
| 31 | |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 32 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 33 | /** Helper to define a function as static except when building invasive tests. |
| 34 | * |
| 35 | * If a function is only used inside its own source file and should be |
| 36 | * declared `static` to allow the compiler to optimize for code size, |
| 37 | * but that function has unit tests, define it with |
| 38 | * ``` |
| 39 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 40 | * ``` |
| 41 | * and declare it in a header in the `library/` directory with |
| 42 | * ``` |
| 43 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 44 | * int mbedtls_foo(...); |
| 45 | * #endif |
| 46 | * ``` |
| 47 | */ |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 48 | #define MBEDTLS_STATIC_TESTABLE |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 49 | |
| 50 | /** Helper macro and function to combine a high and low level error code. |
Chris Jones | 808b7c8 | 2021-01-13 12:33:36 +0000 | [diff] [blame^] | 51 | * |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 52 | * This function uses a hook (`mbedtls_test_err_add_hook`) to allow invasive |
| 53 | * testing of its inputs. This is used in the test infrastructure to report |
| 54 | * on errors when combining two error codes of the same level (e.g: two high |
| 55 | * or two low level errors). |
Chris Jones | 808b7c8 | 2021-01-13 12:33:36 +0000 | [diff] [blame^] | 56 | * |
| 57 | * To set a hook use |
| 58 | * ``` |
| 59 | * mbedtls_set_err_add_hook(&mbedtls_check_foo); |
| 60 | * ``` |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 61 | */ |
Chris Jones | 808b7c8 | 2021-01-13 12:33:36 +0000 | [diff] [blame^] | 62 | void mbedtls_set_err_add_hook( void *hook ); |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 63 | int mbedtls_err_add( int high, int low, const char *file, int line ); |
| 64 | #define MBEDTLS_ERR_ADD( high, low ) \ |
| 65 | ( mbedtls_err_add( high, low, __FILE__, __LINE__ ) ) |
| 66 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 67 | #else |
| 68 | #define MBEDTLS_STATIC_TESTABLE static |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 69 | |
| 70 | #define MBEDTLS_ERR_ADD( high, low ) \ |
| 71 | ( high + low ) |
| 72 | |
| 73 | #endif /* MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 74 | |
| 75 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |