blob: c7f7f8290c373335c9f1e4962f84f0743edc7dd4 [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"
36#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020037#include "polarssl/sha256.h"
38#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/arc4.h"
40#include "polarssl/des.h"
41#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000042#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000043#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000044#include "polarssl/gcm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020045#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020046#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010048#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020049#include "polarssl/ecdsa.h"
50#include "polarssl/ecdh.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakker02faf452011-11-29 11:23:58 +000052#define BUFSIZE 1024
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020053#define HEADER_FORMAT " %-16s : "
54#define TITLE_LEN 17
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020056#if !defined(POLARSSL_TIMING_C)
57int main( int argc, char *argv[] )
58{
59 ((void) argc);
60 ((void) argv);
61
62 printf("POLARSSL_TIMING_C not defined.\n");
63 return( 0 );
64}
65#else
66
Paul Bakkera3d195c2011-11-27 21:07:34 +000067static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000068{
Paul Bakkera3d195c2011-11-27 21:07:34 +000069 size_t use_len;
70 int rnd;
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072 if( rng_state != NULL )
73 rng_state = NULL;
74
Paul Bakkera3d195c2011-11-27 21:07:34 +000075 while( len > 0 )
76 {
77 use_len = len;
78 if( use_len > sizeof(int) )
79 use_len = sizeof(int);
80
81 rnd = rand();
82 memcpy( output, &rnd, use_len );
83 output += use_len;
84 len -= use_len;
85 }
86
87 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
89
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020090#define TIME_AND_TSC( TITLE, CODE ) \
91do { \
92 unsigned long i, j, tsc; \
93 \
94 printf( HEADER_FORMAT, TITLE ); \
95 fflush( stdout ); \
96 \
97 set_alarm( 1 ); \
98 for( i = 1; ! alarmed; i++ ) \
99 { \
100 CODE; \
101 } \
102 \
103 tsc = hardclock(); \
104 for( j = 0; j < 1024; j++ ) \
105 { \
106 CODE; \
107 } \
108 \
109 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
110 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
111} while( 0 )
112
113#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
114do { \
115 unsigned long i; \
116 int ret; \
117 \
118 printf( HEADER_FORMAT, TITLE ); \
119 fflush( stdout ); \
120 set_alarm( 3 ); \
121 \
122 ret = 0; \
123 for( i = 1; ! alarmed && ! ret ; i++ ) \
124 { \
125 CODE; \
126 } \
127 \
128 if( ret != 0 ) \
129 printf( "FAILED\n" ); \
130 else \
131 printf( "%9lu " TYPE "/s\n", i / 3 ); \
132} while( 0 )
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134unsigned char buf[BUFSIZE];
135
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200136typedef struct {
137 char md4, md5, sha1, sha256, sha512,
138 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
139 havege, ctr_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200140 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200141} todo_list;
142
143#define OPTIONS \
144 "md4, md5, sha1, sha256, sha512,\n" \
145 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
146 "havege, ctr_drbg,\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200147 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200148
Paul Bakkercce9d772011-11-18 14:26:47 +0000149int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000150{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200151 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200152 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200153 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200154 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000155
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200156 if( argc == 1 )
157 memset( &todo, 1, sizeof( todo ) );
158 else
159 {
160 memset( &todo, 0, sizeof( todo ) );
161
162 for( i = 1; i < argc; i++ )
163 {
164 if( strcmp( argv[i], "md4" ) == 0 )
165 todo.md4 = 1;
166 else if( strcmp( argv[i], "md5" ) == 0 )
167 todo.md5 = 1;
168 else if( strcmp( argv[i], "sha1" ) == 0 )
169 todo.sha1 = 1;
170 else if( strcmp( argv[i], "sha256" ) == 0 )
171 todo.sha256 = 1;
172 else if( strcmp( argv[i], "sha512" ) == 0 )
173 todo.sha512 = 1;
174 else if( strcmp( argv[i], "arc4" ) == 0 )
175 todo.arc4 = 1;
176 else if( strcmp( argv[i], "des3" ) == 0 )
177 todo.des3 = 1;
178 else if( strcmp( argv[i], "des" ) == 0 )
179 todo.des = 1;
180 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
181 todo.aes_cbc = 1;
182 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
183 todo.aes_gcm = 1;
184 else if( strcmp( argv[i], "camellia" ) == 0 )
185 todo.camellia = 1;
186 else if( strcmp( argv[i], "blowfish" ) == 0 )
187 todo.blowfish = 1;
188 else if( strcmp( argv[i], "havege" ) == 0 )
189 todo.havege = 1;
190 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
191 todo.ctr_drbg = 1;
192 else if( strcmp( argv[i], "rsa" ) == 0 )
193 todo.rsa = 1;
194 else if( strcmp( argv[i], "dhm" ) == 0 )
195 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200196 else if( strcmp( argv[i], "ecdsa" ) == 0 )
197 todo.ecdsa = 1;
198 else if( strcmp( argv[i], "ecdh" ) == 0 )
199 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200200 else
201 {
202 printf( "Unrecognized option: %s\n", argv[i] );
203 printf( "Available options:" OPTIONS );
204 }
205 }
206 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 printf( "\n" );
209
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200210 memset( buf, 0xAA, sizeof( buf ) );
211
Paul Bakker40e46942009-01-03 21:51:57 +0000212#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200213 if( todo.md4 )
214 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215#endif
216
Paul Bakker40e46942009-01-03 21:51:57 +0000217#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200218 if( todo.md5 )
219 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220#endif
221
Paul Bakker40e46942009-01-03 21:51:57 +0000222#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200223 if( todo.sha1 )
224 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225#endif
226
Paul Bakker9e36f042013-06-30 14:34:05 +0200227#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200228 if( todo.sha256 )
229 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230#endif
231
Paul Bakker9e36f042013-06-30 14:34:05 +0200232#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200233 if( todo.sha512 )
234 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000235#endif
236
Paul Bakker40e46942009-01-03 21:51:57 +0000237#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200238 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200239 {
240 arc4_context arc4;
241 arc4_setup( &arc4, tmp, 32 );
242 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
243 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000244#endif
245
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200246#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200247 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200248 {
249 des3_context des3;
250 des3_set3key_enc( &des3, tmp );
251 TIME_AND_TSC( "3DES",
252 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
253 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200256 {
257 des_context des;
258 des_setkey_enc( &des, tmp );
259 TIME_AND_TSC( "DES",
260 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
261 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000262#endif
263
Paul Bakker40e46942009-01-03 21:51:57 +0000264#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200265#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200266 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200268 aes_context aes;
269 for( keysize = 128; keysize <= 256; keysize += 64 )
270 {
271 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200273 memset( buf, 0, sizeof( buf ) );
274 memset( tmp, 0, sizeof( tmp ) );
275 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200277 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200278 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200279 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200281#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000282#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200283 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000284 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200285 gcm_context gcm;
286 for( keysize = 128; keysize <= 256; keysize += 64 )
287 {
288 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000289
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200290 memset( buf, 0, sizeof( buf ) );
291 memset( tmp, 0, sizeof( tmp ) );
292 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000293
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200294 TIME_AND_TSC( title,
295 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
296 12, NULL, 0, buf, buf, 16, tmp ) );
297 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000298 }
299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000300#endif
301
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200302#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200303 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000304 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200305 camellia_context camellia;
306 for( keysize = 128; keysize <= 256; keysize += 64 )
307 {
308 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000309
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200310 memset( buf, 0, sizeof( buf ) );
311 memset( tmp, 0, sizeof( tmp ) );
312 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000313
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200314 TIME_AND_TSC( title,
315 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
316 BUFSIZE, tmp, buf, buf ) );
317 }
Paul Bakker38119b12009-01-10 23:31:23 +0000318 }
319#endif
320
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200321#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200322 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000323 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200324 blowfish_context blowfish;
325 for( keysize = 128; keysize <= 256; keysize += 64 )
326 {
327 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000328
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200329 memset( buf, 0, sizeof( buf ) );
330 memset( tmp, 0, sizeof( tmp ) );
331 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000332
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200333 TIME_AND_TSC( title,
334 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
335 tmp, buf, buf ) );
336 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000337 }
338#endif
339
Paul Bakker02faf452011-11-29 11:23:58 +0000340#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200341 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200342 {
343 havege_state hs;
344 havege_init( &hs );
345 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
346 }
Paul Bakker02faf452011-11-29 11:23:58 +0000347#endif
348
349#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200350 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200351 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200352 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000353
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200354 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000355 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200356 TIME_AND_TSC( "CTR_DRBG (NOPR)",
357 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
358 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000359
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200360 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000361 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200362 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
363 TIME_AND_TSC( "CTR_DRBG (PR)",
364 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
365 exit(1) );
366 }
Paul Bakker02faf452011-11-29 11:23:58 +0000367#endif
368
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200369#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200370 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200372 rsa_context rsa;
373 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
374 {
375 snprintf( title, sizeof( title ), "RSA-%d", keysize );
376
377 rsa_init( &rsa, RSA_PKCS_V15, 0 );
378 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
379
380 TIME_PUBLIC( title, " public",
381 buf[0] = 0;
382 ret = rsa_public( &rsa, buf, buf ) );
383
384 TIME_PUBLIC( title, "private",
385 buf[0] = 0;
386 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
387
388 rsa_free( &rsa );
389 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000391#endif
392
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100393#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200394 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100395 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200396#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200397 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200398 const char *dhm_P[DHM_SIZES] = {
399 POLARSSL_DHM_RFC5114_MODP_1024_P,
400 POLARSSL_DHM_RFC3526_MODP_2048_P,
401 POLARSSL_DHM_RFC3526_MODP_3072_P,
402 };
403 const char *dhm_G[DHM_SIZES] = {
404 POLARSSL_DHM_RFC5114_MODP_1024_G,
405 POLARSSL_DHM_RFC3526_MODP_2048_G,
406 POLARSSL_DHM_RFC3526_MODP_3072_G,
407 };
408
409 dhm_context dhm;
410 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200411 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200412 {
413 memset( &dhm, 0, sizeof( dhm_context ) );
414
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200415 mpi_read_string( &dhm.P, 16, dhm_P[i] );
416 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200417 dhm.len = mpi_size( &dhm.P );
418 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
419 mpi_copy( &dhm.GY, &dhm.GX );
420
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200421 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200422 TIME_PUBLIC( title, "handshake",
423 olen = sizeof( buf );
424 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len,
425 myrand, NULL );
426 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
427
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200428 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200429 TIME_PUBLIC( title, "handshake",
430 olen = sizeof( buf );
431 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
432
433 dhm_free( &dhm );
434 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100435 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100436#endif
437
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200438#if defined(POLARSSL_ECDSA_C)
439 if( todo.ecdsa )
440 {
441 ecdsa_context ecdsa;
442 const ecp_curve_info *curve_info;
443 size_t sig_len;
444
445 memset( buf, 0x2A, sizeof( buf ) );
446
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200447 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200448 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
449 curve_info++ )
450 {
451 ecdsa_init( &ecdsa );
452
453 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
454 exit( 1 );
455
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200456 snprintf( title, sizeof( title ), "ECDSA-%s",
457 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200458 TIME_PUBLIC( title, "sign",
459 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200460 tmp, &sig_len, myrand, NULL ) );
461
462 TIME_PUBLIC( title, "verify",
463 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
464 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200465
466 ecdsa_free( &ecdsa );
467 }
468 }
469#endif
470
471#if defined(POLARSSL_ECDH_C)
472 if( todo.ecdh )
473 {
474 ecdh_context ecdh;
475 const ecp_curve_info *curve_info;
476 size_t olen;
477
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200478 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200479 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
480 curve_info++ )
481 {
482 ecdh_init( &ecdh );
483
484 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
485 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
486 myrand, NULL ) != 0 ||
487 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
488 {
489 exit( 1 );
490 }
491
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200492 snprintf( title, sizeof( title ), "ECDHE-%s",
493 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200494 TIME_PUBLIC( title, "handshake",
495 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
496 myrand, NULL );
497 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
498 myrand, NULL ) );
499
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200500 snprintf( title, sizeof( title ), "ECDH-%s",
501 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200502 TIME_PUBLIC( title, "handshake",
503 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
504 myrand, NULL ) );
505 ecdh_free( &ecdh );
506 }
507 }
508#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000509 printf( "\n" );
510
Paul Bakkercce9d772011-11-18 14:26:47 +0000511#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 printf( " Press Enter to exit this program.\n" );
513 fflush( stdout ); getchar();
514#endif
515
516 return( 0 );
517}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200518
Paul Bakker5690efc2011-05-26 13:16:06 +0000519#endif /* POLARSSL_TIMING_C */