PolarSSL v1.2.5
test_suite_xtea.c
Go to the documentation of this file.
1 #include "fct.h"
2 
3 #include <polarssl/xtea.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_XTEA_C
230 
231 
232  FCT_SUITE_BGN(test_suite_xtea)
233  {
234 
235  FCT_TEST_BGN(xtea_encrypt_ecb_1)
236  {
237  unsigned char key_str[100];
238  unsigned char src_str[100];
239  unsigned char dst_str[100];
240  unsigned char output[100];
241  xtea_context ctx;
242 
243  memset(key_str, 0x00, 100);
244  memset(src_str, 0x00, 100);
245  memset(dst_str, 0x00, 100);
246  memset(output, 0x00, 100);
247 
248  unhexify( key_str, "000102030405060708090a0b0c0d0e0f" );
249  unhexify( src_str, "4142434445464748" );
250 
251  xtea_setup( &ctx, key_str );
252  fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 );
253  hexify( dst_str, output, 8 );
254 
255  fct_chk( strcmp( (char *) dst_str, "497df3d072612cb5" ) == 0 );
256  }
257  FCT_TEST_END();
258 
259 
260  FCT_TEST_BGN(xtea_encrypt_ecb_2)
261  {
262  unsigned char key_str[100];
263  unsigned char src_str[100];
264  unsigned char dst_str[100];
265  unsigned char output[100];
266  xtea_context ctx;
267 
268  memset(key_str, 0x00, 100);
269  memset(src_str, 0x00, 100);
270  memset(dst_str, 0x00, 100);
271  memset(output, 0x00, 100);
272 
273  unhexify( key_str, "000102030405060708090a0b0c0d0e0f" );
274  unhexify( src_str, "4141414141414141" );
275 
276  xtea_setup( &ctx, key_str );
277  fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 );
278  hexify( dst_str, output, 8 );
279 
280  fct_chk( strcmp( (char *) dst_str, "e78f2d13744341d8" ) == 0 );
281  }
282  FCT_TEST_END();
283 
284 
285  FCT_TEST_BGN(xtea_encrypt_ecb_3)
286  {
287  unsigned char key_str[100];
288  unsigned char src_str[100];
289  unsigned char dst_str[100];
290  unsigned char output[100];
291  xtea_context ctx;
292 
293  memset(key_str, 0x00, 100);
294  memset(src_str, 0x00, 100);
295  memset(dst_str, 0x00, 100);
296  memset(output, 0x00, 100);
297 
298  unhexify( key_str, "000102030405060708090a0b0c0d0e0f" );
299  unhexify( src_str, "5a5b6e278948d77f" );
300 
301  xtea_setup( &ctx, key_str );
302  fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 );
303  hexify( dst_str, output, 8 );
304 
305  fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 );
306  }
307  FCT_TEST_END();
308 
309 
310  FCT_TEST_BGN(xtea_encrypt_ecb_4)
311  {
312  unsigned char key_str[100];
313  unsigned char src_str[100];
314  unsigned char dst_str[100];
315  unsigned char output[100];
316  xtea_context ctx;
317 
318  memset(key_str, 0x00, 100);
319  memset(src_str, 0x00, 100);
320  memset(dst_str, 0x00, 100);
321  memset(output, 0x00, 100);
322 
323  unhexify( key_str, "00000000000000000000000000000000" );
324  unhexify( src_str, "4142434445464748" );
325 
326  xtea_setup( &ctx, key_str );
327  fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 );
328  hexify( dst_str, output, 8 );
329 
330  fct_chk( strcmp( (char *) dst_str, "a0390589f8b8efa5" ) == 0 );
331  }
332  FCT_TEST_END();
333 
334 
335  FCT_TEST_BGN(xtea_encrypt_ecb_5)
336  {
337  unsigned char key_str[100];
338  unsigned char src_str[100];
339  unsigned char dst_str[100];
340  unsigned char output[100];
341  xtea_context ctx;
342 
343  memset(key_str, 0x00, 100);
344  memset(src_str, 0x00, 100);
345  memset(dst_str, 0x00, 100);
346  memset(output, 0x00, 100);
347 
348  unhexify( key_str, "00000000000000000000000000000000" );
349  unhexify( src_str, "4141414141414141" );
350 
351  xtea_setup( &ctx, key_str );
352  fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 );
353  hexify( dst_str, output, 8 );
354 
355  fct_chk( strcmp( (char *) dst_str, "ed23375a821a8c2d" ) == 0 );
356  }
357  FCT_TEST_END();
358 
359 
360  FCT_TEST_BGN(xtea_encrypt_ecb_6)
361  {
362  unsigned char key_str[100];
363  unsigned char src_str[100];
364  unsigned char dst_str[100];
365  unsigned char output[100];
366  xtea_context ctx;
367 
368  memset(key_str, 0x00, 100);
369  memset(src_str, 0x00, 100);
370  memset(dst_str, 0x00, 100);
371  memset(output, 0x00, 100);
372 
373  unhexify( key_str, "00000000000000000000000000000000" );
374  unhexify( src_str, "70e1225d6e4e7655" );
375 
376  xtea_setup( &ctx, key_str );
377  fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 );
378  hexify( dst_str, output, 8 );
379 
380  fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 );
381  }
382  FCT_TEST_END();
383 
384 
385  FCT_TEST_BGN(xtea_decrypt_ecb_1)
386  {
387  unsigned char key_str[100];
388  unsigned char src_str[100];
389  unsigned char dst_str[100];
390  unsigned char output[100];
391  xtea_context ctx;
392 
393  memset(key_str, 0x00, 100);
394  memset(src_str, 0x00, 100);
395  memset(dst_str, 0x00, 100);
396  memset(output, 0x00, 100);
397 
398  unhexify( key_str, "000102030405060708090a0b0c0d0e0f" );
399  unhexify( src_str, "497df3d072612cb5" );
400 
401  xtea_setup( &ctx, key_str );
402  fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 );
403  hexify( dst_str, output, 8 );
404 
405  fct_chk( strcmp( (char *) dst_str, "4142434445464748" ) == 0 );
406  }
407  FCT_TEST_END();
408 
409 
410  FCT_TEST_BGN(xtea_decrypt_ecb_2)
411  {
412  unsigned char key_str[100];
413  unsigned char src_str[100];
414  unsigned char dst_str[100];
415  unsigned char output[100];
416  xtea_context ctx;
417 
418  memset(key_str, 0x00, 100);
419  memset(src_str, 0x00, 100);
420  memset(dst_str, 0x00, 100);
421  memset(output, 0x00, 100);
422 
423  unhexify( key_str, "000102030405060708090a0b0c0d0e0f" );
424  unhexify( src_str, "e78f2d13744341d8" );
425 
426  xtea_setup( &ctx, key_str );
427  fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 );
428  hexify( dst_str, output, 8 );
429 
430  fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 );
431  }
432  FCT_TEST_END();
433 
434 
435  FCT_TEST_BGN(xtea_decrypt_ecb_3)
436  {
437  unsigned char key_str[100];
438  unsigned char src_str[100];
439  unsigned char dst_str[100];
440  unsigned char output[100];
441  xtea_context ctx;
442 
443  memset(key_str, 0x00, 100);
444  memset(src_str, 0x00, 100);
445  memset(dst_str, 0x00, 100);
446  memset(output, 0x00, 100);
447 
448  unhexify( key_str, "000102030405060708090a0b0c0d0e0f" );
449  unhexify( src_str, "4141414141414141" );
450 
451  xtea_setup( &ctx, key_str );
452  fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 );
453  hexify( dst_str, output, 8 );
454 
455  fct_chk( strcmp( (char *) dst_str, "5a5b6e278948d77f" ) == 0 );
456  }
457  FCT_TEST_END();
458 
459 
460  FCT_TEST_BGN(xtea_decrypt_ecb_4)
461  {
462  unsigned char key_str[100];
463  unsigned char src_str[100];
464  unsigned char dst_str[100];
465  unsigned char output[100];
466  xtea_context ctx;
467 
468  memset(key_str, 0x00, 100);
469  memset(src_str, 0x00, 100);
470  memset(dst_str, 0x00, 100);
471  memset(output, 0x00, 100);
472 
473  unhexify( key_str, "00000000000000000000000000000000" );
474  unhexify( src_str, "a0390589f8b8efa5" );
475 
476  xtea_setup( &ctx, key_str );
477  fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 );
478  hexify( dst_str, output, 8 );
479 
480  fct_chk( strcmp( (char *) dst_str, "4142434445464748" ) == 0 );
481  }
482  FCT_TEST_END();
483 
484 
485  FCT_TEST_BGN(xtea_decrypt_ecb_5)
486  {
487  unsigned char key_str[100];
488  unsigned char src_str[100];
489  unsigned char dst_str[100];
490  unsigned char output[100];
491  xtea_context ctx;
492 
493  memset(key_str, 0x00, 100);
494  memset(src_str, 0x00, 100);
495  memset(dst_str, 0x00, 100);
496  memset(output, 0x00, 100);
497 
498  unhexify( key_str, "00000000000000000000000000000000" );
499  unhexify( src_str, "ed23375a821a8c2d" );
500 
501  xtea_setup( &ctx, key_str );
502  fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 );
503  hexify( dst_str, output, 8 );
504 
505  fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 );
506  }
507  FCT_TEST_END();
508 
509 
510  FCT_TEST_BGN(xtea_decrypt_ecb_6)
511  {
512  unsigned char key_str[100];
513  unsigned char src_str[100];
514  unsigned char dst_str[100];
515  unsigned char output[100];
516  xtea_context ctx;
517 
518  memset(key_str, 0x00, 100);
519  memset(src_str, 0x00, 100);
520  memset(dst_str, 0x00, 100);
521  memset(output, 0x00, 100);
522 
523  unhexify( key_str, "00000000000000000000000000000000" );
524  unhexify( src_str, "4141414141414141" );
525 
526  xtea_setup( &ctx, key_str );
527  fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 );
528  hexify( dst_str, output, 8 );
529 
530  fct_chk( strcmp( (char *) dst_str, "70e1225d6e4e7655" ) == 0 );
531  }
532  FCT_TEST_END();
533 
534 #ifdef POLARSSL_SELF_TEST
535 
536  FCT_TEST_BGN(xtea_selftest)
537  {
538  fct_chk( xtea_self_test( 0 ) == 0 );
539  }
540  FCT_TEST_END();
541 #endif /* POLARSSL_SELF_TEST */
542 
543  }
544  FCT_SUITE_END();
545 
546 #endif /* POLARSSL_XTEA_C */
547 
548 }
549 FCT_END();
550