platform.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Platform abstraction layer
  3. *
  4. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. extern int ets_snprintf(char *buf, unsigned int size, const char *format, ...);
  29. extern void *pvPortCalloc(unsigned int count, unsigned int size);
  30. extern void vPortFree( void *pv );
  31. #if defined(MBEDTLS_PLATFORM_MEMORY)
  32. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  33. static void *platform_calloc_uninit( size_t n, size_t size )
  34. {
  35. ((void) n);
  36. ((void) size);
  37. return( NULL );
  38. }
  39. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  40. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  41. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  42. static void platform_free_uninit( void *ptr )
  43. {
  44. ((void) ptr);
  45. }
  46. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  47. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  48. void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  49. void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  50. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  51. void (*free_func)( void * ) )
  52. {
  53. mbedtls_calloc = calloc_func;
  54. mbedtls_free = free_func;
  55. return( 0 );
  56. }
  57. #endif /* MBEDTLS_PLATFORM_MEMORY */
  58. #if defined(_WIN32)
  59. #include <stdarg.h>
  60. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  61. {
  62. int ret;
  63. va_list argp;
  64. /* Avoid calling the invalid parameter handler by checking ourselves */
  65. if( s == NULL || n == 0 || fmt == NULL )
  66. return( -1 );
  67. va_start( argp, fmt );
  68. #if defined(_TRUNCATE)
  69. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  70. #else
  71. ret = _vsnprintf( s, n, fmt, argp );
  72. if( ret < 0 || (size_t) ret == n )
  73. {
  74. s[n-1] = '\0';
  75. ret = -1;
  76. }
  77. #endif
  78. va_end( argp );
  79. return( ret );
  80. }
  81. #endif
  82. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  83. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  84. /*
  85. * Make dummy function to prevent NULL pointer dereferences
  86. */
  87. static int platform_snprintf_uninit( char * s, size_t n,
  88. const char * format, ... )
  89. {
  90. ((void) s);
  91. ((void) n);
  92. ((void) format);
  93. return( 0 );
  94. }
  95. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  96. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  97. int (*mbedtls_snprintf)( char * s, size_t n,
  98. const char * format,
  99. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  100. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  101. const char * format,
  102. ... ) )
  103. {
  104. mbedtls_snprintf = snprintf_func;
  105. return( 0 );
  106. }
  107. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  108. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  109. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  110. /*
  111. * Make dummy function to prevent NULL pointer dereferences
  112. */
  113. static int platform_printf_uninit( const char *format, ... )
  114. {
  115. ((void) format);
  116. return( 0 );
  117. }
  118. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  119. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  120. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  121. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  122. {
  123. mbedtls_printf = printf_func;
  124. return( 0 );
  125. }
  126. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  127. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  128. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  129. /*
  130. * Make dummy function to prevent NULL pointer dereferences
  131. */
  132. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  133. {
  134. ((void) stream);
  135. ((void) format);
  136. return( 0 );
  137. }
  138. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  139. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  140. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  141. MBEDTLS_PLATFORM_STD_FPRINTF;
  142. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  143. {
  144. mbedtls_fprintf = fprintf_func;
  145. return( 0 );
  146. }
  147. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  148. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  149. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  150. /*
  151. * Make dummy function to prevent NULL pointer dereferences
  152. */
  153. static void platform_exit_uninit( int status )
  154. {
  155. ((void) status);
  156. }
  157. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  158. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  159. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  160. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  161. {
  162. mbedtls_exit = exit_func;
  163. return( 0 );
  164. }
  165. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  166. #if defined(MBEDTLS_HAVE_TIME)
  167. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  168. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  169. /*
  170. * Make dummy function to prevent NULL pointer dereferences
  171. */
  172. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  173. {
  174. ((void) timer);
  175. return( 0 );
  176. }
  177. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  178. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  179. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  180. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  181. {
  182. mbedtls_time = time_func;
  183. return( 0 );
  184. }
  185. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  186. #endif /* MBEDTLS_HAVE_TIME */
  187. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  188. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  189. /* Default implementations for the platform independent seed functions use
  190. * standard libc file functions to read from and write to a pre-defined filename
  191. */
  192. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  193. {
  194. FILE *file;
  195. size_t n;
  196. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  197. return -1;
  198. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  199. {
  200. fclose( file );
  201. return -1;
  202. }
  203. fclose( file );
  204. return( (int)n );
  205. }
  206. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  207. {
  208. FILE *file;
  209. size_t n;
  210. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  211. return -1;
  212. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  213. {
  214. fclose( file );
  215. return -1;
  216. }
  217. fclose( file );
  218. return( (int)n );
  219. }
  220. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  221. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  222. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  223. /*
  224. * Make dummy function to prevent NULL pointer dereferences
  225. */
  226. static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
  227. {
  228. ((void) buf);
  229. ((void) buf_len);
  230. return( -1 );
  231. }
  232. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
  233. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  234. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  235. /*
  236. * Make dummy function to prevent NULL pointer dereferences
  237. */
  238. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  239. {
  240. ((void) buf);
  241. ((void) buf_len);
  242. return( -1 );
  243. }
  244. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  245. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  246. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  247. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  248. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  249. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  250. int mbedtls_platform_set_nv_seed(
  251. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  252. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  253. {
  254. mbedtls_nv_seed_read = nv_seed_read_func;
  255. mbedtls_nv_seed_write = nv_seed_write_func;
  256. return( 0 );
  257. }
  258. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  259. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  260. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  261. /*
  262. * Placeholder platform setup that does nothing by default
  263. */
  264. int mbedtls_platform_setup( mbedtls_platform_context *ctx )
  265. {
  266. (void)ctx;
  267. return( 0 );
  268. }
  269. /*
  270. * Placeholder platform teardown that does nothing by default
  271. */
  272. void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
  273. {
  274. (void)ctx;
  275. }
  276. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  277. #endif /* MBEDTLS_PLATFORM_C */