Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.  * \file platform.h
  3.  *
  4.  * \brief This file contains the definitions and functions of the
  5.  *        Mbed TLS platform abstraction layer.
  6.  *
  7.  *        The platform abstraction layer removes the need for the library
  8.  *        to directly link to standard C library functions or operating
  9.  *        system services, making the library easier to port and embed.
  10.  *        Application developers and users of the library can provide their own
  11.  *        implementations of these functions, or implementations specific to
  12.  *        their platform, which can be statically linked to the library or
  13.  *        dynamically configured at runtime.
  14.  */
  15. /*
  16.  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  17.  *  SPDX-License-Identifier: GPL-2.0
  18.  *
  19.  *  This program is free software; you can redistribute it and/or modify
  20.  *  it under the terms of the GNU General Public License as published by
  21.  *  the Free Software Foundation; either version 2 of the License, or
  22.  *  (at your option) any later version.
  23.  *
  24.  *  This program is distributed in the hope that it will be useful,
  25.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.  *  GNU General Public License for more details.
  28.  *
  29.  *  You should have received a copy of the GNU General Public License along
  30.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  31.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  32.  *
  33.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  34.  */
  35. #ifndef MBEDTLS_PLATFORM_H
  36. #define MBEDTLS_PLATFORM_H
  37.  
  38. #if !defined(MBEDTLS_CONFIG_FILE)
  39. #include "config.h"
  40. #else
  41. #include MBEDTLS_CONFIG_FILE
  42. #endif
  43.  
  44. #if defined(MBEDTLS_HAVE_TIME)
  45. #include "platform_time.h"
  46. #endif
  47.  
  48. #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED     -0x0070 /**< Hardware accelerator failed */
  49. #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072 /**< The requested feature is not supported by the platform */
  50.  
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54.  
  55. /**
  56.  * \name SECTION: Module settings
  57.  *
  58.  * The configuration options you can set for this module are in this section.
  59.  * Either change them in config.h or define them on the compiler command line.
  60.  * \{
  61.  */
  62.  
  63. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <time.h>
  67. #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
  68. #if defined(_WIN32)
  69. #define MBEDTLS_PLATFORM_STD_SNPRINTF   mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use.  */
  70. #else
  71. #define MBEDTLS_PLATFORM_STD_SNPRINTF   snprintf /**< The default \c snprintf function to use.  */
  72. #endif
  73. #endif
  74. #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
  75. #define MBEDTLS_PLATFORM_STD_PRINTF   printf /**< The default \c printf function to use. */
  76. #endif
  77. #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
  78. #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
  79. #endif
  80. #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
  81. #define MBEDTLS_PLATFORM_STD_CALLOC   calloc /**< The default \c calloc function to use. */
  82. #endif
  83. #if !defined(MBEDTLS_PLATFORM_STD_FREE)
  84. #define MBEDTLS_PLATFORM_STD_FREE       free /**< The default \c free function to use. */
  85. #endif
  86. #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
  87. #define MBEDTLS_PLATFORM_STD_EXIT      exit /**< The default \c exit function to use. */
  88. #endif
  89. #if !defined(MBEDTLS_PLATFORM_STD_TIME)
  90. #define MBEDTLS_PLATFORM_STD_TIME       time    /**< The default \c time function to use. */
  91. #endif
  92. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  93. #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS /**< The default exit value to use. */
  94. #endif
  95. #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  96. #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE /**< The default exit value to use. */
  97. #endif
  98. #if defined(MBEDTLS_FS_IO)
  99. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
  100. #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   mbedtls_platform_std_nv_seed_read
  101. #endif
  102. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
  103. #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE  mbedtls_platform_std_nv_seed_write
  104. #endif
  105. #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
  106. #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE   "seedfile"
  107. #endif
  108. #endif /* MBEDTLS_FS_IO */
  109. #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  110. #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
  111. #include MBEDTLS_PLATFORM_STD_MEM_HDR
  112. #endif
  113. #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
  114.  
  115.  
  116. /* \} name SECTION: Module settings */
  117.  
  118. /*
  119.  * The function pointers for calloc and free.
  120.  */
  121. #if defined(MBEDTLS_PLATFORM_MEMORY)
  122. #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
  123.     defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
  124. #define mbedtls_free       MBEDTLS_PLATFORM_FREE_MACRO
  125. #define mbedtls_calloc     MBEDTLS_PLATFORM_CALLOC_MACRO
  126. #else
  127. /* For size_t */
  128. #include <stddef.h>
  129. extern void *mbedtls_calloc( size_t n, size_t size );
  130. extern void mbedtls_free( void *ptr );
  131.  
  132. /**
  133.  * \brief               This function dynamically sets the memory-management
  134.  *                      functions used by the library, during runtime.
  135.  *
  136.  * \param calloc_func   The \c calloc function implementation.
  137.  * \param free_func     The \c free function implementation.
  138.  *
  139.  * \return              \c 0.
  140.  */
  141. int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
  142.                               void (*free_func)( void * ) );
  143. #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
  144. #else /* !MBEDTLS_PLATFORM_MEMORY */
  145. #define mbedtls_free       free
  146. #define mbedtls_calloc     calloc
  147. #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
  148.  
  149. /*
  150.  * The function pointers for fprintf
  151.  */
  152. #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
  153. /* We need FILE * */
  154. #include <stdio.h>
  155. extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  156.  
  157. /**
  158.  * \brief                This function dynamically configures the fprintf
  159.  *                       function that is called when the
  160.  *                       mbedtls_fprintf() function is invoked by the library.
  161.  *
  162.  * \param fprintf_func   The \c fprintf function implementation.
  163.  *
  164.  * \return               \c 0.
  165.  */
  166. int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
  167.                                                ... ) );
  168. #else
  169. #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
  170. #define mbedtls_fprintf    MBEDTLS_PLATFORM_FPRINTF_MACRO
  171. #else
  172. #define mbedtls_fprintf    fprintf
  173. #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
  174. #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
  175.  
  176. /*
  177.  * The function pointers for printf
  178.  */
  179. #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
  180. extern int (*mbedtls_printf)( const char *format, ... );
  181.  
  182. /**
  183.  * \brief               This function dynamically configures the snprintf
  184.  *                      function that is called when the mbedtls_snprintf()
  185.  *                      function is invoked by the library.
  186.  *
  187.  * \param printf_func   The \c printf function implementation.
  188.  *
  189.  * \return              \c 0 on success.
  190.  */
  191. int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
  192. #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
  193. #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
  194. #define mbedtls_printf     MBEDTLS_PLATFORM_PRINTF_MACRO
  195. #else
  196. #define mbedtls_printf     printf
  197. #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
  198. #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
  199.  
  200. /*
  201.  * The function pointers for snprintf
  202.  *
  203.  * The snprintf implementation should conform to C99:
  204.  * - it *must* always correctly zero-terminate the buffer
  205.  *   (except when n == 0, then it must leave the buffer untouched)
  206.  * - however it is acceptable to return -1 instead of the required length when
  207.  *   the destination buffer is too short.
  208.  */
  209. #if defined(_WIN32)
  210. /* For Windows (inc. MSYS2), we provide our own fixed implementation */
  211. int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
  212. #endif
  213.  
  214. #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
  215. extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
  216.  
  217. /**
  218.  * \brief                 This function allows configuring a custom
  219.  *                        \c snprintf function pointer.
  220.  *
  221.  * \param snprintf_func   The \c snprintf function implementation.
  222.  *
  223.  * \return                \c 0 on success.
  224.  */
  225. int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  226.                                                  const char * format, ... ) );
  227. #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  228. #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
  229. #define mbedtls_snprintf   MBEDTLS_PLATFORM_SNPRINTF_MACRO
  230. #else
  231. #define mbedtls_snprintf   MBEDTLS_PLATFORM_STD_SNPRINTF
  232. #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
  233. #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
  234.  
  235. /*
  236.  * The function pointers for exit
  237.  */
  238. #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
  239. extern void (*mbedtls_exit)( int status );
  240.  
  241. /**
  242.  * \brief             This function dynamically configures the exit
  243.  *                    function that is called when the mbedtls_exit()
  244.  *                    function is invoked by the library.
  245.  *
  246.  * \param exit_func   The \c exit function implementation.
  247.  *
  248.  * \return            \c 0 on success.
  249.  */
  250. int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
  251. #else
  252. #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
  253. #define mbedtls_exit   MBEDTLS_PLATFORM_EXIT_MACRO
  254. #else
  255. #define mbedtls_exit   exit
  256. #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
  257. #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
  258.  
  259. /*
  260.  * The default exit values
  261.  */
  262. #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
  263. #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
  264. #else
  265. #define MBEDTLS_EXIT_SUCCESS 0
  266. #endif
  267. #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
  268. #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
  269. #else
  270. #define MBEDTLS_EXIT_FAILURE 1
  271. #endif
  272.  
  273. /*
  274.  * The function pointers for reading from and writing a seed file to
  275.  * Non-Volatile storage (NV) in a platform-independent way
  276.  *
  277.  * Only enabled when the NV seed entropy source is enabled
  278.  */
  279. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  280. #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
  281. /* Internal standard platform definitions */
  282. int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
  283. int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
  284. #endif
  285.  
  286. #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
  287. extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
  288. extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
  289.  
  290. /**
  291.  * \brief   This function allows configuring custom seed file writing and
  292.  *          reading functions.
  293.  *
  294.  * \param   nv_seed_read_func   The seed reading function implementation.
  295.  * \param   nv_seed_write_func  The seed writing function implementation.
  296.  *
  297.  * \return  \c 0 on success.
  298.  */
  299. int mbedtls_platform_set_nv_seed(
  300.             int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
  301.             int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
  302.             );
  303. #else
  304. #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
  305.     defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
  306. #define mbedtls_nv_seed_read    MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
  307. #define mbedtls_nv_seed_write   MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
  308. #else
  309. #define mbedtls_nv_seed_read    mbedtls_platform_std_nv_seed_read
  310. #define mbedtls_nv_seed_write   mbedtls_platform_std_nv_seed_write
  311. #endif
  312. #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
  313. #endif /* MBEDTLS_ENTROPY_NV_SEED */
  314.  
  315. #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
  316.  
  317. /**
  318.  * \brief   The platform context structure.
  319.  *
  320.  * \note    This structure may be used to assist platform-specific
  321.  *          setup or teardown operations.
  322.  */
  323. typedef struct mbedtls_platform_context
  324. {
  325.     char dummy; /**< A placeholder member, as empty structs are not portable. */
  326. }
  327. mbedtls_platform_context;
  328.  
  329. #else
  330. #include "platform_alt.h"
  331. #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
  332.  
  333. /**
  334.  * \brief   This function performs any platform-specific initialization
  335.  *          operations.
  336.  *
  337.  * \note    This function should be called before any other library functions.
  338.  *
  339.  *          Its implementation is platform-specific, and unless
  340.  *          platform-specific code is provided, it does nothing.
  341.  *
  342.  * \note    The usage and necessity of this function is dependent on the platform.
  343.  *
  344.  * \param   ctx     The platform context.
  345.  *
  346.  * \return  \c 0 on success.
  347.  */
  348. int mbedtls_platform_setup( mbedtls_platform_context *ctx );
  349. /**
  350.  * \brief   This function performs any platform teardown operations.
  351.  *
  352.  * \note    This function should be called after every other Mbed TLS module
  353.  *          has been correctly freed using the appropriate free function.
  354.  *
  355.  *          Its implementation is platform-specific, and unless
  356.  *          platform-specific code is provided, it does nothing.
  357.  *
  358.  * \note    The usage and necessity of this function is dependent on the platform.
  359.  *
  360.  * \param   ctx     The platform context.
  361.  *
  362.  */
  363. void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
  364.  
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368.  
  369. #endif /* platform.h */
  370.