platform.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // XXX Espressif are hacks sometimes. This is BS, but is taken from
  27. // the mbedtls platform.c from their SDK. Really, this should go
  28. // somewhere else. Note that the prototype here for vPortFree differs (!)
  29. // from the one in sdk-overrides.h. That's above my pay grade.
  30. // --nwf; 2018 Feb 18
  31. extern void *pvPortCalloc(unsigned int count, unsigned int size);
  32. extern void vPortFree( void *pv );
  33. #if defined(MBEDTLS_PLATFORM_C)
  34. #include "mbedtls/platform.h"
  35. #if defined(MBEDTLS_ENTROPY_NV_SEED) && \
  36. !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  37. /* Implementation that should never be optimized out by the compiler */
  38. static void mbedtls_zeroize( void *v, size_t n ) {
  39. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  40. }
  41. #endif
  42. #if defined(MBEDTLS_PLATFORM_MEMORY)
  43. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  44. static void *platform_calloc_uninit( size_t n, size_t size )
  45. {
  46. ((void) n);
  47. ((void) size);
  48. return( NULL );
  49. }
  50. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  51. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  52. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  53. static void platform_free_uninit( void *ptr )
  54. {
  55. ((void) ptr);
  56. }
  57. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  58. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  59. void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  60. void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  61. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  62. void (*free_func)( void * ) )
  63. {
  64. mbedtls_calloc = calloc_func;
  65. mbedtls_free = free_func;
  66. return( 0 );
  67. }
  68. #endif /* MBEDTLS_PLATFORM_MEMORY */
  69. #if defined(_WIN32)
  70. #include <stdarg.h>
  71. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  72. {
  73. int ret;
  74. va_list argp;
  75. /* Avoid calling the invalid parameter handler by checking ourselves */
  76. if( s == NULL || n == 0 || fmt == NULL )
  77. return( -1 );
  78. va_start( argp, fmt );
  79. #if defined(_TRUNCATE)
  80. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  81. #else
  82. ret = _vsnprintf( s, n, fmt, argp );
  83. if( ret < 0 || (size_t) ret == n )
  84. {
  85. s[n-1] = '\0';
  86. ret = -1;
  87. }
  88. #endif
  89. va_end( argp );
  90. return( ret );
  91. }
  92. #endif
  93. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  94. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  95. /*
  96. * Make dummy function to prevent NULL pointer dereferences
  97. */
  98. static int platform_snprintf_uninit( char * s, size_t n,
  99. const char * format, ... )
  100. {
  101. ((void) s);
  102. ((void) n);
  103. ((void) format);
  104. return( 0 );
  105. }
  106. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  107. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  108. int (*mbedtls_snprintf)( char * s, size_t n,
  109. const char * format,
  110. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  111. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  112. const char * format,
  113. ... ) )
  114. {
  115. mbedtls_snprintf = snprintf_func;
  116. return( 0 );
  117. }
  118. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  119. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  120. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  121. /*
  122. * Make dummy function to prevent NULL pointer dereferences
  123. */
  124. static int platform_printf_uninit( const char *format, ... )
  125. {
  126. ((void) format);
  127. return( 0 );
  128. }
  129. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  130. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  131. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  132. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  133. {
  134. mbedtls_printf = printf_func;
  135. return( 0 );
  136. }
  137. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  138. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  139. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  140. /*
  141. * Make dummy function to prevent NULL pointer dereferences
  142. */
  143. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  144. {
  145. ((void) stream);
  146. ((void) format);
  147. return( 0 );
  148. }
  149. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  150. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  151. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  152. MBEDTLS_PLATFORM_STD_FPRINTF;
  153. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  154. {
  155. mbedtls_fprintf = fprintf_func;
  156. return( 0 );
  157. }
  158. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  159. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  160. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  161. /*
  162. * Make dummy function to prevent NULL pointer dereferences
  163. */
  164. static void platform_exit_uninit( int status )
  165. {
  166. ((void) status);
  167. }
  168. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  169. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  170. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  171. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  172. {
  173. mbedtls_exit = exit_func;
  174. return( 0 );
  175. }
  176. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  177. #if defined(MBEDTLS_HAVE_TIME)
  178. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  179. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  180. /*
  181. * Make dummy function to prevent NULL pointer dereferences
  182. */
  183. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  184. {
  185. ((void) timer);
  186. return( 0 );
  187. }
  188. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  189. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  190. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  191. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  192. {
  193. mbedtls_time = time_func;
  194. return( 0 );
  195. }
  196. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  197. #endif /* MBEDTLS_HAVE_TIME */
  198. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  199. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  200. /* Default implementations for the platform independent seed functions use
  201. * standard libc file functions to read from and write to a pre-defined filename
  202. */
  203. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  204. {
  205. FILE *file;
  206. size_t n;
  207. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  208. return( -1 );
  209. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  210. {
  211. fclose( file );
  212. mbedtls_zeroize( buf, buf_len );
  213. return( -1 );
  214. }
  215. fclose( file );
  216. return( (int)n );
  217. }
  218. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  219. {
  220. FILE *file;
  221. size_t n;
  222. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  223. return -1;
  224. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  225. {
  226. fclose( file );
  227. return -1;
  228. }
  229. fclose( file );
  230. return( (int)n );
  231. }
  232. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  233. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  234. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  235. /*
  236. * Make dummy function to prevent NULL pointer dereferences
  237. */
  238. static int platform_nv_seed_read_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_READ platform_nv_seed_read_uninit
  245. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  246. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  247. /*
  248. * Make dummy function to prevent NULL pointer dereferences
  249. */
  250. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  251. {
  252. ((void) buf);
  253. ((void) buf_len);
  254. return( -1 );
  255. }
  256. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  257. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  258. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  259. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  260. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  261. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  262. int mbedtls_platform_set_nv_seed(
  263. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  264. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  265. {
  266. mbedtls_nv_seed_read = nv_seed_read_func;
  267. mbedtls_nv_seed_write = nv_seed_write_func;
  268. return( 0 );
  269. }
  270. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  271. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  272. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  273. /*
  274. * Placeholder platform setup that does nothing by default
  275. */
  276. int mbedtls_platform_setup( mbedtls_platform_context *ctx )
  277. {
  278. (void)ctx;
  279. return( 0 );
  280. }
  281. /*
  282. * Placeholder platform teardown that does nothing by default
  283. */
  284. void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
  285. {
  286. (void)ctx;
  287. }
  288. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  289. #endif /* MBEDTLS_PLATFORM_C */