common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * \file common.h
  3. *
  4. * \brief Utility macros for internal use in the library
  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_COMMON_H
  23. #define MBEDTLS_LIBRARY_COMMON_H
  24. #if defined(MBEDTLS_CONFIG_FILE)
  25. #include MBEDTLS_CONFIG_FILE
  26. #else
  27. #include "mbedtls/config.h"
  28. #endif
  29. #include <assert.h>
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. /* Define `inline` on some non-C99-compliant compilers. */
  33. #if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
  34. !defined(inline) && !defined(__cplusplus)
  35. #define inline __inline
  36. #endif
  37. /** Helper to define a function as static except when building invasive tests.
  38. *
  39. * If a function is only used inside its own source file and should be
  40. * declared `static` to allow the compiler to optimize for code size,
  41. * but that function has unit tests, define it with
  42. * ```
  43. * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
  44. * ```
  45. * and declare it in a header in the `library/` directory with
  46. * ```
  47. * #if defined(MBEDTLS_TEST_HOOKS)
  48. * int mbedtls_foo(...);
  49. * #endif
  50. * ```
  51. */
  52. #if defined(MBEDTLS_TEST_HOOKS)
  53. #define MBEDTLS_STATIC_TESTABLE
  54. #else
  55. #define MBEDTLS_STATIC_TESTABLE static
  56. #endif
  57. /** Return an offset into a buffer.
  58. *
  59. * This is just the addition of an offset to a pointer, except that this
  60. * function also accepts an offset of 0 into a buffer whose pointer is null.
  61. * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
  62. * A null pointer is a valid buffer pointer when the size is 0, for example
  63. * as the result of `malloc(0)` on some platforms.)
  64. *
  65. * \param p Pointer to a buffer of at least n bytes.
  66. * This may be \p NULL if \p n is zero.
  67. * \param n An offset in bytes.
  68. * \return Pointer to offset \p n in the buffer \p p.
  69. * Note that this is only a valid pointer if the size of the
  70. * buffer is at least \p n + 1.
  71. */
  72. static inline unsigned char *mbedtls_buffer_offset(
  73. unsigned char *p, size_t n)
  74. {
  75. return p == NULL ? NULL : p + n;
  76. }
  77. /** Return an offset into a read-only buffer.
  78. *
  79. * Similar to mbedtls_buffer_offset(), but for const pointers.
  80. *
  81. * \param p Pointer to a buffer of at least n bytes.
  82. * This may be \p NULL if \p n is zero.
  83. * \param n An offset in bytes.
  84. * \return Pointer to offset \p n in the buffer \p p.
  85. * Note that this is only a valid pointer if the size of the
  86. * buffer is at least \p n + 1.
  87. */
  88. static inline const unsigned char *mbedtls_buffer_offset_const(
  89. const unsigned char *p, size_t n)
  90. {
  91. return p == NULL ? NULL : p + n;
  92. }
  93. /** Byte Reading Macros
  94. *
  95. * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
  96. * byte from x, where byte 0 is the least significant byte.
  97. */
  98. #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
  99. #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
  100. #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
  101. #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
  102. #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
  103. #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
  104. #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
  105. #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
  106. /**
  107. * Get the unsigned 32 bits integer corresponding to four bytes in
  108. * big-endian order (MSB first).
  109. *
  110. * \param data Base address of the memory to get the four bytes from.
  111. * \param offset Offset from \p base of the first and most significant
  112. * byte of the four bytes to build the 32 bits unsigned
  113. * integer from.
  114. */
  115. #ifndef MBEDTLS_GET_UINT32_BE
  116. #define MBEDTLS_GET_UINT32_BE(data, offset) \
  117. ( \
  118. ((uint32_t) (data)[(offset)] << 24) \
  119. | ((uint32_t) (data)[(offset) + 1] << 16) \
  120. | ((uint32_t) (data)[(offset) + 2] << 8) \
  121. | ((uint32_t) (data)[(offset) + 3]) \
  122. )
  123. #endif
  124. /**
  125. * Put in memory a 32 bits unsigned integer in big-endian order.
  126. *
  127. * \param n 32 bits unsigned integer to put in memory.
  128. * \param data Base address of the memory where to put the 32
  129. * bits unsigned integer in.
  130. * \param offset Offset from \p base where to put the most significant
  131. * byte of the 32 bits unsigned integer \p n.
  132. */
  133. #ifndef MBEDTLS_PUT_UINT32_BE
  134. #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
  135. { \
  136. (data)[(offset)] = MBEDTLS_BYTE_3(n); \
  137. (data)[(offset) + 1] = MBEDTLS_BYTE_2(n); \
  138. (data)[(offset) + 2] = MBEDTLS_BYTE_1(n); \
  139. (data)[(offset) + 3] = MBEDTLS_BYTE_0(n); \
  140. }
  141. #endif
  142. /**
  143. * Get the unsigned 32 bits integer corresponding to four bytes in
  144. * little-endian order (LSB first).
  145. *
  146. * \param data Base address of the memory to get the four bytes from.
  147. * \param offset Offset from \p base of the first and least significant
  148. * byte of the four bytes to build the 32 bits unsigned
  149. * integer from.
  150. */
  151. #ifndef MBEDTLS_GET_UINT32_LE
  152. #define MBEDTLS_GET_UINT32_LE(data, offset) \
  153. ( \
  154. ((uint32_t) (data)[(offset)]) \
  155. | ((uint32_t) (data)[(offset) + 1] << 8) \
  156. | ((uint32_t) (data)[(offset) + 2] << 16) \
  157. | ((uint32_t) (data)[(offset) + 3] << 24) \
  158. )
  159. #endif
  160. /**
  161. * Put in memory a 32 bits unsigned integer in little-endian order.
  162. *
  163. * \param n 32 bits unsigned integer to put in memory.
  164. * \param data Base address of the memory where to put the 32
  165. * bits unsigned integer in.
  166. * \param offset Offset from \p base where to put the least significant
  167. * byte of the 32 bits unsigned integer \p n.
  168. */
  169. #ifndef MBEDTLS_PUT_UINT32_LE
  170. #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
  171. { \
  172. (data)[(offset)] = MBEDTLS_BYTE_0(n); \
  173. (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
  174. (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
  175. (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \
  176. }
  177. #endif
  178. /**
  179. * Get the unsigned 16 bits integer corresponding to two bytes in
  180. * little-endian order (LSB first).
  181. *
  182. * \param data Base address of the memory to get the two bytes from.
  183. * \param offset Offset from \p base of the first and least significant
  184. * byte of the two bytes to build the 16 bits unsigned
  185. * integer from.
  186. */
  187. #ifndef MBEDTLS_GET_UINT16_LE
  188. #define MBEDTLS_GET_UINT16_LE(data, offset) \
  189. ( \
  190. ((uint16_t) (data)[(offset)]) \
  191. | ((uint16_t) (data)[(offset) + 1] << 8) \
  192. )
  193. #endif
  194. /**
  195. * Put in memory a 16 bits unsigned integer in little-endian order.
  196. *
  197. * \param n 16 bits unsigned integer to put in memory.
  198. * \param data Base address of the memory where to put the 16
  199. * bits unsigned integer in.
  200. * \param offset Offset from \p base where to put the least significant
  201. * byte of the 16 bits unsigned integer \p n.
  202. */
  203. #ifndef MBEDTLS_PUT_UINT16_LE
  204. #define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
  205. { \
  206. (data)[(offset)] = MBEDTLS_BYTE_0(n); \
  207. (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
  208. }
  209. #endif
  210. /**
  211. * Get the unsigned 16 bits integer corresponding to two bytes in
  212. * big-endian order (MSB first).
  213. *
  214. * \param data Base address of the memory to get the two bytes from.
  215. * \param offset Offset from \p base of the first and most significant
  216. * byte of the two bytes to build the 16 bits unsigned
  217. * integer from.
  218. */
  219. #ifndef MBEDTLS_GET_UINT16_BE
  220. #define MBEDTLS_GET_UINT16_BE(data, offset) \
  221. ( \
  222. ((uint16_t) (data)[(offset)] << 8) \
  223. | ((uint16_t) (data)[(offset) + 1]) \
  224. )
  225. #endif
  226. /**
  227. * Put in memory a 16 bits unsigned integer in big-endian order.
  228. *
  229. * \param n 16 bits unsigned integer to put in memory.
  230. * \param data Base address of the memory where to put the 16
  231. * bits unsigned integer in.
  232. * \param offset Offset from \p base where to put the most significant
  233. * byte of the 16 bits unsigned integer \p n.
  234. */
  235. #ifndef MBEDTLS_PUT_UINT16_BE
  236. #define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
  237. { \
  238. (data)[(offset)] = MBEDTLS_BYTE_1(n); \
  239. (data)[(offset) + 1] = MBEDTLS_BYTE_0(n); \
  240. }
  241. #endif
  242. /**
  243. * Get the unsigned 64 bits integer corresponding to eight bytes in
  244. * big-endian order (MSB first).
  245. *
  246. * \param data Base address of the memory to get the eight bytes from.
  247. * \param offset Offset from \p base of the first and most significant
  248. * byte of the eight bytes to build the 64 bits unsigned
  249. * integer from.
  250. */
  251. #ifndef MBEDTLS_GET_UINT64_BE
  252. #define MBEDTLS_GET_UINT64_BE(data, offset) \
  253. ( \
  254. ((uint64_t) (data)[(offset)] << 56) \
  255. | ((uint64_t) (data)[(offset) + 1] << 48) \
  256. | ((uint64_t) (data)[(offset) + 2] << 40) \
  257. | ((uint64_t) (data)[(offset) + 3] << 32) \
  258. | ((uint64_t) (data)[(offset) + 4] << 24) \
  259. | ((uint64_t) (data)[(offset) + 5] << 16) \
  260. | ((uint64_t) (data)[(offset) + 6] << 8) \
  261. | ((uint64_t) (data)[(offset) + 7]) \
  262. )
  263. #endif
  264. /**
  265. * Put in memory a 64 bits unsigned integer in big-endian order.
  266. *
  267. * \param n 64 bits unsigned integer to put in memory.
  268. * \param data Base address of the memory where to put the 64
  269. * bits unsigned integer in.
  270. * \param offset Offset from \p base where to put the most significant
  271. * byte of the 64 bits unsigned integer \p n.
  272. */
  273. #ifndef MBEDTLS_PUT_UINT64_BE
  274. #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
  275. { \
  276. (data)[(offset)] = MBEDTLS_BYTE_7(n); \
  277. (data)[(offset) + 1] = MBEDTLS_BYTE_6(n); \
  278. (data)[(offset) + 2] = MBEDTLS_BYTE_5(n); \
  279. (data)[(offset) + 3] = MBEDTLS_BYTE_4(n); \
  280. (data)[(offset) + 4] = MBEDTLS_BYTE_3(n); \
  281. (data)[(offset) + 5] = MBEDTLS_BYTE_2(n); \
  282. (data)[(offset) + 6] = MBEDTLS_BYTE_1(n); \
  283. (data)[(offset) + 7] = MBEDTLS_BYTE_0(n); \
  284. }
  285. #endif
  286. /**
  287. * Get the unsigned 64 bits integer corresponding to eight bytes in
  288. * little-endian order (LSB first).
  289. *
  290. * \param data Base address of the memory to get the eight bytes from.
  291. * \param offset Offset from \p base of the first and least significant
  292. * byte of the eight bytes to build the 64 bits unsigned
  293. * integer from.
  294. */
  295. #ifndef MBEDTLS_GET_UINT64_LE
  296. #define MBEDTLS_GET_UINT64_LE(data, offset) \
  297. ( \
  298. ((uint64_t) (data)[(offset) + 7] << 56) \
  299. | ((uint64_t) (data)[(offset) + 6] << 48) \
  300. | ((uint64_t) (data)[(offset) + 5] << 40) \
  301. | ((uint64_t) (data)[(offset) + 4] << 32) \
  302. | ((uint64_t) (data)[(offset) + 3] << 24) \
  303. | ((uint64_t) (data)[(offset) + 2] << 16) \
  304. | ((uint64_t) (data)[(offset) + 1] << 8) \
  305. | ((uint64_t) (data)[(offset)]) \
  306. )
  307. #endif
  308. /**
  309. * Put in memory a 64 bits unsigned integer in little-endian order.
  310. *
  311. * \param n 64 bits unsigned integer to put in memory.
  312. * \param data Base address of the memory where to put the 64
  313. * bits unsigned integer in.
  314. * \param offset Offset from \p base where to put the least significant
  315. * byte of the 64 bits unsigned integer \p n.
  316. */
  317. #ifndef MBEDTLS_PUT_UINT64_LE
  318. #define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
  319. { \
  320. (data)[(offset)] = MBEDTLS_BYTE_0(n); \
  321. (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
  322. (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
  323. (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \
  324. (data)[(offset) + 4] = MBEDTLS_BYTE_4(n); \
  325. (data)[(offset) + 5] = MBEDTLS_BYTE_5(n); \
  326. (data)[(offset) + 6] = MBEDTLS_BYTE_6(n); \
  327. (data)[(offset) + 7] = MBEDTLS_BYTE_7(n); \
  328. }
  329. #endif
  330. /* Always provide a static assert macro, so it can be used unconditionally.
  331. * It will expand to nothing on some systems.
  332. * Can be used outside functions (but don't add a trailing ';' in that case:
  333. * the semicolon is included here to avoid triggering -Wextra-semi when
  334. * MBEDTLS_STATIC_ASSERT() expands to nothing).
  335. * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
  336. * defines static_assert even with -std=c99, but then complains about it.
  337. */
  338. #if defined(static_assert) && !defined(__FreeBSD__)
  339. #define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
  340. #else
  341. #define MBEDTLS_STATIC_ASSERT(expr, msg)
  342. #endif
  343. #endif /* MBEDTLS_LIBRARY_COMMON_H */