blob: 426afaf040172a1bda707bdf560b205dd1bbc351 [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útia2947ac2020-08-19 16:37:36 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050049 */
50#ifndef MBEDTLS_PLATFORM_UTIL_H
51#define MBEDTLS_PLATFORM_UTIL_H
52
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010053#if !defined(MBEDTLS_CONFIG_FILE)
GuHaijun983acb72018-12-28 11:11:10 +080054#include "config.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010055#else
56#include MBEDTLS_CONFIG_FILE
57#endif
58
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050059#include <stddef.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010060#if defined(MBEDTLS_HAVE_TIME_DATE)
GuHaijun983acb72018-12-28 11:11:10 +080061#include "platform_time.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010062#include <time.h>
63#endif /* MBEDTLS_HAVE_TIME_DATE */
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050064
65#ifdef __cplusplus
66extern "C" {
67#endif
68
Manuel Pégourié-Gonnard9b8ea892018-12-11 10:56:56 +010069#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010070
Gilles Peskine30346f62019-06-13 16:44:19 +020071#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
72/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
73 * (which is what our config.h suggests). */
74#include <assert.h>
75#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
76
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010077#if defined(MBEDTLS_PARAM_FAILED)
78/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
79 *
80 * This flag can be used to check whether it is safe to assume that
81 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
82 */
83#define MBEDTLS_PARAM_FAILED_ALT
Gilles Peskine30346f62019-06-13 16:44:19 +020084
85#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
86#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
87#define MBEDTLS_PARAM_FAILED_ALT
88
Manuel Pégourié-Gonnard9b8ea892018-12-11 10:56:56 +010089#else /* MBEDTLS_PARAM_FAILED */
Manuel Pégourié-Gonnard8e661bf2018-12-10 12:41:46 +010090#define MBEDTLS_PARAM_FAILED( cond ) \
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +010091 mbedtls_param_failed( #cond, __FILE__, __LINE__ )
Simon Butcherb4868032018-12-06 17:36:34 +000092
93/**
94 * \brief User supplied callback function for parameter validation failure.
Manuel Pégourié-Gonnard35acb092018-12-11 12:26:49 +010095 * See #MBEDTLS_CHECK_PARAMS for context.
Simon Butcherb4868032018-12-06 17:36:34 +000096 *
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +010097 * This function will be called unless an alternative treatement
Manuel Pégourié-Gonnard35acb092018-12-11 12:26:49 +010098 * is defined through the #MBEDTLS_PARAM_FAILED macro.
Simon Butcherb4868032018-12-06 17:36:34 +000099 *
100 * This function can return, and the operation will be aborted, or
101 * alternatively, through use of setjmp()/longjmp() can resume
102 * execution in the application code.
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +0100103 *
104 * \param failure_condition The assertion that didn't hold.
105 * \param file The file where the assertion failed.
106 * \param line The line in the file where the assertion failed.
Simon Butcherb4868032018-12-06 17:36:34 +0000107 */
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +0100108void mbedtls_param_failed( const char *failure_condition,
109 const char *file,
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100110 int line );
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100111#endif /* MBEDTLS_PARAM_FAILED */
Manuel Pégourié-Gonnard0e9cddb2018-12-10 16:37:51 +0100112
113/* Internal macro meant to be called only from within the library. */
114#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
115 do { \
116 if( !(cond) ) \
117 { \
Manuel Pégourié-Gonnard0e17cc92018-12-11 09:26:54 +0100118 MBEDTLS_PARAM_FAILED( cond ); \
Manuel Pégourié-Gonnard0e9cddb2018-12-10 16:37:51 +0100119 return( ret ); \
120 } \
121 } while( 0 )
122
123/* Internal macro meant to be called only from within the library. */
124#define MBEDTLS_INTERNAL_VALIDATE( cond ) \
125 do { \
126 if( !(cond) ) \
127 { \
Manuel Pégourié-Gonnard0e17cc92018-12-11 09:26:54 +0100128 MBEDTLS_PARAM_FAILED( cond ); \
Manuel Pégourié-Gonnard0e9cddb2018-12-10 16:37:51 +0100129 return; \
130 } \
131 } while( 0 )
132
133#else /* MBEDTLS_CHECK_PARAMS */
134
135/* Internal macros meant to be called only from within the library. */
136#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
137#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
138
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100139#endif /* MBEDTLS_CHECK_PARAMS */
Simon Butcherb4868032018-12-06 17:36:34 +0000140
Hanno Becker6d0816a2018-12-17 11:30:27 +0000141/* Internal helper macros for deprecating API constants. */
142#if !defined(MBEDTLS_DEPRECATED_REMOVED)
143#if defined(MBEDTLS_DEPRECATED_WARNING)
Hanno Becker9dbefa12018-12-17 22:49:13 +0000144/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
145 * to avoid conflict with other headers which define and use
146 * it, too. We might want to move all these definitions here at
147 * some point for uniformity. */
148#define MBEDTLS_DEPRECATED __attribute__((deprecated))
149MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
Hanno Becker6d0816a2018-12-17 11:30:27 +0000150#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
151 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
Hanno Becker9dbefa12018-12-17 22:49:13 +0000152MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
Hanno Becker6d0816a2018-12-17 11:30:27 +0000153#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
154 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
Hanno Becker9dbefa12018-12-17 22:49:13 +0000155#undef MBEDTLS_DEPRECATED
Hanno Becker6d0816a2018-12-17 11:30:27 +0000156#else /* MBEDTLS_DEPRECATED_WARNING */
157#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
158#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
159#endif /* MBEDTLS_DEPRECATED_WARNING */
160#endif /* MBEDTLS_DEPRECATED_REMOVED */
161
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500162/**
163 * \brief Securely zeroize a buffer
164 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500165 * The function is meant to wipe the data contained in a buffer so
166 * that it can no longer be recovered even if the program memory
167 * is later compromised. Call this function on sensitive data
168 * stored on the stack before returning from a function, and on
169 * sensitive data stored on the heap before freeing the heap
170 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500171 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500172 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500173 * mbedtls_platform_zeroize() are not removed by aggressive
174 * compiler optimizations in a portable way. For this reason, Mbed
175 * TLS provides the configuration option
176 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
177 * mbedtls_platform_zeroize() to use a suitable implementation for
178 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500179 *
180 * \param buf Buffer to be zeroized
181 * \param len Length of the buffer in bytes
182 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500183 */
184void mbedtls_platform_zeroize( void *buf, size_t len );
185
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100186#if defined(MBEDTLS_HAVE_TIME_DATE)
187/**
Hanno Beckerc52ef402018-09-05 16:28:59 +0100188 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100189 *
Hanno Beckerc52ef402018-09-05 16:28:59 +0100190 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +0100191 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100192 *
Hanno Becker6a739782018-09-05 15:06:19 +0100193 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +0100194 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +0100195 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
196 * not possible, then gmtime() will be used. In this case, calls
197 * from the library to gmtime() will be guarded by the mutex
198 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
199 * enabled. It is recommended that calls from outside the library
200 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100201 *
Hanno Becker6a739782018-09-05 15:06:19 +0100202 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
203 * unconditionally use the alternative implementation for
204 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100205 *
Hanno Becker272675f2018-09-05 14:03:02 +0100206 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100207 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100208 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100209 *
210 * \return Pointer to an object of type struct tm on success, otherwise
211 * NULL
212 */
Hanno Becker6a739782018-09-05 15:06:19 +0100213struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
214 struct tm *tm_buf );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100215#endif /* MBEDTLS_HAVE_TIME_DATE */
216
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500217#ifdef __cplusplus
218}
219#endif
220
221#endif /* MBEDTLS_PLATFORM_UTIL_H */