/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/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 library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License, version 2.1 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  *  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/md4.h"
00030 #include "xyssl/md5.h"
00031 #include "xyssl/sha1.h"
00032 #include "xyssl/sha2.h"
00033 #include "xyssl/arc4.h"
00034 #include "xyssl/des.h"
00035 #include "xyssl/aes.h"
00036 #include "xyssl/rsa.h"
00037 #include "xyssl/timing.h"
00038 
00039 #define BUFSIZE 510
00040 
00041 int myrand( void *rng_state )
00042 {
00043     rng_state = NULL;
00044     return( rand() );
00045 }
00046 
00047 int main( void )
00048 {
00049     int keysize;
00050     unsigned long i, j, tsc;
00051     unsigned char buf[BUFSIZE];
00052     unsigned char tmp[32];
00053     arc4_context arc4;
00054     des3_context des3;
00055     des_context des;
00056     aes_context aes;
00057     rsa_context rsa;
00058 
00059     memset( buf, 0xAA, sizeof( buf ) );
00060 
00061     printf( "\n" );
00062 
00063     /*
00064      * MD4 timing
00065      */ 
00066     printf( "  MD4       :  " );
00067     fflush( stdout );
00068 
00069     set_alarm( 1 );
00070     for( i = 1; ! alarmed; i++ )
00071         md4( buf, BUFSIZE, tmp );
00072 
00073     tsc = hardclock();
00074     for( j = 0; j < 1024; j++ )
00075         md4( buf, BUFSIZE, tmp );
00076 
00077     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00078                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00079 
00080     /*
00081      * MD5 timing
00082      */ 
00083     printf( "  MD5       :  " );
00084     fflush( stdout );
00085 
00086     set_alarm( 1 );
00087     for( i = 1; ! alarmed; i++ )
00088         md5( buf, BUFSIZE, tmp );
00089 
00090     tsc = hardclock();
00091     for( j = 0; j < 1024; j++ )
00092         md5( buf, BUFSIZE, tmp );
00093 
00094     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00095                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00096 
00097     /*
00098      * SHA-1 timing
00099      */ 
00100     printf( "  SHA-1     :  " );
00101     fflush( stdout );
00102 
00103     set_alarm( 1 );
00104     for( i = 1; ! alarmed; i++ )
00105         sha1( buf, BUFSIZE, tmp );
00106 
00107     tsc = hardclock();
00108     for( j = 0; j < 1024; j++ )
00109         sha1( buf, BUFSIZE, tmp );
00110 
00111     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00112                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00113 
00114     /*
00115      * SHA-256 timing
00116      */ 
00117     printf( "  SHA-256   :  " );
00118     fflush( stdout );
00119 
00120     set_alarm( 1 );
00121     for( i = 1; ! alarmed; i++ )
00122         sha2( buf, BUFSIZE, tmp, 0 );
00123 
00124     tsc = hardclock();
00125     for( j = 0; j < 1024; j++ )
00126         sha2( buf, BUFSIZE, tmp, 0 );
00127 
00128     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00129                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00130 
00131     /*
00132      * ARC4 timing
00133      */ 
00134     printf( "  ARC4      :  " );
00135     fflush( stdout );
00136 
00137     arc4_setup( &arc4, tmp, 32 );
00138 
00139     set_alarm( 1 );
00140     for( i = 1; ! alarmed; i++ )
00141         arc4_crypt( &arc4, buf, BUFSIZE );
00142 
00143     tsc = hardclock();
00144     for( j = 0; j < 1024; j++ )
00145         arc4_crypt( &arc4, buf, BUFSIZE );
00146 
00147     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00148                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00149 
00150     /*
00151      * Triple-DES timing
00152      */ 
00153     printf( "  3DES      :  " );
00154     fflush( stdout );
00155 
00156     des3_set_3keys( &des3, tmp );
00157 
00158     set_alarm( 1 );
00159     for( i = 1; ! alarmed; i++ )
00160         des3_cbc_encrypt( &des3, tmp, buf, buf, BUFSIZE );
00161 
00162     tsc = hardclock();
00163     for( j = 0; j < 1024; j++ )
00164         des3_cbc_encrypt( &des3, tmp, buf, buf, BUFSIZE );
00165 
00166     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00167                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00168 
00169     /*
00170      * DES timing
00171      */ 
00172     printf( "  DES       :  " );
00173     fflush( stdout );
00174 
00175     des_set_key( &des, tmp );
00176 
00177     set_alarm( 1 );
00178     for( i = 1; ! alarmed; i++ )
00179         des_cbc_encrypt( &des, tmp, buf, buf, BUFSIZE );
00180 
00181     tsc = hardclock();
00182     for( j = 0; j < 1024; j++ )
00183         des_cbc_encrypt( &des, tmp, buf, buf, BUFSIZE );
00184 
00185     printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00186                     ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00187 
00188     /*
00189      * AES timings
00190      */ 
00191     for( keysize = 128; keysize <= 256; keysize += 64 )
00192     {
00193         printf( "  AES-%d   :  ", keysize );
00194         fflush( stdout );
00195 
00196         aes_set_key( &aes, tmp, keysize );
00197 
00198         set_alarm( 1 );
00199 
00200         for( i = 1; ! alarmed; i++ )
00201             aes_cbc_encrypt( &aes, tmp, buf, buf, BUFSIZE );
00202 
00203         tsc = hardclock();
00204         for( j = 0; j < 1024; j++ )
00205             aes_cbc_encrypt( &aes, tmp, buf, buf, BUFSIZE );
00206 
00207         printf( "%9ld Kb/s,  %9ld cycles/byte\n", i * BUFSIZE / 1024,
00208                         ( hardclock() - tsc ) / ( j * BUFSIZE ) );
00209     }
00210 
00211     /*
00212      * RSA-1024 timing
00213      */ 
00214     memset( &rsa, 0, sizeof( rsa ) );
00215 
00216     rsa.len = KEY_LEN;
00217     mpi_read_string( &rsa.N , 16, RSA_N  );
00218     mpi_read_string( &rsa.E , 16, RSA_E  );
00219     mpi_read_string( &rsa.D , 16, RSA_D  );
00220     mpi_read_string( &rsa.P , 16, RSA_P  );
00221     mpi_read_string( &rsa.Q , 16, RSA_Q  );
00222     mpi_read_string( &rsa.DP, 16, RSA_DP );
00223     mpi_read_string( &rsa.DQ, 16, RSA_DQ );
00224     mpi_read_string( &rsa.QP, 16, RSA_QP );
00225 
00226     printf( "  RSA-1024  :  " );
00227     fflush( stdout );
00228     set_alarm( 3 );
00229 
00230     for( i = 1; ! alarmed; i++ )
00231     {
00232         buf[0] = 0;
00233         rsa_public( &rsa, buf, 128, buf, 128 );
00234     }
00235 
00236     printf( "%9ld  public/s\n", i / 3 );
00237 
00238     printf( "  RSA-1024  :  " );
00239     fflush( stdout );
00240     set_alarm( 3 );
00241 
00242     for( i = 1; ! alarmed; i++ )
00243     {
00244         buf[0] = 0;
00245         rsa_private( &rsa, buf, 128, buf, 128 );
00246     }
00247 
00248     printf( "%9ld private/s\n\n", i / 3 );
00249 
00250     rsa_free( &rsa );
00251 
00252 #ifdef WIN32
00253     printf( "  Press Enter to exit this program.\n" );
00254     fflush( stdout ); getchar();
00255 #endif
00256 
00257     return( 0 );
00258 }

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4