Magnificent7 / Hackathon
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers platform.h Source File

platform.h

00001 /**
00002  * \file platform.h
00003  *
00004  * \brief mbed TLS Platform abstraction layer
00005  *
00006  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
00007  *  SPDX-License-Identifier: Apache-2.0
00008  *
00009  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  *  not use this file except in compliance with the License.
00011  *  You may obtain a copy of the License at
00012  *
00013  *  http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  *  Unless required by applicable law or agreed to in writing, software
00016  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  *  See the License for the specific language governing permissions and
00019  *  limitations under the License.
00020  *
00021  *  This file is part of mbed TLS (https://tls.mbed.org)
00022  */
00023 #ifndef MBEDTLS_PLATFORM_H
00024 #define MBEDTLS_PLATFORM_H
00025 
00026 #if !defined(MBEDTLS_CONFIG_FILE)
00027 #include "config.h"
00028 #else
00029 #include MBEDTLS_CONFIG_FILE
00030 #endif
00031 
00032 #ifdef __cplusplus
00033 extern "C" {
00034 #endif
00035 
00036 /**
00037  * \name SECTION: Module settings
00038  *
00039  * The configuration options you can set for this module are in this section.
00040  * Either change them in config.h or define them on the compiler command line.
00041  * \{
00042  */
00043 
00044 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <time.h>
00048 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
00049 #if defined(_WIN32)
00050 #define MBEDTLS_PLATFORM_STD_SNPRINTF   mbedtls_platform_win32_snprintf /**< Default snprintf to use  */
00051 #else
00052 #define MBEDTLS_PLATFORM_STD_SNPRINTF   snprintf /**< Default snprintf to use  */
00053 #endif
00054 #endif
00055 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
00056 #define MBEDTLS_PLATFORM_STD_PRINTF   printf /**< Default printf to use  */
00057 #endif
00058 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
00059 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
00060 #endif
00061 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
00062 #define MBEDTLS_PLATFORM_STD_CALLOC   calloc /**< Default allocator to use */
00063 #endif
00064 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
00065 #define MBEDTLS_PLATFORM_STD_FREE       free /**< Default free to use */
00066 #endif
00067 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
00068 #define MBEDTLS_PLATFORM_STD_EXIT      exit /**< Default exit to use */
00069 #endif
00070 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
00071 #define MBEDTLS_PLATFORM_STD_TIME       time    /**< Default time to use */
00072 #endif
00073 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
00074 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS /**< Default exit value to use */
00075 #endif
00076 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
00077 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE /**< Default exit value to use */
00078 #endif
00079 #if defined(MBEDTLS_FS_IO)
00080 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
00081 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   mbedtls_platform_std_nv_seed_read
00082 #endif
00083 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
00084 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE  mbedtls_platform_std_nv_seed_write
00085 #endif
00086 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
00087 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE   "seedfile"
00088 #endif
00089 #endif /* MBEDTLS_FS_IO */
00090 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
00091 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
00092 #include MBEDTLS_PLATFORM_STD_MEM_HDR
00093 #endif
00094 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
00095 
00096 
00097 /* \} name SECTION: Module settings */
00098 
00099 /*
00100  * The function pointers for calloc and free
00101  */
00102 #if defined(MBEDTLS_PLATFORM_MEMORY)
00103 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
00104     defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
00105 #define mbedtls_free       MBEDTLS_PLATFORM_FREE_MACRO
00106 #define mbedtls_calloc     MBEDTLS_PLATFORM_CALLOC_MACRO
00107 #else
00108 /* For size_t */
00109 #include <stddef.h>
00110 extern void * (*mbedtls_calloc)( size_t n, size_t size );
00111 extern void (*mbedtls_free)( void *ptr );
00112 
00113 /**
00114  * \brief   Set your own memory implementation function pointers
00115  *
00116  * \param calloc_func   the calloc function implementation
00117  * \param free_func     the free function implementation
00118  *
00119  * \return              0 if successful
00120  */
00121 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
00122                               void (*free_func)( void * ) );
00123 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
00124 #else /* !MBEDTLS_PLATFORM_MEMORY */
00125 #define mbedtls_free       free
00126 #define mbedtls_calloc     calloc
00127 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
00128 
00129 /*
00130  * The function pointers for fprintf
00131  */
00132 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
00133 /* We need FILE * */
00134 #include <stdio.h>
00135 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
00136 
00137 /**
00138  * \brief   Set your own fprintf function pointer
00139  *
00140  * \param fprintf_func   the fprintf function implementation
00141  *
00142  * \return              0
00143  */
00144 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
00145                                                ... ) );
00146 #else
00147 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
00148 #define mbedtls_fprintf    MBEDTLS_PLATFORM_FPRINTF_MACRO
00149 #else
00150 #define mbedtls_fprintf    fprintf
00151 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
00152 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
00153 
00154 /*
00155  * The function pointers for printf
00156  */
00157 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
00158 extern int (*mbedtls_printf)( const char *format, ... );
00159 
00160 /**
00161  * \brief   Set your own printf function pointer
00162  *
00163  * \param printf_func   the printf function implementation
00164  *
00165  * \return              0
00166  */
00167 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
00168 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
00169 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
00170 #define mbedtls_printf     MBEDTLS_PLATFORM_PRINTF_MACRO
00171 #else
00172 #define mbedtls_printf     printf
00173 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
00174 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
00175 
00176 /*
00177  * The function pointers for snprintf
00178  *
00179  * The snprintf implementation should conform to C99:
00180  * - it *must* always correctly zero-terminate the buffer
00181  *   (except when n == 0, then it must leave the buffer untouched)
00182  * - however it is acceptable to return -1 instead of the required length when
00183  *   the destination buffer is too short.
00184  */
00185 #if defined(_WIN32)
00186 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
00187 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
00188 #endif
00189 
00190 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
00191 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
00192 
00193 /**
00194  * \brief   Set your own snprintf function pointer
00195  *
00196  * \param snprintf_func   the snprintf function implementation
00197  *
00198  * \return              0
00199  */
00200 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
00201                                                  const char * format, ... ) );
00202 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
00203 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
00204 #define mbedtls_snprintf   MBEDTLS_PLATFORM_SNPRINTF_MACRO
00205 #else
00206 #define mbedtls_snprintf   snprintf
00207 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
00208 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
00209 
00210 /*
00211  * The function pointers for exit
00212  */
00213 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
00214 extern void (*mbedtls_exit)( int status );
00215 
00216 /**
00217  * \brief   Set your own exit function pointer
00218  *
00219  * \param exit_func   the exit function implementation
00220  *
00221  * \return              0
00222  */
00223 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
00224 #else
00225 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
00226 #define mbedtls_exit   MBEDTLS_PLATFORM_EXIT_MACRO
00227 #else
00228 #define mbedtls_exit   exit
00229 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
00230 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
00231 
00232 /*
00233  * The default exit values
00234  */
00235 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
00236 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
00237 #else
00238 #define MBEDTLS_EXIT_SUCCESS 0
00239 #endif
00240 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
00241 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
00242 #else
00243 #define MBEDTLS_EXIT_FAILURE 1
00244 #endif
00245 
00246 /*
00247  * The time_t datatype
00248  */
00249 #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
00250 typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
00251 #else
00252 /* For time_t */
00253 #include <time.h>
00254 typedef time_t mbedtls_time_t;
00255 #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
00256 
00257 /*
00258  * The function pointers for time
00259  */
00260 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
00261 extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time );
00262 
00263 /**
00264  * \brief   Set your own time function pointer
00265  *
00266  * \param   time_func   the time function implementation
00267  *
00268  * \return              0
00269  */
00270 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) );
00271 #else
00272 #if defined(MBEDTLS_PLATFORM_TIME_MACRO)
00273 #define mbedtls_time    MBEDTLS_PLATFORM_TIME_MACRO
00274 #else
00275 #define mbedtls_time   time
00276 #endif /* MBEDTLS_PLATFORM_TIME_MACRO */
00277 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
00278 
00279 /*
00280  * The function pointers for reading from and writing a seed file to
00281  * Non-Volatile storage (NV) in a platform-independent way
00282  *
00283  * Only enabled when the NV seed entropy source is enabled
00284  */
00285 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00286 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
00287 /* Internal standard platform definitions */
00288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
00289 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
00290 #endif
00291 
00292 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
00293 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
00294 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
00295 
00296 /**
00297  * \brief   Set your own seed file writing/reading functions
00298  *
00299  * \param   nv_seed_read_func   the seed reading function implementation
00300  * \param   nv_seed_write_func  the seed writing function implementation
00301  *
00302  * \return              0
00303  */
00304 int mbedtls_platform_set_nv_seed(
00305             int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
00306             int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
00307             );
00308 #else
00309 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
00310     defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
00311 #define mbedtls_nv_seed_read    MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
00312 #define mbedtls_nv_seed_write   MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
00313 #else
00314 #define mbedtls_nv_seed_read    mbedtls_platform_std_nv_seed_read
00315 #define mbedtls_nv_seed_write   mbedtls_platform_std_nv_seed_write
00316 #endif
00317 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
00318 #endif /* MBEDTLS_ENTROPY_NV_SEED */
00319 
00320 #ifdef __cplusplus
00321 }
00322 #endif
00323 
00324 #endif /* platform.h */