Mbed OS Reference
Loading...
Searching...
No Matches
platform_util.h
Go to the documentation of this file.
1/**
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 The Mbed TLS Contributors
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#ifndef MBEDTLS_PLATFORM_UTIL_H
24#define MBEDTLS_PLATFORM_UTIL_H
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "mbedtls/config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
32/**
33 * \ingroup mbedtls_platform_module
34 * \{
35 */
36
37#include <stddef.h>
38#if defined(MBEDTLS_HAVE_TIME_DATE)
40#include <time.h>
41#endif /* MBEDTLS_HAVE_TIME_DATE */
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47#if defined(MBEDTLS_CHECK_PARAMS)
48
49#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
50/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
51 * (which is what our config.h suggests). */
52#include <assert.h>
53#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
54
55#if defined(MBEDTLS_PARAM_FAILED)
56/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
57 *
58 * This flag can be used to check whether it is safe to assume that
59 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
60 */
61#define MBEDTLS_PARAM_FAILED_ALT
62
63#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
64#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
65#define MBEDTLS_PARAM_FAILED_ALT
66
67#else /* MBEDTLS_PARAM_FAILED */
68#define MBEDTLS_PARAM_FAILED( cond ) \
69 mbedtls_param_failed( #cond, __FILE__, __LINE__ )
70
71/**
72 * \brief User supplied callback function for parameter validation failure.
73 * See #MBEDTLS_CHECK_PARAMS for context.
74 *
75 * This function will be called unless an alternative treatement
76 * is defined through the #MBEDTLS_PARAM_FAILED macro.
77 *
78 * This function can return, and the operation will be aborted, or
79 * alternatively, through use of setjmp()/longjmp() can resume
80 * execution in the application code.
81 *
82 * \param failure_condition The assertion that didn't hold.
83 * \param file The file where the assertion failed.
84 * \param line The line in the file where the assertion failed.
85 */
86void mbedtls_param_failed( const char *failure_condition,
87 const char *file,
88 int line );
89#endif /* MBEDTLS_PARAM_FAILED */
90
91/* Internal macro meant to be called only from within the library. */
92#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
93 do { \
94 if( !(cond) ) \
95 { \
96 MBEDTLS_PARAM_FAILED( cond ); \
97 return( ret ); \
98 } \
99 } while( 0 )
100
101/* Internal macro meant to be called only from within the library. */
102#define MBEDTLS_INTERNAL_VALIDATE( cond ) \
103 do { \
104 if( !(cond) ) \
105 { \
106 MBEDTLS_PARAM_FAILED( cond ); \
107 return; \
108 } \
109 } while( 0 )
110
111#else /* MBEDTLS_CHECK_PARAMS */
112
113/* Internal macros meant to be called only from within the library. */
114#define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
115#define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
116
117#endif /* MBEDTLS_CHECK_PARAMS */
118
119/* Internal helper macros for deprecating API constants. */
120#if !defined(MBEDTLS_DEPRECATED_REMOVED)
121#if defined(MBEDTLS_DEPRECATED_WARNING)
122/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
123 * to avoid conflict with other headers which define and use
124 * it, too. We might want to move all these definitions here at
125 * some point for uniformity. */
126#define MBEDTLS_DEPRECATED __attribute__((deprecated))
127MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
128#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
129 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
130MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
131#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
132 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
133#undef MBEDTLS_DEPRECATED
134#else /* MBEDTLS_DEPRECATED_WARNING */
135#define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
136#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
137#endif /* MBEDTLS_DEPRECATED_WARNING */
138#endif /* MBEDTLS_DEPRECATED_REMOVED */
139
140/**
141 * \brief Securely zeroize a buffer
142 *
143 * The function is meant to wipe the data contained in a buffer so
144 * that it can no longer be recovered even if the program memory
145 * is later compromised. Call this function on sensitive data
146 * stored on the stack before returning from a function, and on
147 * sensitive data stored on the heap before freeing the heap
148 * object.
149 *
150 * It is extremely difficult to guarantee that calls to
151 * mbedtls_platform_zeroize() are not removed by aggressive
152 * compiler optimizations in a portable way. For this reason, Mbed
153 * TLS provides the configuration option
154 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
155 * mbedtls_platform_zeroize() to use a suitable implementation for
156 * their platform and needs
157 *
158 * \param buf Buffer to be zeroized
159 * \param len Length of the buffer in bytes
160 *
161 */
162void mbedtls_platform_zeroize( void *buf, size_t len );
163
164#if defined(MBEDTLS_HAVE_TIME_DATE)
165/**
166 * \brief Platform-specific implementation of gmtime_r()
167 *
168 * The function is a thread-safe abstraction that behaves
169 * similarly to the gmtime_r() function from Unix/POSIX.
170 *
171 * Mbed TLS will try to identify the underlying platform and
172 * make use of an appropriate underlying implementation (e.g.
173 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
174 * not possible, then gmtime() will be used. In this case, calls
175 * from the library to gmtime() will be guarded by the mutex
176 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
177 * enabled. It is recommended that calls from outside the library
178 * are also guarded by this mutex.
179 *
180 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
181 * unconditionally use the alternative implementation for
182 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
183 *
184 * \param tt Pointer to an object containing time (in seconds) since the
185 * epoch to be converted
186 * \param tm_buf Pointer to an object where the results will be stored
187 *
188 * \return Pointer to an object of type struct tm on success, otherwise
189 * NULL
190 */
191struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
192 struct tm *tm_buf );
193#endif /* MBEDTLS_HAVE_TIME_DATE */
194
195#ifdef __cplusplus
196}
197#endif
198
199/// \}
200
201#endif /* MBEDTLS_PLATFORM_UTIL_H */
Configuration options (set of defines)
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
mbed TLS Platform time abstraction