blob: cad7072cea90f5ed30a9b44ad76f33102ae4f0b7 [file] [log] [blame]
SimonB8bcd5492016-02-17 23:34:30 +00001/*----------------------------------------------------------------------------*/
2/* Headers */
3
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00005#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02006#else
Rich Evans00ab4702015-02-06 13:43:58 +00007#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#define mbedtls_printf printf
9#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020010#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#define mbedtls_free free
12#define mbedtls_exit exit
13#define mbedtls_fprintf fprintf
14#define mbedtls_printf printf
15#define mbedtls_snprintf snprintf
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020016#endif
17
SimonB8bcd5492016-02-17 23:34:30 +000018#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
19#include "mbedtls/memory_buffer_alloc.h"
20#endif
21
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000022#ifdef _MSC_VER
23#include <basetsd.h>
24typedef UINT32 uint32_t;
Nicholas Wilson44ea0112015-11-14 13:09:01 +000025#define strncasecmp _strnicmp
26#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000027#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020028#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000029#endif
30
Rich Evans3d62e722015-02-03 11:48:59 +000031#include <stdio.h>
Paul Bakker19343182013-08-16 13:31:10 +020032#include <stdlib.h>
33#include <string.h>
34
SimonB8bcd5492016-02-17 23:34:30 +000035
36/*----------------------------------------------------------------------------*/
37/* Global variables */
38
39static int test_errors = 0;
40
41
42/*----------------------------------------------------------------------------*/
43/* Macros */
44
45#define TEST_ASSERT( TEST ) \
46 do { \
47 if( ! (TEST) ) \
48 { \
49 test_fail( #TEST ); \
50 goto exit; \
51 } \
52 } while( 0 )
53
Rich Evans4c091142015-02-02 12:04:10 +000054#define assert(a) if( !( a ) ) \
55{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +000057 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +000059}
60
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000061/*
62 * 32-bit integer manipulation macros (big endian)
63 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000064#ifndef GET_UINT32_BE
65#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000066{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000067 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
68 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
69 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
70 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000071}
72#endif
73
Paul Bakker5c2364c2012-10-01 14:41:15 +000074#ifndef PUT_UINT32_BE
75#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000076{ \
77 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
78 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
79 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
80 (b)[(i) + 3] = (unsigned char) ( (n) ); \
81}
82#endif
83
Hanno Becker66580d22017-09-08 10:06:41 +010084/* Helper flags for complex dependencies */
85
86/* Indicates whether we expect mbedtls_entropy_init
87 * to initialize some strong entropy source. */
Hanno Beckerd2cc7ce2017-09-08 10:47:33 +010088#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
89 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
90 defined(MBEDTLS_HAVEGE_C) || \
91 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) )
Hanno Becker66580d22017-09-08 10:06:41 +010092#define ENTROPY_HAVE_STRONG
93#endif
94
SimonB8bcd5492016-02-17 23:34:30 +000095/*----------------------------------------------------------------------------*/
96/* Helper Functions */
97
Rich Evans4c091142015-02-02 12:04:10 +000098static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +000099{
100 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +0000101 int len = strlen( ibuf ) / 2;
SimonB8bcd5492016-02-17 23:34:30 +0000102 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Rich Evans4c091142015-02-02 12:04:10 +0000104 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000105 {
106 c = *ibuf++;
107 if( c >= '0' && c <= '9' )
108 c -= '0';
109 else if( c >= 'a' && c <= 'f' )
110 c -= 'a' - 10;
111 else if( c >= 'A' && c <= 'F' )
112 c -= 'A' - 10;
113 else
114 assert( 0 );
115
116 c2 = *ibuf++;
117 if( c2 >= '0' && c2 <= '9' )
118 c2 -= '0';
119 else if( c2 >= 'a' && c2 <= 'f' )
120 c2 -= 'a' - 10;
121 else if( c2 >= 'A' && c2 <= 'F' )
122 c2 -= 'A' - 10;
123 else
124 assert( 0 );
125
126 *obuf++ = ( c << 4 ) | c2;
127 }
128
129 return len;
130}
131
Rich Evans42914452015-02-02 12:09:25 +0000132static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000133{
134 unsigned char l, h;
135
Rich Evans42914452015-02-02 12:09:25 +0000136 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000137 {
Rich Evans42914452015-02-02 12:09:25 +0000138 h = *ibuf / 16;
139 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000140
141 if( h < 10 )
142 *obuf++ = '0' + h;
143 else
144 *obuf++ = 'a' + h - 10;
145
146 if( l < 10 )
147 *obuf++ = '0' + l;
148 else
149 *obuf++ = 'a' + l - 10;
150
151 ++ibuf;
152 len--;
153 }
154}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000155
156/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200157 * Allocate and zeroize a buffer.
158 *
159 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
160 *
161 * For convenience, dies if allocation fails.
162 */
163static unsigned char *zero_alloc( size_t len )
164{
165 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000166 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200167
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200168 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200169 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200170
171 memset( p, 0x00, actual_len );
172
173 return( p );
174}
175
176/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200177 * Allocate and fill a buffer from hex data.
178 *
179 * The buffer is sized exactly as needed. This allows to detect buffer
180 * overruns (including overreads) when running the test suite under valgrind.
181 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200182 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
183 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200184 * For convenience, dies if allocation fails.
185 */
186static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
187{
188 unsigned char *obuf;
189
Rich Evans42914452015-02-02 12:09:25 +0000190 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200191
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200192 if( *olen == 0 )
193 return( zero_alloc( *olen ) );
194
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200195 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200196 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200197
198 (void) unhexify( obuf, ibuf );
199
200 return( obuf );
201}
202
203/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000204 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000205 * Although predictable and often similar on multiple
206 * runs, this does not result in identical random on
207 * each run. So do not use this if the results of a
208 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000209 *
210 * rng_state shall be NULL.
211 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000212static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000213{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200214#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000215 size_t i;
216
Paul Bakker9dcc3222011-03-08 14:16:06 +0000217 if( rng_state != NULL )
218 rng_state = NULL;
219
Paul Bakkera3d195c2011-11-27 21:07:34 +0000220 for( i = 0; i < len; ++i )
221 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200222#else
223 if( rng_state != NULL )
224 rng_state = NULL;
225
226 arc4random_buf( output, len );
227#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000228
229 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000230}
231
232/**
233 * This function only returns zeros
234 *
235 * rng_state shall be NULL.
236 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000237static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000238{
239 if( rng_state != NULL )
240 rng_state = NULL;
241
Paul Bakkera3d195c2011-11-27 21:07:34 +0000242 memset( output, 0, len );
243
Paul Bakker9dcc3222011-03-08 14:16:06 +0000244 return( 0 );
245}
246
247typedef struct
248{
249 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000250 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000251} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000252
253/**
254 * This function returns random based on a buffer it receives.
255 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000256 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +0100257 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000258 * The number of bytes released from the buffer on each call to
259 * the random function is specified by per_call. (Can be between
260 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000261 *
262 * After the buffer is empty it will return rand();
263 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000264static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000265{
Paul Bakker997bbd12011-03-13 15:45:42 +0000266 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000267 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000268
269 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000270 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000271
Paul Bakkera3d195c2011-11-27 21:07:34 +0000272 use_len = len;
273 if( len > info->length )
274 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000275
Paul Bakkera3d195c2011-11-27 21:07:34 +0000276 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000277 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000278 memcpy( output, info->buf, use_len );
279 info->buf += use_len;
280 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000281 }
282
Paul Bakkera3d195c2011-11-27 21:07:34 +0000283 if( len - use_len > 0 )
284 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
285
286 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000287}
Paul Bakker997bbd12011-03-13 15:45:42 +0000288
289/**
290 * Info structure for the pseudo random function
291 *
292 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000293 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000294 * State( v0, v1 ) should be set to zero.
295 */
296typedef struct
297{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000298 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000299 uint32_t v0, v1;
300} rnd_pseudo_info;
301
302/**
303 * This function returns random based on a pseudo random function.
304 * This means the results should be identical on all systems.
305 * Pseudo random is based on the XTEA encryption algorithm to
306 * generate pseudorandom.
307 *
308 * rng_state shall be a pointer to a rnd_pseudo_info structure.
309 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000310static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000311{
312 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000313 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100314 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000315
316 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000317 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000318
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000319 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000320
321 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000322 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000323 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000324 sum = 0;
325
Paul Bakkera3d195c2011-11-27 21:07:34 +0000326 for( i = 0; i < 32; i++ )
327 {
Rich Evans42914452015-02-02 12:09:25 +0000328 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
329 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000330 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000331 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
332 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000333 }
334
Paul Bakker5c2364c2012-10-01 14:41:15 +0000335 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100336 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000337 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100338 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000339 }
340
Paul Bakkera3d195c2011-11-27 21:07:34 +0000341 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000342}
SimonB8bcd5492016-02-17 23:34:30 +0000343
344static void test_fail( const char *test )
345{
346 test_errors++;
347 if( test_errors == 1 )
348 mbedtls_printf( "FAILED\n" );
349 mbedtls_printf( " %s\n", test );
350}
351