PolarSSL v1.1.4
|
00001 #include "fct.h" 00002 00003 #include <polarssl/xtea.h> 00004 00005 #include <polarssl/config.h> 00006 00007 #ifdef _MSC_VER 00008 #include <basetsd.h> 00009 typedef UINT32 uint32_t; 00010 #else 00011 #include <inttypes.h> 00012 #endif 00013 00014 /* 00015 * 32-bit integer manipulation macros (big endian) 00016 */ 00017 #ifndef GET_ULONG_BE 00018 #define GET_ULONG_BE(n,b,i) \ 00019 { \ 00020 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ 00021 | ( (unsigned long) (b)[(i) + 1] << 16 ) \ 00022 | ( (unsigned long) (b)[(i) + 2] << 8 ) \ 00023 | ( (unsigned long) (b)[(i) + 3] ); \ 00024 } 00025 #endif 00026 00027 #ifndef PUT_ULONG_BE 00028 #define PUT_ULONG_BE(n,b,i) \ 00029 { \ 00030 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 00031 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 00032 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 00033 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 00034 } 00035 #endif 00036 00037 int unhexify(unsigned char *obuf, const char *ibuf) 00038 { 00039 unsigned char c, c2; 00040 int len = strlen(ibuf) / 2; 00041 assert(!(strlen(ibuf) %1)); // must be even number of bytes 00042 00043 while (*ibuf != 0) 00044 { 00045 c = *ibuf++; 00046 if( c >= '0' && c <= '9' ) 00047 c -= '0'; 00048 else if( c >= 'a' && c <= 'f' ) 00049 c -= 'a' - 10; 00050 else if( c >= 'A' && c <= 'F' ) 00051 c -= 'A' - 10; 00052 else 00053 assert( 0 ); 00054 00055 c2 = *ibuf++; 00056 if( c2 >= '0' && c2 <= '9' ) 00057 c2 -= '0'; 00058 else if( c2 >= 'a' && c2 <= 'f' ) 00059 c2 -= 'a' - 10; 00060 else if( c2 >= 'A' && c2 <= 'F' ) 00061 c2 -= 'A' - 10; 00062 else 00063 assert( 0 ); 00064 00065 *obuf++ = ( c << 4 ) | c2; 00066 } 00067 00068 return len; 00069 } 00070 00071 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len) 00072 { 00073 unsigned char l, h; 00074 00075 while (len != 0) 00076 { 00077 h = (*ibuf) / 16; 00078 l = (*ibuf) % 16; 00079 00080 if( h < 10 ) 00081 *obuf++ = '0' + h; 00082 else 00083 *obuf++ = 'a' + h - 10; 00084 00085 if( l < 10 ) 00086 *obuf++ = '0' + l; 00087 else 00088 *obuf++ = 'a' + l - 10; 00089 00090 ++ibuf; 00091 len--; 00092 } 00093 } 00094 00104 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len ) 00105 { 00106 size_t i; 00107 00108 if( rng_state != NULL ) 00109 rng_state = NULL; 00110 00111 for( i = 0; i < len; ++i ) 00112 output[i] = rand(); 00113 00114 return( 0 ); 00115 } 00116 00122 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len ) 00123 { 00124 if( rng_state != NULL ) 00125 rng_state = NULL; 00126 00127 memset( output, 0, len ); 00128 00129 return( 0 ); 00130 } 00131 00132 typedef struct 00133 { 00134 unsigned char *buf; 00135 size_t length; 00136 } rnd_buf_info; 00137 00149 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len ) 00150 { 00151 rnd_buf_info *info = (rnd_buf_info *) rng_state; 00152 size_t use_len; 00153 00154 if( rng_state == NULL ) 00155 return( rnd_std_rand( NULL, output, len ) ); 00156 00157 use_len = len; 00158 if( len > info->length ) 00159 use_len = info->length; 00160 00161 if( use_len ) 00162 { 00163 memcpy( output, info->buf, use_len ); 00164 info->buf += use_len; 00165 info->length -= use_len; 00166 } 00167 00168 if( len - use_len > 0 ) 00169 return( rnd_std_rand( NULL, output + use_len, len - use_len ) ); 00170 00171 return( 0 ); 00172 } 00173 00181 typedef struct 00182 { 00183 uint32_t key[16]; 00184 uint32_t v0, v1; 00185 } rnd_pseudo_info; 00186 00195 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len ) 00196 { 00197 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; 00198 uint32_t i, *k, sum, delta=0x9E3779B9; 00199 unsigned char result[4]; 00200 00201 if( rng_state == NULL ) 00202 return( rnd_std_rand( NULL, output, len ) ); 00203 00204 k = info->key; 00205 00206 while( len > 0 ) 00207 { 00208 size_t use_len = ( len > 4 ) ? 4 : len; 00209 sum = 0; 00210 00211 for( i = 0; i < 32; i++ ) 00212 { 00213 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]); 00214 sum += delta; 00215 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]); 00216 } 00217 00218 PUT_ULONG_BE( info->v0, result, 0 ); 00219 memcpy( output, result, use_len ); 00220 len -= use_len; 00221 } 00222 00223 return( 0 ); 00224 } 00225 00226 00227 FCT_BGN() 00228 { 00229 #ifdef POLARSSL_XTEA_C 00230 00231 00232 FCT_SUITE_BGN(test_suite_xtea) 00233 { 00234 00235 FCT_TEST_BGN(xtea_encrypt_ecb_1) 00236 { 00237 unsigned char key_str[100]; 00238 unsigned char src_str[100]; 00239 unsigned char dst_str[100]; 00240 unsigned char output[100]; 00241 xtea_context ctx; 00242 00243 memset(key_str, 0x00, 100); 00244 memset(src_str, 0x00, 100); 00245 memset(dst_str, 0x00, 100); 00246 memset(output, 0x00, 100); 00247 00248 unhexify( key_str, "000102030405060708090a0b0c0d0e0f" ); 00249 unhexify( src_str, "4142434445464748" ); 00250 00251 xtea_setup( &ctx, key_str ); 00252 fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 ); 00253 hexify( dst_str, output, 8 ); 00254 00255 fct_chk( strcmp( (char *) dst_str, "497df3d072612cb5" ) == 0 ); 00256 } 00257 FCT_TEST_END(); 00258 00259 00260 FCT_TEST_BGN(xtea_encrypt_ecb_2) 00261 { 00262 unsigned char key_str[100]; 00263 unsigned char src_str[100]; 00264 unsigned char dst_str[100]; 00265 unsigned char output[100]; 00266 xtea_context ctx; 00267 00268 memset(key_str, 0x00, 100); 00269 memset(src_str, 0x00, 100); 00270 memset(dst_str, 0x00, 100); 00271 memset(output, 0x00, 100); 00272 00273 unhexify( key_str, "000102030405060708090a0b0c0d0e0f" ); 00274 unhexify( src_str, "4141414141414141" ); 00275 00276 xtea_setup( &ctx, key_str ); 00277 fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 ); 00278 hexify( dst_str, output, 8 ); 00279 00280 fct_chk( strcmp( (char *) dst_str, "e78f2d13744341d8" ) == 0 ); 00281 } 00282 FCT_TEST_END(); 00283 00284 00285 FCT_TEST_BGN(xtea_encrypt_ecb_3) 00286 { 00287 unsigned char key_str[100]; 00288 unsigned char src_str[100]; 00289 unsigned char dst_str[100]; 00290 unsigned char output[100]; 00291 xtea_context ctx; 00292 00293 memset(key_str, 0x00, 100); 00294 memset(src_str, 0x00, 100); 00295 memset(dst_str, 0x00, 100); 00296 memset(output, 0x00, 100); 00297 00298 unhexify( key_str, "000102030405060708090a0b0c0d0e0f" ); 00299 unhexify( src_str, "5a5b6e278948d77f" ); 00300 00301 xtea_setup( &ctx, key_str ); 00302 fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 ); 00303 hexify( dst_str, output, 8 ); 00304 00305 fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 ); 00306 } 00307 FCT_TEST_END(); 00308 00309 00310 FCT_TEST_BGN(xtea_encrypt_ecb_4) 00311 { 00312 unsigned char key_str[100]; 00313 unsigned char src_str[100]; 00314 unsigned char dst_str[100]; 00315 unsigned char output[100]; 00316 xtea_context ctx; 00317 00318 memset(key_str, 0x00, 100); 00319 memset(src_str, 0x00, 100); 00320 memset(dst_str, 0x00, 100); 00321 memset(output, 0x00, 100); 00322 00323 unhexify( key_str, "00000000000000000000000000000000" ); 00324 unhexify( src_str, "4142434445464748" ); 00325 00326 xtea_setup( &ctx, key_str ); 00327 fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 ); 00328 hexify( dst_str, output, 8 ); 00329 00330 fct_chk( strcmp( (char *) dst_str, "a0390589f8b8efa5" ) == 0 ); 00331 } 00332 FCT_TEST_END(); 00333 00334 00335 FCT_TEST_BGN(xtea_encrypt_ecb_5) 00336 { 00337 unsigned char key_str[100]; 00338 unsigned char src_str[100]; 00339 unsigned char dst_str[100]; 00340 unsigned char output[100]; 00341 xtea_context ctx; 00342 00343 memset(key_str, 0x00, 100); 00344 memset(src_str, 0x00, 100); 00345 memset(dst_str, 0x00, 100); 00346 memset(output, 0x00, 100); 00347 00348 unhexify( key_str, "00000000000000000000000000000000" ); 00349 unhexify( src_str, "4141414141414141" ); 00350 00351 xtea_setup( &ctx, key_str ); 00352 fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 ); 00353 hexify( dst_str, output, 8 ); 00354 00355 fct_chk( strcmp( (char *) dst_str, "ed23375a821a8c2d" ) == 0 ); 00356 } 00357 FCT_TEST_END(); 00358 00359 00360 FCT_TEST_BGN(xtea_encrypt_ecb_6) 00361 { 00362 unsigned char key_str[100]; 00363 unsigned char src_str[100]; 00364 unsigned char dst_str[100]; 00365 unsigned char output[100]; 00366 xtea_context ctx; 00367 00368 memset(key_str, 0x00, 100); 00369 memset(src_str, 0x00, 100); 00370 memset(dst_str, 0x00, 100); 00371 memset(output, 0x00, 100); 00372 00373 unhexify( key_str, "00000000000000000000000000000000" ); 00374 unhexify( src_str, "70e1225d6e4e7655" ); 00375 00376 xtea_setup( &ctx, key_str ); 00377 fct_chk( xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, src_str, output ) == 0 ); 00378 hexify( dst_str, output, 8 ); 00379 00380 fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 ); 00381 } 00382 FCT_TEST_END(); 00383 00384 00385 FCT_TEST_BGN(xtea_decrypt_ecb_1) 00386 { 00387 unsigned char key_str[100]; 00388 unsigned char src_str[100]; 00389 unsigned char dst_str[100]; 00390 unsigned char output[100]; 00391 xtea_context ctx; 00392 00393 memset(key_str, 0x00, 100); 00394 memset(src_str, 0x00, 100); 00395 memset(dst_str, 0x00, 100); 00396 memset(output, 0x00, 100); 00397 00398 unhexify( key_str, "000102030405060708090a0b0c0d0e0f" ); 00399 unhexify( src_str, "497df3d072612cb5" ); 00400 00401 xtea_setup( &ctx, key_str ); 00402 fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 ); 00403 hexify( dst_str, output, 8 ); 00404 00405 fct_chk( strcmp( (char *) dst_str, "4142434445464748" ) == 0 ); 00406 } 00407 FCT_TEST_END(); 00408 00409 00410 FCT_TEST_BGN(xtea_decrypt_ecb_2) 00411 { 00412 unsigned char key_str[100]; 00413 unsigned char src_str[100]; 00414 unsigned char dst_str[100]; 00415 unsigned char output[100]; 00416 xtea_context ctx; 00417 00418 memset(key_str, 0x00, 100); 00419 memset(src_str, 0x00, 100); 00420 memset(dst_str, 0x00, 100); 00421 memset(output, 0x00, 100); 00422 00423 unhexify( key_str, "000102030405060708090a0b0c0d0e0f" ); 00424 unhexify( src_str, "e78f2d13744341d8" ); 00425 00426 xtea_setup( &ctx, key_str ); 00427 fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 ); 00428 hexify( dst_str, output, 8 ); 00429 00430 fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 ); 00431 } 00432 FCT_TEST_END(); 00433 00434 00435 FCT_TEST_BGN(xtea_decrypt_ecb_3) 00436 { 00437 unsigned char key_str[100]; 00438 unsigned char src_str[100]; 00439 unsigned char dst_str[100]; 00440 unsigned char output[100]; 00441 xtea_context ctx; 00442 00443 memset(key_str, 0x00, 100); 00444 memset(src_str, 0x00, 100); 00445 memset(dst_str, 0x00, 100); 00446 memset(output, 0x00, 100); 00447 00448 unhexify( key_str, "000102030405060708090a0b0c0d0e0f" ); 00449 unhexify( src_str, "4141414141414141" ); 00450 00451 xtea_setup( &ctx, key_str ); 00452 fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 ); 00453 hexify( dst_str, output, 8 ); 00454 00455 fct_chk( strcmp( (char *) dst_str, "5a5b6e278948d77f" ) == 0 ); 00456 } 00457 FCT_TEST_END(); 00458 00459 00460 FCT_TEST_BGN(xtea_decrypt_ecb_4) 00461 { 00462 unsigned char key_str[100]; 00463 unsigned char src_str[100]; 00464 unsigned char dst_str[100]; 00465 unsigned char output[100]; 00466 xtea_context ctx; 00467 00468 memset(key_str, 0x00, 100); 00469 memset(src_str, 0x00, 100); 00470 memset(dst_str, 0x00, 100); 00471 memset(output, 0x00, 100); 00472 00473 unhexify( key_str, "00000000000000000000000000000000" ); 00474 unhexify( src_str, "a0390589f8b8efa5" ); 00475 00476 xtea_setup( &ctx, key_str ); 00477 fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 ); 00478 hexify( dst_str, output, 8 ); 00479 00480 fct_chk( strcmp( (char *) dst_str, "4142434445464748" ) == 0 ); 00481 } 00482 FCT_TEST_END(); 00483 00484 00485 FCT_TEST_BGN(xtea_decrypt_ecb_5) 00486 { 00487 unsigned char key_str[100]; 00488 unsigned char src_str[100]; 00489 unsigned char dst_str[100]; 00490 unsigned char output[100]; 00491 xtea_context ctx; 00492 00493 memset(key_str, 0x00, 100); 00494 memset(src_str, 0x00, 100); 00495 memset(dst_str, 0x00, 100); 00496 memset(output, 0x00, 100); 00497 00498 unhexify( key_str, "00000000000000000000000000000000" ); 00499 unhexify( src_str, "ed23375a821a8c2d" ); 00500 00501 xtea_setup( &ctx, key_str ); 00502 fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 ); 00503 hexify( dst_str, output, 8 ); 00504 00505 fct_chk( strcmp( (char *) dst_str, "4141414141414141" ) == 0 ); 00506 } 00507 FCT_TEST_END(); 00508 00509 00510 FCT_TEST_BGN(xtea_decrypt_ecb_6) 00511 { 00512 unsigned char key_str[100]; 00513 unsigned char src_str[100]; 00514 unsigned char dst_str[100]; 00515 unsigned char output[100]; 00516 xtea_context ctx; 00517 00518 memset(key_str, 0x00, 100); 00519 memset(src_str, 0x00, 100); 00520 memset(dst_str, 0x00, 100); 00521 memset(output, 0x00, 100); 00522 00523 unhexify( key_str, "00000000000000000000000000000000" ); 00524 unhexify( src_str, "4141414141414141" ); 00525 00526 xtea_setup( &ctx, key_str ); 00527 fct_chk( xtea_crypt_ecb( &ctx, XTEA_DECRYPT, src_str, output ) == 0 ); 00528 hexify( dst_str, output, 8 ); 00529 00530 fct_chk( strcmp( (char *) dst_str, "70e1225d6e4e7655" ) == 0 ); 00531 } 00532 FCT_TEST_END(); 00533 00534 #ifdef POLARSSL_SELF_TEST 00535 00536 FCT_TEST_BGN(xtea_selftest) 00537 { 00538 fct_chk( xtea_self_test( 0 ) == 0 ); 00539 } 00540 FCT_TEST_END(); 00541 #endif /* POLARSSL_SELF_TEST */ 00542 00543 } 00544 FCT_SUITE_END(); 00545 00546 #endif /* POLARSSL_XTEA_C */ 00547 00548 } 00549 FCT_END(); 00550