blob: 2f359ad340b83ca3e9202fc80b0a6b703360f7c3 [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é-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020032#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/md4.h"
35#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010036#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020038#include "polarssl/sha256.h"
39#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/arc4.h"
41#include "polarssl/des.h"
42#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000043#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000044#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000045#include "polarssl/gcm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020046#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020047#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010048#include "polarssl/hmac_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"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020051#include "polarssl/ecdsa.h"
52#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010053#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020055#if defined _MSC_VER && !defined snprintf
56#define snprintf _snprintf
57#endif
58
Paul Bakker02faf452011-11-29 11:23:58 +000059#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010060#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010061#define TITLE_LEN 25
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020063#if !defined(POLARSSL_TIMING_C)
64int main( int argc, char *argv[] )
65{
66 ((void) argc);
67 ((void) argv);
68
69 printf("POLARSSL_TIMING_C not defined.\n");
70 return( 0 );
71}
72#else
73
Paul Bakkera3d195c2011-11-27 21:07:34 +000074static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000075{
Paul Bakkera3d195c2011-11-27 21:07:34 +000076 size_t use_len;
77 int rnd;
78
Paul Bakker5121ce52009-01-03 21:22:43 +000079 if( rng_state != NULL )
80 rng_state = NULL;
81
Paul Bakkera3d195c2011-11-27 21:07:34 +000082 while( len > 0 )
83 {
84 use_len = len;
85 if( use_len > sizeof(int) )
86 use_len = sizeof(int);
87
88 rnd = rand();
89 memcpy( output, &rnd, use_len );
90 output += use_len;
91 len -= use_len;
92 }
93
94 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000095}
96
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020097#define TIME_AND_TSC( TITLE, CODE ) \
98do { \
99 unsigned long i, j, tsc; \
100 \
101 printf( HEADER_FORMAT, TITLE ); \
102 fflush( stdout ); \
103 \
104 set_alarm( 1 ); \
105 for( i = 1; ! alarmed; i++ ) \
106 { \
107 CODE; \
108 } \
109 \
110 tsc = hardclock(); \
111 for( j = 0; j < 1024; j++ ) \
112 { \
113 CODE; \
114 } \
115 \
116 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
117 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
118} while( 0 )
119
120#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
121do { \
122 unsigned long i; \
123 int ret; \
124 \
125 printf( HEADER_FORMAT, TITLE ); \
126 fflush( stdout ); \
127 set_alarm( 3 ); \
128 \
129 ret = 0; \
130 for( i = 1; ! alarmed && ! ret ; i++ ) \
131 { \
132 CODE; \
133 } \
134 \
135 if( ret != 0 ) \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100136 { \
137 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
138 printf( "FAILED: %s\n", tmp ); \
139 } \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200140 else \
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100141 printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200142} while( 0 )
143
Paul Bakker5121ce52009-01-03 21:22:43 +0000144unsigned char buf[BUFSIZE];
145
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200146typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100147 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200148 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100149 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200150 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200151} todo_list;
152
153#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100154 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200155 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100156 "havege, ctr_drbg, hmac_drbg\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200157 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200158
Paul Bakkercce9d772011-11-18 14:26:47 +0000159int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000160{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200161 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200162 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200163 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200164 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000165
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200166 if( argc == 1 )
167 memset( &todo, 1, sizeof( todo ) );
168 else
169 {
170 memset( &todo, 0, sizeof( todo ) );
171
172 for( i = 1; i < argc; i++ )
173 {
174 if( strcmp( argv[i], "md4" ) == 0 )
175 todo.md4 = 1;
176 else if( strcmp( argv[i], "md5" ) == 0 )
177 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100178 else if( strcmp( argv[i], "ripemd160" ) == 0 )
179 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200180 else if( strcmp( argv[i], "sha1" ) == 0 )
181 todo.sha1 = 1;
182 else if( strcmp( argv[i], "sha256" ) == 0 )
183 todo.sha256 = 1;
184 else if( strcmp( argv[i], "sha512" ) == 0 )
185 todo.sha512 = 1;
186 else if( strcmp( argv[i], "arc4" ) == 0 )
187 todo.arc4 = 1;
188 else if( strcmp( argv[i], "des3" ) == 0 )
189 todo.des3 = 1;
190 else if( strcmp( argv[i], "des" ) == 0 )
191 todo.des = 1;
192 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
193 todo.aes_cbc = 1;
194 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
195 todo.aes_gcm = 1;
196 else if( strcmp( argv[i], "camellia" ) == 0 )
197 todo.camellia = 1;
198 else if( strcmp( argv[i], "blowfish" ) == 0 )
199 todo.blowfish = 1;
200 else if( strcmp( argv[i], "havege" ) == 0 )
201 todo.havege = 1;
202 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
203 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100204 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
205 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200206 else if( strcmp( argv[i], "rsa" ) == 0 )
207 todo.rsa = 1;
208 else if( strcmp( argv[i], "dhm" ) == 0 )
209 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200210 else if( strcmp( argv[i], "ecdsa" ) == 0 )
211 todo.ecdsa = 1;
212 else if( strcmp( argv[i], "ecdh" ) == 0 )
213 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200214 else
215 {
216 printf( "Unrecognized option: %s\n", argv[i] );
217 printf( "Available options:" OPTIONS );
218 }
219 }
220 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 printf( "\n" );
223
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200224 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200225 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200226
Paul Bakker40e46942009-01-03 21:51:57 +0000227#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200228 if( todo.md4 )
229 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230#endif
231
Paul Bakker40e46942009-01-03 21:51:57 +0000232#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200233 if( todo.md5 )
234 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235#endif
236
Paul Bakker61b699e2014-01-22 13:35:29 +0100237#if defined(POLARSSL_RIPEMD160_C)
238 if( todo.ripemd160 )
239 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100240#endif
241
Paul Bakker40e46942009-01-03 21:51:57 +0000242#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200243 if( todo.sha1 )
244 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245#endif
246
Paul Bakker9e36f042013-06-30 14:34:05 +0200247#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200248 if( todo.sha256 )
249 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250#endif
251
Paul Bakker9e36f042013-06-30 14:34:05 +0200252#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200253 if( todo.sha512 )
254 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000255#endif
256
Paul Bakker40e46942009-01-03 21:51:57 +0000257#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200258 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200259 {
260 arc4_context arc4;
261 arc4_setup( &arc4, tmp, 32 );
262 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
263 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000264#endif
265
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200266#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200267 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200268 {
269 des3_context des3;
270 des3_set3key_enc( &des3, tmp );
271 TIME_AND_TSC( "3DES",
272 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
273 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200275 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200276 {
277 des_context des;
278 des_setkey_enc( &des, tmp );
279 TIME_AND_TSC( "DES",
280 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
281 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000282#endif
283
Paul Bakker40e46942009-01-03 21:51:57 +0000284#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200285#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200286 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200288 aes_context aes;
289 for( keysize = 128; keysize <= 256; keysize += 64 )
290 {
291 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200293 memset( buf, 0, sizeof( buf ) );
294 memset( tmp, 0, sizeof( tmp ) );
295 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200297 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200298 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200299 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200301#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000302#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200303 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000304 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200305 gcm_context gcm;
306 for( keysize = 128; keysize <= 256; keysize += 64 )
307 {
308 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000309
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200310 memset( buf, 0, sizeof( buf ) );
311 memset( tmp, 0, sizeof( tmp ) );
312 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000313
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200314 TIME_AND_TSC( title,
315 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
316 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100317
318 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200319 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000320 }
321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000322#endif
323
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200324#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200325 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000326 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200327 camellia_context camellia;
328 for( keysize = 128; keysize <= 256; keysize += 64 )
329 {
330 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000331
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200332 memset( buf, 0, sizeof( buf ) );
333 memset( tmp, 0, sizeof( tmp ) );
334 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000335
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200336 TIME_AND_TSC( title,
337 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
338 BUFSIZE, tmp, buf, buf ) );
339 }
Paul Bakker38119b12009-01-10 23:31:23 +0000340 }
341#endif
342
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200343#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200344 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000345 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200346 blowfish_context blowfish;
347 for( keysize = 128; keysize <= 256; keysize += 64 )
348 {
349 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000350
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200351 memset( buf, 0, sizeof( buf ) );
352 memset( tmp, 0, sizeof( tmp ) );
353 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000354
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200355 TIME_AND_TSC( title,
356 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
357 tmp, buf, buf ) );
358 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000359 }
360#endif
361
Paul Bakker02faf452011-11-29 11:23:58 +0000362#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200363 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200364 {
365 havege_state hs;
366 havege_init( &hs );
367 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
368 }
Paul Bakker02faf452011-11-29 11:23:58 +0000369#endif
370
371#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200372 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200373 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000375
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000377 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200378 TIME_AND_TSC( "CTR_DRBG (NOPR)",
379 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
380 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000381
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200382 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000383 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200384 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
385 TIME_AND_TSC( "CTR_DRBG (PR)",
386 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
387 exit(1) );
388 }
Paul Bakker02faf452011-11-29 11:23:58 +0000389#endif
390
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100391#if defined(POLARSSL_HMAC_DRBG_C)
392 if( todo.hmac_drbg )
393 {
394 hmac_drbg_context hmac_drbg;
395 const md_info_t *md_info;
396
397#if defined(POLARSSL_SHA1_C)
398 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
399 exit(1);
400
401 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
402 exit(1);
403 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
404 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
405 exit(1) );
406 hmac_drbg_free( &hmac_drbg );
407
408 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
409 exit(1);
410 hmac_drbg_set_prediction_resistance( &hmac_drbg,
411 POLARSSL_HMAC_DRBG_PR_ON );
412 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
413 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
414 exit(1) );
415 hmac_drbg_free( &hmac_drbg );
416#endif
417
418#if defined(POLARSSL_SHA256_C)
419 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
420 exit(1);
421
422 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
423 exit(1);
424 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
425 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
426 exit(1) );
427 hmac_drbg_free( &hmac_drbg );
428
429 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
430 exit(1);
431 hmac_drbg_set_prediction_resistance( &hmac_drbg,
432 POLARSSL_HMAC_DRBG_PR_ON );
433 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
434 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
435 exit(1) );
436 hmac_drbg_free( &hmac_drbg );
437#endif
438 }
439#endif
440
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200441#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200442 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200444 rsa_context rsa;
445 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
446 {
447 snprintf( title, sizeof( title ), "RSA-%d", keysize );
448
449 rsa_init( &rsa, RSA_PKCS_V15, 0 );
450 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
451
452 TIME_PUBLIC( title, " public",
453 buf[0] = 0;
454 ret = rsa_public( &rsa, buf, buf ) );
455
456 TIME_PUBLIC( title, "private",
457 buf[0] = 0;
458 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
459
460 rsa_free( &rsa );
461 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000463#endif
464
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100465#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200466 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100467 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200468#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200469 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200470 const char *dhm_P[DHM_SIZES] = {
471 POLARSSL_DHM_RFC5114_MODP_1024_P,
472 POLARSSL_DHM_RFC3526_MODP_2048_P,
473 POLARSSL_DHM_RFC3526_MODP_3072_P,
474 };
475 const char *dhm_G[DHM_SIZES] = {
476 POLARSSL_DHM_RFC5114_MODP_1024_G,
477 POLARSSL_DHM_RFC3526_MODP_2048_G,
478 POLARSSL_DHM_RFC3526_MODP_3072_G,
479 };
480
481 dhm_context dhm;
482 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200483 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200484 {
485 memset( &dhm, 0, sizeof( dhm_context ) );
486
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200487 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
488 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
489 {
490 exit( 1 );
491 }
492
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200493 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100494 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200495 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
496 exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200497
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200498 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499 TIME_PUBLIC( title, "handshake",
500 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100501 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200502 myrand, NULL );
503 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
504
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200505 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200506 TIME_PUBLIC( title, "handshake",
507 olen = sizeof( buf );
508 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
509
510 dhm_free( &dhm );
511 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100512 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100513#endif
514
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200515#if defined(POLARSSL_ECDSA_C)
516 if( todo.ecdsa )
517 {
518 ecdsa_context ecdsa;
519 const ecp_curve_info *curve_info;
520 size_t sig_len;
521
522 memset( buf, 0x2A, sizeof( buf ) );
523
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200524 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200525 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
526 curve_info++ )
527 {
528 ecdsa_init( &ecdsa );
529
530 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
531 exit( 1 );
532
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200533 snprintf( title, sizeof( title ), "ECDSA-%s",
534 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200535 TIME_PUBLIC( title, "sign",
536 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200537 tmp, &sig_len, myrand, NULL ) );
538
539 TIME_PUBLIC( title, "verify",
540 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
541 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200542
543 ecdsa_free( &ecdsa );
544 }
545 }
546#endif
547
548#if defined(POLARSSL_ECDH_C)
549 if( todo.ecdh )
550 {
551 ecdh_context ecdh;
552 const ecp_curve_info *curve_info;
553 size_t olen;
554
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200555 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200556 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
557 curve_info++ )
558 {
559 ecdh_init( &ecdh );
560
561 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
562 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
563 myrand, NULL ) != 0 ||
564 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
565 {
566 exit( 1 );
567 }
568
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200569 snprintf( title, sizeof( title ), "ECDHE-%s",
570 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200571 TIME_PUBLIC( title, "handshake",
572 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
573 myrand, NULL );
574 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
575 myrand, NULL ) );
576
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200577 snprintf( title, sizeof( title ), "ECDH-%s",
578 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200579 TIME_PUBLIC( title, "handshake",
580 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
581 myrand, NULL ) );
582 ecdh_free( &ecdh );
583 }
584 }
585#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000586 printf( "\n" );
587
Paul Bakkercce9d772011-11-18 14:26:47 +0000588#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 printf( " Press Enter to exit this program.\n" );
590 fflush( stdout ); getchar();
591#endif
592
593 return( 0 );
594}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200595
Paul Bakker5690efc2011-05-26 13:16:06 +0000596#endif /* POLARSSL_TIMING_C */