blob: 9d3b8fe9336670a139e7064258806138207b64b0 [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
Gilles Peskine42649d92022-11-23 14:15:57 +010029#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010030#include <stdint.h>
Dave Rodgmanc3d80412022-11-22 15:01:39 +000031#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010032
Gilles Peskinec4672fd2019-09-11 13:39:11 +020033/** Helper to define a function as static except when building invasive tests.
34 *
35 * If a function is only used inside its own source file and should be
36 * declared `static` to allow the compiler to optimize for code size,
37 * but that function has unit tests, define it with
38 * ```
39 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
40 * ```
41 * and declare it in a header in the `library/` directory with
42 * ```
43 * #if defined(MBEDTLS_TEST_HOOKS)
44 * int mbedtls_foo(...);
45 * #endif
46 * ```
47 */
48#if defined(MBEDTLS_TEST_HOOKS)
49#define MBEDTLS_STATIC_TESTABLE
50#else
51#define MBEDTLS_STATIC_TESTABLE static
52#endif
53
TRodziewicz7871c2e2021-07-07 17:29:43 +020054#if defined(MBEDTLS_TEST_HOOKS)
55extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
56#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
57 do { \
58 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
59 { \
60 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
61 } \
62 } while( 0 )
63#else
64#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
65#endif /* defined(MBEDTLS_TEST_HOOKS) */
66
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020067/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020068 *
69 * Although structs defined in header files are publicly available,
70 * their members are private and should not be accessed by the user.
71 */
72#define MBEDTLS_ALLOW_PRIVATE_ACCESS
73
Gilles Peskine42649d92022-11-23 14:15:57 +010074/** Return an offset into a buffer.
75 *
76 * This is just the addition of an offset to a pointer, except that this
77 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +010078 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
79 * A null pointer is a valid buffer pointer when the size is 0, for example
80 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +010081 *
82 * \param p Pointer to a buffer of at least n bytes.
83 * This may be \p NULL if \p n is zero.
84 * \param n An offset in bytes.
85 * \return Pointer to offset \p n in the buffer \p p.
86 * Note that this is only a valid pointer if the size of the
87 * buffer is at least \p n + 1.
88 */
89static inline unsigned char *mbedtls_buffer_offset(
90 unsigned char *p, size_t n )
91{
92 return( p == NULL ? NULL : p + n );
93}
94
95/** Return an offset into a read-only buffer.
96 *
Gilles Peskine7d237782022-11-25 13:34:59 +010097 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +010098 *
99 * \param p Pointer to a buffer of at least n bytes.
100 * This may be \p NULL if \p n is zero.
101 * \param n An offset in bytes.
102 * \return Pointer to offset \p n in the buffer \p p.
103 * Note that this is only a valid pointer if the size of the
104 * buffer is at least \p n + 1.
105 */
106static inline const unsigned char *mbedtls_buffer_offset_const(
107 const unsigned char *p, size_t n )
108{
109 return( p == NULL ? NULL : p + n );
110}
111
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000112/**
113 * Perform a fast block XOR operation, such that
114 * r[i] = a[i] ^ b[i] where 0 <= i < n
115 *
116 * \param r Pointer to result (buffer of at least \p n bytes). \p r
117 * may be equal to either \p a or \p b, but behaviour when
118 * it overlaps in other ways is undefined.
119 * \param a Pointer to input (buffer of at least \p n bytes)
120 * \param b Pointer to input (buffer of at least \p n bytes)
121 * \param n Number of bytes to process.
122 */
Dave Rodgman7a910a82022-11-24 21:17:40 +0000123inline void mbedtls_xor( unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000124{
Dave Rodgman96d61d12022-11-24 19:33:22 +0000125 size_t i;
Dave Rodgmanc5885882022-11-24 20:35:04 +0000126 for ( i = 0; ( i + 4 ) <= n; i += 4 )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000127 {
Dave Rodgman4b910c12022-11-24 19:44:52 +0000128 uint32_t x = mbedtls_get_unaligned_uint32( a + i ) ^ mbedtls_get_unaligned_uint32( b + i );
129 mbedtls_put_unaligned_uint32( r + i, x );
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000130 }
Dave Rodgman96d61d12022-11-24 19:33:22 +0000131 for ( ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000132 {
133 r[i] = a[i] ^ b[i];
134 }
135}
136
Jerry Yu6c983522021-09-24 12:45:36 +0800137/* Fix MSVC C99 compatible issue
138 * MSVC support __func__ from visual studio 2015( 1900 )
139 * Use MSVC predefine macro to avoid name check fail.
140 */
141#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800142#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800143#endif
144
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200145#endif /* MBEDTLS_LIBRARY_COMMON_H */