blob: ddc29f6022a466fd2487825a9c9d4db6e3a64578 [file] [log] [blame]
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02001#if defined(POLARSSL_PLATFORM_C)
2#include "polarssl/platform.h"
3#else
Rich Evans00ab4702015-02-06 13:43:58 +00004#include <stdio.h>
5#define polarssl_printf printf
6#define polarssl_fprintf fprintf
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +02007#define polarssl_malloc malloc
8#define polarssl_free free
Rich Evans3d62e722015-02-03 11:48:59 +00009#define polarssl_exit exit
10#define polarssl_fprintf fprintf
11#define polarssl_printf printf
12#define polarssl_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;
18#else
19#include <inttypes.h>
20#endif
21
Rich Evans3d62e722015-02-03 11:48:59 +000022#include <stdio.h>
Paul Bakker19343182013-08-16 13:31:10 +020023#include <stdlib.h>
24#include <string.h>
25
Rich Evans4c091142015-02-02 12:04:10 +000026#define assert(a) if( !( a ) ) \
27{ \
28 polarssl_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
29 __FILE__, __LINE__, #a ); \
30 polarssl_exit( 1 ); \
31}
32
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000033/*
34 * 32-bit integer manipulation macros (big endian)
35 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#ifndef GET_UINT32_BE
37#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000038{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000039 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
40 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
41 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
42 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000043}
44#endif
45
Paul Bakker5c2364c2012-10-01 14:41:15 +000046#ifndef PUT_UINT32_BE
47#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000048{ \
49 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
50 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
51 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
52 (b)[(i) + 3] = (unsigned char) ( (n) ); \
53}
54#endif
55
Hanno Beckeradb9bd22017-09-08 10:53:30 +010056/* Helper flags for complex dependencies */
57
58/* Indicates whether we expect mbedtls_entropy_init
59 * to initialize some strong entropy source. */
60#if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES) && \
61 ( !defined(POLARSSL_NO_PLATFORM_ENTROPY) || \
62 defined(POLARSSL_HAVEGE_C) || \
63 defined(POLARSSL_TIMING_C) )
64#define ENTROPY_HAVE_DEFAULT
65#endif
66
Rich Evans4c091142015-02-02 12:04:10 +000067static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +000068{
69 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +000070 int len = strlen( ibuf ) / 2;
71 assert( strlen( ibuf ) % 2 == 0 ); // must be even number of bytes
Paul Bakker367dae42009-06-28 21:50:27 +000072
Rich Evans4c091142015-02-02 12:04:10 +000073 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +000074 {
75 c = *ibuf++;
76 if( c >= '0' && c <= '9' )
77 c -= '0';
78 else if( c >= 'a' && c <= 'f' )
79 c -= 'a' - 10;
80 else if( c >= 'A' && c <= 'F' )
81 c -= 'A' - 10;
82 else
83 assert( 0 );
84
85 c2 = *ibuf++;
86 if( c2 >= '0' && c2 <= '9' )
87 c2 -= '0';
88 else if( c2 >= 'a' && c2 <= 'f' )
89 c2 -= 'a' - 10;
90 else if( c2 >= 'A' && c2 <= 'F' )
91 c2 -= 'A' - 10;
92 else
93 assert( 0 );
94
95 *obuf++ = ( c << 4 ) | c2;
96 }
97
98 return len;
99}
100
Rich Evans42914452015-02-02 12:09:25 +0000101static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +0000102{
103 unsigned char l, h;
104
Rich Evans42914452015-02-02 12:09:25 +0000105 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +0000106 {
Rich Evans42914452015-02-02 12:09:25 +0000107 h = *ibuf / 16;
108 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000109
110 if( h < 10 )
111 *obuf++ = '0' + h;
112 else
113 *obuf++ = 'a' + h - 10;
114
115 if( l < 10 )
116 *obuf++ = '0' + l;
117 else
118 *obuf++ = 'a' + l - 10;
119
120 ++ibuf;
121 len--;
122 }
123}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000124
125/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200126 * Allocate and zeroize a buffer.
127 *
128 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
129 *
130 * For convenience, dies if allocation fails.
131 */
132static unsigned char *zero_alloc( size_t len )
133{
134 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000135 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200136
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200137 p = polarssl_malloc( actual_len );
138 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200139
140 memset( p, 0x00, actual_len );
141
142 return( p );
143}
144
145/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200146 * Allocate and fill a buffer from hex data.
147 *
148 * The buffer is sized exactly as needed. This allows to detect buffer
149 * overruns (including overreads) when running the test suite under valgrind.
150 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200151 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
152 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200153 * For convenience, dies if allocation fails.
154 */
155static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
156{
157 unsigned char *obuf;
158
Rich Evans42914452015-02-02 12:09:25 +0000159 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200160
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200161 if( *olen == 0 )
162 return( zero_alloc( *olen ) );
163
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200164 obuf = polarssl_malloc( *olen );
165 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200166
167 (void) unhexify( obuf, ibuf );
168
169 return( obuf );
170}
171
172/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000173 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000174 * Although predictable and often similar on multiple
175 * runs, this does not result in identical random on
176 * each run. So do not use this if the results of a
177 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000178 *
179 * rng_state shall be NULL.
180 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000181static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000182{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200183#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000184 size_t i;
185
Paul Bakker9dcc3222011-03-08 14:16:06 +0000186 if( rng_state != NULL )
187 rng_state = NULL;
188
Paul Bakkera3d195c2011-11-27 21:07:34 +0000189 for( i = 0; i < len; ++i )
190 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200191#else
192 if( rng_state != NULL )
193 rng_state = NULL;
194
195 arc4random_buf( output, len );
196#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000197
198 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000199}
200
201/**
202 * This function only returns zeros
203 *
204 * rng_state shall be NULL.
205 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000206static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000207{
208 if( rng_state != NULL )
209 rng_state = NULL;
210
Paul Bakkera3d195c2011-11-27 21:07:34 +0000211 memset( output, 0, len );
212
Paul Bakker9dcc3222011-03-08 14:16:06 +0000213 return( 0 );
214}
215
216typedef struct
217{
218 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000219 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000220} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000221
222/**
223 * This function returns random based on a buffer it receives.
224 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000225 * rng_state shall be a pointer to a rnd_buf_info structure.
Hanno Beckeradb9bd22017-09-08 10:53:30 +0100226 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000227 * The number of bytes released from the buffer on each call to
228 * the random function is specified by per_call. (Can be between
229 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000230 *
231 * After the buffer is empty it will return rand();
232 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000233static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000234{
Paul Bakker997bbd12011-03-13 15:45:42 +0000235 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000236 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000237
238 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000239 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000240
Paul Bakkera3d195c2011-11-27 21:07:34 +0000241 use_len = len;
242 if( len > info->length )
243 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000244
Paul Bakkera3d195c2011-11-27 21:07:34 +0000245 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000246 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000247 memcpy( output, info->buf, use_len );
248 info->buf += use_len;
249 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000250 }
251
Paul Bakkera3d195c2011-11-27 21:07:34 +0000252 if( len - use_len > 0 )
253 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
254
255 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000256}
Paul Bakker997bbd12011-03-13 15:45:42 +0000257
258/**
259 * Info structure for the pseudo random function
260 *
261 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000262 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000263 * State( v0, v1 ) should be set to zero.
264 */
265typedef struct
266{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000267 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000268 uint32_t v0, v1;
269} rnd_pseudo_info;
270
271/**
272 * This function returns random based on a pseudo random function.
273 * This means the results should be identical on all systems.
274 * Pseudo random is based on the XTEA encryption algorithm to
275 * generate pseudorandom.
276 *
277 * rng_state shall be a pointer to a rnd_pseudo_info structure.
278 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000279static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000280{
281 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000282 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100283 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000284
285 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000286 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000287
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000288 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000289
290 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000291 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000292 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000293 sum = 0;
294
Paul Bakkera3d195c2011-11-27 21:07:34 +0000295 for( i = 0; i < 32; i++ )
296 {
Rich Evans42914452015-02-02 12:09:25 +0000297 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
298 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000299 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000300 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
301 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000302 }
303
Paul Bakker5c2364c2012-10-01 14:41:15 +0000304 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100305 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000306 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100307 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000308 }
309
Paul Bakkera3d195c2011-11-27 21:07:34 +0000310 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000311}