platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Platform abstraction layer
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #include "common.h"
  20. #if defined(MBEDTLS_PLATFORM_C)
  21. #include "mbedtls/platform.h"
  22. #include "mbedtls/platform_util.h"
  23. #include "mbedtls/error.h"
  24. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SSL
  25. #include "osi_api.h"
  26. #include "stdlib.h"
  27. #include "string.h"
  28. #endif /*CONFIG_QUEC_PROJECT_FEATURE_SSL*/
  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. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SSL
  40. void *p = malloc((n) * (size));
  41. if (p != NULL)
  42. {
  43. memset(p, 0, (n) * (size));
  44. }
  45. return p;
  46. #else
  47. ((void) n);
  48. ((void) size);
  49. return( NULL );
  50. #endif
  51. }
  52. #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
  53. #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
  54. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  55. static void platform_free_uninit( void *ptr )
  56. {
  57. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SSL
  58. free(ptr);
  59. #else
  60. ((void) ptr);
  61. #endif /*CONFIG_QUEC_PROJECT_FEATURE_SSL*/
  62. }
  63. #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
  64. #endif /* !MBEDTLS_PLATFORM_STD_FREE */
  65. static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
  66. static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
  67. void * mbedtls_calloc( size_t nmemb, size_t size )
  68. {
  69. return (*mbedtls_calloc_func)( nmemb, size );
  70. }
  71. void mbedtls_free( void * ptr )
  72. {
  73. (*mbedtls_free_func)( ptr );
  74. }
  75. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  76. void (*free_func)( void * ) )
  77. {
  78. mbedtls_calloc_func = calloc_func;
  79. mbedtls_free_func = free_func;
  80. return( 0 );
  81. }
  82. #endif /* MBEDTLS_PLATFORM_MEMORY &&
  83. !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
  84. defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
  85. #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
  86. #include <stdarg.h>
  87. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
  88. {
  89. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  90. va_list argp;
  91. va_start( argp, fmt );
  92. ret = mbedtls_vsnprintf( s, n, fmt, argp );
  93. va_end( argp );
  94. return( ret );
  95. }
  96. #endif
  97. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  98. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  99. /*
  100. * Make dummy function to prevent NULL pointer dereferences
  101. */
  102. static int platform_snprintf_uninit( char * s, size_t n,
  103. const char * format, ... )
  104. {
  105. ((void) s);
  106. ((void) n);
  107. ((void) format);
  108. return( 0 );
  109. }
  110. #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
  111. #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
  112. int (*mbedtls_snprintf)( char * s, size_t n,
  113. const char * format,
  114. ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
  115. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  116. const char * format,
  117. ... ) )
  118. {
  119. mbedtls_snprintf = snprintf_func;
  120. return( 0 );
  121. }
  122. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  123. #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
  124. #include <stdarg.h>
  125. int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
  126. {
  127. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  128. /* Avoid calling the invalid parameter handler by checking ourselves */
  129. if( s == NULL || n == 0 || fmt == NULL )
  130. return( -1 );
  131. #if defined(_TRUNCATE)
  132. ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg );
  133. #else
  134. ret = vsnprintf( s, n, fmt, arg );
  135. if( ret < 0 || (size_t) ret == n )
  136. {
  137. s[n-1] = '\0';
  138. ret = -1;
  139. }
  140. #endif
  141. return( ret );
  142. }
  143. #endif
  144. #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
  145. #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
  146. /*
  147. * Make dummy function to prevent NULL pointer dereferences
  148. */
  149. static int platform_vsnprintf_uninit( char * s, size_t n,
  150. const char * format, va_list arg )
  151. {
  152. ((void) s);
  153. ((void) n);
  154. ((void) format);
  155. ((void) arg);
  156. return( -1 );
  157. }
  158. #define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
  159. #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
  160. int (*mbedtls_vsnprintf)( char * s, size_t n,
  161. const char * format,
  162. va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
  163. int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
  164. const char * format,
  165. va_list arg ) )
  166. {
  167. mbedtls_vsnprintf = vsnprintf_func;
  168. return( 0 );
  169. }
  170. #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
  171. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  172. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  173. /*
  174. * Make dummy function to prevent NULL pointer dereferences
  175. */
  176. static int platform_printf_uninit( const char *format, ... )
  177. {
  178. ((void) format);
  179. return( 0 );
  180. }
  181. #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
  182. #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
  183. int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
  184. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
  185. {
  186. mbedtls_printf = printf_func;
  187. return( 0 );
  188. }
  189. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  190. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  191. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  192. /*
  193. * Make dummy function to prevent NULL pointer dereferences
  194. */
  195. static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
  196. {
  197. ((void) stream);
  198. ((void) format);
  199. return( 0 );
  200. }
  201. #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
  202. #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
  203. int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
  204. MBEDTLS_PLATFORM_STD_FPRINTF;
  205. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
  206. {
  207. mbedtls_fprintf = fprintf_func;
  208. return( 0 );
  209. }
  210. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  211. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  212. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  213. /*
  214. * Make dummy function to prevent NULL pointer dereferences
  215. */
  216. static void platform_exit_uninit( int status )
  217. {
  218. ((void) status);
  219. }
  220. #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
  221. #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
  222. void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
  223. int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
  224. {
  225. mbedtls_exit = exit_func;
  226. return( 0 );
  227. }
  228. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  229. #if defined(MBEDTLS_HAVE_TIME)
  230. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  231. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  232. /*
  233. * Make dummy function to prevent NULL pointer dereferences
  234. */
  235. static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
  236. {
  237. ((void) timer);
  238. return( 0 );
  239. }
  240. #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
  241. #endif /* !MBEDTLS_PLATFORM_STD_TIME */
  242. mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
  243. int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
  244. {
  245. mbedtls_time = time_func;
  246. return( 0 );
  247. }
  248. #endif /* MBEDTLS_PLATFORM_TIME_ALT */
  249. #endif /* MBEDTLS_HAVE_TIME */
  250. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  251. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  252. /* Default implementations for the platform independent seed functions use
  253. * standard libc file functions to read from and write to a pre-defined filename
  254. */
  255. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
  256. {
  257. FILE *file;
  258. size_t n;
  259. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
  260. return( -1 );
  261. if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
  262. {
  263. fclose( file );
  264. mbedtls_platform_zeroize( buf, buf_len );
  265. return( -1 );
  266. }
  267. fclose( file );
  268. return( (int)n );
  269. }
  270. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
  271. {
  272. FILE *file;
  273. size_t n;
  274. if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
  275. return -1;
  276. if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
  277. {
  278. fclose( file );
  279. return -1;
  280. }
  281. fclose( file );
  282. return( (int)n );
  283. }
  284. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  285. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  286. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  287. /*
  288. * Make dummy function to prevent NULL pointer dereferences
  289. */
  290. static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
  291. {
  292. ((void) buf);
  293. ((void) buf_len);
  294. return( -1 );
  295. }
  296. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
  297. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
  298. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  299. /*
  300. * Make dummy function to prevent NULL pointer dereferences
  301. */
  302. static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
  303. {
  304. ((void) buf);
  305. ((void) buf_len);
  306. return( -1 );
  307. }
  308. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
  309. #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
  310. int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
  311. MBEDTLS_PLATFORM_STD_NV_SEED_READ;
  312. int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
  313. MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
  314. int mbedtls_platform_set_nv_seed(
  315. int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  316. int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
  317. {
  318. mbedtls_nv_seed_read = nv_seed_read_func;
  319. mbedtls_nv_seed_write = nv_seed_write_func;
  320. return( 0 );
  321. }
  322. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  323. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  324. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  325. /*
  326. * Placeholder platform setup that does nothing by default
  327. */
  328. int mbedtls_platform_setup( mbedtls_platform_context *ctx )
  329. {
  330. (void)ctx;
  331. return( 0 );
  332. }
  333. /*
  334. * Placeholder platform teardown that does nothing by default
  335. */
  336. void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
  337. {
  338. (void)ctx;
  339. }
  340. #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  341. #endif /* MBEDTLS_PLATFORM_C */