blob: 6af918cad539cb9eafb5b6a6e18f3533acdf0621 [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 Wilson733676b2015-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
Rich Evans4c091142015-02-02 12:04:10 +000058static int unhexify( unsigned char *obuf, const char *ibuf )
Paul Bakker367dae42009-06-28 21:50:27 +000059{
60 unsigned char c, c2;
Rich Evans4c091142015-02-02 12:04:10 +000061 int len = strlen( ibuf ) / 2;
62 assert( strlen( ibuf ) % 2 == 0 ); // must be even number of bytes
Paul Bakker367dae42009-06-28 21:50:27 +000063
Rich Evans4c091142015-02-02 12:04:10 +000064 while( *ibuf != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +000065 {
66 c = *ibuf++;
67 if( c >= '0' && c <= '9' )
68 c -= '0';
69 else if( c >= 'a' && c <= 'f' )
70 c -= 'a' - 10;
71 else if( c >= 'A' && c <= 'F' )
72 c -= 'A' - 10;
73 else
74 assert( 0 );
75
76 c2 = *ibuf++;
77 if( c2 >= '0' && c2 <= '9' )
78 c2 -= '0';
79 else if( c2 >= 'a' && c2 <= 'f' )
80 c2 -= 'a' - 10;
81 else if( c2 >= 'A' && c2 <= 'F' )
82 c2 -= 'A' - 10;
83 else
84 assert( 0 );
85
86 *obuf++ = ( c << 4 ) | c2;
87 }
88
89 return len;
90}
91
Rich Evans42914452015-02-02 12:09:25 +000092static void hexify( unsigned char *obuf, const unsigned char *ibuf, int len )
Paul Bakker367dae42009-06-28 21:50:27 +000093{
94 unsigned char l, h;
95
Rich Evans42914452015-02-02 12:09:25 +000096 while( len != 0 )
Paul Bakker367dae42009-06-28 21:50:27 +000097 {
Rich Evans42914452015-02-02 12:09:25 +000098 h = *ibuf / 16;
99 l = *ibuf % 16;
Paul Bakker367dae42009-06-28 21:50:27 +0000100
101 if( h < 10 )
102 *obuf++ = '0' + h;
103 else
104 *obuf++ = 'a' + h - 10;
105
106 if( l < 10 )
107 *obuf++ = '0' + l;
108 else
109 *obuf++ = 'a' + l - 10;
110
111 ++ibuf;
112 len--;
113 }
114}
Paul Bakker9dcc3222011-03-08 14:16:06 +0000115
116/**
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200117 * Allocate and zeroize a buffer.
118 *
119 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
120 *
121 * For convenience, dies if allocation fails.
122 */
123static unsigned char *zero_alloc( size_t len )
124{
125 void *p;
Rich Evans42914452015-02-02 12:09:25 +0000126 size_t actual_len = ( len != 0 ) ? len : 1;
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200127
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200128 p = mbedtls_calloc( 1, actual_len );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200129 assert( p != NULL );
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200130
131 memset( p, 0x00, actual_len );
132
133 return( p );
134}
135
136/**
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200137 * Allocate and fill a buffer from hex data.
138 *
139 * The buffer is sized exactly as needed. This allows to detect buffer
140 * overruns (including overreads) when running the test suite under valgrind.
141 *
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200142 * If the size if zero, a pointer to a zeroized 1-byte buffer is returned.
143 *
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200144 * For convenience, dies if allocation fails.
145 */
146static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen )
147{
148 unsigned char *obuf;
149
Rich Evans42914452015-02-02 12:09:25 +0000150 *olen = strlen( ibuf ) / 2;
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200151
Manuel Pégourié-Gonnard0dc5e0d2014-06-13 21:09:26 +0200152 if( *olen == 0 )
153 return( zero_alloc( *olen ) );
154
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200155 obuf = mbedtls_calloc( 1, *olen );
Paul Bakker4d0cfe82014-07-10 14:37:36 +0200156 assert( obuf != NULL );
Manuel Pégourié-Gonnard3d49b9d2014-06-06 14:48:09 +0200157
158 (void) unhexify( obuf, ibuf );
159
160 return( obuf );
161}
162
163/**
Paul Bakker9dcc3222011-03-08 14:16:06 +0000164 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +0000165 * Although predictable and often similar on multiple
166 * runs, this does not result in identical random on
167 * each run. So do not use this if the results of a
168 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000169 *
170 * rng_state shall be NULL.
171 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000172static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000173{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200174#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000175 size_t i;
176
Paul Bakker9dcc3222011-03-08 14:16:06 +0000177 if( rng_state != NULL )
178 rng_state = NULL;
179
Paul Bakkera3d195c2011-11-27 21:07:34 +0000180 for( i = 0; i < len; ++i )
181 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200182#else
183 if( rng_state != NULL )
184 rng_state = NULL;
185
186 arc4random_buf( output, len );
187#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000188
189 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000190}
191
192/**
193 * This function only returns zeros
194 *
195 * rng_state shall be NULL.
196 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000197static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000198{
199 if( rng_state != NULL )
200 rng_state = NULL;
201
Paul Bakkera3d195c2011-11-27 21:07:34 +0000202 memset( output, 0, len );
203
Paul Bakker9dcc3222011-03-08 14:16:06 +0000204 return( 0 );
205}
206
207typedef struct
208{
209 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000210 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000211} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000212
213/**
214 * This function returns random based on a buffer it receives.
215 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000216 * rng_state shall be a pointer to a rnd_buf_info structure.
Manuel Pégourié-Gonnarde670f902015-10-30 09:23:19 +0100217 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000218 * The number of bytes released from the buffer on each call to
219 * the random function is specified by per_call. (Can be between
220 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000221 *
222 * After the buffer is empty it will return rand();
223 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000225{
Paul Bakker997bbd12011-03-13 15:45:42 +0000226 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000227 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000228
229 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000230 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000231
Paul Bakkera3d195c2011-11-27 21:07:34 +0000232 use_len = len;
233 if( len > info->length )
234 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000235
Paul Bakkera3d195c2011-11-27 21:07:34 +0000236 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000237 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000238 memcpy( output, info->buf, use_len );
239 info->buf += use_len;
240 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000241 }
242
Paul Bakkera3d195c2011-11-27 21:07:34 +0000243 if( len - use_len > 0 )
244 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
245
246 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000247}
Paul Bakker997bbd12011-03-13 15:45:42 +0000248
249/**
250 * Info structure for the pseudo random function
251 *
252 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000253 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000254 * State( v0, v1 ) should be set to zero.
255 */
256typedef struct
257{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000258 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000259 uint32_t v0, v1;
260} rnd_pseudo_info;
261
262/**
263 * This function returns random based on a pseudo random function.
264 * This means the results should be identical on all systems.
265 * Pseudo random is based on the XTEA encryption algorithm to
266 * generate pseudorandom.
267 *
268 * rng_state shall be a pointer to a rnd_pseudo_info structure.
269 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000270static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000271{
272 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000273 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100274 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000275
276 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000277 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000278
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000279 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000280
281 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000282 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000283 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000284 sum = 0;
285
Paul Bakkera3d195c2011-11-27 21:07:34 +0000286 for( i = 0; i < 32; i++ )
287 {
Rich Evans42914452015-02-02 12:09:25 +0000288 info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
289 + info->v1 ) ^ ( sum + k[sum & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000290 sum += delta;
Rich Evans42914452015-02-02 12:09:25 +0000291 info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
292 + info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000293 }
294
Paul Bakker5c2364c2012-10-01 14:41:15 +0000295 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100296 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000297 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100298 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000299 }
300
Paul Bakkera3d195c2011-11-27 21:07:34 +0000301 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000302}