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 |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef MBEDTLS_LIBRARY_COMMON_H |
| 12 | #define MBEDTLS_LIBRARY_COMMON_H |
| 13 | |
| 14 | #if defined(MBEDTLS_CONFIG_FILE) |
| 15 | #include MBEDTLS_CONFIG_FILE |
| 16 | #else |
| 17 | #include "mbedtls/config.h" |
| 18 | #endif |
| 19 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 20 | #include <assert.h> |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 21 | #include <stddef.h> |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | |
Gilles Peskine | 8fe23a0 | 2022-11-23 17:24:37 +0100 | [diff] [blame] | 24 | /* Define `inline` on some non-C99-compliant compilers. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 25 | #if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \ |
Gilles Peskine | 8fe23a0 | 2022-11-23 17:24:37 +0100 | [diff] [blame] | 26 | !defined(inline) && !defined(__cplusplus) |
| 27 | #define inline __inline |
| 28 | #endif |
| 29 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 30 | /** Helper to define a function as static except when building invasive tests. |
| 31 | * |
| 32 | * If a function is only used inside its own source file and should be |
| 33 | * declared `static` to allow the compiler to optimize for code size, |
| 34 | * but that function has unit tests, define it with |
| 35 | * ``` |
| 36 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 37 | * ``` |
| 38 | * and declare it in a header in the `library/` directory with |
| 39 | * ``` |
| 40 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 41 | * int mbedtls_foo(...); |
| 42 | * #endif |
| 43 | * ``` |
| 44 | */ |
| 45 | #if defined(MBEDTLS_TEST_HOOKS) |
| 46 | #define MBEDTLS_STATIC_TESTABLE |
| 47 | #else |
| 48 | #define MBEDTLS_STATIC_TESTABLE static |
| 49 | #endif |
| 50 | |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 51 | /** Return an offset into a buffer. |
| 52 | * |
| 53 | * This is just the addition of an offset to a pointer, except that this |
| 54 | * function also accepts an offset of 0 into a buffer whose pointer is null. |
Gilles Peskine | ff97f33 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 55 | * (`p + n` has undefined behavior when `p` is null, even when `n == 0`. |
| 56 | * A null pointer is a valid buffer pointer when the size is 0, for example |
| 57 | * as the result of `malloc(0)` on some platforms.) |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 58 | * |
| 59 | * \param p Pointer to a buffer of at least n bytes. |
| 60 | * This may be \p NULL if \p n is zero. |
| 61 | * \param n An offset in bytes. |
| 62 | * \return Pointer to offset \p n in the buffer \p p. |
| 63 | * Note that this is only a valid pointer if the size of the |
| 64 | * buffer is at least \p n + 1. |
| 65 | */ |
| 66 | static inline unsigned char *mbedtls_buffer_offset( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | unsigned char *p, size_t n) |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 68 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** Return an offset into a read-only buffer. |
| 73 | * |
Gilles Peskine | ff97f33 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 74 | * Similar to mbedtls_buffer_offset(), but for const pointers. |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 75 | * |
| 76 | * \param p Pointer to a buffer of at least n bytes. |
| 77 | * This may be \p NULL if \p n is zero. |
| 78 | * \param n An offset in bytes. |
| 79 | * \return Pointer to offset \p n in the buffer \p p. |
| 80 | * Note that this is only a valid pointer if the size of the |
| 81 | * buffer is at least \p n + 1. |
| 82 | */ |
| 83 | static inline const unsigned char *mbedtls_buffer_offset_const( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | const unsigned char *p, size_t n) |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 85 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 89 | /** Byte Reading Macros |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 90 | * |
Joe Subbiani | 8799e54 | 2021-07-21 16:35:48 +0100 | [diff] [blame] | 91 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
Joe Subbiani | d3a3f21 | 2021-07-21 15:22:47 +0100 | [diff] [blame] | 92 | * byte from x, where byte 0 is the least significant byte. |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 93 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff)) |
| 95 | #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff)) |
| 96 | #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff)) |
| 97 | #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff)) |
| 98 | #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff)) |
| 99 | #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff)) |
| 100 | #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff)) |
| 101 | #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff)) |
Joe Subbiani | 6b897c9 | 2021-07-08 14:59:52 +0100 | [diff] [blame] | 102 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 103 | /** |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 104 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 105 | * big-endian order (MSB first). |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 106 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 107 | * \param data Base address of the memory to get the four bytes from. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 108 | * \param offset Offset from \p base of the first and most significant |
| 109 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 110 | * integer from. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 111 | */ |
| 112 | #ifndef MBEDTLS_GET_UINT32_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | #define MBEDTLS_GET_UINT32_BE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 114 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | ((uint32_t) (data)[(offset)] << 24) \ |
| 116 | | ((uint32_t) (data)[(offset) + 1] << 16) \ |
| 117 | | ((uint32_t) (data)[(offset) + 2] << 8) \ |
| 118 | | ((uint32_t) (data)[(offset) + 3]) \ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 119 | ) |
| 120 | #endif |
| 121 | |
| 122 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 123 | * Put in memory a 32 bits unsigned integer in big-endian order. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 124 | * |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 125 | * \param n 32 bits unsigned integer to put in memory. |
| 126 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 127 | * bits unsigned integer in. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 128 | * \param offset Offset from \p base where to put the most significant |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 129 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 130 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 131 | #ifndef MBEDTLS_PUT_UINT32_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \ |
| 133 | { \ |
| 134 | (data)[(offset)] = MBEDTLS_BYTE_3(n); \ |
| 135 | (data)[(offset) + 1] = MBEDTLS_BYTE_2(n); \ |
| 136 | (data)[(offset) + 2] = MBEDTLS_BYTE_1(n); \ |
| 137 | (data)[(offset) + 3] = MBEDTLS_BYTE_0(n); \ |
| 138 | } |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 139 | #endif |
| 140 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 141 | /** |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 142 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 143 | * little-endian order (LSB first). |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 144 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 145 | * \param data Base address of the memory to get the four bytes from. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 146 | * \param offset Offset from \p base of the first and least significant |
| 147 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 148 | * integer from. |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 149 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 150 | #ifndef MBEDTLS_GET_UINT32_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | #define MBEDTLS_GET_UINT32_LE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 152 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 153 | ((uint32_t) (data)[(offset)]) \ |
| 154 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 155 | | ((uint32_t) (data)[(offset) + 2] << 16) \ |
| 156 | | ((uint32_t) (data)[(offset) + 3] << 24) \ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 157 | ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 158 | #endif |
| 159 | |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 160 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 161 | * Put in memory a 32 bits unsigned integer in little-endian order. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 162 | * |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 163 | * \param n 32 bits unsigned integer to put in memory. |
| 164 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 165 | * bits unsigned integer in. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 166 | * \param offset Offset from \p base where to put the least significant |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 167 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 168 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 169 | #ifndef MBEDTLS_PUT_UINT32_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \ |
| 171 | { \ |
| 172 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 173 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 174 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 175 | (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \ |
| 176 | } |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 177 | #endif |
| 178 | |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 179 | /** |
Joe Subbiani | 5b96e67 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 180 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 181 | * little-endian order (LSB first). |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 182 | * |
Joe Subbiani | 5b96e67 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 183 | * \param data Base address of the memory to get the two bytes from. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 184 | * \param offset Offset from \p base of the first and least significant |
Joe Subbiani | 5b96e67 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 185 | * byte of the two bytes to build the 16 bits unsigned |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 186 | * integer from. |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 187 | */ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 188 | #ifndef MBEDTLS_GET_UINT16_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | #define MBEDTLS_GET_UINT16_LE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 190 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 191 | ((uint16_t) (data)[(offset)]) \ |
| 192 | | ((uint16_t) (data)[(offset) + 1] << 8) \ |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 193 | ) |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 194 | #endif |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 195 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 196 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 197 | * Put in memory a 16 bits unsigned integer in little-endian order. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 198 | * |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 199 | * \param n 16 bits unsigned integer to put in memory. |
| 200 | * \param data Base address of the memory where to put the 16 |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 201 | * bits unsigned integer in. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 202 | * \param offset Offset from \p base where to put the least significant |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 203 | * byte of the 16 bits unsigned integer \p n. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 204 | */ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 205 | #ifndef MBEDTLS_PUT_UINT16_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | #define MBEDTLS_PUT_UINT16_LE(n, data, offset) \ |
| 207 | { \ |
| 208 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 209 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 210 | } |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 211 | #endif |
| 212 | |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 213 | /** |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 214 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 215 | * big-endian order (MSB first). |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 216 | * |
| 217 | * \param data Base address of the memory to get the two bytes from. |
| 218 | * \param offset Offset from \p base of the first and most significant |
| 219 | * byte of the two bytes to build the 16 bits unsigned |
| 220 | * integer from. |
| 221 | */ |
| 222 | #ifndef MBEDTLS_GET_UINT16_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | #define MBEDTLS_GET_UINT16_BE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 224 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | ((uint16_t) (data)[(offset)] << 8) \ |
| 226 | | ((uint16_t) (data)[(offset) + 1]) \ |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 227 | ) |
| 228 | #endif |
| 229 | |
| 230 | /** |
| 231 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 232 | * |
| 233 | * \param n 16 bits unsigned integer to put in memory. |
| 234 | * \param data Base address of the memory where to put the 16 |
| 235 | * bits unsigned integer in. |
| 236 | * \param offset Offset from \p base where to put the most significant |
| 237 | * byte of the 16 bits unsigned integer \p n. |
| 238 | */ |
| 239 | #ifndef MBEDTLS_PUT_UINT16_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | #define MBEDTLS_PUT_UINT16_BE(n, data, offset) \ |
| 241 | { \ |
| 242 | (data)[(offset)] = MBEDTLS_BYTE_1(n); \ |
| 243 | (data)[(offset) + 1] = MBEDTLS_BYTE_0(n); \ |
| 244 | } |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 245 | #endif |
| 246 | |
| 247 | /** |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 248 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 249 | * big-endian order (MSB first). |
| 250 | * |
| 251 | * \param data Base address of the memory to get the eight bytes from. |
| 252 | * \param offset Offset from \p base of the first and most significant |
| 253 | * byte of the eight bytes to build the 64 bits unsigned |
| 254 | * integer from. |
| 255 | */ |
| 256 | #ifndef MBEDTLS_GET_UINT64_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | #define MBEDTLS_GET_UINT64_BE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 258 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 259 | ((uint64_t) (data)[(offset)] << 56) \ |
| 260 | | ((uint64_t) (data)[(offset) + 1] << 48) \ |
| 261 | | ((uint64_t) (data)[(offset) + 2] << 40) \ |
| 262 | | ((uint64_t) (data)[(offset) + 3] << 32) \ |
| 263 | | ((uint64_t) (data)[(offset) + 4] << 24) \ |
| 264 | | ((uint64_t) (data)[(offset) + 5] << 16) \ |
| 265 | | ((uint64_t) (data)[(offset) + 6] << 8) \ |
| 266 | | ((uint64_t) (data)[(offset) + 7]) \ |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 267 | ) |
| 268 | #endif |
| 269 | |
| 270 | /** |
| 271 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 272 | * |
| 273 | * \param n 64 bits unsigned integer to put in memory. |
| 274 | * \param data Base address of the memory where to put the 64 |
| 275 | * bits unsigned integer in. |
| 276 | * \param offset Offset from \p base where to put the most significant |
| 277 | * byte of the 64 bits unsigned integer \p n. |
| 278 | */ |
| 279 | #ifndef MBEDTLS_PUT_UINT64_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \ |
| 281 | { \ |
| 282 | (data)[(offset)] = MBEDTLS_BYTE_7(n); \ |
| 283 | (data)[(offset) + 1] = MBEDTLS_BYTE_6(n); \ |
| 284 | (data)[(offset) + 2] = MBEDTLS_BYTE_5(n); \ |
| 285 | (data)[(offset) + 3] = MBEDTLS_BYTE_4(n); \ |
| 286 | (data)[(offset) + 4] = MBEDTLS_BYTE_3(n); \ |
| 287 | (data)[(offset) + 5] = MBEDTLS_BYTE_2(n); \ |
| 288 | (data)[(offset) + 6] = MBEDTLS_BYTE_1(n); \ |
| 289 | (data)[(offset) + 7] = MBEDTLS_BYTE_0(n); \ |
| 290 | } |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 291 | #endif |
| 292 | |
| 293 | /** |
| 294 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 295 | * little-endian order (LSB first). |
| 296 | * |
| 297 | * \param data Base address of the memory to get the eight bytes from. |
| 298 | * \param offset Offset from \p base of the first and least significant |
| 299 | * byte of the eight bytes to build the 64 bits unsigned |
| 300 | * integer from. |
| 301 | */ |
| 302 | #ifndef MBEDTLS_GET_UINT64_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | #define MBEDTLS_GET_UINT64_LE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 304 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 305 | ((uint64_t) (data)[(offset) + 7] << 56) \ |
| 306 | | ((uint64_t) (data)[(offset) + 6] << 48) \ |
| 307 | | ((uint64_t) (data)[(offset) + 5] << 40) \ |
| 308 | | ((uint64_t) (data)[(offset) + 4] << 32) \ |
| 309 | | ((uint64_t) (data)[(offset) + 3] << 24) \ |
| 310 | | ((uint64_t) (data)[(offset) + 2] << 16) \ |
| 311 | | ((uint64_t) (data)[(offset) + 1] << 8) \ |
| 312 | | ((uint64_t) (data)[(offset)]) \ |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 313 | ) |
| 314 | #endif |
| 315 | |
| 316 | /** |
| 317 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 318 | * |
| 319 | * \param n 64 bits unsigned integer to put in memory. |
| 320 | * \param data Base address of the memory where to put the 64 |
| 321 | * bits unsigned integer in. |
| 322 | * \param offset Offset from \p base where to put the least significant |
| 323 | * byte of the 64 bits unsigned integer \p n. |
| 324 | */ |
| 325 | #ifndef MBEDTLS_PUT_UINT64_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 326 | #define MBEDTLS_PUT_UINT64_LE(n, data, offset) \ |
| 327 | { \ |
| 328 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 329 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 330 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 331 | (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \ |
| 332 | (data)[(offset) + 4] = MBEDTLS_BYTE_4(n); \ |
| 333 | (data)[(offset) + 5] = MBEDTLS_BYTE_5(n); \ |
| 334 | (data)[(offset) + 6] = MBEDTLS_BYTE_6(n); \ |
| 335 | (data)[(offset) + 7] = MBEDTLS_BYTE_7(n); \ |
| 336 | } |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 337 | #endif |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 338 | |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 339 | /* Always provide a static assert macro, so it can be used unconditionally. |
Gilles Peskine | f555a4e | 2024-06-12 19:13:19 +0200 | [diff] [blame] | 340 | * It will expand to nothing on some systems. */ |
| 341 | /* Can't use the C11-style `defined(static_assert)` on FreeBSD, since it |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 342 | * defines static_assert even with -std=c99, but then complains about it. |
| 343 | */ |
| 344 | #if defined(static_assert) && !defined(__FreeBSD__) |
Gilles Peskine | f555a4e | 2024-06-12 19:13:19 +0200 | [diff] [blame] | 345 | #define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg) |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 346 | #else |
Gilles Peskine | f555a4e | 2024-06-12 19:13:19 +0200 | [diff] [blame] | 347 | /* Make sure `MBEDTLS_STATIC_ASSERT(expr, msg);` is valid both inside and |
| 348 | * outside a function. We choose a struct declaration, which can be repeated |
| 349 | * any number of times and does not need a matching definition. */ |
| 350 | #define MBEDTLS_STATIC_ASSERT(expr, msg) \ |
| 351 | struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function |
Tom Cosgrove | bdd01a7 | 2023-03-08 14:19:51 +0000 | [diff] [blame] | 352 | #endif |
| 353 | |
Dave Rodgman | 52c294a | 2024-01-04 11:37:17 +0000 | [diff] [blame] | 354 | /* Suppress compiler warnings for unused functions and variables. */ |
| 355 | #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute) |
| 356 | # if __has_attribute(unused) |
| 357 | # define MBEDTLS_MAYBE_UNUSED __attribute__((unused)) |
| 358 | # endif |
| 359 | #endif |
| 360 | #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__) |
| 361 | # define MBEDTLS_MAYBE_UNUSED __attribute__((unused)) |
| 362 | #endif |
| 363 | #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__) |
| 364 | /* IAR does support __attribute__((unused)), but only if the -e flag (extended language support) |
| 365 | * is given; the pragma always works. |
| 366 | * Unfortunately the pragma affects the rest of the file where it is used, but this is harmless. |
| 367 | * Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't |
| 368 | * able to find documentation). |
| 369 | */ |
| 370 | # if (__VER__ >= 5020000) |
| 371 | # define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177") |
| 372 | # endif |
| 373 | #endif |
| 374 | #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER) |
| 375 | # define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189)) |
| 376 | #endif |
| 377 | #if !defined(MBEDTLS_MAYBE_UNUSED) |
| 378 | # define MBEDTLS_MAYBE_UNUSED |
| 379 | #endif |
| 380 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 381 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |