blob: f10574afe6f8d4a77f956194d6e9521509760ffe [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
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 * **********
49 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050050 * This file is part of Mbed TLS (https://tls.mbed.org)
51 */
52#ifndef MBEDTLS_PLATFORM_UTIL_H
53#define MBEDTLS_PLATFORM_UTIL_H
54
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010055#if !defined(MBEDTLS_CONFIG_FILE)
GuHaijun983acb72018-12-28 11:11:10 +080056#include "config.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010057#else
58#include MBEDTLS_CONFIG_FILE
59#endif
60
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050061#include <stddef.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010062#if defined(MBEDTLS_HAVE_TIME_DATE)
GuHaijun983acb72018-12-28 11:11:10 +080063#include "platform_time.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010064#include <time.h>
65#endif /* MBEDTLS_HAVE_TIME_DATE */
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050066
67#ifdef __cplusplus
68extern "C" {
69#endif
70
Manuel Pégourié-Gonnard9b8ea892018-12-11 10:56:56 +010071#if defined(MBEDTLS_CHECK_PARAMS)
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010072
Gilles Peskine30346f62019-06-13 16:44:19 +020073#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
74/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
75 * (which is what our config.h suggests). */
76#include <assert.h>
77#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
78
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +010079#if defined(MBEDTLS_PARAM_FAILED)
80/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
81 *
82 * This flag can be used to check whether it is safe to assume that
83 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
84 */
85#define MBEDTLS_PARAM_FAILED_ALT
Gilles Peskine30346f62019-06-13 16:44:19 +020086
87#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
88#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
89#define MBEDTLS_PARAM_FAILED_ALT
90
Manuel Pégourié-Gonnard9b8ea892018-12-11 10:56:56 +010091#else /* MBEDTLS_PARAM_FAILED */
Manuel Pégourié-Gonnard8e661bf2018-12-10 12:41:46 +010092#define MBEDTLS_PARAM_FAILED( cond ) \
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +010093 mbedtls_param_failed( #cond, __FILE__, __LINE__ )
Simon Butcherb4868032018-12-06 17:36:34 +000094
95/**
96 * \brief User supplied callback function for parameter validation failure.
Manuel Pégourié-Gonnard35acb092018-12-11 12:26:49 +010097 * See #MBEDTLS_CHECK_PARAMS for context.
Simon Butcherb4868032018-12-06 17:36:34 +000098 *
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +010099 * This function will be called unless an alternative treatement
Manuel Pégourié-Gonnard35acb092018-12-11 12:26:49 +0100100 * is defined through the #MBEDTLS_PARAM_FAILED macro.
Simon Butcherb4868032018-12-06 17:36:34 +0000101 *
102 * This function can return, and the operation will be aborted, or
103 * alternatively, through use of setjmp()/longjmp() can resume
104 * execution in the application code.
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +0100105 *
106 * \param failure_condition The assertion that didn't hold.
107 * \param file The file where the assertion failed.
108 * \param line The line in the file where the assertion failed.
Simon Butcherb4868032018-12-06 17:36:34 +0000109 */
Manuel Pégourié-Gonnardab588522018-12-10 16:04:46 +0100110void mbedtls_param_failed( const char *failure_condition,
111 const char *file,
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +0100112 int line );
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100113#endif /* MBEDTLS_PARAM_FAILED */
Manuel Pégourié-Gonnard0e9cddb2018-12-10 16:37:51 +0100114
115/* Internal macro meant to be called only from within the library. */
116#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
117 do { \
118 if( !(cond) ) \
119 { \
Manuel Pégourié-Gonnard0e17cc92018-12-11 09:26:54 +0100120 MBEDTLS_PARAM_FAILED( cond ); \
Manuel Pégourié-Gonnard0e9cddb2018-12-10 16:37:51 +0100121 return( ret ); \
122 } \
123 } while( 0 )
124
125/* Internal macro meant to be called only from within the library. */
126#define MBEDTLS_INTERNAL_VALIDATE( cond ) \
127 do { \
128 if( !(cond) ) \
129 { \
Manuel Pégourié-Gonnard0e17cc92018-12-11 09:26:54 +0100130 MBEDTLS_PARAM_FAILED( cond ); \
Manuel Pégourié-Gonnard0e9cddb2018-12-10 16:37:51 +0100131 return; \
132 } \
133 } while( 0 )
134
135#else /* MBEDTLS_CHECK_PARAMS */
136
137/* Internal macros meant to be called only from within the library. */
138#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
139#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
140
Manuel Pégourié-Gonnarda2b0e272018-12-10 15:23:58 +0100141#endif /* MBEDTLS_CHECK_PARAMS */
Simon Butcherb4868032018-12-06 17:36:34 +0000142
Hanno Becker6d0816a2018-12-17 11:30:27 +0000143/* Internal helper macros for deprecating API constants. */
144#if !defined(MBEDTLS_DEPRECATED_REMOVED)
145#if defined(MBEDTLS_DEPRECATED_WARNING)
Hanno Becker9dbefa12018-12-17 22:49:13 +0000146/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
147 * to avoid conflict with other headers which define and use
148 * it, too. We might want to move all these definitions here at
149 * some point for uniformity. */
150#define MBEDTLS_DEPRECATED __attribute__((deprecated))
151MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
Hanno Becker6d0816a2018-12-17 11:30:27 +0000152#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
153 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
Hanno Becker9dbefa12018-12-17 22:49:13 +0000154MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
Hanno Becker6d0816a2018-12-17 11:30:27 +0000155#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
156 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
Hanno Becker9dbefa12018-12-17 22:49:13 +0000157#undef MBEDTLS_DEPRECATED
Hanno Becker6d0816a2018-12-17 11:30:27 +0000158#else /* MBEDTLS_DEPRECATED_WARNING */
159#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
160#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
161#endif /* MBEDTLS_DEPRECATED_WARNING */
162#endif /* MBEDTLS_DEPRECATED_REMOVED */
163
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500164/**
165 * \brief Securely zeroize a buffer
166 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500167 * The function is meant to wipe the data contained in a buffer so
168 * that it can no longer be recovered even if the program memory
169 * is later compromised. Call this function on sensitive data
170 * stored on the stack before returning from a function, and on
171 * sensitive data stored on the heap before freeing the heap
172 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500173 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500174 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500175 * mbedtls_platform_zeroize() are not removed by aggressive
176 * compiler optimizations in a portable way. For this reason, Mbed
177 * TLS provides the configuration option
178 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
179 * mbedtls_platform_zeroize() to use a suitable implementation for
180 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500181 *
182 * \param buf Buffer to be zeroized
183 * \param len Length of the buffer in bytes
184 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500185 */
186void mbedtls_platform_zeroize( void *buf, size_t len );
187
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100188#if defined(MBEDTLS_HAVE_TIME_DATE)
189/**
Hanno Beckerc52ef402018-09-05 16:28:59 +0100190 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100191 *
Hanno Beckerc52ef402018-09-05 16:28:59 +0100192 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +0100193 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100194 *
Hanno Becker6a739782018-09-05 15:06:19 +0100195 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +0100196 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +0100197 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
198 * not possible, then gmtime() will be used. In this case, calls
199 * from the library to gmtime() will be guarded by the mutex
200 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
201 * enabled. It is recommended that calls from outside the library
202 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100203 *
Hanno Becker6a739782018-09-05 15:06:19 +0100204 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
205 * unconditionally use the alternative implementation for
206 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100207 *
Hanno Becker272675f2018-09-05 14:03:02 +0100208 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100209 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100210 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100211 *
212 * \return Pointer to an object of type struct tm on success, otherwise
213 * NULL
214 */
Hanno Becker6a739782018-09-05 15:06:19 +0100215struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
216 struct tm *tm_buf );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100217#endif /* MBEDTLS_HAVE_TIME_DATE */
218
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500219#ifdef __cplusplus
220}
221#endif
222
223#endif /* MBEDTLS_PLATFORM_UTIL_H */