blob: 2d1e38c233e08c12c4a1a490f19a144c03ed7b43 [file] [log] [blame]
Azim Khanec024482017-05-09 17:20:21 +01001#line 2 "suites/helpers.function"
SimonB0269dad2016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Ronald Cron4b8b1992020-06-09 13:52:23 +02005#include <test/macros.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02006#include <test/helpers.h>
Ronald Cron4b8b1992020-06-09 13:52:23 +02007
Simon Butcheredb7fd92016-05-17 13:35:51 +01008#include <stdlib.h>
9
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020012#else
Rich Evans00ab4702015-02-06 13:43:58 +000013#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#define mbedtls_fprintf fprintf
Simon Butcher25731362016-09-30 13:11:29 +010015#define mbedtls_snprintf snprintf
16#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#define mbedtls_free free
18#define mbedtls_exit exit
Simon Butcherb2d5dd12016-04-27 13:35:37 +010019#define mbedtls_time time
20#define mbedtls_time_t time_t
Janos Follath55abc212016-04-18 18:18:48 +010021#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
22#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020023#endif
24
SimonB0269dad2016-02-17 23:34:30 +000025#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
26#include "mbedtls/memory_buffer_alloc.h"
27#endif
28
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029#if defined(MBEDTLS_CHECK_PARAMS)
30#include "mbedtls/platform_util.h"
31#include <setjmp.h>
32#endif
33
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000034#ifdef _MSC_VER
35#include <basetsd.h>
Azim Khan0fa35042018-06-22 11:34:33 +010036typedef UINT8 uint8_t;
37typedef INT32 int32_t;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038typedef UINT32 uint32_t;
Nicholas Wilson733676b2015-11-14 13:09:01 +000039#define strncasecmp _strnicmp
40#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000041#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020042#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043#endif
44
Paul Bakker19343182013-08-16 13:31:10 +020045#include <string.h>
46
Janos Follath8ca53b52016-10-05 10:57:49 +010047#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
48#include <unistd.h>
Nicholas Wilson2682edf2017-12-05 12:08:15 +000049#include <strings.h>
Janos Follath8ca53b52016-10-05 10:57:49 +010050#endif
SimonB0269dad2016-02-17 23:34:30 +000051
Azim Khand30ca132017-06-09 04:32:58 +010052/* Type for Hex parameters */
Azim Khan5fcca462018-06-29 11:05:32 +010053typedef struct data_tag
Azim Khand30ca132017-06-09 04:32:58 +010054{
55 uint8_t * x;
56 uint32_t len;
Azim Khan5fcca462018-06-29 11:05:32 +010057} data_t;
Azim Khand30ca132017-06-09 04:32:58 +010058
SimonB0269dad2016-02-17 23:34:30 +000059/*----------------------------------------------------------------------------*/
Azim Khan62a5d7d2018-06-29 10:02:54 +010060/* Status and error constants */
SimonB0269dad2016-02-17 23:34:30 +000061
Azim Khan62a5d7d2018-06-29 10:02:54 +010062#define DEPENDENCY_SUPPORTED 0 /* Dependency supported by build */
63#define KEY_VALUE_MAPPING_FOUND 0 /* Integer expression found */
64#define DISPATCH_TEST_SUCCESS 0 /* Test dispatch successful */
SimonB8ca7bc42016-04-17 23:24:50 +010065
Azim Khan62a5d7d2018-06-29 10:02:54 +010066#define KEY_VALUE_MAPPING_NOT_FOUND -1 /* Integer expression not found */
67#define DEPENDENCY_NOT_SUPPORTED -2 /* Dependency not supported */
68#define DISPATCH_TEST_FN_NOT_FOUND -3 /* Test function not found */
69#define DISPATCH_INVALID_TEST_DATA -4 /* Invalid test parameter type.
70 Only int, string, binary data
71 and integer expressions are
72 allowed */
73#define DISPATCH_UNSUPPORTED_SUITE -5 /* Test suite not supported by the
74 build */
SimonB0269dad2016-02-17 23:34:30 +000075
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076typedef enum
77{
78 PARAMFAIL_TESTSTATE_IDLE = 0, /* No parameter failure call test */
79 PARAMFAIL_TESTSTATE_PENDING, /* Test call to the parameter failure
80 * is pending */
81 PARAMFAIL_TESTSTATE_CALLED /* The test call to the parameter
82 * failure function has been made */
83} paramfail_test_state_t;
84
SimonB0269dad2016-02-17 23:34:30 +000085
86/*----------------------------------------------------------------------------*/
87/* Macros */
88
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089/**
90 * \brief This macro tests the expression passed to it as a test step or
91 * individual test in a test case.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020092 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * It allows a library function to return a value and return an error
94 * code that can be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020095 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050096 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
97 * callback, MBEDTLS_PARAM_FAILED(), will be assumed to be a test
98 * failure.
Gilles Peskine8954d0c2018-09-27 13:51:25 +020099 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * This macro is not suitable for negative parameter validation tests,
101 * as it assumes the test step will not create an error.
102 *
Jaeden Amero67ea2c52019-02-11 12:05:54 +0000103 * Failing the test means:
104 * - Mark this test case as failed.
105 * - Print a message identifying the failure.
106 * - Jump to the \c exit label.
107 *
108 * This macro expands to an instruction, not an expression.
109 * It may jump to the \c exit label.
110 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * \param TEST The test expression to be tested.
Gilles Peskine8954d0c2018-09-27 13:51:25 +0200112 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113#define TEST_ASSERT( TEST ) \
114 do { \
115 if( ! (TEST) ) \
116 { \
117 test_fail( #TEST, __LINE__, __FILE__ ); \
118 goto exit; \
119 } \
SimonB0269dad2016-02-17 23:34:30 +0000120 } while( 0 )
121
Gilles Peskine5f7aeee2018-12-17 23:26:52 +0100122/** Evaluate two expressions and fail the test case if they have different
123 * values.
124 *
125 * \param expr1 An expression to evaluate.
126 * \param expr2 The expected value of \p expr1. This can be any
127 * expression, but it is typically a constant.
128 */
129#define TEST_EQUAL( expr1, expr2 ) \
130 TEST_ASSERT( ( expr1 ) == ( expr2 ) )
131
Gilles Peskineb75125c2018-09-27 13:52:16 +0200132/** Allocate memory dynamically and fail the test case if this fails.
133 *
134 * You must set \p pointer to \c NULL before calling this macro and
135 * put `mbedtls_free( pointer )` in the test's cleanup code.
136 *
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100137 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200138 * This is usually what we want in tests since API functions are
139 * supposed to accept null pointers when a buffer size is zero.
140 *
141 * This macro expands to an instruction, not an expression.
142 * It may jump to the \c exit label.
143 *
144 * \param pointer An lvalue where the address of the allocated buffer
145 * will be stored.
146 * This expression may be evaluated multiple times.
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100147 * \param length Number of elements to allocate.
Gilles Peskineb75125c2018-09-27 13:52:16 +0200148 * This expression may be evaluated multiple times.
149 *
150 */
Gilles Peskine7f6e3a82018-11-30 18:51:45 +0100151#define ASSERT_ALLOC( pointer, length ) \
152 do \
153 { \
154 TEST_ASSERT( ( pointer ) == NULL ); \
155 if( ( length ) != 0 ) \
156 { \
157 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
158 ( length ) ); \
159 TEST_ASSERT( ( pointer ) != NULL ); \
160 } \
161 } \
Gilles Peskineb75125c2018-09-27 13:52:16 +0200162 while( 0 )
163
Gilles Peskine292672e2020-01-21 16:20:04 +0100164/** Allocate memory dynamically. If the allocation fails, skip the test case.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100165 *
166 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
Gilles Peskine292672e2020-01-21 16:20:04 +0100167 * fails, it marks the test as skipped rather than failed.
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100168 */
169#define ASSERT_ALLOC_WEAK( pointer, length ) \
170 do \
171 { \
172 TEST_ASSERT( ( pointer ) == NULL ); \
173 if( ( length ) != 0 ) \
174 { \
175 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
176 ( length ) ); \
Gilles Peskine292672e2020-01-21 16:20:04 +0100177 TEST_ASSUME( ( pointer ) != NULL ); \
Gilles Peskine2cd8ecc2019-03-04 17:13:43 +0100178 } \
179 } \
180 while( 0 )
181
Gilles Peskine3c225962018-09-27 13:56:31 +0200182/** Compare two buffers and fail the test case if they differ.
183 *
184 * This macro expands to an instruction, not an expression.
185 * It may jump to the \c exit label.
186 *
187 * \param p1 Pointer to the start of the first buffer.
188 * \param size1 Size of the first buffer in bytes.
189 * This expression may be evaluated multiple times.
190 * \param p2 Pointer to the start of the second buffer.
191 * \param size2 Size of the second buffer in bytes.
192 * This expression may be evaluated multiple times.
193 */
194#define ASSERT_COMPARE( p1, size1, p2, size2 ) \
195 do \
196 { \
197 TEST_ASSERT( ( size1 ) == ( size2 ) ); \
198 if( ( size1 ) != 0 ) \
199 TEST_ASSERT( memcmp( ( p1 ), ( p2 ), ( size1 ) ) == 0 ); \
200 } \
201 while( 0 )
202
Hanno Beckere69d0152019-07-05 13:31:30 +0100203/**
204 * \brief This macro tests the expression passed to it and skips the
205 * running test if it doesn't evaluate to 'true'.
206 *
207 * \param TEST The test expression to be tested.
208 */
209#define TEST_ASSUME( TEST ) \
210 do { \
211 if( ! (TEST) ) \
212 { \
213 test_skip( #TEST, __LINE__, __FILE__ ); \
214 goto exit; \
215 } \
216 } while( 0 )
217
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218#if defined(MBEDTLS_CHECK_PARAMS) && !defined(MBEDTLS_PARAM_FAILED_ALT)
219/**
220 * \brief This macro tests the statement passed to it as a test step or
221 * individual test in a test case. The macro assumes the test will fail
222 * and will generate an error.
223 *
224 * It allows a library function to return a value and tests the return
225 * code on return to confirm the given error code was returned.
226 *
227 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
228 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
229 * expected failure, and the test will pass.
230 *
231 * This macro is intended for negative parameter validation tests,
232 * where the failing function may return an error value or call
233 * MBEDTLS_PARAM_FAILED() to indicate the error.
234 *
235 * \param PARAM_ERROR_VALUE The expected error code.
236 *
237 * \param TEST The test expression to be tested.
238 */
239#define TEST_INVALID_PARAM_RET( PARAM_ERR_VALUE, TEST ) \
240 do { \
241 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_PENDING; \
242 if( (TEST) != (PARAM_ERR_VALUE) || \
243 test_info.paramfail_test_state != PARAMFAIL_TESTSTATE_CALLED ) \
244 { \
245 test_fail( #TEST, __LINE__, __FILE__ ); \
246 goto exit; \
247 } \
248 } while( 0 )
249
250/**
251 * \brief This macro tests the statement passed to it as a test step or
252 * individual test in a test case. The macro assumes the test will fail
253 * and will generate an error.
254 *
255 * It assumes the library function under test cannot return a value and
256 * assumes errors can only be indicated byt calls to
257 * MBEDTLS_PARAM_FAILED().
258 *
259 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
260 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
261 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
262 * can be made.
263 *
264 * This macro is intended for negative parameter validation tests,
265 * where the failing function can only return an error by calling
266 * MBEDTLS_PARAM_FAILED() to indicate the error.
267 *
268 * \param TEST The test expression to be tested.
269 */
270#define TEST_INVALID_PARAM( TEST ) \
271 do { \
272 memcpy(jmp_tmp, param_fail_jmp, sizeof(jmp_buf)); \
273 if( setjmp( param_fail_jmp ) == 0 ) \
274 { \
275 TEST; \
276 test_fail( #TEST, __LINE__, __FILE__ ); \
277 goto exit; \
278 } \
279 memcpy(param_fail_jmp, jmp_tmp, sizeof(jmp_buf)); \
280 } while( 0 )
281#endif /* MBEDTLS_CHECK_PARAMS && !MBEDTLS_PARAM_FAILED_ALT */
282
283/**
284 * \brief This macro tests the statement passed to it as a test step or
285 * individual test in a test case. The macro assumes the test will not fail.
286 *
287 * It assumes the library function under test cannot return a value and
288 * assumes errors can only be indicated by calls to
289 * MBEDTLS_PARAM_FAILED().
290 *
291 * When MBEDTLS_CHECK_PARAMS is enabled, calls to the parameter failure
292 * callback, MBEDTLS_PARAM_FAILED(), are assumed to indicate the
293 * expected failure. If MBEDTLS_CHECK_PARAMS is not enabled, no test
294 * can be made.
295 *
296 * This macro is intended to test that functions returning void
297 * accept all of the parameter values they're supposed to accept - eg
298 * that they don't call MBEDTLS_PARAM_FAILED() when a parameter
299 * that's allowed to be NULL happens to be NULL.
300 *
301 * Note: for functions that return something other that void,
302 * checking that they accept all the parameters they're supposed to
303 * accept is best done by using TEST_ASSERT() and checking the return
304 * value as well.
305 *
306 * Note: this macro is available even when #MBEDTLS_CHECK_PARAMS is
307 * disabled, as it makes sense to check that the functions accept all
308 * legal values even if this option is disabled - only in that case,
309 * the test is more about whether the function segfaults than about
310 * whether it invokes MBEDTLS_PARAM_FAILED().
311 *
312 * \param TEST The test expression to be tested.
313 */
314#define TEST_VALID_PARAM( TEST ) \
315 TEST_ASSERT( ( TEST, 1 ) );
316
Gilles Peskine28405302018-09-27 13:52:16 +0200317/** Allocate memory dynamically and fail the test case if this fails.
318 *
319 * You must set \p pointer to \c NULL before calling this macro and
320 * put `mbedtls_free( pointer )` in the test's cleanup code.
321 *
Gilles Peskine6608e712018-11-30 18:51:45 +0100322 * If \p length is zero, the resulting \p pointer will be \c NULL.
Gilles Peskine28405302018-09-27 13:52:16 +0200323 * This is usually what we want in tests since API functions are
324 * supposed to accept null pointers when a buffer size is zero.
325 *
326 * This macro expands to an instruction, not an expression.
327 * It may jump to the \c exit label.
328 *
329 * \param pointer An lvalue where the address of the allocated buffer
330 * will be stored.
331 * This expression may be evaluated multiple times.
Gilles Peskine6608e712018-11-30 18:51:45 +0100332 * \param length Number of elements to allocate.
Gilles Peskine28405302018-09-27 13:52:16 +0200333 * This expression may be evaluated multiple times.
334 *
335 */
Gilles Peskine6608e712018-11-30 18:51:45 +0100336#define ASSERT_ALLOC( pointer, length ) \
337 do \
338 { \
339 TEST_ASSERT( ( pointer ) == NULL ); \
340 if( ( length ) != 0 ) \
341 { \
342 ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
343 ( length ) ); \
344 TEST_ASSERT( ( pointer ) != NULL ); \
345 } \
346 } \
Gilles Peskine28405302018-09-27 13:52:16 +0200347 while( 0 )
348
SimonB0269dad2016-02-17 23:34:30 +0000349/*----------------------------------------------------------------------------*/
SimonB8ca7bc42016-04-17 23:24:50 +0100350/* Global variables */
351
Hanno Beckere69d0152019-07-05 13:31:30 +0100352typedef enum
353{
354 TEST_RESULT_SUCCESS = 0,
355 TEST_RESULT_FAILED,
356 TEST_RESULT_SKIPPED
357} test_result_t;
358
Gilles Peskine47b75402019-09-16 15:12:51 +0200359typedef struct
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100360{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500361 paramfail_test_state_t paramfail_test_state;
Hanno Beckere69d0152019-07-05 13:31:30 +0100362 test_result_t result;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100363 const char *test;
364 const char *filename;
365 int line_no;
Gilles Peskine56055912019-03-01 14:26:30 +0100366 unsigned long step;
Andres Amaya Garcia3f50f512017-10-01 16:42:29 +0100367}
Gilles Peskine47b75402019-09-16 15:12:51 +0200368test_info_t;
369static test_info_t test_info;
SimonB8ca7bc42016-04-17 23:24:50 +0100370
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500371#if defined(MBEDTLS_CHECK_PARAMS)
372jmp_buf param_fail_jmp;
373jmp_buf jmp_tmp;
374#endif
375
SimonB8ca7bc42016-04-17 23:24:50 +0100376/*----------------------------------------------------------------------------*/
Hanno Becker47deec42017-07-24 12:27:09 +0100377/* Helper flags for complex dependencies */
378
379/* Indicates whether we expect mbedtls_entropy_init
380 * to initialize some strong entropy source. */
381#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
382 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
383 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
384 defined(MBEDTLS_HAVEGE_C) || \
385 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) || \
386 defined(ENTROPY_NV_SEED) ) )
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100387#define ENTROPY_HAVE_STRONG
Hanno Becker47deec42017-07-24 12:27:09 +0100388#endif
389
390
391/*----------------------------------------------------------------------------*/
SimonB0269dad2016-02-17 23:34:30 +0000392/* Helper Functions */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393
Gilles Peskine56055912019-03-01 14:26:30 +0100394/** Set the test step number for failure reports.
395 *
396 * Call this function to display "step NNN" in addition to the line number
397 * and file name if a test fails. Typically the "step number" is the index
398 * of a for loop but it can be whatever you want.
399 *
400 * \param step The step number to report.
401 */
402void test_set_step( unsigned long step )
403{
404 test_info.step = step;
405}
406
Simon Butcherecff2192018-10-03 16:17:41 +0100407void test_fail( const char *test, int line_no, const char* filename )
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500408{
Hanno Beckere69d0152019-07-05 13:31:30 +0100409 test_info.result = TEST_RESULT_FAILED;
410 test_info.test = test;
411 test_info.line_no = line_no;
412 test_info.filename = filename;
413}
414
415void test_skip( const char *test, int line_no, const char* filename )
416{
417 test_info.result = TEST_RESULT_SKIPPED;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500418 test_info.test = test;
419 test_info.line_no = line_no;
420 test_info.filename = filename;
421}
422
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423#if defined(MBEDTLS_CHECK_PARAMS)
424void mbedtls_param_failed( const char *failure_condition,
425 const char *file,
426 int line )
427{
428 /* If we are testing the callback function... */
429 if( test_info.paramfail_test_state == PARAMFAIL_TESTSTATE_PENDING )
430 {
431 test_info.paramfail_test_state = PARAMFAIL_TESTSTATE_CALLED;
432 }
433 else
434 {
435 /* ...else we treat this as an error */
436
437 /* Record the location of the failure, but not as a failure yet, in case
438 * it was part of the test */
439 test_fail( failure_condition, line, file );
Hanno Beckere69d0152019-07-05 13:31:30 +0100440 test_info.result = TEST_RESULT_SUCCESS;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500441
442 longjmp( param_fail_jmp, 1 );
443 }
444}
445#endif
446
Janos Follath8ca53b52016-10-05 10:57:49 +0100447#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
448static int redirect_output( FILE** out_stream, const char* path )
449{
450 int stdout_fd = dup( fileno( *out_stream ) );
451
452 if( stdout_fd == -1 )
453 {
454 return -1;
455 }
456
457 fflush( *out_stream );
458 fclose( *out_stream );
459 *out_stream = fopen( path, "w" );
460
461 if( *out_stream == NULL )
462 {
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 close( stdout_fd );
Janos Follath8ca53b52016-10-05 10:57:49 +0100464 return -1;
465 }
466
467 return stdout_fd;
468}
469
470static int restore_output( FILE** out_stream, int old_fd )
471{
472 fflush( *out_stream );
473 fclose( *out_stream );
474
475 *out_stream = fdopen( old_fd, "w" );
476 if( *out_stream == NULL )
477 {
478 return -1;
479 }
480
481 return 0;
482}
Simon Butchere0192962016-10-12 23:07:30 +0100483
Janos Follathe709f7c2016-10-13 11:26:29 +0100484static void close_output( FILE* out_stream )
Simon Butchere0192962016-10-12 23:07:30 +0100485{
Janos Follathe709f7c2016-10-13 11:26:29 +0100486 fclose( out_stream );
Simon Butchere0192962016-10-12 23:07:30 +0100487}
Janos Follath8ca53b52016-10-05 10:57:49 +0100488#endif /* __unix__ || __APPLE__ __MACH__ */
489
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200490/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000491 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000492 * Although predictable and often similar on multiple
493 * runs, this does not result in identical random on
494 * each run. So do not use this if the results of a
495 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000496 *
497 * rng_state shall be NULL.
498 */
Ronald Cronf91c4952020-05-27 16:22:17 +0200499int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000500{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200501#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000502 size_t i;
503
Paul Bakker9dcc3222011-03-08 14:16:06 +0000504 if( rng_state != NULL )
505 rng_state = NULL;
506
Paul Bakkera3d195c2011-11-27 21:07:34 +0000507 for( i = 0; i < len; ++i )
508 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200509#else
510 if( rng_state != NULL )
511 rng_state = NULL;
512
513 arc4random_buf( output, len );
514#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000515
516 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000517}
518
519/**
520 * This function only returns zeros
521 *
522 * rng_state shall be NULL.
523 */
Simon Butcherecff2192018-10-03 16:17:41 +0100524int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000525{
526 if( rng_state != NULL )
527 rng_state = NULL;
528
Paul Bakkera3d195c2011-11-27 21:07:34 +0000529 memset( output, 0, len );
530
Paul Bakker9dcc3222011-03-08 14:16:06 +0000531 return( 0 );
532}
533
534typedef struct
535{
536 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000537 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000538} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000539
540/**
541 * This function returns random based on a buffer it receives.
542 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000543 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100544 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000545 * The number of bytes released from the buffer on each call to
546 * the random function is specified by per_call. (Can be between
547 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000548 *
549 * After the buffer is empty it will return rand();
550 */
Simon Butcherecff2192018-10-03 16:17:41 +0100551int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000552{
Paul Bakker997bbd12011-03-13 15:45:42 +0000553 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000554 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000555
556 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000557 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000558
Paul Bakkera3d195c2011-11-27 21:07:34 +0000559 use_len = len;
560 if( len > info->length )
561 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000562
Paul Bakkera3d195c2011-11-27 21:07:34 +0000563 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000564 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000565 memcpy( output, info->buf, use_len );
566 info->buf += use_len;
567 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000568 }
569
Paul Bakkera3d195c2011-11-27 21:07:34 +0000570 if( len - use_len > 0 )
571 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
572
573 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000574}
Paul Bakker997bbd12011-03-13 15:45:42 +0000575
576/**
577 * Info structure for the pseudo random function
578 *
579 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000580 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000581 * State( v0, v1 ) should be set to zero.
582 */
583typedef struct
584{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000585 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000586 uint32_t v0, v1;
587} rnd_pseudo_info;
588
589/**
590 * This function returns random based on a pseudo random function.
591 * This means the results should be identical on all systems.
592 * Pseudo random is based on the XTEA encryption algorithm to
593 * generate pseudorandom.
594 *
595 * rng_state shall be a pointer to a rnd_pseudo_info structure.
596 */
Simon Butcherecff2192018-10-03 16:17:41 +0100597int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000598{
599 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000600 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100601 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000602
603 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000604 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000605
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000606 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000607
608 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000609 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000610 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000611 sum = 0;
612
Paul Bakkera3d195c2011-11-27 21:07:34 +0000613 for( i = 0; i < 32; i++ )
614 {
Rich Evans42914452015-02-02 12:09:25 +0000615 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
616 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000617 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000618 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
619 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000620 }
621
Paul Bakker5c2364c2012-10-01 14:41:15 +0000622 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100623 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000624 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100625 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000626 }
627
Paul Bakkera3d195c2011-11-27 21:07:34 +0000628 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000629}