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 | |
| 34 | #include "mbedtls/platform_time.h" |
| 35 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 36 | #include <stddef.h> |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 37 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 38 | #include <time.h> |
| 39 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | /** |
| 46 | * \brief Securely zeroize a buffer |
| 47 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 48 | * The function is meant to wipe the data contained in a buffer so |
| 49 | * that it can no longer be recovered even if the program memory |
| 50 | * is later compromised. Call this function on sensitive data |
| 51 | * stored on the stack before returning from a function, and on |
| 52 | * sensitive data stored on the heap before freeing the heap |
| 53 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 54 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 55 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 56 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 57 | * compiler optimizations in a portable way. For this reason, Mbed |
| 58 | * TLS provides the configuration option |
| 59 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 60 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 61 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 62 | * |
| 63 | * \param buf Buffer to be zeroized |
| 64 | * \param len Length of the buffer in bytes |
| 65 | * |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 66 | */ |
| 67 | void mbedtls_platform_zeroize( void *buf, size_t len ); |
| 68 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 69 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 70 | /** |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 71 | * \brief Thread safe implementation of gmtime() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 72 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 73 | * The function is an abstraction that when called behaves similar |
| 74 | * to the gmtime() function from the C standard, but is thread |
| 75 | * safe. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 76 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 77 | * Mbed TLS will try to identify the underlying platform and |
| 78 | * configure an appropriate underlying implementation (e.g. |
| 79 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 80 | * not possible, then gmtime() will be used. In this case, calls |
| 81 | * from the library to gmtime() will be guarded by the mutex |
| 82 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 83 | * enabled. It is recommended that calls from outside the library |
| 84 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 85 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 86 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 87 | * unconditionally use the alternative implementation for |
| 88 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 89 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 90 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame^] | 91 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 92 | * \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] | 93 | * |
| 94 | * \return Pointer to an object of type struct tm on success, otherwise |
| 95 | * NULL |
| 96 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 97 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 98 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 99 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 100 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 101 | #ifdef __cplusplus |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |