platform.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * \file platform.h
  3. *
  4. * \brief This file contains the definitions and functions of the
  5. * Mbed TLS platform abstraction layer.
  6. *
  7. * The platform abstraction layer removes the need for the library
  8. * to directly link to standard C library functions or operating
  9. * system services, making the library easier to port and embed.
  10. * Application developers and users of the library can provide their own
  11. * implementations of these functions, or implementations specific to
  12. * their platform, which can be statically linked to the library or
  13. * dynamically configured at runtime.
  14. */
  15. /*
  16. * Copyright The Mbed TLS Contributors
  17. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  18. *
  19. * This file is provided under the Apache License 2.0, or the
  20. * GNU General Public License v2.0 or later.
  21. *
  22. * **********
  23. * Apache License 2.0:
  24. *
  25. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  26. * not use this file except in compliance with the License.
  27. * You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing, software
  32. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  33. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. * See the License for the specific language governing permissions and
  35. * limitations under the License.
  36. *
  37. * **********
  38. *
  39. * **********
  40. * GNU General Public License v2.0 or later:
  41. *
  42. * This program is free software; you can redistribute it and/or modify
  43. * it under the terms of the GNU General Public License as published by
  44. * the Free Software Foundation; either version 2 of the License, or
  45. * (at your option) any later version.
  46. *
  47. * This program is distributed in the hope that it will be useful,
  48. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  49. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  50. * GNU General Public License for more details.
  51. *
  52. * You should have received a copy of the GNU General Public License along
  53. * with this program; if not, write to the Free Software Foundation, Inc.,
  54. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  55. *
  56. * **********
  57. */
  58. #ifndef MBEDTLS_PLATFORM_H
  59. #define MBEDTLS_PLATFORM_H
  60. #if !defined(MBEDTLS_CONFIG_FILE)
  61. #include "config.h"
  62. #else
  63. #include MBEDTLS_CONFIG_FILE
  64. #endif
  65. #if defined(MBEDTLS_HAVE_TIME)
  66. #include "platform_time.h"
  67. #endif
  68. #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070 /**< Hardware accelerator failed */
  69. #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072 /**< The requested feature is not supported by the platform */
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. /**
  74. * \name SECTION: Module settings
  75. *
  76. * The configuration options you can set for this module are in this section.
  77. * Either change them in config.h or define them on the compiler command line.
  78. * \{
  79. */
  80. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
  81. #include <stdio.h>
  82. #include <stdlib.h>
  83. #include <time.h>
  84. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  85. #if defined(_WIN32)
  86. #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */
  87. #else
  88. #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
  89. #endif
  90. #endif
  91. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  92. #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
  93. #endif
  94. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  95. #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
  96. #endif
  97. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  98. #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
  99. #endif
  100. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  101. #define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
  102. #endif
  103. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  104. #define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
  105. #endif
  106. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  107. #define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
  108. #endif
  109. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  110. #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
  111. #endif
  112. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  113. #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
  114. #endif
  115. #if defined(MBEDTLS_FS_IO)
  116. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  117. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
  118. #endif
  119. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  120. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
  121. #endif
  122. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
  123. #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
  124. #endif
  125. #endif /* MBEDTLS_FS_IO */
  126. #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  127. #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
  128. #include MBEDTLS_PLATFORM_STD_MEM_HDR
  129. #endif
  130. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  131. /* \} name SECTION: Module settings */
  132. /*
  133. * The function pointers for calloc and free.
  134. */
  135. #if defined(MBEDTLS_PLATFORM_MEMORY)
  136. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
  137. defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  138. #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
  139. #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
  140. #else
  141. /* For size_t */
  142. #include <stddef.h>
  143. extern void *mbedtls_calloc( size_t n, size_t size );
  144. extern void mbedtls_free( void *ptr );
  145. /**
  146. * \brief This function dynamically sets the memory-management
  147. * functions used by the library, during runtime.
  148. *
  149. * \param calloc_func The \c calloc function implementation.
  150. * \param free_func The \c free function implementation.
  151. *
  152. * \return \c 0.
  153. */
  154. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  155. void (*free_func)( void * ) );
  156. #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
  157. #else /* !MBEDTLS_PLATFORM_MEMORY */
  158. #define mbedtls_free free
  159. #define mbedtls_calloc calloc
  160. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  161. /*
  162. * The function pointers for fprintf
  163. */
  164. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  165. /* We need FILE * */
  166. #include <stdio.h>
  167. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  168. /**
  169. * \brief This function dynamically configures the fprintf
  170. * function that is called when the
  171. * mbedtls_fprintf() function is invoked by the library.
  172. *
  173. * \param fprintf_func The \c fprintf function implementation.
  174. *
  175. * \return \c 0.
  176. */
  177. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  178. ... ) );
  179. #else
  180. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  181. #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
  182. #else
  183. #define mbedtls_fprintf fprintf
  184. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  185. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  186. /*
  187. * The function pointers for printf
  188. */
  189. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  190. extern int (*mbedtls_printf)( const char *format, ... );
  191. /**
  192. * \brief This function dynamically configures the snprintf
  193. * function that is called when the mbedtls_snprintf()
  194. * function is invoked by the library.
  195. *
  196. * \param printf_func The \c printf function implementation.
  197. *
  198. * \return \c 0 on success.
  199. */
  200. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
  201. #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
  202. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
  203. #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
  204. #else
  205. #define mbedtls_printf printf
  206. #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
  207. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  208. /*
  209. * The function pointers for snprintf
  210. *
  211. * The snprintf implementation should conform to C99:
  212. * - it *must* always correctly zero-terminate the buffer
  213. * (except when n == 0, then it must leave the buffer untouched)
  214. * - however it is acceptable to return -1 instead of the required length when
  215. * the destination buffer is too short.
  216. */
  217. #if defined(_WIN32)
  218. /* For Windows (inc. MSYS2), we provide our own fixed implementation */
  219. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
  220. #endif
  221. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  222. extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
  223. /**
  224. * \brief This function allows configuring a custom
  225. * \c snprintf function pointer.
  226. *
  227. * \param snprintf_func The \c snprintf function implementation.
  228. *
  229. * \return \c 0 on success.
  230. */
  231. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  232. const char * format, ... ) );
  233. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  234. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  235. #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
  236. #else
  237. #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
  238. #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
  239. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  240. /*
  241. * The function pointers for exit
  242. */
  243. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  244. extern void (*mbedtls_exit)( int status );
  245. /**
  246. * \brief This function dynamically configures the exit
  247. * function that is called when the mbedtls_exit()
  248. * function is invoked by the library.
  249. *
  250. * \param exit_func The \c exit function implementation.
  251. *
  252. * \return \c 0 on success.
  253. */
  254. int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
  255. #else
  256. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
  257. #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
  258. #else
  259. #define mbedtls_exit exit
  260. #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
  261. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  262. /*
  263. * The default exit values
  264. */
  265. #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  266. #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
  267. #else
  268. #define MBEDTLS_EXIT_SUCCESS 0
  269. #endif
  270. #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  271. #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
  272. #else
  273. #define MBEDTLS_EXIT_FAILURE 1
  274. #endif
  275. /*
  276. * The function pointers for reading from and writing a seed file to
  277. * Non-Volatile storage (NV) in a platform-independent way
  278. *
  279. * Only enabled when the NV seed entropy source is enabled
  280. */
  281. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  282. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  283. /* Internal standard platform definitions */
  284. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
  285. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
  286. #endif
  287. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  288. extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
  289. extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
  290. /**
  291. * \brief This function allows configuring custom seed file writing and
  292. * reading functions.
  293. *
  294. * \param nv_seed_read_func The seed reading function implementation.
  295. * \param nv_seed_write_func The seed writing function implementation.
  296. *
  297. * \return \c 0 on success.
  298. */
  299. int mbedtls_platform_set_nv_seed(
  300. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  301. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
  302. );
  303. #else
  304. #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
  305. defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
  306. #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
  307. #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
  308. #else
  309. #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
  310. #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
  311. #endif
  312. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  313. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  314. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  315. /**
  316. * \brief The platform context structure.
  317. *
  318. * \note This structure may be used to assist platform-specific
  319. * setup or teardown operations.
  320. */
  321. typedef struct mbedtls_platform_context
  322. {
  323. char dummy; /**< A placeholder member, as empty structs are not portable. */
  324. }
  325. mbedtls_platform_context;
  326. #else
  327. #include "platform_alt.h"
  328. #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  329. /**
  330. * \brief This function performs any platform-specific initialization
  331. * operations.
  332. *
  333. * \note This function should be called before any other library functions.
  334. *
  335. * Its implementation is platform-specific, and unless
  336. * platform-specific code is provided, it does nothing.
  337. *
  338. * \note The usage and necessity of this function is dependent on the platform.
  339. *
  340. * \param ctx The platform context.
  341. *
  342. * \return \c 0 on success.
  343. */
  344. int mbedtls_platform_setup( mbedtls_platform_context *ctx );
  345. /**
  346. * \brief This function performs any platform teardown operations.
  347. *
  348. * \note This function should be called after every other Mbed TLS module
  349. * has been correctly freed using the appropriate free function.
  350. *
  351. * Its implementation is platform-specific, and unless
  352. * platform-specific code is provided, it does nothing.
  353. *
  354. * \note The usage and necessity of this function is dependent on the platform.
  355. *
  356. * \param ctx The platform context.
  357. *
  358. */
  359. void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
  360. #ifdef __cplusplus
  361. }
  362. #endif
  363. #endif /* platform.h */