blob: 091a9cf898f37bdc6bea5d60250532139da2046e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/md4.h"
37#include "polarssl/md5.h"
38#include "polarssl/sha1.h"
39#include "polarssl/sha2.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000040#include "polarssl/sha4.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/arc4.h"
42#include "polarssl/des.h"
43#include "polarssl/aes.h"
Paul Bakker38119b12009-01-10 23:31:23 +000044#include "polarssl/camellia.h"
Paul Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/rsa.h"
46#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000047
48#define BUFSIZE 1024
49
50static int myrand( void *rng_state )
51{
52 if( rng_state != NULL )
53 rng_state = NULL;
54
55 return( rand() );
56}
57
58unsigned char buf[BUFSIZE];
59
Paul Bakker5690efc2011-05-26 13:16:06 +000060#if !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000061int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000062{
Paul Bakkercce9d772011-11-18 14:26:47 +000063 ((void) argc);
64 ((void) argv);
65
Paul Bakker5690efc2011-05-26 13:16:06 +000066 printf("POLARSSL_TIMING_C not defined.\n");
67 return( 0 );
68}
69#else
Paul Bakkercce9d772011-11-18 14:26:47 +000070int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
72 int keysize;
73 unsigned long i, j, tsc;
Paul Bakker5a0aa772009-02-09 22:38:52 +000074 unsigned char tmp[64];
Paul Bakker40e46942009-01-03 21:51:57 +000075#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000076 arc4_context arc4;
77#endif
Paul Bakker40e46942009-01-03 21:51:57 +000078#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000079 des3_context des3;
80 des_context des;
81#endif
Paul Bakker40e46942009-01-03 21:51:57 +000082#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000083 aes_context aes;
84#endif
Paul Bakker38119b12009-01-10 23:31:23 +000085#if defined(POLARSSL_CAMELLIA_C)
86 camellia_context camellia;
87#endif
Paul Bakker5690efc2011-05-26 13:16:06 +000088#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
89 defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000090 rsa_context rsa;
91#endif
92
Paul Bakkercce9d772011-11-18 14:26:47 +000093 ((void) argc);
94 ((void) argv);
95
Paul Bakker5121ce52009-01-03 21:22:43 +000096 memset( buf, 0xAA, sizeof( buf ) );
97
98 printf( "\n" );
99
Paul Bakker40e46942009-01-03 21:51:57 +0000100#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 printf( " MD4 : " );
102 fflush( stdout );
103
104 set_alarm( 1 );
105 for( i = 1; ! alarmed; i++ )
106 md4( buf, BUFSIZE, tmp );
107
108 tsc = hardclock();
109 for( j = 0; j < 1024; j++ )
110 md4( buf, BUFSIZE, tmp );
111
112 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
113 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
114#endif
115
Paul Bakker40e46942009-01-03 21:51:57 +0000116#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 printf( " MD5 : " );
118 fflush( stdout );
119
120 set_alarm( 1 );
121 for( i = 1; ! alarmed; i++ )
122 md5( buf, BUFSIZE, tmp );
123
124 tsc = hardclock();
125 for( j = 0; j < 1024; j++ )
126 md5( buf, BUFSIZE, tmp );
127
128 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
129 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
130#endif
131
Paul Bakker40e46942009-01-03 21:51:57 +0000132#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 printf( " SHA-1 : " );
134 fflush( stdout );
135
136 set_alarm( 1 );
137 for( i = 1; ! alarmed; i++ )
138 sha1( buf, BUFSIZE, tmp );
139
140 tsc = hardclock();
141 for( j = 0; j < 1024; j++ )
142 sha1( buf, BUFSIZE, tmp );
143
144 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
145 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
146#endif
147
Paul Bakker40e46942009-01-03 21:51:57 +0000148#if defined(POLARSSL_SHA2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 printf( " SHA-256 : " );
150 fflush( stdout );
151
152 set_alarm( 1 );
153 for( i = 1; ! alarmed; i++ )
154 sha2( buf, BUFSIZE, tmp, 0 );
155
156 tsc = hardclock();
157 for( j = 0; j < 1024; j++ )
158 sha2( buf, BUFSIZE, tmp, 0 );
159
160 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
161 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
162#endif
163
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000164#if defined(POLARSSL_SHA4_C)
165 printf( " SHA-512 : " );
166 fflush( stdout );
167
168 set_alarm( 1 );
169 for( i = 1; ! alarmed; i++ )
170 sha4( buf, BUFSIZE, tmp, 0 );
171
172 tsc = hardclock();
173 for( j = 0; j < 1024; j++ )
174 sha4( buf, BUFSIZE, tmp, 0 );
175
176 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
177 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
178#endif
179
Paul Bakker40e46942009-01-03 21:51:57 +0000180#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 printf( " ARC4 : " );
182 fflush( stdout );
183
184 arc4_setup( &arc4, tmp, 32 );
185
186 set_alarm( 1 );
187 for( i = 1; ! alarmed; i++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000188 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 tsc = hardclock();
191 for( j = 0; j < 1024; j++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000192 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
195 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
196#endif
197
Paul Bakker40e46942009-01-03 21:51:57 +0000198#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 printf( " 3DES : " );
200 fflush( stdout );
201
202 des3_set3key_enc( &des3, tmp );
203
204 set_alarm( 1 );
205 for( i = 1; ! alarmed; i++ )
206 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
207
208 tsc = hardclock();
209 for( j = 0; j < 1024; j++ )
210 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
211
212 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
213 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
214
215 printf( " DES : " );
216 fflush( stdout );
217
218 des_setkey_enc( &des, tmp );
219
220 set_alarm( 1 );
221 for( i = 1; ! alarmed; i++ )
222 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
223
224 tsc = hardclock();
225 for( j = 0; j < 1024; j++ )
226 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
227
228 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
229 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
230#endif
231
Paul Bakker40e46942009-01-03 21:51:57 +0000232#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 for( keysize = 128; keysize <= 256; keysize += 64 )
234 {
235 printf( " AES-%d : ", keysize );
236 fflush( stdout );
237
238 memset( buf, 0, sizeof( buf ) );
239 memset( tmp, 0, sizeof( tmp ) );
240 aes_setkey_enc( &aes, tmp, keysize );
241
242 set_alarm( 1 );
243
244 for( i = 1; ! alarmed; i++ )
245 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
246
247 tsc = hardclock();
248 for( j = 0; j < 4096; j++ )
249 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
250
251 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
252 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
253 }
254#endif
255
Paul Bakker38119b12009-01-10 23:31:23 +0000256#if defined(POLARSSL_CAMELLIA_C)
257 for( keysize = 128; keysize <= 256; keysize += 64 )
258 {
259 printf( " CAMELLIA-%d : ", keysize );
260 fflush( stdout );
261
262 memset( buf, 0, sizeof( buf ) );
263 memset( tmp, 0, sizeof( tmp ) );
264 camellia_setkey_enc( &camellia, tmp, keysize );
265
266 set_alarm( 1 );
267
268 for( i = 1; ! alarmed; i++ )
269 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
270
271 tsc = hardclock();
272 for( j = 0; j < 4096; j++ )
273 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
274
275 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
276 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
277 }
278#endif
279
Paul Bakker5690efc2011-05-26 13:16:06 +0000280#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
281 defined(POLARSSL_GENPRIME)
Paul Bakkera802e1a2010-08-16 11:56:45 +0000282 rsa_init( &rsa, RSA_PKCS_V15, 0 );
283 rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 printf( " RSA-1024 : " );
286 fflush( stdout );
287 set_alarm( 3 );
288
289 for( i = 1; ! alarmed; i++ )
290 {
291 buf[0] = 0;
292 rsa_public( &rsa, buf, buf );
293 }
294
295 printf( "%9lu public/s\n", i / 3 );
296
297 printf( " RSA-1024 : " );
298 fflush( stdout );
299 set_alarm( 3 );
300
301 for( i = 1; ! alarmed; i++ )
302 {
303 buf[0] = 0;
304 rsa_private( &rsa, buf, buf );
305 }
306
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000307 printf( "%9lu private/s\n", i / 3 );
308
309 rsa_free( &rsa );
310
Paul Bakkera802e1a2010-08-16 11:56:45 +0000311 rsa_init( &rsa, RSA_PKCS_V15, 0 );
312 rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000313
314 printf( " RSA-2048 : " );
315 fflush( stdout );
316 set_alarm( 3 );
317
318 for( i = 1; ! alarmed; i++ )
319 {
320 buf[0] = 0;
321 rsa_public( &rsa, buf, buf );
322 }
323
324 printf( "%9lu public/s\n", i / 3 );
325
326 printf( " RSA-2048 : " );
327 fflush( stdout );
328 set_alarm( 3 );
329
330 for( i = 1; ! alarmed; i++ )
331 {
332 buf[0] = 0;
333 rsa_private( &rsa, buf, buf );
334 }
335
336 printf( "%9lu private/s\n", i / 3 );
337
338 rsa_free( &rsa );
339
Paul Bakkera802e1a2010-08-16 11:56:45 +0000340 rsa_init( &rsa, RSA_PKCS_V15, 0 );
341 rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000342
343 printf( " RSA-4096 : " );
344 fflush( stdout );
345 set_alarm( 3 );
346
347 for( i = 1; ! alarmed; i++ )
348 {
349 buf[0] = 0;
350 rsa_public( &rsa, buf, buf );
351 }
352
353 printf( "%9lu public/s\n", i / 3 );
354
355 printf( " RSA-4096 : " );
356 fflush( stdout );
357 set_alarm( 3 );
358
359 for( i = 1; ! alarmed; i++ )
360 {
361 buf[0] = 0;
362 rsa_private( &rsa, buf, buf );
363 }
364
365 printf( "%9lu private/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367 rsa_free( &rsa );
368#endif
369
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000370 printf( "\n" );
371
Paul Bakkercce9d772011-11-18 14:26:47 +0000372#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 printf( " Press Enter to exit this program.\n" );
374 fflush( stdout ); getchar();
375#endif
376
377 return( 0 );
378}
Paul Bakker5690efc2011-05-26 13:16:06 +0000379#endif /* POLARSSL_TIMING_C */