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