blob: c18eed8951a9bc37628ecf087f3e632e240299eb [file] [log] [blame]
SimonB0269dad2016-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
SimonB0269dad2016-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;
25#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020026#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000027#endif
28
Rich Evans3d62e722015-02-03 11:48:59 +000029#include <stdio.h>
Paul Bakker19343182013-08-16 13:31:10 +020030#include <stdlib.h>
31#include <string.h>
32
SimonB0269dad2016-02-17 23:34:30 +000033
34/*----------------------------------------------------------------------------*/
35/* Global variables */
36
37static int test_errors = 0;
38
39
40/*----------------------------------------------------------------------------*/
41/* Macros */
42
43#define TEST_ASSERT( TEST ) \
44 do { \
45 if( ! (TEST) ) \
46 { \
47 test_fail( #TEST ); \
48 goto exit; \
49 } \
50 } while( 0 )
51
Rich Evans4c091142015-02-02 12:04:10 +000052#define assert(a) if( !( a ) ) \
53{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +000055 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +000057}
58
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000059/*
60 * 32-bit integer manipulation macros (big endian)
61 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000062#ifndef GET_UINT32_BE
63#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000064{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000065 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
66 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
67 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
68 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000069}
70#endif
71
Paul Bakker5c2364c2012-10-01 14:41:15 +000072#ifndef PUT_UINT32_BE
73#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000074{ \
75 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
76 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
77 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
78 (b)[(i) + 3] = (unsigned char) ( (n) ); \
79}
80#endif
81
SimonB0269dad2016-02-17 23:34:30 +000082
83/*----------------------------------------------------------------------------*/
84/* Helper Functions */
85
Rich Evans4c091142015-02-02 12:04:10 +000086static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +000087{
88 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +000089 int len = strlen( ibuf ) / 2;
SimonB0269dad2016-02-17 23:34:30 +000090 assert( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
Paul Bakker367dae42009-06-28 21:50:27 +000091
Rich Evans4c091142015-02-02 12:04:10 +000092 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +000093 {
94 c = *ibuf++;
95 if( c >= '0' && c <= '9' )
96 c -= '0';
97 else if( c >= 'a' && c <= 'f' )
98 c -= 'a' - 10;
99 else if( c >= 'A' && c <= 'F' )
100 c -= 'A' - 10;
101 else
102 assert( 0 );
103
104 c2 = *ibuf++;
105 if( c2 >= '0' && c2 <= '9' )
106 c2 -= '0';
107 else if( c2 >= 'a' && c2 <= 'f' )
108 c2 -= 'a' - 10;
109 else if( c2 >= 'A' && c2 <= 'F' )
110 c2 -= 'A' - 10;
111 else
112 assert( 0 );
113
114 *obuf++ = ( c << 4 ) | c2;
115 }
116
117 return len;
118}
119
Rich Evans42914452015-02-02 12:09:25 +0000120static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000121{
122 unsigned char l, h;
123
Rich Evans42914452015-02-02 12:09:25 +0000124 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000125 {
Rich Evans42914452015-02-02 12:09:25 +0000126 h = *ibuf / 16;
127 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000128
129 if( h < 10 )
130 *obuf++ = '0' + h;
131 else
132 *obuf++ = 'a' + h - 10;
133
134 if( l < 10 )
135 *obuf++ = '0' + l;
136 else
137 *obuf++ = 'a' + l - 10;
138
139 ++ibuf;
140 len--;
141 }
142}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000143
144/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200145 * Allocate and zeroize a buffer.
146 *
147 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
148 *
149 * For convenience, dies if allocation fails.
150 */
151static unsigned char *zero_alloc( size_t len )
152{
153 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000154 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200155
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200156 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200157 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200158
159 memset( p, 0x00, actual_len );
160
161 return( p );
162}
163
164/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200165 * Allocate and fill a buffer from hex data.
166 *
167 * The buffer is sized exactly as needed. This allows to detect buffer
168 * overruns (including overreads) when running the test suite under valgrind.
169 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200170 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
171 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200172 * For convenience, dies if allocation fails.
173 */
174static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
175{
176 unsigned char *obuf;
177
Rich Evans42914452015-02-02 12:09:25 +0000178 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200179
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200180 if( *olen == 0 )
181 return( zero_alloc( *olen ) );
182
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200183 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200184 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200185
186 (void) unhexify( obuf, ibuf );
187
188 return( obuf );
189}
190
191/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000192 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000193 * Although predictable and often similar on multiple
194 * runs, this does not result in identical random on
195 * each run. So do not use this if the results of a
196 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000197 *
198 * rng_state shall be NULL.
199 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000200static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000201{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200202#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000203 size_t i;
204
Paul Bakker9dcc3222011-03-08 14:16:06 +0000205 if( rng_state != NULL )
206 rng_state = NULL;
207
Paul Bakkera3d195c2011-11-27 21:07:34 +0000208 for( i = 0; i < len; ++i )
209 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200210#else
211 if( rng_state != NULL )
212 rng_state = NULL;
213
214 arc4random_buf( output, len );
215#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000216
217 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000218}
219
220/**
221 * This function only returns zeros
222 *
223 * rng_state shall be NULL.
224 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000225static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000226{
227 if( rng_state != NULL )
228 rng_state = NULL;
229
Paul Bakkera3d195c2011-11-27 21:07:34 +0000230 memset( output, 0, len );
231
Paul Bakker9dcc3222011-03-08 14:16:06 +0000232 return( 0 );
233}
234
235typedef struct
236{
237 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000238 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000239} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000240
241/**
242 * This function returns random based on a buffer it receives.
243 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000244 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100245 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000246 * The number of bytes released from the buffer on each call to
247 * the random function is specified by per_call. (Can be between
248 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000249 *
250 * After the buffer is empty it will return rand();
251 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000252static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000253{
Paul Bakker997bbd12011-03-13 15:45:42 +0000254 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000255 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000256
257 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000258 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000259
Paul Bakkera3d195c2011-11-27 21:07:34 +0000260 use_len = len;
261 if( len > info->length )
262 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000263
Paul Bakkera3d195c2011-11-27 21:07:34 +0000264 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000265 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000266 memcpy( output, info->buf, use_len );
267 info->buf += use_len;
268 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000269 }
270
Paul Bakkera3d195c2011-11-27 21:07:34 +0000271 if( len - use_len > 0 )
272 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
273
274 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000275}
Paul Bakker997bbd12011-03-13 15:45:42 +0000276
277/**
278 * Info structure for the pseudo random function
279 *
280 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000281 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000282 * State( v0, v1 ) should be set to zero.
283 */
284typedef struct
285{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000286 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000287 uint32_t v0, v1;
288} rnd_pseudo_info;
289
290/**
291 * This function returns random based on a pseudo random function.
292 * This means the results should be identical on all systems.
293 * Pseudo random is based on the XTEA encryption algorithm to
294 * generate pseudorandom.
295 *
296 * rng_state shall be a pointer to a rnd_pseudo_info structure.
297 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000298static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000299{
300 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000301 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100302 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000303
304 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000305 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000306
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000307 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000308
309 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000310 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000311 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000312 sum = 0;
313
Paul Bakkera3d195c2011-11-27 21:07:34 +0000314 for( i = 0; i < 32; i++ )
315 {
Rich Evans42914452015-02-02 12:09:25 +0000316 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
317 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000318 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000319 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
320 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000321 }
322
Paul Bakker5c2364c2012-10-01 14:41:15 +0000323 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100324 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000325 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100326 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000327 }
328
Paul Bakkera3d195c2011-11-27 21:07:34 +0000329 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000330}
SimonB0269dad2016-02-17 23:34:30 +0000331
332static void test_fail( const char *test )
333{
334 test_errors++;
335 if( test_errors == 1 )
336 mbedtls_printf( "FAILED\n" );
337 mbedtls_printf( " %s\n", test );
338}
339