blob: 48fb6d0d73ae1728ef5b9dffbba847a829333e58 [file] [log] [blame]
Gilles Peskinec4672fd2019-09-11 13:39:11 +02001/**
2 * \file common.h
3 *
4 * \brief Utility macros for internal use in the library
5 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Gilles Peskinec4672fd2019-09-11 13:39:11 +02008 * 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 Peskinec4672fd2019-09-11 13:39:11 +020021 */
22
23#ifndef MBEDTLS_LIBRARY_COMMON_H
24#define MBEDTLS_LIBRARY_COMMON_H
25
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Dave Rodgmanfbc23222022-11-24 18:07:37 +000027#include "alignment.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020028
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +000029#include <assert.h>
Gilles Peskine42649d92022-11-23 14:15:57 +010030#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010031#include <stdint.h>
Dave Rodgmanc3d80412022-11-22 15:01:39 +000032#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010033
Dave Rodgman3f47b3f2023-05-23 16:11:22 +010034#if defined(__ARM_NEON)
Dave Rodgman6f40f8b2023-05-22 18:21:20 +010035#include <arm_neon.h>
Dave Rodgman4ffd7c72023-09-05 11:43:02 +010036#define MBEDTLS_HAVE_NEON_INTRINSICS
Dave Rodgman0a487172023-09-15 11:52:06 +010037#elif defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
Dave Rodgman4ffd7c72023-09-05 11:43:02 +010038#include <arm64_neon.h>
39#define MBEDTLS_HAVE_NEON_INTRINSICS
40#endif
41
Gilles Peskinec4672fd2019-09-11 13:39:11 +020042/** Helper to define a function as static except when building invasive tests.
43 *
44 * If a function is only used inside its own source file and should be
45 * declared `static` to allow the compiler to optimize for code size,
46 * but that function has unit tests, define it with
47 * ```
48 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
49 * ```
50 * and declare it in a header in the `library/` directory with
51 * ```
52 * #if defined(MBEDTLS_TEST_HOOKS)
53 * int mbedtls_foo(...);
54 * #endif
55 * ```
56 */
57#if defined(MBEDTLS_TEST_HOOKS)
58#define MBEDTLS_STATIC_TESTABLE
59#else
60#define MBEDTLS_STATIC_TESTABLE static
61#endif
62
TRodziewicz7871c2e2021-07-07 17:29:43 +020063#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010064extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
65#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
66 do { \
67 if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
68 { \
69 (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
70 } \
71 } while (0)
TRodziewicz7871c2e2021-07-07 17:29:43 +020072#else
Gilles Peskine449bd832023-01-11 14:50:10 +010073#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
TRodziewicz7871c2e2021-07-07 17:29:43 +020074#endif /* defined(MBEDTLS_TEST_HOOKS) */
75
Andrzej Kurekb22b9772023-05-30 09:44:20 -040076/** \def ARRAY_LENGTH
77 * Return the number of elements of a static or stack array.
78 *
79 * \param array A value of array (not pointer) type.
80 *
81 * \return The number of elements of the array.
82 */
83/* A correct implementation of ARRAY_LENGTH, but which silently gives
84 * a nonsensical result if called with a pointer rather than an array. */
85#define ARRAY_LENGTH_UNSAFE(array) \
86 (sizeof(array) / sizeof(*(array)))
87
88#if defined(__GNUC__)
89/* Test if arg and &(arg)[0] have the same type. This is true if arg is
90 * an array but not if it's a pointer. */
91#define IS_ARRAY_NOT_POINTER(arg) \
92 (!__builtin_types_compatible_p(__typeof__(arg), \
93 __typeof__(&(arg)[0])))
94/* A compile-time constant with the value 0. If `const_expr` is not a
95 * compile-time constant with a nonzero value, cause a compile-time error. */
96#define STATIC_ASSERT_EXPR(const_expr) \
97 (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
98
99/* Return the scalar value `value` (possibly promoted). This is a compile-time
100 * constant if `value` is. `condition` must be a compile-time constant.
101 * If `condition` is false, arrange to cause a compile-time error. */
102#define STATIC_ASSERT_THEN_RETURN(condition, value) \
103 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
104
105#define ARRAY_LENGTH(array) \
106 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
107 ARRAY_LENGTH_UNSAFE(array)))
108
109#else
110/* If we aren't sure the compiler supports our non-standard tricks,
111 * fall back to the unsafe implementation. */
112#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
113#endif
Mateusz Starzyk57d1d192021-05-27 14:39:53 +0200114/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +0200115 *
116 * Although structs defined in header files are publicly available,
117 * their members are private and should not be accessed by the user.
118 */
119#define MBEDTLS_ALLOW_PRIVATE_ACCESS
120
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100121/**
122 * \brief Securely zeroize a buffer then free it.
123 *
Tom Cosgrove3a11bb82023-07-18 16:26:29 +0100124 * Similar to making consecutive calls to
125 * \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100126 * code size savings, and potential for optimisation in the future.
127 *
Tom Cosgrove3a11bb82023-07-18 16:26:29 +0100128 * Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0.
129 *
130 * \param buf Buffer to be zeroized then freed.
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100131 * \param len Length of the buffer in bytes
132 */
133void mbedtls_zeroize_and_free(void *buf, size_t len);
134
Gilles Peskine42649d92022-11-23 14:15:57 +0100135/** Return an offset into a buffer.
136 *
137 * This is just the addition of an offset to a pointer, except that this
138 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +0100139 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
140 * A null pointer is a valid buffer pointer when the size is 0, for example
141 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +0100142 *
143 * \param p Pointer to a buffer of at least n bytes.
144 * This may be \p NULL if \p n is zero.
145 * \param n An offset in bytes.
146 * \return Pointer to offset \p n in the buffer \p p.
147 * Note that this is only a valid pointer if the size of the
148 * buffer is at least \p n + 1.
149 */
150static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100152{
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100154}
155
156/** Return an offset into a read-only buffer.
157 *
Gilles Peskine7d237782022-11-25 13:34:59 +0100158 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +0100159 *
160 * \param p Pointer to a buffer of at least n bytes.
161 * This may be \p NULL if \p n is zero.
162 * \param n An offset in bytes.
163 * \return Pointer to offset \p n in the buffer \p p.
164 * Note that this is only a valid pointer if the size of the
165 * buffer is at least \p n + 1.
166 */
167static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 const unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100169{
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100171}
172
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000173/**
174 * Perform a fast block XOR operation, such that
175 * r[i] = a[i] ^ b[i] where 0 <= i < n
176 *
177 * \param r Pointer to result (buffer of at least \p n bytes). \p r
178 * may be equal to either \p a or \p b, but behaviour when
179 * it overlaps in other ways is undefined.
180 * \param a Pointer to input (buffer of at least \p n bytes)
181 * \param b Pointer to input (buffer of at least \p n bytes)
182 * \param n Number of bytes to process.
183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000185{
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000186 size_t i = 0;
187#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgmana0f10da2023-09-05 11:43:17 +0100188#if defined(MBEDTLS_HAVE_NEON_INTRINSICS)
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100189 for (; (i + 16) <= n; i += 16) {
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100190 uint8x16_t v1 = vld1q_u8(a + i);
191 uint8x16_t v2 = vld1q_u8(b + i);
Dave Rodgman2070c202023-06-07 16:25:58 +0100192 uint8x16_t x = veorq_u8(v1, v2);
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100193 vst1q_u8(r + i, x);
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100194 }
Dave Rodgmanc5cc7272023-09-15 11:41:17 +0100195#elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
Dave Rodgman0805ad12023-05-19 11:48:10 +0100196 /* This codepath probably only makes sense on architectures with 64-bit registers */
197 for (; (i + 8) <= n; i += 8) {
198 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
199 mbedtls_put_unaligned_uint64(r + i, x);
200 }
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100201#else
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000202 for (; (i + 4) <= n; i += 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
204 mbedtls_put_unaligned_uint32(r + i, x);
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000205 }
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000206#endif
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100207#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 for (; i < n; i++) {
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000209 r[i] = a[i] ^ b[i];
210 }
211}
212
Dave Rodgman03bb5262023-06-15 18:43:24 +0100213/**
214 * Perform a fast block XOR operation, such that
215 * r[i] = a[i] ^ b[i] where 0 <= i < n
216 *
217 * In some situations, this can perform better than mbedtls_xor (e.g., it's about 5%
218 * better in AES-CBC).
219 *
220 * \param r Pointer to result (buffer of at least \p n bytes). \p r
221 * may be equal to either \p a or \p b, but behaviour when
222 * it overlaps in other ways is undefined.
223 * \param a Pointer to input (buffer of at least \p n bytes)
224 * \param b Pointer to input (buffer of at least \p n bytes)
225 * \param n Number of bytes to process.
226 */
Dave Rodgman2dd15b32023-06-15 20:27:53 +0100227static inline void mbedtls_xor_no_simd(unsigned char *r,
228 const unsigned char *a,
229 const unsigned char *b,
230 size_t n)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100231{
232 size_t i = 0;
233#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgmanc5cc7272023-09-15 11:41:17 +0100234#if defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100235 /* This codepath probably only makes sense on architectures with 64-bit registers */
236 for (; (i + 8) <= n; i += 8) {
237 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
238 mbedtls_put_unaligned_uint64(r + i, x);
239 }
240#else
241 for (; (i + 4) <= n; i += 4) {
242 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
243 mbedtls_put_unaligned_uint32(r + i, x);
244 }
245#endif
246#endif
247 for (; i < n; i++) {
248 r[i] = a[i] ^ b[i];
249 }
250}
251
Jerry Yu6c983522021-09-24 12:45:36 +0800252/* Fix MSVC C99 compatible issue
253 * MSVC support __func__ from visual studio 2015( 1900 )
254 * Use MSVC predefine macro to avoid name check fail.
255 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
Jerry Yud52398d2021-09-28 16:13:44 +0800257#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800258#endif
259
Dave Rodgmanfa960262023-01-10 11:14:02 +0000260/* Define `asm` for compilers which don't define it. */
261/* *INDENT-OFF* */
262#ifndef asm
Agathiyan Bragadeesh789e50e2023-07-14 16:59:36 +0100263#if defined(__IAR_SYSTEMS_ICC__)
264#define asm __asm
265#else
Dave Rodgmanfa960262023-01-10 11:14:02 +0000266#define asm __asm__
267#endif
Agathiyan Bragadeesh789e50e2023-07-14 16:59:36 +0100268#endif
Dave Rodgmanfa960262023-01-10 11:14:02 +0000269/* *INDENT-ON* */
270
Dave Rodgman0400ae22023-06-21 16:14:46 +0100271/*
Dave Rodgman28e2ca52023-06-27 15:25:38 +0100272 * Define the constraint used for read-only pointer operands to aarch64 asm.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100273 *
274 * This is normally the usual "r", but for aarch64_32 (aka ILP32,
275 * as found in watchos), "p" is required to avoid warnings from clang.
Dave Rodgmane6c99962023-06-21 21:16:23 +0100276 *
277 * Note that clang does not recognise '+p' or '=p', and armclang
Dave Rodgman28e2ca52023-06-27 15:25:38 +0100278 * does not recognise 'p' at all. Therefore, to update a pointer from
279 * aarch64 assembly, it is necessary to use something like:
280 *
281 * uintptr_t uptr = (uintptr_t) ptr;
282 * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )
283 * ptr = (void*) uptr;
284 *
285 * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100286 */
287#if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
288#if UINTPTR_MAX == 0xfffffffful
289/* ILP32: Specify the pointer operand slightly differently, as per #7787. */
290#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
291#elif UINTPTR_MAX == 0xfffffffffffffffful
292/* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
293#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
294#else
Antonio de Angelis1ee4d122023-08-16 12:26:37 +0100295#error "Unrecognised pointer size for aarch64"
Dave Rodgman0400ae22023-06-21 16:14:46 +0100296#endif
297#endif
298
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000299/* Always provide a static assert macro, so it can be used unconditionally.
Tom Cosgrove57f04b82023-03-14 12:03:47 +0000300 * It will expand to nothing on some systems.
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000301 * Can be used outside functions (but don't add a trailing ';' in that case:
302 * the semicolon is included here to avoid triggering -Wextra-semi when
303 * MBEDTLS_STATIC_ASSERT() expands to nothing).
304 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
305 * defines static_assert even with -std=c99, but then complains about it.
306 */
307#if defined(static_assert) && !defined(__FreeBSD__)
308#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000309#else
310#define MBEDTLS_STATIC_ASSERT(expr, msg)
311#endif
312
Dave Rodgman360e04f2023-06-09 17:18:32 +0100313/* Define compiler branch hints */
314#if defined(__has_builtin)
315#if __has_builtin(__builtin_expect)
Dave Rodgmane9fcffd2023-07-19 15:42:19 +0100316#define MBEDTLS_LIKELY(x) __builtin_expect(!!(x), 1)
317#define MBEDTLS_UNLIKELY(x) __builtin_expect(!!(x), 0)
Dave Rodgman360e04f2023-06-09 17:18:32 +0100318#endif
319#endif
320#if !defined(MBEDTLS_LIKELY)
321#define MBEDTLS_LIKELY(x) x
322#define MBEDTLS_UNLIKELY(x) x
323#endif
324
Dave Rodgman7fdfd702023-06-15 18:42:25 +0100325#if defined(__GNUC__) && !defined(__ARMCC_VERSION) && !defined(__clang__) \
326 && !defined(__llvm__) && !defined(__INTEL_COMPILER)
327/* Defined if the compiler really is gcc and not clang, etc */
328#define MBEDTLS_COMPILER_IS_GCC
329#endif
330
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100331/* For gcc -Os, override with -O2 for a given function.
332 *
333 * This will not affect behaviour for other optimisation settings, e.g. -O0.
334 */
Dave Rodgmanb055f752023-06-15 18:42:59 +0100335#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100336#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))
Dave Rodgmanb055f752023-06-15 18:42:59 +0100337#else
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100338#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
Dave Rodgmanb055f752023-06-15 18:42:59 +0100339#endif
340
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200341#endif /* MBEDTLS_LIBRARY_COMMON_H */