platform.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Platform abstraction layer
  3. *
  4. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_PLATFORM_C)
  29. #include "mbedtls/platform.h"
  30. #if defined(MBEDTLS_PLATFORM_MEMORY)
  31. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  32. static void *platform_calloc_uninit( size_t n, size_t size )
  33. {
  34. ((void) n);
  35. ((void) size);
  36. return( NULL );
  37. }
  38. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  39. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  40. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  41. static void platform_free_uninit( void *ptr )
  42. {
  43. ((void) ptr);
  44. }
  45. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  46. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  47. void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  48. void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  49. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  50. void (*free_func)( void * ) )
  51. {
  52. mbedtls_calloc = calloc_func;
  53. mbedtls_free = free_func;
  54. return( 0 );
  55. }
  56. #endif /* MBEDTLS_PLATFORM_MEMORY */
  57. #if defined(_WIN32)
  58. #include <stdarg.h>
  59. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  60. {
  61. int ret;
  62. va_list argp;
  63. /* Avoid calling the invalid parameter handler by checking ourselves */
  64. if( s == NULL || n == 0 || fmt == NULL )
  65. return( -1 );
  66. va_start( argp, fmt );
  67. #if defined(_TRUNCATE)
  68. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  69. #else
  70. ret = _vsnprintf( s, n, fmt, argp );
  71. if( ret < 0 || (size_t) ret == n )
  72. {
  73. s[n-1] = '\0';
  74. ret = -1;
  75. }
  76. #endif
  77. va_end( argp );
  78. return( ret );
  79. }
  80. #endif
  81. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  82. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  83. /*
  84. * Make dummy function to prevent NULL pointer dereferences
  85. */
  86. static int platform_snprintf_uninit( char * s, size_t n,
  87. const char * format, ... )
  88. {
  89. ((void) s);
  90. ((void) n);
  91. ((void) format);
  92. return( 0 );
  93. }
  94. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  95. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  96. int (*mbedtls_snprintf)( char * s, size_t n,
  97. const char * format,
  98. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  99. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  100. const char * format,
  101. ... ) )
  102. {
  103. mbedtls_snprintf = snprintf_func;
  104. return( 0 );
  105. }
  106. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  107. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  108. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  109. /*
  110. * Make dummy function to prevent NULL pointer dereferences
  111. */
  112. static int platform_printf_uninit( const char *format, ... )
  113. {
  114. ((void) format);
  115. return( 0 );
  116. }
  117. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  118. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  119. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  120. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  121. {
  122. mbedtls_printf = printf_func;
  123. return( 0 );
  124. }
  125. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  126. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  127. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  128. /*
  129. * Make dummy function to prevent NULL pointer dereferences
  130. */
  131. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  132. {
  133. ((void) stream);
  134. ((void) format);
  135. return( 0 );
  136. }
  137. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  138. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  139. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  140. MBEDTLS_PLATFORM_STD_FPRINTF;
  141. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  142. {
  143. mbedtls_fprintf = fprintf_func;
  144. return( 0 );
  145. }
  146. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  147. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  148. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  149. /*
  150. * Make dummy function to prevent NULL pointer dereferences
  151. */
  152. static void platform_exit_uninit( int status )
  153. {
  154. ((void) status);
  155. }
  156. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  157. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  158. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  159. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  160. {
  161. mbedtls_exit = exit_func;
  162. return( 0 );
  163. }
  164. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  165. #if defined(MBEDTLS_HAVE_TIME)
  166. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  167. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  168. /*
  169. * Make dummy function to prevent NULL pointer dereferences
  170. */
  171. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  172. {
  173. ((void) timer);
  174. return( 0 );
  175. }
  176. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  177. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  178. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  179. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  180. {
  181. mbedtls_time = time_func;
  182. return( 0 );
  183. }
  184. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  185. #endif /* MBEDTLS_HAVE_TIME */
  186. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  187. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  188. /* Default implementations for the platform independent seed functions use
  189. * standard libc file functions to read from and write to a pre-defined filename
  190. */
  191. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  192. {
  193. FILE *file;
  194. size_t n;
  195. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  196. return -1;
  197. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  198. {
  199. fclose( file );
  200. return -1;
  201. }
  202. fclose( file );
  203. return( (int)n );
  204. }
  205. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  206. {
  207. FILE *file;
  208. size_t n;
  209. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  210. return -1;
  211. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  212. {
  213. fclose( file );
  214. return -1;
  215. }
  216. fclose( file );
  217. return( (int)n );
  218. }
  219. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  220. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  221. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  222. /*
  223. * Make dummy function to prevent NULL pointer dereferences
  224. */
  225. static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
  226. {
  227. ((void) buf);
  228. ((void) buf_len);
  229. return( -1 );
  230. }
  231. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
  232. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  233. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  234. /*
  235. * Make dummy function to prevent NULL pointer dereferences
  236. */
  237. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  238. {
  239. ((void) buf);
  240. ((void) buf_len);
  241. return( -1 );
  242. }
  243. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  244. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  245. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  246. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  247. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  248. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  249. int mbedtls_platform_set_nv_seed(
  250. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  251. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  252. {
  253. mbedtls_nv_seed_read = nv_seed_read_func;
  254. mbedtls_nv_seed_write = nv_seed_write_func;
  255. return( 0 );
  256. }
  257. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  258. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  259. #endif /* MBEDTLS_PLATFORM_C */