platform.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /* The compile time configuration of memory allocation via the macros
  43. * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
  44. * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
  45. * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
  46. #if defined(MBEDTLS_PLATFORM_MEMORY) && \
  47. !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
  48. defined(MBEDTLS_PLATFORM_FREE_MACRO) )
  49. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  50. static void *platform_calloc_uninit( size_t n, size_t size )
  51. {
  52. ((void) n);
  53. ((void) size);
  54. return( NULL );
  55. }
  56. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  57. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  58. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  59. static void platform_free_uninit( void *ptr )
  60. {
  61. ((void) ptr);
  62. }
  63. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  64. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  65. void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  66. void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  67. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  68. void (*free_func)( void * ) )
  69. {
  70. mbedtls_calloc = calloc_func;
  71. mbedtls_free = free_func;
  72. return( 0 );
  73. }
  74. #endif /* MBEDTLS_PLATFORM_MEMORY &&
  75. !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
  76. defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
  77. #if defined(_WIN32)
  78. #include <stdarg.h>
  79. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  80. {
  81. int ret;
  82. va_list argp;
  83. /* Avoid calling the invalid parameter handler by checking ourselves */
  84. if( s == NULL || n == 0 || fmt == NULL )
  85. return( -1 );
  86. va_start( argp, fmt );
  87. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  88. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  89. #else
  90. ret = _vsnprintf( s, n, fmt, argp );
  91. if( ret < 0 || (size_t) ret == n )
  92. {
  93. s[n-1] = '\0';
  94. ret = -1;
  95. }
  96. #endif
  97. va_end( argp );
  98. return( ret );
  99. }
  100. #endif
  101. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  102. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  103. /*
  104. * Make dummy function to prevent NULL pointer dereferences
  105. */
  106. static int platform_snprintf_uninit( char * s, size_t n,
  107. const char * format, ... )
  108. {
  109. ((void) s);
  110. ((void) n);
  111. ((void) format);
  112. return( 0 );
  113. }
  114. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  115. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  116. int (*mbedtls_snprintf)( char * s, size_t n,
  117. const char * format,
  118. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  119. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  120. const char * format,
  121. ... ) )
  122. {
  123. mbedtls_snprintf = snprintf_func;
  124. return( 0 );
  125. }
  126. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  127. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  128. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  129. /*
  130. * Make dummy function to prevent NULL pointer dereferences
  131. */
  132. static int platform_printf_uninit( const char *format, ... )
  133. {
  134. ((void) format);
  135. return( 0 );
  136. }
  137. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  138. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  139. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  140. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  141. {
  142. mbedtls_printf = printf_func;
  143. return( 0 );
  144. }
  145. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  146. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  147. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  148. /*
  149. * Make dummy function to prevent NULL pointer dereferences
  150. */
  151. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  152. {
  153. ((void) stream);
  154. ((void) format);
  155. return( 0 );
  156. }
  157. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  158. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  159. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  160. MBEDTLS_PLATFORM_STD_FPRINTF;
  161. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  162. {
  163. mbedtls_fprintf = fprintf_func;
  164. return( 0 );
  165. }
  166. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  167. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  168. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  169. /*
  170. * Make dummy function to prevent NULL pointer dereferences
  171. */
  172. static void platform_exit_uninit( int status )
  173. {
  174. ((void) status);
  175. }
  176. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  177. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  178. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  179. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  180. {
  181. mbedtls_exit = exit_func;
  182. return( 0 );
  183. }
  184. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  185. #if defined(MBEDTLS_HAVE_TIME)
  186. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  187. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  188. /*
  189. * Make dummy function to prevent NULL pointer dereferences
  190. */
  191. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  192. {
  193. ((void) timer);
  194. return( 0 );
  195. }
  196. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  197. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  198. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  199. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  200. {
  201. mbedtls_time = time_func;
  202. return( 0 );
  203. }
  204. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  205. #endif /* MBEDTLS_HAVE_TIME */
  206. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  207. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  208. /* Default implementations for the platform independent seed functions use
  209. * standard libc file functions to read from and write to a pre-defined filename
  210. */
  211. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  212. {
  213. FILE *file;
  214. size_t n;
  215. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  216. return( -1 );
  217. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  218. {
  219. fclose( file );
  220. mbedtls_zeroize( buf, buf_len );
  221. return( -1 );
  222. }
  223. fclose( file );
  224. return( (int)n );
  225. }
  226. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  227. {
  228. FILE *file;
  229. size_t n;
  230. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  231. return -1;
  232. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  233. {
  234. fclose( file );
  235. return -1;
  236. }
  237. fclose( file );
  238. return( (int)n );
  239. }
  240. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  241. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  242. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  243. /*
  244. * Make dummy function to prevent NULL pointer dereferences
  245. */
  246. static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
  247. {
  248. ((void) buf);
  249. ((void) buf_len);
  250. return( -1 );
  251. }
  252. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
  253. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  254. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  255. /*
  256. * Make dummy function to prevent NULL pointer dereferences
  257. */
  258. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  259. {
  260. ((void) buf);
  261. ((void) buf_len);
  262. return( -1 );
  263. }
  264. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  265. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  266. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  267. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  268. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  269. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  270. int mbedtls_platform_set_nv_seed(
  271. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  272. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  273. {
  274. mbedtls_nv_seed_read = nv_seed_read_func;
  275. mbedtls_nv_seed_write = nv_seed_write_func;
  276. return( 0 );
  277. }
  278. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  279. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  280. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  281. /*
  282. * Placeholder platform setup that does nothing by default
  283. */
  284. int mbedtls_platform_setup( mbedtls_platform_context *ctx )
  285. {
  286. (void)ctx;
  287. return( 0 );
  288. }
  289. /*
  290. * Placeholder platform teardown that does nothing by default
  291. */
  292. void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
  293. {
  294. (void)ctx;
  295. }
  296. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  297. #endif /* MBEDTLS_PLATFORM_C */