blob: f91a2d412a463c1818fd3dd14623ebc5c27437d6 [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 ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100301
302 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200303 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000304 }
305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000306#endif
307
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200308#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200309 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000310 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200311 camellia_context camellia;
312 for( keysize = 128; keysize <= 256; keysize += 64 )
313 {
314 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000315
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200316 memset( buf, 0, sizeof( buf ) );
317 memset( tmp, 0, sizeof( tmp ) );
318 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000319
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200320 TIME_AND_TSC( title,
321 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
322 BUFSIZE, tmp, buf, buf ) );
323 }
Paul Bakker38119b12009-01-10 23:31:23 +0000324 }
325#endif
326
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200327#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200328 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000329 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200330 blowfish_context blowfish;
331 for( keysize = 128; keysize <= 256; keysize += 64 )
332 {
333 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000334
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200335 memset( buf, 0, sizeof( buf ) );
336 memset( tmp, 0, sizeof( tmp ) );
337 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000338
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200339 TIME_AND_TSC( title,
340 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
341 tmp, buf, buf ) );
342 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000343 }
344#endif
345
Paul Bakker02faf452011-11-29 11:23:58 +0000346#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200347 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200348 {
349 havege_state hs;
350 havege_init( &hs );
351 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
352 }
Paul Bakker02faf452011-11-29 11:23:58 +0000353#endif
354
355#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200356 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200357 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200358 ctr_drbg_context ctr_drbg;
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 TIME_AND_TSC( "CTR_DRBG (NOPR)",
363 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
364 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000365
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200366 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000367 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200368 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
369 TIME_AND_TSC( "CTR_DRBG (PR)",
370 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
371 exit(1) );
372 }
Paul Bakker02faf452011-11-29 11:23:58 +0000373#endif
374
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200375#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200376 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200378 rsa_context rsa;
379 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
380 {
381 snprintf( title, sizeof( title ), "RSA-%d", keysize );
382
383 rsa_init( &rsa, RSA_PKCS_V15, 0 );
384 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
385
386 TIME_PUBLIC( title, " public",
387 buf[0] = 0;
388 ret = rsa_public( &rsa, buf, buf ) );
389
390 TIME_PUBLIC( title, "private",
391 buf[0] = 0;
392 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
393
394 rsa_free( &rsa );
395 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000397#endif
398
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100399#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200400 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100401 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200403 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200404 const char *dhm_P[DHM_SIZES] = {
405 POLARSSL_DHM_RFC5114_MODP_1024_P,
406 POLARSSL_DHM_RFC3526_MODP_2048_P,
407 POLARSSL_DHM_RFC3526_MODP_3072_P,
408 };
409 const char *dhm_G[DHM_SIZES] = {
410 POLARSSL_DHM_RFC5114_MODP_1024_G,
411 POLARSSL_DHM_RFC3526_MODP_2048_G,
412 POLARSSL_DHM_RFC3526_MODP_3072_G,
413 };
414
415 dhm_context dhm;
416 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200417 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200418 {
419 memset( &dhm, 0, sizeof( dhm_context ) );
420
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200421 mpi_read_string( &dhm.P, 16, dhm_P[i] );
422 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100424 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 mpi_copy( &dhm.GY, &dhm.GX );
426
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200427 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200428 TIME_PUBLIC( title, "handshake",
429 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100430 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200431 myrand, NULL );
432 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
433
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200434 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200435 TIME_PUBLIC( title, "handshake",
436 olen = sizeof( buf );
437 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
438
439 dhm_free( &dhm );
440 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100441 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100442#endif
443
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200444#if defined(POLARSSL_ECDSA_C)
445 if( todo.ecdsa )
446 {
447 ecdsa_context ecdsa;
448 const ecp_curve_info *curve_info;
449 size_t sig_len;
450
451 memset( buf, 0x2A, sizeof( buf ) );
452
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200453 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200454 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
455 curve_info++ )
456 {
457 ecdsa_init( &ecdsa );
458
459 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
460 exit( 1 );
461
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200462 snprintf( title, sizeof( title ), "ECDSA-%s",
463 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200464 TIME_PUBLIC( title, "sign",
465 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200466 tmp, &sig_len, myrand, NULL ) );
467
468 TIME_PUBLIC( title, "verify",
469 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
470 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200471
472 ecdsa_free( &ecdsa );
473 }
474 }
475#endif
476
477#if defined(POLARSSL_ECDH_C)
478 if( todo.ecdh )
479 {
480 ecdh_context ecdh;
481 const ecp_curve_info *curve_info;
482 size_t olen;
483
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200484 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200485 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
486 curve_info++ )
487 {
488 ecdh_init( &ecdh );
489
490 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
491 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
492 myrand, NULL ) != 0 ||
493 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
494 {
495 exit( 1 );
496 }
497
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200498 snprintf( title, sizeof( title ), "ECDHE-%s",
499 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200500 TIME_PUBLIC( title, "handshake",
501 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
502 myrand, NULL );
503 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
504 myrand, NULL ) );
505
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200506 snprintf( title, sizeof( title ), "ECDH-%s",
507 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200508 TIME_PUBLIC( title, "handshake",
509 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
510 myrand, NULL ) );
511 ecdh_free( &ecdh );
512 }
513 }
514#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000515 printf( "\n" );
516
Paul Bakkercce9d772011-11-18 14:26:47 +0000517#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 printf( " Press Enter to exit this program.\n" );
519 fflush( stdout ); getchar();
520#endif
521
522 return( 0 );
523}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200524
Paul Bakker5690efc2011-05-26 13:16:06 +0000525#endif /* POLARSSL_TIMING_C */