blob: e0d65d33db758a349dca55b932db4598754618b5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Paul Bakkerd2681d82013-06-30 14:49:12 +02004 * Copyright (C) 2006-2013, 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"
Paul Bakkerd2681d82013-06-30 14:49:12 +020039#include "polarssl/sha256.h"
40#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/arc4.h"
42#include "polarssl/des.h"
43#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000044#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000045#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000046#include "polarssl/gcm.h"
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010048#include "polarssl/dhm.h"
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/timing.h"
Paul Bakker02faf452011-11-29 11:23:58 +000050#include "polarssl/havege.h"
51#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker02faf452011-11-29 11:23:58 +000053#define BUFSIZE 1024
54#define HEADER_FORMAT " %-15s : "
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakker44618dd2013-07-04 10:34:10 +020056#if defined(POLARSSL_TIMING_C)
Paul Bakkera3d195c2011-11-27 21:07:34 +000057static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
Paul Bakkera3d195c2011-11-27 21:07:34 +000059 size_t use_len;
60 int rnd;
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062 if( rng_state != NULL )
63 rng_state = NULL;
64
Paul Bakkera3d195c2011-11-27 21:07:34 +000065 while( len > 0 )
66 {
67 use_len = len;
68 if( use_len > sizeof(int) )
69 use_len = sizeof(int);
70
71 rnd = rand();
72 memcpy( output, &rnd, use_len );
73 output += use_len;
74 len -= use_len;
75 }
76
77 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000078}
79
80unsigned char buf[BUFSIZE];
Paul Bakker44618dd2013-07-04 10:34:10 +020081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakker5690efc2011-05-26 13:16:06 +000083#if !defined(POLARSSL_TIMING_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000084int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000085{
Paul Bakkercce9d772011-11-18 14:26:47 +000086 ((void) argc);
87 ((void) argv);
88
Paul Bakker5690efc2011-05-26 13:16:06 +000089 printf("POLARSSL_TIMING_C not defined.\n");
90 return( 0 );
91}
92#else
Paul Bakkercce9d772011-11-18 14:26:47 +000093int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +020095 int ret, keysize;
Paul Bakker5121ce52009-01-03 21:22:43 +000096 unsigned long i, j, tsc;
Paul Bakker5a0aa772009-02-09 22:38:52 +000097 unsigned char tmp[64];
Paul Bakker40e46942009-01-03 21:51:57 +000098#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000099 arc4_context arc4;
100#endif
Paul Bakker40e46942009-01-03 21:51:57 +0000101#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 des3_context des3;
103 des_context des;
104#endif
Paul Bakker40e46942009-01-03 21:51:57 +0000105#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 aes_context aes;
Paul Bakker89e80c92012-03-20 13:50:09 +0000107#if defined(POLARSSL_GCM_C)
108 gcm_context gcm;
109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000110#endif
Paul Bakker3d58fe82012-07-04 17:15:31 +0000111#if defined(POLARSSL_BLOWFISH_C)
112 blowfish_context blowfish;
113#endif
Paul Bakker38119b12009-01-10 23:31:23 +0000114#if defined(POLARSSL_CAMELLIA_C)
115 camellia_context camellia;
116#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000117#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
118 defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 rsa_context rsa;
120#endif
Paul Bakker02faf452011-11-29 11:23:58 +0000121#if defined(POLARSSL_HAVEGE_C)
122 havege_state hs;
123#endif
124#if defined(POLARSSL_CTR_DRBG_C)
125 ctr_drbg_context ctr_drbg;
126#endif
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100127#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
128 dhm_context dhm;
129 size_t olen = BUFSIZE;
130#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000131 ((void) argc);
132 ((void) argv);
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 memset( buf, 0xAA, sizeof( buf ) );
135
136 printf( "\n" );
137
Paul Bakker40e46942009-01-03 21:51:57 +0000138#if defined(POLARSSL_MD4_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000139 printf( HEADER_FORMAT, "MD4" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 fflush( stdout );
141
142 set_alarm( 1 );
143 for( i = 1; ! alarmed; i++ )
144 md4( buf, BUFSIZE, tmp );
145
146 tsc = hardclock();
147 for( j = 0; j < 1024; j++ )
148 md4( buf, BUFSIZE, tmp );
149
150 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
151 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
152#endif
153
Paul Bakker40e46942009-01-03 21:51:57 +0000154#if defined(POLARSSL_MD5_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000155 printf( HEADER_FORMAT, "MD5" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 fflush( stdout );
157
158 set_alarm( 1 );
159 for( i = 1; ! alarmed; i++ )
160 md5( buf, BUFSIZE, tmp );
161
162 tsc = hardclock();
163 for( j = 0; j < 1024; j++ )
164 md5( buf, BUFSIZE, tmp );
165
166 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
167 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
168#endif
169
Paul Bakker40e46942009-01-03 21:51:57 +0000170#if defined(POLARSSL_SHA1_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000171 printf( HEADER_FORMAT, "SHA-1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 fflush( stdout );
173
174 set_alarm( 1 );
175 for( i = 1; ! alarmed; i++ )
176 sha1( buf, BUFSIZE, tmp );
177
178 tsc = hardclock();
179 for( j = 0; j < 1024; j++ )
180 sha1( buf, BUFSIZE, tmp );
181
182 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
183 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
184#endif
185
Paul Bakker9e36f042013-06-30 14:34:05 +0200186#if defined(POLARSSL_SHA256_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000187 printf( HEADER_FORMAT, "SHA-256" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 fflush( stdout );
189
190 set_alarm( 1 );
191 for( i = 1; ! alarmed; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200192 sha256( buf, BUFSIZE, tmp, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 tsc = hardclock();
195 for( j = 0; j < 1024; j++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200196 sha256( buf, BUFSIZE, tmp, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
199 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
200#endif
201
Paul Bakker9e36f042013-06-30 14:34:05 +0200202#if defined(POLARSSL_SHA512_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000203 printf( HEADER_FORMAT, "SHA-512" );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000204 fflush( stdout );
205
206 set_alarm( 1 );
207 for( i = 1; ! alarmed; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200208 sha512( buf, BUFSIZE, tmp, 0 );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000209
210 tsc = hardclock();
211 for( j = 0; j < 1024; j++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200212 sha512( buf, BUFSIZE, tmp, 0 );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000213
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_ARC4_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000219 printf( HEADER_FORMAT, "ARC4" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 fflush( stdout );
221
222 arc4_setup( &arc4, tmp, 32 );
223
224 set_alarm( 1 );
225 for( i = 1; ! alarmed; i++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000226 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228 tsc = hardclock();
229 for( j = 0; j < 1024; j++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000230 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
233 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
234#endif
235
Paul Bakker40e46942009-01-03 21:51:57 +0000236#if defined(POLARSSL_DES_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000237 printf( HEADER_FORMAT, "3DES" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 fflush( stdout );
239
240 des3_set3key_enc( &des3, tmp );
241
242 set_alarm( 1 );
243 for( i = 1; ! alarmed; i++ )
244 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
245
246 tsc = hardclock();
247 for( j = 0; j < 1024; j++ )
248 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
249
250 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
251 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
252
Paul Bakker02faf452011-11-29 11:23:58 +0000253 printf( HEADER_FORMAT, "DES" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 fflush( stdout );
255
256 des_setkey_enc( &des, tmp );
257
258 set_alarm( 1 );
259 for( i = 1; ! alarmed; i++ )
260 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
261
262 tsc = hardclock();
263 for( j = 0; j < 1024; j++ )
264 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
265
266 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
267 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
268#endif
269
Paul Bakker40e46942009-01-03 21:51:57 +0000270#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 for( keysize = 128; keysize <= 256; keysize += 64 )
272 {
Paul Bakker89e80c92012-03-20 13:50:09 +0000273 printf( " AES-CBC-%d : ", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 fflush( stdout );
275
276 memset( buf, 0, sizeof( buf ) );
277 memset( tmp, 0, sizeof( tmp ) );
278 aes_setkey_enc( &aes, tmp, keysize );
279
280 set_alarm( 1 );
281
282 for( i = 1; ! alarmed; i++ )
283 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
284
285 tsc = hardclock();
286 for( j = 0; j < 4096; j++ )
287 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
288
289 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
290 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
291 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000292#if defined(POLARSSL_GCM_C)
293 for( keysize = 128; keysize <= 256; keysize += 64 )
294 {
295 printf( " AES-GCM-%d : ", keysize );
296 fflush( stdout );
297
298 memset( buf, 0, sizeof( buf ) );
299 memset( tmp, 0, sizeof( tmp ) );
300 gcm_init( &gcm, tmp, keysize );
301
302 set_alarm( 1 );
303
304 for( i = 1; ! alarmed; i++ )
Paul Bakkerb78c7452012-03-20 15:05:59 +0000305 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 12, NULL, 0, buf, buf, 16, tmp );
Paul Bakker89e80c92012-03-20 13:50:09 +0000306
307 tsc = hardclock();
308 for( j = 0; j < 4096; j++ )
Paul Bakkerb78c7452012-03-20 15:05:59 +0000309 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 12, NULL, 0, buf, buf, 16, tmp );
Paul Bakker89e80c92012-03-20 13:50:09 +0000310
311 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
312 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
313 }
314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000315#endif
316
Paul Bakker38119b12009-01-10 23:31:23 +0000317#if defined(POLARSSL_CAMELLIA_C)
318 for( keysize = 128; keysize <= 256; keysize += 64 )
319 {
Paul Bakker89e80c92012-03-20 13:50:09 +0000320 printf( " CAMELLIA-CBC-%d: ", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000321 fflush( stdout );
322
323 memset( buf, 0, sizeof( buf ) );
324 memset( tmp, 0, sizeof( tmp ) );
325 camellia_setkey_enc( &camellia, tmp, keysize );
326
327 set_alarm( 1 );
328
329 for( i = 1; ! alarmed; i++ )
330 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
331
332 tsc = hardclock();
333 for( j = 0; j < 4096; j++ )
334 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
335
336 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
337 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
338 }
339#endif
340
Paul Bakker3d58fe82012-07-04 17:15:31 +0000341#if defined(POLARSSL_BLOWFISH_C)
342 for( keysize = 128; keysize <= 256; keysize += 64 )
343 {
344 printf( " BLOWFISH-CBC-%d: ", keysize );
345 fflush( stdout );
346
347 memset( buf, 0, sizeof( buf ) );
348 memset( tmp, 0, sizeof( tmp ) );
349 blowfish_setkey( &blowfish, tmp, keysize );
350
351 set_alarm( 1 );
352
353 for( i = 1; ! alarmed; i++ )
354 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf );
355
356 tsc = hardclock();
357 for( j = 0; j < 4096; j++ )
358 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf );
359
360 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
361 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
362 }
363#endif
364
Paul Bakker02faf452011-11-29 11:23:58 +0000365#if defined(POLARSSL_HAVEGE_C)
366 printf( HEADER_FORMAT, "HAVEGE" );
367 fflush( stdout );
368
369 havege_init( &hs );
370
371 set_alarm( 1 );
372 for( i = 1; ! alarmed; i++ )
373 havege_random( &hs, buf, BUFSIZE );
374
375 tsc = hardclock();
376 for( j = 1; j < 1024; j++ )
377 havege_random( &hs, buf, BUFSIZE );
378
379 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
380 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
381#endif
382
383#if defined(POLARSSL_CTR_DRBG_C)
384 printf( HEADER_FORMAT, "CTR_DRBG (NOPR)" );
385 fflush( stdout );
386
387 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
388 exit(1);
389
390 set_alarm( 1 );
391 for( i = 1; ! alarmed; i++ )
392 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
393 exit(1);
394
395 tsc = hardclock();
396 for( j = 1; j < 1024; j++ )
397 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
398 exit(1);
399
400 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
401 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
402
403 printf( HEADER_FORMAT, "CTR_DRBG (PR)" );
404 fflush( stdout );
405
406 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
407 exit(1);
408
409 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
410
411 set_alarm( 1 );
412 for( i = 1; ! alarmed; i++ )
413 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
414 exit(1);
415
416 tsc = hardclock();
417 for( j = 1; j < 1024; j++ )
418 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
419 exit(1);
420
421 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
422 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
423#endif
424
Paul Bakker5690efc2011-05-26 13:16:06 +0000425#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
426 defined(POLARSSL_GENPRIME)
Paul Bakkera802e1a2010-08-16 11:56:45 +0000427 rsa_init( &rsa, RSA_PKCS_V15, 0 );
428 rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Paul Bakker02faf452011-11-29 11:23:58 +0000430 printf( HEADER_FORMAT, "RSA-1024" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 fflush( stdout );
432 set_alarm( 3 );
433
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200434 ret = 0;
435 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 {
437 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200438 ret = rsa_public( &rsa, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 }
440
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200441 if( ret != 0 )
442 printf( "FAILED\n" );
443 else
444 printf( "%9lu public/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
Paul Bakker02faf452011-11-29 11:23:58 +0000446 printf( HEADER_FORMAT, "RSA-1024" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 fflush( stdout );
448 set_alarm( 3 );
449
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200450 ret = 0;
451 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 {
453 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200454 ret = rsa_private( &rsa, myrand, NULL, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 }
456
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200457 if( ret != 0 )
458 printf( "FAILED\n" );
459 else
460 printf( "%9lu private/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000461
462 rsa_free( &rsa );
463
Paul Bakkera802e1a2010-08-16 11:56:45 +0000464 rsa_init( &rsa, RSA_PKCS_V15, 0 );
465 rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000466
Paul Bakker02faf452011-11-29 11:23:58 +0000467 printf( HEADER_FORMAT, "RSA-2048" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000468 fflush( stdout );
469 set_alarm( 3 );
470
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200471 ret = 0;
472 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000473 {
474 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200475 ret = rsa_public( &rsa, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000476 }
477
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200478 if( ret != 0 )
479 printf( "FAILED\n" );
480 else
481 printf( "%9lu public/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000482
Paul Bakker02faf452011-11-29 11:23:58 +0000483 printf( HEADER_FORMAT, "RSA-2048" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000484 fflush( stdout );
485 set_alarm( 3 );
486
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200487 ret = 0;
488 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000489 {
490 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200491 ret = rsa_private( &rsa, myrand, NULL, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000492 }
493
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200494 if( ret != 0 )
495 printf( "FAILED\n" );
496 else
497 printf( "%9lu private/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000498
499 rsa_free( &rsa );
500
Paul Bakkera802e1a2010-08-16 11:56:45 +0000501 rsa_init( &rsa, RSA_PKCS_V15, 0 );
502 rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000503
Paul Bakker02faf452011-11-29 11:23:58 +0000504 printf( HEADER_FORMAT, "RSA-4096" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000505 fflush( stdout );
506 set_alarm( 3 );
507
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200508 ret = 0;
509 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000510 {
511 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200512 ret = rsa_public( &rsa, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000513 }
514
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200515 if( ret != 0 )
516 printf( "FAILED\n" );
517 else
518 printf( "%9lu public/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000519
Paul Bakker02faf452011-11-29 11:23:58 +0000520 printf( HEADER_FORMAT, "RSA-4096" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000521 fflush( stdout );
522 set_alarm( 3 );
523
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200524 ret = 0;
525 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000526 {
527 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200528 ret = rsa_private( &rsa, myrand, NULL, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000529 }
530
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200531 if( ret != 0 )
532 printf( "FAILED\n" );
533 else
534 printf( "%9lu private/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536 rsa_free( &rsa );
537#endif
538
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100539#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
540 memset( &dhm, 0, sizeof( dhm_context ) );
541
542 mpi_read_string( &dhm.P, 16, POLARSSL_DHM_RFC5114_MODP_1024_P );
543 mpi_read_string( &dhm.G, 16, POLARSSL_DHM_RFC5114_MODP_1024_G );
544 dhm.len = mpi_size( &dhm.P );
545 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
546 mpi_copy( &dhm.GY, &dhm.GX );
547
548 printf( HEADER_FORMAT, "DHM-1024" );
549 fflush( stdout );
550 set_alarm( 3 );
551
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200552 ret = 0;
553 for( i = 1; ! alarmed && ! ret ; i++ )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100554 {
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200555 olen = sizeof( buf );
556 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
557 ret |= dhm_calc_secret( &dhm, buf, &olen, NULL, NULL );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100558 }
559
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200560 if( ret != 0 )
561 printf( "FAILED\n" );
562 else
563 printf( "%9lu handshake/s\n", i / 3 );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100564
Manuel Pégourié-Gonnardce6352a2013-09-07 13:05:52 +0200565 printf( HEADER_FORMAT, "fixed-DHM-1024" );
566 fflush( stdout );
567 set_alarm( 3 );
568
569 ret = 0;
570 for( i = 1; ! alarmed && ! ret ; i++ )
571 {
572 olen = sizeof( buf );
573 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL );
574 }
575
576 if( ret != 0 )
577 printf( "FAILED\n" );
578 else
579 printf( "%9lu handshake/s\n", i / 3 );
580
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100581 dhm_free( &dhm );
582
583 memset( &dhm, 0, sizeof( dhm_context ) );
584
585 mpi_read_string( &dhm.P, 16, POLARSSL_DHM_RFC3526_MODP_2048_P );
586 mpi_read_string( &dhm.G, 16, POLARSSL_DHM_RFC3526_MODP_2048_G );
587 dhm.len = mpi_size( &dhm.P );
588 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
589 mpi_copy( &dhm.GY, &dhm.GX );
590
591 printf( HEADER_FORMAT, "DHM-2048" );
592 fflush( stdout );
593 set_alarm( 3 );
594
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200595 ret = 0;
596 for( i = 1; ! alarmed && ! ret ; i++ )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100597 {
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200598 olen = sizeof( buf );
599 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
Manuel Pégourié-Gonnardce6352a2013-09-07 13:05:52 +0200600 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL );
601 }
602
603 if( ret != 0 )
604 printf( "FAILED\n" );
605 else
606 printf( "%9lu handshake/s\n", i / 3 );
607
608 printf( HEADER_FORMAT, "fixed-DHM-2048" );
609 fflush( stdout );
610 set_alarm( 3 );
611
612 ret = 0;
613 for( i = 1; ! alarmed && ! ret ; i++ )
614 {
615 olen = sizeof( buf );
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200616 ret |= dhm_calc_secret( &dhm, buf, &olen, NULL, NULL );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100617 }
618
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200619 if( ret != 0 )
620 printf( "FAILED\n" );
621 else
622 printf( "%9lu handshake/s\n", i / 3 );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100623
624 dhm_free( &dhm );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100625 memset( &dhm, 0, sizeof( dhm_context ) );
626
627 mpi_read_string( &dhm.P, 16, POLARSSL_DHM_RFC3526_MODP_3072_P );
628 mpi_read_string( &dhm.G, 16, POLARSSL_DHM_RFC3526_MODP_3072_G );
629 dhm.len = mpi_size( &dhm.P );
630 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
631 mpi_copy( &dhm.GY, &dhm.GX );
632
633 printf( HEADER_FORMAT, "DHM-3072" );
634 fflush( stdout );
635 set_alarm( 3 );
636
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200637 ret = 0;
638 for( i = 1; ! alarmed && ! ret ; i++ )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100639 {
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200640 olen = sizeof( buf );
641 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
642 ret |= dhm_calc_secret( &dhm, buf, &olen, NULL, NULL );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100643 }
644
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200645 if( ret != 0 )
646 printf( "FAILED\n" );
647 else
648 printf( "%9lu handshake/s\n", i / 3 );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100649
Manuel Pégourié-Gonnardce6352a2013-09-07 13:05:52 +0200650 printf( HEADER_FORMAT, "fixed-DHM-3072" );
651 fflush( stdout );
652 set_alarm( 3 );
653
654 ret = 0;
655 for( i = 1; ! alarmed && ! ret ; i++ )
656 {
657 olen = sizeof( buf );
658 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL );
659 }
660
661 if( ret != 0 )
662 printf( "FAILED\n" );
663 else
664 printf( "%9lu handshake/s\n", i / 3 );
665
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100666 dhm_free( &dhm );
667#endif
668
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000669 printf( "\n" );
670
Paul Bakkercce9d772011-11-18 14:26:47 +0000671#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 printf( " Press Enter to exit this program.\n" );
673 fflush( stdout ); getchar();
674#endif
675
676 return( 0 );
677}
Paul Bakker5690efc2011-05-26 13:16:06 +0000678#endif /* POLARSSL_TIMING_C */