blob: b23d3ff17331465adf3cc3bcea9a6747511bd852 [file] [log] [blame]
Paul Bakkerb3dcbc12011-03-13 16:57:25 +00001#ifdef _MSC_VER
2#include <basetsd.h>
3typedef UINT32 uint32_t;
4#else
5#include <inttypes.h>
6#endif
7
8/*
9 * 32-bit integer manipulation macros (big endian)
10 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000011#ifndef GET_UINT32_BE
12#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000013{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000014 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
15 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
16 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
17 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000018}
19#endif
20
Paul Bakker5c2364c2012-10-01 14:41:15 +000021#ifndef PUT_UINT32_BE
22#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000023{ \
24 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
25 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
26 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
27 (b)[(i) + 3] = (unsigned char) ( (n) ); \
28}
29#endif
30
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020031static int unhexify(unsigned char *obuf, const char *ibuf)
Paul Bakker367dae42009-06-28 21:50:27 +000032{
33 unsigned char c, c2;
34 int len = strlen(ibuf) / 2;
35 assert(!(strlen(ibuf) %1)); // must be even number of bytes
36
37 while (*ibuf != 0)
38 {
39 c = *ibuf++;
40 if( c >= '0' && c <= '9' )
41 c -= '0';
42 else if( c >= 'a' && c <= 'f' )
43 c -= 'a' - 10;
44 else if( c >= 'A' && c <= 'F' )
45 c -= 'A' - 10;
46 else
47 assert( 0 );
48
49 c2 = *ibuf++;
50 if( c2 >= '0' && c2 <= '9' )
51 c2 -= '0';
52 else if( c2 >= 'a' && c2 <= 'f' )
53 c2 -= 'a' - 10;
54 else if( c2 >= 'A' && c2 <= 'F' )
55 c2 -= 'A' - 10;
56 else
57 assert( 0 );
58
59 *obuf++ = ( c << 4 ) | c2;
60 }
61
62 return len;
63}
64
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020065static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Paul Bakker367dae42009-06-28 21:50:27 +000066{
67 unsigned char l, h;
68
69 while (len != 0)
70 {
71 h = (*ibuf) / 16;
72 l = (*ibuf) % 16;
73
74 if( h < 10 )
75 *obuf++ = '0' + h;
76 else
77 *obuf++ = 'a' + h - 10;
78
79 if( l < 10 )
80 *obuf++ = '0' + l;
81 else
82 *obuf++ = 'a' + l - 10;
83
84 ++ibuf;
85 len--;
86 }
87}
Paul Bakker9dcc3222011-03-08 14:16:06 +000088
89/**
90 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +000091 * Although predictable and often similar on multiple
92 * runs, this does not result in identical random on
93 * each run. So do not use this if the results of a
94 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +000095 *
96 * rng_state shall be NULL.
97 */
Paul Bakkera3d195c2011-11-27 21:07:34 +000098static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +000099{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000100 size_t i;
101
Paul Bakker9dcc3222011-03-08 14:16:06 +0000102 if( rng_state != NULL )
103 rng_state = NULL;
104
Paul Bakkera3d195c2011-11-27 21:07:34 +0000105 for( i = 0; i < len; ++i )
106 output[i] = rand();
107
108 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000109}
110
111/**
112 * This function only returns zeros
113 *
114 * rng_state shall be NULL.
115 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000116static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000117{
118 if( rng_state != NULL )
119 rng_state = NULL;
120
Paul Bakkera3d195c2011-11-27 21:07:34 +0000121 memset( output, 0, len );
122
Paul Bakker9dcc3222011-03-08 14:16:06 +0000123 return( 0 );
124}
125
126typedef struct
127{
128 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000129 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000130} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000131
132/**
133 * This function returns random based on a buffer it receives.
134 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000135 * rng_state shall be a pointer to a rnd_buf_info structure.
136 *
137 * The number of bytes released from the buffer on each call to
138 * the random function is specified by per_call. (Can be between
139 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000140 *
141 * After the buffer is empty it will return rand();
142 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000143static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000144{
Paul Bakker997bbd12011-03-13 15:45:42 +0000145 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000146 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000147
148 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000149 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000150
Paul Bakkera3d195c2011-11-27 21:07:34 +0000151 use_len = len;
152 if( len > info->length )
153 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000154
Paul Bakkera3d195c2011-11-27 21:07:34 +0000155 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000156 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000157 memcpy( output, info->buf, use_len );
158 info->buf += use_len;
159 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000160 }
161
Paul Bakkera3d195c2011-11-27 21:07:34 +0000162 if( len - use_len > 0 )
163 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
164
165 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000166}
Paul Bakker997bbd12011-03-13 15:45:42 +0000167
168/**
169 * Info structure for the pseudo random function
170 *
171 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000172 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000173 * State( v0, v1 ) should be set to zero.
174 */
175typedef struct
176{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000177 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000178 uint32_t v0, v1;
179} rnd_pseudo_info;
180
181/**
182 * This function returns random based on a pseudo random function.
183 * This means the results should be identical on all systems.
184 * Pseudo random is based on the XTEA encryption algorithm to
185 * generate pseudorandom.
186 *
187 * rng_state shall be a pointer to a rnd_pseudo_info structure.
188 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000189static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000190{
191 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000192 uint32_t i, *k, sum, delta=0x9E3779B9;
Paul Bakker40dd5302012-05-15 15:02:38 +0000193 unsigned char result[4];
Paul Bakker997bbd12011-03-13 15:45:42 +0000194
195 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000197
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000198 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000199
200 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000201 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000202 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000203 sum = 0;
204
Paul Bakkera3d195c2011-11-27 21:07:34 +0000205 for( i = 0; i < 32; i++ )
206 {
207 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
208 sum += delta;
209 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
210 }
211
Paul Bakker5c2364c2012-10-01 14:41:15 +0000212 PUT_UINT32_BE( info->v0, result, 0 );
Paul Bakker40dd5302012-05-15 15:02:38 +0000213 memcpy( output, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000214 len -= use_len;
Paul Bakker997bbd12011-03-13 15:45:42 +0000215 }
216
Paul Bakkera3d195c2011-11-27 21:07:34 +0000217 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000218}
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +0100219
220/**
221 * This function returns a buffer given as a hex string.
222 *
223 * The buffer is reversed so that the following are equivalent:
224 * mpi_fill_random( x, len, not_rnd, str );
225 * mpi_read_string( x, 16, str );
226 * (So, not random at all. Usefull to match test vectors.)
227 * Based on unhexify(), just reversed (changes marked by "sic")
228 */
229static int not_rnd( void *in, unsigned char *out, size_t len )
230{
231 unsigned char *obuf;
232 const char *ibuf = in;
233 unsigned char c, c2;
234 assert( len == strlen(ibuf) / 2 );
235 assert(!(strlen(ibuf) %1)); // must be even number of bytes
236
237 obuf = out + (len - 1); // sic
238 while (*ibuf != 0)
239 {
240 c = *ibuf++;
241 if( c >= '0' && c <= '9' )
242 c -= '0';
243 else if( c >= 'a' && c <= 'f' )
244 c -= 'a' - 10;
245 else if( c >= 'A' && c <= 'F' )
246 c -= 'A' - 10;
247 else
248 assert( 0 );
249
250 c2 = *ibuf++;
251 if( c2 >= '0' && c2 <= '9' )
252 c2 -= '0';
253 else if( c2 >= 'a' && c2 <= 'f' )
254 c2 -= 'a' - 10;
255 else if( c2 >= 'A' && c2 <= 'F' )
256 c2 -= 'A' - 10;
257 else
258 assert( 0 );
259
260 *obuf-- = ( c << 4 ) | c2; // sic
261 }
262
263 return( 0 );
264}