blob: 3309173d3991a0ca74ad577ed9d152286d2969e9 [file] [log] [blame]
SimonB15942102016-04-25 21:34:49 +01001#line 1 "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
Manuel Pégourié-Gonnard40597ce2020-07-28 10:53:06 +020041#if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
42#include <sanitizer/msan_interface.h>
43
44/* Use macros to avoid messing up with origin tracking */
45#define TEST_CF_SECRET __msan_allocated_memory
46// void __msan_allocated_memory(const volatile void* data, size_t size);
47#define TEST_CF_PUBLIC __msan_unpoison
48// void __msan_unpoison(const volatile void *a, size_t size);
49
50#else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */
51
52#define TEST_CF_SECRET(ptr, size)
53#define TEST_CF_PUBLIC(ptr, size)
54
55#endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN */
56
SimonB0269dad2016-02-17 23:34:30 +000057/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +010058/* Constants */
SimonB0269dad2016-02-17 23:34:30 +000059
SimonB8ca7bc42016-04-17 23:24:50 +010060#define DEPENDENCY_SUPPORTED 0
61#define DEPENDENCY_NOT_SUPPORTED 1
62
63#define KEY_VALUE_MAPPING_FOUND 0
64#define KEY_VALUE_MAPPING_NOT_FOUND -1
65
66#define DISPATCH_TEST_SUCCESS 0
67#define DISPATCH_TEST_FN_NOT_FOUND 1
68#define DISPATCH_INVALID_TEST_DATA 2
69#define DISPATCH_UNSUPPORTED_SUITE 3
SimonB0269dad2016-02-17 23:34:30 +000070
71
72/*----------------------------------------------------------------------------*/
73/* Macros */
74
75#define TEST_ASSERT( TEST ) \
76 do { \
77 if( ! (TEST) ) \
78 { \
SimonB31a6c492016-05-02 21:32:44 +010079 test_fail( #TEST, __LINE__, __FILE__ ); \
SimonB0269dad2016-02-17 23:34:30 +000080 goto exit; \
81 } \
82 } while( 0 )
83
Rich Evans4c091142015-02-02 12:04:10 +000084#define assert(a) if( !( a ) ) \
85{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +000087 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +000089}
90
Ronald Croneb5d0e92020-04-06 10:34:22 +020091#if defined(__GNUC__)
92/* Test if arg and &(arg)[0] have the same type. This is true if arg is
93 * an array but not if it's a pointer. */
94#define IS_ARRAY_NOT_POINTER( arg ) \
95 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
96 __typeof__( &( arg )[0] ) ) )
97#else
98/* On platforms where we don't know how to implement this check,
99 * omit it. Oh well, a non-portable check is better than nothing. */
100#define IS_ARRAY_NOT_POINTER( arg ) 1
101#endif
102
103/* A compile-time constant with the value 0. If `const_expr` is not a
104 * compile-time constant with a nonzero value, cause a compile-time error. */
105#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homura03c2b8f2020-08-23 00:28:45 +0300106 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Ronald Croneb5d0e92020-04-06 10:34:22 +0200107/* Return the scalar value `value` (possibly promoted). This is a compile-time
108 * constant if `value` is. `condition` must be a compile-time constant.
109 * If `condition` is false, arrange to cause a compile-time error. */
110#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
111 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
112
113#define ARRAY_LENGTH_UNSAFE( array ) \
114 ( sizeof( array ) / sizeof( *( array ) ) )
115/** Return the number of elements of a static or stack array.
116 *
117 * \param array A value of array (not pointer) type.
118 *
119 * \return The number of elements of the array.
120 */
121#define ARRAY_LENGTH( array ) \
122 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
123 ARRAY_LENGTH_UNSAFE( array ) ) )
124
125
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000126/*
127 * 32-bit integer manipulation macros (big endian)
128 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000129#ifndef GET_UINT32_BE
130#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000131{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000132 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
133 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
134 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
135 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000136}
137#endif
138
Paul Bakker5c2364c2012-10-01 14:41:15 +0000139#ifndef PUT_UINT32_BE
140#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000141{ \
142 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
143 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
144 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
145 (b)[(i) + 3] = (unsigned char) ( (n) ); \
146}
147#endif
148
SimonB0269dad2016-02-17 23:34:30 +0000149
150/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100151/* Global variables */
152
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100153
154static struct
155{
156 int failed;
157 const char *test;
158 const char *filename;
159 int line_no;
160}
161test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100162
163
164/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100165/* Helper flags for complex dependencies */
166
167/* Indicates whether we expect mbedtls_entropy_init
168 * to initialize some strong entropy source. */
169#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
170 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
171 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
172 defined(MBEDTLS_HAVEGE_C) || \
173 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
174 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100175#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100176#endif
177
178
179/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000180/* Helper Functions */
181
Simon Butcher638dceb2018-10-03 16:17:41 +0100182void test_fail( const char *test, int line_no, const char* filename )
183{
184 test_info.failed = 1;
185 test_info.test = test;
186 test_info.line_no = line_no;
187 test_info.filename = filename;
188}
189
Janos Follath8ca53b52016-10-05 10:57:49 +0100190#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
gufe44b0ab8c22020-07-30 09:02:27 +0200191static int redirect_output( FILE* out_stream, const char* path )
Janos Follath8ca53b52016-10-05 10:57:49 +0100192{
gufe44b0ab8c22020-07-30 09:02:27 +0200193 int out_fd, dup_fd;
194 FILE* path_stream;
Janos Follath8ca53b52016-10-05 10:57:49 +0100195
gufe44b0ab8c22020-07-30 09:02:27 +0200196 out_fd = fileno( out_stream );
197 dup_fd = dup( out_fd );
198
199 if( dup_fd == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100200 {
gufe44b0ab8c22020-07-30 09:02:27 +0200201 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100202 }
203
gufe44b0ab8c22020-07-30 09:02:27 +0200204 path_stream = fopen( path, "w" );
205 if( path_stream == NULL )
Janos Follath8ca53b52016-10-05 10:57:49 +0100206 {
gufe44b0ab8c22020-07-30 09:02:27 +0200207 close( dup_fd );
208 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100209 }
210
gufe44b0ab8c22020-07-30 09:02:27 +0200211 fflush( out_stream );
212 if( dup2( fileno( path_stream ), out_fd ) == -1 )
213 {
214 close( dup_fd );
215 fclose( path_stream );
216 return( -1 );
217 }
218
219 fclose( path_stream );
220 return( dup_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100221}
222
gufe44b0ab8c22020-07-30 09:02:27 +0200223static int restore_output( FILE* out_stream, int dup_fd )
Janos Follath8ca53b52016-10-05 10:57:49 +0100224{
gufe44b0ab8c22020-07-30 09:02:27 +0200225 int out_fd = fileno( out_stream );
Janos Follath8ca53b52016-10-05 10:57:49 +0100226
gufe44b0ab8c22020-07-30 09:02:27 +0200227 fflush( out_stream );
228 if( dup2( dup_fd, out_fd ) == -1 )
Janos Follath8ca53b52016-10-05 10:57:49 +0100229 {
gufe44b0ab8c22020-07-30 09:02:27 +0200230 close( out_fd );
231 close( dup_fd );
232 return( -1 );
Janos Follath8ca53b52016-10-05 10:57:49 +0100233 }
234
gufe44b0ab8c22020-07-30 09:02:27 +0200235 close( dup_fd );
236 return( 0 );
Simon Butchere0192962016-10-12 23:07:30 +0100237}
Janos Follath8ca53b52016-10-05 10:57:49 +0100238#endif /* __unix__ || __APPLE__ __MACH__ */
239
Simon Butcher638dceb2018-10-03 16:17:41 +0100240int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000241{
242 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000243 int len = strlen( ibuf ) / 2;
SimonB0269dad2016-02-17 23:34:30 +0000244 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000245
Rich Evans4c091142015-02-02 12:04:10 +0000246 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000247 {
248 c = *ibuf++;
249 if( c >= '0' && c <= '9' )
250 c -= '0';
251 else if( c >= 'a' && c <= 'f' )
252 c -= 'a' - 10;
253 else if( c >= 'A' && c <= 'F' )
254 c -= 'A' - 10;
255 else
256 assert( 0 );
257
258 c2 = *ibuf++;
259 if( c2 >= '0' && c2 <= '9' )
260 c2 -= '0';
261 else if( c2 >= 'a' && c2 <= 'f' )
262 c2 -= 'a' - 10;
263 else if( c2 >= 'A' && c2 <= 'F' )
264 c2 -= 'A' - 10;
265 else
266 assert( 0 );
267
268 *obuf++ = ( c << 4 ) | c2;
269 }
270
271 return len;
272}
273
Simon Butcher638dceb2018-10-03 16:17:41 +0100274void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000275{
276 unsigned char l, h;
277
Rich Evans42914452015-02-02 12:09:25 +0000278 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000279 {
Rich Evans42914452015-02-02 12:09:25 +0000280 h = *ibuf / 16;
281 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000282
283 if( h < 10 )
284 *obuf++ = '0' + h;
285 else
286 *obuf++ = 'a' + h - 10;
287
288 if( l < 10 )
289 *obuf++ = '0' + l;
290 else
291 *obuf++ = 'a' + l - 10;
292
293 ++ibuf;
294 len--;
295 }
296}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000297
298/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200299 * Allocate and zeroize a buffer.
300 *
301 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
302 *
303 * For convenience, dies if allocation fails.
304 */
305static unsigned char *zero_alloc( size_t len )
306{
307 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000308 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200309
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200310 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200311 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200312
313 memset( p, 0x00, actual_len );
314
315 return( p );
316}
317
318/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200319 * Allocate and fill a buffer from hex data.
320 *
321 * The buffer is sized exactly as needed. This allows to detect buffer
322 * overruns (including overreads) when running the test suite under valgrind.
323 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200324 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
325 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200326 * For convenience, dies if allocation fails.
327 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100328unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200329{
330 unsigned char *obuf;
331
Rich Evans42914452015-02-02 12:09:25 +0000332 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200333
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200334 if( *olen == 0 )
335 return( zero_alloc( *olen ) );
336
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200337 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200338 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200339
340 (void) unhexify( obuf, ibuf );
341
342 return( obuf );
343}
344
345/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000346 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000347 * Although predictable and often similar on multiple
348 * runs, this does not result in identical random on
349 * each run. So do not use this if the results of a
350 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000351 *
352 * rng_state shall be NULL.
353 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000354static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000355{
gufe44206cb392020-08-03 17:56:50 +0200356#if !defined(__OpenBSD__) && !defined(__NetBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000357 size_t i;
358
Paul Bakker9dcc3222011-03-08 14:16:06 +0000359 if( rng_state != NULL )
360 rng_state = NULL;
361
Paul Bakkera3d195c2011-11-27 21:07:34 +0000362 for( i = 0; i < len; ++i )
363 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200364#else
365 if( rng_state != NULL )
366 rng_state = NULL;
367
368 arc4random_buf( output, len );
gufe44206cb392020-08-03 17:56:50 +0200369#endif /* !OpenBSD && !NetBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000370
371 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000372}
373
374/**
375 * This function only returns zeros
376 *
377 * rng_state shall be NULL.
378 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100379int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000380{
381 if( rng_state != NULL )
382 rng_state = NULL;
383
Paul Bakkera3d195c2011-11-27 21:07:34 +0000384 memset( output, 0, len );
385
Paul Bakker9dcc3222011-03-08 14:16:06 +0000386 return( 0 );
387}
388
389typedef struct
390{
391 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000392 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000393} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000394
395/**
396 * This function returns random based on a buffer it receives.
397 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000398 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100399 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000400 * The number of bytes released from the buffer on each call to
401 * the random function is specified by per_call. (Can be between
402 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000403 *
404 * After the buffer is empty it will return rand();
405 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100406int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000407{
Paul Bakker997bbd12011-03-13 15:45:42 +0000408 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000409 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000410
411 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000412 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000413
Paul Bakkera3d195c2011-11-27 21:07:34 +0000414 use_len = len;
415 if( len > info->length )
416 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000417
Paul Bakkera3d195c2011-11-27 21:07:34 +0000418 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000419 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000420 memcpy( output, info->buf, use_len );
421 info->buf += use_len;
422 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000423 }
424
Paul Bakkera3d195c2011-11-27 21:07:34 +0000425 if( len - use_len > 0 )
426 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
427
428 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000429}
Paul Bakker997bbd12011-03-13 15:45:42 +0000430
431/**
432 * Info structure for the pseudo random function
433 *
434 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000435 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000436 * State( v0, v1 ) should be set to zero.
437 */
438typedef struct
439{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000440 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000441 uint32_t v0, v1;
442} rnd_pseudo_info;
443
444/**
445 * This function returns random based on a pseudo random function.
446 * This means the results should be identical on all systems.
447 * Pseudo random is based on the XTEA encryption algorithm to
448 * generate pseudorandom.
449 *
450 * rng_state shall be a pointer to a rnd_pseudo_info structure.
451 */
Simon Butcher638dceb2018-10-03 16:17:41 +0100452int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000453{
454 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000455 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100456 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000457
458 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000459 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000460
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000461 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000462
463 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000464 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000465 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000466 sum = 0;
467
Paul Bakkera3d195c2011-11-27 21:07:34 +0000468 for( i = 0; i < 32; i++ )
469 {
Rich Evans42914452015-02-02 12:09:25 +0000470 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
471 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000472 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000473 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
474 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000475 }
476
Paul Bakker5c2364c2012-10-01 14:41:15 +0000477 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100478 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000479 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100480 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000481 }
482
Paul Bakkera3d195c2011-11-27 21:07:34 +0000483 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000484}
SimonB0269dad2016-02-17 23:34:30 +0000485