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 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame^] | 27 | #include "alignment.h" |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 28 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 29 | #include <stdint.h> |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 30 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 31 | |
Dave Rodgman | 468df31 | 2022-11-23 16:56:35 +0000 | [diff] [blame] | 32 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 33 | !defined(inline) && !defined(__cplusplus) |
| 34 | #define inline __inline |
| 35 | #endif |
| 36 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 37 | /** Helper to define a function as static except when building invasive tests. |
| 38 | * |
| 39 | * If a function is only used inside its own source file and should be |
| 40 | * declared `static` to allow the compiler to optimize for code size, |
| 41 | * but that function has unit tests, define it with |
| 42 | * ``` |
| 43 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 44 | * ``` |
| 45 | * and declare it in a header in the `library/` directory with |
| 46 | * ``` |
| 47 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 48 | * int mbedtls_foo(...); |
| 49 | * #endif |
| 50 | * ``` |
| 51 | */ |
| 52 | #if defined(MBEDTLS_TEST_HOOKS) |
| 53 | #define MBEDTLS_STATIC_TESTABLE |
| 54 | #else |
| 55 | #define MBEDTLS_STATIC_TESTABLE static |
| 56 | #endif |
| 57 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_TEST_HOOKS) |
| 59 | extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file ); |
| 60 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \ |
| 61 | do { \ |
| 62 | if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \ |
| 63 | { \ |
| 64 | ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \ |
| 65 | } \ |
| 66 | } while( 0 ) |
| 67 | #else |
| 68 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) |
| 69 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 70 | |
Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 71 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 72 | * |
| 73 | * Although structs defined in header files are publicly available, |
| 74 | * their members are private and should not be accessed by the user. |
| 75 | */ |
| 76 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 77 | |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Perform a fast block XOR operation, such that |
| 80 | * r[i] = a[i] ^ b[i] where 0 <= i < n |
| 81 | * |
| 82 | * \param r Pointer to result (buffer of at least \p n bytes). \p r |
| 83 | * may be equal to either \p a or \p b, but behaviour when |
| 84 | * it overlaps in other ways is undefined. |
| 85 | * \param a Pointer to input (buffer of at least \p n bytes) |
| 86 | * \param b Pointer to input (buffer of at least \p n bytes) |
| 87 | * \param n Number of bytes to process. |
| 88 | */ |
Dave Rodgman | 3c8eb7e | 2022-11-23 14:50:03 +0000 | [diff] [blame] | 89 | inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n ) |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 90 | { |
Dave Rodgman | fdd967e | 2022-11-22 18:55:17 +0000 | [diff] [blame] | 91 | #if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS) |
Dave Rodgman | dd3103e | 2022-11-23 19:42:13 +0000 | [diff] [blame] | 92 | UNALIGNED_UINT32_T *a32 = (uint32_t *)a; |
| 93 | UNALIGNED_UINT32_T *b32 = (uint32_t *)b; |
| 94 | UNALIGNED_UINT32_T *r32 = (uint32_t *)r; |
Dave Rodgman | f9a1c37 | 2022-11-23 14:02:00 +0000 | [diff] [blame] | 95 | for ( size_t i = 0; i < ( n >> 2 ); i++ ) |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 96 | { |
| 97 | r32[i] = a32[i] ^ b32[i]; |
| 98 | } |
Dave Rodgman | f9a1c37 | 2022-11-23 14:02:00 +0000 | [diff] [blame] | 99 | for ( size_t i = n - ( n % 4 ) ; i < n; i++ ) |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 100 | { |
| 101 | r[i] = a[i] ^ b[i]; |
| 102 | } |
Dave Rodgman | fdd967e | 2022-11-22 18:55:17 +0000 | [diff] [blame] | 103 | #else |
| 104 | for ( size_t i = 0; i < n; i++ ) |
| 105 | { |
| 106 | r[i] = a[i] ^ b[i]; |
| 107 | } |
| 108 | #endif |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 111 | /* Fix MSVC C99 compatible issue |
| 112 | * MSVC support __func__ from visual studio 2015( 1900 ) |
| 113 | * Use MSVC predefine macro to avoid name check fail. |
| 114 | */ |
| 115 | #if (defined(_MSC_VER) && ( _MSC_VER <= 1900 )) |
Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 116 | #define /*no-check-names*/ __func__ __FUNCTION__ |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 117 | #endif |
| 118 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 119 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |