blob: 57fe67b946a764f2e85e03f59f05d3f5ba9d4d93 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Paul Bakkerd2681d82013-06-30 14:49:12 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020032#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/md4.h"
35#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010036#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020038#include "polarssl/sha256.h"
39#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/arc4.h"
41#include "polarssl/des.h"
42#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000043#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000044#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000045#include "polarssl/gcm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020046#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020047#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000048#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010049#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020050#include "polarssl/ecdsa.h"
51#include "polarssl/ecdh.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020053#if defined _MSC_VER && !defined snprintf
54#define snprintf _snprintf
55#endif
56
Paul Bakker02faf452011-11-29 11:23:58 +000057#define BUFSIZE 1024
Manuel Pégourié-Gonnard22f64c82013-10-10 13:11:20 +020058#define HEADER_FORMAT " %-18s : "
59#define TITLE_LEN 19
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020061#if !defined(POLARSSL_TIMING_C)
62int main( int argc, char *argv[] )
63{
64 ((void) argc);
65 ((void) argv);
66
67 printf("POLARSSL_TIMING_C not defined.\n");
68 return( 0 );
69}
70#else
71
Paul Bakkera3d195c2011-11-27 21:07:34 +000072static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000073{
Paul Bakkera3d195c2011-11-27 21:07:34 +000074 size_t use_len;
75 int rnd;
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077 if( rng_state != NULL )
78 rng_state = NULL;
79
Paul Bakkera3d195c2011-11-27 21:07:34 +000080 while( len > 0 )
81 {
82 use_len = len;
83 if( use_len > sizeof(int) )
84 use_len = sizeof(int);
85
86 rnd = rand();
87 memcpy( output, &rnd, use_len );
88 output += use_len;
89 len -= use_len;
90 }
91
92 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000093}
94
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020095#define TIME_AND_TSC( TITLE, CODE ) \
96do { \
97 unsigned long i, j, tsc; \
98 \
99 printf( HEADER_FORMAT, TITLE ); \
100 fflush( stdout ); \
101 \
102 set_alarm( 1 ); \
103 for( i = 1; ! alarmed; i++ ) \
104 { \
105 CODE; \
106 } \
107 \
108 tsc = hardclock(); \
109 for( j = 0; j < 1024; j++ ) \
110 { \
111 CODE; \
112 } \
113 \
114 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
115 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
116} while( 0 )
117
118#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
119do { \
120 unsigned long i; \
121 int ret; \
122 \
123 printf( HEADER_FORMAT, TITLE ); \
124 fflush( stdout ); \
125 set_alarm( 3 ); \
126 \
127 ret = 0; \
128 for( i = 1; ! alarmed && ! ret ; i++ ) \
129 { \
130 CODE; \
131 } \
132 \
133 if( ret != 0 ) \
134 printf( "FAILED\n" ); \
135 else \
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100136 printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200137} while( 0 )
138
Paul Bakker5121ce52009-01-03 21:22:43 +0000139unsigned char buf[BUFSIZE];
140
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200141typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100142 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200143 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
144 havege, ctr_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200145 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200146} todo_list;
147
148#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100149 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200150 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
151 "havege, ctr_drbg,\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200152 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200153
Paul Bakkercce9d772011-11-18 14:26:47 +0000154int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000155{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200156 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200157 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200158 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200159 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000160
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200161 if( argc == 1 )
162 memset( &todo, 1, sizeof( todo ) );
163 else
164 {
165 memset( &todo, 0, sizeof( todo ) );
166
167 for( i = 1; i < argc; i++ )
168 {
169 if( strcmp( argv[i], "md4" ) == 0 )
170 todo.md4 = 1;
171 else if( strcmp( argv[i], "md5" ) == 0 )
172 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100173 else if( strcmp( argv[i], "ripemd160" ) == 0 )
174 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200175 else if( strcmp( argv[i], "sha1" ) == 0 )
176 todo.sha1 = 1;
177 else if( strcmp( argv[i], "sha256" ) == 0 )
178 todo.sha256 = 1;
179 else if( strcmp( argv[i], "sha512" ) == 0 )
180 todo.sha512 = 1;
181 else if( strcmp( argv[i], "arc4" ) == 0 )
182 todo.arc4 = 1;
183 else if( strcmp( argv[i], "des3" ) == 0 )
184 todo.des3 = 1;
185 else if( strcmp( argv[i], "des" ) == 0 )
186 todo.des = 1;
187 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
188 todo.aes_cbc = 1;
189 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
190 todo.aes_gcm = 1;
191 else if( strcmp( argv[i], "camellia" ) == 0 )
192 todo.camellia = 1;
193 else if( strcmp( argv[i], "blowfish" ) == 0 )
194 todo.blowfish = 1;
195 else if( strcmp( argv[i], "havege" ) == 0 )
196 todo.havege = 1;
197 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
198 todo.ctr_drbg = 1;
199 else if( strcmp( argv[i], "rsa" ) == 0 )
200 todo.rsa = 1;
201 else if( strcmp( argv[i], "dhm" ) == 0 )
202 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200203 else if( strcmp( argv[i], "ecdsa" ) == 0 )
204 todo.ecdsa = 1;
205 else if( strcmp( argv[i], "ecdh" ) == 0 )
206 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200207 else
208 {
209 printf( "Unrecognized option: %s\n", argv[i] );
210 printf( "Available options:" OPTIONS );
211 }
212 }
213 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 printf( "\n" );
216
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200217 memset( buf, 0xAA, sizeof( buf ) );
218
Paul Bakker40e46942009-01-03 21:51:57 +0000219#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200220 if( todo.md4 )
221 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222#endif
223
Paul Bakker40e46942009-01-03 21:51:57 +0000224#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200225 if( todo.md5 )
226 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227#endif
228
Paul Bakker61b699e2014-01-22 13:35:29 +0100229#if defined(POLARSSL_RIPEMD160_C)
230 if( todo.ripemd160 )
231 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100232#endif
233
Paul Bakker40e46942009-01-03 21:51:57 +0000234#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200235 if( todo.sha1 )
236 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237#endif
238
Paul Bakker9e36f042013-06-30 14:34:05 +0200239#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200240 if( todo.sha256 )
241 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242#endif
243
Paul Bakker9e36f042013-06-30 14:34:05 +0200244#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200245 if( todo.sha512 )
246 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000247#endif
248
Paul Bakker40e46942009-01-03 21:51:57 +0000249#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200250 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200251 {
252 arc4_context arc4;
253 arc4_setup( &arc4, tmp, 32 );
254 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
255 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000256#endif
257
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200258#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200259 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200260 {
261 des3_context des3;
262 des3_set3key_enc( &des3, tmp );
263 TIME_AND_TSC( "3DES",
264 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
265 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200267 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200268 {
269 des_context des;
270 des_setkey_enc( &des, tmp );
271 TIME_AND_TSC( "DES",
272 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
273 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000274#endif
275
Paul Bakker40e46942009-01-03 21:51:57 +0000276#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200277#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200278 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200280 aes_context aes;
281 for( keysize = 128; keysize <= 256; keysize += 64 )
282 {
283 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200285 memset( buf, 0, sizeof( buf ) );
286 memset( tmp, 0, sizeof( tmp ) );
287 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200289 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200290 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200291 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200293#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000294#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200295 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000296 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200297 gcm_context gcm;
298 for( keysize = 128; keysize <= 256; keysize += 64 )
299 {
300 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000301
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200302 memset( buf, 0, sizeof( buf ) );
303 memset( tmp, 0, sizeof( tmp ) );
304 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000305
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200306 TIME_AND_TSC( title,
307 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
308 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100309
310 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200311 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000312 }
313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000314#endif
315
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200316#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200317 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000318 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200319 camellia_context camellia;
320 for( keysize = 128; keysize <= 256; keysize += 64 )
321 {
322 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000323
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200324 memset( buf, 0, sizeof( buf ) );
325 memset( tmp, 0, sizeof( tmp ) );
326 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000327
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200328 TIME_AND_TSC( title,
329 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
330 BUFSIZE, tmp, buf, buf ) );
331 }
Paul Bakker38119b12009-01-10 23:31:23 +0000332 }
333#endif
334
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200335#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200336 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000337 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200338 blowfish_context blowfish;
339 for( keysize = 128; keysize <= 256; keysize += 64 )
340 {
341 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000342
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200343 memset( buf, 0, sizeof( buf ) );
344 memset( tmp, 0, sizeof( tmp ) );
345 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000346
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200347 TIME_AND_TSC( title,
348 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
349 tmp, buf, buf ) );
350 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000351 }
352#endif
353
Paul Bakker02faf452011-11-29 11:23:58 +0000354#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200355 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200356 {
357 havege_state hs;
358 havege_init( &hs );
359 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
360 }
Paul Bakker02faf452011-11-29 11:23:58 +0000361#endif
362
363#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200364 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200365 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200366 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000367
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200368 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000369 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200370 TIME_AND_TSC( "CTR_DRBG (NOPR)",
371 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
372 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000373
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200374 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000375 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
377 TIME_AND_TSC( "CTR_DRBG (PR)",
378 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
379 exit(1) );
380 }
Paul Bakker02faf452011-11-29 11:23:58 +0000381#endif
382
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200383#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200384 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200386 rsa_context rsa;
387 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
388 {
389 snprintf( title, sizeof( title ), "RSA-%d", keysize );
390
391 rsa_init( &rsa, RSA_PKCS_V15, 0 );
392 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
393
394 TIME_PUBLIC( title, " public",
395 buf[0] = 0;
396 ret = rsa_public( &rsa, buf, buf ) );
397
398 TIME_PUBLIC( title, "private",
399 buf[0] = 0;
400 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
401
402 rsa_free( &rsa );
403 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000405#endif
406
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100407#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200408 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100409 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200410#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200411 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200412 const char *dhm_P[DHM_SIZES] = {
413 POLARSSL_DHM_RFC5114_MODP_1024_P,
414 POLARSSL_DHM_RFC3526_MODP_2048_P,
415 POLARSSL_DHM_RFC3526_MODP_3072_P,
416 };
417 const char *dhm_G[DHM_SIZES] = {
418 POLARSSL_DHM_RFC5114_MODP_1024_G,
419 POLARSSL_DHM_RFC3526_MODP_2048_G,
420 POLARSSL_DHM_RFC3526_MODP_3072_G,
421 };
422
423 dhm_context dhm;
424 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200425 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200426 {
427 memset( &dhm, 0, sizeof( dhm_context ) );
428
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200429 mpi_read_string( &dhm.P, 16, dhm_P[i] );
430 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100432 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200433 mpi_copy( &dhm.GY, &dhm.GX );
434
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200435 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200436 TIME_PUBLIC( title, "handshake",
437 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100438 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200439 myrand, NULL );
440 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
441
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200442 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200443 TIME_PUBLIC( title, "handshake",
444 olen = sizeof( buf );
445 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
446
447 dhm_free( &dhm );
448 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100449 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100450#endif
451
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200452#if defined(POLARSSL_ECDSA_C)
453 if( todo.ecdsa )
454 {
455 ecdsa_context ecdsa;
456 const ecp_curve_info *curve_info;
457 size_t sig_len;
458
459 memset( buf, 0x2A, sizeof( buf ) );
460
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200461 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200462 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
463 curve_info++ )
464 {
465 ecdsa_init( &ecdsa );
466
467 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
468 exit( 1 );
469
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200470 snprintf( title, sizeof( title ), "ECDSA-%s",
471 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200472 TIME_PUBLIC( title, "sign",
473 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200474 tmp, &sig_len, myrand, NULL ) );
475
476 TIME_PUBLIC( title, "verify",
477 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
478 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200479
480 ecdsa_free( &ecdsa );
481 }
482 }
483#endif
484
485#if defined(POLARSSL_ECDH_C)
486 if( todo.ecdh )
487 {
488 ecdh_context ecdh;
489 const ecp_curve_info *curve_info;
490 size_t olen;
491
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200492 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200493 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
494 curve_info++ )
495 {
496 ecdh_init( &ecdh );
497
498 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
499 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
500 myrand, NULL ) != 0 ||
501 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
502 {
503 exit( 1 );
504 }
505
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200506 snprintf( title, sizeof( title ), "ECDHE-%s",
507 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200508 TIME_PUBLIC( title, "handshake",
509 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
510 myrand, NULL );
511 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
512 myrand, NULL ) );
513
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200514 snprintf( title, sizeof( title ), "ECDH-%s",
515 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200516 TIME_PUBLIC( title, "handshake",
517 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
518 myrand, NULL ) );
519 ecdh_free( &ecdh );
520 }
521 }
522#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000523 printf( "\n" );
524
Paul Bakkercce9d772011-11-18 14:26:47 +0000525#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 printf( " Press Enter to exit this program.\n" );
527 fflush( stdout ); getchar();
528#endif
529
530 return( 0 );
531}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200532
Paul Bakker5690efc2011-05-26 13:16:06 +0000533#endif /* POLARSSL_TIMING_C */