PolarSSL v1.1.4
test_suite_arc4.c
Go to the documentation of this file.
00001 #include "fct.h"
00002 
00003 #include <polarssl/arc4.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_ARC4_C
00230 
00231 
00232     FCT_SUITE_BGN(test_suite_arc4)
00233     {
00234 
00235         FCT_TEST_BGN(test_vector_arc4_cryptlib)
00236         {
00237             unsigned char src_str[1000];
00238             unsigned char key_str[1000];
00239             unsigned char dst_str[1000];
00240             unsigned char dst_hexstr[2000];
00241             int src_len, key_len;
00242             arc4_context ctx;
00243         
00244             memset(src_str, 0x00, 1000);
00245             memset(key_str, 0x00, 1000);
00246             memset(dst_str, 0x00, 1000);
00247             memset(dst_hexstr, 0x00, 2000);
00248         
00249             src_len = unhexify( src_str, "0000000000000000" );
00250             key_len = unhexify( key_str, "0123456789abcdef" );
00251         
00252             arc4_setup(&ctx, key_str, key_len);
00253             fct_chk( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
00254             hexify( dst_hexstr, dst_str, src_len );
00255         
00256             fct_chk( strcmp( (char *) dst_hexstr, "7494c2e7104b0879" ) == 0 );
00257         }
00258         FCT_TEST_END();
00259 
00260 
00261         FCT_TEST_BGN(test_vector_arc4_commerce)
00262         {
00263             unsigned char src_str[1000];
00264             unsigned char key_str[1000];
00265             unsigned char dst_str[1000];
00266             unsigned char dst_hexstr[2000];
00267             int src_len, key_len;
00268             arc4_context ctx;
00269         
00270             memset(src_str, 0x00, 1000);
00271             memset(key_str, 0x00, 1000);
00272             memset(dst_str, 0x00, 1000);
00273             memset(dst_hexstr, 0x00, 2000);
00274         
00275             src_len = unhexify( src_str, "dcee4cf92c" );
00276             key_len = unhexify( key_str, "618a63d2fb" );
00277         
00278             arc4_setup(&ctx, key_str, key_len);
00279             fct_chk( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
00280             hexify( dst_hexstr, dst_str, src_len );
00281         
00282             fct_chk( strcmp( (char *) dst_hexstr, "f13829c9de" ) == 0 );
00283         }
00284         FCT_TEST_END();
00285 
00286 
00287         FCT_TEST_BGN(test_vector_arc4_ssh_arcfour)
00288         {
00289             unsigned char src_str[1000];
00290             unsigned char key_str[1000];
00291             unsigned char dst_str[1000];
00292             unsigned char dst_hexstr[2000];
00293             int src_len, key_len;
00294             arc4_context ctx;
00295         
00296             memset(src_str, 0x00, 1000);
00297             memset(key_str, 0x00, 1000);
00298             memset(dst_str, 0x00, 1000);
00299             memset(dst_hexstr, 0x00, 2000);
00300         
00301             src_len = unhexify( src_str, "527569736c696e6e756e206c61756c75206b6f727669737373616e692c2074e4686be470e46964656e2070e4e46c6ce42074e47973696b75752e204b6573e479f66e206f6e206f6e6e69206f6d616e616e692c206b61736b6973617675756e206c61616b736f7420766572686f75752e20456e206d6120696c6f697473652c20737572652068756f6b61612c206d75747461206d657473e46e2074756d6d757573206d756c6c652074756f6b61612e205075756e746f2070696c76656e2c206d692068756b6b75752c207369696e746f20766172616e207475756c6973656e2c206d69206e756b6b75752e2054756f6b7375742076616e616d6f6e206a61207661726a6f74207665656e2c206e69697374e420737964e46d656e69206c61756c756e207465656e2e202d2045696e6f204c65696e6f" );
00302             key_len = unhexify( key_str, "29041972fb42ba5fc7127712f13829c9" );
00303         
00304             arc4_setup(&ctx, key_str, key_len);
00305             fct_chk( arc4_crypt(&ctx, src_len, src_str, dst_str ) == 0 );
00306             hexify( dst_hexstr, dst_str, src_len );
00307         
00308             fct_chk( strcmp( (char *) dst_hexstr, "358186999001e6b5daf05eceeb7eee21e0689c1f00eea81f7dd2caaee1d2763e68af0ead33d66c268bc946c484fbe94c5f5e0b86a59279e4f824e7a640bd223210b0a61160b7bce986ea65688003596b630a6b90f8e0caf6912a98eb872176e83c202caa64166d2cce57ff1bca57b213f0ed1aa72fb8ea52b0be01cd1e412867720b326eb389d011bd70d8af035fb0d8589dbce3c666f5ea8d4c7954c50c3f340b0467f81b425961c11843074df620f208404b394cf9d37ff54b5f1ad8f6ea7da3c561dfa7281f964463d2cc35a4d1b03490dec51b0711fbd6f55f79234d5b7c766622a66de92be996461d5e4dc878ef9bca030521e8351e4baed2fd04f9467368c4ad6ac186d08245b263a2666d1f6c5420f1599dfd9f438921c2f5a463938ce0982265eef70179bc553f339eb1a4c1af5f6a547f" ) == 0 );
00309         }
00310         FCT_TEST_END();
00311 
00312 #ifdef POLARSSL_SELF_TEST
00313 
00314         FCT_TEST_BGN(arc4_selftest)
00315         {
00316             fct_chk( arc4_self_test( 0 ) == 0 );
00317         }
00318         FCT_TEST_END();
00319 #endif /* POLARSSL_SELF_TEST */
00320 
00321     }
00322     FCT_SUITE_END();
00323 
00324 #endif /* POLARSSL_ARC4_C */
00325 
00326 }
00327 FCT_END();
00328