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 | |
| 26 | #if defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include MBEDTLS_CONFIG_FILE |
| 28 | #else |
| 29 | #include "mbedtls/config.h" |
| 30 | #endif |
| 31 | |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 32 | #include <stddef.h> |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | |
Gilles Peskine | 8fe23a0 | 2022-11-23 17:24:37 +0100 | [diff] [blame] | 35 | /* Define `inline` on some non-C99-compliant compilers. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | #if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \ |
Gilles Peskine | 8fe23a0 | 2022-11-23 17:24:37 +0100 | [diff] [blame] | 37 | !defined(inline) && !defined(__cplusplus) |
| 38 | #define inline __inline |
| 39 | #endif |
| 40 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 41 | /** Helper to define a function as static except when building invasive tests. |
| 42 | * |
| 43 | * If a function is only used inside its own source file and should be |
| 44 | * declared `static` to allow the compiler to optimize for code size, |
| 45 | * but that function has unit tests, define it with |
| 46 | * ``` |
| 47 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 48 | * ``` |
| 49 | * and declare it in a header in the `library/` directory with |
| 50 | * ``` |
| 51 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 52 | * int mbedtls_foo(...); |
| 53 | * #endif |
| 54 | * ``` |
| 55 | */ |
| 56 | #if defined(MBEDTLS_TEST_HOOKS) |
| 57 | #define MBEDTLS_STATIC_TESTABLE |
| 58 | #else |
| 59 | #define MBEDTLS_STATIC_TESTABLE static |
| 60 | #endif |
| 61 | |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 62 | /** Return an offset into a buffer. |
| 63 | * |
| 64 | * This is just the addition of an offset to a pointer, except that this |
| 65 | * 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] | 66 | * (`p + n` has undefined behavior when `p` is null, even when `n == 0`. |
| 67 | * A null pointer is a valid buffer pointer when the size is 0, for example |
| 68 | * as the result of `malloc(0)` on some platforms.) |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 69 | * |
| 70 | * \param p Pointer to a buffer of at least n bytes. |
| 71 | * This may be \p NULL if \p n is zero. |
| 72 | * \param n An offset in bytes. |
| 73 | * \return Pointer to offset \p n in the buffer \p p. |
| 74 | * Note that this is only a valid pointer if the size of the |
| 75 | * buffer is at least \p n + 1. |
| 76 | */ |
| 77 | static inline unsigned char *mbedtls_buffer_offset( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | unsigned char *p, size_t n) |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 79 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** Return an offset into a read-only buffer. |
| 84 | * |
Gilles Peskine | ff97f33 | 2022-11-25 13:34:59 +0100 | [diff] [blame] | 85 | * Similar to mbedtls_buffer_offset(), but for const pointers. |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 86 | * |
| 87 | * \param p Pointer to a buffer of at least n bytes. |
| 88 | * This may be \p NULL if \p n is zero. |
| 89 | * \param n An offset in bytes. |
| 90 | * \return Pointer to offset \p n in the buffer \p p. |
| 91 | * Note that this is only a valid pointer if the size of the |
| 92 | * buffer is at least \p n + 1. |
| 93 | */ |
| 94 | static inline const unsigned char *mbedtls_buffer_offset_const( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | const unsigned char *p, size_t n) |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 96 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | return p == NULL ? NULL : p + n; |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 100 | /** Byte Reading Macros |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 101 | * |
Joe Subbiani | 8799e54 | 2021-07-21 16:35:48 +0100 | [diff] [blame] | 102 | * 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] | 103 | * byte from x, where byte 0 is the least significant byte. |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 104 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff)) |
| 106 | #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff)) |
| 107 | #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff)) |
| 108 | #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff)) |
| 109 | #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff)) |
| 110 | #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff)) |
| 111 | #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff)) |
| 112 | #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff)) |
Joe Subbiani | 6b897c9 | 2021-07-08 14:59:52 +0100 | [diff] [blame] | 113 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 114 | /** |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 115 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 116 | * big-endian order (MSB first). |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 117 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 118 | * \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] | 119 | * \param offset Offset from \p base of the first and most significant |
| 120 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 121 | * integer from. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 122 | */ |
| 123 | #ifndef MBEDTLS_GET_UINT32_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 124 | #define MBEDTLS_GET_UINT32_BE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 125 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | ((uint32_t) (data)[(offset)] << 24) \ |
| 127 | | ((uint32_t) (data)[(offset) + 1] << 16) \ |
| 128 | | ((uint32_t) (data)[(offset) + 2] << 8) \ |
| 129 | | ((uint32_t) (data)[(offset) + 3]) \ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 130 | ) |
| 131 | #endif |
| 132 | |
| 133 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 134 | * Put in memory a 32 bits unsigned integer in big-endian order. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 135 | * |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 136 | * \param n 32 bits unsigned integer to put in memory. |
| 137 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 138 | * bits unsigned integer in. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 139 | * \param offset Offset from \p base where to put the most significant |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 140 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 141 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 142 | #ifndef MBEDTLS_PUT_UINT32_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \ |
| 144 | { \ |
| 145 | (data)[(offset)] = MBEDTLS_BYTE_3(n); \ |
| 146 | (data)[(offset) + 1] = MBEDTLS_BYTE_2(n); \ |
| 147 | (data)[(offset) + 2] = MBEDTLS_BYTE_1(n); \ |
| 148 | (data)[(offset) + 3] = MBEDTLS_BYTE_0(n); \ |
| 149 | } |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 150 | #endif |
| 151 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 152 | /** |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 153 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 154 | * little-endian order (LSB first). |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 155 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 156 | * \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] | 157 | * \param offset Offset from \p base of the first and least significant |
| 158 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 159 | * integer from. |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 160 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 161 | #ifndef MBEDTLS_GET_UINT32_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | #define MBEDTLS_GET_UINT32_LE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 163 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | ((uint32_t) (data)[(offset)]) \ |
| 165 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 166 | | ((uint32_t) (data)[(offset) + 2] << 16) \ |
| 167 | | ((uint32_t) (data)[(offset) + 3] << 24) \ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 168 | ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 169 | #endif |
| 170 | |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 171 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 172 | * Put in memory a 32 bits unsigned integer in little-endian order. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 173 | * |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 174 | * \param n 32 bits unsigned integer to put in memory. |
| 175 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 176 | * bits unsigned integer in. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 177 | * \param offset Offset from \p base where to put the least significant |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 178 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 179 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 180 | #ifndef MBEDTLS_PUT_UINT32_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \ |
| 182 | { \ |
| 183 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 184 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 185 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 186 | (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \ |
| 187 | } |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 188 | #endif |
| 189 | |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 190 | /** |
Joe Subbiani | 5b96e67 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 191 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 192 | * little-endian order (LSB first). |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 193 | * |
Joe Subbiani | 5b96e67 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 194 | * \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] | 195 | * \param offset Offset from \p base of the first and least significant |
Joe Subbiani | 5b96e67 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 196 | * byte of the two bytes to build the 16 bits unsigned |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 197 | * integer from. |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 198 | */ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 199 | #ifndef MBEDTLS_GET_UINT16_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | #define MBEDTLS_GET_UINT16_LE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 201 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | ((uint16_t) (data)[(offset)]) \ |
| 203 | | ((uint16_t) (data)[(offset) + 1] << 8) \ |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 204 | ) |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 205 | #endif |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 206 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 207 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 208 | * Put in memory a 16 bits unsigned integer in little-endian order. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 209 | * |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 210 | * \param n 16 bits unsigned integer to put in memory. |
| 211 | * \param data Base address of the memory where to put the 16 |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 212 | * bits unsigned integer in. |
Joe Subbiani | 6350d3a | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 213 | * \param offset Offset from \p base where to put the least significant |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 214 | * byte of the 16 bits unsigned integer \p n. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 215 | */ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 216 | #ifndef MBEDTLS_PUT_UINT16_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | #define MBEDTLS_PUT_UINT16_LE(n, data, offset) \ |
| 218 | { \ |
| 219 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 220 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 221 | } |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 222 | #endif |
| 223 | |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 224 | /** |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 225 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 226 | * big-endian order (MSB first). |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 227 | * |
| 228 | * \param data Base address of the memory to get the two bytes from. |
| 229 | * \param offset Offset from \p base of the first and most significant |
| 230 | * byte of the two bytes to build the 16 bits unsigned |
| 231 | * integer from. |
| 232 | */ |
| 233 | #ifndef MBEDTLS_GET_UINT16_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | #define MBEDTLS_GET_UINT16_BE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 235 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 236 | ((uint16_t) (data)[(offset)] << 8) \ |
| 237 | | ((uint16_t) (data)[(offset) + 1]) \ |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 238 | ) |
| 239 | #endif |
| 240 | |
| 241 | /** |
| 242 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 243 | * |
| 244 | * \param n 16 bits unsigned integer to put in memory. |
| 245 | * \param data Base address of the memory where to put the 16 |
| 246 | * bits unsigned integer in. |
| 247 | * \param offset Offset from \p base where to put the most significant |
| 248 | * byte of the 16 bits unsigned integer \p n. |
| 249 | */ |
| 250 | #ifndef MBEDTLS_PUT_UINT16_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | #define MBEDTLS_PUT_UINT16_BE(n, data, offset) \ |
| 252 | { \ |
| 253 | (data)[(offset)] = MBEDTLS_BYTE_1(n); \ |
| 254 | (data)[(offset) + 1] = MBEDTLS_BYTE_0(n); \ |
| 255 | } |
Joe Subbiani | c54e908 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 256 | #endif |
| 257 | |
| 258 | /** |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 259 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 260 | * big-endian order (MSB first). |
| 261 | * |
| 262 | * \param data Base address of the memory to get the eight bytes from. |
| 263 | * \param offset Offset from \p base of the first and most significant |
| 264 | * byte of the eight bytes to build the 64 bits unsigned |
| 265 | * integer from. |
| 266 | */ |
| 267 | #ifndef MBEDTLS_GET_UINT64_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | #define MBEDTLS_GET_UINT64_BE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 269 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | ((uint64_t) (data)[(offset)] << 56) \ |
| 271 | | ((uint64_t) (data)[(offset) + 1] << 48) \ |
| 272 | | ((uint64_t) (data)[(offset) + 2] << 40) \ |
| 273 | | ((uint64_t) (data)[(offset) + 3] << 32) \ |
| 274 | | ((uint64_t) (data)[(offset) + 4] << 24) \ |
| 275 | | ((uint64_t) (data)[(offset) + 5] << 16) \ |
| 276 | | ((uint64_t) (data)[(offset) + 6] << 8) \ |
| 277 | | ((uint64_t) (data)[(offset) + 7]) \ |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 278 | ) |
| 279 | #endif |
| 280 | |
| 281 | /** |
| 282 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 283 | * |
| 284 | * \param n 64 bits unsigned integer to put in memory. |
| 285 | * \param data Base address of the memory where to put the 64 |
| 286 | * bits unsigned integer in. |
| 287 | * \param offset Offset from \p base where to put the most significant |
| 288 | * byte of the 64 bits unsigned integer \p n. |
| 289 | */ |
| 290 | #ifndef MBEDTLS_PUT_UINT64_BE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \ |
| 292 | { \ |
| 293 | (data)[(offset)] = MBEDTLS_BYTE_7(n); \ |
| 294 | (data)[(offset) + 1] = MBEDTLS_BYTE_6(n); \ |
| 295 | (data)[(offset) + 2] = MBEDTLS_BYTE_5(n); \ |
| 296 | (data)[(offset) + 3] = MBEDTLS_BYTE_4(n); \ |
| 297 | (data)[(offset) + 4] = MBEDTLS_BYTE_3(n); \ |
| 298 | (data)[(offset) + 5] = MBEDTLS_BYTE_2(n); \ |
| 299 | (data)[(offset) + 6] = MBEDTLS_BYTE_1(n); \ |
| 300 | (data)[(offset) + 7] = MBEDTLS_BYTE_0(n); \ |
| 301 | } |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 302 | #endif |
| 303 | |
| 304 | /** |
| 305 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 306 | * little-endian order (LSB first). |
| 307 | * |
| 308 | * \param data Base address of the memory to get the eight bytes from. |
| 309 | * \param offset Offset from \p base of the first and least significant |
| 310 | * byte of the eight bytes to build the 64 bits unsigned |
| 311 | * integer from. |
| 312 | */ |
| 313 | #ifndef MBEDTLS_GET_UINT64_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | #define MBEDTLS_GET_UINT64_LE(data, offset) \ |
Joe Subbiani | 896f4ee | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 315 | ( \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | ((uint64_t) (data)[(offset) + 7] << 56) \ |
| 317 | | ((uint64_t) (data)[(offset) + 6] << 48) \ |
| 318 | | ((uint64_t) (data)[(offset) + 5] << 40) \ |
| 319 | | ((uint64_t) (data)[(offset) + 4] << 32) \ |
| 320 | | ((uint64_t) (data)[(offset) + 3] << 24) \ |
| 321 | | ((uint64_t) (data)[(offset) + 2] << 16) \ |
| 322 | | ((uint64_t) (data)[(offset) + 1] << 8) \ |
| 323 | | ((uint64_t) (data)[(offset)]) \ |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 324 | ) |
| 325 | #endif |
| 326 | |
| 327 | /** |
| 328 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 329 | * |
| 330 | * \param n 64 bits unsigned integer to put in memory. |
| 331 | * \param data Base address of the memory where to put the 64 |
| 332 | * bits unsigned integer in. |
| 333 | * \param offset Offset from \p base where to put the least significant |
| 334 | * byte of the 64 bits unsigned integer \p n. |
| 335 | */ |
| 336 | #ifndef MBEDTLS_PUT_UINT64_LE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | #define MBEDTLS_PUT_UINT64_LE(n, data, offset) \ |
| 338 | { \ |
| 339 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 340 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 341 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 342 | (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \ |
| 343 | (data)[(offset) + 4] = MBEDTLS_BYTE_4(n); \ |
| 344 | (data)[(offset) + 5] = MBEDTLS_BYTE_5(n); \ |
| 345 | (data)[(offset) + 6] = MBEDTLS_BYTE_6(n); \ |
| 346 | (data)[(offset) + 7] = MBEDTLS_BYTE_7(n); \ |
| 347 | } |
Joe Subbiani | 1bd5d7d | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 348 | #endif |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 349 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 350 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |