platform.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include "FreeRTOS.h"
  121. #include "task.h"
  122. #define my_calloc(x,y) pvPortMalloc(x*y)
  123. #define mbedtls_free vPortFree//free
  124. #define mbedtls_calloc my_calloc
  125. //#define mbedtls_free free
  126. //#define mbedtls_calloc calloc
  127. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  128. /*
  129. * The function pointers for fprintf
  130. */
  131. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  132. /* We need FILE * */
  133. #include <stdio.h>
  134. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  135. /**
  136. * \brief Set your own fprintf function pointer
  137. *
  138. * \param fprintf_func the fprintf function implementation
  139. *
  140. * \return 0
  141. */
  142. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  143. ... ) );
  144. #else
  145. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  146. #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
  147. #else
  148. #define mbedtls_fprintf fprintf
  149. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  150. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  151. /*
  152. * The function pointers for printf
  153. */
  154. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  155. extern int (*mbedtls_printf)( const char *format, ... );
  156. /**
  157. * \brief Set your own printf function pointer
  158. *
  159. * \param printf_func the printf function implementation
  160. *
  161. * \return 0
  162. */
  163. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
  164. #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
  165. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
  166. #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
  167. #else
  168. #define mbedtls_printf printf
  169. #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
  170. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  171. /*
  172. * The function pointers for snprintf
  173. *
  174. * The snprintf implementation should conform to C99:
  175. * - it *must* always correctly zero-terminate the buffer
  176. * (except when n == 0, then it must leave the buffer untouched)
  177. * - however it is acceptable to return -1 instead of the required length when
  178. * the destination buffer is too short.
  179. */
  180. #if defined(_WIN32)
  181. /* For Windows (inc. MSYS2), we provide our own fixed implementation */
  182. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
  183. #endif
  184. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  185. extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
  186. /**
  187. * \brief Set your own snprintf function pointer
  188. *
  189. * \param snprintf_func the snprintf function implementation
  190. *
  191. * \return 0
  192. */
  193. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  194. const char * format, ... ) );
  195. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  196. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  197. #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
  198. #else
  199. #define mbedtls_snprintf snprintf
  200. #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
  201. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  202. /*
  203. * The function pointers for exit
  204. */
  205. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  206. extern void (*mbedtls_exit)( int status );
  207. /**
  208. * \brief Set your own exit function pointer
  209. *
  210. * \param exit_func the exit function implementation
  211. *
  212. * \return 0
  213. */
  214. int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
  215. #else
  216. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
  217. #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
  218. #else
  219. #define mbedtls_exit exit
  220. #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
  221. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  222. /*
  223. * The default exit values
  224. */
  225. #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  226. #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
  227. #else
  228. #define MBEDTLS_EXIT_SUCCESS 0
  229. #endif
  230. #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  231. #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
  232. #else
  233. #define MBEDTLS_EXIT_FAILURE 1
  234. #endif
  235. /*
  236. * The function pointers for reading from and writing a seed file to
  237. * Non-Volatile storage (NV) in a platform-independent way
  238. *
  239. * Only enabled when the NV seed entropy source is enabled
  240. */
  241. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  242. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  243. /* Internal standard platform definitions */
  244. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
  245. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
  246. #endif
  247. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  248. extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
  249. extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
  250. /**
  251. * \brief Set your own seed file writing/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 0
  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 Platform context structure
  276. *
  277. * \note This structure may be used to assist platform-specific
  278. * setup/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 Perform any platform initialisation operations
  289. *
  290. * \param ctx mbed TLS context
  291. *
  292. * \return 0 if successful
  293. *
  294. * \note This function is intended to allow platform specific initialisation,
  295. * and should be called before any other library functions. Its
  296. * implementation is platform specific, and by default, unless platform
  297. * specific code is provided, it does nothing.
  298. *
  299. * Its use and whether its necessary to be called is dependent on the
  300. * platform.
  301. */
  302. int mbedtls_platform_setup( mbedtls_platform_context *ctx );
  303. /**
  304. * \brief Perform any platform teardown operations
  305. *
  306. * \param ctx mbed TLS context
  307. *
  308. * \note This function should be called after every other mbed TLS module has
  309. * been correctly freed using the appropriate free function.
  310. * Its implementation is platform specific, and by default, unless
  311. * platform specific code is provided, it does nothing.
  312. *
  313. * Its use and whether its necessary to be called 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 */