blob: 4c3a235f77dfb231d011df8a002dd3543b850418 [file] [log] [blame]
Gilles Peskinebe807262018-03-13 16:07:01 +01001#line 1 "helpers.function"
SimonB8bcd5492016-02-17 23:34:30 +00002/*----------------------------------------------------------------------------*/
3/* Headers */
4
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00006#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02007#else
Rich Evans00ab4702015-02-06 13:43:58 +00008#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009#define mbedtls_printf printf
10#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020011#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#define mbedtls_free free
13#define mbedtls_exit exit
14#define mbedtls_fprintf fprintf
15#define mbedtls_printf printf
16#define mbedtls_snprintf snprintf
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020017#endif
18
SimonB8bcd5492016-02-17 23:34:30 +000019#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
20#include "mbedtls/memory_buffer_alloc.h"
21#endif
22
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000023#ifdef _MSC_VER
24#include <basetsd.h>
25typedef UINT32 uint32_t;
Nicholas Wilson44ea0112015-11-14 13:09:01 +000026#define strncasecmp _strnicmp
27#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000028#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020029#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000030#endif
31
Rich Evans3d62e722015-02-03 11:48:59 +000032#include <stdio.h>
Paul Bakker19343182013-08-16 13:31:10 +020033#include <stdlib.h>
34#include <string.h>
35
SimonB8bcd5492016-02-17 23:34:30 +000036
37/*----------------------------------------------------------------------------*/
38/* Global variables */
39
40static int test_errors = 0;
41
42
43/*----------------------------------------------------------------------------*/
44/* Macros */
45
46#define TEST_ASSERT( TEST ) \
47 do { \
48 if( ! (TEST) ) \
49 { \
50 test_fail( #TEST ); \
51 goto exit; \
52 } \
53 } while( 0 )
54
Rich Evans4c091142015-02-02 12:04:10 +000055#define assert(a) if( !( a ) ) \
56{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +000058 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +000060}
61
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000062/*
63 * 32-bit integer manipulation macros (big endian)
64 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000065#ifndef GET_UINT32_BE
66#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000067{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
69 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
70 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
71 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000072}
73#endif
74
Paul Bakker5c2364c2012-10-01 14:41:15 +000075#ifndef PUT_UINT32_BE
76#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000077{ \
78 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
79 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
80 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
81 (b)[(i) + 3] = (unsigned char) ( (n) ); \
82}
83#endif
84
Hanno Becker66580d22017-09-08 10:06:41 +010085/* Helper flags for complex dependencies */
86
87/* Indicates whether we expect mbedtls_entropy_init
88 * to initialize some strong entropy source. */
Hanno Beckerd2cc7ce2017-09-08 10:47:33 +010089#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
90 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
91 defined(MBEDTLS_HAVEGE_C) || \
92 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) )
Hanno Becker66580d22017-09-08 10:06:41 +010093#define ENTROPY_HAVE_STRONG
94#endif
95
SimonB8bcd5492016-02-17 23:34:30 +000096/*----------------------------------------------------------------------------*/
97/* Helper Functions */
98
Rich Evans4c091142015-02-02 12:04:10 +000099static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +0000100{
101 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000102 int len = strlen( ibuf ) / 2;
SimonB8bcd5492016-02-17 23:34:30 +0000103 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000104
Rich Evans4c091142015-02-02 12:04:10 +0000105 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000106 {
107 c = *ibuf++;
108 if( c >= '0' && c <= '9' )
109 c -= '0';
110 else if( c >= 'a' && c <= 'f' )
111 c -= 'a' - 10;
112 else if( c >= 'A' && c <= 'F' )
113 c -= 'A' - 10;
114 else
115 assert( 0 );
116
117 c2 = *ibuf++;
118 if( c2 >= '0' && c2 <= '9' )
119 c2 -= '0';
120 else if( c2 >= 'a' && c2 <= 'f' )
121 c2 -= 'a' - 10;
122 else if( c2 >= 'A' && c2 <= 'F' )
123 c2 -= 'A' - 10;
124 else
125 assert( 0 );
126
127 *obuf++ = ( c << 4 ) | c2;
128 }
129
130 return len;
131}
132
Rich Evans42914452015-02-02 12:09:25 +0000133static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000134{
135 unsigned char l, h;
136
Rich Evans42914452015-02-02 12:09:25 +0000137 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000138 {
Rich Evans42914452015-02-02 12:09:25 +0000139 h = *ibuf / 16;
140 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000141
142 if( h < 10 )
143 *obuf++ = '0' + h;
144 else
145 *obuf++ = 'a' + h - 10;
146
147 if( l < 10 )
148 *obuf++ = '0' + l;
149 else
150 *obuf++ = 'a' + l - 10;
151
152 ++ibuf;
153 len--;
154 }
155}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000156
157/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200158 * Allocate and zeroize a buffer.
159 *
160 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
161 *
162 * For convenience, dies if allocation fails.
163 */
164static unsigned char *zero_alloc( size_t len )
165{
166 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000167 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200168
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200169 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200170 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200171
172 memset( p, 0x00, actual_len );
173
174 return( p );
175}
176
177/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200178 * Allocate and fill a buffer from hex data.
179 *
180 * The buffer is sized exactly as needed. This allows to detect buffer
181 * overruns (including overreads) when running the test suite under valgrind.
182 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200183 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
184 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200185 * For convenience, dies if allocation fails.
186 */
187static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
188{
189 unsigned char *obuf;
190
Rich Evans42914452015-02-02 12:09:25 +0000191 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200192
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200193 if( *olen == 0 )
194 return( zero_alloc( *olen ) );
195
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200196 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200197 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200198
199 (void) unhexify( obuf, ibuf );
200
201 return( obuf );
202}
203
204/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000205 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000206 * Although predictable and often similar on multiple
207 * runs, this does not result in identical random on
208 * each run. So do not use this if the results of a
209 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000210 *
211 * rng_state shall be NULL.
212 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000213static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000214{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200215#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000216 size_t i;
217
Paul Bakker9dcc3222011-03-08 14:16:06 +0000218 if( rng_state != NULL )
219 rng_state = NULL;
220
Paul Bakkera3d195c2011-11-27 21:07:34 +0000221 for( i = 0; i < len; ++i )
222 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200223#else
224 if( rng_state != NULL )
225 rng_state = NULL;
226
227 arc4random_buf( output, len );
228#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000229
230 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000231}
232
233/**
234 * This function only returns zeros
235 *
236 * rng_state shall be NULL.
237 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000238static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000239{
240 if( rng_state != NULL )
241 rng_state = NULL;
242
Paul Bakkera3d195c2011-11-27 21:07:34 +0000243 memset( output, 0, len );
244
Paul Bakker9dcc3222011-03-08 14:16:06 +0000245 return( 0 );
246}
247
248typedef struct
249{
250 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000251 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000252} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000253
254/**
255 * This function returns random based on a buffer it receives.
256 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000257 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +0100258 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000259 * The number of bytes released from the buffer on each call to
260 * the random function is specified by per_call. (Can be between
261 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000262 *
263 * After the buffer is empty it will return rand();
264 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000265static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000266{
Paul Bakker997bbd12011-03-13 15:45:42 +0000267 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000268 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000269
270 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000271 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000272
Paul Bakkera3d195c2011-11-27 21:07:34 +0000273 use_len = len;
274 if( len > info->length )
275 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000276
Paul Bakkera3d195c2011-11-27 21:07:34 +0000277 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000278 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000279 memcpy( output, info->buf, use_len );
280 info->buf += use_len;
281 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000282 }
283
Paul Bakkera3d195c2011-11-27 21:07:34 +0000284 if( len - use_len > 0 )
285 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
286
287 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000288}
Paul Bakker997bbd12011-03-13 15:45:42 +0000289
290/**
291 * Info structure for the pseudo random function
292 *
293 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000294 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000295 * State( v0, v1 ) should be set to zero.
296 */
297typedef struct
298{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000299 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000300 uint32_t v0, v1;
301} rnd_pseudo_info;
302
303/**
304 * This function returns random based on a pseudo random function.
305 * This means the results should be identical on all systems.
306 * Pseudo random is based on the XTEA encryption algorithm to
307 * generate pseudorandom.
308 *
309 * rng_state shall be a pointer to a rnd_pseudo_info structure.
310 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000311static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000312{
313 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000314 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100315 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000316
317 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000318 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000319
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000320 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000321
322 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000323 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000324 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000325 sum = 0;
326
Paul Bakkera3d195c2011-11-27 21:07:34 +0000327 for( i = 0; i < 32; i++ )
328 {
Rich Evans42914452015-02-02 12:09:25 +0000329 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
330 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000331 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000332 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
333 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000334 }
335
Paul Bakker5c2364c2012-10-01 14:41:15 +0000336 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100337 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000338 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100339 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000340 }
341
Paul Bakkera3d195c2011-11-27 21:07:34 +0000342 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000343}
SimonB8bcd5492016-02-17 23:34:30 +0000344
345static void test_fail( const char *test )
346{
347 test_errors++;
348 if( test_errors == 1 )
349 mbedtls_printf( "FAILED\n" );
350 mbedtls_printf( " %s\n", test );
351}
352