blob: 7262b563e1f306d7c12a7553a18e15074f6c3f9f [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020036#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/md4.h"
39#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010040#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020042#include "polarssl/sha256.h"
43#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/arc4.h"
45#include "polarssl/des.h"
46#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000047#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000048#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000049#include "polarssl/gcm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020050#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020051#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010052#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010054#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020055#include "polarssl/ecdsa.h"
56#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010057#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020059#if defined _MSC_VER && !defined snprintf
60#define snprintf _snprintf
61#endif
62
Paul Bakker02faf452011-11-29 11:23:58 +000063#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010064#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010065#define TITLE_LEN 25
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020067#if !defined(POLARSSL_TIMING_C)
68int main( int argc, char *argv[] )
69{
70 ((void) argc);
71 ((void) argv);
72
73 printf("POLARSSL_TIMING_C not defined.\n");
74 return( 0 );
75}
76#else
77
Paul Bakkera3d195c2011-11-27 21:07:34 +000078static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
Paul Bakkera3d195c2011-11-27 21:07:34 +000080 size_t use_len;
81 int rnd;
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083 if( rng_state != NULL )
84 rng_state = NULL;
85
Paul Bakkera3d195c2011-11-27 21:07:34 +000086 while( len > 0 )
87 {
88 use_len = len;
89 if( use_len > sizeof(int) )
90 use_len = sizeof(int);
91
92 rnd = rand();
93 memcpy( output, &rnd, use_len );
94 output += use_len;
95 len -= use_len;
96 }
97
98 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000099}
100
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200101#define TIME_AND_TSC( TITLE, CODE ) \
102do { \
103 unsigned long i, j, tsc; \
104 \
105 printf( HEADER_FORMAT, TITLE ); \
106 fflush( stdout ); \
107 \
108 set_alarm( 1 ); \
109 for( i = 1; ! alarmed; i++ ) \
110 { \
111 CODE; \
112 } \
113 \
114 tsc = hardclock(); \
115 for( j = 0; j < 1024; j++ ) \
116 { \
117 CODE; \
118 } \
119 \
120 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
121 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
122} while( 0 )
123
Paul Bakker0c5e4292014-05-22 14:11:13 +0200124#if defined(POLARSSL_ERROR_C)
125#define PRINT_ERROR \
126 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
127 printf( "FAILED: %s\n", tmp );
128#else
129#define PRINT_ERROR \
130 printf( "FAILED: -0x%04x\n", -ret );
131#endif
132
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200133#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
134do { \
135 unsigned long i; \
136 int ret; \
137 \
138 printf( HEADER_FORMAT, TITLE ); \
139 fflush( stdout ); \
140 set_alarm( 3 ); \
141 \
142 ret = 0; \
143 for( i = 1; ! alarmed && ! ret ; i++ ) \
144 { \
145 CODE; \
146 } \
147 \
148 if( ret != 0 ) \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100149 { \
Paul Bakker0c5e4292014-05-22 14:11:13 +0200150PRINT_ERROR; \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100151 } \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200152 else \
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100153 printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200154} while( 0 )
155
Paul Bakker5121ce52009-01-03 21:22:43 +0000156unsigned char buf[BUFSIZE];
157
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200158typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100159 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200160 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100161 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200162 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200163} todo_list;
164
165#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100166 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200167 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100168 "havege, ctr_drbg, hmac_drbg\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200169 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200170
Paul Bakkercce9d772011-11-18 14:26:47 +0000171int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000172{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200173 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200174 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200175 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200176 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000177
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200178 if( argc == 1 )
179 memset( &todo, 1, sizeof( todo ) );
180 else
181 {
182 memset( &todo, 0, sizeof( todo ) );
183
184 for( i = 1; i < argc; i++ )
185 {
186 if( strcmp( argv[i], "md4" ) == 0 )
187 todo.md4 = 1;
188 else if( strcmp( argv[i], "md5" ) == 0 )
189 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100190 else if( strcmp( argv[i], "ripemd160" ) == 0 )
191 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200192 else if( strcmp( argv[i], "sha1" ) == 0 )
193 todo.sha1 = 1;
194 else if( strcmp( argv[i], "sha256" ) == 0 )
195 todo.sha256 = 1;
196 else if( strcmp( argv[i], "sha512" ) == 0 )
197 todo.sha512 = 1;
198 else if( strcmp( argv[i], "arc4" ) == 0 )
199 todo.arc4 = 1;
200 else if( strcmp( argv[i], "des3" ) == 0 )
201 todo.des3 = 1;
202 else if( strcmp( argv[i], "des" ) == 0 )
203 todo.des = 1;
204 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
205 todo.aes_cbc = 1;
206 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
207 todo.aes_gcm = 1;
208 else if( strcmp( argv[i], "camellia" ) == 0 )
209 todo.camellia = 1;
210 else if( strcmp( argv[i], "blowfish" ) == 0 )
211 todo.blowfish = 1;
212 else if( strcmp( argv[i], "havege" ) == 0 )
213 todo.havege = 1;
214 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
215 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100216 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
217 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200218 else if( strcmp( argv[i], "rsa" ) == 0 )
219 todo.rsa = 1;
220 else if( strcmp( argv[i], "dhm" ) == 0 )
221 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200222 else if( strcmp( argv[i], "ecdsa" ) == 0 )
223 todo.ecdsa = 1;
224 else if( strcmp( argv[i], "ecdh" ) == 0 )
225 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200226 else
227 {
228 printf( "Unrecognized option: %s\n", argv[i] );
229 printf( "Available options:" OPTIONS );
230 }
231 }
232 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234 printf( "\n" );
235
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200236 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200237 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200238
Paul Bakker40e46942009-01-03 21:51:57 +0000239#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200240 if( todo.md4 )
241 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242#endif
243
Paul Bakker40e46942009-01-03 21:51:57 +0000244#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200245 if( todo.md5 )
246 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247#endif
248
Paul Bakker61b699e2014-01-22 13:35:29 +0100249#if defined(POLARSSL_RIPEMD160_C)
250 if( todo.ripemd160 )
251 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100252#endif
253
Paul Bakker40e46942009-01-03 21:51:57 +0000254#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255 if( todo.sha1 )
256 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257#endif
258
Paul Bakker9e36f042013-06-30 14:34:05 +0200259#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200260 if( todo.sha256 )
261 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262#endif
263
Paul Bakker9e36f042013-06-30 14:34:05 +0200264#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200265 if( todo.sha512 )
266 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000267#endif
268
Paul Bakker40e46942009-01-03 21:51:57 +0000269#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200271 {
272 arc4_context arc4;
273 arc4_setup( &arc4, tmp, 32 );
274 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
275 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000276#endif
277
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200278#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200279 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200280 {
281 des3_context des3;
282 des3_set3key_enc( &des3, tmp );
283 TIME_AND_TSC( "3DES",
284 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
285 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200287 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200288 {
289 des_context des;
290 des_setkey_enc( &des, tmp );
291 TIME_AND_TSC( "DES",
292 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
293 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000294#endif
295
Paul Bakker40e46942009-01-03 21:51:57 +0000296#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200297#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200298 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200300 aes_context aes;
301 for( keysize = 128; keysize <= 256; keysize += 64 )
302 {
303 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200305 memset( buf, 0, sizeof( buf ) );
306 memset( tmp, 0, sizeof( tmp ) );
307 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200309 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200310 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200311 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200313#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000314#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200315 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000316 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200317 gcm_context gcm;
318 for( keysize = 128; keysize <= 256; keysize += 64 )
319 {
320 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000321
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200322 memset( buf, 0, sizeof( buf ) );
323 memset( tmp, 0, sizeof( tmp ) );
324 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000325
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200326 TIME_AND_TSC( title,
327 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
328 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100329
330 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200331 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000332 }
333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000334#endif
335
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200336#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200337 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000338 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200339 camellia_context camellia;
340 for( keysize = 128; keysize <= 256; keysize += 64 )
341 {
342 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000343
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200344 memset( buf, 0, sizeof( buf ) );
345 memset( tmp, 0, sizeof( tmp ) );
346 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000347
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200348 TIME_AND_TSC( title,
349 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
350 BUFSIZE, tmp, buf, buf ) );
351 }
Paul Bakker38119b12009-01-10 23:31:23 +0000352 }
353#endif
354
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200355#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200356 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000357 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200358 blowfish_context blowfish;
359 for( keysize = 128; keysize <= 256; keysize += 64 )
360 {
361 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000362
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200363 memset( buf, 0, sizeof( buf ) );
364 memset( tmp, 0, sizeof( tmp ) );
365 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000366
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200367 TIME_AND_TSC( title,
368 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
369 tmp, buf, buf ) );
370 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000371 }
372#endif
373
Paul Bakker02faf452011-11-29 11:23:58 +0000374#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200375 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 {
377 havege_state hs;
378 havege_init( &hs );
379 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
380 }
Paul Bakker02faf452011-11-29 11:23:58 +0000381#endif
382
383#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200384 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200385 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200386 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000387
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200388 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000389 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200390 TIME_AND_TSC( "CTR_DRBG (NOPR)",
391 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
392 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000393
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200394 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000395 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200396 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
397 TIME_AND_TSC( "CTR_DRBG (PR)",
398 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
399 exit(1) );
400 }
Paul Bakker02faf452011-11-29 11:23:58 +0000401#endif
402
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100403#if defined(POLARSSL_HMAC_DRBG_C)
404 if( todo.hmac_drbg )
405 {
406 hmac_drbg_context hmac_drbg;
407 const md_info_t *md_info;
408
409#if defined(POLARSSL_SHA1_C)
410 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
411 exit(1);
412
413 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
414 exit(1);
415 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
416 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
417 exit(1) );
418 hmac_drbg_free( &hmac_drbg );
419
420 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
421 exit(1);
422 hmac_drbg_set_prediction_resistance( &hmac_drbg,
423 POLARSSL_HMAC_DRBG_PR_ON );
424 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
425 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
426 exit(1) );
427 hmac_drbg_free( &hmac_drbg );
428#endif
429
430#if defined(POLARSSL_SHA256_C)
431 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
432 exit(1);
433
434 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
435 exit(1);
436 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
437 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
438 exit(1) );
439 hmac_drbg_free( &hmac_drbg );
440
441 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
442 exit(1);
443 hmac_drbg_set_prediction_resistance( &hmac_drbg,
444 POLARSSL_HMAC_DRBG_PR_ON );
445 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
446 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
447 exit(1) );
448 hmac_drbg_free( &hmac_drbg );
449#endif
450 }
451#endif
452
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200454 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200456 rsa_context rsa;
457 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
458 {
459 snprintf( title, sizeof( title ), "RSA-%d", keysize );
460
461 rsa_init( &rsa, RSA_PKCS_V15, 0 );
462 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
463
464 TIME_PUBLIC( title, " public",
465 buf[0] = 0;
466 ret = rsa_public( &rsa, buf, buf ) );
467
468 TIME_PUBLIC( title, "private",
469 buf[0] = 0;
470 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
471
472 rsa_free( &rsa );
473 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000475#endif
476
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100477#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200478 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100479 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200480#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200481 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200482 const char *dhm_P[DHM_SIZES] = {
483 POLARSSL_DHM_RFC5114_MODP_1024_P,
484 POLARSSL_DHM_RFC3526_MODP_2048_P,
485 POLARSSL_DHM_RFC3526_MODP_3072_P,
486 };
487 const char *dhm_G[DHM_SIZES] = {
488 POLARSSL_DHM_RFC5114_MODP_1024_G,
489 POLARSSL_DHM_RFC3526_MODP_2048_G,
490 POLARSSL_DHM_RFC3526_MODP_3072_G,
491 };
492
493 dhm_context dhm;
494 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200495 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200496 {
497 memset( &dhm, 0, sizeof( dhm_context ) );
498
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200499 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
500 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
501 {
502 exit( 1 );
503 }
504
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200505 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100506 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200507 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
508 exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200509
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200510 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200511 TIME_PUBLIC( title, "handshake",
512 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100513 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200514 myrand, NULL );
515 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
516
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200517 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200518 TIME_PUBLIC( title, "handshake",
519 olen = sizeof( buf );
520 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
521
522 dhm_free( &dhm );
523 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100524 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100525#endif
526
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200527#if defined(POLARSSL_ECDSA_C)
528 if( todo.ecdsa )
529 {
530 ecdsa_context ecdsa;
531 const ecp_curve_info *curve_info;
532 size_t sig_len;
533
534 memset( buf, 0x2A, sizeof( buf ) );
535
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200536 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200537 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
538 curve_info++ )
539 {
540 ecdsa_init( &ecdsa );
541
542 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
543 exit( 1 );
544
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200545 snprintf( title, sizeof( title ), "ECDSA-%s",
546 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200547 TIME_PUBLIC( title, "sign",
548 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200549 tmp, &sig_len, myrand, NULL ) );
550
551 TIME_PUBLIC( title, "verify",
552 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
553 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200554
555 ecdsa_free( &ecdsa );
556 }
557 }
558#endif
559
560#if defined(POLARSSL_ECDH_C)
561 if( todo.ecdh )
562 {
563 ecdh_context ecdh;
564 const ecp_curve_info *curve_info;
565 size_t olen;
566
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200567 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200568 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
569 curve_info++ )
570 {
571 ecdh_init( &ecdh );
572
573 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
574 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
575 myrand, NULL ) != 0 ||
576 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
577 {
578 exit( 1 );
579 }
580
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200581 snprintf( title, sizeof( title ), "ECDHE-%s",
582 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200583 TIME_PUBLIC( title, "handshake",
584 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
585 myrand, NULL );
586 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
587 myrand, NULL ) );
588
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200589 snprintf( title, sizeof( title ), "ECDH-%s",
590 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200591 TIME_PUBLIC( title, "handshake",
592 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
593 myrand, NULL ) );
594 ecdh_free( &ecdh );
595 }
596 }
597#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000598 printf( "\n" );
599
Paul Bakkercce9d772011-11-18 14:26:47 +0000600#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 printf( " Press Enter to exit this program.\n" );
602 fflush( stdout ); getchar();
603#endif
604
605 return( 0 );
606}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200607
Paul Bakker5690efc2011-05-26 13:16:06 +0000608#endif /* POLARSSL_TIMING_C */