blob: fd2aecb208c183160b30f514a7888f1c77342e5e [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 Rodgman6f40f8b2023-05-22 18:21:20 +010037#endif /* __ARM_NEON */
38
Dave Rodgman4ffd7c72023-09-05 11:43:02 +010039#if defined(_M_ARM64) || defined(_M_ARM64EC)
40#include <arm64_neon.h>
41#define MBEDTLS_HAVE_NEON_INTRINSICS
42#endif
43
Gilles Peskinec4672fd2019-09-11 13:39:11 +020044/** Helper to define a function as static except when building invasive tests.
45 *
46 * If a function is only used inside its own source file and should be
47 * declared `static` to allow the compiler to optimize for code size,
48 * but that function has unit tests, define it with
49 * ```
50 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
51 * ```
52 * and declare it in a header in the `library/` directory with
53 * ```
54 * #if defined(MBEDTLS_TEST_HOOKS)
55 * int mbedtls_foo(...);
56 * #endif
57 * ```
58 */
59#if defined(MBEDTLS_TEST_HOOKS)
60#define MBEDTLS_STATIC_TESTABLE
61#else
62#define MBEDTLS_STATIC_TESTABLE static
63#endif
64
TRodziewicz7871c2e2021-07-07 17:29:43 +020065#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010066extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
67#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
68 do { \
69 if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
70 { \
71 (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
72 } \
73 } while (0)
TRodziewicz7871c2e2021-07-07 17:29:43 +020074#else
Gilles Peskine449bd832023-01-11 14:50:10 +010075#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
TRodziewicz7871c2e2021-07-07 17:29:43 +020076#endif /* defined(MBEDTLS_TEST_HOOKS) */
77
Andrzej Kurekb22b9772023-05-30 09:44:20 -040078/** \def ARRAY_LENGTH
79 * Return the number of elements of a static or stack array.
80 *
81 * \param array A value of array (not pointer) type.
82 *
83 * \return The number of elements of the array.
84 */
85/* A correct implementation of ARRAY_LENGTH, but which silently gives
86 * a nonsensical result if called with a pointer rather than an array. */
87#define ARRAY_LENGTH_UNSAFE(array) \
88 (sizeof(array) / sizeof(*(array)))
89
90#if defined(__GNUC__)
91/* Test if arg and &(arg)[0] have the same type. This is true if arg is
92 * an array but not if it's a pointer. */
93#define IS_ARRAY_NOT_POINTER(arg) \
94 (!__builtin_types_compatible_p(__typeof__(arg), \
95 __typeof__(&(arg)[0])))
96/* A compile-time constant with the value 0. If `const_expr` is not a
97 * compile-time constant with a nonzero value, cause a compile-time error. */
98#define STATIC_ASSERT_EXPR(const_expr) \
99 (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
100
101/* Return the scalar value `value` (possibly promoted). This is a compile-time
102 * constant if `value` is. `condition` must be a compile-time constant.
103 * If `condition` is false, arrange to cause a compile-time error. */
104#define STATIC_ASSERT_THEN_RETURN(condition, value) \
105 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
106
107#define ARRAY_LENGTH(array) \
108 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
109 ARRAY_LENGTH_UNSAFE(array)))
110
111#else
112/* If we aren't sure the compiler supports our non-standard tricks,
113 * fall back to the unsafe implementation. */
114#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
115#endif
Mateusz Starzyk57d1d192021-05-27 14:39:53 +0200116/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +0200117 *
118 * Although structs defined in header files are publicly available,
119 * their members are private and should not be accessed by the user.
120 */
121#define MBEDTLS_ALLOW_PRIVATE_ACCESS
122
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100123/**
124 * \brief Securely zeroize a buffer then free it.
125 *
Tom Cosgrove3a11bb82023-07-18 16:26:29 +0100126 * Similar to making consecutive calls to
127 * \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100128 * code size savings, and potential for optimisation in the future.
129 *
Tom Cosgrove3a11bb82023-07-18 16:26:29 +0100130 * Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0.
131 *
132 * \param buf Buffer to be zeroized then freed.
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100133 * \param len Length of the buffer in bytes
134 */
135void mbedtls_zeroize_and_free(void *buf, size_t len);
136
Gilles Peskine42649d92022-11-23 14:15:57 +0100137/** Return an offset into a buffer.
138 *
139 * This is just the addition of an offset to a pointer, except that this
140 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +0100141 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
142 * A null pointer is a valid buffer pointer when the size is 0, for example
143 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +0100144 *
145 * \param p Pointer to a buffer of at least n bytes.
146 * This may be \p NULL if \p n is zero.
147 * \param n An offset in bytes.
148 * \return Pointer to offset \p n in the buffer \p p.
149 * Note that this is only a valid pointer if the size of the
150 * buffer is at least \p n + 1.
151 */
152static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100154{
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100156}
157
158/** Return an offset into a read-only buffer.
159 *
Gilles Peskine7d237782022-11-25 13:34:59 +0100160 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +0100161 *
162 * \param p Pointer to a buffer of at least n bytes.
163 * This may be \p NULL if \p n is zero.
164 * \param n An offset in bytes.
165 * \return Pointer to offset \p n in the buffer \p p.
166 * Note that this is only a valid pointer if the size of the
167 * buffer is at least \p n + 1.
168 */
169static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 const unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100171{
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100173}
174
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000175/**
176 * Perform a fast block XOR operation, such that
177 * r[i] = a[i] ^ b[i] where 0 <= i < n
178 *
179 * \param r Pointer to result (buffer of at least \p n bytes). \p r
180 * may be equal to either \p a or \p b, but behaviour when
181 * it overlaps in other ways is undefined.
182 * \param a Pointer to input (buffer of at least \p n bytes)
183 * \param b Pointer to input (buffer of at least \p n bytes)
184 * \param n Number of bytes to process.
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000187{
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000188 size_t i = 0;
189#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgmana0f10da2023-09-05 11:43:17 +0100190#if defined(MBEDTLS_HAVE_NEON_INTRINSICS)
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100191 for (; (i + 16) <= n; i += 16) {
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100192 uint8x16_t v1 = vld1q_u8(a + i);
193 uint8x16_t v2 = vld1q_u8(b + i);
Dave Rodgman2070c202023-06-07 16:25:58 +0100194 uint8x16_t x = veorq_u8(v1, v2);
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100195 vst1q_u8(r + i, x);
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100196 }
Dave Rodgmanbe092862023-08-08 10:42:55 +0100197#elif defined(__amd64__) || defined(__x86_64__) || \
198 defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
Dave Rodgman0805ad12023-05-19 11:48:10 +0100199 /* This codepath probably only makes sense on architectures with 64-bit registers */
200 for (; (i + 8) <= n; i += 8) {
201 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
202 mbedtls_put_unaligned_uint64(r + i, x);
203 }
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100204#else
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000205 for (; (i + 4) <= n; i += 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
207 mbedtls_put_unaligned_uint32(r + i, x);
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000208 }
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000209#endif
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100210#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 for (; i < n; i++) {
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000212 r[i] = a[i] ^ b[i];
213 }
214}
215
Dave Rodgman03bb5262023-06-15 18:43:24 +0100216/**
217 * Perform a fast block XOR operation, such that
218 * r[i] = a[i] ^ b[i] where 0 <= i < n
219 *
220 * In some situations, this can perform better than mbedtls_xor (e.g., it's about 5%
221 * better in AES-CBC).
222 *
223 * \param r Pointer to result (buffer of at least \p n bytes). \p r
224 * may be equal to either \p a or \p b, but behaviour when
225 * it overlaps in other ways is undefined.
226 * \param a Pointer to input (buffer of at least \p n bytes)
227 * \param b Pointer to input (buffer of at least \p n bytes)
228 * \param n Number of bytes to process.
229 */
Dave Rodgman2dd15b32023-06-15 20:27:53 +0100230static inline void mbedtls_xor_no_simd(unsigned char *r,
231 const unsigned char *a,
232 const unsigned char *b,
233 size_t n)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100234{
235 size_t i = 0;
236#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgmanbe092862023-08-08 10:42:55 +0100237#if defined(__amd64__) || defined(__x86_64__) || \
238 defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100239 /* This codepath probably only makes sense on architectures with 64-bit registers */
240 for (; (i + 8) <= n; i += 8) {
241 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
242 mbedtls_put_unaligned_uint64(r + i, x);
243 }
244#else
245 for (; (i + 4) <= n; i += 4) {
246 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
247 mbedtls_put_unaligned_uint32(r + i, x);
248 }
249#endif
250#endif
251 for (; i < n; i++) {
252 r[i] = a[i] ^ b[i];
253 }
254}
255
Jerry Yu6c983522021-09-24 12:45:36 +0800256/* Fix MSVC C99 compatible issue
257 * MSVC support __func__ from visual studio 2015( 1900 )
258 * Use MSVC predefine macro to avoid name check fail.
259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
Jerry Yud52398d2021-09-28 16:13:44 +0800261#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800262#endif
263
Dave Rodgmanfa960262023-01-10 11:14:02 +0000264/* Define `asm` for compilers which don't define it. */
265/* *INDENT-OFF* */
266#ifndef asm
Agathiyan Bragadeesh789e50e2023-07-14 16:59:36 +0100267#if defined(__IAR_SYSTEMS_ICC__)
268#define asm __asm
269#else
Dave Rodgmanfa960262023-01-10 11:14:02 +0000270#define asm __asm__
271#endif
Agathiyan Bragadeesh789e50e2023-07-14 16:59:36 +0100272#endif
Dave Rodgmanfa960262023-01-10 11:14:02 +0000273/* *INDENT-ON* */
274
Dave Rodgman0400ae22023-06-21 16:14:46 +0100275/*
Dave Rodgman28e2ca52023-06-27 15:25:38 +0100276 * Define the constraint used for read-only pointer operands to aarch64 asm.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100277 *
278 * This is normally the usual "r", but for aarch64_32 (aka ILP32,
279 * as found in watchos), "p" is required to avoid warnings from clang.
Dave Rodgmane6c99962023-06-21 21:16:23 +0100280 *
281 * Note that clang does not recognise '+p' or '=p', and armclang
Dave Rodgman28e2ca52023-06-27 15:25:38 +0100282 * does not recognise 'p' at all. Therefore, to update a pointer from
283 * aarch64 assembly, it is necessary to use something like:
284 *
285 * uintptr_t uptr = (uintptr_t) ptr;
286 * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )
287 * ptr = (void*) uptr;
288 *
289 * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100290 */
291#if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
292#if UINTPTR_MAX == 0xfffffffful
293/* ILP32: Specify the pointer operand slightly differently, as per #7787. */
294#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
295#elif UINTPTR_MAX == 0xfffffffffffffffful
296/* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
297#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
298#else
Antonio de Angelis1ee4d122023-08-16 12:26:37 +0100299#error "Unrecognised pointer size for aarch64"
Dave Rodgman0400ae22023-06-21 16:14:46 +0100300#endif
301#endif
302
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000303/* Always provide a static assert macro, so it can be used unconditionally.
Tom Cosgrove57f04b82023-03-14 12:03:47 +0000304 * It will expand to nothing on some systems.
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000305 * Can be used outside functions (but don't add a trailing ';' in that case:
306 * the semicolon is included here to avoid triggering -Wextra-semi when
307 * MBEDTLS_STATIC_ASSERT() expands to nothing).
308 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
309 * defines static_assert even with -std=c99, but then complains about it.
310 */
311#if defined(static_assert) && !defined(__FreeBSD__)
312#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000313#else
314#define MBEDTLS_STATIC_ASSERT(expr, msg)
315#endif
316
Dave Rodgman360e04f2023-06-09 17:18:32 +0100317/* Define compiler branch hints */
318#if defined(__has_builtin)
319#if __has_builtin(__builtin_expect)
Dave Rodgmane9fcffd2023-07-19 15:42:19 +0100320#define MBEDTLS_LIKELY(x) __builtin_expect(!!(x), 1)
321#define MBEDTLS_UNLIKELY(x) __builtin_expect(!!(x), 0)
Dave Rodgman360e04f2023-06-09 17:18:32 +0100322#endif
323#endif
324#if !defined(MBEDTLS_LIKELY)
325#define MBEDTLS_LIKELY(x) x
326#define MBEDTLS_UNLIKELY(x) x
327#endif
328
Dave Rodgman7fdfd702023-06-15 18:42:25 +0100329#if defined(__GNUC__) && !defined(__ARMCC_VERSION) && !defined(__clang__) \
330 && !defined(__llvm__) && !defined(__INTEL_COMPILER)
331/* Defined if the compiler really is gcc and not clang, etc */
332#define MBEDTLS_COMPILER_IS_GCC
333#endif
334
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100335/* For gcc -Os, override with -O2 for a given function.
336 *
337 * This will not affect behaviour for other optimisation settings, e.g. -O0.
338 */
Dave Rodgmanb055f752023-06-15 18:42:59 +0100339#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100340#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))
Dave Rodgmanb055f752023-06-15 18:42:59 +0100341#else
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100342#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
Dave Rodgmanb055f752023-06-15 18:42:59 +0100343#endif
344
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200345#endif /* MBEDTLS_LIBRARY_COMMON_H */