platform.c 9.5 KB

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