blob: b80831ee50d56248ddfde41eb5619471ed60114b [file] [log] [blame]
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02003#else
Rich Evans00ab4702015-02-06 13:43:58 +00004#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005#define mbedtls_printf printf
6#define mbedtls_fprintf fprintf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#define mbedtls_free free
9#define mbedtls_exit exit
10#define mbedtls_fprintf fprintf
11#define mbedtls_printf printf
12#define mbedtls_snprintf snprintf
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +020013#endif
14
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000015#ifdef _MSC_VER
16#include <basetsd.h>
17typedef UINT32 uint32_t;
Nicholas Wilson44ea0112015-11-14 13:09:01 +000018#define strncasecmp _strnicmp
19#define strcasecmp _stricmp
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000020#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020021#include <stdint.h>
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000022#endif
23
Rich Evans3d62e722015-02-03 11:48:59 +000024#include <stdio.h>
Paul Bakker19343182013-08-16 13:31:10 +020025#include <stdlib.h>
26#include <string.h>
27
Rich Evans4c091142015-02-02 12:04:10 +000028#define assert(a) if( !( a ) ) \
29{ \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
Rich Evans4c091142015-02-02 12:04:10 +000031 __FILE__, __LINE__, #a ); \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_exit( 1 ); \
Rich Evans4c091142015-02-02 12:04:10 +000033}
34
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000035/*
36 * 32-bit integer manipulation macros (big endian)
37 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000038#ifndef GET_UINT32_BE
39#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000040{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000041 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
42 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
43 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
44 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000045}
46#endif
47
Paul Bakker5c2364c2012-10-01 14:41:15 +000048#ifndef PUT_UINT32_BE
49#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000050{ \
51 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
52 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
53 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
54 (b)[(i) + 3] = (unsigned char) ( (n) ); \
55}
56#endif
57
Hanno Becker66580d22017-09-08 10:06:41 +010058/* Helper flags for complex dependencies */
59
60/* Indicates whether we expect mbedtls_entropy_init
61 * to initialize some strong entropy source. */
62#if defined(MBEDTLS_TEST_NULL_ENTROPY) || \
63 ( !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) && \
64 ( !defined(MBEDTLS_NO_PLATFORM_ENTROPY) || \
65 defined(MBEDTLS_HAVEGE_C) || \
66 defined(MBEDTLS_ENTROPY_HARDWARE_ALT) ) )
67#define ENTROPY_HAVE_STRONG
68#endif
69
Rich Evans4c091142015-02-02 12:04:10 +000070static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +000071{
72 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +000073 int len = strlen( ibuf ) / 2;
74 assert( strlen( ibuf ) % 2 == 0 ); // must be even number of bytes
Paul Bakker367dae42009-06-28 21:50:27 +000075
Rich Evans4c091142015-02-02 12:04:10 +000076 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +000077 {
78 c = *ibuf++;
79 if( c >= '0' && c <= '9' )
80 c -= '0';
81 else if( c >= 'a' && c <= 'f' )
82 c -= 'a' - 10;
83 else if( c >= 'A' && c <= 'F' )
84 c -= 'A' - 10;
85 else
86 assert( 0 );
87
88 c2 = *ibuf++;
89 if( c2 >= '0' && c2 <= '9' )
90 c2 -= '0';
91 else if( c2 >= 'a' && c2 <= 'f' )
92 c2 -= 'a' - 10;
93 else if( c2 >= 'A' && c2 <= 'F' )
94 c2 -= 'A' - 10;
95 else
96 assert( 0 );
97
98 *obuf++ = ( c << 4 ) | c2;
99 }
100
101 return len;
102}
103
Rich Evans42914452015-02-02 12:09:25 +0000104static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000105{
106 unsigned char l, h;
107
Rich Evans42914452015-02-02 12:09:25 +0000108 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000109 {
Rich Evans42914452015-02-02 12:09:25 +0000110 h = *ibuf / 16;
111 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000112
113 if( h < 10 )
114 *obuf++ = '0' + h;
115 else
116 *obuf++ = 'a' + h - 10;
117
118 if( l < 10 )
119 *obuf++ = '0' + l;
120 else
121 *obuf++ = 'a' + l - 10;
122
123 ++ibuf;
124 len--;
125 }
126}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000127
128/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200129 * Allocate and zeroize a buffer.
130 *
131 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
132 *
133 * For convenience, dies if allocation fails.
134 */
135static unsigned char *zero_alloc( size_t len )
136{
137 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000138 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200139
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200140 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200141 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200142
143 memset( p, 0x00, actual_len );
144
145 return( p );
146}
147
148/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200149 * Allocate and fill a buffer from hex data.
150 *
151 * The buffer is sized exactly as needed. This allows to detect buffer
152 * overruns (including overreads) when running the test suite under valgrind.
153 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200154 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
155 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200156 * For convenience, dies if allocation fails.
157 */
158static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
159{
160 unsigned char *obuf;
161
Rich Evans42914452015-02-02 12:09:25 +0000162 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200163
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200164 if( *olen == 0 )
165 return( zero_alloc( *olen ) );
166
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200167 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200168 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200169
170 (void) unhexify( obuf, ibuf );
171
172 return( obuf );
173}
174
175/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000176 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000177 * Although predictable and often similar on multiple
178 * runs, this does not result in identical random on
179 * each run. So do not use this if the results of a
180 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000181 *
182 * rng_state shall be NULL.
183 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000184static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000185{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200186#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000187 size_t i;
188
Paul Bakker9dcc3222011-03-08 14:16:06 +0000189 if( rng_state != NULL )
190 rng_state = NULL;
191
Paul Bakkera3d195c2011-11-27 21:07:34 +0000192 for( i = 0; i < len; ++i )
193 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200194#else
195 if( rng_state != NULL )
196 rng_state = NULL;
197
198 arc4random_buf( output, len );
199#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000200
201 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000202}
203
204/**
205 * This function only returns zeros
206 *
207 * rng_state shall be NULL.
208 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000209static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000210{
211 if( rng_state != NULL )
212 rng_state = NULL;
213
Paul Bakkera3d195c2011-11-27 21:07:34 +0000214 memset( output, 0, len );
215
Paul Bakker9dcc3222011-03-08 14:16:06 +0000216 return( 0 );
217}
218
219typedef struct
220{
221 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000222 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000223} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000224
225/**
226 * This function returns random based on a buffer it receives.
227 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000228 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnardfd1f9e72015-10-30 09:23:19 +0100229 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000230 * The number of bytes released from the buffer on each call to
231 * the random function is specified by per_call. (Can be between
232 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000233 *
234 * After the buffer is empty it will return rand();
235 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000236static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000237{
Paul Bakker997bbd12011-03-13 15:45:42 +0000238 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000239 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000240
241 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000242 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000243
Paul Bakkera3d195c2011-11-27 21:07:34 +0000244 use_len = len;
245 if( len > info->length )
246 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000247
Paul Bakkera3d195c2011-11-27 21:07:34 +0000248 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000249 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000250 memcpy( output, info->buf, use_len );
251 info->buf += use_len;
252 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000253 }
254
Paul Bakkera3d195c2011-11-27 21:07:34 +0000255 if( len - use_len > 0 )
256 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
257
258 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000259}
Paul Bakker997bbd12011-03-13 15:45:42 +0000260
261/**
262 * Info structure for the pseudo random function
263 *
264 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000265 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000266 * State( v0, v1 ) should be set to zero.
267 */
268typedef struct
269{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000270 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000271 uint32_t v0, v1;
272} rnd_pseudo_info;
273
274/**
275 * This function returns random based on a pseudo random function.
276 * This means the results should be identical on all systems.
277 * Pseudo random is based on the XTEA encryption algorithm to
278 * generate pseudorandom.
279 *
280 * rng_state shall be a pointer to a rnd_pseudo_info structure.
281 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000282static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000283{
284 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000285 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100286 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000287
288 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000289 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000290
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000291 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000292
293 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000294 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000295 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000296 sum = 0;
297
Paul Bakkera3d195c2011-11-27 21:07:34 +0000298 for( i = 0; i < 32; i++ )
299 {
Rich Evans42914452015-02-02 12:09:25 +0000300 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
301 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000302 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000303 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
304 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000305 }
306
Paul Bakker5c2364c2012-10-01 14:41:15 +0000307 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100308 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000309 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100310 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000311 }
312
Paul Bakkera3d195c2011-11-27 21:07:34 +0000313 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000314}