blob: 239d9685064d456bf8abfa0ef1eac3e12cf95a38 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020033#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/md4.h"
36#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010037#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020039#include "polarssl/sha256.h"
40#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/arc4.h"
42#include "polarssl/des.h"
43#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000044#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000045#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000046#include "polarssl/gcm.h"
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +020047#include "polarssl/ccm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020048#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020049#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010050#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000051#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010052#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020053#include "polarssl/ecdsa.h"
54#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010055#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020057#if defined _MSC_VER && !defined snprintf
58#define snprintf _snprintf
59#endif
60
Paul Bakker02faf452011-11-29 11:23:58 +000061#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010062#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010063#define TITLE_LEN 25
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020065#if !defined(POLARSSL_TIMING_C)
66int main( int argc, char *argv[] )
67{
68 ((void) argc);
69 ((void) argv);
70
71 printf("POLARSSL_TIMING_C not defined.\n");
72 return( 0 );
73}
74#else
75
Paul Bakkera3d195c2011-11-27 21:07:34 +000076static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000077{
Paul Bakkera3d195c2011-11-27 21:07:34 +000078 size_t use_len;
79 int rnd;
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081 if( rng_state != NULL )
82 rng_state = NULL;
83
Paul Bakkera3d195c2011-11-27 21:07:34 +000084 while( len > 0 )
85 {
86 use_len = len;
87 if( use_len > sizeof(int) )
88 use_len = sizeof(int);
89
90 rnd = rand();
91 memcpy( output, &rnd, use_len );
92 output += use_len;
93 len -= use_len;
94 }
95
96 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000097}
98
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020099#define TIME_AND_TSC( TITLE, CODE ) \
100do { \
101 unsigned long i, j, tsc; \
102 \
103 printf( HEADER_FORMAT, TITLE ); \
104 fflush( stdout ); \
105 \
106 set_alarm( 1 ); \
107 for( i = 1; ! alarmed; i++ ) \
108 { \
109 CODE; \
110 } \
111 \
112 tsc = hardclock(); \
113 for( j = 0; j < 1024; j++ ) \
114 { \
115 CODE; \
116 } \
117 \
118 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
119 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
120} while( 0 )
121
Paul Bakker0c5e4292014-05-22 14:11:13 +0200122#if defined(POLARSSL_ERROR_C)
123#define PRINT_ERROR \
124 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
125 printf( "FAILED: %s\n", tmp );
126#else
127#define PRINT_ERROR \
128 printf( "FAILED: -0x%04x\n", -ret );
129#endif
130
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200131#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
132do { \
133 unsigned long i; \
134 int ret; \
135 \
136 printf( HEADER_FORMAT, TITLE ); \
137 fflush( stdout ); \
138 set_alarm( 3 ); \
139 \
140 ret = 0; \
141 for( i = 1; ! alarmed && ! ret ; i++ ) \
142 { \
143 CODE; \
144 } \
145 \
146 if( ret != 0 ) \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100147 { \
Paul Bakker0c5e4292014-05-22 14:11:13 +0200148PRINT_ERROR; \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100149 } \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200150 else \
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100151 printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200152} while( 0 )
153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154unsigned char buf[BUFSIZE];
155
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200156typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100157 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200158 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100159 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200160 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200161} todo_list;
162
163#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100164 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200165 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100166 "havege, ctr_drbg, hmac_drbg\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200167 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000170{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200171 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200172 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200173 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200174 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000175
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200176 if( argc == 1 )
177 memset( &todo, 1, sizeof( todo ) );
178 else
179 {
180 memset( &todo, 0, sizeof( todo ) );
181
182 for( i = 1; i < argc; i++ )
183 {
184 if( strcmp( argv[i], "md4" ) == 0 )
185 todo.md4 = 1;
186 else if( strcmp( argv[i], "md5" ) == 0 )
187 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100188 else if( strcmp( argv[i], "ripemd160" ) == 0 )
189 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200190 else if( strcmp( argv[i], "sha1" ) == 0 )
191 todo.sha1 = 1;
192 else if( strcmp( argv[i], "sha256" ) == 0 )
193 todo.sha256 = 1;
194 else if( strcmp( argv[i], "sha512" ) == 0 )
195 todo.sha512 = 1;
196 else if( strcmp( argv[i], "arc4" ) == 0 )
197 todo.arc4 = 1;
198 else if( strcmp( argv[i], "des3" ) == 0 )
199 todo.des3 = 1;
200 else if( strcmp( argv[i], "des" ) == 0 )
201 todo.des = 1;
202 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
203 todo.aes_cbc = 1;
204 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
205 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200206 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
207 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200208 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] );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200229 printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200230 }
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;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200273 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200274 arc4_setup( &arc4, tmp, 32 );
275 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200276 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200277 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000278#endif
279
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200280#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200281 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200282 {
283 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200284 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200285 des3_set3key_enc( &des3, tmp );
286 TIME_AND_TSC( "3DES",
287 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200288 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200289 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200291 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200292 {
293 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200294 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200295 des_setkey_enc( &des, tmp );
296 TIME_AND_TSC( "DES",
297 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200298 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200299 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000300#endif
301
Paul Bakker40e46942009-01-03 21:51:57 +0000302#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200303#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200304 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200306 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200307 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200308 for( keysize = 128; keysize <= 256; keysize += 64 )
309 {
310 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200312 memset( buf, 0, sizeof( buf ) );
313 memset( tmp, 0, sizeof( tmp ) );
314 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200316 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200317 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200318 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200319 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200321#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000322#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200323 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000324 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200325 gcm_context gcm;
326 for( keysize = 128; keysize <= 256; keysize += 64 )
327 {
328 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000329
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200330 memset( buf, 0, sizeof( buf ) );
331 memset( tmp, 0, sizeof( tmp ) );
332 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000333
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200334 TIME_AND_TSC( title,
335 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
336 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100337
338 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200339 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000340 }
341#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200342#if defined(POLARSSL_CCM_C)
343 if( todo.aes_ccm )
344 {
345 ccm_context ccm;
346 for( keysize = 128; keysize <= 256; keysize += 64 )
347 {
348 snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
349
350 memset( buf, 0, sizeof( buf ) );
351 memset( tmp, 0, sizeof( tmp ) );
352 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
353
354 TIME_AND_TSC( title,
355 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
356 12, NULL, 0, buf, buf, tmp, 16 ) );
357
358 ccm_free( &ccm );
359 }
360 }
361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000362#endif
363
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200364#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200365 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000366 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200367 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200368 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200369 for( keysize = 128; keysize <= 256; keysize += 64 )
370 {
371 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000372
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200373 memset( buf, 0, sizeof( buf ) );
374 memset( tmp, 0, sizeof( tmp ) );
375 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000376
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200377 TIME_AND_TSC( title,
378 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
379 BUFSIZE, tmp, buf, buf ) );
380 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200381 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000382 }
383#endif
384
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200385#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200386 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000387 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200388 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200389 blowfish_init( &blowfish );
390
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200391 for( keysize = 128; keysize <= 256; keysize += 64 )
392 {
393 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000394
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200395 memset( buf, 0, sizeof( buf ) );
396 memset( tmp, 0, sizeof( tmp ) );
397 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000398
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200399 TIME_AND_TSC( title,
400 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
401 tmp, buf, buf ) );
402 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200403
404 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000405 }
406#endif
407
Paul Bakker02faf452011-11-29 11:23:58 +0000408#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200409 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200410 {
411 havege_state hs;
412 havege_init( &hs );
413 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200414 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 }
Paul Bakker02faf452011-11-29 11:23:58 +0000416#endif
417
418#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200419 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200421 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000422
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000424 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 TIME_AND_TSC( "CTR_DRBG (NOPR)",
426 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
427 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000428
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200429 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000430 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
432 TIME_AND_TSC( "CTR_DRBG (PR)",
433 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
434 exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200435 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200436 }
Paul Bakker02faf452011-11-29 11:23:58 +0000437#endif
438
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100439#if defined(POLARSSL_HMAC_DRBG_C)
440 if( todo.hmac_drbg )
441 {
442 hmac_drbg_context hmac_drbg;
443 const md_info_t *md_info;
444
445#if defined(POLARSSL_SHA1_C)
446 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
447 exit(1);
448
449 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
450 exit(1);
451 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
452 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
453 exit(1) );
454 hmac_drbg_free( &hmac_drbg );
455
456 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
457 exit(1);
458 hmac_drbg_set_prediction_resistance( &hmac_drbg,
459 POLARSSL_HMAC_DRBG_PR_ON );
460 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
461 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
462 exit(1) );
463 hmac_drbg_free( &hmac_drbg );
464#endif
465
466#if defined(POLARSSL_SHA256_C)
467 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
468 exit(1);
469
470 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
471 exit(1);
472 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
473 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
474 exit(1) );
475 hmac_drbg_free( &hmac_drbg );
476
477 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
478 exit(1);
479 hmac_drbg_set_prediction_resistance( &hmac_drbg,
480 POLARSSL_HMAC_DRBG_PR_ON );
481 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
482 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
483 exit(1) );
484 hmac_drbg_free( &hmac_drbg );
485#endif
486 }
487#endif
488
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200489#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200490 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200492 rsa_context rsa;
493 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
494 {
495 snprintf( title, sizeof( title ), "RSA-%d", keysize );
496
497 rsa_init( &rsa, RSA_PKCS_V15, 0 );
498 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
499
500 TIME_PUBLIC( title, " public",
501 buf[0] = 0;
502 ret = rsa_public( &rsa, buf, buf ) );
503
504 TIME_PUBLIC( title, "private",
505 buf[0] = 0;
506 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
507
508 rsa_free( &rsa );
509 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000511#endif
512
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100513#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200514 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100515 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200516#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200517 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200518 const char *dhm_P[DHM_SIZES] = {
519 POLARSSL_DHM_RFC5114_MODP_1024_P,
520 POLARSSL_DHM_RFC3526_MODP_2048_P,
521 POLARSSL_DHM_RFC3526_MODP_3072_P,
522 };
523 const char *dhm_G[DHM_SIZES] = {
524 POLARSSL_DHM_RFC5114_MODP_1024_G,
525 POLARSSL_DHM_RFC3526_MODP_2048_G,
526 POLARSSL_DHM_RFC3526_MODP_3072_G,
527 };
528
529 dhm_context dhm;
530 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200531 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200532 {
Paul Bakkera317a982014-06-18 16:44:11 +0200533 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200534
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200535 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
536 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
537 {
538 exit( 1 );
539 }
540
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200541 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100542 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200543 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
544 exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200545
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200546 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200547 TIME_PUBLIC( title, "handshake",
548 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100549 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200550 myrand, NULL );
551 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
552
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200553 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200554 TIME_PUBLIC( title, "handshake",
555 olen = sizeof( buf );
556 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
557
558 dhm_free( &dhm );
559 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100560 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100561#endif
562
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200563#if defined(POLARSSL_ECDSA_C)
564 if( todo.ecdsa )
565 {
566 ecdsa_context ecdsa;
567 const ecp_curve_info *curve_info;
568 size_t sig_len;
569
570 memset( buf, 0x2A, sizeof( buf ) );
571
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200572 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200573 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
574 curve_info++ )
575 {
576 ecdsa_init( &ecdsa );
577
578 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
579 exit( 1 );
580
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200581 snprintf( title, sizeof( title ), "ECDSA-%s",
582 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200583 TIME_PUBLIC( title, "sign",
584 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200585 tmp, &sig_len, myrand, NULL ) );
586
587 TIME_PUBLIC( title, "verify",
588 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
589 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200590
591 ecdsa_free( &ecdsa );
592 }
593 }
594#endif
595
596#if defined(POLARSSL_ECDH_C)
597 if( todo.ecdh )
598 {
599 ecdh_context ecdh;
600 const ecp_curve_info *curve_info;
601 size_t olen;
602
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200603 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200604 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
605 curve_info++ )
606 {
607 ecdh_init( &ecdh );
608
609 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
610 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
611 myrand, NULL ) != 0 ||
612 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
613 {
614 exit( 1 );
615 }
616
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200617 snprintf( title, sizeof( title ), "ECDHE-%s",
618 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200619 TIME_PUBLIC( title, "handshake",
620 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
621 myrand, NULL );
622 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
623 myrand, NULL ) );
624
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200625 snprintf( title, sizeof( title ), "ECDH-%s",
626 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200627 TIME_PUBLIC( title, "handshake",
628 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
629 myrand, NULL ) );
630 ecdh_free( &ecdh );
631 }
632 }
633#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000634 printf( "\n" );
635
Paul Bakkercce9d772011-11-18 14:26:47 +0000636#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 printf( " Press Enter to exit this program.\n" );
638 fflush( stdout ); getchar();
639#endif
640
641 return( 0 );
642}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200643
Paul Bakker5690efc2011-05-26 13:16:06 +0000644#endif /* POLARSSL_TIMING_C */