blob: b978f01ccc3bc5dda131c82aa6c5e14fcf83eec3 [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é-Gonnard58d78a82014-05-07 12:03:02 +020050#include "polarssl/ccm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020051#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020052#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010053#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000054#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010055#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020056#include "polarssl/ecdsa.h"
57#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010058#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020060#if defined _MSC_VER && !defined snprintf
61#define snprintf _snprintf
62#endif
63
Paul Bakker02faf452011-11-29 11:23:58 +000064#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010065#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010066#define TITLE_LEN 25
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020068#if !defined(POLARSSL_TIMING_C)
69int main( int argc, char *argv[] )
70{
71 ((void) argc);
72 ((void) argv);
73
74 printf("POLARSSL_TIMING_C not defined.\n");
75 return( 0 );
76}
77#else
78
Paul Bakkera3d195c2011-11-27 21:07:34 +000079static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Paul Bakkera3d195c2011-11-27 21:07:34 +000081 size_t use_len;
82 int rnd;
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084 if( rng_state != NULL )
85 rng_state = NULL;
86
Paul Bakkera3d195c2011-11-27 21:07:34 +000087 while( len > 0 )
88 {
89 use_len = len;
90 if( use_len > sizeof(int) )
91 use_len = sizeof(int);
92
93 rnd = rand();
94 memcpy( output, &rnd, use_len );
95 output += use_len;
96 len -= use_len;
97 }
98
99 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100}
101
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200102#define TIME_AND_TSC( TITLE, CODE ) \
103do { \
104 unsigned long i, j, tsc; \
105 \
106 printf( HEADER_FORMAT, TITLE ); \
107 fflush( stdout ); \
108 \
109 set_alarm( 1 ); \
110 for( i = 1; ! alarmed; i++ ) \
111 { \
112 CODE; \
113 } \
114 \
115 tsc = hardclock(); \
116 for( j = 0; j < 1024; j++ ) \
117 { \
118 CODE; \
119 } \
120 \
121 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
122 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
123} while( 0 )
124
Paul Bakker0c5e4292014-05-22 14:11:13 +0200125#if defined(POLARSSL_ERROR_C)
126#define PRINT_ERROR \
127 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
128 printf( "FAILED: %s\n", tmp );
129#else
130#define PRINT_ERROR \
131 printf( "FAILED: -0x%04x\n", -ret );
132#endif
133
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200134#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
135do { \
136 unsigned long i; \
137 int ret; \
138 \
139 printf( HEADER_FORMAT, TITLE ); \
140 fflush( stdout ); \
141 set_alarm( 3 ); \
142 \
143 ret = 0; \
144 for( i = 1; ! alarmed && ! ret ; i++ ) \
145 { \
146 CODE; \
147 } \
148 \
149 if( ret != 0 ) \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100150 { \
Paul Bakker0c5e4292014-05-22 14:11:13 +0200151PRINT_ERROR; \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100152 } \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200153 else \
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100154 printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200155} while( 0 )
156
Paul Bakker5121ce52009-01-03 21:22:43 +0000157unsigned char buf[BUFSIZE];
158
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200159typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100160 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200161 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100162 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200163 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200164} todo_list;
165
166#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100167 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200168 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100169 "havege, ctr_drbg, hmac_drbg\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200170 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200171
Paul Bakkercce9d772011-11-18 14:26:47 +0000172int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000173{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200174 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200175 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200176 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200177 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000178
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200179 if( argc == 1 )
180 memset( &todo, 1, sizeof( todo ) );
181 else
182 {
183 memset( &todo, 0, sizeof( todo ) );
184
185 for( i = 1; i < argc; i++ )
186 {
187 if( strcmp( argv[i], "md4" ) == 0 )
188 todo.md4 = 1;
189 else if( strcmp( argv[i], "md5" ) == 0 )
190 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100191 else if( strcmp( argv[i], "ripemd160" ) == 0 )
192 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200193 else if( strcmp( argv[i], "sha1" ) == 0 )
194 todo.sha1 = 1;
195 else if( strcmp( argv[i], "sha256" ) == 0 )
196 todo.sha256 = 1;
197 else if( strcmp( argv[i], "sha512" ) == 0 )
198 todo.sha512 = 1;
199 else if( strcmp( argv[i], "arc4" ) == 0 )
200 todo.arc4 = 1;
201 else if( strcmp( argv[i], "des3" ) == 0 )
202 todo.des3 = 1;
203 else if( strcmp( argv[i], "des" ) == 0 )
204 todo.des = 1;
205 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
206 todo.aes_cbc = 1;
207 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
208 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200209 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
210 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200211 else if( strcmp( argv[i], "camellia" ) == 0 )
212 todo.camellia = 1;
213 else if( strcmp( argv[i], "blowfish" ) == 0 )
214 todo.blowfish = 1;
215 else if( strcmp( argv[i], "havege" ) == 0 )
216 todo.havege = 1;
217 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
218 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100219 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
220 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200221 else if( strcmp( argv[i], "rsa" ) == 0 )
222 todo.rsa = 1;
223 else if( strcmp( argv[i], "dhm" ) == 0 )
224 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200225 else if( strcmp( argv[i], "ecdsa" ) == 0 )
226 todo.ecdsa = 1;
227 else if( strcmp( argv[i], "ecdh" ) == 0 )
228 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200229 else
230 {
231 printf( "Unrecognized option: %s\n", argv[i] );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200232 printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200233 }
234 }
235 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 printf( "\n" );
238
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200239 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200240 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200241
Paul Bakker40e46942009-01-03 21:51:57 +0000242#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200243 if( todo.md4 )
244 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245#endif
246
Paul Bakker40e46942009-01-03 21:51:57 +0000247#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200248 if( todo.md5 )
249 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250#endif
251
Paul Bakker61b699e2014-01-22 13:35:29 +0100252#if defined(POLARSSL_RIPEMD160_C)
253 if( todo.ripemd160 )
254 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100255#endif
256
Paul Bakker40e46942009-01-03 21:51:57 +0000257#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200258 if( todo.sha1 )
259 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260#endif
261
Paul Bakker9e36f042013-06-30 14:34:05 +0200262#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200263 if( todo.sha256 )
264 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265#endif
266
Paul Bakker9e36f042013-06-30 14:34:05 +0200267#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200268 if( todo.sha512 )
269 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000270#endif
271
Paul Bakker40e46942009-01-03 21:51:57 +0000272#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200273 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200274 {
275 arc4_context arc4;
276 arc4_setup( &arc4, tmp, 32 );
277 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
278 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000279#endif
280
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200281#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200282 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200283 {
284 des3_context des3;
285 des3_set3key_enc( &des3, tmp );
286 TIME_AND_TSC( "3DES",
287 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
288 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200290 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200291 {
292 des_context des;
293 des_setkey_enc( &des, tmp );
294 TIME_AND_TSC( "DES",
295 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
296 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000297#endif
298
Paul Bakker40e46942009-01-03 21:51:57 +0000299#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200300#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200301 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200303 aes_context aes;
304 for( keysize = 128; keysize <= 256; keysize += 64 )
305 {
306 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200308 memset( buf, 0, sizeof( buf ) );
309 memset( tmp, 0, sizeof( tmp ) );
310 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200312 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200313 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200314 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200316#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000317#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200318 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000319 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200320 gcm_context gcm;
321 for( keysize = 128; keysize <= 256; keysize += 64 )
322 {
323 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000324
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200325 memset( buf, 0, sizeof( buf ) );
326 memset( tmp, 0, sizeof( tmp ) );
327 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000328
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200329 TIME_AND_TSC( title,
330 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
331 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100332
333 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200334 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000335 }
336#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200337#if defined(POLARSSL_CCM_C)
338 if( todo.aes_ccm )
339 {
340 ccm_context ccm;
341 for( keysize = 128; keysize <= 256; keysize += 64 )
342 {
343 snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
344
345 memset( buf, 0, sizeof( buf ) );
346 memset( tmp, 0, sizeof( tmp ) );
347 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
348
349 TIME_AND_TSC( title,
350 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
351 12, NULL, 0, buf, buf, tmp, 16 ) );
352
353 ccm_free( &ccm );
354 }
355 }
356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000357#endif
358
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200359#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200360 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000361 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200362 camellia_context camellia;
363 for( keysize = 128; keysize <= 256; keysize += 64 )
364 {
365 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000366
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200367 memset( buf, 0, sizeof( buf ) );
368 memset( tmp, 0, sizeof( tmp ) );
369 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000370
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200371 TIME_AND_TSC( title,
372 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
373 BUFSIZE, tmp, buf, buf ) );
374 }
Paul Bakker38119b12009-01-10 23:31:23 +0000375 }
376#endif
377
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200378#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200379 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000380 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200381 blowfish_context blowfish;
382 for( keysize = 128; keysize <= 256; keysize += 64 )
383 {
384 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000385
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200386 memset( buf, 0, sizeof( buf ) );
387 memset( tmp, 0, sizeof( tmp ) );
388 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000389
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200390 TIME_AND_TSC( title,
391 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
392 tmp, buf, buf ) );
393 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000394 }
395#endif
396
Paul Bakker02faf452011-11-29 11:23:58 +0000397#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200398 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200399 {
400 havege_state hs;
401 havege_init( &hs );
402 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
403 }
Paul Bakker02faf452011-11-29 11:23:58 +0000404#endif
405
406#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200407 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200408 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200409 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000410
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200411 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000412 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200413 TIME_AND_TSC( "CTR_DRBG (NOPR)",
414 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
415 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000416
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200417 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000418 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
420 TIME_AND_TSC( "CTR_DRBG (PR)",
421 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
422 exit(1) );
423 }
Paul Bakker02faf452011-11-29 11:23:58 +0000424#endif
425
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100426#if defined(POLARSSL_HMAC_DRBG_C)
427 if( todo.hmac_drbg )
428 {
429 hmac_drbg_context hmac_drbg;
430 const md_info_t *md_info;
431
432#if defined(POLARSSL_SHA1_C)
433 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
434 exit(1);
435
436 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
437 exit(1);
438 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
439 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
440 exit(1) );
441 hmac_drbg_free( &hmac_drbg );
442
443 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
444 exit(1);
445 hmac_drbg_set_prediction_resistance( &hmac_drbg,
446 POLARSSL_HMAC_DRBG_PR_ON );
447 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
448 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
449 exit(1) );
450 hmac_drbg_free( &hmac_drbg );
451#endif
452
453#if defined(POLARSSL_SHA256_C)
454 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
455 exit(1);
456
457 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
458 exit(1);
459 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
460 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
461 exit(1) );
462 hmac_drbg_free( &hmac_drbg );
463
464 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
465 exit(1);
466 hmac_drbg_set_prediction_resistance( &hmac_drbg,
467 POLARSSL_HMAC_DRBG_PR_ON );
468 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
469 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
470 exit(1) );
471 hmac_drbg_free( &hmac_drbg );
472#endif
473 }
474#endif
475
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200476#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200477 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200479 rsa_context rsa;
480 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
481 {
482 snprintf( title, sizeof( title ), "RSA-%d", keysize );
483
484 rsa_init( &rsa, RSA_PKCS_V15, 0 );
485 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
486
487 TIME_PUBLIC( title, " public",
488 buf[0] = 0;
489 ret = rsa_public( &rsa, buf, buf ) );
490
491 TIME_PUBLIC( title, "private",
492 buf[0] = 0;
493 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
494
495 rsa_free( &rsa );
496 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000498#endif
499
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100500#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200501 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100502 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200504 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200505 const char *dhm_P[DHM_SIZES] = {
506 POLARSSL_DHM_RFC5114_MODP_1024_P,
507 POLARSSL_DHM_RFC3526_MODP_2048_P,
508 POLARSSL_DHM_RFC3526_MODP_3072_P,
509 };
510 const char *dhm_G[DHM_SIZES] = {
511 POLARSSL_DHM_RFC5114_MODP_1024_G,
512 POLARSSL_DHM_RFC3526_MODP_2048_G,
513 POLARSSL_DHM_RFC3526_MODP_3072_G,
514 };
515
516 dhm_context dhm;
517 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200518 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200519 {
520 memset( &dhm, 0, sizeof( dhm_context ) );
521
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200522 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
523 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
524 {
525 exit( 1 );
526 }
527
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200528 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100529 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200530 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
531 exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200532
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200533 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200534 TIME_PUBLIC( title, "handshake",
535 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100536 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200537 myrand, NULL );
538 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
539
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200540 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200541 TIME_PUBLIC( title, "handshake",
542 olen = sizeof( buf );
543 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
544
545 dhm_free( &dhm );
546 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100547 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100548#endif
549
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200550#if defined(POLARSSL_ECDSA_C)
551 if( todo.ecdsa )
552 {
553 ecdsa_context ecdsa;
554 const ecp_curve_info *curve_info;
555 size_t sig_len;
556
557 memset( buf, 0x2A, sizeof( buf ) );
558
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200559 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200560 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
561 curve_info++ )
562 {
563 ecdsa_init( &ecdsa );
564
565 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
566 exit( 1 );
567
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200568 snprintf( title, sizeof( title ), "ECDSA-%s",
569 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200570 TIME_PUBLIC( title, "sign",
571 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200572 tmp, &sig_len, myrand, NULL ) );
573
574 TIME_PUBLIC( title, "verify",
575 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
576 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200577
578 ecdsa_free( &ecdsa );
579 }
580 }
581#endif
582
583#if defined(POLARSSL_ECDH_C)
584 if( todo.ecdh )
585 {
586 ecdh_context ecdh;
587 const ecp_curve_info *curve_info;
588 size_t olen;
589
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200590 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200591 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
592 curve_info++ )
593 {
594 ecdh_init( &ecdh );
595
596 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
597 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
598 myrand, NULL ) != 0 ||
599 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
600 {
601 exit( 1 );
602 }
603
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200604 snprintf( title, sizeof( title ), "ECDHE-%s",
605 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200606 TIME_PUBLIC( title, "handshake",
607 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
608 myrand, NULL );
609 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
610 myrand, NULL ) );
611
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200612 snprintf( title, sizeof( title ), "ECDH-%s",
613 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200614 TIME_PUBLIC( title, "handshake",
615 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
616 myrand, NULL ) );
617 ecdh_free( &ecdh );
618 }
619 }
620#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000621 printf( "\n" );
622
Paul Bakkercce9d772011-11-18 14:26:47 +0000623#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 printf( " Press Enter to exit this program.\n" );
625 fflush( stdout ); getchar();
626#endif
627
628 return( 0 );
629}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200630
Paul Bakker5690efc2011-05-26 13:16:06 +0000631#endif /* POLARSSL_TIMING_C */