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 | */ |
| 168 | #ifndef MBEDTLS_GET_UINT32_BE |
| 169 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 170 | ( \ |
| 171 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 172 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 173 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 174 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
| 175 | ) |
| 176 | #endif |
| 177 | |
| 178 | /** |
| 179 | * Put in memory a 32 bits unsigned integer in big-endian order. |
| 180 | * |
| 181 | * \param n 32 bits unsigned integer to put in memory. |
| 182 | * \param data Base address of the memory where to put the 32 |
| 183 | * bits unsigned integer in. |
| 184 | * \param offset Offset from \p data where to put the most significant |
| 185 | * byte of the 32 bits unsigned integer \p n. |
| 186 | */ |
| 187 | #ifndef MBEDTLS_PUT_UINT32_BE |
| 188 | #define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ |
| 189 | { \ |
| 190 | ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \ |
| 191 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \ |
| 192 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \ |
| 193 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \ |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | /** |
| 198 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 199 | * little-endian order (LSB first). |
| 200 | * |
| 201 | * \param data Base address of the memory to get the four bytes from. |
| 202 | * \param offset Offset from \p data of the first and least significant |
| 203 | * byte of the four bytes to build the 32 bits unsigned |
| 204 | * integer from. |
| 205 | */ |
| 206 | #ifndef MBEDTLS_GET_UINT32_LE |
| 207 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 208 | ( \ |
| 209 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 210 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 211 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 212 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
| 213 | ) |
| 214 | #endif |
| 215 | |
| 216 | /** |
| 217 | * Put in memory a 32 bits unsigned integer in little-endian order. |
| 218 | * |
| 219 | * \param n 32 bits unsigned integer to put in memory. |
| 220 | * \param data Base address of the memory where to put the 32 |
| 221 | * bits unsigned integer in. |
| 222 | * \param offset Offset from \p data where to put the least significant |
| 223 | * byte of the 32 bits unsigned integer \p n. |
| 224 | */ |
| 225 | #ifndef MBEDTLS_PUT_UINT32_LE |
| 226 | #define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ |
| 227 | { \ |
| 228 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 229 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 230 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 231 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | /** |
| 236 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 237 | * little-endian order (LSB first). |
| 238 | * |
| 239 | * \param data Base address of the memory to get the two bytes from. |
| 240 | * \param offset Offset from \p data of the first and least significant |
| 241 | * byte of the two bytes to build the 16 bits unsigned |
| 242 | * integer from. |
| 243 | */ |
| 244 | #ifndef MBEDTLS_GET_UINT16_LE |
| 245 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 246 | ( \ |
| 247 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 248 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
| 249 | ) |
| 250 | #endif |
| 251 | |
| 252 | /** |
| 253 | * Put in memory a 16 bits unsigned integer in little-endian order. |
| 254 | * |
| 255 | * \param n 16 bits unsigned integer to put in memory. |
| 256 | * \param data Base address of the memory where to put the 16 |
| 257 | * bits unsigned integer in. |
| 258 | * \param offset Offset from \p data where to put the least significant |
| 259 | * byte of the 16 bits unsigned integer \p n. |
| 260 | */ |
| 261 | #ifndef MBEDTLS_PUT_UINT16_LE |
| 262 | #define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ |
| 263 | { \ |
| 264 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 265 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | /** |
| 270 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 271 | * big-endian order (MSB first). |
| 272 | * |
| 273 | * \param data Base address of the memory to get the two bytes from. |
| 274 | * \param offset Offset from \p data of the first and most significant |
| 275 | * byte of the two bytes to build the 16 bits unsigned |
| 276 | * integer from. |
| 277 | */ |
| 278 | #ifndef MBEDTLS_GET_UINT16_BE |
| 279 | #define MBEDTLS_GET_UINT16_BE( data, offset ) \ |
| 280 | ( \ |
| 281 | ( (uint16_t) ( data )[( offset ) ] << 8 ) \ |
| 282 | | ( (uint16_t) ( data )[( offset ) + 1] ) \ |
| 283 | ) |
| 284 | #endif |
| 285 | |
| 286 | /** |
| 287 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 288 | * |
| 289 | * \param n 16 bits unsigned integer to put in memory. |
| 290 | * \param data Base address of the memory where to put the 16 |
| 291 | * bits unsigned integer in. |
| 292 | * \param offset Offset from \p data where to put the most significant |
| 293 | * byte of the 16 bits unsigned integer \p n. |
| 294 | */ |
| 295 | #ifndef MBEDTLS_PUT_UINT16_BE |
| 296 | #define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \ |
| 297 | { \ |
| 298 | ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \ |
| 299 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \ |
| 300 | } |
| 301 | #endif |
| 302 | |
| 303 | /** |
| 304 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 305 | * big-endian order (MSB first). |
| 306 | * |
| 307 | * \param data Base address of the memory to get the three bytes from. |
| 308 | * \param offset Offset from \p data of the first and most significant |
| 309 | * byte of the three bytes to build the 24 bits unsigned |
| 310 | * integer from. |
| 311 | */ |
| 312 | #ifndef MBEDTLS_GET_UINT24_BE |
| 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 | ) |
| 319 | #endif |
| 320 | |
| 321 | /** |
| 322 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 323 | * |
| 324 | * \param n 24 bits unsigned integer to put in memory. |
| 325 | * \param data Base address of the memory where to put the 24 |
| 326 | * bits unsigned integer in. |
| 327 | * \param offset Offset from \p data where to put the most significant |
| 328 | * byte of the 24 bits unsigned integer \p n. |
| 329 | */ |
| 330 | #ifndef MBEDTLS_PUT_UINT24_BE |
| 331 | #define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \ |
| 332 | { \ |
| 333 | ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \ |
| 334 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 335 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \ |
| 336 | } |
| 337 | #endif |
| 338 | |
| 339 | /** |
| 340 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 341 | * little-endian order (LSB first). |
| 342 | * |
| 343 | * \param data Base address of the memory to get the three bytes from. |
| 344 | * \param offset Offset from \p data of the first and least significant |
| 345 | * byte of the three bytes to build the 24 bits unsigned |
| 346 | * integer from. |
| 347 | */ |
| 348 | #ifndef MBEDTLS_GET_UINT24_LE |
| 349 | #define MBEDTLS_GET_UINT24_LE( data, offset ) \ |
| 350 | ( \ |
| 351 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 352 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 353 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 354 | ) |
| 355 | #endif |
| 356 | |
| 357 | /** |
| 358 | * Put in memory a 24 bits unsigned integer in little-endian order. |
| 359 | * |
| 360 | * \param n 24 bits unsigned integer to put in memory. |
| 361 | * \param data Base address of the memory where to put the 24 |
| 362 | * bits unsigned integer in. |
| 363 | * \param offset Offset from \p data where to put the least significant |
| 364 | * byte of the 24 bits unsigned integer \p n. |
| 365 | */ |
| 366 | #ifndef MBEDTLS_PUT_UINT24_LE |
| 367 | #define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \ |
| 368 | { \ |
| 369 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 370 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 371 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | /** |
| 376 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 377 | * big-endian order (MSB first). |
| 378 | * |
| 379 | * \param data Base address of the memory to get the eight bytes from. |
| 380 | * \param offset Offset from \p data of the first and most significant |
| 381 | * byte of the eight bytes to build the 64 bits unsigned |
| 382 | * integer from. |
| 383 | */ |
| 384 | #ifndef MBEDTLS_GET_UINT64_BE |
| 385 | #define MBEDTLS_GET_UINT64_BE( data, offset ) \ |
| 386 | ( \ |
| 387 | ( (uint64_t) ( data )[( offset ) ] << 56 ) \ |
| 388 | | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \ |
| 389 | | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \ |
| 390 | | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \ |
| 391 | | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \ |
| 392 | | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \ |
| 393 | | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \ |
| 394 | | ( (uint64_t) ( data )[( offset ) + 7] ) \ |
| 395 | ) |
| 396 | #endif |
| 397 | |
| 398 | /** |
| 399 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 400 | * |
| 401 | * \param n 64 bits unsigned integer to put in memory. |
| 402 | * \param data Base address of the memory where to put the 64 |
| 403 | * bits unsigned integer in. |
| 404 | * \param offset Offset from \p data where to put the most significant |
| 405 | * byte of the 64 bits unsigned integer \p n. |
| 406 | */ |
| 407 | #ifndef MBEDTLS_PUT_UINT64_BE |
| 408 | #define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \ |
| 409 | { \ |
| 410 | ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \ |
| 411 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \ |
| 412 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \ |
| 413 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \ |
| 414 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \ |
| 415 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \ |
| 416 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \ |
| 417 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \ |
| 418 | } |
| 419 | #endif |
| 420 | |
| 421 | /** |
| 422 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 423 | * little-endian order (LSB first). |
| 424 | * |
| 425 | * \param data Base address of the memory to get the eight bytes from. |
| 426 | * \param offset Offset from \p data of the first and least significant |
| 427 | * byte of the eight bytes to build the 64 bits unsigned |
| 428 | * integer from. |
| 429 | */ |
| 430 | #ifndef MBEDTLS_GET_UINT64_LE |
| 431 | #define MBEDTLS_GET_UINT64_LE( data, offset ) \ |
| 432 | ( \ |
| 433 | ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \ |
| 434 | | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \ |
| 435 | | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \ |
| 436 | | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \ |
| 437 | | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \ |
| 438 | | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \ |
| 439 | | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \ |
| 440 | | ( (uint64_t) ( data )[( offset ) ] ) \ |
| 441 | ) |
| 442 | #endif |
| 443 | |
| 444 | /** |
| 445 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 446 | * |
| 447 | * \param n 64 bits unsigned integer to put in memory. |
| 448 | * \param data Base address of the memory where to put the 64 |
| 449 | * bits unsigned integer in. |
| 450 | * \param offset Offset from \p data where to put the least significant |
| 451 | * byte of the 64 bits unsigned integer \p n. |
| 452 | */ |
| 453 | #ifndef MBEDTLS_PUT_UINT64_LE |
| 454 | #define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \ |
| 455 | { \ |
| 456 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 457 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 458 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 459 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ |
| 460 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \ |
| 461 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \ |
| 462 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \ |
| 463 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \ |
| 464 | } |
| 465 | #endif |
| 466 | |
| 467 | #endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */ |