blob: 66af8af27ab5bddb857f9c8873fb9b0afb18be96 [file] [log] [blame]
Paul Bakkere07c4312013-07-03 14:00:49 +02001#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
2#include "polarssl/memory.h"
3#endif
4
Paul Bakkerb3dcbc12011-03-13 16:57:25 +00005#ifdef _MSC_VER
6#include <basetsd.h>
7typedef UINT32 uint32_t;
8#else
9#include <inttypes.h>
10#endif
11
12/*
13 * 32-bit integer manipulation macros (big endian)
14 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000015#ifndef GET_UINT32_BE
16#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000017{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000018 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
19 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
20 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
21 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000022}
23#endif
24
Paul Bakker5c2364c2012-10-01 14:41:15 +000025#ifndef PUT_UINT32_BE
26#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000027{ \
28 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
29 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
30 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
31 (b)[(i) + 3] = (unsigned char) ( (n) ); \
32}
33#endif
34
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020035static int unhexify(unsigned char *obuf, const char *ibuf)
Paul Bakker367dae42009-06-28 21:50:27 +000036{
37 unsigned char c, c2;
38 int len = strlen(ibuf) / 2;
39 assert(!(strlen(ibuf) %1)); // must be even number of bytes
40
41 while (*ibuf != 0)
42 {
43 c = *ibuf++;
44 if( c >= '0' && c <= '9' )
45 c -= '0';
46 else if( c >= 'a' && c <= 'f' )
47 c -= 'a' - 10;
48 else if( c >= 'A' && c <= 'F' )
49 c -= 'A' - 10;
50 else
51 assert( 0 );
52
53 c2 = *ibuf++;
54 if( c2 >= '0' && c2 <= '9' )
55 c2 -= '0';
56 else if( c2 >= 'a' && c2 <= 'f' )
57 c2 -= 'a' - 10;
58 else if( c2 >= 'A' && c2 <= 'F' )
59 c2 -= 'A' - 10;
60 else
61 assert( 0 );
62
63 *obuf++ = ( c << 4 ) | c2;
64 }
65
66 return len;
67}
68
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020069static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Paul Bakker367dae42009-06-28 21:50:27 +000070{
71 unsigned char l, h;
72
73 while (len != 0)
74 {
75 h = (*ibuf) / 16;
76 l = (*ibuf) % 16;
77
78 if( h < 10 )
79 *obuf++ = '0' + h;
80 else
81 *obuf++ = 'a' + h - 10;
82
83 if( l < 10 )
84 *obuf++ = '0' + l;
85 else
86 *obuf++ = 'a' + l - 10;
87
88 ++ibuf;
89 len--;
90 }
91}
Paul Bakker9dcc3222011-03-08 14:16:06 +000092
93/**
94 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +000095 * Although predictable and often similar on multiple
96 * runs, this does not result in identical random on
97 * each run. So do not use this if the results of a
98 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +000099 *
100 * rng_state shall be NULL.
101 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000102static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000103{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000104 size_t i;
105
Paul Bakker9dcc3222011-03-08 14:16:06 +0000106 if( rng_state != NULL )
107 rng_state = NULL;
108
Paul Bakkera3d195c2011-11-27 21:07:34 +0000109 for( i = 0; i < len; ++i )
110 output[i] = rand();
111
112 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000113}
114
115/**
116 * This function only returns zeros
117 *
118 * rng_state shall be NULL.
119 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000120static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000121{
122 if( rng_state != NULL )
123 rng_state = NULL;
124
Paul Bakkera3d195c2011-11-27 21:07:34 +0000125 memset( output, 0, len );
126
Paul Bakker9dcc3222011-03-08 14:16:06 +0000127 return( 0 );
128}
129
130typedef struct
131{
132 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000133 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000134} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000135
136/**
137 * This function returns random based on a buffer it receives.
138 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000139 * rng_state shall be a pointer to a rnd_buf_info structure.
140 *
141 * The number of bytes released from the buffer on each call to
142 * the random function is specified by per_call. (Can be between
143 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000144 *
145 * After the buffer is empty it will return rand();
146 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000147static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000148{
Paul Bakker997bbd12011-03-13 15:45:42 +0000149 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000150 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000151
152 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000153 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000154
Paul Bakkera3d195c2011-11-27 21:07:34 +0000155 use_len = len;
156 if( len > info->length )
157 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000158
Paul Bakkera3d195c2011-11-27 21:07:34 +0000159 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000160 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000161 memcpy( output, info->buf, use_len );
162 info->buf += use_len;
163 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000164 }
165
Paul Bakkera3d195c2011-11-27 21:07:34 +0000166 if( len - use_len > 0 )
167 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
168
169 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000170}
Paul Bakker997bbd12011-03-13 15:45:42 +0000171
172/**
173 * Info structure for the pseudo random function
174 *
175 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000176 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000177 * State( v0, v1 ) should be set to zero.
178 */
179typedef struct
180{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000181 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000182 uint32_t v0, v1;
183} rnd_pseudo_info;
184
185/**
186 * This function returns random based on a pseudo random function.
187 * This means the results should be identical on all systems.
188 * Pseudo random is based on the XTEA encryption algorithm to
189 * generate pseudorandom.
190 *
191 * rng_state shall be a pointer to a rnd_pseudo_info structure.
192 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000193static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000194{
195 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196 uint32_t i, *k, sum, delta=0x9E3779B9;
Paul Bakker40dd5302012-05-15 15:02:38 +0000197 unsigned char result[4];
Paul Bakker997bbd12011-03-13 15:45:42 +0000198
199 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000200 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000201
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000202 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000203
204 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000205 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000206 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000207 sum = 0;
208
Paul Bakkera3d195c2011-11-27 21:07:34 +0000209 for( i = 0; i < 32; i++ )
210 {
211 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
212 sum += delta;
213 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
214 }
215
Paul Bakker5c2364c2012-10-01 14:41:15 +0000216 PUT_UINT32_BE( info->v0, result, 0 );
Paul Bakker40dd5302012-05-15 15:02:38 +0000217 memcpy( output, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000218 len -= use_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000219 }
220
Paul Bakkera3d195c2011-11-27 21:07:34 +0000221 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000222}
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +0100223
224/**
225 * This function returns a buffer given as a hex string.
226 *
227 * The buffer is reversed so that the following are equivalent:
228 * mpi_fill_random( x, len, not_rnd, str );
229 * mpi_read_string( x, 16, str );
230 * (So, not random at all. Usefull to match test vectors.)
231 * Based on unhexify(), just reversed (changes marked by "sic")
232 */
233static int not_rnd( void *in, unsigned char *out, size_t len )
234{
235 unsigned char *obuf;
236 const char *ibuf = in;
237 unsigned char c, c2;
238 assert( len == strlen(ibuf) / 2 );
239 assert(!(strlen(ibuf) %1)); // must be even number of bytes
240
241 obuf = out + (len - 1); // sic
242 while (*ibuf != 0)
243 {
244 c = *ibuf++;
245 if( c >= '0' && c <= '9' )
246 c -= '0';
247 else if( c >= 'a' && c <= 'f' )
248 c -= 'a' - 10;
249 else if( c >= 'A' && c <= 'F' )
250 c -= 'A' - 10;
251 else
252 assert( 0 );
253
254 c2 = *ibuf++;
255 if( c2 >= '0' && c2 <= '9' )
256 c2 -= '0';
257 else if( c2 >= 'a' && c2 <= 'f' )
258 c2 -= 'a' - 10;
259 else if( c2 >= 'A' && c2 <= 'F' )
260 c2 -= 'A' - 10;
261 else
262 assert( 0 );
263
264 *obuf-- = ( c << 4 ) | c2; // sic
265 }
266
267 return( 0 );
268}