8 typedef UINT32 uint32_t;
17 #define GET_UINT32_BE(n,b,i) \
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] ); \
27 #define PUT_UINT32_BE(n,b,i) \
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) ); \
36 int unhexify(
unsigned char *obuf,
const char *ibuf)
39 int len = strlen(ibuf) / 2;
40 assert(!(strlen(ibuf) %1));
45 if( c >=
'0' && c <=
'9' )
47 else if( c >=
'a' && c <=
'f' )
49 else if( c >=
'A' && c <=
'F' )
55 if( c2 >=
'0' && c2 <=
'9' )
57 else if( c2 >=
'a' && c2 <=
'f' )
59 else if( c2 >=
'A' && c2 <=
'F' )
64 *obuf++ = ( c << 4 ) | c2;
70 void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
82 *obuf++ =
'a' + h - 10;
87 *obuf++ =
'a' + l - 10;
103 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
107 if( rng_state != NULL )
110 for( i = 0; i < len; ++i )
121 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
123 if( rng_state != NULL )
126 memset( output, 0, len );
153 if( rng_state == NULL )
162 memcpy( output, info->
buf, use_len );
163 info->
buf += use_len;
167 if( len - use_len > 0 )
168 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
197 uint32_t i, *k, sum, delta=0x9E3779B9;
198 unsigned char result[4];
200 if( rng_state == NULL )
207 size_t use_len = ( len > 4 ) ? 4 : len;
210 for( i = 0; i < 32; i++ )
212 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
214 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
218 memcpy( output, result, use_len );
228 #ifdef POLARSSL_XTEA_C
231 FCT_SUITE_BGN(test_suite_xtea)
234 FCT_TEST_BGN(xtea_encrypt_ecb_1)
236 unsigned char key_str[100];
237 unsigned char src_str[100];
238 unsigned char dst_str[100];
239 unsigned char output[100];
242 memset(key_str, 0x00, 100);
243 memset(src_str, 0x00, 100);
244 memset(dst_str, 0x00, 100);
245 memset(output, 0x00, 100);
247 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
248 unhexify( src_str,
"4142434445464748" );
252 hexify( dst_str, output, 8 );
254 fct_chk( strcmp( (
char *) dst_str,
"497df3d072612cb5" ) == 0 );
259 FCT_TEST_BGN(xtea_encrypt_ecb_2)
261 unsigned char key_str[100];
262 unsigned char src_str[100];
263 unsigned char dst_str[100];
264 unsigned char output[100];
267 memset(key_str, 0x00, 100);
268 memset(src_str, 0x00, 100);
269 memset(dst_str, 0x00, 100);
270 memset(output, 0x00, 100);
272 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
273 unhexify( src_str,
"4141414141414141" );
277 hexify( dst_str, output, 8 );
279 fct_chk( strcmp( (
char *) dst_str,
"e78f2d13744341d8" ) == 0 );
284 FCT_TEST_BGN(xtea_encrypt_ecb_3)
286 unsigned char key_str[100];
287 unsigned char src_str[100];
288 unsigned char dst_str[100];
289 unsigned char output[100];
292 memset(key_str, 0x00, 100);
293 memset(src_str, 0x00, 100);
294 memset(dst_str, 0x00, 100);
295 memset(output, 0x00, 100);
297 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
298 unhexify( src_str,
"5a5b6e278948d77f" );
302 hexify( dst_str, output, 8 );
304 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
309 FCT_TEST_BGN(xtea_encrypt_ecb_4)
311 unsigned char key_str[100];
312 unsigned char src_str[100];
313 unsigned char dst_str[100];
314 unsigned char output[100];
317 memset(key_str, 0x00, 100);
318 memset(src_str, 0x00, 100);
319 memset(dst_str, 0x00, 100);
320 memset(output, 0x00, 100);
322 unhexify( key_str,
"00000000000000000000000000000000" );
323 unhexify( src_str,
"4142434445464748" );
327 hexify( dst_str, output, 8 );
329 fct_chk( strcmp( (
char *) dst_str,
"a0390589f8b8efa5" ) == 0 );
334 FCT_TEST_BGN(xtea_encrypt_ecb_5)
336 unsigned char key_str[100];
337 unsigned char src_str[100];
338 unsigned char dst_str[100];
339 unsigned char output[100];
342 memset(key_str, 0x00, 100);
343 memset(src_str, 0x00, 100);
344 memset(dst_str, 0x00, 100);
345 memset(output, 0x00, 100);
347 unhexify( key_str,
"00000000000000000000000000000000" );
348 unhexify( src_str,
"4141414141414141" );
352 hexify( dst_str, output, 8 );
354 fct_chk( strcmp( (
char *) dst_str,
"ed23375a821a8c2d" ) == 0 );
359 FCT_TEST_BGN(xtea_encrypt_ecb_6)
361 unsigned char key_str[100];
362 unsigned char src_str[100];
363 unsigned char dst_str[100];
364 unsigned char output[100];
367 memset(key_str, 0x00, 100);
368 memset(src_str, 0x00, 100);
369 memset(dst_str, 0x00, 100);
370 memset(output, 0x00, 100);
372 unhexify( key_str,
"00000000000000000000000000000000" );
373 unhexify( src_str,
"70e1225d6e4e7655" );
377 hexify( dst_str, output, 8 );
379 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
384 FCT_TEST_BGN(xtea_decrypt_ecb_1)
386 unsigned char key_str[100];
387 unsigned char src_str[100];
388 unsigned char dst_str[100];
389 unsigned char output[100];
392 memset(key_str, 0x00, 100);
393 memset(src_str, 0x00, 100);
394 memset(dst_str, 0x00, 100);
395 memset(output, 0x00, 100);
397 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
398 unhexify( src_str,
"497df3d072612cb5" );
402 hexify( dst_str, output, 8 );
404 fct_chk( strcmp( (
char *) dst_str,
"4142434445464748" ) == 0 );
409 FCT_TEST_BGN(xtea_decrypt_ecb_2)
411 unsigned char key_str[100];
412 unsigned char src_str[100];
413 unsigned char dst_str[100];
414 unsigned char output[100];
417 memset(key_str, 0x00, 100);
418 memset(src_str, 0x00, 100);
419 memset(dst_str, 0x00, 100);
420 memset(output, 0x00, 100);
422 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
423 unhexify( src_str,
"e78f2d13744341d8" );
427 hexify( dst_str, output, 8 );
429 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
434 FCT_TEST_BGN(xtea_decrypt_ecb_3)
436 unsigned char key_str[100];
437 unsigned char src_str[100];
438 unsigned char dst_str[100];
439 unsigned char output[100];
442 memset(key_str, 0x00, 100);
443 memset(src_str, 0x00, 100);
444 memset(dst_str, 0x00, 100);
445 memset(output, 0x00, 100);
447 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
448 unhexify( src_str,
"4141414141414141" );
452 hexify( dst_str, output, 8 );
454 fct_chk( strcmp( (
char *) dst_str,
"5a5b6e278948d77f" ) == 0 );
459 FCT_TEST_BGN(xtea_decrypt_ecb_4)
461 unsigned char key_str[100];
462 unsigned char src_str[100];
463 unsigned char dst_str[100];
464 unsigned char output[100];
467 memset(key_str, 0x00, 100);
468 memset(src_str, 0x00, 100);
469 memset(dst_str, 0x00, 100);
470 memset(output, 0x00, 100);
472 unhexify( key_str,
"00000000000000000000000000000000" );
473 unhexify( src_str,
"a0390589f8b8efa5" );
477 hexify( dst_str, output, 8 );
479 fct_chk( strcmp( (
char *) dst_str,
"4142434445464748" ) == 0 );
484 FCT_TEST_BGN(xtea_decrypt_ecb_5)
486 unsigned char key_str[100];
487 unsigned char src_str[100];
488 unsigned char dst_str[100];
489 unsigned char output[100];
492 memset(key_str, 0x00, 100);
493 memset(src_str, 0x00, 100);
494 memset(dst_str, 0x00, 100);
495 memset(output, 0x00, 100);
497 unhexify( key_str,
"00000000000000000000000000000000" );
498 unhexify( src_str,
"ed23375a821a8c2d" );
502 hexify( dst_str, output, 8 );
504 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
509 FCT_TEST_BGN(xtea_decrypt_ecb_6)
511 unsigned char key_str[100];
512 unsigned char src_str[100];
513 unsigned char dst_str[100];
514 unsigned char output[100];
517 memset(key_str, 0x00, 100);
518 memset(src_str, 0x00, 100);
519 memset(dst_str, 0x00, 100);
520 memset(output, 0x00, 100);
522 unhexify( key_str,
"00000000000000000000000000000000" );
523 unhexify( src_str,
"4141414141414141" );
527 hexify( dst_str, output, 8 );
529 fct_chk( strcmp( (
char *) dst_str,
"70e1225d6e4e7655" ) == 0 );
533 #ifdef POLARSSL_SELF_TEST
535 FCT_TEST_BGN(xtea_selftest)