platform.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #ifdef PRINTF_STDLIB
  45. #include <stdio.h>
  46. #endif
  47. #ifdef PRINTF_CUSTOM
  48. #include "tinystdio.h"
  49. #endif
  50. #include <stdlib.h>
  51. #include <time.h>
  52. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  53. #if defined(_WIN32)
  54. #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
  55. #else
  56. #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
  57. #endif
  58. #endif
  59. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  60. #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
  61. #endif
  62. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  63. #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
  64. #endif
  65. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  66. #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
  67. #endif
  68. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  69. #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
  70. #endif
  71. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  72. #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
  73. #endif
  74. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  75. #define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
  76. #endif
  77. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  78. #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
  79. #endif
  80. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  81. #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
  82. #endif
  83. #if defined(MBEDTLS_FS_IO)
  84. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  85. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
  86. #endif
  87. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  88. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
  89. #endif
  90. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
  91. #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
  92. #endif
  93. #endif /* MBEDTLS_FS_IO */
  94. #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  95. #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
  96. #include MBEDTLS_PLATFORM_STD_MEM_HDR
  97. #endif
  98. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  99. /* \} name SECTION: Module settings */
  100. /*
  101. * The function pointers for calloc and free
  102. */
  103. #if defined(MBEDTLS_PLATFORM_MEMORY)
  104. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
  105. defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  106. #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
  107. #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
  108. #else
  109. /* For size_t */
  110. #include <stddef.h>
  111. extern void * (*mbedtls_calloc)( size_t n, size_t size );
  112. extern void (*mbedtls_free)( void *ptr );
  113. /**
  114. * \brief Set your own memory implementation function pointers
  115. *
  116. * \param calloc_func the calloc function implementation
  117. * \param free_func the free function implementation
  118. *
  119. * \return 0 if successful
  120. */
  121. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  122. void (*free_func)( void * ) );
  123. #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
  124. #else /* !MBEDTLS_PLATFORM_MEMORY */
  125. #include "FreeRTOS.h"
  126. #include "task.h"
  127. #define my_calloc(x,y) pvPortMalloc(x*y)
  128. #define mbedtls_free vPortFree//free
  129. #define mbedtls_calloc my_calloc
  130. //#define mbedtls_free free
  131. //#define mbedtls_calloc calloc
  132. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  133. /*
  134. * The function pointers for fprintf
  135. */
  136. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  137. /* We need FILE * */
  138. #ifdef PRINTF_STDLIB
  139. #include <stdio.h>
  140. #endif
  141. #ifdef PRINTF_CUSTOM
  142. #include "tinystdio.h"
  143. #endif
  144. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  145. /**
  146. * \brief Set your own fprintf function pointer
  147. *
  148. * \param fprintf_func the fprintf function implementation
  149. *
  150. * \return 0
  151. */
  152. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  153. ... ) );
  154. #else
  155. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  156. #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
  157. #else
  158. #define mbedtls_fprintf fprintf
  159. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  160. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  161. /*
  162. * The function pointers for printf
  163. */
  164. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  165. extern int (*mbedtls_printf)( const char *format, ... );
  166. /**
  167. * \brief Set your own printf function pointer
  168. *
  169. * \param printf_func the printf function implementation
  170. *
  171. * \return 0
  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 Set your own snprintf function pointer
  198. *
  199. * \param snprintf_func the snprintf function implementation
  200. *
  201. * \return 0
  202. */
  203. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  204. const char * format, ... ) );
  205. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  206. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  207. #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
  208. #else
  209. #define mbedtls_snprintf snprintf
  210. #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
  211. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  212. /*
  213. * The function pointers for exit
  214. */
  215. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  216. extern void (*mbedtls_exit)( int status );
  217. /**
  218. * \brief Set your own exit function pointer
  219. *
  220. * \param exit_func the exit function implementation
  221. *
  222. * \return 0
  223. */
  224. int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
  225. #else
  226. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
  227. #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
  228. #else
  229. #define mbedtls_exit exit
  230. #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
  231. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  232. /*
  233. * The default exit values
  234. */
  235. #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  236. #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
  237. #else
  238. #define MBEDTLS_EXIT_SUCCESS 0
  239. #endif
  240. #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  241. #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
  242. #else
  243. #define MBEDTLS_EXIT_FAILURE 1
  244. #endif
  245. /*
  246. * The function pointers for reading from and writing a seed file to
  247. * Non-Volatile storage (NV) in a platform-independent way
  248. *
  249. * Only enabled when the NV seed entropy source is enabled
  250. */
  251. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  252. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  253. /* Internal standard platform definitions */
  254. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
  255. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
  256. #endif
  257. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  258. extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
  259. extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
  260. /**
  261. * \brief Set your own seed file writing/reading functions
  262. *
  263. * \param nv_seed_read_func the seed reading function implementation
  264. * \param nv_seed_write_func the seed writing function implementation
  265. *
  266. * \return 0
  267. */
  268. int mbedtls_platform_set_nv_seed(
  269. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  270. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
  271. );
  272. #else
  273. #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
  274. defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
  275. #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
  276. #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
  277. #else
  278. #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
  279. #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
  280. #endif
  281. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  282. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  283. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  284. /**
  285. * \brief Platform context structure
  286. *
  287. * \note This structure may be used to assist platform-specific
  288. * setup/teardown operations.
  289. */
  290. typedef struct {
  291. char dummy; /**< Placeholder member as empty structs are not portable */
  292. }
  293. mbedtls_platform_context;
  294. #else
  295. #include "platform_alt.h"
  296. #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  297. /**
  298. * \brief Perform any platform initialisation operations
  299. *
  300. * \param ctx mbed TLS context
  301. *
  302. * \return 0 if successful
  303. *
  304. * \note This function is intended to allow platform specific initialisation,
  305. * and should be called before any other library functions. Its
  306. * implementation is platform specific, and by default, unless platform
  307. * specific code is provided, it does nothing.
  308. *
  309. * Its use and whether its necessary to be called is dependent on the
  310. * platform.
  311. */
  312. int mbedtls_platform_setup( mbedtls_platform_context *ctx );
  313. /**
  314. * \brief Perform any platform teardown operations
  315. *
  316. * \param ctx mbed TLS context
  317. *
  318. * \note This function should be called after every other mbed TLS module has
  319. * been correctly freed using the appropriate free function.
  320. * Its implementation is platform specific, and by default, unless
  321. * platform specific code is provided, it does nothing.
  322. *
  323. * Its use and whether its necessary to be called is dependent on the
  324. * platform.
  325. */
  326. void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
  327. #ifdef __cplusplus
  328. }
  329. #endif
  330. #endif /* platform.h */