blob: 675547b3b472523399ed27964179b478c01f1b9b [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evans77d36382015-01-30 12:12:11 +000033#define polarssl_exit exit
Rich Evansb92965b2015-01-30 11:11:57 +000034#define polarssl_printf printf
35#define polarssl_snprintf snprintf
Rich Evans012acfc2015-01-30 12:12:11 +000036#define polarssl_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000037#endif
38
Rich Evans18b78c72015-02-11 14:06:19 +000039#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020040#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/md4.h"
43#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010044#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020046#include "polarssl/sha256.h"
47#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000048#include "polarssl/arc4.h"
49#include "polarssl/des.h"
50#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000051#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000052#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000053#include "polarssl/gcm.h"
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +020054#include "polarssl/ccm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020055#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020056#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010057#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000058#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010059#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020060#include "polarssl/ecdsa.h"
61#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010062#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Rich Evans18b78c72015-02-11 14:06:19 +000064#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#endif
68
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020069#if defined _MSC_VER && !defined snprintf
70#define snprintf _snprintf
71#endif
72
Paul Bakker02faf452011-11-29 11:23:58 +000073#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010074#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010075#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000076
Rich Evans85b05ec2015-02-12 11:37:29 +000077#define DHM_SIZES 3
78
79#define OPTIONS \
80 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
81 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
82 "havege, ctr_drbg, hmac_drbg\n" \
83 "rsa, dhm, ecdsa, ecdh.\n"
84
85#if defined(POLARSSL_ERROR_C)
86#define PRINT_ERROR \
87 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
88 polarssl_printf( "FAILED: %s\n", tmp );
89#else
90#define PRINT_ERROR \
91 polarssl_printf( "FAILED: -0x%04x\n", -ret );
92#endif
93
94#define TIME_AND_TSC( TITLE, CODE ) \
95do { \
96 unsigned long i, j, tsc; \
97 \
98 polarssl_printf( HEADER_FORMAT, TITLE ); \
99 fflush( stdout ); \
100 \
101 set_alarm( 1 ); \
102 for( i = 1; ! alarmed; i++ ) \
103 { \
104 CODE; \
105 } \
106 \
107 tsc = hardclock(); \
108 for( j = 0; j < 1024; j++ ) \
109 { \
110 CODE; \
111 } \
112 \
113 polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
114 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
115} while( 0 )
116
117#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
118do { \
119 unsigned long i; \
120 int ret; \
121 \
122 polarssl_printf( HEADER_FORMAT, TITLE ); \
123 fflush( stdout ); \
124 set_alarm( 3 ); \
125 \
126 ret = 0; \
127 for( i = 1; ! alarmed && ! ret ; i++ ) \
128 { \
129 CODE; \
130 } \
131 \
132 if( ret != 0 ) \
133 { \
134PRINT_ERROR; \
135 } \
136 else \
137 polarssl_printf( "%9lu " TYPE "/s\n", i / 3 ); \
138} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200140#if !defined(POLARSSL_TIMING_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000141int main( void )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200142{
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf("POLARSSL_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200144 return( 0 );
145}
146#else
Paul Bakkera3d195c2011-11-27 21:07:34 +0000147static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000149 size_t use_len;
150 int rnd;
151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 if( rng_state != NULL )
153 rng_state = NULL;
154
Paul Bakkera3d195c2011-11-27 21:07:34 +0000155 while( len > 0 )
156 {
157 use_len = len;
158 if( use_len > sizeof(int) )
159 use_len = sizeof(int);
160
161 rnd = rand();
162 memcpy( output, &rnd, use_len );
163 output += use_len;
164 len -= use_len;
165 }
166
167 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168}
169
170unsigned char buf[BUFSIZE];
171
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200172typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100173 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200174 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100175 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200176 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200177} todo_list;
178
Paul Bakkercce9d772011-11-18 14:26:47 +0000179int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000180{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200181 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200182 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200183 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200184 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000185
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200186 if( argc == 1 )
187 memset( &todo, 1, sizeof( todo ) );
188 else
189 {
190 memset( &todo, 0, sizeof( todo ) );
191
192 for( i = 1; i < argc; i++ )
193 {
194 if( strcmp( argv[i], "md4" ) == 0 )
195 todo.md4 = 1;
196 else if( strcmp( argv[i], "md5" ) == 0 )
197 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100198 else if( strcmp( argv[i], "ripemd160" ) == 0 )
199 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200200 else if( strcmp( argv[i], "sha1" ) == 0 )
201 todo.sha1 = 1;
202 else if( strcmp( argv[i], "sha256" ) == 0 )
203 todo.sha256 = 1;
204 else if( strcmp( argv[i], "sha512" ) == 0 )
205 todo.sha512 = 1;
206 else if( strcmp( argv[i], "arc4" ) == 0 )
207 todo.arc4 = 1;
208 else if( strcmp( argv[i], "des3" ) == 0 )
209 todo.des3 = 1;
210 else if( strcmp( argv[i], "des" ) == 0 )
211 todo.des = 1;
212 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
213 todo.aes_cbc = 1;
214 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
215 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200216 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
217 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200218 else if( strcmp( argv[i], "camellia" ) == 0 )
219 todo.camellia = 1;
220 else if( strcmp( argv[i], "blowfish" ) == 0 )
221 todo.blowfish = 1;
222 else if( strcmp( argv[i], "havege" ) == 0 )
223 todo.havege = 1;
224 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
225 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100226 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
227 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200228 else if( strcmp( argv[i], "rsa" ) == 0 )
229 todo.rsa = 1;
230 else if( strcmp( argv[i], "dhm" ) == 0 )
231 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200232 else if( strcmp( argv[i], "ecdsa" ) == 0 )
233 todo.ecdsa = 1;
234 else if( strcmp( argv[i], "ecdh" ) == 0 )
235 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200236 else
237 {
Rich Evansf90016a2015-01-19 14:26:37 +0000238 polarssl_printf( "Unrecognized option: %s\n", argv[i] );
239 polarssl_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200240 }
241 }
242 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Rich Evansf90016a2015-01-19 14:26:37 +0000244 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200246 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200247 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200248
Paul Bakker40e46942009-01-03 21:51:57 +0000249#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200250 if( todo.md4 )
251 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252#endif
253
Paul Bakker40e46942009-01-03 21:51:57 +0000254#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255 if( todo.md5 )
256 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257#endif
258
Paul Bakker61b699e2014-01-22 13:35:29 +0100259#if defined(POLARSSL_RIPEMD160_C)
260 if( todo.ripemd160 )
261 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100262#endif
263
Paul Bakker40e46942009-01-03 21:51:57 +0000264#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200265 if( todo.sha1 )
266 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267#endif
268
Paul Bakker9e36f042013-06-30 14:34:05 +0200269#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270 if( todo.sha256 )
271 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272#endif
273
Paul Bakker9e36f042013-06-30 14:34:05 +0200274#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200275 if( todo.sha512 )
276 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000277#endif
278
Paul Bakker40e46942009-01-03 21:51:57 +0000279#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200280 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200281 {
282 arc4_context arc4;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200283 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200284 arc4_setup( &arc4, tmp, 32 );
285 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200286 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200287 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000288#endif
289
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200290#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200291 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200292 {
293 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200294 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200295 des3_set3key_enc( &des3, tmp );
296 TIME_AND_TSC( "3DES",
297 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200298 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200299 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200301 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200302 {
303 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200304 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200305 des_setkey_enc( &des, tmp );
306 TIME_AND_TSC( "DES",
307 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200308 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200309 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000310#endif
311
Paul Bakker40e46942009-01-03 21:51:57 +0000312#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200313#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200314 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200316 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200317 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200318 for( keysize = 128; keysize <= 256; keysize += 64 )
319 {
Rich Evans783d9d12015-01-30 11:11:57 +0000320 polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200322 memset( buf, 0, sizeof( buf ) );
323 memset( tmp, 0, sizeof( tmp ) );
324 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200326 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200328 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200329 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200331#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000332#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200333 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000334 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200335 gcm_context gcm;
336 for( keysize = 128; keysize <= 256; keysize += 64 )
337 {
Rich Evans783d9d12015-01-30 11:11:57 +0000338 polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000339
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200340 memset( buf, 0, sizeof( buf ) );
341 memset( tmp, 0, sizeof( tmp ) );
342 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000343
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200344 TIME_AND_TSC( title,
345 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
346 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100347
348 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200349 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000350 }
351#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200352#if defined(POLARSSL_CCM_C)
353 if( todo.aes_ccm )
354 {
355 ccm_context ccm;
356 for( keysize = 128; keysize <= 256; keysize += 64 )
357 {
Rich Evans783d9d12015-01-30 11:11:57 +0000358 polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200359
360 memset( buf, 0, sizeof( buf ) );
361 memset( tmp, 0, sizeof( tmp ) );
362 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
363
364 TIME_AND_TSC( title,
365 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
366 12, NULL, 0, buf, buf, tmp, 16 ) );
367
368 ccm_free( &ccm );
369 }
370 }
371#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000372#endif
373
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200374#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200375 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000376 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200377 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200378 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200379 for( keysize = 128; keysize <= 256; keysize += 64 )
380 {
Rich Evans783d9d12015-01-30 11:11:57 +0000381 polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000382
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200383 memset( buf, 0, sizeof( buf ) );
384 memset( tmp, 0, sizeof( tmp ) );
385 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000386
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200387 TIME_AND_TSC( title,
388 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
389 BUFSIZE, tmp, buf, buf ) );
390 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200391 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000392 }
393#endif
394
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200395#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200396 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000397 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200398 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200399 blowfish_init( &blowfish );
400
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200401 for( keysize = 128; keysize <= 256; keysize += 64 )
402 {
Rich Evans783d9d12015-01-30 11:11:57 +0000403 polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000404
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200405 memset( buf, 0, sizeof( buf ) );
406 memset( tmp, 0, sizeof( tmp ) );
407 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000408
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 TIME_AND_TSC( title,
410 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
411 tmp, buf, buf ) );
412 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200413
414 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000415 }
416#endif
417
Paul Bakker02faf452011-11-29 11:23:58 +0000418#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200419 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 {
421 havege_state hs;
422 havege_init( &hs );
423 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200424 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 }
Paul Bakker02faf452011-11-29 11:23:58 +0000426#endif
427
428#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200429 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200430 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200431 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000432
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200433 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000434 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200435 TIME_AND_TSC( "CTR_DRBG (NOPR)",
436 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000437 polarssl_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000438
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200439 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000440 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200441 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
442 TIME_AND_TSC( "CTR_DRBG (PR)",
443 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000444 polarssl_exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200445 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200446 }
Paul Bakker02faf452011-11-29 11:23:58 +0000447#endif
448
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100449#if defined(POLARSSL_HMAC_DRBG_C)
450 if( todo.hmac_drbg )
451 {
452 hmac_drbg_context hmac_drbg;
453 const md_info_t *md_info;
454
455#if defined(POLARSSL_SHA1_C)
456 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000457 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100458
459 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000460 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100461 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
462 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000463 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100464 hmac_drbg_free( &hmac_drbg );
465
466 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000467 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100468 hmac_drbg_set_prediction_resistance( &hmac_drbg,
469 POLARSSL_HMAC_DRBG_PR_ON );
470 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
471 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000472 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100473 hmac_drbg_free( &hmac_drbg );
474#endif
475
476#if defined(POLARSSL_SHA256_C)
477 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000478 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100479
480 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000481 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100482 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
483 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000484 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100485 hmac_drbg_free( &hmac_drbg );
486
487 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000488 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100489 hmac_drbg_set_prediction_resistance( &hmac_drbg,
490 POLARSSL_HMAC_DRBG_PR_ON );
491 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
492 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000493 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100494 hmac_drbg_free( &hmac_drbg );
495#endif
496 }
497#endif
498
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200500 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200502 rsa_context rsa;
503 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
504 {
Rich Evans783d9d12015-01-30 11:11:57 +0000505 polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200506
507 rsa_init( &rsa, RSA_PKCS_V15, 0 );
508 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
509
510 TIME_PUBLIC( title, " public",
511 buf[0] = 0;
512 ret = rsa_public( &rsa, buf, buf ) );
513
514 TIME_PUBLIC( title, "private",
515 buf[0] = 0;
516 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
517
518 rsa_free( &rsa );
519 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000521#endif
522
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100523#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200524 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100525 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200526 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200527 const char *dhm_P[DHM_SIZES] = {
528 POLARSSL_DHM_RFC5114_MODP_1024_P,
529 POLARSSL_DHM_RFC3526_MODP_2048_P,
530 POLARSSL_DHM_RFC3526_MODP_3072_P,
531 };
532 const char *dhm_G[DHM_SIZES] = {
533 POLARSSL_DHM_RFC5114_MODP_1024_G,
534 POLARSSL_DHM_RFC3526_MODP_2048_G,
535 POLARSSL_DHM_RFC3526_MODP_3072_G,
536 };
537
538 dhm_context dhm;
539 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200540 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200541 {
Paul Bakkera317a982014-06-18 16:44:11 +0200542 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200543
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200544 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
545 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
546 {
Rich Evans77d36382015-01-30 12:12:11 +0000547 polarssl_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200548 }
549
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200550 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100551 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200552 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000553 polarssl_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200554
Rich Evans783d9d12015-01-30 11:11:57 +0000555 polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200556 TIME_PUBLIC( title, "handshake",
557 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100558 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200559 myrand, NULL );
560 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
561
Rich Evans783d9d12015-01-30 11:11:57 +0000562 polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200563 TIME_PUBLIC( title, "handshake",
564 olen = sizeof( buf );
565 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
566
567 dhm_free( &dhm );
568 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100569 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100570#endif
571
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200572#if defined(POLARSSL_ECDSA_C)
573 if( todo.ecdsa )
574 {
575 ecdsa_context ecdsa;
576 const ecp_curve_info *curve_info;
577 size_t sig_len;
578
579 memset( buf, 0x2A, sizeof( buf ) );
580
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200581 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200582 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
583 curve_info++ )
584 {
585 ecdsa_init( &ecdsa );
586
587 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000588 polarssl_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200589
Rich Evans783d9d12015-01-30 11:11:57 +0000590 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200591 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200592 TIME_PUBLIC( title, "sign",
593 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200594 tmp, &sig_len, myrand, NULL ) );
595
596 TIME_PUBLIC( title, "verify",
597 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
598 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200599
600 ecdsa_free( &ecdsa );
601 }
602 }
603#endif
604
605#if defined(POLARSSL_ECDH_C)
606 if( todo.ecdh )
607 {
608 ecdh_context ecdh;
609 const ecp_curve_info *curve_info;
610 size_t olen;
611
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200612 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200613 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
614 curve_info++ )
615 {
616 ecdh_init( &ecdh );
617
618 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
619 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
620 myrand, NULL ) != 0 ||
621 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
622 {
Rich Evans77d36382015-01-30 12:12:11 +0000623 polarssl_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200624 }
625
Rich Evans783d9d12015-01-30 11:11:57 +0000626 polarssl_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200627 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200628 TIME_PUBLIC( title, "handshake",
629 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
630 myrand, NULL );
631 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
632 myrand, NULL ) );
633
Rich Evans783d9d12015-01-30 11:11:57 +0000634 polarssl_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200635 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200636 TIME_PUBLIC( title, "handshake",
637 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
638 myrand, NULL ) );
639 ecdh_free( &ecdh );
640 }
641 }
642#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000643 polarssl_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000644
Paul Bakkercce9d772011-11-18 14:26:47 +0000645#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000646 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 fflush( stdout ); getchar();
648#endif
649
650 return( 0 );
651}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200652
Paul Bakker5690efc2011-05-26 13:16:06 +0000653#endif /* POLARSSL_TIMING_C */