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 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 29 | #include <assert.h> |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 30 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 31 | #include <stdint.h> |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 32 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 33 | |
Dave Rodgman | 3f47b3f | 2023-05-23 16:11:22 +0100 | [diff] [blame] | 34 | #if defined(__ARM_NEON) |
Dave Rodgman | 6f40f8b | 2023-05-22 18:21:20 +0100 | [diff] [blame] | 35 | #include <arm_neon.h> |
| 36 | #endif /* __ARM_NEON */ |
| 37 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 38 | /** Helper to define a function as static except when building invasive tests. |
| 39 | * |
| 40 | * If a function is only used inside its own source file and should be |
| 41 | * declared `static` to allow the compiler to optimize for code size, |
| 42 | * but that function has unit tests, define it with |
| 43 | * ``` |
| 44 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 45 | * ``` |
| 46 | * and declare it in a header in the `library/` directory with |
| 47 | * ``` |
| 48 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 49 | * int mbedtls_foo(...); |
| 50 | * #endif |
| 51 | * ``` |
| 52 | */ |
| 53 | #if defined(MBEDTLS_TEST_HOOKS) |
| 54 | #define MBEDTLS_STATIC_TESTABLE |
| 55 | #else |
| 56 | #define MBEDTLS_STATIC_TESTABLE static |
| 57 | #endif |
| 58 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file); |
| 61 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \ |
| 62 | do { \ |
| 63 | if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \ |
| 64 | { \ |
| 65 | (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \ |
| 66 | } \ |
| 67 | } while (0) |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 68 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 70 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 71 | |
Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 72 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 73 | * |
| 74 | * Although structs defined in header files are publicly available, |
| 75 | * their members are private and should not be accessed by the user. |
| 76 | */ |
| 77 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 78 | |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 79 | /** Return an offset into a buffer. |
| 80 | * |
| 81 | * This is just the addition of an offset to a pointer, except that this |
| 82 | * function also accepts an offset of 0 into a buffer whose pointer is null. |
Gilles Peskine | 7d23778 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 83 | * (`p + n` has undefined behavior when `p` is null, even when `n == 0`. |
| 84 | * A null pointer is a valid buffer pointer when the size is 0, for example |
| 85 | * as the result of `malloc(0)` on some platforms.) |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 86 | * |
| 87 | * \param p Pointer to a buffer of at least n bytes. |
| 88 | * This may be \p NULL if \p n is zero. |
| 89 | * \param n An offset in bytes. |
| 90 | * \return Pointer to offset \p n in the buffer \p p. |
| 91 | * Note that this is only a valid pointer if the size of the |
| 92 | * buffer is at least \p n + 1. |
| 93 | */ |
| 94 | static inline unsigned char *mbedtls_buffer_offset( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | unsigned char *p, size_t n) |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 96 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** Return an offset into a read-only buffer. |
| 101 | * |
Gilles Peskine | 7d23778 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 102 | * Similar to mbedtls_buffer_offset(), but for const pointers. |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 103 | * |
| 104 | * \param p Pointer to a buffer of at least n bytes. |
| 105 | * This may be \p NULL if \p n is zero. |
| 106 | * \param n An offset in bytes. |
| 107 | * \return Pointer to offset \p n in the buffer \p p. |
| 108 | * Note that this is only a valid pointer if the size of the |
| 109 | * buffer is at least \p n + 1. |
| 110 | */ |
| 111 | static inline const unsigned char *mbedtls_buffer_offset_const( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | const unsigned char *p, size_t n) |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 113 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Perform a fast block XOR operation, such that |
| 119 | * r[i] = a[i] ^ b[i] where 0 <= i < n |
| 120 | * |
| 121 | * \param r Pointer to result (buffer of at least \p n bytes). \p r |
| 122 | * may be equal to either \p a or \p b, but behaviour when |
| 123 | * it overlaps in other ways is undefined. |
| 124 | * \param a Pointer to input (buffer of at least \p n bytes) |
| 125 | * \param b Pointer to input (buffer of at least \p n bytes) |
| 126 | * \param n Number of bytes to process. |
| 127 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n) |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 129 | { |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 130 | size_t i = 0; |
| 131 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) |
Dave Rodgman | 3f47b3f | 2023-05-23 16:11:22 +0100 | [diff] [blame] | 132 | #if defined(__ARM_NEON) |
Dave Rodgman | 6f40f8b | 2023-05-22 18:21:20 +0100 | [diff] [blame] | 133 | for (; (i + 16) <= n; i += 16) { |
Dave Rodgman | f32176c | 2023-06-09 16:25:49 +0100 | [diff] [blame] | 134 | uint8x16_t v1 = vld1q_u8(a + i); |
| 135 | uint8x16_t v2 = vld1q_u8(b + i); |
Dave Rodgman | 2070c20 | 2023-06-07 16:25:58 +0100 | [diff] [blame] | 136 | uint8x16_t x = veorq_u8(v1, v2); |
Dave Rodgman | f32176c | 2023-06-09 16:25:49 +0100 | [diff] [blame] | 137 | vst1q_u8(r + i, x); |
Dave Rodgman | 6f40f8b | 2023-05-22 18:21:20 +0100 | [diff] [blame] | 138 | } |
| 139 | #elif defined(__amd64__) || defined(__x86_64__) || defined(__aarch64__) |
Dave Rodgman | 0805ad1 | 2023-05-19 11:48:10 +0100 | [diff] [blame] | 140 | /* This codepath probably only makes sense on architectures with 64-bit registers */ |
| 141 | for (; (i + 8) <= n; i += 8) { |
| 142 | uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i); |
| 143 | mbedtls_put_unaligned_uint64(r + i, x); |
| 144 | } |
Dave Rodgman | 5c394ff | 2023-06-09 20:10:36 +0100 | [diff] [blame] | 145 | #else |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 146 | for (; (i + 4) <= n; i += 4) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i); |
| 148 | mbedtls_put_unaligned_uint32(r + i, x); |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 149 | } |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 150 | #endif |
Dave Rodgman | 5c394ff | 2023-06-09 20:10:36 +0100 | [diff] [blame] | 151 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | for (; i < n; i++) { |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 153 | r[i] = a[i] ^ b[i]; |
| 154 | } |
| 155 | } |
| 156 | |
Dave Rodgman | 03bb526 | 2023-06-15 18:43:24 +0100 | [diff] [blame] | 157 | /** |
| 158 | * Perform a fast block XOR operation, such that |
| 159 | * r[i] = a[i] ^ b[i] where 0 <= i < n |
| 160 | * |
| 161 | * In some situations, this can perform better than mbedtls_xor (e.g., it's about 5% |
| 162 | * better in AES-CBC). |
| 163 | * |
| 164 | * \param r Pointer to result (buffer of at least \p n bytes). \p r |
| 165 | * may be equal to either \p a or \p b, but behaviour when |
| 166 | * it overlaps in other ways is undefined. |
| 167 | * \param a Pointer to input (buffer of at least \p n bytes) |
| 168 | * \param b Pointer to input (buffer of at least \p n bytes) |
| 169 | * \param n Number of bytes to process. |
| 170 | */ |
Dave Rodgman | 2dd15b3 | 2023-06-15 20:27:53 +0100 | [diff] [blame] | 171 | static inline void mbedtls_xor_no_simd(unsigned char *r, |
| 172 | const unsigned char *a, |
| 173 | const unsigned char *b, |
| 174 | size_t n) |
Dave Rodgman | 03bb526 | 2023-06-15 18:43:24 +0100 | [diff] [blame] | 175 | { |
| 176 | size_t i = 0; |
| 177 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) |
| 178 | #if defined(__amd64__) || defined(__x86_64__) || defined(__aarch64__) |
| 179 | /* This codepath probably only makes sense on architectures with 64-bit registers */ |
| 180 | for (; (i + 8) <= n; i += 8) { |
| 181 | uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i); |
| 182 | mbedtls_put_unaligned_uint64(r + i, x); |
| 183 | } |
| 184 | #else |
| 185 | for (; (i + 4) <= n; i += 4) { |
| 186 | uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i); |
| 187 | mbedtls_put_unaligned_uint32(r + i, x); |
| 188 | } |
| 189 | #endif |
| 190 | #endif |
| 191 | for (; i < n; i++) { |
| 192 | r[i] = a[i] ^ b[i]; |
| 193 | } |
| 194 | } |
| 195 | |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 196 | /* Fix MSVC C99 compatible issue |
| 197 | * MSVC support __func__ from visual studio 2015( 1900 ) |
| 198 | * Use MSVC predefine macro to avoid name check fail. |
| 199 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | #if (defined(_MSC_VER) && (_MSC_VER <= 1900)) |
Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 201 | #define /*no-check-names*/ __func__ __FUNCTION__ |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 202 | #endif |
| 203 | |
Dave Rodgman | fa96026 | 2023-01-10 11:14:02 +0000 | [diff] [blame] | 204 | /* Define `asm` for compilers which don't define it. */ |
| 205 | /* *INDENT-OFF* */ |
| 206 | #ifndef asm |
| 207 | #define asm __asm__ |
| 208 | #endif |
| 209 | /* *INDENT-ON* */ |
| 210 | |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 211 | /* Always provide a static assert macro, so it can be used unconditionally. |
Tom Cosgrove | 57f04b8 | 2023-03-14 12:03:47 +0000 | [diff] [blame] | 212 | * It will expand to nothing on some systems. |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 213 | * Can be used outside functions (but don't add a trailing ';' in that case: |
| 214 | * the semicolon is included here to avoid triggering -Wextra-semi when |
| 215 | * MBEDTLS_STATIC_ASSERT() expands to nothing). |
| 216 | * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it |
| 217 | * defines static_assert even with -std=c99, but then complains about it. |
| 218 | */ |
| 219 | #if defined(static_assert) && !defined(__FreeBSD__) |
| 220 | #define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg); |
Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 221 | #else |
| 222 | #define MBEDTLS_STATIC_ASSERT(expr, msg) |
| 223 | #endif |
| 224 | |
Dave Rodgman | 360e04f | 2023-06-09 17:18:32 +0100 | [diff] [blame] | 225 | /* Define compiler branch hints */ |
| 226 | #if defined(__has_builtin) |
| 227 | #if __has_builtin(__builtin_expect) |
Dave Rodgman | 159dc09 | 2023-06-09 19:46:07 +0100 | [diff] [blame] | 228 | #define MBEDTLS_LIKELY(x) __builtin_expect((x), 1) |
| 229 | #define MBEDTLS_UNLIKELY(x) __builtin_expect((x), 0) |
Dave Rodgman | 360e04f | 2023-06-09 17:18:32 +0100 | [diff] [blame] | 230 | #endif |
| 231 | #endif |
| 232 | #if !defined(MBEDTLS_LIKELY) |
| 233 | #define MBEDTLS_LIKELY(x) x |
| 234 | #define MBEDTLS_UNLIKELY(x) x |
| 235 | #endif |
| 236 | |
Dave Rodgman | 7fdfd70 | 2023-06-15 18:42:25 +0100 | [diff] [blame] | 237 | #if defined(__GNUC__) && !defined(__ARMCC_VERSION) && !defined(__clang__) \ |
| 238 | && !defined(__llvm__) && !defined(__INTEL_COMPILER) |
| 239 | /* Defined if the compiler really is gcc and not clang, etc */ |
| 240 | #define MBEDTLS_COMPILER_IS_GCC |
| 241 | #endif |
| 242 | |
Dave Rodgman | b055f75 | 2023-06-15 18:42:59 +0100 | [diff] [blame] | 243 | /* If -Os is specified, override with -O2 for a given function */ |
| 244 | #if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__) |
| 245 | #define MBEDTLS_OPTIMIZE_ALWAYS __attribute__((optimize("-O2"))) |
| 246 | #else |
| 247 | #define MBEDTLS_OPTIMIZE_ALWAYS |
| 248 | #endif |
| 249 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 250 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |