blob: 7f700838bdc7fde59fddeec22984606a4487a24d [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 Bakker367dae42009-06-28 21:50:27 +000031int unhexify(unsigned char *obuf, const char *ibuf)
32{
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
65void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
66{
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 Bakker95a11f82014-04-30 16:02:38 +0200100#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000101 size_t i;
102
Paul Bakker9dcc3222011-03-08 14:16:06 +0000103 if( rng_state != NULL )
104 rng_state = NULL;
105
Paul Bakkera3d195c2011-11-27 21:07:34 +0000106 for( i = 0; i < len; ++i )
107 output[i] = rand();
Paul Bakker95a11f82014-04-30 16:02:38 +0200108#else
109 if( rng_state != NULL )
110 rng_state = NULL;
111
112 arc4random_buf( output, len );
113#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000114
115 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000116}
117
118/**
119 * This function only returns zeros
120 *
121 * rng_state shall be NULL.
122 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000123static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000124{
125 if( rng_state != NULL )
126 rng_state = NULL;
127
Paul Bakkera3d195c2011-11-27 21:07:34 +0000128 memset( output, 0, len );
129
Paul Bakker9dcc3222011-03-08 14:16:06 +0000130 return( 0 );
131}
132
133typedef struct
134{
135 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000136 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000137} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000138
139/**
140 * This function returns random based on a buffer it receives.
141 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000142 * rng_state shall be a pointer to a rnd_buf_info structure.
143 *
144 * The number of bytes released from the buffer on each call to
145 * the random function is specified by per_call. (Can be between
146 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000147 *
148 * After the buffer is empty it will return rand();
149 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000150static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000151{
Paul Bakker997bbd12011-03-13 15:45:42 +0000152 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000153 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000154
155 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000156 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000157
Paul Bakkera3d195c2011-11-27 21:07:34 +0000158 use_len = len;
159 if( len > info->length )
160 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000161
Paul Bakkera3d195c2011-11-27 21:07:34 +0000162 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000163 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000164 memcpy( output, info->buf, use_len );
165 info->buf += use_len;
166 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000167 }
168
Paul Bakkera3d195c2011-11-27 21:07:34 +0000169 if( len - use_len > 0 )
170 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
171
172 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000173}
Paul Bakker997bbd12011-03-13 15:45:42 +0000174
175/**
176 * Info structure for the pseudo random function
177 *
178 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000179 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000180 * State( v0, v1 ) should be set to zero.
181 */
182typedef struct
183{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000184 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000185 uint32_t v0, v1;
186} rnd_pseudo_info;
187
188/**
189 * This function returns random based on a pseudo random function.
190 * This means the results should be identical on all systems.
191 * Pseudo random is based on the XTEA encryption algorithm to
192 * generate pseudorandom.
193 *
194 * rng_state shall be a pointer to a rnd_pseudo_info structure.
195 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000197{
198 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000199 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnardec8f2ff2014-01-03 11:59:09 +0100200 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000201
202 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000203 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000204
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000205 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000206
207 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000208 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000209 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000210 sum = 0;
211
Paul Bakkera3d195c2011-11-27 21:07:34 +0000212 for( i = 0; i < 32; i++ )
213 {
214 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
215 sum += delta;
216 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
217 }
218
Paul Bakker5c2364c2012-10-01 14:41:15 +0000219 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnardec8f2ff2014-01-03 11:59:09 +0100220 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000221 len -= use_len;
Manuel Pégourié-Gonnardec8f2ff2014-01-03 11:59:09 +0100222 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000223 }
224
Paul Bakkera3d195c2011-11-27 21:07:34 +0000225 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000226}