platform.h 11 KB

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