common.h 12 KB

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