blob: 9da6f0fa0e2d4a44d6f1db3a48a0252bd49c3f30 [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/*
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 Garcia1abb3682018-08-16 21:42:09 +010028#if !defined(MBEDTLS_CONFIG_FILE)
29#include "mbedtls/config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050034#include <stddef.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010035#if defined(MBEDTLS_HAVE_TIME_DATE)
Hanno Becker5a7fe142018-09-05 16:24:34 +010036#include "mbedtls/platform_time.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010037#include <time.h>
38#endif /* MBEDTLS_HAVE_TIME_DATE */
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
Hanno Becker6d0816a2018-12-17 11:30:27 +000044/* Internal helper macros for deprecating API constants. */
45#if !defined(MBEDTLS_DEPRECATED_REMOVED)
46#if defined(MBEDTLS_DEPRECATED_WARNING)
47/* Deliberately don't (yet) define MBEDTLS_DEPRECATED here to avoid
48 * conflict with other headers which define and use it, too.
49 * We might want to move all these definitions here at some point
50 * for uniformity. */
51__attribute__((deprecated))
52typedef char const * mbedtls_deprecated_string_constant_t;
53#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
54 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
55 __attribute__((deprecated))
56typedef int mbedtls_deprecated_numeric_constant_t;
57#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
58 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
59#else /* MBEDTLS_DEPRECATED_WARNING */
60#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
61#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
62#endif /* MBEDTLS_DEPRECATED_WARNING */
63#endif /* MBEDTLS_DEPRECATED_REMOVED */
64
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050065/**
66 * \brief Securely zeroize a buffer
67 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -050068 * The function is meant to wipe the data contained in a buffer so
69 * that it can no longer be recovered even if the program memory
70 * is later compromised. Call this function on sensitive data
71 * stored on the stack before returning from a function, and on
72 * sensitive data stored on the heap before freeing the heap
73 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050074 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -050075 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050076 * mbedtls_platform_zeroize() are not removed by aggressive
77 * compiler optimizations in a portable way. For this reason, Mbed
78 * TLS provides the configuration option
79 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
80 * mbedtls_platform_zeroize() to use a suitable implementation for
81 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -050082 *
83 * \param buf Buffer to be zeroized
84 * \param len Length of the buffer in bytes
85 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050086 */
87void mbedtls_platform_zeroize( void *buf, size_t len );
88
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010089#if defined(MBEDTLS_HAVE_TIME_DATE)
90/**
Hanno Beckerc52ef402018-09-05 16:28:59 +010091 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010092 *
Hanno Beckerc52ef402018-09-05 16:28:59 +010093 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +010094 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010095 *
Hanno Becker6a739782018-09-05 15:06:19 +010096 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +010097 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +010098 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
99 * not possible, then gmtime() will be used. In this case, calls
100 * from the library to gmtime() will be guarded by the mutex
101 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
102 * enabled. It is recommended that calls from outside the library
103 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100104 *
Hanno Becker6a739782018-09-05 15:06:19 +0100105 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
106 * unconditionally use the alternative implementation for
107 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100108 *
Hanno Becker272675f2018-09-05 14:03:02 +0100109 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100110 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100111 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100112 *
113 * \return Pointer to an object of type struct tm on success, otherwise
114 * NULL
115 */
Hanno Becker6a739782018-09-05 15:06:19 +0100116struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
117 struct tm *tm_buf );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100118#endif /* MBEDTLS_HAVE_TIME_DATE */
119
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500120#ifdef __cplusplus
121}
122#endif
123
124#endif /* MBEDTLS_PLATFORM_UTIL_H */