common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #include "mbedtls/build_info.h"
  25. #include <stdint.h>
  26. /** Helper to define a function as static except when building invasive tests.
  27. *
  28. * If a function is only used inside its own source file and should be
  29. * declared `static` to allow the compiler to optimize for code size,
  30. * but that function has unit tests, define it with
  31. * ```
  32. * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
  33. * ```
  34. * and declare it in a header in the `library/` directory with
  35. * ```
  36. * #if defined(MBEDTLS_TEST_HOOKS)
  37. * int mbedtls_foo(...);
  38. * #endif
  39. * ```
  40. */
  41. #if defined(MBEDTLS_TEST_HOOKS)
  42. #define MBEDTLS_STATIC_TESTABLE
  43. #else
  44. #define MBEDTLS_STATIC_TESTABLE static
  45. #endif
  46. #if defined(MBEDTLS_TEST_HOOKS)
  47. extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
  48. #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
  49. do { \
  50. if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
  51. { \
  52. ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
  53. } \
  54. } while( 0 )
  55. #else
  56. #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
  57. #endif /* defined(MBEDTLS_TEST_HOOKS) */
  58. /** Allow library to access its structs' private members.
  59. *
  60. * Although structs defined in header files are publicly available,
  61. * their members are private and should not be accessed by the user.
  62. */
  63. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  64. /** Byte Reading Macros
  65. *
  66. * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
  67. * byte from x, where byte 0 is the least significant byte.
  68. */
  69. #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
  70. #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
  71. #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
  72. #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
  73. #define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
  74. #define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
  75. #define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
  76. #define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
  77. /**
  78. * Get the unsigned 32 bits integer corresponding to four bytes in
  79. * big-endian order (MSB first).
  80. *
  81. * \param data Base address of the memory to get the four bytes from.
  82. * \param offset Offset from \p data of the first and most significant
  83. * byte of the four bytes to build the 32 bits unsigned
  84. * integer from.
  85. */
  86. #ifndef MBEDTLS_GET_UINT32_BE
  87. #define MBEDTLS_GET_UINT32_BE( data , offset ) \
  88. ( \
  89. ( (uint32_t) ( data )[( offset ) ] << 24 ) \
  90. | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
  91. | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
  92. | ( (uint32_t) ( data )[( offset ) + 3] ) \
  93. )
  94. #endif
  95. /**
  96. * Put in memory a 32 bits unsigned integer in big-endian order.
  97. *
  98. * \param n 32 bits unsigned integer to put in memory.
  99. * \param data Base address of the memory where to put the 32
  100. * bits unsigned integer in.
  101. * \param offset Offset from \p data where to put the most significant
  102. * byte of the 32 bits unsigned integer \p n.
  103. */
  104. #ifndef MBEDTLS_PUT_UINT32_BE
  105. #define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
  106. { \
  107. ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
  108. ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
  109. ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
  110. ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
  111. }
  112. #endif
  113. /**
  114. * Get the unsigned 32 bits integer corresponding to four bytes in
  115. * little-endian order (LSB first).
  116. *
  117. * \param data Base address of the memory to get the four bytes from.
  118. * \param offset Offset from \p data of the first and least significant
  119. * byte of the four bytes to build the 32 bits unsigned
  120. * integer from.
  121. */
  122. #ifndef MBEDTLS_GET_UINT32_LE
  123. #define MBEDTLS_GET_UINT32_LE( data, offset ) \
  124. ( \
  125. ( (uint32_t) ( data )[( offset ) ] ) \
  126. | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
  127. | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
  128. | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
  129. )
  130. #endif
  131. /**
  132. * Put in memory a 32 bits unsigned integer in little-endian order.
  133. *
  134. * \param n 32 bits unsigned integer to put in memory.
  135. * \param data Base address of the memory where to put the 32
  136. * bits unsigned integer in.
  137. * \param offset Offset from \p data where to put the least significant
  138. * byte of the 32 bits unsigned integer \p n.
  139. */
  140. #ifndef MBEDTLS_PUT_UINT32_LE
  141. #define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
  142. { \
  143. ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
  144. ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
  145. ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
  146. ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
  147. }
  148. #endif
  149. /**
  150. * Get the unsigned 16 bits integer corresponding to two bytes in
  151. * little-endian order (LSB first).
  152. *
  153. * \param data Base address of the memory to get the two bytes from.
  154. * \param offset Offset from \p data of the first and least significant
  155. * byte of the two bytes to build the 16 bits unsigned
  156. * integer from.
  157. */
  158. #ifndef MBEDTLS_GET_UINT16_LE
  159. #define MBEDTLS_GET_UINT16_LE( data, offset ) \
  160. ( \
  161. ( (uint16_t) ( data )[( offset ) ] ) \
  162. | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
  163. )
  164. #endif
  165. /**
  166. * Put in memory a 16 bits unsigned integer in little-endian order.
  167. *
  168. * \param n 16 bits unsigned integer to put in memory.
  169. * \param data Base address of the memory where to put the 16
  170. * bits unsigned integer in.
  171. * \param offset Offset from \p data where to put the least significant
  172. * byte of the 16 bits unsigned integer \p n.
  173. */
  174. #ifndef MBEDTLS_PUT_UINT16_LE
  175. #define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
  176. { \
  177. ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
  178. ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
  179. }
  180. #endif
  181. /**
  182. * Get the unsigned 16 bits integer corresponding to two bytes in
  183. * big-endian order (MSB first).
  184. *
  185. * \param data Base address of the memory to get the two bytes from.
  186. * \param offset Offset from \p data of the first and most significant
  187. * byte of the two bytes to build the 16 bits unsigned
  188. * integer from.
  189. */
  190. #ifndef MBEDTLS_GET_UINT16_BE
  191. #define MBEDTLS_GET_UINT16_BE( data, offset ) \
  192. ( \
  193. ( (uint16_t) ( data )[( offset ) ] << 8 ) \
  194. | ( (uint16_t) ( data )[( offset ) + 1] ) \
  195. )
  196. #endif
  197. /**
  198. * Put in memory a 16 bits unsigned integer in big-endian order.
  199. *
  200. * \param n 16 bits unsigned integer to put in memory.
  201. * \param data Base address of the memory where to put the 16
  202. * bits unsigned integer in.
  203. * \param offset Offset from \p data where to put the most significant
  204. * byte of the 16 bits unsigned integer \p n.
  205. */
  206. #ifndef MBEDTLS_PUT_UINT16_BE
  207. #define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
  208. { \
  209. ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
  210. ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
  211. }
  212. #endif
  213. /**
  214. * Get the unsigned 24 bits integer corresponding to three bytes in
  215. * big-endian order (MSB first).
  216. *
  217. * \param data Base address of the memory to get the three bytes from.
  218. * \param offset Offset from \p data of the first and most significant
  219. * byte of the three bytes to build the 24 bits unsigned
  220. * integer from.
  221. */
  222. #ifndef MBEDTLS_GET_UINT24_BE
  223. #define MBEDTLS_GET_UINT24_BE( data , offset ) \
  224. ( \
  225. ( (uint32_t) ( data )[( offset ) ] << 16 ) \
  226. | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
  227. | ( (uint32_t) ( data )[( offset ) + 2] ) \
  228. )
  229. #endif
  230. /**
  231. * Put in memory a 24 bits unsigned integer in big-endian order.
  232. *
  233. * \param n 24 bits unsigned integer to put in memory.
  234. * \param data Base address of the memory where to put the 24
  235. * bits unsigned integer in.
  236. * \param offset Offset from \p data where to put the most significant
  237. * byte of the 24 bits unsigned integer \p n.
  238. */
  239. #ifndef MBEDTLS_PUT_UINT24_BE
  240. #define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
  241. { \
  242. ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
  243. ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
  244. ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
  245. }
  246. #endif
  247. /**
  248. * Get the unsigned 24 bits integer corresponding to three bytes in
  249. * little-endian order (LSB first).
  250. *
  251. * \param data Base address of the memory to get the three bytes from.
  252. * \param offset Offset from \p data of the first and least significant
  253. * byte of the three bytes to build the 24 bits unsigned
  254. * integer from.
  255. */
  256. #ifndef MBEDTLS_GET_UINT24_LE
  257. #define MBEDTLS_GET_UINT24_LE( data, offset ) \
  258. ( \
  259. ( (uint32_t) ( data )[( offset ) ] ) \
  260. | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
  261. | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
  262. )
  263. #endif
  264. /**
  265. * Put in memory a 24 bits unsigned integer in little-endian order.
  266. *
  267. * \param n 24 bits unsigned integer to put in memory.
  268. * \param data Base address of the memory where to put the 24
  269. * bits unsigned integer in.
  270. * \param offset Offset from \p data where to put the least significant
  271. * byte of the 24 bits unsigned integer \p n.
  272. */
  273. #ifndef MBEDTLS_PUT_UINT24_LE
  274. #define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
  275. { \
  276. ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
  277. ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
  278. ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
  279. }
  280. #endif
  281. /**
  282. * Get the unsigned 64 bits integer corresponding to eight bytes in
  283. * big-endian order (MSB first).
  284. *
  285. * \param data Base address of the memory to get the eight bytes from.
  286. * \param offset Offset from \p data of the first and most significant
  287. * byte of the eight bytes to build the 64 bits unsigned
  288. * integer from.
  289. */
  290. #ifndef MBEDTLS_GET_UINT64_BE
  291. #define MBEDTLS_GET_UINT64_BE( data, offset ) \
  292. ( \
  293. ( (uint64_t) ( data )[( offset ) ] << 56 ) \
  294. | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
  295. | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
  296. | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
  297. | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
  298. | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
  299. | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
  300. | ( (uint64_t) ( data )[( offset ) + 7] ) \
  301. )
  302. #endif
  303. /**
  304. * Put in memory a 64 bits unsigned integer in big-endian order.
  305. *
  306. * \param n 64 bits unsigned integer to put in memory.
  307. * \param data Base address of the memory where to put the 64
  308. * bits unsigned integer in.
  309. * \param offset Offset from \p data where to put the most significant
  310. * byte of the 64 bits unsigned integer \p n.
  311. */
  312. #ifndef MBEDTLS_PUT_UINT64_BE
  313. #define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
  314. { \
  315. ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
  316. ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
  317. ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
  318. ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
  319. ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
  320. ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
  321. ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
  322. ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
  323. }
  324. #endif
  325. /**
  326. * Get the unsigned 64 bits integer corresponding to eight bytes in
  327. * little-endian order (LSB first).
  328. *
  329. * \param data Base address of the memory to get the eight bytes from.
  330. * \param offset Offset from \p data of the first and least significant
  331. * byte of the eight bytes to build the 64 bits unsigned
  332. * integer from.
  333. */
  334. #ifndef MBEDTLS_GET_UINT64_LE
  335. #define MBEDTLS_GET_UINT64_LE( data, offset ) \
  336. ( \
  337. ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
  338. | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
  339. | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
  340. | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
  341. | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
  342. | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
  343. | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
  344. | ( (uint64_t) ( data )[( offset ) ] ) \
  345. )
  346. #endif
  347. /**
  348. * Put in memory a 64 bits unsigned integer in little-endian order.
  349. *
  350. * \param n 64 bits unsigned integer to put in memory.
  351. * \param data Base address of the memory where to put the 64
  352. * bits unsigned integer in.
  353. * \param offset Offset from \p data where to put the least significant
  354. * byte of the 64 bits unsigned integer \p n.
  355. */
  356. #ifndef MBEDTLS_PUT_UINT64_LE
  357. #define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
  358. { \
  359. ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
  360. ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
  361. ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
  362. ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
  363. ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
  364. ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
  365. ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
  366. ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
  367. }
  368. #endif
  369. /* Fix MSVC C99 compatible issue
  370. * MSVC support __func__ from visual studio 2015( 1900 )
  371. * Use MSVC predefine macro to avoid name check fail.
  372. */
  373. #if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
  374. #define /*no-check-names*/ __func__ __FUNCTION__
  375. #endif
  376. #endif /* MBEDTLS_LIBRARY_COMMON_H */