PolarSSL v1.2.5
test_suite_arc4.c
Go to the documentation of this file.
1 #include "fct.h"
2 
3 #include <polarssl/arc4.h>
4 
5 #include <polarssl/config.h>
6 
7 #ifdef _MSC_VER
8 #include <basetsd.h>
9 typedef UINT32 uint32_t;
10 #else
11 #include <inttypes.h>
12 #endif
13 
14 /*
15  * 32-bit integer manipulation macros (big endian)
16  */
17 #ifndef GET_UINT32_BE
18 #define GET_UINT32_BE(n,b,i) \
19 { \
20  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
21  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
22  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
23  | ( (uint32_t) (b)[(i) + 3] ); \
24 }
25 #endif
26 
27 #ifndef PUT_UINT32_BE
28 #define PUT_UINT32_BE(n,b,i) \
29 { \
30  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
31  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
32  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
33  (b)[(i) + 3] = (unsigned char) ( (n) ); \
34 }
35 #endif
36 
37 int unhexify(unsigned char *obuf, const char *ibuf)
38 {
39  unsigned char c, c2;
40  int len = strlen(ibuf) / 2;
41  assert(!(strlen(ibuf) %1)); // must be even number of bytes
42 
43  while (*ibuf != 0)
44  {
45  c = *ibuf++;
46  if( c >= '0' && c <= '9' )
47  c -= '0';
48  else if( c >= 'a' && c <= 'f' )
49  c -= 'a' - 10;
50  else if( c >= 'A' && c <= 'F' )
51  c -= 'A' - 10;
52  else
53  assert( 0 );
54 
55  c2 = *ibuf++;
56  if( c2 >= '0' && c2 <= '9' )
57  c2 -= '0';
58  else if( c2 >= 'a' && c2 <= 'f' )
59  c2 -= 'a' - 10;
60  else if( c2 >= 'A' && c2 <= 'F' )
61  c2 -= 'A' - 10;
62  else
63  assert( 0 );
64 
65  *obuf++ = ( c << 4 ) | c2;
66  }
67 
68  return len;
69 }
70 
71 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
72 {
73  unsigned char l, h;
74 
75  while (len != 0)
76  {
77  h = (*ibuf) / 16;
78  l = (*ibuf) % 16;
79 
80  if( h < 10 )
81  *obuf++ = '0' + h;
82  else
83  *obuf++ = 'a' + h - 10;
84 
85  if( l < 10 )
86  *obuf++ = '0' + l;
87  else
88  *obuf++ = 'a' + l - 10;
89 
90  ++ibuf;
91  len--;
92  }
93 }
94 
104 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
105 {
106  size_t i;
107 
108  if( rng_state != NULL )
109  rng_state = NULL;
110 
111  for( i = 0; i < len; ++i )
112  output[i] = rand();
113 
114  return( 0 );
115 }
116 
122 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
123 {
124  if( rng_state != NULL )
125  rng_state = NULL;
126 
127  memset( output, 0, len );
128 
129  return( 0 );
130 }
131 
132 typedef struct
133 {
134  unsigned char *buf;
135  size_t length;
136 } rnd_buf_info;
137 
149 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
150 {
151  rnd_buf_info *info = (rnd_buf_info *) rng_state;
152  size_t use_len;
153 
154  if( rng_state == NULL )
155  return( rnd_std_rand( NULL, output, len ) );
156 
157  use_len = len;
158  if( len > info->length )
159  use_len = info->length;
160 
161  if( use_len )
162  {
163  memcpy( output, info->buf, use_len );
164  info->buf += use_len;
165  info->length -= use_len;
166  }
167 
168  if( len - use_len > 0 )
169  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
170 
171  return( 0 );
172 }
173 
181 typedef struct
182 {
183  uint32_t key[16];
184  uint32_t v0, v1;
186 
195 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
196 {
197  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
198  uint32_t i, *k, sum, delta=0x9E3779B9;
199  unsigned char result[4];
200 
201  if( rng_state == NULL )
202  return( rnd_std_rand( NULL, output, len ) );
203 
204  k = info->key;
205 
206  while( len > 0 )
207  {
208  size_t use_len = ( len > 4 ) ? 4 : len;
209  sum = 0;
210 
211  for( i = 0; i < 32; i++ )
212  {
213  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
214  sum += delta;
215  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
216  }
217 
218  PUT_UINT32_BE( info->v0, result, 0 );
219  memcpy( output, result, use_len );
220  len -= use_len;
221  }
222 
223  return( 0 );
224 }
225 
226 
228 {
229 #ifdef POLARSSL_ARC4_C
230 
231 
232  FCT_SUITE_BGN(test_suite_arc4)
233  {
234 
235  FCT_TEST_BGN(test_vector_arc4_cryptlib)
236  {
237  unsigned char src_str[1000];
238  unsigned char key_str[1000];
239  unsigned char dst_str[1000];
240  unsigned char dst_hexstr[2000];
241  int src_len, key_len;
242  arc4_context ctx;
243 
244  memset(src_str, 0x00, 1000);
245  memset(key_str, 0x00, 1000);
246  memset(dst_str, 0x00, 1000);
247  memset(dst_hexstr, 0x00, 2000);
248 
249  src_len = unhexify( src_str, "0000000000000000" );
250  key_len = unhexify( key_str, "0123456789abcdef" );
251 
252  arc4_setup(&ctx, key_str, key_len);
253  fct_chk( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
254  hexify( dst_hexstr, dst_str, src_len );
255 
256  fct_chk( strcmp( (char *) dst_hexstr, "7494c2e7104b0879" ) == 0 );
257  }
258  FCT_TEST_END();
259 
260 
261  FCT_TEST_BGN(test_vector_arc4_commerce)
262  {
263  unsigned char src_str[1000];
264  unsigned char key_str[1000];
265  unsigned char dst_str[1000];
266  unsigned char dst_hexstr[2000];
267  int src_len, key_len;
268  arc4_context ctx;
269 
270  memset(src_str, 0x00, 1000);
271  memset(key_str, 0x00, 1000);
272  memset(dst_str, 0x00, 1000);
273  memset(dst_hexstr, 0x00, 2000);
274 
275  src_len = unhexify( src_str, "dcee4cf92c" );
276  key_len = unhexify( key_str, "618a63d2fb" );
277 
278  arc4_setup(&ctx, key_str, key_len);
279  fct_chk( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
280  hexify( dst_hexstr, dst_str, src_len );
281 
282  fct_chk( strcmp( (char *) dst_hexstr, "f13829c9de" ) == 0 );
283  }
284  FCT_TEST_END();
285 
286 
287  FCT_TEST_BGN(test_vector_arc4_ssh_arcfour)
288  {
289  unsigned char src_str[1000];
290  unsigned char key_str[1000];
291  unsigned char dst_str[1000];
292  unsigned char dst_hexstr[2000];
293  int src_len, key_len;
294  arc4_context ctx;
295 
296  memset(src_str, 0x00, 1000);
297  memset(key_str, 0x00, 1000);
298  memset(dst_str, 0x00, 1000);
299  memset(dst_hexstr, 0x00, 2000);
300 
301  src_len = unhexify( src_str, "527569736c696e6e756e206c61756c75206b6f727669737373616e692c2074e4686be470e46964656e2070e4e46c6ce42074e47973696b75752e204b6573e479f66e206f6e206f6e6e69206f6d616e616e692c206b61736b6973617675756e206c61616b736f7420766572686f75752e20456e206d6120696c6f697473652c20737572652068756f6b61612c206d75747461206d657473e46e2074756d6d757573206d756c6c652074756f6b61612e205075756e746f2070696c76656e2c206d692068756b6b75752c207369696e746f20766172616e207475756c6973656e2c206d69206e756b6b75752e2054756f6b7375742076616e616d6f6e206a61207661726a6f74207665656e2c206e69697374e420737964e46d656e69206c61756c756e207465656e2e202d2045696e6f204c65696e6f" );
302  key_len = unhexify( key_str, "29041972fb42ba5fc7127712f13829c9" );
303 
304  arc4_setup(&ctx, key_str, key_len);
305  fct_chk( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
306  hexify( dst_hexstr, dst_str, src_len );
307 
308  fct_chk( strcmp( (char *) dst_hexstr, "358186999001e6b5daf05eceeb7eee21e0689c1f00eea81f7dd2caaee1d2763e68af0ead33d66c268bc946c484fbe94c5f5e0b86a59279e4f824e7a640bd223210b0a61160b7bce986ea65688003596b630a6b90f8e0caf6912a98eb872176e83c202caa64166d2cce57ff1bca57b213f0ed1aa72fb8ea52b0be01cd1e412867720b326eb389d011bd70d8af035fb0d8589dbce3c666f5ea8d4c7954c50c3f340b0467f81b425961c11843074df620f208404b394cf9d37ff54b5f1ad8f6ea7da3c561dfa7281f964463d2cc35a4d1b03490dec51b0711fbd6f55f79234d5b7c766622a66de92be996461d5e4dc878ef9bca030521e8351e4baed2fd04f9467368c4ad6ac186d08245b263a2666d1f6c5420f1599dfd9f438921c2f5a463938ce0982265eef70179bc553f339eb1a4c1af5f6a547f" ) == 0 );
309  }
310  FCT_TEST_END();
311 
312 #ifdef POLARSSL_SELF_TEST
313 
314  FCT_TEST_BGN(arc4_selftest)
315  {
316  fct_chk( arc4_self_test( 0 ) == 0 );
317  }
318  FCT_TEST_END();
319 #endif /* POLARSSL_SELF_TEST */
320 
321  }
322  FCT_SUITE_END();
323 
324 #endif /* POLARSSL_ARC4_C */
325 
326 }
327 FCT_END();
328