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