blob: 68af8405edd1ca075c4d4259f1b44469e13a3b31 [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
Gilles Peskinec4672fd2019-09-11 13:39:11 +020034/** Helper to define a function as static except when building invasive tests.
35 *
36 * If a function is only used inside its own source file and should be
37 * declared `static` to allow the compiler to optimize for code size,
38 * but that function has unit tests, define it with
39 * ```
40 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
41 * ```
42 * and declare it in a header in the `library/` directory with
43 * ```
44 * #if defined(MBEDTLS_TEST_HOOKS)
45 * int mbedtls_foo(...);
46 * #endif
47 * ```
48 */
49#if defined(MBEDTLS_TEST_HOOKS)
50#define MBEDTLS_STATIC_TESTABLE
51#else
52#define MBEDTLS_STATIC_TESTABLE static
53#endif
54
TRodziewicz7871c2e2021-07-07 17:29:43 +020055#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010056extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
57#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
58 do { \
59 if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
60 { \
61 (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
62 } \
63 } while (0)
TRodziewicz7871c2e2021-07-07 17:29:43 +020064#else
Gilles Peskine449bd832023-01-11 14:50:10 +010065#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
TRodziewicz7871c2e2021-07-07 17:29:43 +020066#endif /* defined(MBEDTLS_TEST_HOOKS) */
67
Andrzej Kurekb22b9772023-05-30 09:44:20 -040068/** \def ARRAY_LENGTH
69 * Return the number of elements of a static or stack array.
70 *
71 * \param array A value of array (not pointer) type.
72 *
73 * \return The number of elements of the array.
74 */
75/* A correct implementation of ARRAY_LENGTH, but which silently gives
76 * a nonsensical result if called with a pointer rather than an array. */
77#define ARRAY_LENGTH_UNSAFE(array) \
78 (sizeof(array) / sizeof(*(array)))
79
80#if defined(__GNUC__)
81/* Test if arg and &(arg)[0] have the same type. This is true if arg is
82 * an array but not if it's a pointer. */
83#define IS_ARRAY_NOT_POINTER(arg) \
84 (!__builtin_types_compatible_p(__typeof__(arg), \
85 __typeof__(&(arg)[0])))
86/* A compile-time constant with the value 0. If `const_expr` is not a
87 * compile-time constant with a nonzero value, cause a compile-time error. */
88#define STATIC_ASSERT_EXPR(const_expr) \
89 (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
90
91/* Return the scalar value `value` (possibly promoted). This is a compile-time
92 * constant if `value` is. `condition` must be a compile-time constant.
93 * If `condition` is false, arrange to cause a compile-time error. */
94#define STATIC_ASSERT_THEN_RETURN(condition, value) \
95 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
96
97#define ARRAY_LENGTH(array) \
98 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
99 ARRAY_LENGTH_UNSAFE(array)))
100
101#else
102/* If we aren't sure the compiler supports our non-standard tricks,
103 * fall back to the unsafe implementation. */
104#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
105#endif
Mateusz Starzyk57d1d192021-05-27 14:39:53 +0200106/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +0200107 *
108 * Although structs defined in header files are publicly available,
109 * their members are private and should not be accessed by the user.
110 */
111#define MBEDTLS_ALLOW_PRIVATE_ACCESS
112
Gilles Peskine42649d92022-11-23 14:15:57 +0100113/** Return an offset into a buffer.
114 *
115 * This is just the addition of an offset to a pointer, except that this
116 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +0100117 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
118 * A null pointer is a valid buffer pointer when the size is 0, for example
119 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +0100120 *
121 * \param p Pointer to a buffer of at least n bytes.
122 * This may be \p NULL if \p n is zero.
123 * \param n An offset in bytes.
124 * \return Pointer to offset \p n in the buffer \p p.
125 * Note that this is only a valid pointer if the size of the
126 * buffer is at least \p n + 1.
127 */
128static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100130{
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100132}
133
134/** Return an offset into a read-only buffer.
135 *
Gilles Peskine7d237782022-11-25 13:34:59 +0100136 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +0100137 *
138 * \param p Pointer to a buffer of at least n bytes.
139 * This may be \p NULL if \p n is zero.
140 * \param n An offset in bytes.
141 * \return Pointer to offset \p n in the buffer \p p.
142 * Note that this is only a valid pointer if the size of the
143 * buffer is at least \p n + 1.
144 */
145static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 const unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100147{
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100149}
150
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000151/**
152 * Perform a fast block XOR operation, such that
153 * r[i] = a[i] ^ b[i] where 0 <= i < n
154 *
155 * \param r Pointer to result (buffer of at least \p n bytes). \p r
156 * may be equal to either \p a or \p b, but behaviour when
157 * it overlaps in other ways is undefined.
158 * \param a Pointer to input (buffer of at least \p n bytes)
159 * \param b Pointer to input (buffer of at least \p n bytes)
160 * \param n Number of bytes to process.
161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000163{
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000164 size_t i = 0;
165#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
166 for (; (i + 4) <= n; i += 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
168 mbedtls_put_unaligned_uint32(r + i, x);
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000169 }
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000170#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 for (; i < n; i++) {
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000172 r[i] = a[i] ^ b[i];
173 }
174}
175
Jerry Yu6c983522021-09-24 12:45:36 +0800176/* Fix MSVC C99 compatible issue
177 * MSVC support __func__ from visual studio 2015( 1900 )
178 * Use MSVC predefine macro to avoid name check fail.
179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
Jerry Yud52398d2021-09-28 16:13:44 +0800181#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800182#endif
183
Dave Rodgmanfa960262023-01-10 11:14:02 +0000184/* Define `asm` for compilers which don't define it. */
185/* *INDENT-OFF* */
186#ifndef asm
187#define asm __asm__
188#endif
189/* *INDENT-ON* */
190
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000191/* Always provide a static assert macro, so it can be used unconditionally.
Tom Cosgrove57f04b82023-03-14 12:03:47 +0000192 * It will expand to nothing on some systems.
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000193 * Can be used outside functions (but don't add a trailing ';' in that case:
194 * the semicolon is included here to avoid triggering -Wextra-semi when
195 * MBEDTLS_STATIC_ASSERT() expands to nothing).
196 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
197 * defines static_assert even with -std=c99, but then complains about it.
198 */
199#if defined(static_assert) && !defined(__FreeBSD__)
200#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000201#else
202#define MBEDTLS_STATIC_ASSERT(expr, msg)
203#endif
204
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200205#endif /* MBEDTLS_LIBRARY_COMMON_H */