platform.c 8.2 KB

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