platform.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_PLATFORM_H
  26. #define MBEDTLS_PLATFORM_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_HAVE_TIME)
  33. #include "mbedtls/platform_time.h"
  34. #endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * \name SECTION: Module settings
  40. *
  41. * The configuration options you can set for this module are in this section.
  42. * Either change them in config.h or define them on the compiler command line.
  43. * \{
  44. */
  45. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <time.h>
  49. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  50. #if defined(_WIN32)
  51. #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
  52. #else
  53. #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
  54. #endif
  55. #endif
  56. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  57. #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
  58. #endif
  59. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  60. #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
  61. #endif
  62. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  63. #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
  64. #endif
  65. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  66. #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
  67. #endif
  68. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  69. #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
  70. #endif
  71. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  72. #define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
  73. #endif
  74. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  75. #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
  76. #endif
  77. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  78. #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
  79. #endif
  80. #if defined(MBEDTLS_FS_IO)
  81. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  82. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
  83. #endif
  84. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  85. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
  86. #endif
  87. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
  88. #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
  89. #endif
  90. #endif /* MBEDTLS_FS_IO */
  91. #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  92. #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
  93. #include MBEDTLS_PLATFORM_STD_MEM_HDR
  94. #endif
  95. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  96. /* \} name SECTION: Module settings */
  97. /*
  98. * The function pointers for calloc and free
  99. */
  100. #if defined(MBEDTLS_PLATFORM_MEMORY)
  101. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
  102. defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  103. #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
  104. #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
  105. #else
  106. /* For size_t */
  107. #include <stddef.h>
  108. extern void * (*mbedtls_calloc)( size_t n, size_t size );
  109. extern void (*mbedtls_free)( void *ptr );
  110. /**
  111. * \brief Set your own memory implementation function pointers
  112. *
  113. * \param calloc_func the calloc function implementation
  114. * \param free_func the free function implementation
  115. *
  116. * \return 0 if successful
  117. */
  118. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  119. void (*free_func)( void * ) );
  120. #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
  121. #else /* !MBEDTLS_PLATFORM_MEMORY */
  122. #define mbedtls_free free
  123. #define mbedtls_calloc calloc
  124. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  125. /*
  126. * The function pointers for fprintf
  127. */
  128. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  129. /* We need FILE * */
  130. #include <stdio.h>
  131. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  132. /**
  133. * \brief Set your own fprintf function pointer
  134. *
  135. * \param fprintf_func the fprintf function implementation
  136. *
  137. * \return 0
  138. */
  139. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  140. ... ) );
  141. #else
  142. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  143. #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
  144. #else
  145. #define mbedtls_fprintf fprintf
  146. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  147. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  148. /*
  149. * The function pointers for printf
  150. */
  151. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  152. extern int (*mbedtls_printf)( const char *format, ... );
  153. /**
  154. * \brief Set your own printf function pointer
  155. *
  156. * \param printf_func the printf function implementation
  157. *
  158. * \return 0
  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 Set your own snprintf function pointer
  185. *
  186. * \param snprintf_func the snprintf function implementation
  187. *
  188. * \return 0
  189. */
  190. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  191. const char * format, ... ) );
  192. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  193. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  194. #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
  195. #else
  196. #define mbedtls_snprintf snprintf
  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. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif /* platform.h */