blob: b0aba116e17794114a979e3ca8d240359fefb39e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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
60int main( void )
61{
62 int keysize;
63 unsigned long i, j, tsc;
Paul Bakker5a0aa772009-02-09 22:38:52 +000064 unsigned char tmp[64];
Paul Bakker40e46942009-01-03 21:51:57 +000065#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000066 arc4_context arc4;
67#endif
Paul Bakker40e46942009-01-03 21:51:57 +000068#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000069 des3_context des3;
70 des_context des;
71#endif
Paul Bakker40e46942009-01-03 21:51:57 +000072#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000073 aes_context aes;
74#endif
Paul Bakker38119b12009-01-10 23:31:23 +000075#if defined(POLARSSL_CAMELLIA_C)
76 camellia_context camellia;
77#endif
Paul Bakker40e46942009-01-03 21:51:57 +000078#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000079 rsa_context rsa;
80#endif
81
82 memset( buf, 0xAA, sizeof( buf ) );
83
84 printf( "\n" );
85
Paul Bakker40e46942009-01-03 21:51:57 +000086#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000087 printf( " MD4 : " );
88 fflush( stdout );
89
90 set_alarm( 1 );
91 for( i = 1; ! alarmed; i++ )
92 md4( buf, BUFSIZE, tmp );
93
94 tsc = hardclock();
95 for( j = 0; j < 1024; j++ )
96 md4( buf, BUFSIZE, tmp );
97
98 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
99 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
100#endif
101
Paul Bakker40e46942009-01-03 21:51:57 +0000102#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 printf( " MD5 : " );
104 fflush( stdout );
105
106 set_alarm( 1 );
107 for( i = 1; ! alarmed; i++ )
108 md5( buf, BUFSIZE, tmp );
109
110 tsc = hardclock();
111 for( j = 0; j < 1024; j++ )
112 md5( buf, BUFSIZE, tmp );
113
114 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
115 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
116#endif
117
Paul Bakker40e46942009-01-03 21:51:57 +0000118#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 printf( " SHA-1 : " );
120 fflush( stdout );
121
122 set_alarm( 1 );
123 for( i = 1; ! alarmed; i++ )
124 sha1( buf, BUFSIZE, tmp );
125
126 tsc = hardclock();
127 for( j = 0; j < 1024; j++ )
128 sha1( buf, BUFSIZE, tmp );
129
130 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
131 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
132#endif
133
Paul Bakker40e46942009-01-03 21:51:57 +0000134#if defined(POLARSSL_SHA2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 printf( " SHA-256 : " );
136 fflush( stdout );
137
138 set_alarm( 1 );
139 for( i = 1; ! alarmed; i++ )
140 sha2( buf, BUFSIZE, tmp, 0 );
141
142 tsc = hardclock();
143 for( j = 0; j < 1024; j++ )
144 sha2( buf, BUFSIZE, tmp, 0 );
145
146 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
147 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
148#endif
149
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000150#if defined(POLARSSL_SHA4_C)
151 printf( " SHA-512 : " );
152 fflush( stdout );
153
154 set_alarm( 1 );
155 for( i = 1; ! alarmed; i++ )
156 sha4( buf, BUFSIZE, tmp, 0 );
157
158 tsc = hardclock();
159 for( j = 0; j < 1024; j++ )
160 sha4( buf, BUFSIZE, tmp, 0 );
161
162 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
163 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
164#endif
165
Paul Bakker40e46942009-01-03 21:51:57 +0000166#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 printf( " ARC4 : " );
168 fflush( stdout );
169
170 arc4_setup( &arc4, tmp, 32 );
171
172 set_alarm( 1 );
173 for( i = 1; ! alarmed; i++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000174 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176 tsc = hardclock();
177 for( j = 0; j < 1024; j++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000178 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
181 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
182#endif
183
Paul Bakker40e46942009-01-03 21:51:57 +0000184#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 printf( " 3DES : " );
186 fflush( stdout );
187
188 des3_set3key_enc( &des3, tmp );
189
190 set_alarm( 1 );
191 for( i = 1; ! alarmed; i++ )
192 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
193
194 tsc = hardclock();
195 for( j = 0; j < 1024; j++ )
196 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
197
198 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
199 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
200
201 printf( " DES : " );
202 fflush( stdout );
203
204 des_setkey_enc( &des, tmp );
205
206 set_alarm( 1 );
207 for( i = 1; ! alarmed; i++ )
208 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
209
210 tsc = hardclock();
211 for( j = 0; j < 1024; j++ )
212 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
213
214 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
215 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
216#endif
217
Paul Bakker40e46942009-01-03 21:51:57 +0000218#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 for( keysize = 128; keysize <= 256; keysize += 64 )
220 {
221 printf( " AES-%d : ", keysize );
222 fflush( stdout );
223
224 memset( buf, 0, sizeof( buf ) );
225 memset( tmp, 0, sizeof( tmp ) );
226 aes_setkey_enc( &aes, tmp, keysize );
227
228 set_alarm( 1 );
229
230 for( i = 1; ! alarmed; i++ )
231 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
232
233 tsc = hardclock();
234 for( j = 0; j < 4096; j++ )
235 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
236
237 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
238 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
239 }
240#endif
241
Paul Bakker38119b12009-01-10 23:31:23 +0000242#if defined(POLARSSL_CAMELLIA_C)
243 for( keysize = 128; keysize <= 256; keysize += 64 )
244 {
245 printf( " CAMELLIA-%d : ", keysize );
246 fflush( stdout );
247
248 memset( buf, 0, sizeof( buf ) );
249 memset( tmp, 0, sizeof( tmp ) );
250 camellia_setkey_enc( &camellia, tmp, keysize );
251
252 set_alarm( 1 );
253
254 for( i = 1; ! alarmed; i++ )
255 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
256
257 tsc = hardclock();
258 for( j = 0; j < 4096; j++ )
259 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
260
261 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
262 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
263 }
264#endif
265
Paul Bakker40e46942009-01-03 21:51:57 +0000266#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
268 rsa_gen_key( &rsa, 1024, 65537 );
269
270 printf( " RSA-1024 : " );
271 fflush( stdout );
272 set_alarm( 3 );
273
274 for( i = 1; ! alarmed; i++ )
275 {
276 buf[0] = 0;
277 rsa_public( &rsa, buf, buf );
278 }
279
280 printf( "%9lu public/s\n", i / 3 );
281
282 printf( " RSA-1024 : " );
283 fflush( stdout );
284 set_alarm( 3 );
285
286 for( i = 1; ! alarmed; i++ )
287 {
288 buf[0] = 0;
289 rsa_private( &rsa, buf, buf );
290 }
291
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000292 printf( "%9lu private/s\n", i / 3 );
293
294 rsa_free( &rsa );
295
296 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
297 rsa_gen_key( &rsa, 2048, 65537 );
298
299 printf( " RSA-2048 : " );
300 fflush( stdout );
301 set_alarm( 3 );
302
303 for( i = 1; ! alarmed; i++ )
304 {
305 buf[0] = 0;
306 rsa_public( &rsa, buf, buf );
307 }
308
309 printf( "%9lu public/s\n", i / 3 );
310
311 printf( " RSA-2048 : " );
312 fflush( stdout );
313 set_alarm( 3 );
314
315 for( i = 1; ! alarmed; i++ )
316 {
317 buf[0] = 0;
318 rsa_private( &rsa, buf, buf );
319 }
320
321 printf( "%9lu private/s\n", i / 3 );
322
323 rsa_free( &rsa );
324
325 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
326 rsa_gen_key( &rsa, 4096, 65537 );
327
328 printf( " RSA-4096 : " );
329 fflush( stdout );
330 set_alarm( 3 );
331
332 for( i = 1; ! alarmed; i++ )
333 {
334 buf[0] = 0;
335 rsa_public( &rsa, buf, buf );
336 }
337
338 printf( "%9lu public/s\n", i / 3 );
339
340 printf( " RSA-4096 : " );
341 fflush( stdout );
342 set_alarm( 3 );
343
344 for( i = 1; ! alarmed; i++ )
345 {
346 buf[0] = 0;
347 rsa_private( &rsa, buf, buf );
348 }
349
350 printf( "%9lu private/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 rsa_free( &rsa );
353#endif
354
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000355 printf( "\n" );
356
Paul Bakker5121ce52009-01-03 21:22:43 +0000357#ifdef WIN32
358 printf( " Press Enter to exit this program.\n" );
359 fflush( stdout ); getchar();
360#endif
361
362 return( 0 );
363}