blob: eb159a7c48755798633a6c61de0a8fd14f95a2d0 [file] [log] [blame]
Jerome Forissier79013242021-07-28 10:24:04 +02001/**
2 * \file common.h
3 *
4 * \brief Utility macros for internal use in the library
5 */
6/*
7 * Copyright The Mbed TLS Contributors
8 * 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.
21 */
22
23#ifndef MBEDTLS_LIBRARY_COMMON_H
24#define MBEDTLS_LIBRARY_COMMON_H
25
Jens Wiklander32b31802023-10-06 16:59:46 +020026#include "mbedtls/build_info.h"
27#include "alignment.h"
Jerome Forissier79013242021-07-28 10:24:04 +020028
Jens Wiklander32b31802023-10-06 16:59:46 +020029#include <assert.h>
30#include <stddef.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020031#include <stdint.h>
Jens Wiklander32b31802023-10-06 16:59:46 +020032#include <stddef.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020033
Jerome Forissier79013242021-07-28 10:24:04 +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
Jens Wiklander32b31802023-10-06 16:59:46 +020055#if defined(MBEDTLS_TEST_HOOKS)
56extern 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)
64#else
65#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
66#endif /* defined(MBEDTLS_TEST_HOOKS) */
Jerome Forissier039e02d2022-08-09 17:10:15 +020067
Jens Wiklander32b31802023-10-06 16:59:46 +020068/** Allow library to access its structs' private members.
Jerome Forissier039e02d2022-08-09 17:10:15 +020069 *
Jens Wiklander32b31802023-10-06 16:59:46 +020070 * Although structs defined in header files are publicly available,
71 * their members are private and should not be accessed by the user.
Jerome Forissier039e02d2022-08-09 17:10:15 +020072 */
Jens Wiklander32b31802023-10-06 16:59:46 +020073#define MBEDTLS_ALLOW_PRIVATE_ACCESS
Jerome Forissier039e02d2022-08-09 17:10:15 +020074
Jens Wiklander32b31802023-10-06 16:59:46 +020075/** Return an offset into a buffer.
Jerome Forissier039e02d2022-08-09 17:10:15 +020076 *
Jens Wiklander32b31802023-10-06 16:59:46 +020077 * 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.
79 * (`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.)
82 *
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.
Jerome Forissier039e02d2022-08-09 17:10:15 +020089 */
Jens Wiklander32b31802023-10-06 16:59:46 +020090static inline unsigned char *mbedtls_buffer_offset(
91 unsigned char *p, size_t n)
92{
93 return p == NULL ? NULL : p + n;
Jerome Forissier039e02d2022-08-09 17:10:15 +020094}
Jerome Forissier039e02d2022-08-09 17:10:15 +020095
Jens Wiklander32b31802023-10-06 16:59:46 +020096/** Return an offset into a read-only buffer.
Jerome Forissier039e02d2022-08-09 17:10:15 +020097 *
Jens Wiklander32b31802023-10-06 16:59:46 +020098 * Similar to mbedtls_buffer_offset(), but for const pointers.
Jerome Forissier039e02d2022-08-09 17:10:15 +020099 *
Jens Wiklander32b31802023-10-06 16:59:46 +0200100 * \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.
Jerome Forissier039e02d2022-08-09 17:10:15 +0200106 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200107static inline const unsigned char *mbedtls_buffer_offset_const(
108 const unsigned char *p, size_t n)
109{
110 return p == NULL ? NULL : p + n;
Jerome Forissier039e02d2022-08-09 17:10:15 +0200111}
Jerome Forissier039e02d2022-08-09 17:10:15 +0200112
113/**
Jens Wiklander32b31802023-10-06 16:59:46 +0200114 * Perform a fast block XOR operation, such that
115 * r[i] = a[i] ^ b[i] where 0 <= i < n
Jerome Forissier039e02d2022-08-09 17:10:15 +0200116 *
Jens Wiklander32b31802023-10-06 16:59:46 +0200117 * \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.
Jerome Forissier039e02d2022-08-09 17:10:15 +0200123 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200124inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n)
125{
126 size_t i = 0;
127#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
128 for (; (i + 4) <= n; i += 4) {
129 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
130 mbedtls_put_unaligned_uint32(r + i, x);
131 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200132#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200133 for (; i < n; i++) {
134 r[i] = a[i] ^ b[i];
135 }
Jerome Forissier039e02d2022-08-09 17:10:15 +0200136}
Jens Wiklander32b31802023-10-06 16:59:46 +0200137
138/* Fix MSVC C99 compatible issue
139 * MSVC support __func__ from visual studio 2015( 1900 )
140 * Use MSVC predefine macro to avoid name check fail.
141 */
142#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
143#define /*no-check-names*/ __func__ __FUNCTION__
Jerome Forissier039e02d2022-08-09 17:10:15 +0200144#endif
145
Jens Wiklander32b31802023-10-06 16:59:46 +0200146/* Define `asm` for compilers which don't define it. */
147/* *INDENT-OFF* */
148#ifndef asm
149#define asm __asm__
Jerome Forissier039e02d2022-08-09 17:10:15 +0200150#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200151/* *INDENT-ON* */
Jerome Forissier039e02d2022-08-09 17:10:15 +0200152
Jens Wiklander32b31802023-10-06 16:59:46 +0200153/* Always provide a static assert macro, so it can be used unconditionally.
154 * It will expand to nothing on some systems.
155 * Can be used outside functions (but don't add a trailing ';' in that case:
156 * the semicolon is included here to avoid triggering -Wextra-semi when
157 * MBEDTLS_STATIC_ASSERT() expands to nothing).
158 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
159 * defines static_assert even with -std=c99, but then complains about it.
Jerome Forissier039e02d2022-08-09 17:10:15 +0200160 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200161#if defined(static_assert) && !defined(__FreeBSD__)
162#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
163#else
164#define MBEDTLS_STATIC_ASSERT(expr, msg)
Jerome Forissier039e02d2022-08-09 17:10:15 +0200165#endif
166
Jerome Forissier79013242021-07-28 10:24:04 +0200167#endif /* MBEDTLS_LIBRARY_COMMON_H */