/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/security/xyssl-0.9/programs/test/benchmark.c

Go to the documentation of this file.
00001 /*
00002  *  Benchmark demonstration program
00003  *
00004  *  Copyright (C) 2006-2007  Christophe Devine
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #ifndef _CRT_SECURE_NO_DEPRECATE
00022 #define _CRT_SECURE_NO_DEPRECATE 1
00023 #endif
00024 
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 
00029 #include "xyssl/config.h"
00030 
00031 #include "xyssl/md4.h"
00032 #include "xyssl/md5.h"
00033 #include "xyssl/sha1.h"
00034 #include "xyssl/sha2.h"
00035 #include "xyssl/arc4.h"
00036 #include "xyssl/des.h"
00037 #include "xyssl/aes.h"
00038 #include "xyssl/rsa.h"
00039 #include "xyssl/timing.h"
00040 
00041 #define BUFSIZE 1024
00042 
00043 static int myrand( void *rng_state )
00044 {
00045     if( rng_state != NULL )
00046         rng_state  = NULL;
00047 
00048     return( rand() );
00049 }
00050 
00051 unsigned char buf[BUFSIZE];
00052 
00053 int main( void )
00054 {
00055     int keysize;
00056     unsigned long i, j, tsc;
00057     unsigned char tmp[32];
00058 #if defined(XYSSL_ARC4_C)
00059     arc4_context arc4;
00060 #endif
00061 #if defined(XYSSL_DES_C)
00062     des3_context des3;
00063     des_context des;
00064 #endif
00065 #if defined(XYSSL_AES_C)
00066     aes_context aes;
00067 #endif
00068 #if defined(XYSSL_RSA_C)
00069     rsa_context rsa;
00070 #endif
00071 
00072     memset( buf, 0xAA, sizeof( buf ) );
00073 
00074     printf( "\n" );
00075 
00076 #if defined(XYSSL_MD4_C)
00077     printf( "  MD4       :  " );
00078     fflush( stdout );
00079 
00080     set_alarm( 1 );
00081     for( i = 1; ! alarmed; i++ )
00082         md4( buf, BUFSIZE, tmp );
00083 
00084     tsc = hardclock();
00085     for( j = 0; j < 1024; j++ )
00086         md4( buf, BUFSIZE, tmp );
00087 
00088     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00089                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00090 #endif
00091 
00092 #if defined(XYSSL_MD5_C)
00093     printf( "  MD5       :  " );
00094     fflush( stdout );
00095 
00096     set_alarm( 1 );
00097     for( i = 1; ! alarmed; i++ )
00098         md5( buf, BUFSIZE, tmp );
00099 
00100     tsc = hardclock();
00101     for( j = 0; j < 1024; j++ )
00102         md5( buf, BUFSIZE, tmp );
00103 
00104     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00105                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00106 #endif
00107 
00108 #if defined(XYSSL_SHA1_C)
00109     printf( "  SHA-1     :  " );
00110     fflush( stdout );
00111 
00112     set_alarm( 1 );
00113     for( i = 1; ! alarmed; i++ )
00114         sha1( buf, BUFSIZE, tmp );
00115 
00116     tsc = hardclock();
00117     for( j = 0; j < 1024; j++ )
00118         sha1( buf, BUFSIZE, tmp );
00119 
00120     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00121                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00122 #endif
00123 
00124 #if defined(XYSSL_SHA2_C)
00125     printf( "  SHA-256   :  " );
00126     fflush( stdout );
00127 
00128     set_alarm( 1 );
00129     for( i = 1; ! alarmed; i++ )
00130         sha2( buf, BUFSIZE, tmp, 0 );
00131 
00132     tsc = hardclock();
00133     for( j = 0; j < 1024; j++ )
00134         sha2( buf, BUFSIZE, tmp, 0 );
00135 
00136     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00137                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00138 #endif
00139 
00140 #if defined(XYSSL_ARC4_C)
00141     printf( "  ARC4      :  " );
00142     fflush( stdout );
00143 
00144     arc4_setup( &arc4, tmp, 32 );
00145 
00146     set_alarm( 1 );
00147     for( i = 1; ! alarmed; i++ )
00148         arc4_crypt( &arc4, buf, BUFSIZE );
00149 
00150     tsc = hardclock();
00151     for( j = 0; j < 1024; j++ )
00152         arc4_crypt( &arc4, buf, BUFSIZE );
00153 
00154     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00155                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00156 #endif
00157 
00158 #if defined(XYSSL_DES_C)
00159     printf( "  3DES      :  " );
00160     fflush( stdout );
00161 
00162     des3_set3key_enc( &des3, tmp );
00163 
00164     set_alarm( 1 );
00165     for( i = 1; ! alarmed; i++ )
00166         des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
00167 
00168     tsc = hardclock();
00169     for( j = 0; j < 1024; j++ )
00170         des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
00171 
00172     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00173                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00174 
00175     printf( "  DES       :  " );
00176     fflush( stdout );
00177 
00178     des_setkey_enc( &des, tmp );
00179 
00180     set_alarm( 1 );
00181     for( i = 1; ! alarmed; i++ )
00182         des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
00183 
00184     tsc = hardclock();
00185     for( j = 0; j < 1024; j++ )
00186         des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
00187 
00188     printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00189                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00190 #endif
00191 
00192 #if defined(XYSSL_AES_C)
00193     for( keysize = 128; keysize <= 256; keysize += 64 )
00194     {
00195         printf( "  AES-%d   :  ", keysize );
00196         fflush( stdout );
00197 
00198         memset( buf, 0, sizeof( buf ) );
00199         memset( tmp, 0, sizeof( tmp ) );
00200         aes_setkey_enc( &aes, tmp, keysize );
00201 
00202         set_alarm( 1 );
00203 
00204         for( i = 1; ! alarmed; i++ )
00205             aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
00206 
00207         tsc = hardclock();
00208         for( j = 0; j < 4096; j++ )
00209             aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
00210 
00211         printf( "%9lu Kb/s,  %9lu cycles/byte\n", i * BUFSIZE / 1024,
00212                         ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00213     }
00214 #endif
00215 
00216 #if defined(XYSSL_RSA_C)
00217     rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
00218     rsa_gen_key( &rsa, 1024, 65537 );
00219 
00220     printf( "  RSA-1024  :  " );
00221     fflush( stdout );
00222     set_alarm( 3 );
00223 
00224     for( i = 1; ! alarmed; i++ )
00225     {
00226         buf[0] = 0;
00227         rsa_public( &rsa, buf, buf );
00228     }
00229 
00230     printf( "%9lu  public/s\n", i / 3 );
00231 
00232     printf( "  RSA-1024  :  " );
00233     fflush( stdout );
00234     set_alarm( 3 );
00235 
00236     for( i = 1; ! alarmed; i++ )
00237     {
00238         buf[0] = 0;
00239         rsa_private( &rsa, buf, buf );
00240     }
00241 
00242     printf( "%9lu private/s\n\n", i / 3 );
00243 
00244     rsa_free( &rsa );
00245 #endif
00246 
00247 #ifdef WIN32
00248     printf( "  Press Enter to exit this program.\n" );
00249     fflush( stdout ); getchar();
00250 #endif
00251 
00252     return( 0 );
00253 }

Generated on Fri Jul 11 17:59:46 2008 for Mobile-C by  doxygen 1.5.4