Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:33

0001 /**
0002  * \file platform_util.h
0003  *
0004  * \brief Common and shared functions used by multiple modules in the Mbed TLS
0005  *        library.
0006  */
0007 /*
0008  *  Copyright The Mbed TLS Contributors
0009  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0010  */
0011 #ifndef MBEDTLS_PLATFORM_UTIL_H
0012 #define MBEDTLS_PLATFORM_UTIL_H
0013 
0014 #include "mbedtls/build_info.h"
0015 
0016 #include <stddef.h>
0017 #if defined(MBEDTLS_HAVE_TIME_DATE)
0018 #include "mbedtls/platform_time.h"
0019 #include <time.h>
0020 #endif /* MBEDTLS_HAVE_TIME_DATE */
0021 
0022 #ifdef __cplusplus
0023 extern "C" {
0024 #endif
0025 
0026 /* Internal helper macros for deprecating API constants. */
0027 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
0028 #if defined(MBEDTLS_DEPRECATED_WARNING)
0029 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
0030 MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t;
0031 #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL)       \
0032     ((mbedtls_deprecated_string_constant_t) (VAL))
0033 MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
0034 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL)       \
0035     ((mbedtls_deprecated_numeric_constant_t) (VAL))
0036 #else /* MBEDTLS_DEPRECATED_WARNING */
0037 #define MBEDTLS_DEPRECATED
0038 #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
0039 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
0040 #endif /* MBEDTLS_DEPRECATED_WARNING */
0041 #endif /* MBEDTLS_DEPRECATED_REMOVED */
0042 
0043 /* Implementation of the check-return facility.
0044  * See the user documentation in mbedtls_config.h.
0045  *
0046  * Do not use this macro directly to annotate function: instead,
0047  * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL
0048  * depending on how important it is to check the return value.
0049  */
0050 #if !defined(MBEDTLS_CHECK_RETURN)
0051 #if defined(__GNUC__)
0052 #define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
0053 #elif defined(_MSC_VER) && _MSC_VER >= 1700
0054 #include <sal.h>
0055 #define MBEDTLS_CHECK_RETURN _Check_return_
0056 #else
0057 #define MBEDTLS_CHECK_RETURN
0058 #endif
0059 #endif
0060 
0061 /** Critical-failure function
0062  *
0063  * This macro appearing at the beginning of the declaration of a function
0064  * indicates that its return value should be checked in all applications.
0065  * Omitting the check is very likely to indicate a bug in the application
0066  * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN
0067  * is implemented for the compiler in use.
0068  *
0069  * \note  The use of this macro is a work in progress.
0070  *        This macro may be added to more functions in the future.
0071  *        Such an extension is not considered an API break, provided that
0072  *        there are near-unavoidable circumstances under which the function
0073  *        can fail. For example, signature/MAC/AEAD verification functions,
0074  *        and functions that require a random generator, are considered
0075  *        return-check-critical.
0076  */
0077 #define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
0078 
0079 /** Ordinary-failure function
0080  *
0081  * This macro appearing at the beginning of the declaration of a function
0082  * indicates that its return value should be generally be checked in portable
0083  * applications. Omitting the check will result in a compile-time warning if
0084  * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use and
0085  * #MBEDTLS_CHECK_RETURN_WARNING is enabled in the compile-time configuration.
0086  *
0087  * You can use #MBEDTLS_IGNORE_RETURN to explicitly ignore the return value
0088  * of a function that is annotated with #MBEDTLS_CHECK_RETURN.
0089  *
0090  * \note  The use of this macro is a work in progress.
0091  *        This macro will be added to more functions in the future.
0092  *        Eventually this should appear before most functions returning
0093  *        an error code (as \c int in the \c mbedtls_xxx API or
0094  *        as ::psa_status_t in the \c psa_xxx API).
0095  */
0096 #if defined(MBEDTLS_CHECK_RETURN_WARNING)
0097 #define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
0098 #else
0099 #define MBEDTLS_CHECK_RETURN_TYPICAL
0100 #endif
0101 
0102 /** Benign-failure function
0103  *
0104  * This macro appearing at the beginning of the declaration of a function
0105  * indicates that it is rarely useful to check its return value.
0106  *
0107  * This macro has an empty expansion. It exists for documentation purposes:
0108  * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function
0109  * has been analyzed for return-check usefulness, whereas the lack of
0110  * an annotation indicates that the function has not been analyzed and its
0111  * return-check usefulness is unknown.
0112  */
0113 #define MBEDTLS_CHECK_RETURN_OPTIONAL
0114 
0115 /** \def MBEDTLS_IGNORE_RETURN
0116  *
0117  * Call this macro with one argument, a function call, to suppress a warning
0118  * from #MBEDTLS_CHECK_RETURN due to that function call.
0119  */
0120 #if !defined(MBEDTLS_IGNORE_RETURN)
0121 /* GCC doesn't silence the warning with just (void)(result).
0122  * (void)!(result) is known to work up at least up to GCC 10, as well
0123  * as with Clang and MSVC.
0124  *
0125  * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html
0126  * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result
0127  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
0128  */
0129 #define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
0130 #endif
0131 
0132 /* If the following macro is defined, the library is being built by the test
0133  * framework, and the framework is going to provide a replacement
0134  * mbedtls_platform_zeroize() using a preprocessor macro, so the function
0135  * declaration should be omitted.  */
0136 #if !defined(MBEDTLS_TEST_DEFINES_ZEROIZE) //no-check-names
0137 /**
0138  * \brief       Securely zeroize a buffer
0139  *
0140  *              The function is meant to wipe the data contained in a buffer so
0141  *              that it can no longer be recovered even if the program memory
0142  *              is later compromised. Call this function on sensitive data
0143  *              stored on the stack before returning from a function, and on
0144  *              sensitive data stored on the heap before freeing the heap
0145  *              object.
0146  *
0147  *              It is extremely difficult to guarantee that calls to
0148  *              mbedtls_platform_zeroize() are not removed by aggressive
0149  *              compiler optimizations in a portable way. For this reason, Mbed
0150  *              TLS provides the configuration option
0151  *              MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
0152  *              mbedtls_platform_zeroize() to use a suitable implementation for
0153  *              their platform and needs
0154  *
0155  * \param buf   Buffer to be zeroized
0156  * \param len   Length of the buffer in bytes
0157  *
0158  */
0159 void mbedtls_platform_zeroize(void *buf, size_t len);
0160 #endif
0161 
0162 #if defined(MBEDTLS_HAVE_TIME_DATE)
0163 /**
0164  * \brief      Platform-specific implementation of gmtime_r()
0165  *
0166  *             The function is a thread-safe abstraction that behaves
0167  *             similarly to the gmtime_r() function from Unix/POSIX.
0168  *
0169  *             Mbed TLS will try to identify the underlying platform and
0170  *             make use of an appropriate underlying implementation (e.g.
0171  *             gmtime_r() for POSIX and gmtime_s() for Windows). If this is
0172  *             not possible, then gmtime() will be used. In this case, calls
0173  *             from the library to gmtime() will be guarded by the mutex
0174  *             mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
0175  *             enabled. It is recommended that calls from outside the library
0176  *             are also guarded by this mutex.
0177  *
0178  *             If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
0179  *             unconditionally use the alternative implementation for
0180  *             mbedtls_platform_gmtime_r() supplied by the user at compile time.
0181  *
0182  * \param tt     Pointer to an object containing time (in seconds) since the
0183  *               epoch to be converted
0184  * \param tm_buf Pointer to an object where the results will be stored
0185  *
0186  * \return      Pointer to an object of type struct tm on success, otherwise
0187  *              NULL
0188  */
0189 struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
0190                                      struct tm *tm_buf);
0191 #endif /* MBEDTLS_HAVE_TIME_DATE */
0192 
0193 #ifdef __cplusplus
0194 }
0195 #endif
0196 
0197 #endif /* MBEDTLS_PLATFORM_UTIL_H */