| 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 |  | 
| Andrzej Kurek | b22b977 | 2023-05-30 09:44:20 -0400 | [diff] [blame] | 72 | /** \def ARRAY_LENGTH | 
|  | 73 | * Return the number of elements of a static or stack array. | 
|  | 74 | * | 
|  | 75 | * \param array         A value of array (not pointer) type. | 
|  | 76 | * | 
|  | 77 | * \return The number of elements of the array. | 
|  | 78 | */ | 
|  | 79 | /* A correct implementation of ARRAY_LENGTH, but which silently gives | 
|  | 80 | * a nonsensical result if called with a pointer rather than an array. */ | 
|  | 81 | #define ARRAY_LENGTH_UNSAFE(array)            \ | 
|  | 82 | (sizeof(array) / sizeof(*(array))) | 
|  | 83 |  | 
|  | 84 | #if defined(__GNUC__) | 
|  | 85 | /* Test if arg and &(arg)[0] have the same type. This is true if arg is | 
|  | 86 | * an array but not if it's a pointer. */ | 
|  | 87 | #define IS_ARRAY_NOT_POINTER(arg)                                     \ | 
|  | 88 | (!__builtin_types_compatible_p(__typeof__(arg),                \ | 
|  | 89 | __typeof__(&(arg)[0]))) | 
|  | 90 | /* A compile-time constant with the value 0. If `const_expr` is not a | 
|  | 91 | * compile-time constant with a nonzero value, cause a compile-time error. */ | 
|  | 92 | #define STATIC_ASSERT_EXPR(const_expr)                                \ | 
|  | 93 | (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); })) | 
|  | 94 |  | 
|  | 95 | /* Return the scalar value `value` (possibly promoted). This is a compile-time | 
|  | 96 | * constant if `value` is. `condition` must be a compile-time constant. | 
|  | 97 | * If `condition` is false, arrange to cause a compile-time error. */ | 
|  | 98 | #define STATIC_ASSERT_THEN_RETURN(condition, value)   \ | 
|  | 99 | (STATIC_ASSERT_EXPR(condition) ? 0 : (value)) | 
|  | 100 |  | 
|  | 101 | #define ARRAY_LENGTH(array)                                           \ | 
|  | 102 | (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array),         \ | 
|  | 103 | ARRAY_LENGTH_UNSAFE(array))) | 
|  | 104 |  | 
|  | 105 | #else | 
|  | 106 | /* If we aren't sure the compiler supports our non-standard tricks, | 
|  | 107 | * fall back to the unsafe implementation. */ | 
|  | 108 | #define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array) | 
|  | 109 | #endif | 
| Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 110 | /** Allow library to access its structs' private members. | 
| Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 111 | * | 
|  | 112 | * Although structs defined in header files are publicly available, | 
|  | 113 | * their members are private and should not be accessed by the user. | 
|  | 114 | */ | 
|  | 115 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS | 
|  | 116 |  | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 117 | /** | 
|  | 118 | * \brief       Securely zeroize a buffer then free it. | 
|  | 119 | * | 
| Tom Cosgrove | 3a11bb8 | 2023-07-18 16:26:29 +0100 | [diff] [blame] | 120 | *              Similar to making consecutive calls to | 
|  | 121 | *              \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 122 | *              code size savings, and potential for optimisation in the future. | 
|  | 123 | * | 
| Tom Cosgrove | 3a11bb8 | 2023-07-18 16:26:29 +0100 | [diff] [blame] | 124 | *              Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0. | 
|  | 125 | * | 
|  | 126 | * \param buf   Buffer to be zeroized then freed. | 
| Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 127 | * \param len   Length of the buffer in bytes | 
|  | 128 | */ | 
|  | 129 | void mbedtls_zeroize_and_free(void *buf, size_t len); | 
|  | 130 |  | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 131 | /** Return an offset into a buffer. | 
|  | 132 | * | 
|  | 133 | * This is just the addition of an offset to a pointer, except that this | 
|  | 134 | * 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] | 135 | * (`p + n` has undefined behavior when `p` is null, even when `n == 0`. | 
|  | 136 | * A null pointer is a valid buffer pointer when the size is 0, for example | 
|  | 137 | * as the result of `malloc(0)` on some platforms.) | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 138 | * | 
|  | 139 | * \param p     Pointer to a buffer of at least n bytes. | 
|  | 140 | *              This may be \p NULL if \p n is zero. | 
|  | 141 | * \param n     An offset in bytes. | 
|  | 142 | * \return      Pointer to offset \p n in the buffer \p p. | 
|  | 143 | *              Note that this is only a valid pointer if the size of the | 
|  | 144 | *              buffer is at least \p n + 1. | 
|  | 145 | */ | 
|  | 146 | static inline unsigned char *mbedtls_buffer_offset( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | unsigned char *p, size_t n) | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 148 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | return p == NULL ? NULL : p + n; | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | /** Return an offset into a read-only buffer. | 
|  | 153 | * | 
| Gilles Peskine | 7d23778 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 154 | * Similar to mbedtls_buffer_offset(), but for const pointers. | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 155 | * | 
|  | 156 | * \param p     Pointer to a buffer of at least n bytes. | 
|  | 157 | *              This may be \p NULL if \p n is zero. | 
|  | 158 | * \param n     An offset in bytes. | 
|  | 159 | * \return      Pointer to offset \p n in the buffer \p p. | 
|  | 160 | *              Note that this is only a valid pointer if the size of the | 
|  | 161 | *              buffer is at least \p n + 1. | 
|  | 162 | */ | 
|  | 163 | static inline const unsigned char *mbedtls_buffer_offset_const( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | const unsigned char *p, size_t n) | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 165 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | return p == NULL ? NULL : p + n; | 
| Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
| Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 169 | /** | 
|  | 170 | * Perform a fast block XOR operation, such that | 
|  | 171 | * r[i] = a[i] ^ b[i] where 0 <= i < n | 
|  | 172 | * | 
|  | 173 | * \param   r Pointer to result (buffer of at least \p n bytes). \p r | 
|  | 174 | *            may be equal to either \p a or \p b, but behaviour when | 
|  | 175 | *            it overlaps in other ways is undefined. | 
|  | 176 | * \param   a Pointer to input (buffer of at least \p n bytes) | 
|  | 177 | * \param   b Pointer to input (buffer of at least \p n bytes) | 
|  | 178 | * \param   n Number of bytes to process. | 
|  | 179 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | 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] | 181 | { | 
| Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 182 | size_t i = 0; | 
|  | 183 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) | 
| Dave Rodgman | 3f47b3f | 2023-05-23 16:11:22 +0100 | [diff] [blame] | 184 | #if defined(__ARM_NEON) | 
| Dave Rodgman | 6f40f8b | 2023-05-22 18:21:20 +0100 | [diff] [blame] | 185 | for (; (i + 16) <= n; i += 16) { | 
| Dave Rodgman | f32176c | 2023-06-09 16:25:49 +0100 | [diff] [blame] | 186 | uint8x16_t v1 = vld1q_u8(a + i); | 
|  | 187 | uint8x16_t v2 = vld1q_u8(b + i); | 
| Dave Rodgman | 2070c20 | 2023-06-07 16:25:58 +0100 | [diff] [blame] | 188 | uint8x16_t x = veorq_u8(v1, v2); | 
| Dave Rodgman | f32176c | 2023-06-09 16:25:49 +0100 | [diff] [blame] | 189 | vst1q_u8(r + i, x); | 
| Dave Rodgman | 6f40f8b | 2023-05-22 18:21:20 +0100 | [diff] [blame] | 190 | } | 
|  | 191 | #elif defined(__amd64__) || defined(__x86_64__) || defined(__aarch64__) | 
| Dave Rodgman | 0805ad1 | 2023-05-19 11:48:10 +0100 | [diff] [blame] | 192 | /* This codepath probably only makes sense on architectures with 64-bit registers */ | 
|  | 193 | for (; (i + 8) <= n; i += 8) { | 
|  | 194 | uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i); | 
|  | 195 | mbedtls_put_unaligned_uint64(r + i, x); | 
|  | 196 | } | 
| Dave Rodgman | 5c394ff | 2023-06-09 20:10:36 +0100 | [diff] [blame] | 197 | #else | 
| Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 198 | for (; (i + 4) <= n; i += 4) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i); | 
|  | 200 | mbedtls_put_unaligned_uint32(r + i, x); | 
| Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 201 | } | 
| Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 202 | #endif | 
| Dave Rodgman | 5c394ff | 2023-06-09 20:10:36 +0100 | [diff] [blame] | 203 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | for (; i < n; i++) { | 
| Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 205 | r[i] = a[i] ^ b[i]; | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 |  | 
| Dave Rodgman | 03bb526 | 2023-06-15 18:43:24 +0100 | [diff] [blame] | 209 | /** | 
|  | 210 | * Perform a fast block XOR operation, such that | 
|  | 211 | * r[i] = a[i] ^ b[i] where 0 <= i < n | 
|  | 212 | * | 
|  | 213 | * In some situations, this can perform better than mbedtls_xor (e.g., it's about 5% | 
|  | 214 | * better in AES-CBC). | 
|  | 215 | * | 
|  | 216 | * \param   r Pointer to result (buffer of at least \p n bytes). \p r | 
|  | 217 | *            may be equal to either \p a or \p b, but behaviour when | 
|  | 218 | *            it overlaps in other ways is undefined. | 
|  | 219 | * \param   a Pointer to input (buffer of at least \p n bytes) | 
|  | 220 | * \param   b Pointer to input (buffer of at least \p n bytes) | 
|  | 221 | * \param   n Number of bytes to process. | 
|  | 222 | */ | 
| Dave Rodgman | 2dd15b3 | 2023-06-15 20:27:53 +0100 | [diff] [blame] | 223 | static inline void mbedtls_xor_no_simd(unsigned char *r, | 
|  | 224 | const unsigned char *a, | 
|  | 225 | const unsigned char *b, | 
|  | 226 | size_t n) | 
| Dave Rodgman | 03bb526 | 2023-06-15 18:43:24 +0100 | [diff] [blame] | 227 | { | 
|  | 228 | size_t i = 0; | 
|  | 229 | #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) | 
|  | 230 | #if defined(__amd64__) || defined(__x86_64__) || defined(__aarch64__) | 
|  | 231 | /* This codepath probably only makes sense on architectures with 64-bit registers */ | 
|  | 232 | for (; (i + 8) <= n; i += 8) { | 
|  | 233 | uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i); | 
|  | 234 | mbedtls_put_unaligned_uint64(r + i, x); | 
|  | 235 | } | 
|  | 236 | #else | 
|  | 237 | for (; (i + 4) <= n; i += 4) { | 
|  | 238 | uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i); | 
|  | 239 | mbedtls_put_unaligned_uint32(r + i, x); | 
|  | 240 | } | 
|  | 241 | #endif | 
|  | 242 | #endif | 
|  | 243 | for (; i < n; i++) { | 
|  | 244 | r[i] = a[i] ^ b[i]; | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 |  | 
| Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 248 | /* Fix MSVC C99 compatible issue | 
|  | 249 | *      MSVC support __func__ from visual studio 2015( 1900 ) | 
|  | 250 | *      Use MSVC predefine macro to avoid name check fail. | 
|  | 251 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | #if (defined(_MSC_VER) && (_MSC_VER <= 1900)) | 
| Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 253 | #define /*no-check-names*/ __func__ __FUNCTION__ | 
| Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 254 | #endif | 
|  | 255 |  | 
| Dave Rodgman | fa96026 | 2023-01-10 11:14:02 +0000 | [diff] [blame] | 256 | /* Define `asm` for compilers which don't define it. */ | 
|  | 257 | /* *INDENT-OFF* */ | 
|  | 258 | #ifndef asm | 
| Agathiyan Bragadeesh | 789e50e | 2023-07-14 16:59:36 +0100 | [diff] [blame] | 259 | #if defined(__IAR_SYSTEMS_ICC__) | 
|  | 260 | #define asm __asm | 
|  | 261 | #else | 
| Dave Rodgman | fa96026 | 2023-01-10 11:14:02 +0000 | [diff] [blame] | 262 | #define asm __asm__ | 
|  | 263 | #endif | 
| Agathiyan Bragadeesh | 789e50e | 2023-07-14 16:59:36 +0100 | [diff] [blame] | 264 | #endif | 
| Dave Rodgman | fa96026 | 2023-01-10 11:14:02 +0000 | [diff] [blame] | 265 | /* *INDENT-ON* */ | 
|  | 266 |  | 
| Dave Rodgman | 0400ae2 | 2023-06-21 16:14:46 +0100 | [diff] [blame] | 267 | /* | 
| Dave Rodgman | 28e2ca5 | 2023-06-27 15:25:38 +0100 | [diff] [blame] | 268 | * Define the constraint used for read-only pointer operands to aarch64 asm. | 
| Dave Rodgman | 0400ae2 | 2023-06-21 16:14:46 +0100 | [diff] [blame] | 269 | * | 
|  | 270 | * This is normally the usual "r", but for aarch64_32 (aka ILP32, | 
|  | 271 | * as found in watchos), "p" is required to avoid warnings from clang. | 
| Dave Rodgman | e6c9996 | 2023-06-21 21:16:23 +0100 | [diff] [blame] | 272 | * | 
|  | 273 | * Note that clang does not recognise '+p' or '=p', and armclang | 
| Dave Rodgman | 28e2ca5 | 2023-06-27 15:25:38 +0100 | [diff] [blame] | 274 | * does not recognise 'p' at all. Therefore, to update a pointer from | 
|  | 275 | * aarch64 assembly, it is necessary to use something like: | 
|  | 276 | * | 
|  | 277 | * uintptr_t uptr = (uintptr_t) ptr; | 
|  | 278 | * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : ) | 
|  | 279 | * ptr = (void*) uptr; | 
|  | 280 | * | 
|  | 281 | * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings. | 
| Dave Rodgman | 0400ae2 | 2023-06-21 16:14:46 +0100 | [diff] [blame] | 282 | */ | 
|  | 283 | #if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM) | 
|  | 284 | #if UINTPTR_MAX == 0xfffffffful | 
|  | 285 | /* ILP32: Specify the pointer operand slightly differently, as per #7787. */ | 
|  | 286 | #define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p" | 
|  | 287 | #elif UINTPTR_MAX == 0xfffffffffffffffful | 
|  | 288 | /* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */ | 
|  | 289 | #define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r" | 
|  | 290 | #else | 
| Antonio de Angelis | 1ee4d12 | 2023-08-16 12:26:37 +0100 | [diff] [blame] | 291 | #error "Unrecognised pointer size for aarch64" | 
| Dave Rodgman | 0400ae2 | 2023-06-21 16:14:46 +0100 | [diff] [blame] | 292 | #endif | 
|  | 293 | #endif | 
|  | 294 |  | 
| Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 295 | /* Always provide a static assert macro, so it can be used unconditionally. | 
| Tom Cosgrove | 57f04b8 | 2023-03-14 12:03:47 +0000 | [diff] [blame] | 296 | * It will expand to nothing on some systems. | 
| Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 297 | * Can be used outside functions (but don't add a trailing ';' in that case: | 
|  | 298 | * the semicolon is included here to avoid triggering -Wextra-semi when | 
|  | 299 | * MBEDTLS_STATIC_ASSERT() expands to nothing). | 
|  | 300 | * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it | 
|  | 301 | * defines static_assert even with -std=c99, but then complains about it. | 
|  | 302 | */ | 
|  | 303 | #if defined(static_assert) && !defined(__FreeBSD__) | 
|  | 304 | #define MBEDTLS_STATIC_ASSERT(expr, msg)    static_assert(expr, msg); | 
| Tom Cosgrove | 6ef9bb3 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 305 | #else | 
|  | 306 | #define MBEDTLS_STATIC_ASSERT(expr, msg) | 
|  | 307 | #endif | 
|  | 308 |  | 
| Dave Rodgman | 360e04f | 2023-06-09 17:18:32 +0100 | [diff] [blame] | 309 | /* Define compiler branch hints */ | 
|  | 310 | #if defined(__has_builtin) | 
|  | 311 | #if __has_builtin(__builtin_expect) | 
| Dave Rodgman | e9fcffd | 2023-07-19 15:42:19 +0100 | [diff] [blame] | 312 | #define MBEDTLS_LIKELY(x)       __builtin_expect(!!(x), 1) | 
|  | 313 | #define MBEDTLS_UNLIKELY(x)     __builtin_expect(!!(x), 0) | 
| Dave Rodgman | 360e04f | 2023-06-09 17:18:32 +0100 | [diff] [blame] | 314 | #endif | 
|  | 315 | #endif | 
|  | 316 | #if !defined(MBEDTLS_LIKELY) | 
|  | 317 | #define MBEDTLS_LIKELY(x)       x | 
|  | 318 | #define MBEDTLS_UNLIKELY(x)     x | 
|  | 319 | #endif | 
|  | 320 |  | 
| Dave Rodgman | 7fdfd70 | 2023-06-15 18:42:25 +0100 | [diff] [blame] | 321 | #if defined(__GNUC__) && !defined(__ARMCC_VERSION) && !defined(__clang__) \ | 
|  | 322 | && !defined(__llvm__) && !defined(__INTEL_COMPILER) | 
|  | 323 | /* Defined if the compiler really is gcc and not clang, etc */ | 
|  | 324 | #define MBEDTLS_COMPILER_IS_GCC | 
|  | 325 | #endif | 
|  | 326 |  | 
| Dave Rodgman | 9bb7e6f | 2023-06-16 09:41:21 +0100 | [diff] [blame] | 327 | /* For gcc -Os, override with -O2 for a given function. | 
|  | 328 | * | 
|  | 329 | * This will not affect behaviour for other optimisation settings, e.g. -O0. | 
|  | 330 | */ | 
| Dave Rodgman | b055f75 | 2023-06-15 18:42:59 +0100 | [diff] [blame] | 331 | #if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__) | 
| Dave Rodgman | 9bb7e6f | 2023-06-16 09:41:21 +0100 | [diff] [blame] | 332 | #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2"))) | 
| Dave Rodgman | b055f75 | 2023-06-15 18:42:59 +0100 | [diff] [blame] | 333 | #else | 
| Dave Rodgman | 9bb7e6f | 2023-06-16 09:41:21 +0100 | [diff] [blame] | 334 | #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE | 
| Dave Rodgman | b055f75 | 2023-06-15 18:42:59 +0100 | [diff] [blame] | 335 | #endif | 
|  | 336 |  | 
| Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 337 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |