blob: 6d9cfecfa8bf60b04624d989603e7e92c48b60cf [file] [log] [blame]
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -05001/**
2 * \file platform_util.h
3 *
4 * \brief Common and shared functions used by multiple modules in the Mbed TLS
5 * library.
6 */
7/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -05009 * 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.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050022 */
23#ifndef MBEDTLS_PLATFORM_UTIL_H
24#define MBEDTLS_PLATFORM_UTIL_H
25
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010026#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/config.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010028#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050032#include <stddef.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010033#if defined(MBEDTLS_HAVE_TIME_DATE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/platform_time.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010035#include <time.h>
36#endif /* MBEDTLS_HAVE_TIME_DATE */
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050037
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050042/* Internal macros meant to be called only from within the library. */
43#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
44#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
45
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046/* Internal helper macros for deprecating API constants. */
47#if !defined(MBEDTLS_DEPRECATED_REMOVED)
48#if defined(MBEDTLS_DEPRECATED_WARNING)
49/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
50 * to avoid conflict with other headers which define and use
51 * it, too. We might want to move all these definitions here at
52 * some point for uniformity. */
53#define MBEDTLS_DEPRECATED __attribute__((deprecated))
54MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
55#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
56 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
57MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
58#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
59 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
60#undef MBEDTLS_DEPRECATED
61#else /* MBEDTLS_DEPRECATED_WARNING */
62#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
63#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
64#endif /* MBEDTLS_DEPRECATED_WARNING */
65#endif /* MBEDTLS_DEPRECATED_REMOVED */
66
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050067/**
68 * \brief Securely zeroize a buffer
69 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -050070 * The function is meant to wipe the data contained in a buffer so
71 * that it can no longer be recovered even if the program memory
72 * is later compromised. Call this function on sensitive data
73 * stored on the stack before returning from a function, and on
74 * sensitive data stored on the heap before freeing the heap
75 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050076 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -050077 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050078 * mbedtls_platform_zeroize() are not removed by aggressive
79 * compiler optimizations in a portable way. For this reason, Mbed
80 * TLS provides the configuration option
81 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
82 * mbedtls_platform_zeroize() to use a suitable implementation for
83 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -050084 *
85 * \param buf Buffer to be zeroized
86 * \param len Length of the buffer in bytes
87 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050088 */
89void mbedtls_platform_zeroize( void *buf, size_t len );
90
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010091#if defined(MBEDTLS_HAVE_TIME_DATE)
92/**
Hanno Beckerc52ef402018-09-05 16:28:59 +010093 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010094 *
Hanno Beckerc52ef402018-09-05 16:28:59 +010095 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +010096 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010097 *
Hanno Becker6a739782018-09-05 15:06:19 +010098 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +010099 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +0100100 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
101 * not possible, then gmtime() will be used. In this case, calls
102 * from the library to gmtime() will be guarded by the mutex
103 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
104 * enabled. It is recommended that calls from outside the library
105 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100106 *
Hanno Becker6a739782018-09-05 15:06:19 +0100107 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
108 * unconditionally use the alternative implementation for
109 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100110 *
Hanno Becker272675f2018-09-05 14:03:02 +0100111 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100112 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100113 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100114 *
115 * \return Pointer to an object of type struct tm on success, otherwise
116 * NULL
117 */
Hanno Becker6a739782018-09-05 15:06:19 +0100118struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
119 struct tm *tm_buf );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100120#endif /* MBEDTLS_HAVE_TIME_DATE */
121
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500122#ifdef __cplusplus
123}
124#endif
125
126#endif /* MBEDTLS_PLATFORM_UTIL_H */