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