blob: 82001a9f9d9489a871b0f619733f82572ab41396 [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
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020068/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020069 *
70 * Although structs defined in header files are publicly available,
71 * their members are private and should not be accessed by the user.
72 */
73#define MBEDTLS_ALLOW_PRIVATE_ACCESS
74
Gilles Peskine42649d92022-11-23 14:15:57 +010075/** Return an offset into a buffer.
76 *
77 * This is just the addition of an offset to a pointer, except that this
78 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +010079 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
80 * A null pointer is a valid buffer pointer when the size is 0, for example
81 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +010082 *
83 * \param p Pointer to a buffer of at least n bytes.
84 * This may be \p NULL if \p n is zero.
85 * \param n An offset in bytes.
86 * \return Pointer to offset \p n in the buffer \p p.
87 * Note that this is only a valid pointer if the size of the
88 * buffer is at least \p n + 1.
89 */
90static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine449bd832023-01-11 14:50:10 +010091 unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +010092{
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +010094}
95
96/** Return an offset into a read-only buffer.
97 *
Gilles Peskine7d237782022-11-25 13:34:59 +010098 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +010099 *
100 * \param p Pointer to a buffer of at least n bytes.
101 * This may be \p NULL if \p n is zero.
102 * \param n An offset in bytes.
103 * \return Pointer to offset \p n in the buffer \p p.
104 * Note that this is only a valid pointer if the size of the
105 * buffer is at least \p n + 1.
106 */
107static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 const unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100109{
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100111}
112
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000113/**
114 * Perform a fast block XOR operation, such that
115 * r[i] = a[i] ^ b[i] where 0 <= i < n
116 *
117 * \param r Pointer to result (buffer of at least \p n bytes). \p r
118 * may be equal to either \p a or \p b, but behaviour when
119 * it overlaps in other ways is undefined.
120 * \param a Pointer to input (buffer of at least \p n bytes)
121 * \param b Pointer to input (buffer of at least \p n bytes)
122 * \param n Number of bytes to process.
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000125{
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000126 size_t i = 0;
127#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgman0805ad12023-05-19 11:48:10 +0100128#if defined(__amd64__) || defined(__x86_64__) || defined(__aarch64__)
129 /* This codepath probably only makes sense on architectures with 64-bit registers */
130 for (; (i + 8) <= n; i += 8) {
131 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
132 mbedtls_put_unaligned_uint64(r + i, x);
133 }
134#endif
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000135 for (; (i + 4) <= n; i += 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
137 mbedtls_put_unaligned_uint32(r + i, x);
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000138 }
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000139#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 for (; i < n; i++) {
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000141 r[i] = a[i] ^ b[i];
142 }
143}
144
Jerry Yu6c983522021-09-24 12:45:36 +0800145/* Fix MSVC C99 compatible issue
146 * MSVC support __func__ from visual studio 2015( 1900 )
147 * Use MSVC predefine macro to avoid name check fail.
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
Jerry Yud52398d2021-09-28 16:13:44 +0800150#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800151#endif
152
Dave Rodgmanfa960262023-01-10 11:14:02 +0000153/* Define `asm` for compilers which don't define it. */
154/* *INDENT-OFF* */
155#ifndef asm
156#define asm __asm__
157#endif
158/* *INDENT-ON* */
159
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000160/* Always provide a static assert macro, so it can be used unconditionally.
Tom Cosgrove57f04b82023-03-14 12:03:47 +0000161 * It will expand to nothing on some systems.
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000162 * Can be used outside functions (but don't add a trailing ';' in that case:
163 * the semicolon is included here to avoid triggering -Wextra-semi when
164 * MBEDTLS_STATIC_ASSERT() expands to nothing).
165 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
166 * defines static_assert even with -std=c99, but then complains about it.
167 */
168#if defined(static_assert) && !defined(__FreeBSD__)
169#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000170#else
171#define MBEDTLS_STATIC_ASSERT(expr, msg)
172#endif
173
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200174#endif /* MBEDTLS_LIBRARY_COMMON_H */