Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file alignment.h |
| 3 | * |
| 4 | * \brief Utility code for dealing with unaligned memory accesses |
| 5 | */ |
| 6 | /* |
| 7 | * Copyright The Mbed TLS Contributors |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #ifndef MBEDTLS_LIBRARY_ALIGNMENT_H |
| 24 | #define MBEDTLS_LIBRARY_ALIGNMENT_H |
| 25 | |
| 26 | #include <stdint.h> |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 27 | #include <string.h> |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 29 | |
Dave Rodgman | a616afe | 2022-11-25 17:11:45 +0000 | [diff] [blame] | 30 | #include "mbedtls/build_info.h" |
Dave Rodgman | 8f6583d | 2022-11-25 09:16:41 +0000 | [diff] [blame] | 31 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 32 | /** |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 33 | * Read the unsigned 16 bits integer from the given address, which need not |
| 34 | * be aligned. |
| 35 | * |
| 36 | * \param p pointer to 2 bytes of data |
| 37 | * \return Data at the given address |
| 38 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 39 | inline uint16_t mbedtls_get_unaligned_uint16(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 40 | { |
| 41 | uint16_t r; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 42 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 43 | return r; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Write the unsigned 16 bits integer to the given address, which need not |
| 48 | * be aligned. |
| 49 | * |
| 50 | * \param p pointer to 2 bytes of data |
| 51 | * \param x data to write |
| 52 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 53 | inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 54 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 55 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 59 | * Read the unsigned 32 bits integer from the given address, which need not |
| 60 | * be aligned. |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 61 | * |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 62 | * \param p pointer to 4 bytes of data |
Dave Rodgman | 875d238 | 2022-11-24 20:43:15 +0000 | [diff] [blame] | 63 | * \return Data at the given address |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 64 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 65 | inline uint32_t mbedtls_get_unaligned_uint32(const void *p) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 66 | { |
| 67 | uint32_t r; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 68 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 69 | return r; |
| 70 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 71 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Write the unsigned 32 bits integer to the given address, which need not |
| 74 | * be aligned. |
| 75 | * |
| 76 | * \param p pointer to 4 bytes of data |
| 77 | * \param x data to write |
| 78 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 79 | inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 80 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 81 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 82 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 83 | |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 84 | /** |
| 85 | * Read the unsigned 64 bits integer from the given address, which need not |
| 86 | * be aligned. |
| 87 | * |
| 88 | * \param p pointer to 8 bytes of data |
| 89 | * \return Data at the given address |
| 90 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 91 | inline uint64_t mbedtls_get_unaligned_uint64(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 92 | { |
| 93 | uint64_t r; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 94 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 95 | return r; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Write the unsigned 64 bits integer to the given address, which need not |
| 100 | * be aligned. |
| 101 | * |
| 102 | * \param p pointer to 8 bytes of data |
| 103 | * \param x data to write |
| 104 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 105 | inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 106 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 107 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 110 | /** Byte Reading Macros |
| 111 | * |
| 112 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
| 113 | * byte from x, where byte 0 is the least significant byte. |
| 114 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 115 | #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff)) |
| 116 | #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff)) |
| 117 | #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff)) |
| 118 | #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff)) |
| 119 | #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff)) |
| 120 | #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff)) |
| 121 | #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff)) |
| 122 | #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff)) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 123 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Detect GCC built-in byteswap routines |
| 126 | */ |
| 127 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 128 | #if __GNUC_PREREQ(4, 8) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 129 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 130 | #endif /* __GNUC_PREREQ(4,8) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 131 | #if __GNUC_PREREQ(4, 3) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 132 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 133 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 134 | #endif /* __GNUC_PREREQ(4,3) */ |
| 135 | #endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */ |
| 136 | |
| 137 | /* |
| 138 | * Detect Clang built-in byteswap routines |
| 139 | */ |
| 140 | #if defined(__clang__) && defined(__has_builtin) |
| 141 | #if __has_builtin(__builtin_bswap16) |
| 142 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 143 | #endif /* __has_builtin(__builtin_bswap16) */ |
| 144 | #if __has_builtin(__builtin_bswap32) |
| 145 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 146 | #endif /* __has_builtin(__builtin_bswap32) */ |
| 147 | #if __has_builtin(__builtin_bswap64) |
| 148 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 149 | #endif /* __has_builtin(__builtin_bswap64) */ |
| 150 | #endif /* defined(__clang__) && defined(__has_builtin) */ |
| 151 | |
| 152 | /* |
| 153 | * Detect MSVC built-in byteswap routines |
| 154 | */ |
| 155 | #if defined(_MSC_VER) |
| 156 | #define MBEDTLS_BSWAP16 _byteswap_ushort |
| 157 | #define MBEDTLS_BSWAP32 _byteswap_ulong |
| 158 | #define MBEDTLS_BSWAP64 _byteswap_uint64 |
| 159 | #endif /* defined(_MSC_VER) */ |
| 160 | |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 161 | /* Detect armcc built-in byteswap routine */ |
| 162 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) |
| 163 | #define MBEDTLS_BSWAP32 __rev |
| 164 | #endif |
| 165 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 166 | /* |
| 167 | * Where compiler built-ins are not present, fall back to C code that the |
| 168 | * compiler may be able to detect and transform into the relevant bswap or |
| 169 | * similar instruction. |
| 170 | */ |
| 171 | #if !defined(MBEDTLS_BSWAP16) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 172 | static inline uint16_t mbedtls_bswap16(uint16_t x) |
| 173 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 174 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 175 | (x & 0x00ff) << 8 | |
| 176 | (x & 0xff00) >> 8; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 177 | } |
| 178 | #define MBEDTLS_BSWAP16 mbedtls_bswap16 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 179 | #endif /* !defined(MBEDTLS_BSWAP16) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 180 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 181 | #if !defined(MBEDTLS_BSWAP32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 182 | static inline uint32_t mbedtls_bswap32(uint32_t x) |
| 183 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 184 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 185 | (x & 0x000000ff) << 24 | |
| 186 | (x & 0x0000ff00) << 8 | |
| 187 | (x & 0x00ff0000) >> 8 | |
| 188 | (x & 0xff000000) >> 24; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 189 | } |
| 190 | #define MBEDTLS_BSWAP32 mbedtls_bswap32 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 191 | #endif /* !defined(MBEDTLS_BSWAP32) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 192 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 193 | #if !defined(MBEDTLS_BSWAP64) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 194 | static inline uint64_t mbedtls_bswap64(uint64_t x) |
| 195 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 196 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 197 | (x & 0x00000000000000ff) << 56 | |
| 198 | (x & 0x000000000000ff00) << 40 | |
| 199 | (x & 0x0000000000ff0000) << 24 | |
| 200 | (x & 0x00000000ff000000) << 8 | |
| 201 | (x & 0x000000ff00000000) >> 8 | |
| 202 | (x & 0x0000ff0000000000) >> 24 | |
| 203 | (x & 0x00ff000000000000) >> 40 | |
| 204 | (x & 0xff00000000000000) >> 56; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 205 | } |
| 206 | #define MBEDTLS_BSWAP64 mbedtls_bswap64 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 207 | #endif /* !defined(MBEDTLS_BSWAP64) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 208 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 209 | #if !defined(__BYTE_ORDER__) |
| 210 | static const uint16_t mbedtls_byte_order_detector = { 0x100 }; |
| 211 | #define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01) |
| 212 | #else |
| 213 | #define MBEDTLS_IS_BIG_ENDIAN ((__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__)) |
| 214 | #endif /* !defined(__BYTE_ORDER__) */ |
| 215 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 218 | * big-endian order (MSB first). |
| 219 | * |
| 220 | * \param data Base address of the memory to get the four bytes from. |
| 221 | * \param offset Offset from \p data of the first and most significant |
| 222 | * byte of the four bytes to build the 32 bits unsigned |
| 223 | * integer from. |
| 224 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 225 | #define MBEDTLS_GET_UINT32_BE(data, offset) \ |
| 226 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 227 | ? mbedtls_get_unaligned_uint32((data) + (offset)) \ |
| 228 | : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 229 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Put in memory a 32 bits unsigned integer in big-endian order. |
| 233 | * |
| 234 | * \param n 32 bits unsigned integer to put in memory. |
| 235 | * \param data Base address of the memory where to put the 32 |
| 236 | * bits unsigned integer in. |
| 237 | * \param offset Offset from \p data where to put the most significant |
| 238 | * byte of the 32 bits unsigned integer \p n. |
| 239 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 240 | #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \ |
| 241 | { \ |
| 242 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
| 243 | { \ |
| 244 | mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \ |
| 245 | } \ |
| 246 | else \ |
| 247 | { \ |
| 248 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 249 | } \ |
| 250 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 254 | * little-endian order (LSB first). |
| 255 | * |
| 256 | * \param data Base address of the memory to get the four bytes from. |
| 257 | * \param offset Offset from \p data of the first and least significant |
| 258 | * byte of the four bytes to build the 32 bits unsigned |
| 259 | * integer from. |
| 260 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 261 | #define MBEDTLS_GET_UINT32_LE(data, offset) \ |
| 262 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 263 | ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
| 264 | : mbedtls_get_unaligned_uint32((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 265 | ) |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 266 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 267 | |
| 268 | /** |
| 269 | * Put in memory a 32 bits unsigned integer in little-endian order. |
| 270 | * |
| 271 | * \param n 32 bits unsigned integer to put in memory. |
| 272 | * \param data Base address of the memory where to put the 32 |
| 273 | * bits unsigned integer in. |
| 274 | * \param offset Offset from \p data where to put the least significant |
| 275 | * byte of the 32 bits unsigned integer \p n. |
| 276 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 277 | #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \ |
| 278 | { \ |
| 279 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
| 280 | { \ |
| 281 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 282 | } \ |
| 283 | else \ |
| 284 | { \ |
| 285 | mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \ |
| 286 | } \ |
| 287 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 291 | * little-endian order (LSB first). |
| 292 | * |
| 293 | * \param data Base address of the memory to get the two bytes from. |
| 294 | * \param offset Offset from \p data of the first and least significant |
| 295 | * byte of the two bytes to build the 16 bits unsigned |
| 296 | * integer from. |
| 297 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 298 | #define MBEDTLS_GET_UINT16_LE(data, offset) \ |
| 299 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 300 | ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
| 301 | : mbedtls_get_unaligned_uint16((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 302 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Put in memory a 16 bits unsigned integer in little-endian order. |
| 306 | * |
| 307 | * \param n 16 bits unsigned integer to put in memory. |
| 308 | * \param data Base address of the memory where to put the 16 |
| 309 | * bits unsigned integer in. |
| 310 | * \param offset Offset from \p data where to put the least significant |
| 311 | * byte of the 16 bits unsigned integer \p n. |
| 312 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 313 | #define MBEDTLS_PUT_UINT16_LE(n, data, offset) \ |
| 314 | { \ |
| 315 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
| 316 | { \ |
| 317 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 318 | } \ |
| 319 | else \ |
| 320 | { \ |
| 321 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
| 322 | } \ |
| 323 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 324 | |
| 325 | /** |
| 326 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 327 | * big-endian order (MSB first). |
| 328 | * |
| 329 | * \param data Base address of the memory to get the two bytes from. |
| 330 | * \param offset Offset from \p data of the first and most significant |
| 331 | * byte of the two bytes to build the 16 bits unsigned |
| 332 | * integer from. |
| 333 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 334 | #define MBEDTLS_GET_UINT16_BE(data, offset) \ |
| 335 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 336 | ? mbedtls_get_unaligned_uint16((data) + (offset)) \ |
| 337 | : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 338 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 342 | * |
| 343 | * \param n 16 bits unsigned integer to put in memory. |
| 344 | * \param data Base address of the memory where to put the 16 |
| 345 | * bits unsigned integer in. |
| 346 | * \param offset Offset from \p data where to put the most significant |
| 347 | * byte of the 16 bits unsigned integer \p n. |
| 348 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 349 | #define MBEDTLS_PUT_UINT16_BE(n, data, offset) \ |
| 350 | { \ |
| 351 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
| 352 | { \ |
| 353 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
| 354 | } \ |
| 355 | else \ |
| 356 | { \ |
| 357 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 358 | } \ |
| 359 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 363 | * big-endian order (MSB first). |
| 364 | * |
| 365 | * \param data Base address of the memory to get the three bytes from. |
| 366 | * \param offset Offset from \p data of the first and most significant |
| 367 | * byte of the three bytes to build the 24 bits unsigned |
| 368 | * integer from. |
| 369 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 370 | #define MBEDTLS_GET_UINT24_BE(data, offset) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 371 | ( \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 372 | ((uint32_t) (data)[(offset)] << 16) \ |
| 373 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 374 | | ((uint32_t) (data)[(offset) + 2]) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 375 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 376 | |
| 377 | /** |
| 378 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 379 | * |
| 380 | * \param n 24 bits unsigned integer to put in memory. |
| 381 | * \param data Base address of the memory where to put the 24 |
| 382 | * bits unsigned integer in. |
| 383 | * \param offset Offset from \p data where to put the most significant |
| 384 | * byte of the 24 bits unsigned integer \p n. |
| 385 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 386 | #define MBEDTLS_PUT_UINT24_BE(n, data, offset) \ |
| 387 | { \ |
| 388 | (data)[(offset)] = MBEDTLS_BYTE_2(n); \ |
| 389 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 390 | (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \ |
| 391 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 392 | |
| 393 | /** |
| 394 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 395 | * little-endian order (LSB first). |
| 396 | * |
| 397 | * \param data Base address of the memory to get the three bytes from. |
| 398 | * \param offset Offset from \p data of the first and least significant |
| 399 | * byte of the three bytes to build the 24 bits unsigned |
| 400 | * integer from. |
| 401 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 402 | #define MBEDTLS_GET_UINT24_LE(data, offset) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 403 | ( \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 404 | ((uint32_t) (data)[(offset)]) \ |
| 405 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 406 | | ((uint32_t) (data)[(offset) + 2] << 16) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 407 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 408 | |
| 409 | /** |
| 410 | * Put in memory a 24 bits unsigned integer in little-endian order. |
| 411 | * |
| 412 | * \param n 24 bits unsigned integer to put in memory. |
| 413 | * \param data Base address of the memory where to put the 24 |
| 414 | * bits unsigned integer in. |
| 415 | * \param offset Offset from \p data where to put the least significant |
| 416 | * byte of the 24 bits unsigned integer \p n. |
| 417 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 418 | #define MBEDTLS_PUT_UINT24_LE(n, data, offset) \ |
| 419 | { \ |
| 420 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
| 421 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 422 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 423 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 427 | * big-endian order (MSB first). |
| 428 | * |
| 429 | * \param data Base address of the memory to get the eight bytes from. |
| 430 | * \param offset Offset from \p data of the first and most significant |
| 431 | * byte of the eight bytes to build the 64 bits unsigned |
| 432 | * integer from. |
| 433 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 434 | #define MBEDTLS_GET_UINT64_BE(data, offset) \ |
| 435 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 436 | ? mbedtls_get_unaligned_uint64((data) + (offset)) \ |
| 437 | : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 438 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 439 | |
| 440 | /** |
| 441 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 442 | * |
| 443 | * \param n 64 bits unsigned integer to put in memory. |
| 444 | * \param data Base address of the memory where to put the 64 |
| 445 | * bits unsigned integer in. |
| 446 | * \param offset Offset from \p data where to put the most significant |
| 447 | * byte of the 64 bits unsigned integer \p n. |
| 448 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 449 | #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \ |
| 450 | { \ |
| 451 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
| 452 | { \ |
| 453 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
| 454 | } \ |
| 455 | else \ |
| 456 | { \ |
| 457 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 458 | } \ |
| 459 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 463 | * little-endian order (LSB first). |
| 464 | * |
| 465 | * \param data Base address of the memory to get the eight bytes from. |
| 466 | * \param offset Offset from \p data of the first and least significant |
| 467 | * byte of the eight bytes to build the 64 bits unsigned |
| 468 | * integer from. |
| 469 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 470 | #define MBEDTLS_GET_UINT64_LE(data, offset) \ |
| 471 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 472 | ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
| 473 | : mbedtls_get_unaligned_uint64((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 474 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 475 | |
| 476 | /** |
| 477 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 478 | * |
| 479 | * \param n 64 bits unsigned integer to put in memory. |
| 480 | * \param data Base address of the memory where to put the 64 |
| 481 | * bits unsigned integer in. |
| 482 | * \param offset Offset from \p data where to put the least significant |
| 483 | * byte of the 64 bits unsigned integer \p n. |
| 484 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame^] | 485 | #define MBEDTLS_PUT_UINT64_LE(n, data, offset) \ |
| 486 | { \ |
| 487 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
| 488 | { \ |
| 489 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 490 | } \ |
| 491 | else \ |
| 492 | { \ |
| 493 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
| 494 | } \ |
| 495 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 496 | |
| 497 | #endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */ |