Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 1 | /** |
| 2 | * \file platform_util.h |
| 3 | * |
| 4 | * \brief Common and shared functions used by multiple modules in the Mbed TLS |
| 5 | * library. |
| 6 | */ |
| 7 | /* |
| 8 | * Copyright (C) 2018, Arm Limited, All Rights Reserved |
| 9 | * SPDX-License-Identifier: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
| 22 | * |
| 23 | * This file is part of Mbed TLS (https://tls.mbed.org) |
| 24 | */ |
| 25 | #ifndef MBEDTLS_PLATFORM_UTIL_H |
| 26 | #define MBEDTLS_PLATFORM_UTIL_H |
| 27 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 29 | #include "mbedtls/config.h" |
| 30 | #else |
| 31 | #include MBEDTLS_CONFIG_FILE |
| 32 | #endif |
| 33 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 34 | #include <stddef.h> |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 35 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Hanno Becker | 5a7fe14 | 2018-09-05 16:24:34 +0100 | [diff] [blame] | 36 | #include "mbedtls/platform_time.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 37 | #include <time.h> |
| 38 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Simon Butcher | 4c37db6 | 2018-12-09 22:42:11 +0000 | [diff] [blame^] | 44 | #if defined( MBEDTLS_CHECK_PARAMS ) && !defined(MBEDTLS_PARAM_FAILED) |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 45 | #define MBEDTLS_PARAM_FAILED( cond, file, line ) \ |
| 46 | mbedtls_param_failed( cond, file, line ) |
| 47 | |
| 48 | /** |
| 49 | * \brief User supplied callback function for parameter validation failure. |
| 50 | * |
| 51 | * When the MBEDTLS_CHECK_PARAMS option is enabled, the library |
| 52 | * provides additional validation of all input parameters to |
| 53 | * confirm that they conform to what the interface can accept. |
| 54 | * For example - NULL paramater checks. |
| 55 | * |
| 56 | * These checks are designed to check programmatic issues in the |
| 57 | * application software using Mbed TLS, or catch other runtime |
| 58 | * errors which may be due to issues in the application software. |
| 59 | * |
| 60 | * This function will be called unless an alternative function is |
| 61 | * defined through the MBEDTLS_PARAM_FAILURE function. |
| 62 | * |
| 63 | * This function can return, and the operation will be aborted, or |
| 64 | * alternatively, through use of setjmp()/longjmp() can resume |
| 65 | * execution in the application code. |
| 66 | */ |
| 67 | void mbedtls_param_failed( char* failure_condition, char* file, int line ); |
| 68 | |
Simon Butcher | 4c37db6 | 2018-12-09 22:42:11 +0000 | [diff] [blame^] | 69 | #endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED */ |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 70 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 71 | /** |
| 72 | * \brief Securely zeroize a buffer |
| 73 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 74 | * The function is meant to wipe the data contained in a buffer so |
| 75 | * that it can no longer be recovered even if the program memory |
| 76 | * is later compromised. Call this function on sensitive data |
| 77 | * stored on the stack before returning from a function, and on |
| 78 | * sensitive data stored on the heap before freeing the heap |
| 79 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 80 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 81 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 82 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 83 | * compiler optimizations in a portable way. For this reason, Mbed |
| 84 | * TLS provides the configuration option |
| 85 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 86 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 87 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 88 | * |
| 89 | * \param buf Buffer to be zeroized |
| 90 | * \param len Length of the buffer in bytes |
| 91 | * |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 92 | */ |
| 93 | void mbedtls_platform_zeroize( void *buf, size_t len ); |
| 94 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 95 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 96 | /** |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 97 | * \brief Platform-specific implementation of gmtime_r() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 98 | * |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 99 | * The function is a thread-safe abstraction that behaves |
Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 100 | * similarly to the gmtime_r() function from Unix/POSIX. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 101 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 102 | * Mbed TLS will try to identify the underlying platform and |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 103 | * make use of an appropriate underlying implementation (e.g. |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 104 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 105 | * not possible, then gmtime() will be used. In this case, calls |
| 106 | * from the library to gmtime() will be guarded by the mutex |
| 107 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 108 | * enabled. It is recommended that calls from outside the library |
| 109 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 110 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 111 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 112 | * unconditionally use the alternative implementation for |
| 113 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 114 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 115 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 116 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 117 | * \param tm_buf Pointer to an object where the results will be stored |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 118 | * |
| 119 | * \return Pointer to an object of type struct tm on success, otherwise |
| 120 | * NULL |
| 121 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 122 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 123 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 124 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 125 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 126 | #ifdef __cplusplus |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |