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