blob: 88987d80708d4eb0f0f4c5378cfafc69dd747fe9 [file] [log] [blame]
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01001/*
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -05002 * Common and shared functions used by multiple modules in the Mbed TLS
3 * library.
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01004 *
Andres Amaya Garcia757cd722018-03-08 21:25:25 +00005 * Copyright (C) 2018, Arm Limited, All Rights Reserved
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
Andres Amaya Garcia757cd722018-03-08 21:25:25 +000020 * This file is part of Mbed TLS (https://tls.mbed.org)
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010021 */
22
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010023/*
Hanno Becker48a816f2018-09-05 15:22:22 +010024 * Ensure gmtime_r is available even with -std=c99; must be defined before
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010025 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
26 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010027#if !defined(_POSIX_C_SOURCE)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010028#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010029#endif
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010030
Andres Amaya Garciab1262a32017-10-25 09:51:14 +010031#if !defined(MBEDTLS_CONFIG_FILE)
32#include "mbedtls/config.h"
33#else
34#include MBEDTLS_CONFIG_FILE
35#endif
36
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050037#include "mbedtls/platform_util.h"
Simon Butcherb4868032018-12-06 17:36:34 +000038#include "mbedtls/platform.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010039#include "mbedtls/threading.h"
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010040
Piotr Nowicki5d5841f2020-06-05 16:33:24 +020041#if !defined(MBEDTLS_PLATFORM_C)
42#include <stdlib.h>
43#define mbedtls_calloc calloc
44#define mbedtls_free free
45#endif
46
Jarno Lamsae29e8a42019-10-03 11:06:35 +030047#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
48#include "mbedtls/entropy_poll.h"
49#endif
50
Andrzej Kurek7f81c862020-11-17 14:22:39 +010051#if defined(MBEDTLS_PLATFORM_FAULT_CALLBACKS)
52#include "platform_fault.h"
53#else
54static void mbedtls_platform_fault(){}
55#endif
56
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010057#include <stddef.h>
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010058#include <string.h>
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010059
Piotr Nowicki77b7a772020-07-31 16:11:06 +020060/* Max number of loops for mbedtls_platform_random_delay. */
Arto Kinnunenb1486512020-01-09 11:11:23 +020061#define MAX_RAND_DELAY 100
Arto Kinnunenac6d2262020-01-09 10:11:20 +020062
Piotr Nowicki77b7a772020-07-31 16:11:06 +020063/* Parameters for the linear congruential generator used as a non-cryptographic
64 * random number generator. The same parameters are used by e.g. ANSI C. */
65#define RAND_MULTIPLIER 1103515245
66#define RAND_INCREMENT 12345
67#define RAND_MODULUS 0x80000000
68
69/* The number of iterations after which the seed of the non-cryptographic
70 * random number generator will be changed. This is used only if the
71 * MBEDTLS_ENTROPY_HARDWARE_ALT option is enabled. */
72#define RAND_SEED_LIFE 10000
73
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050074#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010075/*
76 * This implementation should never be optimized out by the compiler
77 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050078 * This implementation for mbedtls_platform_zeroize() was inspired from Colin
79 * Percival's blog article at:
Andres Amaya Garcia1e8ea5f2018-03-08 20:46:39 +000080 *
81 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
82 *
83 * It uses a volatile function pointer to the standard memset(). Because the
84 * pointer is volatile the compiler expects it to change at
85 * any time and will not optimize out the call that could potentially perform
86 * other operations on the input buffer instead of just setting it to 0.
87 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
88 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
89 * details), optimizations of the following form are still possible:
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010090 *
91 * if( memset_func != memset )
92 * memset_func( buf, 0, len );
93 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050094 * Note that it is extremely difficult to guarantee that
95 * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
96 * in a portable way. For this reason, Mbed TLS also provides the configuration
97 * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
98 * mbedtls_platform_zeroize() to use a suitable implementation for their
99 * platform and needs.
Andres Amaya Garciaecd18912017-10-26 22:43:41 +0100100 */
Manuel Pégourié-Gonnard14f33e72019-10-02 16:23:52 +0200101void *mbedtls_platform_memset( void *, int, size_t );
102static void * (* const volatile memset_func)( void *, int, size_t ) = mbedtls_platform_memset;
Andres Amaya Garciaecd18912017-10-26 22:43:41 +0100103
Piotr Nowickied840db2020-06-23 12:59:56 +0200104void *mbedtls_platform_zeroize( void *buf, size_t len )
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +0100105{
Piotr Nowickied840db2020-06-23 12:59:56 +0200106 volatile size_t vlen = len;
Vikas Katariya0c344992019-08-15 14:24:20 +0100107
Piotr Nowickied840db2020-06-23 12:59:56 +0200108 MBEDTLS_INTERNAL_VALIDATE_RET( ( len == 0 || buf != NULL ), NULL );
109
110 if( vlen > 0 )
111 {
112 return memset_func( buf, 0, vlen );
113 }
114 else
115 {
116 mbedtls_platform_random_delay();
117 if( vlen == 0 && vlen == len )
118 {
119 return buf;
120 }
121 }
122 return NULL;
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +0100123}
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500124#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100125
Manuel Pégourié-Gonnard51f65e42019-10-02 16:01:14 +0200126void *mbedtls_platform_memset( void *ptr, int value, size_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300127{
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100128 size_t i, start_offset = 0;
Piotr Nowickied840db2020-06-23 12:59:56 +0200129 volatile size_t flow_counter = 0;
130 volatile char *b = ptr;
131 char rnd_data;
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100132 if( num > 0 )
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200133 {
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100134 start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200135
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100136 rnd_data = (char) mbedtls_platform_random_in_range( 256 );
Piotr Nowickied840db2020-06-23 12:59:56 +0200137
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100138 /* Perform a memset operations with random data and start from a random
139 * location */
140 for( i = start_offset; i < num; ++i )
141 {
142 b[i] = rnd_data;
143 flow_counter++;
144 }
Piotr Nowickied840db2020-06-23 12:59:56 +0200145
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100146 /* Start from a random location with target data */
147 for( i = start_offset; i < num; ++i )
148 {
149 b[i] = value;
150 flow_counter++;
151 }
Piotr Nowickied840db2020-06-23 12:59:56 +0200152
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100153 /* Second memset operation with random data */
154 for( i = 0; i < start_offset; ++i )
155 {
156 b[i] = rnd_data;
157 flow_counter++;
158 }
159
160 /* Finish memset operation with correct data */
161 for( i = 0; i < start_offset; ++i )
162 {
163 b[i] = value;
164 flow_counter++;
165 }
166 }
Piotr Nowickied840db2020-06-23 12:59:56 +0200167 /* check the correct number of iterations */
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200168 if( flow_counter == 2 * num )
Piotr Nowickied840db2020-06-23 12:59:56 +0200169 {
170 mbedtls_platform_random_delay();
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200171 if( flow_counter == 2 * num )
Piotr Nowickied840db2020-06-23 12:59:56 +0200172 {
173 return ptr;
174 }
175 }
Andrzej Kurek7f81c862020-11-17 14:22:39 +0100176 mbedtls_platform_fault();
Piotr Nowickied840db2020-06-23 12:59:56 +0200177 return NULL;
Jarno Lamsa07363252019-09-27 16:20:11 +0300178}
179
Manuel Pégourié-Gonnard51f65e42019-10-02 16:01:14 +0200180void *mbedtls_platform_memcpy( void *dst, const void *src, size_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300181{
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200182 size_t i;
183 volatile size_t flow_counter = 0;
Jarno Lamsa07363252019-09-27 16:20:11 +0300184
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200185 if( num > 0 )
186 {
187 /* Randomize start offset. */
188 size_t start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
189 /* Randomize initial data to prevent leakage while copying */
190 uint32_t data = mbedtls_platform_random_in_range( 256 );
191
192 /* Use memset with random value at first to increase security - memset is
193 not normally part of the memcpy function and here can be useed
194 with regular, unsecured implementation */
195 memset( (void *) dst, data, num );
196
197 /* Make a copy starting from a random location. */
198 i = start_offset;
199 do
200 {
201 ( (char*) dst )[i] = ( (char*) src )[i];
202 flow_counter++;
203 }
204 while( ( i = ( i + 1 ) % num ) != start_offset );
205 }
206
207 /* check the correct number of iterations */
208 if( flow_counter == num )
209 {
210 mbedtls_platform_random_delay();
211 if( flow_counter == num )
212 {
213 return dst;
214 }
215 }
Andrzej Kurek7f81c862020-11-17 14:22:39 +0100216 mbedtls_platform_fault();
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200217 return NULL;
Jarno Lamsa07363252019-09-27 16:20:11 +0300218}
219
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200220int mbedtls_platform_memmove( void *dst, const void *src, size_t num )
221{
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200222 void *ret1 = NULL;
223 void *ret2 = NULL;
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200224 /* The buffers can have a common part, so we cannot do a copy from a random
225 * location. By using a temporary buffer we can do so, but the cost of it
226 * is using more memory and longer transfer time. */
227 void *tmp = mbedtls_calloc( 1, num );
228 if( tmp != NULL )
229 {
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200230 ret1 = mbedtls_platform_memcpy( tmp, src, num );
231 ret2 = mbedtls_platform_memcpy( dst, tmp, num );
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200232 mbedtls_free( tmp );
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200233 if( ret1 == tmp && ret2 == dst )
234 {
235 return 0;
236 }
237 return MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200238 }
239
Piotr Nowickice0aab42020-06-08 14:08:49 +0200240 return MBEDTLS_ERR_PLATFORM_ALLOC_FAILED;
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200241}
242
Andrzej Kurek8bb08392020-08-09 02:11:19 -0400243#if !defined(MBEDTLS_DEPRECATED_REMOVED)
244int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num )
245{
246 return( mbedtls_platform_memequal( buf1, buf2, num ) );
247}
248#endif /* MBEDTLS_DEPRECATED_REMOVED */
249
Piotr Nowickie3c4ee52020-06-23 12:59:56 +0200250int mbedtls_platform_memequal( const void *buf1, const void *buf2, size_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300251{
Jarno Lamsa7cb90272019-10-02 08:32:51 +0300252 volatile const unsigned char *A = (volatile const unsigned char *) buf1;
253 volatile const unsigned char *B = (volatile const unsigned char *) buf2;
254 volatile unsigned char diff = 0;
Jarno Lamsa07363252019-09-27 16:20:11 +0300255
Piotr Nowickif0ab6d62020-05-25 12:48:30 +0200256 /* Start from a random location and check the correct number of iterations */
257 size_t i, flow_counter = 0;
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100258 size_t start_offset = 0;
259 if( num > 0 )
Jarno Lamsa07363252019-09-27 16:20:11 +0300260 {
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100261 start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
Jarno Lamsa07363252019-09-27 16:20:11 +0300262
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100263 for( i = start_offset; i < num; i++ )
264 {
265 unsigned char x = A[i], y = B[i];
266 flow_counter++;
267 diff |= x ^ y;
268 }
Jarno Lamsa07363252019-09-27 16:20:11 +0300269
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100270 for( i = 0; i < start_offset; i++ )
271 {
272 unsigned char x = A[i], y = B[i];
273 flow_counter++;
274 diff |= x ^ y;
275 }
276 }
Piotr Nowicki4aaa34c2020-05-20 13:57:38 +0200277 /* Return 0 only when diff is 0 and flow_counter is equal to num */
278 return( (int) diff | (int) ( flow_counter ^ num ) );
Jarno Lamsa07363252019-09-27 16:20:11 +0300279}
280
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200281/* This function implements a non-cryptographic random number generator based
282 * on the linear congruential generator algorithm. Additionally, if the
283 * MBEDTLS_ENTROPY_HARDWARE_ALT flag is defined, the seed is set at the first
284 * call of this function with using a hardware random number generator and
285 * changed every RAND_SEED_LIFE number of iterations.
286 *
287 * The value of the returned number is in the range [0; 0xffff].
288 *
289 * Note: The range of values with a 16-bit precision is related to the modulo
290 * parameter of the generator and the fact that the function does not return the
291 * full value of the internal state of the generator.
292 */
293static uint32_t mbedtls_platform_random_uint16( void )
Andrzej Kurek189ee742020-06-24 17:28:31 -0400294{
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200295 /* Set random_state - the first random value should not be zero. */
296 static uint32_t random_state = RAND_INCREMENT;
Andrzej Kurek189ee742020-06-24 17:28:31 -0400297
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200298#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
299
300 static uint32_t random_seed_life = 0;
301
302 if( 0 < random_seed_life )
303 {
304 --random_seed_life;
305 }
306 else
307 {
308 size_t olen = 0;
309 uint32_t hw_random;
310 mbedtls_hardware_poll( NULL,
311 (unsigned char *) &hw_random, sizeof( hw_random ),
312 &olen );
313 if( olen == sizeof( hw_random ) )
314 {
315 random_state ^= hw_random;
316 random_seed_life = RAND_SEED_LIFE;
317 }
318 }
319
320#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
321
322 random_state = ( ( random_state * RAND_MULTIPLIER ) + RAND_INCREMENT ) % RAND_MODULUS;
323
324 /* Do not return the entire random_state to hide generator predictability for
325 * the next iteration */
326 return( ( random_state >> 15 ) & 0xffff );
327}
328
329uint32_t mbedtls_platform_random_uint32( void )
330{
331 return( ( mbedtls_platform_random_uint16() << 16 ) |
332 mbedtls_platform_random_uint16() );
Andrzej Kurek189ee742020-06-24 17:28:31 -0400333}
334
Shelly Liberman05beb9a2020-09-13 15:23:56 +0300335void mbedtls_platform_random_buf( uint8_t *buf, size_t len )
336{
Shelly Liberman699aebe2020-12-14 18:30:53 +0200337 volatile size_t flow_control = 0, flow_control_check = len;
Shelly Liberman05beb9a2020-09-13 15:23:56 +0300338 uint16_t val;
339
340 while( len > 1 )
341 {
342 val = mbedtls_platform_random_uint16();
343 buf[len-1] = (uint8_t)val;
344 buf[len-2] = (uint8_t)(val>>8);
345 len -= 2;
Shelly Liberman7326c622020-12-01 13:28:30 +0200346 flow_control += 2;
Shelly Liberman05beb9a2020-09-13 15:23:56 +0300347 }
348 if( len == 1 )
349 {
350 buf[0] = (uint8_t)mbedtls_platform_random_uint16();
Shelly Liberman7326c622020-12-01 13:28:30 +0200351 flow_control ++;
Shelly Liberman05beb9a2020-09-13 15:23:56 +0300352 }
353
Shelly Liberman699aebe2020-12-14 18:30:53 +0200354 if ( flow_control == flow_control_check )
Shelly Liberman7326c622020-12-01 13:28:30 +0200355 {
356 return;
357 }
358 mbedtls_platform_fault();
Shelly Liberman05beb9a2020-09-13 15:23:56 +0300359}
360
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200361uint32_t mbedtls_platform_random_in_range( uint32_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300362{
Andrzej Kureka138c0a2020-11-02 15:53:39 +0100363 return mbedtls_platform_random_uint32() % num;
Jarno Lamsa07363252019-09-27 16:20:11 +0300364}
365
Arto Kinnunenac6d2262020-01-09 10:11:20 +0200366void mbedtls_platform_random_delay( void )
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200367{
Piotr Nowicki057daa32020-08-03 13:08:33 +0200368#if defined(MBEDTLS_FI_COUNTERMEASURES)
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200369 uint32_t rn_1, rn_2, rn_3;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200370 volatile size_t i = 0;
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200371 uint8_t shift;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200372
Arto Kinnunenb1486512020-01-09 11:11:23 +0200373 rn_1 = mbedtls_platform_random_in_range( MAX_RAND_DELAY );
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200374 rn_2 = mbedtls_platform_random_in_range( 0xffffffff ) + 1;
375 rn_3 = mbedtls_platform_random_in_range( 0xffffffff ) + 1;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200376
Arto Kinnunenb47b1052019-12-05 17:32:05 +0200377 do
378 {
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200379 i++;
Piotr Nowickib06ec052020-06-03 15:59:59 +0200380 /* Dummy calculations to increase the time between iterations and
381 * make side channel attack more difficult by reducing predictability
Piotr Nowicki26c33692020-08-11 13:58:47 +0200382 * of its behaviour. */
383 shift = ( rn_2 & 0x07 ) + 1;
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200384 if ( i % 2 )
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200385 rn_2 = ( rn_2 >> shift ) | ( rn_2 << ( 32 - shift ) );
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200386 else
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200387 rn_3 = ( rn_3 << shift ) | ( rn_3 >> ( 32 - shift ) );
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200388 rn_2 ^= rn_3;
389 } while( i < rn_1 || rn_2 == 0 || rn_3 == 0 );
Piotr Nowicki057daa32020-08-03 13:08:33 +0200390
391#endif /* MBEDTLS_FI_COUNTERMEASURES */
392 return;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200393}
394
Hanno Becker6a739782018-09-05 15:06:19 +0100395#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100396#include <time.h>
Hanno Beckercfeb70c2018-09-05 13:50:22 +0100397#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +0100398 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
399 defined(__MACH__)))
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100400#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +0100401#endif /* !_WIN32 && (unix || __unix || __unix__ ||
402 * (__APPLE__ && __MACH__)) */
Hanno Becker6f705812018-09-06 09:06:33 +0100403
404#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
405 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
406 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100407/*
408 * This is a convenience shorthand macro to avoid checking the long
409 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +0100410 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100411 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +0100412 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100413 */
Hanno Beckerf5106d52018-09-06 12:09:56 +0100414#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100415#define PLATFORM_UTIL_USE_GMTIME
Hanno Beckerf5106d52018-09-06 12:09:56 +0100416#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
417
Hanno Becker6f705812018-09-06 09:06:33 +0100418#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
419 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
420 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100421
Hanno Becker6a739782018-09-05 15:06:19 +0100422struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
423 struct tm *tm_buf )
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100424{
425#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100426 return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100427#elif !defined(PLATFORM_UTIL_USE_GMTIME)
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100428 return( gmtime_r( tt, tm_buf ) );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100429#else
430 struct tm *lt;
431
432#if defined(MBEDTLS_THREADING_C)
433 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
434 return( NULL );
435#endif /* MBEDTLS_THREADING_C */
436
437 lt = gmtime( tt );
438
439 if( lt != NULL )
440 {
441 memcpy( tm_buf, lt, sizeof( struct tm ) );
442 }
443
444#if defined(MBEDTLS_THREADING_C)
445 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
446 return( NULL );
447#endif /* MBEDTLS_THREADING_C */
448
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100449 return( ( lt == NULL ) ? NULL : tm_buf );
450#endif /* _WIN32 && !EFIX64 && !EFI32 */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100451}
Hanno Becker6a739782018-09-05 15:06:19 +0100452#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300453
Andrzej Kureka7932372020-09-19 07:56:06 +0200454#if defined(MBEDTLS_VALIDATE_AES_KEYS_INTEGRITY) || defined(MBEDTLS_VALIDATE_SSL_KEYS_INTEGRITY)
455uint32_t mbedtls_hash( const void *data, size_t data_len_bytes )
456{
457 uint32_t result = 0;
458 size_t i;
459 /* data_len_bytes - only multiples of 4 are considered, rest is truncated */
460 for( i = 0; i < data_len_bytes >> 2; i++ )
461 {
462 result ^= ( (uint32_t*) data )[i];
463 }
464 return result;
465}
466#endif
467
Arto Kinnunenee9bfca2019-09-06 16:59:00 +0300468unsigned char* mbedtls_platform_put_uint32_be( unsigned char *buf,
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300469 size_t num )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300470{
471 *buf++ = (unsigned char) ( num >> 24 );
472 *buf++ = (unsigned char) ( num >> 16 );
473 *buf++ = (unsigned char) ( num >> 8 );
474 *buf++ = (unsigned char) ( num );
475
476 return buf;
477}
478
Arto Kinnunenee9bfca2019-09-06 16:59:00 +0300479unsigned char* mbedtls_platform_put_uint24_be( unsigned char *buf,
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300480 size_t num )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300481{
482 *buf++ = (unsigned char) ( num >> 16 );
483 *buf++ = (unsigned char) ( num >> 8 );
484 *buf++ = (unsigned char) ( num );
485
486 return buf;
487}
488
Arto Kinnunenee9bfca2019-09-06 16:59:00 +0300489unsigned char* mbedtls_platform_put_uint16_be( unsigned char *buf,
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300490 size_t num )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300491{
492 *buf++ = (unsigned char) ( num >> 8 );
493 *buf++ = (unsigned char) ( num );
494
495 return buf;
496}
497
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300498size_t mbedtls_platform_get_uint32_be( const unsigned char *buf )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300499{
500 return ( ( (unsigned int) buf[0] << 24 ) |
501 ( (unsigned int) buf[1] << 16 ) |
502 ( (unsigned int) buf[2] << 8 ) |
503 ( (unsigned int) buf[3] ) );
504}
505
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300506size_t mbedtls_platform_get_uint24_be( const unsigned char *buf )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300507{
508 return ( ( buf[0] << 16 ) |
509 ( buf[1] << 8) |
510 ( buf[2] ) );
511}
512
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300513size_t mbedtls_platform_get_uint16_be( const unsigned char *buf )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300514{
515 return ( ( buf[0] << 8 ) |
516 ( buf[1] ) );
517}