Mbed TLS v2.28.5
platform_util.h
Go to the documentation of this file.
1
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#include <stddef.h>
33#if defined(MBEDTLS_HAVE_TIME_DATE)
35#include <time.h>
36#endif /* MBEDTLS_HAVE_TIME_DATE */
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#if defined(MBEDTLS_CHECK_PARAMS)
43
44#if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
45/* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
46 * (which is what our config.h suggests). */
47#include <assert.h>
48#endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
49
50#if defined(MBEDTLS_PARAM_FAILED)
56#define MBEDTLS_PARAM_FAILED_ALT
57
58#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
59#define MBEDTLS_PARAM_FAILED(cond) assert(cond)
60#define MBEDTLS_PARAM_FAILED_ALT
61
62#else /* MBEDTLS_PARAM_FAILED */
63#define MBEDTLS_PARAM_FAILED(cond) \
64 mbedtls_param_failed( #cond, __FILE__, __LINE__)
65
81void mbedtls_param_failed(const char *failure_condition,
82 const char *file,
83 int line);
84#endif /* MBEDTLS_PARAM_FAILED */
85
86/* Internal macro meant to be called only from within the library. */
87#define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) \
88 do { \
89 if (!(cond)) \
90 { \
91 MBEDTLS_PARAM_FAILED(cond); \
92 return ret; \
93 } \
94 } while (0)
95
96/* Internal macro meant to be called only from within the library. */
97#define MBEDTLS_INTERNAL_VALIDATE(cond) \
98 do { \
99 if (!(cond)) \
100 { \
101 MBEDTLS_PARAM_FAILED(cond); \
102 return; \
103 } \
104 } while (0)
105
106#else /* MBEDTLS_CHECK_PARAMS */
107
108/* Internal macros meant to be called only from within the library. */
109#define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) do { } while (0)
110#define MBEDTLS_INTERNAL_VALIDATE(cond) do { } while (0)
111
112#endif /* MBEDTLS_CHECK_PARAMS */
113
114/* Internal helper macros for deprecating API constants. */
115#if !defined(MBEDTLS_DEPRECATED_REMOVED)
116#if defined(MBEDTLS_DEPRECATED_WARNING)
117/* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
118 * to avoid conflict with other headers which define and use
119 * it, too. We might want to move all these definitions here at
120 * some point for uniformity. */
121#define MBEDTLS_DEPRECATED __attribute__((deprecated))
123#define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) \
124 ((mbedtls_deprecated_string_constant_t) (VAL))
126#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) \
127 ((mbedtls_deprecated_numeric_constant_t) (VAL))
128#undef MBEDTLS_DEPRECATED
129#else /* MBEDTLS_DEPRECATED_WARNING */
130#define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
131#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
132#endif /* MBEDTLS_DEPRECATED_WARNING */
133#endif /* MBEDTLS_DEPRECATED_REMOVED */
134
135/* Implementation of the check-return facility.
136 * See the user documentation in config.h.
137 *
138 * Do not use this macro directly to annotate function: instead,
139 * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL
140 * depending on how important it is to check the return value.
141 */
142#if !defined(MBEDTLS_CHECK_RETURN)
143#if defined(__GNUC__)
144#define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
145#elif defined(_MSC_VER) && _MSC_VER >= 1700
146#include <sal.h>
147#define MBEDTLS_CHECK_RETURN _Check_return_
148#else
149#define MBEDTLS_CHECK_RETURN
150#endif
151#endif
152
169#define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
170
188#if defined(MBEDTLS_CHECK_RETURN_WARNING)
189#define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
190#else
191#define MBEDTLS_CHECK_RETURN_TYPICAL
192#endif
193
205#define MBEDTLS_CHECK_RETURN_OPTIONAL
206
212#if !defined(MBEDTLS_IGNORE_RETURN)
213/* GCC doesn't silence the warning with just (void)(result).
214 * (void)!(result) is known to work up at least up to GCC 10, as well
215 * as with Clang and MSVC.
216 *
217 * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html
218 * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result
219 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
220 */
221#define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
222#endif
223
224/* If the following macro is defined, the library is being built by the test
225 * framework, and the framework is going to provide a replacement
226 * mbedtls_platform_zeroize() using a preprocessor macro, so the function
227 * declaration should be omitted. */
228#if !defined(MBEDTLS_TEST_DEFINES_ZEROIZE) //no-check-names
251void mbedtls_platform_zeroize(void *buf, size_t len);
252#endif
253
254#if defined(MBEDTLS_HAVE_TIME_DATE)
282 struct tm *tm_buf);
283#endif /* MBEDTLS_HAVE_TIME_DATE */
284
285#ifdef __cplusplus
286}
287#endif
288
289#endif /* MBEDTLS_PLATFORM_UTIL_H */
Configuration options (set of defines)
Mbed TLS Platform time abstraction.
time_t mbedtls_time_t
Definition: platform_time.h:43
MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t
MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t
#define MBEDTLS_DEPRECATED
struct tm * mbedtls_platform_gmtime_r(const mbedtls_time_t *tt, struct tm *tm_buf)
Platform-specific implementation of gmtime_r()