PolarSSL v1.1.4
test_suite_version.c
Go to the documentation of this file.
00001 #include "fct.h"
00002 
00003 #include <polarssl/version.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_VERSION_C
00230 
00231 
00232     FCT_SUITE_BGN(test_suite_version)
00233     {
00234 
00235         FCT_TEST_BGN(check_compiletime_library_version)
00236         {
00237             char build_str[100];
00238             char build_str_full[100];
00239             unsigned int build_int;
00240         
00241             memset( build_str, 0, 100 );
00242             memset( build_str_full, 0, 100 );
00243         
00244             snprintf (build_str, 100, "%d.%d.%d", POLARSSL_VERSION_MAJOR,
00245                 POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH );
00246         
00247             snprintf( build_str_full, 100, "PolarSSL %d.%d.%d", POLARSSL_VERSION_MAJOR,
00248                 POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH );
00249         
00250             build_int = POLARSSL_VERSION_MAJOR << 24 |
00251                     POLARSSL_VERSION_MINOR << 16 |
00252                     POLARSSL_VERSION_PATCH << 8;
00253         
00254             fct_chk( build_int == POLARSSL_VERSION_NUMBER );
00255             fct_chk( strcmp( build_str, POLARSSL_VERSION_STRING ) == 0 );
00256             fct_chk( strcmp( build_str_full, POLARSSL_VERSION_STRING_FULL ) == 0 );
00257             fct_chk( strcmp( "1.1.4", POLARSSL_VERSION_STRING ) == 0 );
00258         }
00259         FCT_TEST_END();
00260 
00261 
00262         FCT_TEST_BGN(check_runtime_library_version)
00263         {
00264             char build_str[100];
00265             char get_str[100];
00266             char build_str_full[100];
00267             char get_str_full[100];
00268             unsigned int get_int;
00269         
00270             memset( build_str, 0, 100 );
00271             memset( get_str, 0, 100 );
00272             memset( build_str_full, 0, 100 );
00273             memset( get_str_full, 0, 100 );
00274         
00275             get_int = version_get_number();
00276             version_get_string( get_str );
00277             version_get_string_full( get_str_full );
00278         
00279             snprintf( build_str, 100, "%d.%d.%d",
00280                 (get_int >> 24) & 0xFF,
00281                 (get_int >> 16) & 0xFF,
00282                 (get_int >> 8) & 0xFF );
00283             snprintf( build_str_full, 100, "PolarSSL %s", "1.1.4" );
00284         
00285             fct_chk( strcmp( build_str, "1.1.4" ) == 0 );
00286             fct_chk( strcmp( build_str_full, get_str_full ) == 0 );
00287             fct_chk( strcmp( "1.1.4", get_str ) == 0 );
00288         }
00289         FCT_TEST_END();
00290 
00291     }
00292     FCT_SUITE_END();
00293 
00294 #endif /* POLARSSL_VERSION_C */
00295 
00296 }
00297 FCT_END();
00298