blob: 0aa37db6969a5bf50911e2b3b80b4c96cc45e2ef [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020035#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#include "polarssl/md4.h"
38#include "polarssl/md5.h"
39#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020040#include "polarssl/sha256.h"
41#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/arc4.h"
43#include "polarssl/des.h"
44#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000045#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000046#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000047#include "polarssl/gcm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020048#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020049#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010051#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020052#include "polarssl/ecdsa.h"
53#include "polarssl/ecdh.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker02faf452011-11-29 11:23:58 +000055#define BUFSIZE 1024
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020056#define HEADER_FORMAT " %-16s : "
57#define TITLE_LEN 17
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020059#if !defined(POLARSSL_TIMING_C)
60int main( int argc, char *argv[] )
61{
62 ((void) argc);
63 ((void) argv);
64
65 printf("POLARSSL_TIMING_C not defined.\n");
66 return( 0 );
67}
68#else
69
Paul Bakkera3d195c2011-11-27 21:07:34 +000070static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
Paul Bakkera3d195c2011-11-27 21:07:34 +000072 size_t use_len;
73 int rnd;
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075 if( rng_state != NULL )
76 rng_state = NULL;
77
Paul Bakkera3d195c2011-11-27 21:07:34 +000078 while( len > 0 )
79 {
80 use_len = len;
81 if( use_len > sizeof(int) )
82 use_len = sizeof(int);
83
84 rnd = rand();
85 memcpy( output, &rnd, use_len );
86 output += use_len;
87 len -= use_len;
88 }
89
90 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
92
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020093#define TIME_AND_TSC( TITLE, CODE ) \
94do { \
95 unsigned long i, j, tsc; \
96 \
97 printf( HEADER_FORMAT, TITLE ); \
98 fflush( stdout ); \
99 \
100 set_alarm( 1 ); \
101 for( i = 1; ! alarmed; i++ ) \
102 { \
103 CODE; \
104 } \
105 \
106 tsc = hardclock(); \
107 for( j = 0; j < 1024; j++ ) \
108 { \
109 CODE; \
110 } \
111 \
112 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
113 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
114} while( 0 )
115
116#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
117do { \
118 unsigned long i; \
119 int ret; \
120 \
121 printf( HEADER_FORMAT, TITLE ); \
122 fflush( stdout ); \
123 set_alarm( 3 ); \
124 \
125 ret = 0; \
126 for( i = 1; ! alarmed && ! ret ; i++ ) \
127 { \
128 CODE; \
129 } \
130 \
131 if( ret != 0 ) \
132 printf( "FAILED\n" ); \
133 else \
134 printf( "%9lu " TYPE "/s\n", i / 3 ); \
135} while( 0 )
136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137unsigned char buf[BUFSIZE];
138
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200139typedef struct {
140 char md4, md5, sha1, sha256, sha512,
141 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
142 havege, ctr_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200143 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200144} todo_list;
145
146#define OPTIONS \
147 "md4, md5, sha1, sha256, sha512,\n" \
148 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
149 "havege, ctr_drbg,\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200150 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200151
Paul Bakkercce9d772011-11-18 14:26:47 +0000152int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000153{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200154 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200155 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200156 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200157 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000158
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200159 if( argc == 1 )
160 memset( &todo, 1, sizeof( todo ) );
161 else
162 {
163 memset( &todo, 0, sizeof( todo ) );
164
165 for( i = 1; i < argc; i++ )
166 {
167 if( strcmp( argv[i], "md4" ) == 0 )
168 todo.md4 = 1;
169 else if( strcmp( argv[i], "md5" ) == 0 )
170 todo.md5 = 1;
171 else if( strcmp( argv[i], "sha1" ) == 0 )
172 todo.sha1 = 1;
173 else if( strcmp( argv[i], "sha256" ) == 0 )
174 todo.sha256 = 1;
175 else if( strcmp( argv[i], "sha512" ) == 0 )
176 todo.sha512 = 1;
177 else if( strcmp( argv[i], "arc4" ) == 0 )
178 todo.arc4 = 1;
179 else if( strcmp( argv[i], "des3" ) == 0 )
180 todo.des3 = 1;
181 else if( strcmp( argv[i], "des" ) == 0 )
182 todo.des = 1;
183 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
184 todo.aes_cbc = 1;
185 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
186 todo.aes_gcm = 1;
187 else if( strcmp( argv[i], "camellia" ) == 0 )
188 todo.camellia = 1;
189 else if( strcmp( argv[i], "blowfish" ) == 0 )
190 todo.blowfish = 1;
191 else if( strcmp( argv[i], "havege" ) == 0 )
192 todo.havege = 1;
193 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
194 todo.ctr_drbg = 1;
195 else if( strcmp( argv[i], "rsa" ) == 0 )
196 todo.rsa = 1;
197 else if( strcmp( argv[i], "dhm" ) == 0 )
198 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200199 else if( strcmp( argv[i], "ecdsa" ) == 0 )
200 todo.ecdsa = 1;
201 else if( strcmp( argv[i], "ecdh" ) == 0 )
202 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200203 else
204 {
205 printf( "Unrecognized option: %s\n", argv[i] );
206 printf( "Available options:" OPTIONS );
207 }
208 }
209 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 printf( "\n" );
212
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200213 memset( buf, 0xAA, sizeof( buf ) );
214
Paul Bakker40e46942009-01-03 21:51:57 +0000215#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200216 if( todo.md4 )
217 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218#endif
219
Paul Bakker40e46942009-01-03 21:51:57 +0000220#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200221 if( todo.md5 )
222 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223#endif
224
Paul Bakker40e46942009-01-03 21:51:57 +0000225#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200226 if( todo.sha1 )
227 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228#endif
229
Paul Bakker9e36f042013-06-30 14:34:05 +0200230#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200231 if( todo.sha256 )
232 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233#endif
234
Paul Bakker9e36f042013-06-30 14:34:05 +0200235#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200236 if( todo.sha512 )
237 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000238#endif
239
Paul Bakker40e46942009-01-03 21:51:57 +0000240#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200241 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200242 {
243 arc4_context arc4;
244 arc4_setup( &arc4, tmp, 32 );
245 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
246 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000247#endif
248
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200249#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200250 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200251 {
252 des3_context des3;
253 des3_set3key_enc( &des3, tmp );
254 TIME_AND_TSC( "3DES",
255 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
256 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200258 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200259 {
260 des_context des;
261 des_setkey_enc( &des, tmp );
262 TIME_AND_TSC( "DES",
263 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
264 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000265#endif
266
Paul Bakker40e46942009-01-03 21:51:57 +0000267#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200268#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200269 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200271 aes_context aes;
272 for( keysize = 128; keysize <= 256; keysize += 64 )
273 {
274 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200276 memset( buf, 0, sizeof( buf ) );
277 memset( tmp, 0, sizeof( tmp ) );
278 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200280 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200281 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200282 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200284#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000285#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200286 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000287 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200288 gcm_context gcm;
289 for( keysize = 128; keysize <= 256; keysize += 64 )
290 {
291 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000292
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200293 memset( buf, 0, sizeof( buf ) );
294 memset( tmp, 0, sizeof( tmp ) );
295 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000296
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200297 TIME_AND_TSC( title,
298 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
299 12, NULL, 0, buf, buf, 16, tmp ) );
300 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000301 }
302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000303#endif
304
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200305#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200306 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000307 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200308 camellia_context camellia;
309 for( keysize = 128; keysize <= 256; keysize += 64 )
310 {
311 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000312
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200313 memset( buf, 0, sizeof( buf ) );
314 memset( tmp, 0, sizeof( tmp ) );
315 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000316
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200317 TIME_AND_TSC( title,
318 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
319 BUFSIZE, tmp, buf, buf ) );
320 }
Paul Bakker38119b12009-01-10 23:31:23 +0000321 }
322#endif
323
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200324#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200325 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000326 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200327 blowfish_context blowfish;
328 for( keysize = 128; keysize <= 256; keysize += 64 )
329 {
330 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000331
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200332 memset( buf, 0, sizeof( buf ) );
333 memset( tmp, 0, sizeof( tmp ) );
334 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000335
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200336 TIME_AND_TSC( title,
337 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
338 tmp, buf, buf ) );
339 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000340 }
341#endif
342
Paul Bakker02faf452011-11-29 11:23:58 +0000343#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200344 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200345 {
346 havege_state hs;
347 havege_init( &hs );
348 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
349 }
Paul Bakker02faf452011-11-29 11:23:58 +0000350#endif
351
352#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200353 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200354 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200355 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000356
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200357 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000358 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200359 TIME_AND_TSC( "CTR_DRBG (NOPR)",
360 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
361 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000362
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200363 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000364 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200365 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
366 TIME_AND_TSC( "CTR_DRBG (PR)",
367 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
368 exit(1) );
369 }
Paul Bakker02faf452011-11-29 11:23:58 +0000370#endif
371
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200372#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200373 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200375 rsa_context rsa;
376 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
377 {
378 snprintf( title, sizeof( title ), "RSA-%d", keysize );
379
380 rsa_init( &rsa, RSA_PKCS_V15, 0 );
381 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
382
383 TIME_PUBLIC( title, " public",
384 buf[0] = 0;
385 ret = rsa_public( &rsa, buf, buf ) );
386
387 TIME_PUBLIC( title, "private",
388 buf[0] = 0;
389 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
390
391 rsa_free( &rsa );
392 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000394#endif
395
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100396#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200397 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100398 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200399#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200400 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200401 const char *dhm_P[DHM_SIZES] = {
402 POLARSSL_DHM_RFC5114_MODP_1024_P,
403 POLARSSL_DHM_RFC3526_MODP_2048_P,
404 POLARSSL_DHM_RFC3526_MODP_3072_P,
405 };
406 const char *dhm_G[DHM_SIZES] = {
407 POLARSSL_DHM_RFC5114_MODP_1024_G,
408 POLARSSL_DHM_RFC3526_MODP_2048_G,
409 POLARSSL_DHM_RFC3526_MODP_3072_G,
410 };
411
412 dhm_context dhm;
413 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200414 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 {
416 memset( &dhm, 0, sizeof( dhm_context ) );
417
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200418 mpi_read_string( &dhm.P, 16, dhm_P[i] );
419 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 dhm.len = mpi_size( &dhm.P );
421 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
422 mpi_copy( &dhm.GY, &dhm.GX );
423
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200424 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 TIME_PUBLIC( title, "handshake",
426 olen = sizeof( buf );
427 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len,
428 myrand, NULL );
429 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
430
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200431 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200432 TIME_PUBLIC( title, "handshake",
433 olen = sizeof( buf );
434 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
435
436 dhm_free( &dhm );
437 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100438 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100439#endif
440
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200441#if defined(POLARSSL_ECDSA_C)
442 if( todo.ecdsa )
443 {
444 ecdsa_context ecdsa;
445 const ecp_curve_info *curve_info;
446 size_t sig_len;
447
448 memset( buf, 0x2A, sizeof( buf ) );
449
450 for( curve_info = ecp_supported_curves;
451 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
452 curve_info++ )
453 {
454 ecdsa_init( &ecdsa );
455
456 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
457 exit( 1 );
458
459 snprintf( title, sizeof( title ), "ECDSA-%d",
460 (int) curve_info->size );
461 TIME_PUBLIC( title, "sign",
462 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
463 tmp, &sig_len, myrand, NULL ) );
464
465 ecdsa_free( &ecdsa );
466 }
467 }
468#endif
469
470#if defined(POLARSSL_ECDH_C)
471 if( todo.ecdh )
472 {
473 ecdh_context ecdh;
474 const ecp_curve_info *curve_info;
475 size_t olen;
476
477 for( curve_info = ecp_supported_curves;
478 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
479 curve_info++ )
480 {
481 ecdh_init( &ecdh );
482
483 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
484 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
485 myrand, NULL ) != 0 ||
486 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
487 {
488 exit( 1 );
489 }
490
491 snprintf( title, sizeof( title ), "ECDHE-%d",
492 (int) curve_info->size );
493 TIME_PUBLIC( title, "handshake",
494 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
495 myrand, NULL );
496 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
497 myrand, NULL ) );
498
499 snprintf( title, sizeof( title ), "ECDH-%d",
500 (int) curve_info->size );
501 TIME_PUBLIC( title, "handshake",
502 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
503 myrand, NULL ) );
504 ecdh_free( &ecdh );
505 }
506 }
507#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000508 printf( "\n" );
509
Paul Bakkercce9d772011-11-18 14:26:47 +0000510#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 printf( " Press Enter to exit this program.\n" );
512 fflush( stdout ); getchar();
513#endif
514
515 return( 0 );
516}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200517
Paul Bakker5690efc2011-05-26 13:16:06 +0000518#endif /* POLARSSL_TIMING_C */