blob: 5058cffecd8e59f1422399f24d8e11498ef0d1bf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
4 * Copyright (C) 2006-2007 Christophe Devine
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#ifndef _CRT_SECURE_NO_DEPRECATE
22#define _CRT_SECURE_NO_DEPRECATE 1
23#endif
24
25#include <string.h>
26#include <stdlib.h>
27#include <stdio.h>
28
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/md4.h"
32#include "polarssl/md5.h"
33#include "polarssl/sha1.h"
34#include "polarssl/sha2.h"
35#include "polarssl/arc4.h"
36#include "polarssl/des.h"
37#include "polarssl/aes.h"
38#include "polarssl/rsa.h"
39#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#define BUFSIZE 1024
42
43static int myrand( void *rng_state )
44{
45 if( rng_state != NULL )
46 rng_state = NULL;
47
48 return( rand() );
49}
50
51unsigned char buf[BUFSIZE];
52
53int main( void )
54{
55 int keysize;
56 unsigned long i, j, tsc;
57 unsigned char tmp[32];
Paul Bakker40e46942009-01-03 21:51:57 +000058#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000059 arc4_context arc4;
60#endif
Paul Bakker40e46942009-01-03 21:51:57 +000061#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000062 des3_context des3;
63 des_context des;
64#endif
Paul Bakker40e46942009-01-03 21:51:57 +000065#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000066 aes_context aes;
67#endif
Paul Bakker40e46942009-01-03 21:51:57 +000068#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000069 rsa_context rsa;
70#endif
71
72 memset( buf, 0xAA, sizeof( buf ) );
73
74 printf( "\n" );
75
Paul Bakker40e46942009-01-03 21:51:57 +000076#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000077 printf( " MD4 : " );
78 fflush( stdout );
79
80 set_alarm( 1 );
81 for( i = 1; ! alarmed; i++ )
82 md4( buf, BUFSIZE, tmp );
83
84 tsc = hardclock();
85 for( j = 0; j < 1024; j++ )
86 md4( buf, BUFSIZE, tmp );
87
88 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
89 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
90#endif
91
Paul Bakker40e46942009-01-03 21:51:57 +000092#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000093 printf( " MD5 : " );
94 fflush( stdout );
95
96 set_alarm( 1 );
97 for( i = 1; ! alarmed; i++ )
98 md5( buf, BUFSIZE, tmp );
99
100 tsc = hardclock();
101 for( j = 0; j < 1024; j++ )
102 md5( buf, BUFSIZE, tmp );
103
104 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
105 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
106#endif
107
Paul Bakker40e46942009-01-03 21:51:57 +0000108#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 printf( " SHA-1 : " );
110 fflush( stdout );
111
112 set_alarm( 1 );
113 for( i = 1; ! alarmed; i++ )
114 sha1( buf, BUFSIZE, tmp );
115
116 tsc = hardclock();
117 for( j = 0; j < 1024; j++ )
118 sha1( buf, BUFSIZE, tmp );
119
120 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
121 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
122#endif
123
Paul Bakker40e46942009-01-03 21:51:57 +0000124#if defined(POLARSSL_SHA2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 printf( " SHA-256 : " );
126 fflush( stdout );
127
128 set_alarm( 1 );
129 for( i = 1; ! alarmed; i++ )
130 sha2( buf, BUFSIZE, tmp, 0 );
131
132 tsc = hardclock();
133 for( j = 0; j < 1024; j++ )
134 sha2( buf, BUFSIZE, tmp, 0 );
135
136 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
137 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
138#endif
139
Paul Bakker40e46942009-01-03 21:51:57 +0000140#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 printf( " ARC4 : " );
142 fflush( stdout );
143
144 arc4_setup( &arc4, tmp, 32 );
145
146 set_alarm( 1 );
147 for( i = 1; ! alarmed; i++ )
148 arc4_crypt( &arc4, buf, BUFSIZE );
149
150 tsc = hardclock();
151 for( j = 0; j < 1024; j++ )
152 arc4_crypt( &arc4, buf, BUFSIZE );
153
154 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
155 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
156#endif
157
Paul Bakker40e46942009-01-03 21:51:57 +0000158#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 printf( " 3DES : " );
160 fflush( stdout );
161
162 des3_set3key_enc( &des3, tmp );
163
164 set_alarm( 1 );
165 for( i = 1; ! alarmed; i++ )
166 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
167
168 tsc = hardclock();
169 for( j = 0; j < 1024; j++ )
170 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
171
172 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
173 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
174
175 printf( " DES : " );
176 fflush( stdout );
177
178 des_setkey_enc( &des, tmp );
179
180 set_alarm( 1 );
181 for( i = 1; ! alarmed; i++ )
182 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
183
184 tsc = hardclock();
185 for( j = 0; j < 1024; j++ )
186 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
187
188 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
189 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
190#endif
191
Paul Bakker40e46942009-01-03 21:51:57 +0000192#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 for( keysize = 128; keysize <= 256; keysize += 64 )
194 {
195 printf( " AES-%d : ", keysize );
196 fflush( stdout );
197
198 memset( buf, 0, sizeof( buf ) );
199 memset( tmp, 0, sizeof( tmp ) );
200 aes_setkey_enc( &aes, tmp, keysize );
201
202 set_alarm( 1 );
203
204 for( i = 1; ! alarmed; i++ )
205 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
206
207 tsc = hardclock();
208 for( j = 0; j < 4096; j++ )
209 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
210
211 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
212 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
213 }
214#endif
215
Paul Bakker40e46942009-01-03 21:51:57 +0000216#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
218 rsa_gen_key( &rsa, 1024, 65537 );
219
220 printf( " RSA-1024 : " );
221 fflush( stdout );
222 set_alarm( 3 );
223
224 for( i = 1; ! alarmed; i++ )
225 {
226 buf[0] = 0;
227 rsa_public( &rsa, buf, buf );
228 }
229
230 printf( "%9lu public/s\n", i / 3 );
231
232 printf( " RSA-1024 : " );
233 fflush( stdout );
234 set_alarm( 3 );
235
236 for( i = 1; ! alarmed; i++ )
237 {
238 buf[0] = 0;
239 rsa_private( &rsa, buf, buf );
240 }
241
242 printf( "%9lu private/s\n\n", i / 3 );
243
244 rsa_free( &rsa );
245#endif
246
247#ifdef WIN32
248 printf( " Press Enter to exit this program.\n" );
249 fflush( stdout ); getchar();
250#endif
251
252 return( 0 );
253}