platform.h 12 KB

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