platform.c 8.9 KB

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