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