common.h 14 KB

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