9 typedef UINT32 uint32_t;
18 #define GET_ULONG_BE(n,b,i) \
20 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
21 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
22 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
23 | ( (unsigned long) (b)[(i) + 3] ); \
28 #define PUT_ULONG_BE(n,b,i) \
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) ); \
37 int unhexify(
unsigned char *obuf,
const char *ibuf)
40 int len = strlen(ibuf) / 2;
41 assert(!(strlen(ibuf) %1));
46 if( c >=
'0' && c <=
'9' )
48 else if( c >=
'a' && c <=
'f' )
50 else if( c >=
'A' && c <=
'F' )
56 if( c2 >=
'0' && c2 <=
'9' )
58 else if( c2 >=
'a' && c2 <=
'f' )
60 else if( c2 >=
'A' && c2 <=
'F' )
65 *obuf++ = ( c << 4 ) | c2;
71 void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
83 *obuf++ =
'a' + h - 10;
88 *obuf++ =
'a' + l - 10;
104 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
108 if( rng_state != NULL )
111 for( i = 0; i < len; ++i )
122 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
124 if( rng_state != NULL )
127 memset( output, 0, len );
154 if( rng_state == NULL )
163 memcpy( output, info->
buf, use_len );
164 info->
buf += use_len;
168 if( len - use_len > 0 )
169 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
198 uint32_t i, *k, sum, delta=0x9E3779B9;
199 unsigned char result[4];
201 if( rng_state == NULL )
208 size_t use_len = ( len > 4 ) ? 4 : len;
211 for( i = 0; i < 32; i++ )
213 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
215 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
219 memcpy( output, result, use_len );
229 #ifdef POLARSSL_XTEA_C
232 FCT_SUITE_BGN(test_suite_xtea)
235 FCT_TEST_BGN(xtea_encrypt_ecb_1)
237 unsigned char key_str[100];
238 unsigned char src_str[100];
239 unsigned char dst_str[100];
240 unsigned char output[100];
243 memset(key_str, 0x00, 100);
244 memset(src_str, 0x00, 100);
245 memset(dst_str, 0x00, 100);
246 memset(output, 0x00, 100);
248 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
249 unhexify( src_str,
"4142434445464748" );
253 hexify( dst_str, output, 8 );
255 fct_chk( strcmp( (
char *) dst_str,
"497df3d072612cb5" ) == 0 );
260 FCT_TEST_BGN(xtea_encrypt_ecb_2)
262 unsigned char key_str[100];
263 unsigned char src_str[100];
264 unsigned char dst_str[100];
265 unsigned char output[100];
268 memset(key_str, 0x00, 100);
269 memset(src_str, 0x00, 100);
270 memset(dst_str, 0x00, 100);
271 memset(output, 0x00, 100);
273 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
274 unhexify( src_str,
"4141414141414141" );
278 hexify( dst_str, output, 8 );
280 fct_chk( strcmp( (
char *) dst_str,
"e78f2d13744341d8" ) == 0 );
285 FCT_TEST_BGN(xtea_encrypt_ecb_3)
287 unsigned char key_str[100];
288 unsigned char src_str[100];
289 unsigned char dst_str[100];
290 unsigned char output[100];
293 memset(key_str, 0x00, 100);
294 memset(src_str, 0x00, 100);
295 memset(dst_str, 0x00, 100);
296 memset(output, 0x00, 100);
298 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
299 unhexify( src_str,
"5a5b6e278948d77f" );
303 hexify( dst_str, output, 8 );
305 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
310 FCT_TEST_BGN(xtea_encrypt_ecb_4)
312 unsigned char key_str[100];
313 unsigned char src_str[100];
314 unsigned char dst_str[100];
315 unsigned char output[100];
318 memset(key_str, 0x00, 100);
319 memset(src_str, 0x00, 100);
320 memset(dst_str, 0x00, 100);
321 memset(output, 0x00, 100);
323 unhexify( key_str,
"00000000000000000000000000000000" );
324 unhexify( src_str,
"4142434445464748" );
328 hexify( dst_str, output, 8 );
330 fct_chk( strcmp( (
char *) dst_str,
"a0390589f8b8efa5" ) == 0 );
335 FCT_TEST_BGN(xtea_encrypt_ecb_5)
337 unsigned char key_str[100];
338 unsigned char src_str[100];
339 unsigned char dst_str[100];
340 unsigned char output[100];
343 memset(key_str, 0x00, 100);
344 memset(src_str, 0x00, 100);
345 memset(dst_str, 0x00, 100);
346 memset(output, 0x00, 100);
348 unhexify( key_str,
"00000000000000000000000000000000" );
349 unhexify( src_str,
"4141414141414141" );
353 hexify( dst_str, output, 8 );
355 fct_chk( strcmp( (
char *) dst_str,
"ed23375a821a8c2d" ) == 0 );
360 FCT_TEST_BGN(xtea_encrypt_ecb_6)
362 unsigned char key_str[100];
363 unsigned char src_str[100];
364 unsigned char dst_str[100];
365 unsigned char output[100];
368 memset(key_str, 0x00, 100);
369 memset(src_str, 0x00, 100);
370 memset(dst_str, 0x00, 100);
371 memset(output, 0x00, 100);
373 unhexify( key_str,
"00000000000000000000000000000000" );
374 unhexify( src_str,
"70e1225d6e4e7655" );
378 hexify( dst_str, output, 8 );
380 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
385 FCT_TEST_BGN(xtea_decrypt_ecb_1)
387 unsigned char key_str[100];
388 unsigned char src_str[100];
389 unsigned char dst_str[100];
390 unsigned char output[100];
393 memset(key_str, 0x00, 100);
394 memset(src_str, 0x00, 100);
395 memset(dst_str, 0x00, 100);
396 memset(output, 0x00, 100);
398 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
399 unhexify( src_str,
"497df3d072612cb5" );
403 hexify( dst_str, output, 8 );
405 fct_chk( strcmp( (
char *) dst_str,
"4142434445464748" ) == 0 );
410 FCT_TEST_BGN(xtea_decrypt_ecb_2)
412 unsigned char key_str[100];
413 unsigned char src_str[100];
414 unsigned char dst_str[100];
415 unsigned char output[100];
418 memset(key_str, 0x00, 100);
419 memset(src_str, 0x00, 100);
420 memset(dst_str, 0x00, 100);
421 memset(output, 0x00, 100);
423 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
424 unhexify( src_str,
"e78f2d13744341d8" );
428 hexify( dst_str, output, 8 );
430 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
435 FCT_TEST_BGN(xtea_decrypt_ecb_3)
437 unsigned char key_str[100];
438 unsigned char src_str[100];
439 unsigned char dst_str[100];
440 unsigned char output[100];
443 memset(key_str, 0x00, 100);
444 memset(src_str, 0x00, 100);
445 memset(dst_str, 0x00, 100);
446 memset(output, 0x00, 100);
448 unhexify( key_str,
"000102030405060708090a0b0c0d0e0f" );
449 unhexify( src_str,
"4141414141414141" );
453 hexify( dst_str, output, 8 );
455 fct_chk( strcmp( (
char *) dst_str,
"5a5b6e278948d77f" ) == 0 );
460 FCT_TEST_BGN(xtea_decrypt_ecb_4)
462 unsigned char key_str[100];
463 unsigned char src_str[100];
464 unsigned char dst_str[100];
465 unsigned char output[100];
468 memset(key_str, 0x00, 100);
469 memset(src_str, 0x00, 100);
470 memset(dst_str, 0x00, 100);
471 memset(output, 0x00, 100);
473 unhexify( key_str,
"00000000000000000000000000000000" );
474 unhexify( src_str,
"a0390589f8b8efa5" );
478 hexify( dst_str, output, 8 );
480 fct_chk( strcmp( (
char *) dst_str,
"4142434445464748" ) == 0 );
485 FCT_TEST_BGN(xtea_decrypt_ecb_5)
487 unsigned char key_str[100];
488 unsigned char src_str[100];
489 unsigned char dst_str[100];
490 unsigned char output[100];
493 memset(key_str, 0x00, 100);
494 memset(src_str, 0x00, 100);
495 memset(dst_str, 0x00, 100);
496 memset(output, 0x00, 100);
498 unhexify( key_str,
"00000000000000000000000000000000" );
499 unhexify( src_str,
"ed23375a821a8c2d" );
503 hexify( dst_str, output, 8 );
505 fct_chk( strcmp( (
char *) dst_str,
"4141414141414141" ) == 0 );
510 FCT_TEST_BGN(xtea_decrypt_ecb_6)
512 unsigned char key_str[100];
513 unsigned char src_str[100];
514 unsigned char dst_str[100];
515 unsigned char output[100];
518 memset(key_str, 0x00, 100);
519 memset(src_str, 0x00, 100);
520 memset(dst_str, 0x00, 100);
521 memset(output, 0x00, 100);
523 unhexify( key_str,
"00000000000000000000000000000000" );
524 unhexify( src_str,
"4141414141414141" );
528 hexify( dst_str, output, 8 );
530 fct_chk( strcmp( (
char *) dst_str,
"70e1225d6e4e7655" ) == 0 );
534 #ifdef POLARSSL_SELF_TEST
536 FCT_TEST_BGN(xtea_selftest)