alignment.h 21 KB

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