/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/test/benchmark.c

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

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4