blob: cfb02533fffb5acd86a39487cb7bcdd77923f74f [file] [log] [blame]
Paul Bakker579923c2011-03-26 13:39:34 +00001#include <polarssl/config.h>
2
Paul Bakkerb3dcbc12011-03-13 16:57:25 +00003#ifdef _MSC_VER
4#include <basetsd.h>
5typedef UINT32 uint32_t;
6#else
7#include <inttypes.h>
8#endif
9
10/*
11 * 32-bit integer manipulation macros (big endian)
12 */
13#ifndef GET_ULONG_BE
14#define GET_ULONG_BE(n,b,i) \
15{ \
16 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
17 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
18 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
19 | ( (unsigned long) (b)[(i) + 3] ); \
20}
21#endif
22
23#ifndef PUT_ULONG_BE
24#define PUT_ULONG_BE(n,b,i) \
25{ \
26 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
27 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
28 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
29 (b)[(i) + 3] = (unsigned char) ( (n) ); \
30}
31#endif
32
Paul Bakker367dae42009-06-28 21:50:27 +000033int unhexify(unsigned char *obuf, const char *ibuf)
34{
35 unsigned char c, c2;
36 int len = strlen(ibuf) / 2;
37 assert(!(strlen(ibuf) %1)); // must be even number of bytes
38
39 while (*ibuf != 0)
40 {
41 c = *ibuf++;
42 if( c >= '0' && c <= '9' )
43 c -= '0';
44 else if( c >= 'a' && c <= 'f' )
45 c -= 'a' - 10;
46 else if( c >= 'A' && c <= 'F' )
47 c -= 'A' - 10;
48 else
49 assert( 0 );
50
51 c2 = *ibuf++;
52 if( c2 >= '0' && c2 <= '9' )
53 c2 -= '0';
54 else if( c2 >= 'a' && c2 <= 'f' )
55 c2 -= 'a' - 10;
56 else if( c2 >= 'A' && c2 <= 'F' )
57 c2 -= 'A' - 10;
58 else
59 assert( 0 );
60
61 *obuf++ = ( c << 4 ) | c2;
62 }
63
64 return len;
65}
66
67void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
68{
69 unsigned char l, h;
70
71 while (len != 0)
72 {
73 h = (*ibuf) / 16;
74 l = (*ibuf) % 16;
75
76 if( h < 10 )
77 *obuf++ = '0' + h;
78 else
79 *obuf++ = 'a' + h - 10;
80
81 if( l < 10 )
82 *obuf++ = '0' + l;
83 else
84 *obuf++ = 'a' + l - 10;
85
86 ++ibuf;
87 len--;
88 }
89}
Paul Bakker9dcc3222011-03-08 14:16:06 +000090
91/**
92 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +000093 * Although predictable and often similar on multiple
94 * runs, this does not result in identical random on
95 * each run. So do not use this if the results of a
96 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +000097 *
98 * rng_state shall be NULL.
99 */
100static int rnd_std_rand( void *rng_state )
101{
102 if( rng_state != NULL )
103 rng_state = NULL;
104
105 return( rand() );
106}
107
108/**
109 * This function only returns zeros
110 *
111 * rng_state shall be NULL.
112 */
113static int rnd_zero_rand( void *rng_state )
114{
115 if( rng_state != NULL )
116 rng_state = NULL;
117
118 return( 0 );
119}
120
121typedef struct
122{
123 unsigned char *buf;
124 int length;
125 int per_call;
Paul Bakker997bbd12011-03-13 15:45:42 +0000126} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000127
128/**
129 * This function returns random based on a buffer it receives.
130 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000131 * rng_state shall be a pointer to a rnd_buf_info structure.
132 *
133 * The number of bytes released from the buffer on each call to
134 * the random function is specified by per_call. (Can be between
135 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000136 *
137 * After the buffer is empty it will return rand();
138 */
139static int rnd_buffer_rand( void *rng_state )
140{
Paul Bakker997bbd12011-03-13 15:45:42 +0000141 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000142 int res;
143
144 if( rng_state == NULL )
145 return( rand() );
146
Paul Bakker997bbd12011-03-13 15:45:42 +0000147 if( info->per_call > 4 )
148 info->per_call = 4;
149 else if( info->per_call < 1 )
150 info->per_call = 1;
151
Paul Bakker9dcc3222011-03-08 14:16:06 +0000152 res = rand();
153
154 if( info->length >= info->per_call )
155 {
156 memcpy( &res, info->buf, info->per_call );
157 info->buf += info->per_call;
158 info->length -= info->per_call;
159 }
160 else if( info->length > 0 )
161 {
162 memcpy( &res, info->buf, info->length );
163 info->length = 0;
164 }
165
166 return( res );
167}
Paul Bakker997bbd12011-03-13 15:45:42 +0000168
169/**
170 * Info structure for the pseudo random function
171 *
172 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000173 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000174 * State( v0, v1 ) should be set to zero.
175 */
176typedef struct
177{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000178 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000179 uint32_t v0, v1;
180} rnd_pseudo_info;
181
182/**
183 * This function returns random based on a pseudo random function.
184 * This means the results should be identical on all systems.
185 * Pseudo random is based on the XTEA encryption algorithm to
186 * generate pseudorandom.
187 *
188 * rng_state shall be a pointer to a rnd_pseudo_info structure.
189 */
190static int rnd_pseudo_rand( void *rng_state )
191{
192 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000193 uint32_t i, *k, sum = 0, delta=0x9E3779B9;
Paul Bakker997bbd12011-03-13 15:45:42 +0000194
195 if( rng_state == NULL )
196 return( rand() );
197
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000198 k = info->key;
Paul Bakker997bbd12011-03-13 15:45:42 +0000199 for( i = 0; i < 32; i++ )
200 {
201 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
202 sum += delta;
203 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
204 }
205
206 return( info->v0 );
207}