blob: 458dfc9bee63832a5f7417d4d2ee90d24ec8b5b3 [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
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010051#include <stddef.h>
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010052#include <string.h>
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010053
Piotr Nowicki77b7a772020-07-31 16:11:06 +020054/* Max number of loops for mbedtls_platform_random_delay. */
Arto Kinnunenb1486512020-01-09 11:11:23 +020055#define MAX_RAND_DELAY 100
Arto Kinnunenac6d2262020-01-09 10:11:20 +020056
Piotr Nowicki77b7a772020-07-31 16:11:06 +020057/* Parameters for the linear congruential generator used as a non-cryptographic
58 * random number generator. The same parameters are used by e.g. ANSI C. */
59#define RAND_MULTIPLIER 1103515245
60#define RAND_INCREMENT 12345
61#define RAND_MODULUS 0x80000000
62
63/* The number of iterations after which the seed of the non-cryptographic
64 * random number generator will be changed. This is used only if the
65 * MBEDTLS_ENTROPY_HARDWARE_ALT option is enabled. */
66#define RAND_SEED_LIFE 10000
67
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050068#if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010069/*
70 * This implementation should never be optimized out by the compiler
71 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050072 * This implementation for mbedtls_platform_zeroize() was inspired from Colin
73 * Percival's blog article at:
Andres Amaya Garcia1e8ea5f2018-03-08 20:46:39 +000074 *
75 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
76 *
77 * It uses a volatile function pointer to the standard memset(). Because the
78 * pointer is volatile the compiler expects it to change at
79 * any time and will not optimize out the call that could potentially perform
80 * other operations on the input buffer instead of just setting it to 0.
81 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
82 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
83 * details), optimizations of the following form are still possible:
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010084 *
85 * if( memset_func != memset )
86 * memset_func( buf, 0, len );
87 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050088 * Note that it is extremely difficult to guarantee that
89 * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
90 * in a portable way. For this reason, Mbed TLS also provides the configuration
91 * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
92 * mbedtls_platform_zeroize() to use a suitable implementation for their
93 * platform and needs.
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010094 */
Manuel Pégourié-Gonnard14f33e72019-10-02 16:23:52 +020095void *mbedtls_platform_memset( void *, int, size_t );
96static void * (* const volatile memset_func)( void *, int, size_t ) = mbedtls_platform_memset;
Andres Amaya Garciaecd18912017-10-26 22:43:41 +010097
Piotr Nowickied840db2020-06-23 12:59:56 +020098void *mbedtls_platform_zeroize( void *buf, size_t len )
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +010099{
Piotr Nowickied840db2020-06-23 12:59:56 +0200100 volatile size_t vlen = len;
Vikas Katariya0c344992019-08-15 14:24:20 +0100101
Piotr Nowickied840db2020-06-23 12:59:56 +0200102 MBEDTLS_INTERNAL_VALIDATE_RET( ( len == 0 || buf != NULL ), NULL );
103
104 if( vlen > 0 )
105 {
106 return memset_func( buf, 0, vlen );
107 }
108 else
109 {
110 mbedtls_platform_random_delay();
111 if( vlen == 0 && vlen == len )
112 {
113 return buf;
114 }
115 }
116 return NULL;
Andres Amaya Garcia614d9c02017-10-24 21:27:43 +0100117}
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500118#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100119
Manuel Pégourié-Gonnard51f65e42019-10-02 16:01:14 +0200120void *mbedtls_platform_memset( void *ptr, int value, size_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300121{
Piotr Nowickied840db2020-06-23 12:59:56 +0200122 size_t i, start_offset;
123 volatile size_t flow_counter = 0;
124 volatile char *b = ptr;
125 char rnd_data;
Jarno Lamsa07363252019-09-27 16:20:11 +0300126
Piotr Nowickied840db2020-06-23 12:59:56 +0200127 start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
128 rnd_data = (char) mbedtls_platform_random_in_range( 256 );
Jarno Lamsa07363252019-09-27 16:20:11 +0300129
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200130 /* Perform a memset operations with random data and start from a random
131 * location */
132 for( i = start_offset; i < num; ++i )
133 {
134 b[i] = rnd_data;
135 flow_counter++;
136 }
137
138 /* Start from a random location with target data */
Piotr Nowickied840db2020-06-23 12:59:56 +0200139 for( i = start_offset; i < num; ++i )
140 {
141 b[i] = value;
142 flow_counter++;
143 }
144
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200145 /* Second memset operation with random data */
Piotr Nowickied840db2020-06-23 12:59:56 +0200146 for( i = 0; i < start_offset; ++i )
147 {
148 b[i] = rnd_data;
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200149 flow_counter++;
Piotr Nowickied840db2020-06-23 12:59:56 +0200150 }
151
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200152 /* Finish memset operation with correct data */
Piotr Nowickied840db2020-06-23 12:59:56 +0200153 for( i = 0; i < start_offset; ++i )
154 {
155 b[i] = value;
156 flow_counter++;
157 }
158
159 /* check the correct number of iterations */
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200160 if( flow_counter == 2 * num )
Piotr Nowickied840db2020-06-23 12:59:56 +0200161 {
162 mbedtls_platform_random_delay();
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200163 if( flow_counter == 2 * num )
Piotr Nowickied840db2020-06-23 12:59:56 +0200164 {
165 return ptr;
166 }
167 }
168 return NULL;
Jarno Lamsa07363252019-09-27 16:20:11 +0300169}
170
Manuel Pégourié-Gonnard51f65e42019-10-02 16:01:14 +0200171void *mbedtls_platform_memcpy( void *dst, const void *src, size_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300172{
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200173 size_t i;
174 volatile size_t flow_counter = 0;
Jarno Lamsa07363252019-09-27 16:20:11 +0300175
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200176 if( num > 0 )
177 {
178 /* Randomize start offset. */
179 size_t start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
180 /* Randomize initial data to prevent leakage while copying */
181 uint32_t data = mbedtls_platform_random_in_range( 256 );
182
183 /* Use memset with random value at first to increase security - memset is
184 not normally part of the memcpy function and here can be useed
185 with regular, unsecured implementation */
186 memset( (void *) dst, data, num );
187
188 /* Make a copy starting from a random location. */
189 i = start_offset;
190 do
191 {
192 ( (char*) dst )[i] = ( (char*) src )[i];
193 flow_counter++;
194 }
195 while( ( i = ( i + 1 ) % num ) != start_offset );
196 }
197
198 /* check the correct number of iterations */
199 if( flow_counter == num )
200 {
201 mbedtls_platform_random_delay();
202 if( flow_counter == num )
203 {
204 return dst;
205 }
206 }
207 return NULL;
Jarno Lamsa07363252019-09-27 16:20:11 +0300208}
209
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200210int mbedtls_platform_memmove( void *dst, const void *src, size_t num )
211{
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200212 void *ret1 = NULL;
213 void *ret2 = NULL;
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200214 /* The buffers can have a common part, so we cannot do a copy from a random
215 * location. By using a temporary buffer we can do so, but the cost of it
216 * is using more memory and longer transfer time. */
217 void *tmp = mbedtls_calloc( 1, num );
218 if( tmp != NULL )
219 {
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200220 ret1 = mbedtls_platform_memcpy( tmp, src, num );
221 ret2 = mbedtls_platform_memcpy( dst, tmp, num );
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200222 mbedtls_free( tmp );
Piotr Nowickiea8e8462020-08-10 15:20:26 +0200223 if( ret1 == tmp && ret2 == dst )
224 {
225 return 0;
226 }
227 return MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200228 }
229
Piotr Nowickice0aab42020-06-08 14:08:49 +0200230 return MBEDTLS_ERR_PLATFORM_ALLOC_FAILED;
Piotr Nowicki5d5841f2020-06-05 16:33:24 +0200231}
232
Andrzej Kurek8bb08392020-08-09 02:11:19 -0400233#if !defined(MBEDTLS_DEPRECATED_REMOVED)
234int mbedtls_platform_memcmp( const void *buf1, const void *buf2, size_t num )
235{
236 return( mbedtls_platform_memequal( buf1, buf2, num ) );
237}
238#endif /* MBEDTLS_DEPRECATED_REMOVED */
239
Piotr Nowickie3c4ee52020-06-23 12:59:56 +0200240int mbedtls_platform_memequal( const void *buf1, const void *buf2, size_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300241{
Jarno Lamsa7cb90272019-10-02 08:32:51 +0300242 volatile const unsigned char *A = (volatile const unsigned char *) buf1;
243 volatile const unsigned char *B = (volatile const unsigned char *) buf2;
244 volatile unsigned char diff = 0;
Jarno Lamsa07363252019-09-27 16:20:11 +0300245
Piotr Nowickif0ab6d62020-05-25 12:48:30 +0200246 /* Start from a random location and check the correct number of iterations */
247 size_t i, flow_counter = 0;
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200248 size_t start_offset = (size_t) mbedtls_platform_random_in_range( (uint32_t) num );
Jarno Lamsa07363252019-09-27 16:20:11 +0300249
Jarno Lamsa0ff71092019-10-02 08:18:29 +0300250 for( i = start_offset; i < num; i++ )
Jarno Lamsa07363252019-09-27 16:20:11 +0300251 {
Jarno Lamsa7cb90272019-10-02 08:32:51 +0300252 unsigned char x = A[i], y = B[i];
Piotr Nowicki4aaa34c2020-05-20 13:57:38 +0200253 flow_counter++;
Jarno Lamsa7cb90272019-10-02 08:32:51 +0300254 diff |= x ^ y;
Jarno Lamsa07363252019-09-27 16:20:11 +0300255 }
256
Jarno Lamsa0ff71092019-10-02 08:18:29 +0300257 for( i = 0; i < start_offset; i++ )
Jarno Lamsa07363252019-09-27 16:20:11 +0300258 {
Jarno Lamsa7cb90272019-10-02 08:32:51 +0300259 unsigned char x = A[i], y = B[i];
Piotr Nowicki4aaa34c2020-05-20 13:57:38 +0200260 flow_counter++;
Jarno Lamsa7cb90272019-10-02 08:32:51 +0300261 diff |= x ^ y;
Jarno Lamsa07363252019-09-27 16:20:11 +0300262 }
263
Piotr Nowicki4aaa34c2020-05-20 13:57:38 +0200264 /* Return 0 only when diff is 0 and flow_counter is equal to num */
265 return( (int) diff | (int) ( flow_counter ^ num ) );
Jarno Lamsa07363252019-09-27 16:20:11 +0300266}
267
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200268/* This function implements a non-cryptographic random number generator based
269 * on the linear congruential generator algorithm. Additionally, if the
270 * MBEDTLS_ENTROPY_HARDWARE_ALT flag is defined, the seed is set at the first
271 * call of this function with using a hardware random number generator and
272 * changed every RAND_SEED_LIFE number of iterations.
273 *
274 * The value of the returned number is in the range [0; 0xffff].
275 *
276 * Note: The range of values with a 16-bit precision is related to the modulo
277 * parameter of the generator and the fact that the function does not return the
278 * full value of the internal state of the generator.
279 */
280static uint32_t mbedtls_platform_random_uint16( void )
Andrzej Kurek189ee742020-06-24 17:28:31 -0400281{
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200282 /* Set random_state - the first random value should not be zero. */
283 static uint32_t random_state = RAND_INCREMENT;
Andrzej Kurek189ee742020-06-24 17:28:31 -0400284
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200285#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
286
287 static uint32_t random_seed_life = 0;
288
289 if( 0 < random_seed_life )
290 {
291 --random_seed_life;
292 }
293 else
294 {
295 size_t olen = 0;
296 uint32_t hw_random;
297 mbedtls_hardware_poll( NULL,
298 (unsigned char *) &hw_random, sizeof( hw_random ),
299 &olen );
300 if( olen == sizeof( hw_random ) )
301 {
302 random_state ^= hw_random;
303 random_seed_life = RAND_SEED_LIFE;
304 }
305 }
306
307#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
308
309 random_state = ( ( random_state * RAND_MULTIPLIER ) + RAND_INCREMENT ) % RAND_MODULUS;
310
311 /* Do not return the entire random_state to hide generator predictability for
312 * the next iteration */
313 return( ( random_state >> 15 ) & 0xffff );
314}
315
316uint32_t mbedtls_platform_random_uint32( void )
317{
318 return( ( mbedtls_platform_random_uint16() << 16 ) |
319 mbedtls_platform_random_uint16() );
Andrzej Kurek189ee742020-06-24 17:28:31 -0400320}
321
Shelly Liberman05beb9a2020-09-13 15:23:56 +0300322void mbedtls_platform_random_buf( uint8_t *buf, size_t len )
323{
324 uint16_t val;
325
326 while( len > 1 )
327 {
328 val = mbedtls_platform_random_uint16();
329 buf[len-1] = (uint8_t)val;
330 buf[len-2] = (uint8_t)(val>>8);
331 len -= 2;
332 }
333 if( len == 1 )
334 {
335 buf[0] = (uint8_t)mbedtls_platform_random_uint16();
336 }
337
338 return;
339}
340
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200341uint32_t mbedtls_platform_random_in_range( uint32_t num )
Jarno Lamsa07363252019-09-27 16:20:11 +0300342{
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200343 uint32_t result;
Jarno Lamsae29e8a42019-10-03 11:06:35 +0300344
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200345 if( num <= 1 )
Jarno Lamsa436d18d2019-10-03 11:46:30 +0300346 {
347 result = 0;
348 }
349 else
350 {
Piotr Nowicki77b7a772020-07-31 16:11:06 +0200351 result = mbedtls_platform_random_uint32() % num;
Jarno Lamsa436d18d2019-10-03 11:46:30 +0300352 }
353
354 return( result );
Jarno Lamsa07363252019-09-27 16:20:11 +0300355}
356
Arto Kinnunenac6d2262020-01-09 10:11:20 +0200357void mbedtls_platform_random_delay( void )
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200358{
Piotr Nowicki057daa32020-08-03 13:08:33 +0200359#if defined(MBEDTLS_FI_COUNTERMEASURES)
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200360 uint32_t rn_1, rn_2, rn_3;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200361 volatile size_t i = 0;
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200362 uint8_t shift;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200363
Arto Kinnunenb1486512020-01-09 11:11:23 +0200364 rn_1 = mbedtls_platform_random_in_range( MAX_RAND_DELAY );
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200365 rn_2 = mbedtls_platform_random_in_range( 0xffffffff ) + 1;
366 rn_3 = mbedtls_platform_random_in_range( 0xffffffff ) + 1;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200367
Arto Kinnunenb47b1052019-12-05 17:32:05 +0200368 do
369 {
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200370 i++;
Piotr Nowickib06ec052020-06-03 15:59:59 +0200371 /* Dummy calculations to increase the time between iterations and
372 * make side channel attack more difficult by reducing predictability
Piotr Nowicki26c33692020-08-11 13:58:47 +0200373 * of its behaviour. */
374 shift = ( rn_2 & 0x07 ) + 1;
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200375 if ( i % 2 )
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200376 rn_2 = ( rn_2 >> shift ) | ( rn_2 << ( 32 - shift ) );
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200377 else
Piotr Nowicki8656fc62020-06-23 12:30:40 +0200378 rn_3 = ( rn_3 << shift ) | ( rn_3 >> ( 32 - shift ) );
Arto Kinnunendbf2b432019-12-30 12:55:30 +0200379 rn_2 ^= rn_3;
380 } while( i < rn_1 || rn_2 == 0 || rn_3 == 0 );
Piotr Nowicki057daa32020-08-03 13:08:33 +0200381
382#endif /* MBEDTLS_FI_COUNTERMEASURES */
383 return;
Arto Kinnunen4c63b982019-12-02 15:01:41 +0200384}
385
Hanno Becker6a739782018-09-05 15:06:19 +0100386#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100387#include <time.h>
Hanno Beckercfeb70c2018-09-05 13:50:22 +0100388#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +0100389 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
390 defined(__MACH__)))
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100391#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +0100392#endif /* !_WIN32 && (unix || __unix || __unix__ ||
393 * (__APPLE__ && __MACH__)) */
Hanno Becker6f705812018-09-06 09:06:33 +0100394
395#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
396 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
397 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100398/*
399 * This is a convenience shorthand macro to avoid checking the long
400 * preprocessor conditions above. Ideally, we could expose this macro in
Hanno Becker7dd82b42018-09-05 16:25:50 +0100401 * platform_util.h and simply use it in platform_util.c, threading.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100402 * threading.h. However, this macro is not part of the Mbed TLS public API, so
Andres Amaya Garcia3c9733a2018-09-05 11:52:07 +0100403 * we keep it private by only defining it in this file
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100404 */
Hanno Beckerf5106d52018-09-06 12:09:56 +0100405#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100406#define PLATFORM_UTIL_USE_GMTIME
Hanno Beckerf5106d52018-09-06 12:09:56 +0100407#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
408
Hanno Becker6f705812018-09-06 09:06:33 +0100409#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
410 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
411 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100412
Hanno Becker6a739782018-09-05 15:06:19 +0100413struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
414 struct tm *tm_buf )
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100415{
416#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100417 return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100418#elif !defined(PLATFORM_UTIL_USE_GMTIME)
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100419 return( gmtime_r( tt, tm_buf ) );
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100420#else
421 struct tm *lt;
422
423#if defined(MBEDTLS_THREADING_C)
424 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
425 return( NULL );
426#endif /* MBEDTLS_THREADING_C */
427
428 lt = gmtime( tt );
429
430 if( lt != NULL )
431 {
432 memcpy( tm_buf, lt, sizeof( struct tm ) );
433 }
434
435#if defined(MBEDTLS_THREADING_C)
436 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
437 return( NULL );
438#endif /* MBEDTLS_THREADING_C */
439
Andres Amaya Garciaa658d7d2018-08-21 19:33:02 +0100440 return( ( lt == NULL ) ? NULL : tm_buf );
441#endif /* _WIN32 && !EFIX64 && !EFI32 */
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100442}
Hanno Becker6a739782018-09-05 15:06:19 +0100443#endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300444
Arto Kinnunenee9bfca2019-09-06 16:59:00 +0300445unsigned char* mbedtls_platform_put_uint32_be( unsigned char *buf,
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300446 size_t num )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300447{
448 *buf++ = (unsigned char) ( num >> 24 );
449 *buf++ = (unsigned char) ( num >> 16 );
450 *buf++ = (unsigned char) ( num >> 8 );
451 *buf++ = (unsigned char) ( num );
452
453 return buf;
454}
455
Arto Kinnunenee9bfca2019-09-06 16:59:00 +0300456unsigned char* mbedtls_platform_put_uint24_be( unsigned char *buf,
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300457 size_t num )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300458{
459 *buf++ = (unsigned char) ( num >> 16 );
460 *buf++ = (unsigned char) ( num >> 8 );
461 *buf++ = (unsigned char) ( num );
462
463 return buf;
464}
465
Arto Kinnunenee9bfca2019-09-06 16:59:00 +0300466unsigned char* mbedtls_platform_put_uint16_be( unsigned char *buf,
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300467 size_t num )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300468{
469 *buf++ = (unsigned char) ( num >> 8 );
470 *buf++ = (unsigned char) ( num );
471
472 return buf;
473}
474
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300475size_t mbedtls_platform_get_uint32_be( const unsigned char *buf )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300476{
477 return ( ( (unsigned int) buf[0] << 24 ) |
478 ( (unsigned int) buf[1] << 16 ) |
479 ( (unsigned int) buf[2] << 8 ) |
480 ( (unsigned int) buf[3] ) );
481}
482
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300483size_t mbedtls_platform_get_uint24_be( const unsigned char *buf )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300484{
485 return ( ( buf[0] << 16 ) |
486 ( buf[1] << 8) |
487 ( buf[2] ) );
488}
489
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300490size_t mbedtls_platform_get_uint16_be( const unsigned char *buf )
Arto Kinnunen0b62ce82019-09-04 14:04:57 +0300491{
492 return ( ( buf[0] << 8 ) |
493 ( buf[1] ) );
494}