blob: f6a35295bbe3b6cb6212e9d76b72982d01898891 [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
Paul Bakker19343182013-08-16 13:31:10 +020012#include <assert.h>
13#include <stdlib.h>
14#include <string.h>
15
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000016/*
17 * 32-bit integer manipulation macros (big endian)
18 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000019#ifndef GET_UINT32_BE
20#define GET_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000021{ \
Paul Bakker5c2364c2012-10-01 14:41:15 +000022 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
23 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
24 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
25 | ( (uint32_t) (b)[(i) + 3] ); \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000026}
27#endif
28
Paul Bakker5c2364c2012-10-01 14:41:15 +000029#ifndef PUT_UINT32_BE
30#define PUT_UINT32_BE(n,b,i) \
Paul Bakkerb3dcbc12011-03-13 16:57:25 +000031{ \
32 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
33 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
34 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
35 (b)[(i) + 3] = (unsigned char) ( (n) ); \
36}
37#endif
38
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020039static int unhexify(unsigned char *obuf, const char *ibuf)
Paul Bakker367dae42009-06-28 21:50:27 +000040{
41 unsigned char c, c2;
42 int len = strlen(ibuf) / 2;
43 assert(!(strlen(ibuf) %1)); // must be even number of bytes
44
45 while (*ibuf != 0)
46 {
47 c = *ibuf++;
48 if( c >= '0' && c <= '9' )
49 c -= '0';
50 else if( c >= 'a' && c <= 'f' )
51 c -= 'a' - 10;
52 else if( c >= 'A' && c <= 'F' )
53 c -= 'A' - 10;
54 else
55 assert( 0 );
56
57 c2 = *ibuf++;
58 if( c2 >= '0' && c2 <= '9' )
59 c2 -= '0';
60 else if( c2 >= 'a' && c2 <= 'f' )
61 c2 -= 'a' - 10;
62 else if( c2 >= 'A' && c2 <= 'F' )
63 c2 -= 'A' - 10;
64 else
65 assert( 0 );
66
67 *obuf++ = ( c << 4 ) | c2;
68 }
69
70 return len;
71}
72
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020073static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
Paul Bakker367dae42009-06-28 21:50:27 +000074{
75 unsigned char l, h;
76
77 while (len != 0)
78 {
79 h = (*ibuf) / 16;
80 l = (*ibuf) % 16;
81
82 if( h < 10 )
83 *obuf++ = '0' + h;
84 else
85 *obuf++ = 'a' + h - 10;
86
87 if( l < 10 )
88 *obuf++ = '0' + l;
89 else
90 *obuf++ = 'a' + l - 10;
91
92 ++ibuf;
93 len--;
94 }
95}
Paul Bakker9dcc3222011-03-08 14:16:06 +000096
97/**
98 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +000099 * Although predictable and often similar on multiple
100 * runs, this does not result in identical random on
101 * each run. So do not use this if the results of a
102 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +0000103 *
104 * rng_state shall be NULL.
105 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000106static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000107{
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200108#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000109 size_t i;
110
Paul Bakker9dcc3222011-03-08 14:16:06 +0000111 if( rng_state != NULL )
112 rng_state = NULL;
113
Paul Bakkera3d195c2011-11-27 21:07:34 +0000114 for( i = 0; i < len; ++i )
115 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +0200116#else
117 if( rng_state != NULL )
118 rng_state = NULL;
119
120 arc4random_buf( output, len );
121#endif /* !OpenBSD */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000122
123 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000124}
125
126/**
127 * This function only returns zeros
128 *
129 * rng_state shall be NULL.
130 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000131static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000132{
133 if( rng_state != NULL )
134 rng_state = NULL;
135
Paul Bakkera3d195c2011-11-27 21:07:34 +0000136 memset( output, 0, len );
137
Paul Bakker9dcc3222011-03-08 14:16:06 +0000138 return( 0 );
139}
140
141typedef struct
142{
143 unsigned char *buf;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000144 size_t length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000145} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000146
147/**
148 * This function returns random based on a buffer it receives.
149 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000150 * rng_state shall be a pointer to a rnd_buf_info structure.
151 *
152 * The number of bytes released from the buffer on each call to
153 * the random function is specified by per_call. (Can be between
154 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000155 *
156 * After the buffer is empty it will return rand();
157 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000158static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000159{
Paul Bakker997bbd12011-03-13 15:45:42 +0000160 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000161 size_t use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000162
163 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000164 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000165
Paul Bakkera3d195c2011-11-27 21:07:34 +0000166 use_len = len;
167 if( len > info->length )
168 use_len = info->length;
Paul Bakker997bbd12011-03-13 15:45:42 +0000169
Paul Bakkera3d195c2011-11-27 21:07:34 +0000170 if( use_len )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000171 {
Paul Bakkera3d195c2011-11-27 21:07:34 +0000172 memcpy( output, info->buf, use_len );
173 info->buf += use_len;
174 info->length -= use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000175 }
176
Paul Bakkera3d195c2011-11-27 21:07:34 +0000177 if( len - use_len > 0 )
178 return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
179
180 return( 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000181}
Paul Bakker997bbd12011-03-13 15:45:42 +0000182
183/**
184 * Info structure for the pseudo random function
185 *
186 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000187 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000188 * State( v0, v1 ) should be set to zero.
189 */
190typedef struct
191{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000192 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000193 uint32_t v0, v1;
194} rnd_pseudo_info;
195
196/**
197 * This function returns random based on a pseudo random function.
198 * This means the results should be identical on all systems.
199 * Pseudo random is based on the XTEA encryption algorithm to
200 * generate pseudorandom.
201 *
202 * rng_state shall be a pointer to a rnd_pseudo_info structure.
203 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000204static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker997bbd12011-03-13 15:45:42 +0000205{
206 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000207 uint32_t i, *k, sum, delta=0x9E3779B9;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100208 unsigned char result[4], *out = output;
Paul Bakker997bbd12011-03-13 15:45:42 +0000209
210 if( rng_state == NULL )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000211 return( rnd_std_rand( NULL, output, len ) );
Paul Bakker997bbd12011-03-13 15:45:42 +0000212
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000213 k = info->key;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000214
215 while( len > 0 )
Paul Bakker997bbd12011-03-13 15:45:42 +0000216 {
Paul Bakker40dd5302012-05-15 15:02:38 +0000217 size_t use_len = ( len > 4 ) ? 4 : len;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000218 sum = 0;
219
Paul Bakkera3d195c2011-11-27 21:07:34 +0000220 for( i = 0; i < 32; i++ )
221 {
222 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
223 sum += delta;
224 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
225 }
226
Paul Bakker5c2364c2012-10-01 14:41:15 +0000227 PUT_UINT32_BE( info->v0, result, 0 );
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100228 memcpy( out, result, use_len );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000229 len -= use_len;
Manuel Pégourié-Gonnard217a29c2014-01-03 11:59:09 +0100230 out += 4;
Paul Bakker997bbd12011-03-13 15:45:42 +0000231 }
232
Paul Bakkera3d195c2011-11-27 21:07:34 +0000233 return( 0 );
Paul Bakker997bbd12011-03-13 15:45:42 +0000234}