blob: e2d2b98f6fd1c3f5c294daa43880e27ec2582dcc [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"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020035#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/md4.h"
38#include "polarssl/md5.h"
39#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020040#include "polarssl/sha256.h"
41#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/arc4.h"
43#include "polarssl/des.h"
44#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000045#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000046#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000047#include "polarssl/gcm.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020048#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010050#include "polarssl/dhm.h"
Paul Bakker02faf452011-11-29 11:23:58 +000051#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker02faf452011-11-29 11:23:58 +000053#define BUFSIZE 1024
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020054#define HEADER_FORMAT " %-16s : "
55#define TITLE_LEN 17
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020057#if !defined(POLARSSL_TIMING_C)
58int main( int argc, char *argv[] )
59{
60 ((void) argc);
61 ((void) argv);
62
63 printf("POLARSSL_TIMING_C not defined.\n");
64 return( 0 );
65}
66#else
67
Paul Bakkera3d195c2011-11-27 21:07:34 +000068static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
Paul Bakkera3d195c2011-11-27 21:07:34 +000070 size_t use_len;
71 int rnd;
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073 if( rng_state != NULL )
74 rng_state = NULL;
75
Paul Bakkera3d195c2011-11-27 21:07:34 +000076 while( len > 0 )
77 {
78 use_len = len;
79 if( use_len > sizeof(int) )
80 use_len = sizeof(int);
81
82 rnd = rand();
83 memcpy( output, &rnd, use_len );
84 output += use_len;
85 len -= use_len;
86 }
87
88 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000089}
90
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020091#define TIME_AND_TSC( TITLE, CODE ) \
92do { \
93 unsigned long i, j, tsc; \
94 \
95 printf( HEADER_FORMAT, TITLE ); \
96 fflush( stdout ); \
97 \
98 set_alarm( 1 ); \
99 for( i = 1; ! alarmed; i++ ) \
100 { \
101 CODE; \
102 } \
103 \
104 tsc = hardclock(); \
105 for( j = 0; j < 1024; j++ ) \
106 { \
107 CODE; \
108 } \
109 \
110 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
111 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
112} while( 0 )
113
114#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
115do { \
116 unsigned long i; \
117 int ret; \
118 \
119 printf( HEADER_FORMAT, TITLE ); \
120 fflush( stdout ); \
121 set_alarm( 3 ); \
122 \
123 ret = 0; \
124 for( i = 1; ! alarmed && ! ret ; i++ ) \
125 { \
126 CODE; \
127 } \
128 \
129 if( ret != 0 ) \
130 printf( "FAILED\n" ); \
131 else \
132 printf( "%9lu " TYPE "/s\n", i / 3 ); \
133} while( 0 )
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135unsigned char buf[BUFSIZE];
136
Paul Bakkercce9d772011-11-18 14:26:47 +0000137int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000138{
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200139 int keysize;
Paul Bakker5a0aa772009-02-09 22:38:52 +0000140 unsigned char tmp[64];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200141 char title[TITLE_LEN];
Paul Bakkercce9d772011-11-18 14:26:47 +0000142 ((void) argc);
143 ((void) argv);
144
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 memset( buf, 0xAA, sizeof( buf ) );
146
147 printf( "\n" );
148
Paul Bakker40e46942009-01-03 21:51:57 +0000149#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200150 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151#endif
152
Paul Bakker40e46942009-01-03 21:51:57 +0000153#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200154 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155#endif
156
Paul Bakker40e46942009-01-03 21:51:57 +0000157#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200158 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159#endif
160
Paul Bakker9e36f042013-06-30 14:34:05 +0200161#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200162 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163#endif
164
Paul Bakker9e36f042013-06-30 14:34:05 +0200165#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200166 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000167#endif
168
Paul Bakker40e46942009-01-03 21:51:57 +0000169#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200170 {
171 arc4_context arc4;
172 arc4_setup( &arc4, tmp, 32 );
173 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
174 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000175#endif
176
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200177#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200178 {
179 des3_context des3;
180 des3_set3key_enc( &des3, tmp );
181 TIME_AND_TSC( "3DES",
182 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
183 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200185 {
186 des_context des;
187 des_setkey_enc( &des, tmp );
188 TIME_AND_TSC( "DES",
189 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
190 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000191#endif
192
Paul Bakker40e46942009-01-03 21:51:57 +0000193#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200194#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200196 aes_context aes;
197 for( keysize = 128; keysize <= 256; keysize += 64 )
198 {
199 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200201 memset( buf, 0, sizeof( buf ) );
202 memset( tmp, 0, sizeof( tmp ) );
203 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200205 TIME_AND_TSC( title,
206 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
207 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200209#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000210#if defined(POLARSSL_GCM_C)
Paul Bakker89e80c92012-03-20 13:50:09 +0000211 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200212 gcm_context gcm;
213 for( keysize = 128; keysize <= 256; keysize += 64 )
214 {
215 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000216
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200217 memset( buf, 0, sizeof( buf ) );
218 memset( tmp, 0, sizeof( tmp ) );
219 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000220
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200221 TIME_AND_TSC( title,
222 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
223 12, NULL, 0, buf, buf, 16, tmp ) );
224 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000225 }
226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000227#endif
228
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200229#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker38119b12009-01-10 23:31:23 +0000230 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200231 camellia_context camellia;
232 for( keysize = 128; keysize <= 256; keysize += 64 )
233 {
234 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000235
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200236 memset( buf, 0, sizeof( buf ) );
237 memset( tmp, 0, sizeof( tmp ) );
238 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000239
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200240 TIME_AND_TSC( title,
241 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
242 BUFSIZE, tmp, buf, buf ) );
243 }
Paul Bakker38119b12009-01-10 23:31:23 +0000244 }
245#endif
246
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200247#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker3d58fe82012-07-04 17:15:31 +0000248 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200249 blowfish_context blowfish;
250 for( keysize = 128; keysize <= 256; keysize += 64 )
251 {
252 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000253
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200254 memset( buf, 0, sizeof( buf ) );
255 memset( tmp, 0, sizeof( tmp ) );
256 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000257
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200258 TIME_AND_TSC( title,
259 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
260 tmp, buf, buf ) );
261 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000262 }
263#endif
264
Paul Bakker02faf452011-11-29 11:23:58 +0000265#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200266 {
267 havege_state hs;
268 havege_init( &hs );
269 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
270 }
Paul Bakker02faf452011-11-29 11:23:58 +0000271#endif
272
273#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200274 {
275 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000276
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200277 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000278 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200279 TIME_AND_TSC( "CTR_DRBG (NOPR)",
280 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
281 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000282
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200283 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000284 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200285 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
286 TIME_AND_TSC( "CTR_DRBG (PR)",
287 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
288 exit(1) );
289 }
Paul Bakker02faf452011-11-29 11:23:58 +0000290#endif
291
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200292#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200294 rsa_context rsa;
295 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
296 {
297 snprintf( title, sizeof( title ), "RSA-%d", keysize );
298
299 rsa_init( &rsa, RSA_PKCS_V15, 0 );
300 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
301
302 TIME_PUBLIC( title, " public",
303 buf[0] = 0;
304 ret = rsa_public( &rsa, buf, buf ) );
305
306 TIME_PUBLIC( title, "private",
307 buf[0] = 0;
308 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
309
310 rsa_free( &rsa );
311 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000313#endif
314
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100315#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100316 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200317#define DHM_SIZES 3
318 int sizes[DHM_SIZES] = { 1024, 2048, 3072 };
319 const char *dhm_P[DHM_SIZES] = {
320 POLARSSL_DHM_RFC5114_MODP_1024_P,
321 POLARSSL_DHM_RFC3526_MODP_2048_P,
322 POLARSSL_DHM_RFC3526_MODP_3072_P,
323 };
324 const char *dhm_G[DHM_SIZES] = {
325 POLARSSL_DHM_RFC5114_MODP_1024_G,
326 POLARSSL_DHM_RFC3526_MODP_2048_G,
327 POLARSSL_DHM_RFC3526_MODP_3072_G,
328 };
329
330 dhm_context dhm;
331 size_t olen;
332 for( keysize = 0; keysize < DHM_SIZES; keysize++ )
333 {
334 memset( &dhm, 0, sizeof( dhm_context ) );
335
336 mpi_read_string( &dhm.P, 16, dhm_P[keysize] );
337 mpi_read_string( &dhm.G, 16, dhm_G[keysize] );
338 dhm.len = mpi_size( &dhm.P );
339 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
340 mpi_copy( &dhm.GY, &dhm.GX );
341
342 snprintf( title, sizeof( title ), "DHM-%d", sizes[keysize] );
343 TIME_PUBLIC( title, "handshake",
344 olen = sizeof( buf );
345 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len,
346 myrand, NULL );
347 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
348
349 snprintf( title, sizeof( title ), "DHM-%d-fixed", sizes[keysize] );
350 TIME_PUBLIC( title, "handshake",
351 olen = sizeof( buf );
352 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
353
354 dhm_free( &dhm );
355 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100356 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100357#endif
358
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000359 printf( "\n" );
360
Paul Bakkercce9d772011-11-18 14:26:47 +0000361#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 printf( " Press Enter to exit this program.\n" );
363 fflush( stdout ); getchar();
364#endif
365
366 return( 0 );
367}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200368
Paul Bakker5690efc2011-05-26 13:16:06 +0000369#endif /* POLARSSL_TIMING_C */