PolarSSL v1.1.4
test_suite_error.c
Go to the documentation of this file.
00001 #include "fct.h"
00002 
00003 #include <polarssl/error.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_ERROR_C
00230 
00231 
00232     FCT_SUITE_BGN(test_suite_error)
00233     {
00234 #ifdef POLARSSL_AES_C
00235 
00236         FCT_TEST_BGN(single_low_error)
00237         {
00238             char buf[500];
00239         
00240             error_strerror( -0x0020, buf, 500 );
00241         
00242             fct_chk( strcmp( buf, "AES - Invalid key length" ) == 0 );
00243         }
00244         FCT_TEST_END();
00245 #endif /* POLARSSL_AES_C */
00246 
00247 #ifdef POLARSSL_RSA_C
00248 
00249         FCT_TEST_BGN(single_high_error)
00250         {
00251             char buf[500];
00252         
00253             error_strerror( -0x4080, buf, 500 );
00254         
00255             fct_chk( strcmp( buf, "RSA - Bad input parameters to function" ) == 0 );
00256         }
00257         FCT_TEST_END();
00258 #endif /* POLARSSL_RSA_C */
00259 
00260 #ifdef POLARSSL_AES_C
00261 #ifdef POLARSSL_RSA_C
00262 
00263         FCT_TEST_BGN(low_and_high_error)
00264         {
00265             char buf[500];
00266         
00267             error_strerror( -0x40A0, buf, 500 );
00268         
00269             fct_chk( strcmp( buf, "RSA - Bad input parameters to function : AES - Invalid key length" ) == 0 );
00270         }
00271         FCT_TEST_END();
00272 #endif /* POLARSSL_AES_C */
00273 #endif /* POLARSSL_RSA_C */
00274 
00275 
00276         FCT_TEST_BGN(non_existing_high_error)
00277         {
00278             char buf[500];
00279         
00280             error_strerror( -0x8880, buf, 500 );
00281         
00282             fct_chk( strcmp( buf, "UNKNOWN ERROR CODE (8880)" ) == 0 );
00283         }
00284         FCT_TEST_END();
00285 
00286 
00287         FCT_TEST_BGN(non_existing_low_error)
00288         {
00289             char buf[500];
00290         
00291             error_strerror( -0x0001, buf, 500 );
00292         
00293             fct_chk( strcmp( buf, "UNKNOWN ERROR CODE (0001)" ) == 0 );
00294         }
00295         FCT_TEST_END();
00296 
00297 
00298         FCT_TEST_BGN(non_existing_low_and_high_error)
00299         {
00300             char buf[500];
00301         
00302             error_strerror( -0x8881, buf, 500 );
00303         
00304             fct_chk( strcmp( buf, "UNKNOWN ERROR CODE (8880) : UNKNOWN ERROR CODE (0001)" ) == 0 );
00305         }
00306         FCT_TEST_END();
00307 
00308     }
00309     FCT_SUITE_END();
00310 
00311 #endif /* POLARSSL_ERROR_C */
00312 
00313 }
00314 FCT_END();
00315