Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 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 (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 Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
GuHaijun | 983acb7 | 2018-12-28 11:11:10 +0800 | [diff] [blame] | 29 | #include "config.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 30 | #else |
| 31 | #include MBEDTLS_CONFIG_FILE |
| 32 | #endif |
Arto Kinnunen | ee9bfca | 2019-09-06 16:59:00 +0300 | [diff] [blame] | 33 | #include <stdint.h> |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 34 | #include <stddef.h> |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 35 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
GuHaijun | 983acb7 | 2018-12-28 11:11:10 +0800 | [diff] [blame] | 36 | #include "platform_time.h" |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 37 | #include <time.h> |
| 38 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
Manuel Pégourié-Gonnard | 9b8ea89 | 2018-12-11 10:56:56 +0100 | [diff] [blame] | 44 | #if defined(MBEDTLS_CHECK_PARAMS) |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 30346f6 | 2019-06-13 16:44:19 +0200 | [diff] [blame] | 46 | #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 | |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 52 | #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 Peskine | 30346f6 | 2019-06-13 16:44:19 +0200 | [diff] [blame] | 59 | |
| 60 | #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT) |
| 61 | #define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) |
| 62 | #define MBEDTLS_PARAM_FAILED_ALT |
| 63 | |
Manuel Pégourié-Gonnard | 9b8ea89 | 2018-12-11 10:56:56 +0100 | [diff] [blame] | 64 | #else /* MBEDTLS_PARAM_FAILED */ |
Manuel Pégourié-Gonnard | 8e661bf | 2018-12-10 12:41:46 +0100 | [diff] [blame] | 65 | #define MBEDTLS_PARAM_FAILED( cond ) \ |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 66 | mbedtls_param_failed( #cond, __FILE__, __LINE__ ) |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * \brief User supplied callback function for parameter validation failure. |
Manuel Pégourié-Gonnard | 35acb09 | 2018-12-11 12:26:49 +0100 | [diff] [blame] | 70 | * See #MBEDTLS_CHECK_PARAMS for context. |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 71 | * |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 72 | * This function will be called unless an alternative treatement |
Manuel Pégourié-Gonnard | 35acb09 | 2018-12-11 12:26:49 +0100 | [diff] [blame] | 73 | * is defined through the #MBEDTLS_PARAM_FAILED macro. |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 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. |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 82 | */ |
Manuel Pégourié-Gonnard | ab58852 | 2018-12-10 16:04:46 +0100 | [diff] [blame] | 83 | void mbedtls_param_failed( const char *failure_condition, |
| 84 | const char *file, |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 85 | int line ); |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 86 | #endif /* MBEDTLS_PARAM_FAILED */ |
Manuel Pégourié-Gonnard | 0e9cddb | 2018-12-10 16:37:51 +0100 | [diff] [blame] | 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 | { \ |
Manuel Pégourié-Gonnard | 0e17cc9 | 2018-12-11 09:26:54 +0100 | [diff] [blame] | 93 | MBEDTLS_PARAM_FAILED( cond ); \ |
Manuel Pégourié-Gonnard | 0e9cddb | 2018-12-10 16:37:51 +0100 | [diff] [blame] | 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 | { \ |
Manuel Pégourié-Gonnard | 0e17cc9 | 2018-12-11 09:26:54 +0100 | [diff] [blame] | 103 | MBEDTLS_PARAM_FAILED( cond ); \ |
Manuel Pégourié-Gonnard | 0e9cddb | 2018-12-10 16:37:51 +0100 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 114 | #endif /* MBEDTLS_CHECK_PARAMS */ |
Simon Butcher | b486803 | 2018-12-06 17:36:34 +0000 | [diff] [blame] | 115 | |
Hanno Becker | 7bcf2b5 | 2019-07-26 09:02:40 +0100 | [diff] [blame] | 116 | #if defined(__GNUC__) || defined(__arm__) |
| 117 | #define MBEDTLS_ALWAYS_INLINE __attribute__((always_inline)) |
| 118 | #else |
| 119 | #define MBEDTLS_ALWAYS_INLINE |
| 120 | #endif |
| 121 | |
Hanno Becker | 6d0816a | 2018-12-17 11:30:27 +0000 | [diff] [blame] | 122 | /* Internal helper macros for deprecating API constants. */ |
| 123 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 124 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
Hanno Becker | 9dbefa1 | 2018-12-17 22:49:13 +0000 | [diff] [blame] | 125 | /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here |
| 126 | * to avoid conflict with other headers which define and use |
| 127 | * it, too. We might want to move all these definitions here at |
| 128 | * some point for uniformity. */ |
| 129 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 130 | MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; |
Hanno Becker | 6d0816a | 2018-12-17 11:30:27 +0000 | [diff] [blame] | 131 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ |
| 132 | ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) |
Hanno Becker | 9dbefa1 | 2018-12-17 22:49:13 +0000 | [diff] [blame] | 133 | MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; |
Hanno Becker | 6d0816a | 2018-12-17 11:30:27 +0000 | [diff] [blame] | 134 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ |
| 135 | ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) |
Hanno Becker | 9dbefa1 | 2018-12-17 22:49:13 +0000 | [diff] [blame] | 136 | #undef MBEDTLS_DEPRECATED |
Hanno Becker | 6d0816a | 2018-12-17 11:30:27 +0000 | [diff] [blame] | 137 | #else /* MBEDTLS_DEPRECATED_WARNING */ |
| 138 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL |
| 139 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL |
| 140 | #endif /* MBEDTLS_DEPRECATED_WARNING */ |
| 141 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 142 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 143 | /** |
| 144 | * \brief Securely zeroize a buffer |
| 145 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 146 | * The function is meant to wipe the data contained in a buffer so |
| 147 | * that it can no longer be recovered even if the program memory |
| 148 | * is later compromised. Call this function on sensitive data |
| 149 | * stored on the stack before returning from a function, and on |
| 150 | * sensitive data stored on the heap before freeing the heap |
| 151 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 152 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 153 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 154 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 155 | * compiler optimizations in a portable way. For this reason, Mbed |
| 156 | * TLS provides the configuration option |
| 157 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 158 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 159 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 160 | * |
| 161 | * \param buf Buffer to be zeroized |
| 162 | * \param len Length of the buffer in bytes |
| 163 | * |
Piotr Nowicki | ed840db | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 164 | * \return The value of \p buf if the operation was successful. |
| 165 | * \return NULL if a potential FI attack was detected or input parameters |
| 166 | * are not valid. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 167 | */ |
Piotr Nowicki | ed840db | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 168 | void *mbedtls_platform_zeroize( void *buf, size_t len ); |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 169 | |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 170 | /** |
| 171 | * \brief Secure memset |
| 172 | * |
Piotr Nowicki | 77b7a77 | 2020-07-31 16:11:06 +0200 | [diff] [blame] | 173 | * This is a constant-time version of memset(). The buffer is |
| 174 | * initialised with random data and the order is also randomised |
| 175 | * using the RNG in order to further harden against side-channel |
| 176 | * attacks. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 177 | * |
| 178 | * \param ptr Buffer to be set. |
| 179 | * \param value Value to be used when setting the buffer. |
| 180 | * \param num The length of the buffer in bytes. |
Jarno Lamsa | 0ff7109 | 2019-10-02 08:18:29 +0300 | [diff] [blame] | 181 | * |
Piotr Nowicki | ed840db | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 182 | * \return The value of \p ptr if the operation was successful. |
| 183 | * \return NULL if a potential FI attack was detected. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 184 | */ |
Manuel Pégourié-Gonnard | 51f65e4 | 2019-10-02 16:01:14 +0200 | [diff] [blame] | 185 | void *mbedtls_platform_memset( void *ptr, int value, size_t num ); |
Jarno Lamsa | 0736325 | 2019-09-27 16:20:11 +0300 | [diff] [blame] | 186 | |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 187 | /** |
| 188 | * \brief Secure memcpy |
| 189 | * |
Piotr Nowicki | 77b7a77 | 2020-07-31 16:11:06 +0200 | [diff] [blame] | 190 | * This is a constant-time version of memcpy(). The buffer is |
| 191 | * initialised with random data and the order is also randomised |
| 192 | * using the RNG in order to further harden against side-channel |
| 193 | * attacks. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 194 | * |
| 195 | * \param dst Destination buffer where the data is being copied to. |
| 196 | * \param src Source buffer where the data is being copied from. |
| 197 | * \param num The length of the buffers in bytes. |
| 198 | * |
Manuel Pégourié-Gonnard | 51f65e4 | 2019-10-02 16:01:14 +0200 | [diff] [blame] | 199 | * \return The value of \p dst. |
Piotr Nowicki | ea8e846 | 2020-08-10 15:20:26 +0200 | [diff] [blame] | 200 | * \return NULL if a potential FI attack was detected. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 201 | */ |
Manuel Pégourié-Gonnard | 51f65e4 | 2019-10-02 16:01:14 +0200 | [diff] [blame] | 202 | void *mbedtls_platform_memcpy( void *dst, const void *src, size_t num ); |
Jarno Lamsa | 0736325 | 2019-09-27 16:20:11 +0300 | [diff] [blame] | 203 | |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 204 | /** |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 205 | * \brief Secure memmove |
| 206 | * |
| 207 | * This is a constant-time version of memmove(). It is based on |
| 208 | * the double use of the mbedtls_platform_memcpy() function secured |
| 209 | * against side-channel attacks. |
| 210 | * |
| 211 | * \param dst Destination buffer where the data is being moved to. |
| 212 | * \param src Source buffer where the data is being moved from. |
| 213 | * \param num The length of the buffers in bytes. |
| 214 | * |
Piotr Nowicki | ce0aab4 | 2020-06-08 14:08:49 +0200 | [diff] [blame] | 215 | * \return 0 if the operation was successful |
| 216 | * \return #MBEDTLS_ERR_PLATFORM_ALLOC_FAILED if a memory allocation failed |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 217 | */ |
| 218 | int mbedtls_platform_memmove( void *dst, const void *src, size_t num ); |
| 219 | |
Andrzej Kurek | 8bb0839 | 2020-08-09 02:11:19 -0400 | [diff] [blame] | 220 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 221 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 222 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 223 | #else |
| 224 | #define MBEDTLS_DEPRECATED |
| 225 | #endif |
| 226 | |
| 227 | /** |
| 228 | * \brief Secure memcmp |
| 229 | * |
| 230 | * This is a constant-time version of memcmp(), but without checking |
| 231 | * if the bytes are greater or lower. The order is also randomised |
| 232 | * using the RNG in order to further harden against side-channel attacks. |
| 233 | * |
| 234 | * \param buf1 First buffer to compare. |
| 235 | * \param buf2 Second buffer to compare against. |
| 236 | * \param num The length of the buffers in bytes. |
| 237 | * |
| 238 | * \deprecated Superseded by mbedtls_platform_memequal(), and is only an alias to it. |
| 239 | * |
| 240 | * \return 0 if the buffers were equal or an unspecified non-zero value |
| 241 | * otherwise. |
| 242 | */ |
| 243 | int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num ); |
| 244 | |
| 245 | #endif |
Piotr Nowicki | 5d5841f | 2020-06-05 16:33:24 +0200 | [diff] [blame] | 246 | /** |
Piotr Nowicki | e3c4ee5 | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 247 | * \brief Secure check if the buffers have the same data. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 248 | * |
Piotr Nowicki | e3c4ee5 | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 249 | * This is a constant-time version of memcmp(), but without checking |
Piotr Nowicki | 77b7a77 | 2020-07-31 16:11:06 +0200 | [diff] [blame] | 250 | * if the bytes are greater or lower. The order is also randomised |
| 251 | * using the RNG in order to further harden against side-channel attacks. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 252 | * |
| 253 | * \param buf1 First buffer to compare. |
| 254 | * \param buf2 Second buffer to compare against. |
| 255 | * \param num The length of the buffers in bytes. |
| 256 | * |
Jarno Lamsa | c4315e6 | 2019-10-04 15:42:39 +0300 | [diff] [blame] | 257 | * \return 0 if the buffers were equal or an unspecified non-zero value |
| 258 | * otherwise. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 259 | */ |
Piotr Nowicki | e3c4ee5 | 2020-06-23 12:59:56 +0200 | [diff] [blame] | 260 | int mbedtls_platform_memequal( const void *buf1, const void *buf2, size_t num ); |
Jarno Lamsa | 0736325 | 2019-09-27 16:20:11 +0300 | [diff] [blame] | 261 | |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 262 | /** |
Andrzej Kurek | 189ee74 | 2020-06-24 17:28:31 -0400 | [diff] [blame] | 263 | * \brief RNG-function for getting a random 32-bit integer. |
| 264 | * |
Andrzej Kurek | 189ee74 | 2020-06-24 17:28:31 -0400 | [diff] [blame] | 265 | * \return The generated random number. |
| 266 | */ |
| 267 | uint32_t mbedtls_platform_random_uint32( void ); |
| 268 | |
| 269 | /** |
Jarno Lamsa | 39a9d40 | 2019-10-03 13:36:06 +0300 | [diff] [blame] | 270 | * \brief RNG-function for getting a random in given range. |
Jarno Lamsa | 7d28155 | 2019-10-01 15:56:17 +0300 | [diff] [blame] | 271 | * |
| 272 | * This function is meant to provide a global RNG to be used |
| 273 | * throughout Mbed TLS for hardening the library. It is used |
| 274 | * for generating a random delay, random data or random offset |
| 275 | * for utility functions. It is not meant to be a |
| 276 | * cryptographically secure RNG, but provide an RNG for utility |
| 277 | * functions. |
| 278 | * |
Jarno Lamsa | f5ebe2a | 2019-10-02 08:23:11 +0300 | [diff] [blame] | 279 | * \param num Max-value for the generated random number, exclusive. |
Andrzej Kurek | a138c0a | 2020-11-02 15:53:39 +0100 | [diff] [blame^] | 280 | * Must be greater than zero, otherwise an undefined behavior |
| 281 | * will occur on "num % 0". |
Jarno Lamsa | f5ebe2a | 2019-10-02 08:23:11 +0300 | [diff] [blame] | 282 | * The generated number will be on range [0, num). |
Jarno Lamsa | 77a0e07 | 2019-10-02 08:39:32 +0300 | [diff] [blame] | 283 | * |
Jarno Lamsa | f5ebe2a | 2019-10-02 08:23:11 +0300 | [diff] [blame] | 284 | * \return The generated random number. |
Jarno Lamsa | a1e5054 | 2019-10-02 12:44:36 +0300 | [diff] [blame] | 285 | */ |
Piotr Nowicki | 8656fc6 | 2020-06-23 12:30:40 +0200 | [diff] [blame] | 286 | uint32_t mbedtls_platform_random_in_range( uint32_t num ); |
Jarno Lamsa | 0736325 | 2019-09-27 16:20:11 +0300 | [diff] [blame] | 287 | |
Manuel Pégourié-Gonnard | 72a8c9e | 2019-11-08 10:21:00 +0100 | [diff] [blame] | 288 | /** |
Arto Kinnunen | 4c63b98 | 2019-12-02 15:01:41 +0200 | [diff] [blame] | 289 | * \brief Random delay function. |
| 290 | * |
Arto Kinnunen | 9a506e7 | 2019-12-09 10:54:03 +0200 | [diff] [blame] | 291 | * Function implements a random delay by incrementing a local |
| 292 | * variable randomized number of times (busy-looping). |
| 293 | * |
| 294 | * Duration of the delay is random as number of variable increments |
| 295 | * is randomized. |
Piotr Nowicki | 057daa3 | 2020-08-03 13:08:33 +0200 | [diff] [blame] | 296 | * |
| 297 | * \note This function works only if the MBEDTLS_FI_COUNTERMEASURES flag |
| 298 | * is defined in the configuration. Otherwise, the function does |
| 299 | * nothing. |
Arto Kinnunen | 4c63b98 | 2019-12-02 15:01:41 +0200 | [diff] [blame] | 300 | */ |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 301 | void mbedtls_platform_random_delay( void ); |
Arto Kinnunen | 4c63b98 | 2019-12-02 15:01:41 +0200 | [diff] [blame] | 302 | |
Shelly Liberman | 05beb9a | 2020-09-13 15:23:56 +0300 | [diff] [blame] | 303 | /** |
| 304 | * \brief RNG-function for getting a random buffer. |
| 305 | * |
| 306 | * \param buf Buffer for random data |
| 307 | * \param len Length of the buffer in bytes |
| 308 | * |
| 309 | */ |
| 310 | void mbedtls_platform_random_buf( uint8_t *buf, size_t len); |
| 311 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 312 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 313 | /** |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 314 | * \brief Platform-specific implementation of gmtime_r() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 315 | * |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 316 | * The function is a thread-safe abstraction that behaves |
Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 317 | * similarly to the gmtime_r() function from Unix/POSIX. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 318 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 319 | * Mbed TLS will try to identify the underlying platform and |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 320 | * make use of an appropriate underlying implementation (e.g. |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 321 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 322 | * not possible, then gmtime() will be used. In this case, calls |
| 323 | * from the library to gmtime() will be guarded by the mutex |
| 324 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 325 | * enabled. It is recommended that calls from outside the library |
| 326 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 327 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 328 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 329 | * unconditionally use the alternative implementation for |
| 330 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 331 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 332 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 333 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 334 | * \param tm_buf Pointer to an object where the results will be stored |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 335 | * |
| 336 | * \return Pointer to an object of type struct tm on success, otherwise |
| 337 | * NULL |
| 338 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 339 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 340 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 341 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 342 | |
Andrzej Kurek | a793237 | 2020-09-19 07:56:06 +0200 | [diff] [blame] | 343 | #if defined(MBEDTLS_VALIDATE_AES_KEYS_INTEGRITY) || defined(MBEDTLS_VALIDATE_SSL_KEYS_INTEGRITY) |
| 344 | /** |
| 345 | * \brief Calculate a hash from the given data. |
| 346 | * |
| 347 | * \param data Data from which the hash is calculated. |
| 348 | * \param data_len_bytes Length of the data in bytes. |
| 349 | * |
| 350 | * \return A hash calculated from the provided data. |
| 351 | */ |
| 352 | uint32_t mbedtls_hash( const void *data, size_t data_len_bytes ); |
| 353 | #endif |
| 354 | |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 355 | /** |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 356 | * \brief Convert a 32-bit number to the big endian format and write it to |
| 357 | * the given buffer. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 358 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 359 | * \param buf Address where the converted number is written. |
| 360 | * \param num A number that needs to be converted to the big endian format. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 361 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 362 | * \return Address to the end of buffer where the converted number is |
| 363 | * written. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 364 | */ |
Arto Kinnunen | ee9bfca | 2019-09-06 16:59:00 +0300 | [diff] [blame] | 365 | unsigned char* mbedtls_platform_put_uint32_be( unsigned char *buf, |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 366 | size_t num ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 367 | |
| 368 | /** |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 369 | * \brief Convert a 24-bit number to the big endian format and write it to |
| 370 | * the given buffer. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 371 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 372 | * \param buf Address where the converted number is written. |
| 373 | * \param num A number that needs to be converted to the big endian format. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 374 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 375 | * \return Address to the end of buffer where the converted number is |
| 376 | * written. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 377 | */ |
Arto Kinnunen | ee9bfca | 2019-09-06 16:59:00 +0300 | [diff] [blame] | 378 | unsigned char* mbedtls_platform_put_uint24_be( unsigned char *buf, |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 379 | size_t num ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 380 | |
| 381 | /** |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 382 | * \brief Convert a 16-bit number to the big endian format and write it to |
| 383 | * the given buffer. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 384 | * |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 385 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 386 | * \param buf Address where the converted number is written. |
| 387 | * \param num A number that needs to be converted to the big endian format. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 388 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 389 | * \return Address to the end of buffer where the converted number is |
| 390 | * written. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 391 | */ |
Arto Kinnunen | ee9bfca | 2019-09-06 16:59:00 +0300 | [diff] [blame] | 392 | unsigned char* mbedtls_platform_put_uint16_be( unsigned char *buf, |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 393 | size_t num ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 394 | |
| 395 | /** |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 396 | * \brief Convert a 32-bit number from the big endian format. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 397 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 398 | * The function reads a 32-bit number from the given buffer in the |
| 399 | * big endian format and returns it to the caller. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 400 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 401 | * \param buf Buffer where the 32-bit number locates. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 402 | * |
| 403 | * \return Converted number. |
| 404 | */ |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 405 | size_t mbedtls_platform_get_uint32_be( const unsigned char *buf ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 406 | |
| 407 | /** |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 408 | * \brief Convert a 24-bit number from the big endian format. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 409 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 410 | * The function reads a 14-bit number from the given buffer in the |
| 411 | * big endian format and returns it to the caller. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 412 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 413 | * \param buf Buffer where the 24-bit number locates. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 414 | * |
| 415 | * \return Converted number. |
| 416 | */ |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 417 | size_t mbedtls_platform_get_uint24_be( const unsigned char *buf ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 418 | |
| 419 | /** |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 420 | * \brief Convert a 16-bit number from the big endian format. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 421 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 422 | * The function reads a 16-bit number from the given buffer in the |
| 423 | * big endian format and returns it to the caller. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 424 | * |
Arto Kinnunen | 9b3b194 | 2019-09-09 17:02:35 +0300 | [diff] [blame] | 425 | * \param buf Buffer where the 16-bit number locates. |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 426 | * |
| 427 | * \return Converted number. |
| 428 | */ |
Arto Kinnunen | 4f4849a | 2019-09-09 10:21:18 +0300 | [diff] [blame] | 429 | size_t mbedtls_platform_get_uint16_be( const unsigned char *buf ); |
Arto Kinnunen | 0b62ce8 | 2019-09-04 14:04:57 +0300 | [diff] [blame] | 430 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 431 | #ifdef __cplusplus |
| 432 | } |
| 433 | #endif |
| 434 | |
| 435 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |