blob: 3acf78bf1dbdeb623ff075ae271e686e9a71a74a [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 Evans783d9d12015-01-30 11:11:57 +000032#define polarssl_snprintf snprintf
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker5121ce52009-01-03 21:22:43 +000036#include <string.h>
37#include <stdlib.h>
38#include <stdio.h>
39
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
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020064#if defined _MSC_VER && !defined snprintf
65#define snprintf _snprintf
66#endif
67
Paul Bakker02faf452011-11-29 11:23:58 +000068#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010069#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010070#define TITLE_LEN 25
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020072#if !defined(POLARSSL_TIMING_C)
73int main( int argc, char *argv[] )
74{
75 ((void) argc);
76 ((void) argv);
77
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf("POLARSSL_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020079 return( 0 );
80}
81#else
82
Paul Bakkera3d195c2011-11-27 21:07:34 +000083static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000084{
Paul Bakkera3d195c2011-11-27 21:07:34 +000085 size_t use_len;
86 int rnd;
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088 if( rng_state != NULL )
89 rng_state = NULL;
90
Paul Bakkera3d195c2011-11-27 21:07:34 +000091 while( len > 0 )
92 {
93 use_len = len;
94 if( use_len > sizeof(int) )
95 use_len = sizeof(int);
96
97 rnd = rand();
98 memcpy( output, &rnd, use_len );
99 output += use_len;
100 len -= use_len;
101 }
102
103 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104}
105
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200106#define TIME_AND_TSC( TITLE, CODE ) \
107do { \
108 unsigned long i, j, tsc; \
109 \
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf( HEADER_FORMAT, TITLE ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200111 fflush( stdout ); \
112 \
113 set_alarm( 1 ); \
114 for( i = 1; ! alarmed; i++ ) \
115 { \
116 CODE; \
117 } \
118 \
119 tsc = hardclock(); \
120 for( j = 0; j < 1024; j++ ) \
121 { \
122 CODE; \
123 } \
124 \
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200126 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
127} while( 0 )
128
Paul Bakker0c5e4292014-05-22 14:11:13 +0200129#if defined(POLARSSL_ERROR_C)
130#define PRINT_ERROR \
131 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Rich Evansf90016a2015-01-19 14:26:37 +0000132 polarssl_printf( "FAILED: %s\n", tmp );
Paul Bakker0c5e4292014-05-22 14:11:13 +0200133#else
134#define PRINT_ERROR \
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( "FAILED: -0x%04x\n", -ret );
Paul Bakker0c5e4292014-05-22 14:11:13 +0200136#endif
137
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200138#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
139do { \
140 unsigned long i; \
141 int ret; \
142 \
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( HEADER_FORMAT, TITLE ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200144 fflush( stdout ); \
145 set_alarm( 3 ); \
146 \
147 ret = 0; \
148 for( i = 1; ! alarmed && ! ret ; i++ ) \
149 { \
150 CODE; \
151 } \
152 \
153 if( ret != 0 ) \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100154 { \
Paul Bakker0c5e4292014-05-22 14:11:13 +0200155PRINT_ERROR; \
Gergely Budaia5d336b2014-01-27 23:27:06 +0100156 } \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200157 else \
Rich Evansf90016a2015-01-19 14:26:37 +0000158 polarssl_printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200159} while( 0 )
160
Paul Bakker5121ce52009-01-03 21:22:43 +0000161unsigned char buf[BUFSIZE];
162
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200163typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100164 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200165 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100166 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200167 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200168} todo_list;
169
170#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100171 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200172 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100173 "havege, ctr_drbg, hmac_drbg\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200174 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200175
Paul Bakkercce9d772011-11-18 14:26:47 +0000176int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000177{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200178 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200179 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200180 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200181 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000182
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200183 if( argc == 1 )
184 memset( &todo, 1, sizeof( todo ) );
185 else
186 {
187 memset( &todo, 0, sizeof( todo ) );
188
189 for( i = 1; i < argc; i++ )
190 {
191 if( strcmp( argv[i], "md4" ) == 0 )
192 todo.md4 = 1;
193 else if( strcmp( argv[i], "md5" ) == 0 )
194 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100195 else if( strcmp( argv[i], "ripemd160" ) == 0 )
196 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200197 else if( strcmp( argv[i], "sha1" ) == 0 )
198 todo.sha1 = 1;
199 else if( strcmp( argv[i], "sha256" ) == 0 )
200 todo.sha256 = 1;
201 else if( strcmp( argv[i], "sha512" ) == 0 )
202 todo.sha512 = 1;
203 else if( strcmp( argv[i], "arc4" ) == 0 )
204 todo.arc4 = 1;
205 else if( strcmp( argv[i], "des3" ) == 0 )
206 todo.des3 = 1;
207 else if( strcmp( argv[i], "des" ) == 0 )
208 todo.des = 1;
209 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
210 todo.aes_cbc = 1;
211 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
212 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200213 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
214 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200215 else if( strcmp( argv[i], "camellia" ) == 0 )
216 todo.camellia = 1;
217 else if( strcmp( argv[i], "blowfish" ) == 0 )
218 todo.blowfish = 1;
219 else if( strcmp( argv[i], "havege" ) == 0 )
220 todo.havege = 1;
221 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
222 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100223 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
224 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200225 else if( strcmp( argv[i], "rsa" ) == 0 )
226 todo.rsa = 1;
227 else if( strcmp( argv[i], "dhm" ) == 0 )
228 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200229 else if( strcmp( argv[i], "ecdsa" ) == 0 )
230 todo.ecdsa = 1;
231 else if( strcmp( argv[i], "ecdh" ) == 0 )
232 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200233 else
234 {
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( "Unrecognized option: %s\n", argv[i] );
236 polarssl_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200237 }
238 }
239 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Rich Evansf90016a2015-01-19 14:26:37 +0000241 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200243 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200244 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200245
Paul Bakker40e46942009-01-03 21:51:57 +0000246#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200247 if( todo.md4 )
248 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249#endif
250
Paul Bakker40e46942009-01-03 21:51:57 +0000251#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200252 if( todo.md5 )
253 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254#endif
255
Paul Bakker61b699e2014-01-22 13:35:29 +0100256#if defined(POLARSSL_RIPEMD160_C)
257 if( todo.ripemd160 )
258 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100259#endif
260
Paul Bakker40e46942009-01-03 21:51:57 +0000261#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200262 if( todo.sha1 )
263 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264#endif
265
Paul Bakker9e36f042013-06-30 14:34:05 +0200266#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200267 if( todo.sha256 )
268 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269#endif
270
Paul Bakker9e36f042013-06-30 14:34:05 +0200271#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200272 if( todo.sha512 )
273 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000274#endif
275
Paul Bakker40e46942009-01-03 21:51:57 +0000276#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200277 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200278 {
279 arc4_context arc4;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200280 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200281 arc4_setup( &arc4, tmp, 32 );
282 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200283 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200284 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000285#endif
286
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200287#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200289 {
290 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200291 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200292 des3_set3key_enc( &des3, tmp );
293 TIME_AND_TSC( "3DES",
294 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200295 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200296 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200298 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200299 {
300 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200301 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200302 des_setkey_enc( &des, tmp );
303 TIME_AND_TSC( "DES",
304 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200305 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200306 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000307#endif
308
Paul Bakker40e46942009-01-03 21:51:57 +0000309#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200310#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200311 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200313 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200314 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200315 for( keysize = 128; keysize <= 256; keysize += 64 )
316 {
Rich Evans783d9d12015-01-30 11:11:57 +0000317 polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200319 memset( buf, 0, sizeof( buf ) );
320 memset( tmp, 0, sizeof( tmp ) );
321 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200323 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200324 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200325 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200326 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200328#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000329#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200330 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000331 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200332 gcm_context gcm;
333 for( keysize = 128; keysize <= 256; keysize += 64 )
334 {
Rich Evans783d9d12015-01-30 11:11:57 +0000335 polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000336
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200337 memset( buf, 0, sizeof( buf ) );
338 memset( tmp, 0, sizeof( tmp ) );
339 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000340
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200341 TIME_AND_TSC( title,
342 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
343 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100344
345 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200346 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000347 }
348#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200349#if defined(POLARSSL_CCM_C)
350 if( todo.aes_ccm )
351 {
352 ccm_context ccm;
353 for( keysize = 128; keysize <= 256; keysize += 64 )
354 {
Rich Evans783d9d12015-01-30 11:11:57 +0000355 polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200356
357 memset( buf, 0, sizeof( buf ) );
358 memset( tmp, 0, sizeof( tmp ) );
359 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
360
361 TIME_AND_TSC( title,
362 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
363 12, NULL, 0, buf, buf, tmp, 16 ) );
364
365 ccm_free( &ccm );
366 }
367 }
368#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000369#endif
370
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200371#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200372 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000373 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200374 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200375 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 for( keysize = 128; keysize <= 256; keysize += 64 )
377 {
Rich Evans783d9d12015-01-30 11:11:57 +0000378 polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000379
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200380 memset( buf, 0, sizeof( buf ) );
381 memset( tmp, 0, sizeof( tmp ) );
382 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000383
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200384 TIME_AND_TSC( title,
385 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
386 BUFSIZE, tmp, buf, buf ) );
387 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200388 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000389 }
390#endif
391
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200392#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200393 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000394 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200395 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200396 blowfish_init( &blowfish );
397
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200398 for( keysize = 128; keysize <= 256; keysize += 64 )
399 {
Rich Evans783d9d12015-01-30 11:11:57 +0000400 polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000401
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402 memset( buf, 0, sizeof( buf ) );
403 memset( tmp, 0, sizeof( tmp ) );
404 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000405
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200406 TIME_AND_TSC( title,
407 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
408 tmp, buf, buf ) );
409 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200410
411 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000412 }
413#endif
414
Paul Bakker02faf452011-11-29 11:23:58 +0000415#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200416 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200417 {
418 havege_state hs;
419 havege_init( &hs );
420 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200421 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200422 }
Paul Bakker02faf452011-11-29 11:23:58 +0000423#endif
424
425#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200426 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200427 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200428 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000429
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200430 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000431 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200432 TIME_AND_TSC( "CTR_DRBG (NOPR)",
433 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
434 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000435
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200436 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000437 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200438 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
439 TIME_AND_TSC( "CTR_DRBG (PR)",
440 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
441 exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200442 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200443 }
Paul Bakker02faf452011-11-29 11:23:58 +0000444#endif
445
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100446#if defined(POLARSSL_HMAC_DRBG_C)
447 if( todo.hmac_drbg )
448 {
449 hmac_drbg_context hmac_drbg;
450 const md_info_t *md_info;
451
452#if defined(POLARSSL_SHA1_C)
453 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
454 exit(1);
455
456 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
457 exit(1);
458 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
459 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
460 exit(1) );
461 hmac_drbg_free( &hmac_drbg );
462
463 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
464 exit(1);
465 hmac_drbg_set_prediction_resistance( &hmac_drbg,
466 POLARSSL_HMAC_DRBG_PR_ON );
467 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
468 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
469 exit(1) );
470 hmac_drbg_free( &hmac_drbg );
471#endif
472
473#if defined(POLARSSL_SHA256_C)
474 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
475 exit(1);
476
477 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
478 exit(1);
479 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
480 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
481 exit(1) );
482 hmac_drbg_free( &hmac_drbg );
483
484 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
485 exit(1);
486 hmac_drbg_set_prediction_resistance( &hmac_drbg,
487 POLARSSL_HMAC_DRBG_PR_ON );
488 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
489 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
490 exit(1) );
491 hmac_drbg_free( &hmac_drbg );
492#endif
493 }
494#endif
495
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200496#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200497 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499 rsa_context rsa;
500 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
501 {
Rich Evans783d9d12015-01-30 11:11:57 +0000502 polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503
504 rsa_init( &rsa, RSA_PKCS_V15, 0 );
505 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
506
507 TIME_PUBLIC( title, " public",
508 buf[0] = 0;
509 ret = rsa_public( &rsa, buf, buf ) );
510
511 TIME_PUBLIC( title, "private",
512 buf[0] = 0;
513 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
514
515 rsa_free( &rsa );
516 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000518#endif
519
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100520#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200521 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100522 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200524 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200525 const char *dhm_P[DHM_SIZES] = {
526 POLARSSL_DHM_RFC5114_MODP_1024_P,
527 POLARSSL_DHM_RFC3526_MODP_2048_P,
528 POLARSSL_DHM_RFC3526_MODP_3072_P,
529 };
530 const char *dhm_G[DHM_SIZES] = {
531 POLARSSL_DHM_RFC5114_MODP_1024_G,
532 POLARSSL_DHM_RFC3526_MODP_2048_G,
533 POLARSSL_DHM_RFC3526_MODP_3072_G,
534 };
535
536 dhm_context dhm;
537 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200538 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200539 {
Paul Bakkera317a982014-06-18 16:44:11 +0200540 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200541
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200542 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
543 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
544 {
545 exit( 1 );
546 }
547
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200548 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100549 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200550 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
551 exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200552
Rich Evans783d9d12015-01-30 11:11:57 +0000553 polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200554 TIME_PUBLIC( title, "handshake",
555 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100556 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200557 myrand, NULL );
558 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
559
Rich Evans783d9d12015-01-30 11:11:57 +0000560 polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200561 TIME_PUBLIC( title, "handshake",
562 olen = sizeof( buf );
563 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
564
565 dhm_free( &dhm );
566 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100567 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100568#endif
569
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200570#if defined(POLARSSL_ECDSA_C)
571 if( todo.ecdsa )
572 {
573 ecdsa_context ecdsa;
574 const ecp_curve_info *curve_info;
575 size_t sig_len;
576
577 memset( buf, 0x2A, sizeof( buf ) );
578
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200579 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200580 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
581 curve_info++ )
582 {
583 ecdsa_init( &ecdsa );
584
585 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
586 exit( 1 );
587
Rich Evans783d9d12015-01-30 11:11:57 +0000588 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200589 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200590 TIME_PUBLIC( title, "sign",
591 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200592 tmp, &sig_len, myrand, NULL ) );
593
594 TIME_PUBLIC( title, "verify",
595 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
596 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200597
598 ecdsa_free( &ecdsa );
599 }
600 }
601#endif
602
603#if defined(POLARSSL_ECDH_C)
604 if( todo.ecdh )
605 {
606 ecdh_context ecdh;
607 const ecp_curve_info *curve_info;
608 size_t olen;
609
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200610 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200611 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
612 curve_info++ )
613 {
614 ecdh_init( &ecdh );
615
616 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
617 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
618 myrand, NULL ) != 0 ||
619 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
620 {
621 exit( 1 );
622 }
623
Rich Evans783d9d12015-01-30 11:11:57 +0000624 polarssl_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200625 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200626 TIME_PUBLIC( title, "handshake",
627 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
628 myrand, NULL );
629 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
630 myrand, NULL ) );
631
Rich Evans783d9d12015-01-30 11:11:57 +0000632 polarssl_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200633 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200634 TIME_PUBLIC( title, "handshake",
635 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
636 myrand, NULL ) );
637 ecdh_free( &ecdh );
638 }
639 }
640#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000641 polarssl_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000642
Paul Bakkercce9d772011-11-18 14:26:47 +0000643#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000644 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 fflush( stdout ); getchar();
646#endif
647
648 return( 0 );
649}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200650
Paul Bakker5690efc2011-05-26 13:16:06 +0000651#endif /* POLARSSL_TIMING_C */