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