blob: dba6d45982279fe2536a552a5012e590dfdfa338 [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)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029#include "config.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010030#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)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050036#include "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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044#if defined(MBEDTLS_CHECK_PARAMS)
45
46#if defined(MBEDTLS_PARAM_FAILED)
47/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
48 *
49 * This flag can be used to check whether it is safe to assume that
50 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
51 */
52#define MBEDTLS_PARAM_FAILED_ALT
53#else /* MBEDTLS_PARAM_FAILED */
54#define MBEDTLS_PARAM_FAILED( cond ) \
55 mbedtls_param_failed( #cond, __FILE__, __LINE__ )
56
57/**
58 * \brief User supplied callback function for parameter validation failure.
59 * See #MBEDTLS_CHECK_PARAMS for context.
60 *
61 * This function will be called unless an alternative treatement
62 * is defined through the #MBEDTLS_PARAM_FAILED macro.
63 *
64 * This function can return, and the operation will be aborted, or
65 * alternatively, through use of setjmp()/longjmp() can resume
66 * execution in the application code.
67 *
68 * \param failure_condition The assertion that didn't hold.
69 * \param file The file where the assertion failed.
70 * \param line The line in the file where the assertion failed.
71 */
72void mbedtls_param_failed( const char *failure_condition,
73 const char *file,
74 int line );
75#endif /* MBEDTLS_PARAM_FAILED */
76
77/* Internal macro meant to be called only from within the library. */
78#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
79 do { \
80 if( !(cond) ) \
81 { \
82 MBEDTLS_PARAM_FAILED( cond ); \
83 return( ret ); \
84 } \
85 } while( 0 )
86
87/* Internal macro meant to be called only from within the library. */
88#define MBEDTLS_INTERNAL_VALIDATE( cond ) \
89 do { \
90 if( !(cond) ) \
91 { \
92 MBEDTLS_PARAM_FAILED( cond ); \
93 return; \
94 } \
95 } while( 0 )
96
97#else /* MBEDTLS_CHECK_PARAMS */
98
99/* Internal macros meant to be called only from within the library. */
100#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
101#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
102
103#endif /* MBEDTLS_CHECK_PARAMS */
104
105/* Internal helper macros for deprecating API constants. */
106#if !defined(MBEDTLS_DEPRECATED_REMOVED)
107#if defined(MBEDTLS_DEPRECATED_WARNING)
108/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
109 * to avoid conflict with other headers which define and use
110 * it, too. We might want to move all these definitions here at
111 * some point for uniformity. */
112#define MBEDTLS_DEPRECATED __attribute__((deprecated))
113MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
114#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
115 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
116MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
117#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
118 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
119#undef MBEDTLS_DEPRECATED
120#else /* MBEDTLS_DEPRECATED_WARNING */
121#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
122#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
123#endif /* MBEDTLS_DEPRECATED_WARNING */
124#endif /* MBEDTLS_DEPRECATED_REMOVED */
125
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500126/**
127 * \brief Securely zeroize a buffer
128 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500129 * The function is meant to wipe the data contained in a buffer so
130 * that it can no longer be recovered even if the program memory
131 * is later compromised. Call this function on sensitive data
132 * stored on the stack before returning from a function, and on
133 * sensitive data stored on the heap before freeing the heap
134 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500135 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500136 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500137 * mbedtls_platform_zeroize() are not removed by aggressive
138 * compiler optimizations in a portable way. For this reason, Mbed
139 * TLS provides the configuration option
140 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
141 * mbedtls_platform_zeroize() to use a suitable implementation for
142 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500143 *
144 * \param buf Buffer to be zeroized
145 * \param len Length of the buffer in bytes
146 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500147 */
148void mbedtls_platform_zeroize( void *buf, size_t len );
149
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100150#if defined(MBEDTLS_HAVE_TIME_DATE)
151/**
Hanno Beckerc52ef402018-09-05 16:28:59 +0100152 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100153 *
Hanno Beckerc52ef402018-09-05 16:28:59 +0100154 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +0100155 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100156 *
Hanno Becker6a739782018-09-05 15:06:19 +0100157 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +0100158 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +0100159 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
160 * not possible, then gmtime() will be used. In this case, calls
161 * from the library to gmtime() will be guarded by the mutex
162 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
163 * enabled. It is recommended that calls from outside the library
164 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100165 *
Hanno Becker6a739782018-09-05 15:06:19 +0100166 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
167 * unconditionally use the alternative implementation for
168 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100169 *
Hanno Becker272675f2018-09-05 14:03:02 +0100170 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100171 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100172 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100173 *
174 * \return Pointer to an object of type struct tm on success, otherwise
175 * NULL
176 */
Hanno Becker6a739782018-09-05 15:06:19 +0100177struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
178 struct tm *tm_buf );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100179#endif /* MBEDTLS_HAVE_TIME_DATE */
180
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500181#ifdef __cplusplus
182}
183#endif
184
185#endif /* MBEDTLS_PLATFORM_UTIL_H */