blob: fa3a5514d2e1c4b19489dc49eddbcf066dcc0c77 [file] [log] [blame]
Gilles Peskinee137ebc2021-01-29 21:12:52 +01001#line 2 "helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Simon Butcheredb7fd92016-05-17 13:35:51 +01005#include <stdlib.h>
6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00008#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02009#else
Rich Evans00ab4702015-02-06 13:43:58 +000010#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010012#define mbedtls_snprintf snprintf
13#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_free free
15#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010016#define mbedtls_time time
17#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010018#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
19#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020020#endif
21
SimonB0269dad2016-02-17 23:34:30 +000022#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
23#include "mbedtls/memory_buffer_alloc.h"
24#endif
25
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000026#ifdef _MSC_VER
27#include <basetsd.h>
28typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000029#define strncasecmp _strnicmp
30#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020032#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000033#endif
34
Paul Bakker19343182013-08-16 13:31:10 +020035#include <string.h>
36
Janos Follath8ca53b52016-10-05 10:57:49 +010037#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
38#include <unistd.h>
39#endif
SimonB0269dad2016-02-17 23:34:30 +000040
Gilles Peskine44498ff2021-01-29 21:17:11 +010041#if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \
42 defined(MBEDTLS_TEST_HOOKS)
43#include "mbedtls/threading.h"
44#define MBEDTLS_TEST_MUTEX_USAGE
45#endif
46
Manuel Pégourié-Gonnard426c2d42020-08-25 11:26:37 +020047/*
48 * Define the two macros
49 *
50 * #define TEST_CF_SECRET(ptr, size)
51 * #define TEST_CF_PUBLIC(ptr, size)
52 *
53 * that can be used in tests to mark a memory area as secret (no branch or
54 * memory access should depend on it) or public (default, only needs to be
55 * marked explicitly when it was derived from secret data).
56 *
57 * Arguments:
58 * - ptr: a pointer to the memory area to be marked
59 * - size: the size in bytes of the memory area
60 *
61 * Implementation:
62 * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can
63 * re-use tools that were designed for checking use of uninitialized memory.
64 * This file contains two implementations: one based on MemorySanitizer, the
65 * other on valgrind's memcheck. If none of them is enabled, dummy macros that
66 * do nothing are defined for convenience.
67 */
Manuel Pégourié-Gonnard40597ce2020-07-28 10:53:06 +020068#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
69#include <sanitizer/msan_interface.h>
70
71/* Use macros to avoid messing up with origin tracking */
72#define TEST_CF_SECRET __msan_allocated_memory
73// void __msan_allocated_memory(const volatile void* data, size_t size);
74#define TEST_CF_PUBLIC __msan_unpoison
75// void __msan_unpoison(const volatile void *a, size_t size);
76
Manuel Pégourié-Gonnard426c2d42020-08-25 11:26:37 +020077#elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
78#include <valgrind/memcheck.h>
79
80#define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
81// VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
82#define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
83// VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
84
85#else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
86 MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
Manuel Pégourié-Gonnard40597ce2020-07-28 10:53:06 +020087
88#define TEST_CF_SECRET(ptr, size)
89#define TEST_CF_PUBLIC(ptr, size)
90
91#endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */
92
SimonB0269dad2016-02-17 23:34:30 +000093/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010094/* Constants */
SimonB0269dad2016-02-17 23:34:30 +000095
SimonB8ca7bc42016-04-17 23:24:50 +010096#define DEPENDENCY_SUPPORTED 0
97#define DEPENDENCY_NOT_SUPPORTED 1
98
99#define KEY_VALUE_MAPPING_FOUND 0
100#define KEY_VALUE_MAPPING_NOT_FOUND -1
101
102#define DISPATCH_TEST_SUCCESS 0
103#define DISPATCH_TEST_FN_NOT_FOUND 1
104#define DISPATCH_INVALID_TEST_DATA 2
105#define DISPATCH_UNSUPPORTED_SUITE 3
SimonB0269dad2016-02-17 23:34:30 +0000106
107
108/*----------------------------------------------------------------------------*/
109/* Macros */
110
111#define TEST_ASSERT( TEST ) \
112 do { \
113 if( ! (TEST) ) \
114 { \
SimonB31a6c492016-05-02 21:32:44 +0100115 test_fail( #TEST, __LINE__, __FILE__ ); \
SimonB0269dad2016-02-17 23:34:30 +0000116 goto exit; \
117 } \
118 } while( 0 )
119
Rich Evans4c091142015-02-02 12:04:10 +0000120#define assert(a) if( !( a ) ) \
121{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +0000123 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +0000125}
126
Ronald Croneb5d0e92020-04-06 10:34:22 +0200127#if defined(__GNUC__)
128/* Test if arg and &(arg)[0] have the same type. This is true if arg is
129 * an array but not if it's a pointer. */
130#define IS_ARRAY_NOT_POINTER( arg ) \
131 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
132 __typeof__( &( arg )[0] ) ) )
133#else
134/* On platforms where we don't know how to implement this check,
135 * omit it. Oh well, a non-portable check is better than nothing. */
136#define IS_ARRAY_NOT_POINTER( arg ) 1
137#endif
138
139/* A compile-time constant with the value 0. If `const_expr` is not a
140 * compile-time constant with a nonzero value, cause a compile-time error. */
141#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homura03c2b8f2020-08-23 00:28:45 +0300142 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Ronald Croneb5d0e92020-04-06 10:34:22 +0200143/* Return the scalar value `value` (possibly promoted). This is a compile-time
144 * constant if `value` is. `condition` must be a compile-time constant.
145 * If `condition` is false, arrange to cause a compile-time error. */
146#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
147 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
148
149#define ARRAY_LENGTH_UNSAFE( array ) \
150 ( sizeof( array ) / sizeof( *( array ) ) )
151/** Return the number of elements of a static or stack array.
152 *
153 * \param array A value of array (not pointer) type.
154 *
155 * \return The number of elements of the array.
156 */
157#define ARRAY_LENGTH( array ) \
158 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
159 ARRAY_LENGTH_UNSAFE( array ) ) )
160
161
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000162/*
163 * 32-bit integer manipulation macros (big endian)
164 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000165#ifndef GET_UINT32_BE
166#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000167{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000168 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
169 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
170 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
171 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000172}
173#endif
174
Paul Bakker5c2364c2012-10-01 14:41:15 +0000175#ifndef PUT_UINT32_BE
176#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000177{ \
178 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
179 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
180 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
181 (b)[(i) + 3] = (unsigned char) ( (n) ); \
182}
183#endif
184
SimonB0269dad2016-02-17 23:34:30 +0000185
186/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100187/* Global variables */
188
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100189
190static struct
191{
192 int failed;
193 const char *test;
194 const char *filename;
195 int line_no;
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100196#if defined(MBEDTLS_TEST_MUTEX_USAGE)
197 const char *mutex_usage_error;
198#endif
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100199}
200test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100201
202
203/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100204/* Helper flags for complex dependencies */
205
206/* Indicates whether we expect mbedtls_entropy_init
207 * to initialize some strong entropy source. */
208#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
209 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
210 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
211 defined(MBEDTLS_HAVEGE_C) || \
212 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
213 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100214#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100215#endif
216
217
218/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000219/* Helper Functions */
220
Simon Butcher638dceb2018-10-03 16:17:41 +0100221void test_fail( const char *test, int line_no, const char* filename )
222{
Gilles Peskined4c9fd12020-08-31 10:21:58 +0200223 if( test_info.failed )
224 {
225 /* We've already recorded the test as having failed. Don't
226 * overwrite any previous information about the failure. */
227 return;
228 }
Simon Butcher638dceb2018-10-03 16:17:41 +0100229 test_info.failed = 1;
230 test_info.test = test;
231 test_info.line_no = line_no;
232 test_info.filename = filename;
233}
234
Janos Follath8ca53b52016-10-05 10:57:49 +0100235#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44b0ab8c22020-07-30 09:02:27 +0200236static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100237{
gufe44b0ab8c22020-07-30 09:02:27 +0200238 int out_fd, dup_fd;
239 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100240
gufe44b0ab8c22020-07-30 09:02:27 +0200241 out_fd = fileno( out_stream );
242 dup_fd = dup( out_fd );
243
244 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100245 {
gufe44b0ab8c22020-07-30 09:02:27 +0200246 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100247 }
248
gufe44b0ab8c22020-07-30 09:02:27 +0200249 path_stream = fopen( path, "w" );
250 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100251 {
gufe44b0ab8c22020-07-30 09:02:27 +0200252 close( dup_fd );
253 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100254 }
255
gufe44b0ab8c22020-07-30 09:02:27 +0200256 fflush( out_stream );
257 if( dup2( fileno( path_stream ), out_fd ) == -1 )
258 {
259 close( dup_fd );
260 fclose( path_stream );
261 return( -1 );
262 }
263
264 fclose( path_stream );
265 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100266}
267
gufe44b0ab8c22020-07-30 09:02:27 +0200268static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100269{
gufe44b0ab8c22020-07-30 09:02:27 +0200270 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100271
gufe44b0ab8c22020-07-30 09:02:27 +0200272 fflush( out_stream );
273 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100274 {
gufe44b0ab8c22020-07-30 09:02:27 +0200275 close( out_fd );
276 close( dup_fd );
277 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100278 }
279
gufe44b0ab8c22020-07-30 09:02:27 +0200280 close( dup_fd );
281 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100282}
Janos Follath8ca53b52016-10-05 10:57:49 +0100283#endif /* __unix__ || __APPLE__ __MACH__ */
284
Simon Butcher638dceb2018-10-03 16:17:41 +0100285int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000286{
287 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000288 int len = strlen( ibuf ) / 2;
SimonB0269dad2016-02-17 23:34:30 +0000289 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000290
Rich Evans4c091142015-02-02 12:04:10 +0000291 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000292 {
293 c = *ibuf++;
294 if( c >= '0' && c <= '9' )
295 c -= '0';
296 else if( c >= 'a' && c <= 'f' )
297 c -= 'a' - 10;
298 else if( c >= 'A' && c <= 'F' )
299 c -= 'A' - 10;
300 else
301 assert( 0 );
302
303 c2 = *ibuf++;
304 if( c2 >= '0' && c2 <= '9' )
305 c2 -= '0';
306 else if( c2 >= 'a' && c2 <= 'f' )
307 c2 -= 'a' - 10;
308 else if( c2 >= 'A' && c2 <= 'F' )
309 c2 -= 'A' - 10;
310 else
311 assert( 0 );
312
313 *obuf++ = ( c << 4 ) | c2;
314 }
315
316 return len;
317}
318
Simon Butcher638dceb2018-10-03 16:17:41 +0100319void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000320{
321 unsigned char l, h;
322
Rich Evans42914452015-02-02 12:09:25 +0000323 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000324 {
Rich Evans42914452015-02-02 12:09:25 +0000325 h = *ibuf / 16;
326 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000327
328 if( h < 10 )
329 *obuf++ = '0' + h;
330 else
331 *obuf++ = 'a' + h - 10;
332
333 if( l < 10 )
334 *obuf++ = '0' + l;
335 else
336 *obuf++ = 'a' + l - 10;
337
338 ++ibuf;
339 len--;
340 }
341}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000342
343/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200344 * Allocate and zeroize a buffer.
345 *
346 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
347 *
348 * For convenience, dies if allocation fails.
349 */
350static unsigned char *zero_alloc( size_t len )
351{
352 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000353 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200354
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200355 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200356 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200357
358 memset( p, 0x00, actual_len );
359
360 return( p );
361}
362
363/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200364 * Allocate and fill a buffer from hex data.
365 *
366 * The buffer is sized exactly as needed. This allows to detect buffer
367 * overruns (including overreads) when running the test suite under valgrind.
368 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200369 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
370 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200371 * For convenience, dies if allocation fails.
372 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100373unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200374{
375 unsigned char *obuf;
376
Rich Evans42914452015-02-02 12:09:25 +0000377 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200378
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200379 if( *olen == 0 )
380 return( zero_alloc( *olen ) );
381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200383 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200384
385 (void) unhexify( obuf, ibuf );
386
387 return( obuf );
388}
389
390/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000391 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000392 * Although predictable and often similar on multiple
393 * runs, this does not result in identical random on
394 * each run. So do not use this if the results of a
395 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000396 *
397 * rng_state shall be NULL.
398 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000399static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000400{
gufe44206cb392020-08-03 17:56:50 +0200401#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000402 size_t i;
403
Paul Bakker9dcc3222011-03-08 14:16:06 +0000404 if( rng_state != NULL )
405 rng_state = NULL;
406
Paul Bakkera3d195c2011-11-27 21:07:34 +0000407 for( i = 0; i < len; ++i )
408 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200409#else
410 if( rng_state != NULL )
411 rng_state = NULL;
412
413 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +0200414#endif /* !OpenBSD && !NetBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000415
416 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000417}
418
419/**
420 * This function only returns zeros
421 *
422 * rng_state shall be NULL.
423 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100424int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000425{
426 if( rng_state != NULL )
427 rng_state = NULL;
428
Paul Bakkera3d195c2011-11-27 21:07:34 +0000429 memset( output, 0, len );
430
Paul Bakker9dcc3222011-03-08 14:16:06 +0000431 return( 0 );
432}
433
434typedef struct
435{
436 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000437 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000438} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000439
440/**
441 * This function returns random based on a buffer it receives.
442 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000443 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100444 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000445 * The number of bytes released from the buffer on each call to
446 * the random function is specified by per_call. (Can be between
447 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000448 *
449 * After the buffer is empty it will return rand();
450 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100451int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000452{
Paul Bakker997bbd12011-03-13 15:45:42 +0000453 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000454 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000455
456 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000457 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000458
Paul Bakkera3d195c2011-11-27 21:07:34 +0000459 use_len = len;
460 if( len > info->length )
461 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000462
Paul Bakkera3d195c2011-11-27 21:07:34 +0000463 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000464 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000465 memcpy( output, info->buf, use_len );
466 info->buf += use_len;
467 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000468 }
469
Paul Bakkera3d195c2011-11-27 21:07:34 +0000470 if( len - use_len > 0 )
471 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
472
473 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000474}
Paul Bakker997bbd12011-03-13 15:45:42 +0000475
476/**
477 * Info structure for the pseudo random function
478 *
479 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000480 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000481 * State( v0, v1 ) should be set to zero.
482 */
483typedef struct
484{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000485 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000486 uint32_t v0, v1;
487} rnd_pseudo_info;
488
489/**
490 * This function returns random based on a pseudo random function.
491 * This means the results should be identical on all systems.
492 * Pseudo random is based on the XTEA encryption algorithm to
493 * generate pseudorandom.
494 *
495 * rng_state shall be a pointer to a rnd_pseudo_info structure.
496 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100497int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000498{
499 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000500 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100501 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000502
503 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000504 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000505
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000506 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000507
508 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000509 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000510 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000511 sum = 0;
512
Paul Bakkera3d195c2011-11-27 21:07:34 +0000513 for( i = 0; i < 32; i++ )
514 {
Rich Evans42914452015-02-02 12:09:25 +0000515 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
516 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000517 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000518 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
519 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000520 }
521
Paul Bakker5c2364c2012-10-01 14:41:15 +0000522 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100523 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000524 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100525 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000526 }
527
Paul Bakkera3d195c2011-11-27 21:07:34 +0000528 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000529}
SimonB0269dad2016-02-17 23:34:30 +0000530
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100531#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine44498ff2021-01-29 21:17:11 +0100532/** Mutex usage verification framework.
533 *
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100534 * The mutex usage verification code below aims to detect bad usage of
535 * Mbed TLS's mutex abstraction layer at runtime. Note that this is solely
536 * about the use of the mutex itself, not about checking whether the mutex
537 * correctly protects whatever it is supposed to protect.
538 *
539 * The normal usage of a mutex is:
540 * ```
541 * digraph mutex_states {
542 * "UNINITIALIZED"; // the initial state
543 * "IDLE";
544 * "FREED";
545 * "LOCKED";
546 * "UNINITIALIZED" -> "IDLE" [label="init"];
547 * "FREED" -> "IDLE" [label="init"];
548 * "IDLE" -> "LOCKED" [label="lock"];
549 * "LOCKED" -> "IDLE" [label="unlock"];
550 * "IDLE" -> "FREED" [label="free"];
551 * }
552 * ```
553 *
554 * All bad transitions that can be unambiguously detected are reported.
555 * An attempt to use an uninitialized mutex cannot be detected in general
556 * since the memory content may happen to denote a valid state. For the same
557 * reason, a double init cannot be detected.
558 * All-bits-zero is the state of a freed mutex, which is distinct from an
559 * initialized mutex, so attempting to use zero-initialized memory as a mutex
560 * without calling the init function is detected.
561 *
562 * If an error is detected, this framework will report what happened and the
563 * test case will be marked as failed. Unfortunately, the error report cannot
564 * indicate the exact location of the problematic call. To locate the error,
565 * use a debugger and set a breakpoint on mbedtls_test_mutex_usage_error().
Gilles Peskine44498ff2021-01-29 21:17:11 +0100566 */
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100567enum value_of_mutex_is_valid
568{
569 MUTEX_FREED = 0, //!< Set by threading_mutex_free_pthread
570 MUTEX_IDLE = 1, //!< Set by threading_mutex_init_pthread and by our unlock
571 MUTEX_LOCKED = 2, //!< Set by our lock
572};
Gilles Peskine44498ff2021-01-29 21:17:11 +0100573
Gilles Peskine44498ff2021-01-29 21:17:11 +0100574typedef struct
575{
576 void (*init)( mbedtls_threading_mutex_t * );
577 void (*free)( mbedtls_threading_mutex_t * );
578 int (*lock)( mbedtls_threading_mutex_t * );
579 int (*unlock)( mbedtls_threading_mutex_t * );
580} mutex_functions_t;
581static mutex_functions_t mutex_functions;
582
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100583static void mbedtls_test_mutex_usage_error( mbedtls_threading_mutex_t *mutex,
584 const char *msg )
585{
586 (void) mutex;
587 if( test_info.mutex_usage_error == NULL )
588 test_info.mutex_usage_error = msg;
589 mbedtls_fprintf( stdout, "[mutex: %s] ", msg );
590 /* Don't mark the test as failed yet. This way, if the test fails later
591 * for a functional reason, the test framework will report the message
592 * and location for this functional reason. If the test passes,
593 * mbedtls_test_mutex_usage_check() will mark it as failed. */
594}
595
Gilles Peskine44498ff2021-01-29 21:17:11 +0100596static void mbedtls_test_wrap_mutex_init( mbedtls_threading_mutex_t *mutex )
597{
598 mutex_functions.init( mutex );
599}
600
601static void mbedtls_test_wrap_mutex_free( mbedtls_threading_mutex_t *mutex )
602{
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100603 switch( mutex->is_valid )
604 {
605 case MUTEX_FREED:
606 mbedtls_test_mutex_usage_error( mutex, "free without init or double free" );
607 break;
608 case MUTEX_IDLE:
609 /* Do nothing. The underlying free function will reset is_valid
610 * to 0. */
611 break;
612 case MUTEX_LOCKED:
613 mbedtls_test_mutex_usage_error( mutex, "free without unlock" );
614 break;
615 default:
616 mbedtls_test_mutex_usage_error( mutex, "corrupted state" );
617 break;
618 }
Gilles Peskine44498ff2021-01-29 21:17:11 +0100619 mutex_functions.free( mutex );
620}
621
622static int mbedtls_test_wrap_mutex_lock( mbedtls_threading_mutex_t *mutex )
623{
624 int ret = mutex_functions.lock( mutex );
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100625 switch( mutex->is_valid )
626 {
627 case MUTEX_FREED:
628 mbedtls_test_mutex_usage_error( mutex, "lock without init" );
629 break;
630 case MUTEX_IDLE:
631 if( ret == 0 )
632 mutex->is_valid = 2;
633 break;
634 case MUTEX_LOCKED:
635 mbedtls_test_mutex_usage_error( mutex, "double lock" );
636 break;
637 default:
638 mbedtls_test_mutex_usage_error( mutex, "corrupted state" );
639 break;
640 }
Gilles Peskine44498ff2021-01-29 21:17:11 +0100641 return( ret );
642}
643
644static int mbedtls_test_wrap_mutex_unlock( mbedtls_threading_mutex_t *mutex )
645{
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100646 int ret = mutex_functions.unlock( mutex );
647 switch( mutex->is_valid )
648 {
649 case MUTEX_FREED:
650 mbedtls_test_mutex_usage_error( mutex, "unlock without init" );
651 break;
652 case MUTEX_IDLE:
653 mbedtls_test_mutex_usage_error( mutex, "unlock without lock" );
654 break;
655 case MUTEX_LOCKED:
656 if( ret == 0 )
657 mutex->is_valid = MUTEX_IDLE;
658 break;
659 default:
660 mbedtls_test_mutex_usage_error( mutex, "corrupted state" );
661 break;
662 }
663 return( ret );
Gilles Peskine44498ff2021-01-29 21:17:11 +0100664}
665
666static void mbedtls_test_mutex_usage_init( void )
667{
668 mutex_functions.init = mbedtls_mutex_init;
669 mutex_functions.free = mbedtls_mutex_free;
670 mutex_functions.lock = mbedtls_mutex_lock;
671 mutex_functions.unlock = mbedtls_mutex_unlock;
672 mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init;
673 mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free;
674 mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock;
675 mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock;
676}
677
Gilles Peskine0abb8e42021-01-29 21:18:09 +0100678static void mbedtls_test_mutex_usage_check( void )
679{
680 if( test_info.mutex_usage_error != NULL && ! test_info.failed )
681 {
682 /* Functionally, the test passed. But there was a mutex usage error,
683 * so mark the test as failed after all. */
684 test_fail( "Mutex usage error", __LINE__, __FILE__ );
685 }
686 test_info.mutex_usage_error = NULL;
687}
688
Gilles Peskine44498ff2021-01-29 21:17:11 +0100689#endif /* MBEDTLS_TEST_MUTEX_USAGE */