blob: 3a109eaba32d33c1bf46d527a25e132bc4c57d89 [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.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdlib.h>
29#include <stdio.h>
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/md4.h"
34#include "polarssl/md5.h"
35#include "polarssl/sha1.h"
36#include "polarssl/sha2.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000037#include "polarssl/sha4.h"
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/arc4.h"
39#include "polarssl/des.h"
40#include "polarssl/aes.h"
Paul Bakker38119b12009-01-10 23:31:23 +000041#include "polarssl/camellia.h"
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/rsa.h"
43#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
45#define BUFSIZE 1024
46
47static int myrand( void *rng_state )
48{
49 if( rng_state != NULL )
50 rng_state = NULL;
51
52 return( rand() );
53}
54
55unsigned char buf[BUFSIZE];
56
57int main( void )
58{
59 int keysize;
60 unsigned long i, j, tsc;
Paul Bakker5a0aa772009-02-09 22:38:52 +000061 unsigned char tmp[64];
Paul Bakker40e46942009-01-03 21:51:57 +000062#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000063 arc4_context arc4;
64#endif
Paul Bakker40e46942009-01-03 21:51:57 +000065#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000066 des3_context des3;
67 des_context des;
68#endif
Paul Bakker40e46942009-01-03 21:51:57 +000069#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000070 aes_context aes;
71#endif
Paul Bakker38119b12009-01-10 23:31:23 +000072#if defined(POLARSSL_CAMELLIA_C)
73 camellia_context camellia;
74#endif
Paul Bakker40e46942009-01-03 21:51:57 +000075#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000076 rsa_context rsa;
77#endif
78
79 memset( buf, 0xAA, sizeof( buf ) );
80
81 printf( "\n" );
82
Paul Bakker40e46942009-01-03 21:51:57 +000083#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000084 printf( " MD4 : " );
85 fflush( stdout );
86
87 set_alarm( 1 );
88 for( i = 1; ! alarmed; i++ )
89 md4( buf, BUFSIZE, tmp );
90
91 tsc = hardclock();
92 for( j = 0; j < 1024; j++ )
93 md4( buf, BUFSIZE, tmp );
94
95 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
96 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
97#endif
98
Paul Bakker40e46942009-01-03 21:51:57 +000099#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 printf( " MD5 : " );
101 fflush( stdout );
102
103 set_alarm( 1 );
104 for( i = 1; ! alarmed; i++ )
105 md5( buf, BUFSIZE, tmp );
106
107 tsc = hardclock();
108 for( j = 0; j < 1024; j++ )
109 md5( buf, BUFSIZE, tmp );
110
111 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
112 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
113#endif
114
Paul Bakker40e46942009-01-03 21:51:57 +0000115#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 printf( " SHA-1 : " );
117 fflush( stdout );
118
119 set_alarm( 1 );
120 for( i = 1; ! alarmed; i++ )
121 sha1( buf, BUFSIZE, tmp );
122
123 tsc = hardclock();
124 for( j = 0; j < 1024; j++ )
125 sha1( 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_SHA2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 printf( " SHA-256 : " );
133 fflush( stdout );
134
135 set_alarm( 1 );
136 for( i = 1; ! alarmed; i++ )
137 sha2( buf, BUFSIZE, tmp, 0 );
138
139 tsc = hardclock();
140 for( j = 0; j < 1024; j++ )
141 sha2( buf, BUFSIZE, tmp, 0 );
142
143 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
144 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
145#endif
146
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000147#if defined(POLARSSL_SHA4_C)
148 printf( " SHA-512 : " );
149 fflush( stdout );
150
151 set_alarm( 1 );
152 for( i = 1; ! alarmed; i++ )
153 sha4( buf, BUFSIZE, tmp, 0 );
154
155 tsc = hardclock();
156 for( j = 0; j < 1024; j++ )
157 sha4( buf, BUFSIZE, tmp, 0 );
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_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 printf( " ARC4 : " );
165 fflush( stdout );
166
167 arc4_setup( &arc4, tmp, 32 );
168
169 set_alarm( 1 );
170 for( i = 1; ! alarmed; i++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000171 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173 tsc = hardclock();
174 for( j = 0; j < 1024; j++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000175 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
177 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
178 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
179#endif
180
Paul Bakker40e46942009-01-03 21:51:57 +0000181#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 printf( " 3DES : " );
183 fflush( stdout );
184
185 des3_set3key_enc( &des3, tmp );
186
187 set_alarm( 1 );
188 for( i = 1; ! alarmed; i++ )
189 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
190
191 tsc = hardclock();
192 for( j = 0; j < 1024; j++ )
193 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
194
195 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
196 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
197
198 printf( " DES : " );
199 fflush( stdout );
200
201 des_setkey_enc( &des, tmp );
202
203 set_alarm( 1 );
204 for( i = 1; ! alarmed; i++ )
205 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
206
207 tsc = hardclock();
208 for( j = 0; j < 1024; j++ )
209 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
210
211 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
212 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
213#endif
214
Paul Bakker40e46942009-01-03 21:51:57 +0000215#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 for( keysize = 128; keysize <= 256; keysize += 64 )
217 {
218 printf( " AES-%d : ", keysize );
219 fflush( stdout );
220
221 memset( buf, 0, sizeof( buf ) );
222 memset( tmp, 0, sizeof( tmp ) );
223 aes_setkey_enc( &aes, tmp, keysize );
224
225 set_alarm( 1 );
226
227 for( i = 1; ! alarmed; i++ )
228 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
229
230 tsc = hardclock();
231 for( j = 0; j < 4096; j++ )
232 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
233
234 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
235 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
236 }
237#endif
238
Paul Bakker38119b12009-01-10 23:31:23 +0000239#if defined(POLARSSL_CAMELLIA_C)
240 for( keysize = 128; keysize <= 256; keysize += 64 )
241 {
242 printf( " CAMELLIA-%d : ", keysize );
243 fflush( stdout );
244
245 memset( buf, 0, sizeof( buf ) );
246 memset( tmp, 0, sizeof( tmp ) );
247 camellia_setkey_enc( &camellia, tmp, keysize );
248
249 set_alarm( 1 );
250
251 for( i = 1; ! alarmed; i++ )
252 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
253
254 tsc = hardclock();
255 for( j = 0; j < 4096; j++ )
256 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
257
258 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
259 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
260 }
261#endif
262
Paul Bakker40e46942009-01-03 21:51:57 +0000263#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
265 rsa_gen_key( &rsa, 1024, 65537 );
266
267 printf( " RSA-1024 : " );
268 fflush( stdout );
269 set_alarm( 3 );
270
271 for( i = 1; ! alarmed; i++ )
272 {
273 buf[0] = 0;
274 rsa_public( &rsa, buf, buf );
275 }
276
277 printf( "%9lu public/s\n", i / 3 );
278
279 printf( " RSA-1024 : " );
280 fflush( stdout );
281 set_alarm( 3 );
282
283 for( i = 1; ! alarmed; i++ )
284 {
285 buf[0] = 0;
286 rsa_private( &rsa, buf, buf );
287 }
288
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000289 printf( "%9lu private/s\n", i / 3 );
290
291 rsa_free( &rsa );
292
293 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
294 rsa_gen_key( &rsa, 2048, 65537 );
295
296 printf( " RSA-2048 : " );
297 fflush( stdout );
298 set_alarm( 3 );
299
300 for( i = 1; ! alarmed; i++ )
301 {
302 buf[0] = 0;
303 rsa_public( &rsa, buf, buf );
304 }
305
306 printf( "%9lu public/s\n", i / 3 );
307
308 printf( " RSA-2048 : " );
309 fflush( stdout );
310 set_alarm( 3 );
311
312 for( i = 1; ! alarmed; i++ )
313 {
314 buf[0] = 0;
315 rsa_private( &rsa, buf, buf );
316 }
317
318 printf( "%9lu private/s\n", i / 3 );
319
320 rsa_free( &rsa );
321
322 rsa_init( &rsa, RSA_PKCS_V15, 0, myrand, NULL );
323 rsa_gen_key( &rsa, 4096, 65537 );
324
325 printf( " RSA-4096 : " );
326 fflush( stdout );
327 set_alarm( 3 );
328
329 for( i = 1; ! alarmed; i++ )
330 {
331 buf[0] = 0;
332 rsa_public( &rsa, buf, buf );
333 }
334
335 printf( "%9lu public/s\n", i / 3 );
336
337 printf( " RSA-4096 : " );
338 fflush( stdout );
339 set_alarm( 3 );
340
341 for( i = 1; ! alarmed; i++ )
342 {
343 buf[0] = 0;
344 rsa_private( &rsa, buf, buf );
345 }
346
347 printf( "%9lu private/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
349 rsa_free( &rsa );
350#endif
351
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000352 printf( "\n" );
353
Paul Bakker5121ce52009-01-03 21:22:43 +0000354#ifdef WIN32
355 printf( " Press Enter to exit this program.\n" );
356 fflush( stdout ); getchar();
357#endif
358
359 return( 0 );
360}