17901324dSJerome Forissier /** 27901324dSJerome Forissier * \file common.h 37901324dSJerome Forissier * 47901324dSJerome Forissier * \brief Utility macros for internal use in the library 57901324dSJerome Forissier */ 67901324dSJerome Forissier /* 77901324dSJerome Forissier * Copyright The Mbed TLS Contributors 87901324dSJerome Forissier * SPDX-License-Identifier: Apache-2.0 97901324dSJerome Forissier * 107901324dSJerome Forissier * Licensed under the Apache License, Version 2.0 (the "License"); you may 117901324dSJerome Forissier * not use this file except in compliance with the License. 127901324dSJerome Forissier * You may obtain a copy of the License at 137901324dSJerome Forissier * 147901324dSJerome Forissier * http://www.apache.org/licenses/LICENSE-2.0 157901324dSJerome Forissier * 167901324dSJerome Forissier * Unless required by applicable law or agreed to in writing, software 177901324dSJerome Forissier * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 187901324dSJerome Forissier * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 197901324dSJerome Forissier * See the License for the specific language governing permissions and 207901324dSJerome Forissier * limitations under the License. 217901324dSJerome Forissier */ 227901324dSJerome Forissier 237901324dSJerome Forissier #ifndef MBEDTLS_LIBRARY_COMMON_H 247901324dSJerome Forissier #define MBEDTLS_LIBRARY_COMMON_H 257901324dSJerome Forissier 26*32b31808SJens Wiklander #include "mbedtls/build_info.h" 27*32b31808SJens Wiklander #include "alignment.h" 287901324dSJerome Forissier 29*32b31808SJens Wiklander #include <assert.h> 30*32b31808SJens Wiklander #include <stddef.h> 31039e02dfSJerome Forissier #include <stdint.h> 32*32b31808SJens Wiklander #include <stddef.h> 33039e02dfSJerome Forissier 347901324dSJerome Forissier /** Helper to define a function as static except when building invasive tests. 357901324dSJerome Forissier * 367901324dSJerome Forissier * If a function is only used inside its own source file and should be 377901324dSJerome Forissier * declared `static` to allow the compiler to optimize for code size, 387901324dSJerome Forissier * but that function has unit tests, define it with 397901324dSJerome Forissier * ``` 407901324dSJerome Forissier * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } 417901324dSJerome Forissier * ``` 427901324dSJerome Forissier * and declare it in a header in the `library/` directory with 437901324dSJerome Forissier * ``` 447901324dSJerome Forissier * #if defined(MBEDTLS_TEST_HOOKS) 457901324dSJerome Forissier * int mbedtls_foo(...); 467901324dSJerome Forissier * #endif 477901324dSJerome Forissier * ``` 487901324dSJerome Forissier */ 497901324dSJerome Forissier #if defined(MBEDTLS_TEST_HOOKS) 507901324dSJerome Forissier #define MBEDTLS_STATIC_TESTABLE 517901324dSJerome Forissier #else 527901324dSJerome Forissier #define MBEDTLS_STATIC_TESTABLE static 537901324dSJerome Forissier #endif 547901324dSJerome Forissier 55*32b31808SJens Wiklander #if defined(MBEDTLS_TEST_HOOKS) 56*32b31808SJens Wiklander extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file); 57*32b31808SJens Wiklander #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \ 58*32b31808SJens Wiklander do { \ 59*32b31808SJens Wiklander if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \ 60039e02dfSJerome Forissier { \ 61*32b31808SJens Wiklander (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \ 62*32b31808SJens Wiklander } \ 63*32b31808SJens Wiklander } while (0) 64*32b31808SJens Wiklander #else 65*32b31808SJens Wiklander #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) 66*32b31808SJens Wiklander #endif /* defined(MBEDTLS_TEST_HOOKS) */ 67*32b31808SJens Wiklander 68*32b31808SJens Wiklander /** Allow library to access its structs' private members. 69*32b31808SJens Wiklander * 70*32b31808SJens Wiklander * Although structs defined in header files are publicly available, 71*32b31808SJens Wiklander * their members are private and should not be accessed by the user. 72*32b31808SJens Wiklander */ 73*32b31808SJens Wiklander #define MBEDTLS_ALLOW_PRIVATE_ACCESS 74*32b31808SJens Wiklander 75*32b31808SJens Wiklander /** Return an offset into a buffer. 76*32b31808SJens Wiklander * 77*32b31808SJens Wiklander * This is just the addition of an offset to a pointer, except that this 78*32b31808SJens Wiklander * function also accepts an offset of 0 into a buffer whose pointer is null. 79*32b31808SJens Wiklander * (`p + n` has undefined behavior when `p` is null, even when `n == 0`. 80*32b31808SJens Wiklander * A null pointer is a valid buffer pointer when the size is 0, for example 81*32b31808SJens Wiklander * as the result of `malloc(0)` on some platforms.) 82*32b31808SJens Wiklander * 83*32b31808SJens Wiklander * \param p Pointer to a buffer of at least n bytes. 84*32b31808SJens Wiklander * This may be \p NULL if \p n is zero. 85*32b31808SJens Wiklander * \param n An offset in bytes. 86*32b31808SJens Wiklander * \return Pointer to offset \p n in the buffer \p p. 87*32b31808SJens Wiklander * Note that this is only a valid pointer if the size of the 88*32b31808SJens Wiklander * buffer is at least \p n + 1. 89*32b31808SJens Wiklander */ 90*32b31808SJens Wiklander static inline unsigned char *mbedtls_buffer_offset( 91*32b31808SJens Wiklander unsigned char *p, size_t n) 92*32b31808SJens Wiklander { 93*32b31808SJens Wiklander return p == NULL ? NULL : p + n; 94*32b31808SJens Wiklander } 95*32b31808SJens Wiklander 96*32b31808SJens Wiklander /** Return an offset into a read-only buffer. 97*32b31808SJens Wiklander * 98*32b31808SJens Wiklander * Similar to mbedtls_buffer_offset(), but for const pointers. 99*32b31808SJens Wiklander * 100*32b31808SJens Wiklander * \param p Pointer to a buffer of at least n bytes. 101*32b31808SJens Wiklander * This may be \p NULL if \p n is zero. 102*32b31808SJens Wiklander * \param n An offset in bytes. 103*32b31808SJens Wiklander * \return Pointer to offset \p n in the buffer \p p. 104*32b31808SJens Wiklander * Note that this is only a valid pointer if the size of the 105*32b31808SJens Wiklander * buffer is at least \p n + 1. 106*32b31808SJens Wiklander */ 107*32b31808SJens Wiklander static inline const unsigned char *mbedtls_buffer_offset_const( 108*32b31808SJens Wiklander const unsigned char *p, size_t n) 109*32b31808SJens Wiklander { 110*32b31808SJens Wiklander return p == NULL ? NULL : p + n; 111*32b31808SJens Wiklander } 112*32b31808SJens Wiklander 113*32b31808SJens Wiklander /** 114*32b31808SJens Wiklander * Perform a fast block XOR operation, such that 115*32b31808SJens Wiklander * r[i] = a[i] ^ b[i] where 0 <= i < n 116*32b31808SJens Wiklander * 117*32b31808SJens Wiklander * \param r Pointer to result (buffer of at least \p n bytes). \p r 118*32b31808SJens Wiklander * may be equal to either \p a or \p b, but behaviour when 119*32b31808SJens Wiklander * it overlaps in other ways is undefined. 120*32b31808SJens Wiklander * \param a Pointer to input (buffer of at least \p n bytes) 121*32b31808SJens Wiklander * \param b Pointer to input (buffer of at least \p n bytes) 122*32b31808SJens Wiklander * \param n Number of bytes to process. 123*32b31808SJens Wiklander */ 124*32b31808SJens Wiklander inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n) 125*32b31808SJens Wiklander { 126*32b31808SJens Wiklander size_t i = 0; 127*32b31808SJens Wiklander #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS) 128*32b31808SJens Wiklander for (; (i + 4) <= n; i += 4) { 129*32b31808SJens Wiklander uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i); 130*32b31808SJens Wiklander mbedtls_put_unaligned_uint32(r + i, x); 131039e02dfSJerome Forissier } 132039e02dfSJerome Forissier #endif 133*32b31808SJens Wiklander for (; i < n; i++) { 134*32b31808SJens Wiklander r[i] = a[i] ^ b[i]; 135039e02dfSJerome Forissier } 136039e02dfSJerome Forissier } 137*32b31808SJens Wiklander 138*32b31808SJens Wiklander /* Fix MSVC C99 compatible issue 139*32b31808SJens Wiklander * MSVC support __func__ from visual studio 2015( 1900 ) 140*32b31808SJens Wiklander * Use MSVC predefine macro to avoid name check fail. 141*32b31808SJens Wiklander */ 142*32b31808SJens Wiklander #if (defined(_MSC_VER) && (_MSC_VER <= 1900)) 143*32b31808SJens Wiklander #define /*no-check-names*/ __func__ __FUNCTION__ 144039e02dfSJerome Forissier #endif 145039e02dfSJerome Forissier 146*32b31808SJens Wiklander /* Define `asm` for compilers which don't define it. */ 147*32b31808SJens Wiklander /* *INDENT-OFF* */ 148*32b31808SJens Wiklander #ifndef asm 149*32b31808SJens Wiklander #define asm __asm__ 150039e02dfSJerome Forissier #endif 151*32b31808SJens Wiklander /* *INDENT-ON* */ 152039e02dfSJerome Forissier 153*32b31808SJens Wiklander /* Always provide a static assert macro, so it can be used unconditionally. 154*32b31808SJens Wiklander * It will expand to nothing on some systems. 155*32b31808SJens Wiklander * Can be used outside functions (but don't add a trailing ';' in that case: 156*32b31808SJens Wiklander * the semicolon is included here to avoid triggering -Wextra-semi when 157*32b31808SJens Wiklander * MBEDTLS_STATIC_ASSERT() expands to nothing). 158*32b31808SJens Wiklander * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it 159*32b31808SJens Wiklander * defines static_assert even with -std=c99, but then complains about it. 160039e02dfSJerome Forissier */ 161*32b31808SJens Wiklander #if defined(static_assert) && !defined(__FreeBSD__) 162*32b31808SJens Wiklander #define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg); 163*32b31808SJens Wiklander #else 164*32b31808SJens Wiklander #define MBEDTLS_STATIC_ASSERT(expr, msg) 165039e02dfSJerome Forissier #endif 166039e02dfSJerome Forissier 1677901324dSJerome Forissier #endif /* MBEDTLS_LIBRARY_COMMON_H */ 168