PageRenderTime 25ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/ssrNative/depends/mbedtls/include/mbedtls/platform.h

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