blob: 1588b47dfa05454aa467a5d18bfddd9521a6b5fe [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
Paul Bakkera3d195c2011-11-27 21:07:34 +000050static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000051{
Paul Bakkera3d195c2011-11-27 21:07:34 +000052 size_t use_len;
53 int rnd;
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055 if( rng_state != NULL )
56 rng_state = NULL;
57
Paul Bakkera3d195c2011-11-27 21:07:34 +000058 while( len > 0 )
59 {
60 use_len = len;
61 if( use_len > sizeof(int) )
62 use_len = sizeof(int);
63
64 rnd = rand();
65 memcpy( output, &rnd, use_len );
66 output += use_len;
67 len -= use_len;
68 }
69
70 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000071}
72
73unsigned char buf[BUFSIZE];
74
Paul Bakker5690efc2011-05-26 13:16:06 +000075#if !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000076int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000077{
Paul Bakkercce9d772011-11-18 14:26:47 +000078 ((void) argc);
79 ((void) argv);
80
Paul Bakker5690efc2011-05-26 13:16:06 +000081 printf("POLARSSL_TIMING_C not defined.\n");
82 return( 0 );
83}
84#else
Paul Bakkercce9d772011-11-18 14:26:47 +000085int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
87 int keysize;
88 unsigned long i, j, tsc;
Paul Bakker5a0aa772009-02-09 22:38:52 +000089 unsigned char tmp[64];
Paul Bakker40e46942009-01-03 21:51:57 +000090#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000091 arc4_context arc4;
92#endif
Paul Bakker40e46942009-01-03 21:51:57 +000093#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000094 des3_context des3;
95 des_context des;
96#endif
Paul Bakker40e46942009-01-03 21:51:57 +000097#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000098 aes_context aes;
99#endif
Paul Bakker38119b12009-01-10 23:31:23 +0000100#if defined(POLARSSL_CAMELLIA_C)
101 camellia_context camellia;
102#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000103#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
104 defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 rsa_context rsa;
106#endif
107
Paul Bakkercce9d772011-11-18 14:26:47 +0000108 ((void) argc);
109 ((void) argv);
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 memset( buf, 0xAA, sizeof( buf ) );
112
113 printf( "\n" );
114
Paul Bakker40e46942009-01-03 21:51:57 +0000115#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 printf( " MD4 : " );
117 fflush( stdout );
118
119 set_alarm( 1 );
120 for( i = 1; ! alarmed; i++ )
121 md4( buf, BUFSIZE, tmp );
122
123 tsc = hardclock();
124 for( j = 0; j < 1024; j++ )
125 md4( buf, BUFSIZE, tmp );
126
127 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
128 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
129#endif
130
Paul Bakker40e46942009-01-03 21:51:57 +0000131#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 printf( " MD5 : " );
133 fflush( stdout );
134
135 set_alarm( 1 );
136 for( i = 1; ! alarmed; i++ )
137 md5( buf, BUFSIZE, tmp );
138
139 tsc = hardclock();
140 for( j = 0; j < 1024; j++ )
141 md5( buf, BUFSIZE, tmp );
142
143 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
144 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
145#endif
146
Paul Bakker40e46942009-01-03 21:51:57 +0000147#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 printf( " SHA-1 : " );
149 fflush( stdout );
150
151 set_alarm( 1 );
152 for( i = 1; ! alarmed; i++ )
153 sha1( buf, BUFSIZE, tmp );
154
155 tsc = hardclock();
156 for( j = 0; j < 1024; j++ )
157 sha1( buf, BUFSIZE, tmp );
158
159 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
160 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
161#endif
162
Paul Bakker40e46942009-01-03 21:51:57 +0000163#if defined(POLARSSL_SHA2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 printf( " SHA-256 : " );
165 fflush( stdout );
166
167 set_alarm( 1 );
168 for( i = 1; ! alarmed; i++ )
169 sha2( buf, BUFSIZE, tmp, 0 );
170
171 tsc = hardclock();
172 for( j = 0; j < 1024; j++ )
173 sha2( buf, BUFSIZE, tmp, 0 );
174
175 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
176 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
177#endif
178
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000179#if defined(POLARSSL_SHA4_C)
180 printf( " SHA-512 : " );
181 fflush( stdout );
182
183 set_alarm( 1 );
184 for( i = 1; ! alarmed; i++ )
185 sha4( buf, BUFSIZE, tmp, 0 );
186
187 tsc = hardclock();
188 for( j = 0; j < 1024; j++ )
189 sha4( buf, BUFSIZE, tmp, 0 );
190
191 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
192 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
193#endif
194
Paul Bakker40e46942009-01-03 21:51:57 +0000195#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 printf( " ARC4 : " );
197 fflush( stdout );
198
199 arc4_setup( &arc4, tmp, 32 );
200
201 set_alarm( 1 );
202 for( i = 1; ! alarmed; i++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000203 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205 tsc = hardclock();
206 for( j = 0; j < 1024; j++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000207 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
210 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
211#endif
212
Paul Bakker40e46942009-01-03 21:51:57 +0000213#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 printf( " 3DES : " );
215 fflush( stdout );
216
217 des3_set3key_enc( &des3, tmp );
218
219 set_alarm( 1 );
220 for( i = 1; ! alarmed; i++ )
221 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
222
223 tsc = hardclock();
224 for( j = 0; j < 1024; j++ )
225 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
226
227 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
228 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
229
230 printf( " DES : " );
231 fflush( stdout );
232
233 des_setkey_enc( &des, tmp );
234
235 set_alarm( 1 );
236 for( i = 1; ! alarmed; i++ )
237 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
238
239 tsc = hardclock();
240 for( j = 0; j < 1024; j++ )
241 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
242
243 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
244 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
245#endif
246
Paul Bakker40e46942009-01-03 21:51:57 +0000247#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 for( keysize = 128; keysize <= 256; keysize += 64 )
249 {
250 printf( " AES-%d : ", keysize );
251 fflush( stdout );
252
253 memset( buf, 0, sizeof( buf ) );
254 memset( tmp, 0, sizeof( tmp ) );
255 aes_setkey_enc( &aes, tmp, keysize );
256
257 set_alarm( 1 );
258
259 for( i = 1; ! alarmed; i++ )
260 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
261
262 tsc = hardclock();
263 for( j = 0; j < 4096; j++ )
264 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
265
266 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
267 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
268 }
269#endif
270
Paul Bakker38119b12009-01-10 23:31:23 +0000271#if defined(POLARSSL_CAMELLIA_C)
272 for( keysize = 128; keysize <= 256; keysize += 64 )
273 {
274 printf( " CAMELLIA-%d : ", keysize );
275 fflush( stdout );
276
277 memset( buf, 0, sizeof( buf ) );
278 memset( tmp, 0, sizeof( tmp ) );
279 camellia_setkey_enc( &camellia, tmp, keysize );
280
281 set_alarm( 1 );
282
283 for( i = 1; ! alarmed; i++ )
284 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
285
286 tsc = hardclock();
287 for( j = 0; j < 4096; j++ )
288 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
289
290 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
291 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
292 }
293#endif
294
Paul Bakker5690efc2011-05-26 13:16:06 +0000295#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
296 defined(POLARSSL_GENPRIME)
Paul Bakkera802e1a2010-08-16 11:56:45 +0000297 rsa_init( &rsa, RSA_PKCS_V15, 0 );
298 rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300 printf( " RSA-1024 : " );
301 fflush( stdout );
302 set_alarm( 3 );
303
304 for( i = 1; ! alarmed; i++ )
305 {
306 buf[0] = 0;
307 rsa_public( &rsa, buf, buf );
308 }
309
310 printf( "%9lu public/s\n", i / 3 );
311
312 printf( " RSA-1024 : " );
313 fflush( stdout );
314 set_alarm( 3 );
315
316 for( i = 1; ! alarmed; i++ )
317 {
318 buf[0] = 0;
319 rsa_private( &rsa, buf, buf );
320 }
321
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000322 printf( "%9lu private/s\n", i / 3 );
323
324 rsa_free( &rsa );
325
Paul Bakkera802e1a2010-08-16 11:56:45 +0000326 rsa_init( &rsa, RSA_PKCS_V15, 0 );
327 rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000328
329 printf( " RSA-2048 : " );
330 fflush( stdout );
331 set_alarm( 3 );
332
333 for( i = 1; ! alarmed; i++ )
334 {
335 buf[0] = 0;
336 rsa_public( &rsa, buf, buf );
337 }
338
339 printf( "%9lu public/s\n", i / 3 );
340
341 printf( " RSA-2048 : " );
342 fflush( stdout );
343 set_alarm( 3 );
344
345 for( i = 1; ! alarmed; i++ )
346 {
347 buf[0] = 0;
348 rsa_private( &rsa, buf, buf );
349 }
350
351 printf( "%9lu private/s\n", i / 3 );
352
353 rsa_free( &rsa );
354
Paul Bakkera802e1a2010-08-16 11:56:45 +0000355 rsa_init( &rsa, RSA_PKCS_V15, 0 );
356 rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000357
358 printf( " RSA-4096 : " );
359 fflush( stdout );
360 set_alarm( 3 );
361
362 for( i = 1; ! alarmed; i++ )
363 {
364 buf[0] = 0;
365 rsa_public( &rsa, buf, buf );
366 }
367
368 printf( "%9lu public/s\n", i / 3 );
369
370 printf( " RSA-4096 : " );
371 fflush( stdout );
372 set_alarm( 3 );
373
374 for( i = 1; ! alarmed; i++ )
375 {
376 buf[0] = 0;
377 rsa_private( &rsa, buf, buf );
378 }
379
380 printf( "%9lu private/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382 rsa_free( &rsa );
383#endif
384
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000385 printf( "\n" );
386
Paul Bakkercce9d772011-11-18 14:26:47 +0000387#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 printf( " Press Enter to exit this program.\n" );
389 fflush( stdout ); getchar();
390#endif
391
392 return( 0 );
393}
Paul Bakker5690efc2011-05-26 13:16:06 +0000394#endif /* POLARSSL_TIMING_C */