blob: 52aecf2b8d027177a2d07ab1cd03f53a8ea3d36a [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
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200101#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
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)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200106#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 aes_context aes;
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200108#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000109#if defined(POLARSSL_GCM_C)
110 gcm_context gcm;
111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000112#endif
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200113#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker3d58fe82012-07-04 17:15:31 +0000114 blowfish_context blowfish;
115#endif
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200116#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000117 camellia_context camellia;
118#endif
Paul Bakker5690efc2011-05-26 13:16:06 +0000119#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
120 defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 rsa_context rsa;
122#endif
Paul Bakker02faf452011-11-29 11:23:58 +0000123#if defined(POLARSSL_HAVEGE_C)
124 havege_state hs;
125#endif
126#if defined(POLARSSL_CTR_DRBG_C)
127 ctr_drbg_context ctr_drbg;
128#endif
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100129#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
130 dhm_context dhm;
131 size_t olen = BUFSIZE;
132#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000133 ((void) argc);
134 ((void) argv);
135
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 memset( buf, 0xAA, sizeof( buf ) );
137
138 printf( "\n" );
139
Paul Bakker40e46942009-01-03 21:51:57 +0000140#if defined(POLARSSL_MD4_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000141 printf( HEADER_FORMAT, "MD4" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 fflush( stdout );
143
144 set_alarm( 1 );
145 for( i = 1; ! alarmed; i++ )
146 md4( buf, BUFSIZE, tmp );
147
148 tsc = hardclock();
149 for( j = 0; j < 1024; j++ )
150 md4( buf, BUFSIZE, tmp );
151
152 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
153 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
154#endif
155
Paul Bakker40e46942009-01-03 21:51:57 +0000156#if defined(POLARSSL_MD5_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000157 printf( HEADER_FORMAT, "MD5" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 fflush( stdout );
159
160 set_alarm( 1 );
161 for( i = 1; ! alarmed; i++ )
162 md5( buf, BUFSIZE, tmp );
163
164 tsc = hardclock();
165 for( j = 0; j < 1024; j++ )
166 md5( buf, BUFSIZE, tmp );
167
168 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
169 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
170#endif
171
Paul Bakker40e46942009-01-03 21:51:57 +0000172#if defined(POLARSSL_SHA1_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000173 printf( HEADER_FORMAT, "SHA-1" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 fflush( stdout );
175
176 set_alarm( 1 );
177 for( i = 1; ! alarmed; i++ )
178 sha1( buf, BUFSIZE, tmp );
179
180 tsc = hardclock();
181 for( j = 0; j < 1024; j++ )
182 sha1( buf, BUFSIZE, tmp );
183
184 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
185 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
186#endif
187
Paul Bakker9e36f042013-06-30 14:34:05 +0200188#if defined(POLARSSL_SHA256_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000189 printf( HEADER_FORMAT, "SHA-256" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 fflush( stdout );
191
192 set_alarm( 1 );
193 for( i = 1; ! alarmed; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200194 sha256( buf, BUFSIZE, tmp, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 tsc = hardclock();
197 for( j = 0; j < 1024; j++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200198 sha256( buf, BUFSIZE, tmp, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
201 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
202#endif
203
Paul Bakker9e36f042013-06-30 14:34:05 +0200204#if defined(POLARSSL_SHA512_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000205 printf( HEADER_FORMAT, "SHA-512" );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000206 fflush( stdout );
207
208 set_alarm( 1 );
209 for( i = 1; ! alarmed; i++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200210 sha512( buf, BUFSIZE, tmp, 0 );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000211
212 tsc = hardclock();
213 for( j = 0; j < 1024; j++ )
Paul Bakker9e36f042013-06-30 14:34:05 +0200214 sha512( buf, BUFSIZE, tmp, 0 );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000215
216 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
217 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
218#endif
219
Paul Bakker40e46942009-01-03 21:51:57 +0000220#if defined(POLARSSL_ARC4_C)
Paul Bakker02faf452011-11-29 11:23:58 +0000221 printf( HEADER_FORMAT, "ARC4" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 fflush( stdout );
223
224 arc4_setup( &arc4, tmp, 32 );
225
226 set_alarm( 1 );
227 for( i = 1; ! alarmed; i++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000228 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230 tsc = hardclock();
231 for( j = 0; j < 1024; j++ )
Paul Bakkerbaad6502010-03-21 15:42:15 +0000232 arc4_crypt( &arc4, BUFSIZE, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
235 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
236#endif
237
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200238#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker02faf452011-11-29 11:23:58 +0000239 printf( HEADER_FORMAT, "3DES" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 fflush( stdout );
241
242 des3_set3key_enc( &des3, tmp );
243
244 set_alarm( 1 );
245 for( i = 1; ! alarmed; i++ )
246 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
247
248 tsc = hardclock();
249 for( j = 0; j < 1024; j++ )
250 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
251
252 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
253 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
254
Paul Bakker02faf452011-11-29 11:23:58 +0000255 printf( HEADER_FORMAT, "DES" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 fflush( stdout );
257
258 des_setkey_enc( &des, tmp );
259
260 set_alarm( 1 );
261 for( i = 1; ! alarmed; i++ )
262 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
263
264 tsc = hardclock();
265 for( j = 0; j < 1024; j++ )
266 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf );
267
268 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
269 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
270#endif
271
Paul Bakker40e46942009-01-03 21:51:57 +0000272#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200273#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 for( keysize = 128; keysize <= 256; keysize += 64 )
275 {
Paul Bakker89e80c92012-03-20 13:50:09 +0000276 printf( " AES-CBC-%d : ", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 fflush( stdout );
278
279 memset( buf, 0, sizeof( buf ) );
280 memset( tmp, 0, sizeof( tmp ) );
281 aes_setkey_enc( &aes, tmp, keysize );
282
283 set_alarm( 1 );
284
285 for( i = 1; ! alarmed; i++ )
286 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
287
288 tsc = hardclock();
289 for( j = 0; j < 4096; j++ )
290 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf );
291
292 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
293 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
294 }
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200295#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker89e80c92012-03-20 13:50:09 +0000296#if defined(POLARSSL_GCM_C)
297 for( keysize = 128; keysize <= 256; keysize += 64 )
298 {
299 printf( " AES-GCM-%d : ", keysize );
300 fflush( stdout );
301
302 memset( buf, 0, sizeof( buf ) );
303 memset( tmp, 0, sizeof( tmp ) );
Paul Bakker8f0423a2013-09-10 14:51:50 +0200304 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000305
306 set_alarm( 1 );
307
308 for( i = 1; ! alarmed; i++ )
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 tsc = hardclock();
312 for( j = 0; j < 4096; j++ )
Paul Bakkerb78c7452012-03-20 15:05:59 +0000313 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, 12, NULL, 0, buf, buf, 16, tmp );
Paul Bakker89e80c92012-03-20 13:50:09 +0000314
315 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
316 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
317 }
318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000319#endif
320
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200321#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000322 for( keysize = 128; keysize <= 256; keysize += 64 )
323 {
Paul Bakker89e80c92012-03-20 13:50:09 +0000324 printf( " CAMELLIA-CBC-%d: ", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000325 fflush( stdout );
326
327 memset( buf, 0, sizeof( buf ) );
328 memset( tmp, 0, sizeof( tmp ) );
329 camellia_setkey_enc( &camellia, tmp, keysize );
330
331 set_alarm( 1 );
332
333 for( i = 1; ! alarmed; i++ )
334 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
335
336 tsc = hardclock();
337 for( j = 0; j < 4096; j++ )
338 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, BUFSIZE, tmp, buf, buf );
339
340 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
341 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
342 }
343#endif
344
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200345#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker3d58fe82012-07-04 17:15:31 +0000346 for( keysize = 128; keysize <= 256; keysize += 64 )
347 {
348 printf( " BLOWFISH-CBC-%d: ", keysize );
349 fflush( stdout );
350
351 memset( buf, 0, sizeof( buf ) );
352 memset( tmp, 0, sizeof( tmp ) );
353 blowfish_setkey( &blowfish, tmp, keysize );
354
355 set_alarm( 1 );
356
357 for( i = 1; ! alarmed; i++ )
358 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf );
359
360 tsc = hardclock();
361 for( j = 0; j < 4096; j++ )
362 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, tmp, buf, buf );
363
364 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
365 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
366 }
367#endif
368
Paul Bakker02faf452011-11-29 11:23:58 +0000369#if defined(POLARSSL_HAVEGE_C)
370 printf( HEADER_FORMAT, "HAVEGE" );
371 fflush( stdout );
372
373 havege_init( &hs );
374
375 set_alarm( 1 );
376 for( i = 1; ! alarmed; i++ )
377 havege_random( &hs, buf, BUFSIZE );
378
379 tsc = hardclock();
380 for( j = 1; j < 1024; j++ )
381 havege_random( &hs, buf, BUFSIZE );
382
383 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
384 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
385#endif
386
387#if defined(POLARSSL_CTR_DRBG_C)
388 printf( HEADER_FORMAT, "CTR_DRBG (NOPR)" );
389 fflush( stdout );
390
391 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
392 exit(1);
393
394 set_alarm( 1 );
395 for( i = 1; ! alarmed; i++ )
396 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
397 exit(1);
398
399 tsc = hardclock();
400 for( j = 1; j < 1024; j++ )
401 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
402 exit(1);
403
404 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
405 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
406
407 printf( HEADER_FORMAT, "CTR_DRBG (PR)" );
408 fflush( stdout );
409
410 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
411 exit(1);
412
413 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
414
415 set_alarm( 1 );
416 for( i = 1; ! alarmed; i++ )
417 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
418 exit(1);
419
420 tsc = hardclock();
421 for( j = 1; j < 1024; j++ )
422 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
423 exit(1);
424
425 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024,
426 ( hardclock() - tsc ) / ( j * BUFSIZE ) );
427#endif
428
Paul Bakker5690efc2011-05-26 13:16:06 +0000429#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) && \
430 defined(POLARSSL_GENPRIME)
Paul Bakkera802e1a2010-08-16 11:56:45 +0000431 rsa_init( &rsa, RSA_PKCS_V15, 0 );
432 rsa_gen_key( &rsa, myrand, NULL, 1024, 65537 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Paul Bakker02faf452011-11-29 11:23:58 +0000434 printf( HEADER_FORMAT, "RSA-1024" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 fflush( stdout );
436 set_alarm( 3 );
437
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200438 ret = 0;
439 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 {
441 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200442 ret = rsa_public( &rsa, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 }
444
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200445 if( ret != 0 )
446 printf( "FAILED\n" );
447 else
448 printf( "%9lu public/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
Paul Bakker02faf452011-11-29 11:23:58 +0000450 printf( HEADER_FORMAT, "RSA-1024" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 fflush( stdout );
452 set_alarm( 3 );
453
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200454 ret = 0;
455 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 {
457 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200458 ret = rsa_private( &rsa, myrand, NULL, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 }
460
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200461 if( ret != 0 )
462 printf( "FAILED\n" );
463 else
464 printf( "%9lu private/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000465
466 rsa_free( &rsa );
467
Paul Bakkera802e1a2010-08-16 11:56:45 +0000468 rsa_init( &rsa, RSA_PKCS_V15, 0 );
469 rsa_gen_key( &rsa, myrand, NULL, 2048, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000470
Paul Bakker02faf452011-11-29 11:23:58 +0000471 printf( HEADER_FORMAT, "RSA-2048" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000472 fflush( stdout );
473 set_alarm( 3 );
474
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200475 ret = 0;
476 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000477 {
478 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200479 ret = rsa_public( &rsa, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000480 }
481
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200482 if( ret != 0 )
483 printf( "FAILED\n" );
484 else
485 printf( "%9lu public/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000486
Paul Bakker02faf452011-11-29 11:23:58 +0000487 printf( HEADER_FORMAT, "RSA-2048" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000488 fflush( stdout );
489 set_alarm( 3 );
490
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200491 ret = 0;
492 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000493 {
494 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200495 ret = rsa_private( &rsa, myrand, NULL, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000496 }
497
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200498 if( ret != 0 )
499 printf( "FAILED\n" );
500 else
501 printf( "%9lu private/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000502
503 rsa_free( &rsa );
504
Paul Bakkera802e1a2010-08-16 11:56:45 +0000505 rsa_init( &rsa, RSA_PKCS_V15, 0 );
506 rsa_gen_key( &rsa, myrand, NULL, 4096, 65537 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000507
Paul Bakker02faf452011-11-29 11:23:58 +0000508 printf( HEADER_FORMAT, "RSA-4096" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000509 fflush( stdout );
510 set_alarm( 3 );
511
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200512 ret = 0;
513 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000514 {
515 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200516 ret = rsa_public( &rsa, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000517 }
518
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200519 if( ret != 0 )
520 printf( "FAILED\n" );
521 else
522 printf( "%9lu public/s\n", i / 3 );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000523
Paul Bakker02faf452011-11-29 11:23:58 +0000524 printf( HEADER_FORMAT, "RSA-4096" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000525 fflush( stdout );
526 set_alarm( 3 );
527
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200528 ret = 0;
529 for( i = 1; ! alarmed && ! ret ; i++ )
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000530 {
531 buf[0] = 0;
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200532 ret = rsa_private( &rsa, myrand, NULL, buf, buf );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000533 }
534
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200535 if( ret != 0 )
536 printf( "FAILED\n" );
537 else
538 printf( "%9lu private/s\n", i / 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540 rsa_free( &rsa );
541#endif
542
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100543#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
544 memset( &dhm, 0, sizeof( dhm_context ) );
545
546 mpi_read_string( &dhm.P, 16, POLARSSL_DHM_RFC5114_MODP_1024_P );
547 mpi_read_string( &dhm.G, 16, POLARSSL_DHM_RFC5114_MODP_1024_G );
548 dhm.len = mpi_size( &dhm.P );
549 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
550 mpi_copy( &dhm.GY, &dhm.GX );
551
552 printf( HEADER_FORMAT, "DHM-1024" );
553 fflush( stdout );
554 set_alarm( 3 );
555
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200556 ret = 0;
557 for( i = 1; ! alarmed && ! ret ; i++ )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100558 {
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200559 olen = sizeof( buf );
560 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
561 ret |= dhm_calc_secret( &dhm, buf, &olen, NULL, NULL );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100562 }
563
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200564 if( ret != 0 )
565 printf( "FAILED\n" );
566 else
567 printf( "%9lu handshake/s\n", i / 3 );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100568
Manuel Pégourié-Gonnardce6352a2013-09-07 13:05:52 +0200569 printf( HEADER_FORMAT, "fixed-DHM-1024" );
570 fflush( stdout );
571 set_alarm( 3 );
572
573 ret = 0;
574 for( i = 1; ! alarmed && ! ret ; i++ )
575 {
576 olen = sizeof( buf );
577 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL );
578 }
579
580 if( ret != 0 )
581 printf( "FAILED\n" );
582 else
583 printf( "%9lu handshake/s\n", i / 3 );
584
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100585 dhm_free( &dhm );
586
587 memset( &dhm, 0, sizeof( dhm_context ) );
588
589 mpi_read_string( &dhm.P, 16, POLARSSL_DHM_RFC3526_MODP_2048_P );
590 mpi_read_string( &dhm.G, 16, POLARSSL_DHM_RFC3526_MODP_2048_G );
591 dhm.len = mpi_size( &dhm.P );
592 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
593 mpi_copy( &dhm.GY, &dhm.GX );
594
595 printf( HEADER_FORMAT, "DHM-2048" );
596 fflush( stdout );
597 set_alarm( 3 );
598
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200599 ret = 0;
600 for( i = 1; ! alarmed && ! ret ; i++ )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100601 {
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200602 olen = sizeof( buf );
603 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
Manuel Pégourié-Gonnardce6352a2013-09-07 13:05:52 +0200604 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL );
605 }
606
607 if( ret != 0 )
608 printf( "FAILED\n" );
609 else
610 printf( "%9lu handshake/s\n", i / 3 );
611
612 printf( HEADER_FORMAT, "fixed-DHM-2048" );
613 fflush( stdout );
614 set_alarm( 3 );
615
616 ret = 0;
617 for( i = 1; ! alarmed && ! ret ; i++ )
618 {
619 olen = sizeof( buf );
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200620 ret |= dhm_calc_secret( &dhm, buf, &olen, NULL, NULL );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100621 }
622
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200623 if( ret != 0 )
624 printf( "FAILED\n" );
625 else
626 printf( "%9lu handshake/s\n", i / 3 );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100627
628 dhm_free( &dhm );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100629 memset( &dhm, 0, sizeof( dhm_context ) );
630
631 mpi_read_string( &dhm.P, 16, POLARSSL_DHM_RFC3526_MODP_3072_P );
632 mpi_read_string( &dhm.G, 16, POLARSSL_DHM_RFC3526_MODP_3072_G );
633 dhm.len = mpi_size( &dhm.P );
634 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
635 mpi_copy( &dhm.GY, &dhm.GX );
636
637 printf( HEADER_FORMAT, "DHM-3072" );
638 fflush( stdout );
639 set_alarm( 3 );
640
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200641 ret = 0;
642 for( i = 1; ! alarmed && ! ret ; i++ )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100643 {
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200644 olen = sizeof( buf );
645 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
646 ret |= dhm_calc_secret( &dhm, buf, &olen, NULL, NULL );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100647 }
648
Manuel Pégourié-Gonnard1a201242013-09-07 12:27:35 +0200649 if( ret != 0 )
650 printf( "FAILED\n" );
651 else
652 printf( "%9lu handshake/s\n", i / 3 );
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100653
Manuel Pégourié-Gonnardce6352a2013-09-07 13:05:52 +0200654 printf( HEADER_FORMAT, "fixed-DHM-3072" );
655 fflush( stdout );
656 set_alarm( 3 );
657
658 ret = 0;
659 for( i = 1; ! alarmed && ! ret ; i++ )
660 {
661 olen = sizeof( buf );
662 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL );
663 }
664
665 if( ret != 0 )
666 printf( "FAILED\n" );
667 else
668 printf( "%9lu handshake/s\n", i / 3 );
669
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100670 dhm_free( &dhm );
671#endif
672
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000673 printf( "\n" );
674
Paul Bakkercce9d772011-11-18 14:26:47 +0000675#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 printf( " Press Enter to exit this program.\n" );
677 fflush( stdout ); getchar();
678#endif
679
680 return( 0 );
681}
Paul Bakker5690efc2011-05-26 13:16:06 +0000682#endif /* POLARSSL_TIMING_C */