platform.c 9.0 KB

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