blob: 34219c5f3d806bcf28b6ec0ab2f2108dad7173d0 [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
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020052#if defined _MSC_VER && !defined snprintf
53#define snprintf _snprintf
54#endif
55
Paul Bakker02faf452011-11-29 11:23:58 +000056#define BUFSIZE 1024
Manuel Pégourié-Gonnard22f64c82013-10-10 13:11:20 +020057#define HEADER_FORMAT " %-18s : "
58#define TITLE_LEN 19
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020060#if !defined(POLARSSL_TIMING_C)
61int main( int argc, char *argv[] )
62{
63 ((void) argc);
64 ((void) argv);
65
66 printf("POLARSSL_TIMING_C not defined.\n");
67 return( 0 );
68}
69#else
70
Paul Bakkera3d195c2011-11-27 21:07:34 +000071static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
Paul Bakkera3d195c2011-11-27 21:07:34 +000073 size_t use_len;
74 int rnd;
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076 if( rng_state != NULL )
77 rng_state = NULL;
78
Paul Bakkera3d195c2011-11-27 21:07:34 +000079 while( len > 0 )
80 {
81 use_len = len;
82 if( use_len > sizeof(int) )
83 use_len = sizeof(int);
84
85 rnd = rand();
86 memcpy( output, &rnd, use_len );
87 output += use_len;
88 len -= use_len;
89 }
90
91 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
93
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020094#define TIME_AND_TSC( TITLE, CODE ) \
95do { \
96 unsigned long i, j, tsc; \
97 \
98 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 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 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 printf( "FAILED\n" ); \
134 else \
135 printf( "%9lu " TYPE "/s\n", i / 3 ); \
136} while( 0 )
137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138unsigned char buf[BUFSIZE];
139
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200140typedef struct {
141 char md4, md5, sha1, sha256, sha512,
142 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
143 havege, ctr_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200144 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200145} todo_list;
146
147#define OPTIONS \
148 "md4, md5, sha1, sha256, sha512,\n" \
149 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
150 "havege, ctr_drbg,\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200151 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200152
Paul Bakkercce9d772011-11-18 14:26:47 +0000153int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000154{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200155 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200156 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200157 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200158 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000159
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200160 if( argc == 1 )
161 memset( &todo, 1, sizeof( todo ) );
162 else
163 {
164 memset( &todo, 0, sizeof( todo ) );
165
166 for( i = 1; i < argc; i++ )
167 {
168 if( strcmp( argv[i], "md4" ) == 0 )
169 todo.md4 = 1;
170 else if( strcmp( argv[i], "md5" ) == 0 )
171 todo.md5 = 1;
172 else if( strcmp( argv[i], "sha1" ) == 0 )
173 todo.sha1 = 1;
174 else if( strcmp( argv[i], "sha256" ) == 0 )
175 todo.sha256 = 1;
176 else if( strcmp( argv[i], "sha512" ) == 0 )
177 todo.sha512 = 1;
178 else if( strcmp( argv[i], "arc4" ) == 0 )
179 todo.arc4 = 1;
180 else if( strcmp( argv[i], "des3" ) == 0 )
181 todo.des3 = 1;
182 else if( strcmp( argv[i], "des" ) == 0 )
183 todo.des = 1;
184 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
185 todo.aes_cbc = 1;
186 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
187 todo.aes_gcm = 1;
188 else if( strcmp( argv[i], "camellia" ) == 0 )
189 todo.camellia = 1;
190 else if( strcmp( argv[i], "blowfish" ) == 0 )
191 todo.blowfish = 1;
192 else if( strcmp( argv[i], "havege" ) == 0 )
193 todo.havege = 1;
194 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
195 todo.ctr_drbg = 1;
196 else if( strcmp( argv[i], "rsa" ) == 0 )
197 todo.rsa = 1;
198 else if( strcmp( argv[i], "dhm" ) == 0 )
199 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200200 else if( strcmp( argv[i], "ecdsa" ) == 0 )
201 todo.ecdsa = 1;
202 else if( strcmp( argv[i], "ecdh" ) == 0 )
203 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200204 else
205 {
206 printf( "Unrecognized option: %s\n", argv[i] );
207 printf( "Available options:" OPTIONS );
208 }
209 }
210 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212 printf( "\n" );
213
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200214 memset( buf, 0xAA, sizeof( buf ) );
215
Paul Bakker40e46942009-01-03 21:51:57 +0000216#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200217 if( todo.md4 )
218 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219#endif
220
Paul Bakker40e46942009-01-03 21:51:57 +0000221#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200222 if( todo.md5 )
223 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224#endif
225
Paul Bakker40e46942009-01-03 21:51:57 +0000226#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200227 if( todo.sha1 )
228 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229#endif
230
Paul Bakker9e36f042013-06-30 14:34:05 +0200231#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200232 if( todo.sha256 )
233 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234#endif
235
Paul Bakker9e36f042013-06-30 14:34:05 +0200236#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200237 if( todo.sha512 )
238 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000239#endif
240
Paul Bakker40e46942009-01-03 21:51:57 +0000241#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200242 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200243 {
244 arc4_context arc4;
245 arc4_setup( &arc4, tmp, 32 );
246 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
247 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000248#endif
249
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200250#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200251 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200252 {
253 des3_context des3;
254 des3_set3key_enc( &des3, tmp );
255 TIME_AND_TSC( "3DES",
256 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
257 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200259 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200260 {
261 des_context des;
262 des_setkey_enc( &des, tmp );
263 TIME_AND_TSC( "DES",
264 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
265 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000266#endif
267
Paul Bakker40e46942009-01-03 21:51:57 +0000268#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200269#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200272 aes_context aes;
273 for( keysize = 128; keysize <= 256; keysize += 64 )
274 {
275 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200277 memset( buf, 0, sizeof( buf ) );
278 memset( tmp, 0, sizeof( tmp ) );
279 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200281 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200282 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200283 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200285#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000286#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200287 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000288 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200289 gcm_context gcm;
290 for( keysize = 128; keysize <= 256; keysize += 64 )
291 {
292 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000293
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200294 memset( buf, 0, sizeof( buf ) );
295 memset( tmp, 0, sizeof( tmp ) );
296 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000297
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200298 TIME_AND_TSC( title,
299 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
300 12, NULL, 0, buf, buf, 16, tmp ) );
301 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000302 }
303#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000304#endif
305
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200306#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200307 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000308 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200309 camellia_context camellia;
310 for( keysize = 128; keysize <= 256; keysize += 64 )
311 {
312 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000313
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200314 memset( buf, 0, sizeof( buf ) );
315 memset( tmp, 0, sizeof( tmp ) );
316 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000317
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200318 TIME_AND_TSC( title,
319 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
320 BUFSIZE, tmp, buf, buf ) );
321 }
Paul Bakker38119b12009-01-10 23:31:23 +0000322 }
323#endif
324
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200325#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200326 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000327 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200328 blowfish_context blowfish;
329 for( keysize = 128; keysize <= 256; keysize += 64 )
330 {
331 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000332
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200333 memset( buf, 0, sizeof( buf ) );
334 memset( tmp, 0, sizeof( tmp ) );
335 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000336
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200337 TIME_AND_TSC( title,
338 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
339 tmp, buf, buf ) );
340 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000341 }
342#endif
343
Paul Bakker02faf452011-11-29 11:23:58 +0000344#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200345 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200346 {
347 havege_state hs;
348 havege_init( &hs );
349 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
350 }
Paul Bakker02faf452011-11-29 11:23:58 +0000351#endif
352
353#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200354 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200355 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200356 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000357
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200358 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000359 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200360 TIME_AND_TSC( "CTR_DRBG (NOPR)",
361 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
362 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000363
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200364 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000365 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200366 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
367 TIME_AND_TSC( "CTR_DRBG (PR)",
368 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
369 exit(1) );
370 }
Paul Bakker02faf452011-11-29 11:23:58 +0000371#endif
372
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200373#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 rsa_context rsa;
377 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
378 {
379 snprintf( title, sizeof( title ), "RSA-%d", keysize );
380
381 rsa_init( &rsa, RSA_PKCS_V15, 0 );
382 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
383
384 TIME_PUBLIC( title, " public",
385 buf[0] = 0;
386 ret = rsa_public( &rsa, buf, buf ) );
387
388 TIME_PUBLIC( title, "private",
389 buf[0] = 0;
390 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
391
392 rsa_free( &rsa );
393 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000395#endif
396
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100397#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200398 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100399 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200400#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200401 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402 const char *dhm_P[DHM_SIZES] = {
403 POLARSSL_DHM_RFC5114_MODP_1024_P,
404 POLARSSL_DHM_RFC3526_MODP_2048_P,
405 POLARSSL_DHM_RFC3526_MODP_3072_P,
406 };
407 const char *dhm_G[DHM_SIZES] = {
408 POLARSSL_DHM_RFC5114_MODP_1024_G,
409 POLARSSL_DHM_RFC3526_MODP_2048_G,
410 POLARSSL_DHM_RFC3526_MODP_3072_G,
411 };
412
413 dhm_context dhm;
414 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200415 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200416 {
417 memset( &dhm, 0, sizeof( dhm_context ) );
418
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200419 mpi_read_string( &dhm.P, 16, dhm_P[i] );
420 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200421 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100422 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 mpi_copy( &dhm.GY, &dhm.GX );
424
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200425 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200426 TIME_PUBLIC( title, "handshake",
427 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100428 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200429 myrand, NULL );
430 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
431
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200432 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200433 TIME_PUBLIC( title, "handshake",
434 olen = sizeof( buf );
435 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
436
437 dhm_free( &dhm );
438 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100439 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100440#endif
441
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200442#if defined(POLARSSL_ECDSA_C)
443 if( todo.ecdsa )
444 {
445 ecdsa_context ecdsa;
446 const ecp_curve_info *curve_info;
447 size_t sig_len;
448
449 memset( buf, 0x2A, sizeof( buf ) );
450
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200451 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200452 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
453 curve_info++ )
454 {
455 ecdsa_init( &ecdsa );
456
457 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
458 exit( 1 );
459
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200460 snprintf( title, sizeof( title ), "ECDSA-%s",
461 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200462 TIME_PUBLIC( title, "sign",
463 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200464 tmp, &sig_len, myrand, NULL ) );
465
466 TIME_PUBLIC( title, "verify",
467 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
468 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200469
470 ecdsa_free( &ecdsa );
471 }
472 }
473#endif
474
475#if defined(POLARSSL_ECDH_C)
476 if( todo.ecdh )
477 {
478 ecdh_context ecdh;
479 const ecp_curve_info *curve_info;
480 size_t olen;
481
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200482 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200483 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
484 curve_info++ )
485 {
486 ecdh_init( &ecdh );
487
488 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
489 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
490 myrand, NULL ) != 0 ||
491 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
492 {
493 exit( 1 );
494 }
495
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200496 snprintf( title, sizeof( title ), "ECDHE-%s",
497 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200498 TIME_PUBLIC( title, "handshake",
499 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
500 myrand, NULL );
501 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
502 myrand, NULL ) );
503
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200504 snprintf( title, sizeof( title ), "ECDH-%s",
505 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200506 TIME_PUBLIC( title, "handshake",
507 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
508 myrand, NULL ) );
509 ecdh_free( &ecdh );
510 }
511 }
512#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000513 printf( "\n" );
514
Paul Bakkercce9d772011-11-18 14:26:47 +0000515#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 printf( " Press Enter to exit this program.\n" );
517 fflush( stdout ); getchar();
518#endif
519
520 return( 0 );
521}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200522
Paul Bakker5690efc2011-05-26 13:16:06 +0000523#endif /* POLARSSL_TIMING_C */