blob: ba9cb75c08ddffec9974ece136654d1e87d72476 [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>
36#endif /* __ARM_NEON */
37
Gilles Peskinec4672fd2019-09-11 13:39:11 +020038/** 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
TRodziewicz7871c2e2021-07-07 17:29:43 +020059#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010060extern 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)
TRodziewicz7871c2e2021-07-07 17:29:43 +020068#else
Gilles Peskine449bd832023-01-11 14:50:10 +010069#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
TRodziewicz7871c2e2021-07-07 17:29:43 +020070#endif /* defined(MBEDTLS_TEST_HOOKS) */
71
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020072/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020073 *
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 Peskine42649d92022-11-23 14:15:57 +010079/** 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 Peskine7d237782022-11-25 13:34:59 +010083 * (`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 Peskine42649d92022-11-23 14:15:57 +010086 *
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 */
94static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine449bd832023-01-11 14:50:10 +010095 unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +010096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +010098}
99
100/** Return an offset into a read-only buffer.
101 *
Gilles Peskine7d237782022-11-25 13:34:59 +0100102 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +0100103 *
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 */
111static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 const unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100113{
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100115}
116
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000117/**
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 Peskine449bd832023-01-11 14:50:10 +0100128inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000129{
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000130 size_t i = 0;
131#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgman3f47b3f2023-05-23 16:11:22 +0100132#if defined(__ARM_NEON)
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100133 for (; (i + 16) <= n; i += 16) {
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100134 uint8x16_t v1 = vld1q_u8(a + i);
135 uint8x16_t v2 = vld1q_u8(b + i);
Dave Rodgman2070c202023-06-07 16:25:58 +0100136 uint8x16_t x = veorq_u8(v1, v2);
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100137 vst1q_u8(r + i, x);
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100138 }
139#elif defined(__amd64__) || defined(__x86_64__) || defined(__aarch64__)
Dave Rodgman0805ad12023-05-19 11:48:10 +0100140 /* 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 Rodgman5c394ff2023-06-09 20:10:36 +0100145#else
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000146 for (; (i + 4) <= n; i += 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
148 mbedtls_put_unaligned_uint32(r + i, x);
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000149 }
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000150#endif
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100151#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 for (; i < n; i++) {
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000153 r[i] = a[i] ^ b[i];
154 }
155}
156
Jerry Yu6c983522021-09-24 12:45:36 +0800157/* Fix MSVC C99 compatible issue
158 * MSVC support __func__ from visual studio 2015( 1900 )
159 * Use MSVC predefine macro to avoid name check fail.
160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
Jerry Yud52398d2021-09-28 16:13:44 +0800162#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800163#endif
164
Dave Rodgmanfa960262023-01-10 11:14:02 +0000165/* Define `asm` for compilers which don't define it. */
166/* *INDENT-OFF* */
167#ifndef asm
168#define asm __asm__
169#endif
170/* *INDENT-ON* */
171
Dave Rodgman0400ae22023-06-21 16:14:46 +0100172/*
173 * Define the constraint used for pointer operands to asm.
174 *
175 * This is normally the usual "r", but for aarch64_32 (aka ILP32,
176 * as found in watchos), "p" is required to avoid warnings from clang.
Dave Rodgmane6c99962023-06-21 21:16:23 +0100177 *
178 * Note that clang does not recognise '+p' or '=p', and armclang
179 * does not recognise 'p' at all.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100180 */
181#if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
182#if UINTPTR_MAX == 0xfffffffful
183/* ILP32: Specify the pointer operand slightly differently, as per #7787. */
184#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
185#elif UINTPTR_MAX == 0xfffffffffffffffful
186/* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
187#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
188#else
189#error Unrecognised pointer size for aarch64
190#endif
191#endif
192
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000193/* Always provide a static assert macro, so it can be used unconditionally.
Tom Cosgrove57f04b82023-03-14 12:03:47 +0000194 * It will expand to nothing on some systems.
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000195 * Can be used outside functions (but don't add a trailing ';' in that case:
196 * the semicolon is included here to avoid triggering -Wextra-semi when
197 * MBEDTLS_STATIC_ASSERT() expands to nothing).
198 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
199 * defines static_assert even with -std=c99, but then complains about it.
200 */
201#if defined(static_assert) && !defined(__FreeBSD__)
202#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000203#else
204#define MBEDTLS_STATIC_ASSERT(expr, msg)
205#endif
206
Dave Rodgman360e04f2023-06-09 17:18:32 +0100207/* Define compiler branch hints */
208#if defined(__has_builtin)
209#if __has_builtin(__builtin_expect)
Dave Rodgman159dc092023-06-09 19:46:07 +0100210#define MBEDTLS_LIKELY(x) __builtin_expect((x), 1)
211#define MBEDTLS_UNLIKELY(x) __builtin_expect((x), 0)
Dave Rodgman360e04f2023-06-09 17:18:32 +0100212#endif
213#endif
214#if !defined(MBEDTLS_LIKELY)
215#define MBEDTLS_LIKELY(x) x
216#define MBEDTLS_UNLIKELY(x) x
217#endif
218
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200219#endif /* MBEDTLS_LIBRARY_COMMON_H */