Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file common.h |
| 3 | * |
| 4 | * \brief Utility macros for internal use in the library |
| 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 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. |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef MBEDTLS_LIBRARY_COMMON_H |
| 24 | #define MBEDTLS_LIBRARY_COMMON_H |
| 25 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 27 | #include "alignment.h" |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 28 | |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 29 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 30 | #include <stdint.h> |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 31 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 32 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 33 | /** 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 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 54 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 55 | extern 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) |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 63 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 64 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 65 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 66 | |
Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 67 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 68 | * |
| 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 Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 74 | /** 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 Peskine | 7d23778 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 78 | * (`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 Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 81 | * |
| 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 | */ |
| 89 | static inline unsigned char *mbedtls_buffer_offset( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 90 | unsigned char *p, size_t n) |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 91 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 92 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** Return an offset into a read-only buffer. |
| 96 | * |
Gilles Peskine | 7d23778 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 97 | * Similar to mbedtls_buffer_offset(), but for const pointers. |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 98 | * |
| 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 | */ |
| 106 | static inline const unsigned char *mbedtls_buffer_offset_const( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 107 | const unsigned char *p, size_t n) |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 108 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 109 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 112 | /** |
| 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 123 | inline void mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n) |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 124 | { |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 125 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 126 | for (i = 0; (i + 4) <= n; i += 4) { |
| 127 | uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i); |
| 128 | mbedtls_put_unaligned_uint32(r + i, x); |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 129 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 130 | for (; i < n; i++) { |
Dave Rodgman | c3d8041 | 2022-11-22 15:01:39 +0000 | [diff] [blame] | 131 | r[i] = a[i] ^ b[i]; |
| 132 | } |
| 133 | } |
| 134 | |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 135 | /* Fix MSVC C99 compatible issue |
| 136 | * MSVC support __func__ from visual studio 2015( 1900 ) |
| 137 | * Use MSVC predefine macro to avoid name check fail. |
| 138 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 139 | #if (defined(_MSC_VER) && (_MSC_VER <= 1900)) |
Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 140 | #define /*no-check-names*/ __func__ __FUNCTION__ |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 141 | #endif |
| 142 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 143 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |