blob: b1974fed1f1822eca674ecf34292c0f28bd18124 [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"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010048#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010050#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020051#include "polarssl/ecdsa.h"
52#include "polarssl/ecdh.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020054#if defined _MSC_VER && !defined snprintf
55#define snprintf _snprintf
56#endif
57
Paul Bakker02faf452011-11-29 11:23:58 +000058#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010059#define HEADER_FORMAT " %-24s : "
60#define TITLE_LEN 15
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020062#if !defined(POLARSSL_TIMING_C)
63int main( int argc, char *argv[] )
64{
65 ((void) argc);
66 ((void) argv);
67
68 printf("POLARSSL_TIMING_C not defined.\n");
69 return( 0 );
70}
71#else
72
Paul Bakkera3d195c2011-11-27 21:07:34 +000073static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
Paul Bakkera3d195c2011-11-27 21:07:34 +000075 size_t use_len;
76 int rnd;
77
Paul Bakker5121ce52009-01-03 21:22:43 +000078 if( rng_state != NULL )
79 rng_state = NULL;
80
Paul Bakkera3d195c2011-11-27 21:07:34 +000081 while( len > 0 )
82 {
83 use_len = len;
84 if( use_len > sizeof(int) )
85 use_len = sizeof(int);
86
87 rnd = rand();
88 memcpy( output, &rnd, use_len );
89 output += use_len;
90 len -= use_len;
91 }
92
93 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
95
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020096#define TIME_AND_TSC( TITLE, CODE ) \
97do { \
98 unsigned long i, j, tsc; \
99 \
100 printf( HEADER_FORMAT, TITLE ); \
101 fflush( stdout ); \
102 \
103 set_alarm( 1 ); \
104 for( i = 1; ! alarmed; i++ ) \
105 { \
106 CODE; \
107 } \
108 \
109 tsc = hardclock(); \
110 for( j = 0; j < 1024; j++ ) \
111 { \
112 CODE; \
113 } \
114 \
115 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
116 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
117} while( 0 )
118
119#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
120do { \
121 unsigned long i; \
122 int ret; \
123 \
124 printf( HEADER_FORMAT, TITLE ); \
125 fflush( stdout ); \
126 set_alarm( 3 ); \
127 \
128 ret = 0; \
129 for( i = 1; ! alarmed && ! ret ; i++ ) \
130 { \
131 CODE; \
132 } \
133 \
134 if( ret != 0 ) \
135 printf( "FAILED\n" ); \
136 else \
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100137 printf( "%9lu " TYPE "/s\n", i / 3 ); \
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200138} while( 0 )
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140unsigned char buf[BUFSIZE];
141
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200142typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100143 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200144 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100145 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200146 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200147} todo_list;
148
149#define OPTIONS \
Paul Bakker61b699e2014-01-22 13:35:29 +0100150 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200151 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100152 "havege, ctr_drbg, hmac_drbg\n" \
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200153 "rsa, dhm, ecdsa, ecdh.\n"
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200154
Paul Bakkercce9d772011-11-18 14:26:47 +0000155int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000156{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200157 int keysize, i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200158 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200159 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200160 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000161
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200162 if( argc == 1 )
163 memset( &todo, 1, sizeof( todo ) );
164 else
165 {
166 memset( &todo, 0, sizeof( todo ) );
167
168 for( i = 1; i < argc; i++ )
169 {
170 if( strcmp( argv[i], "md4" ) == 0 )
171 todo.md4 = 1;
172 else if( strcmp( argv[i], "md5" ) == 0 )
173 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100174 else if( strcmp( argv[i], "ripemd160" ) == 0 )
175 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200176 else if( strcmp( argv[i], "sha1" ) == 0 )
177 todo.sha1 = 1;
178 else if( strcmp( argv[i], "sha256" ) == 0 )
179 todo.sha256 = 1;
180 else if( strcmp( argv[i], "sha512" ) == 0 )
181 todo.sha512 = 1;
182 else if( strcmp( argv[i], "arc4" ) == 0 )
183 todo.arc4 = 1;
184 else if( strcmp( argv[i], "des3" ) == 0 )
185 todo.des3 = 1;
186 else if( strcmp( argv[i], "des" ) == 0 )
187 todo.des = 1;
188 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
189 todo.aes_cbc = 1;
190 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
191 todo.aes_gcm = 1;
192 else if( strcmp( argv[i], "camellia" ) == 0 )
193 todo.camellia = 1;
194 else if( strcmp( argv[i], "blowfish" ) == 0 )
195 todo.blowfish = 1;
196 else if( strcmp( argv[i], "havege" ) == 0 )
197 todo.havege = 1;
198 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
199 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100200 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
201 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200202 else if( strcmp( argv[i], "rsa" ) == 0 )
203 todo.rsa = 1;
204 else if( strcmp( argv[i], "dhm" ) == 0 )
205 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200206 else if( strcmp( argv[i], "ecdsa" ) == 0 )
207 todo.ecdsa = 1;
208 else if( strcmp( argv[i], "ecdh" ) == 0 )
209 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200210 else
211 {
212 printf( "Unrecognized option: %s\n", argv[i] );
213 printf( "Available options:" OPTIONS );
214 }
215 }
216 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218 printf( "\n" );
219
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200220 memset( buf, 0xAA, sizeof( buf ) );
221
Paul Bakker40e46942009-01-03 21:51:57 +0000222#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200223 if( todo.md4 )
224 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225#endif
226
Paul Bakker40e46942009-01-03 21:51:57 +0000227#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200228 if( todo.md5 )
229 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230#endif
231
Paul Bakker61b699e2014-01-22 13:35:29 +0100232#if defined(POLARSSL_RIPEMD160_C)
233 if( todo.ripemd160 )
234 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100235#endif
236
Paul Bakker40e46942009-01-03 21:51:57 +0000237#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200238 if( todo.sha1 )
239 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240#endif
241
Paul Bakker9e36f042013-06-30 14:34:05 +0200242#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200243 if( todo.sha256 )
244 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245#endif
246
Paul Bakker9e36f042013-06-30 14:34:05 +0200247#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200248 if( todo.sha512 )
249 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000250#endif
251
Paul Bakker40e46942009-01-03 21:51:57 +0000252#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200253 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200254 {
255 arc4_context arc4;
256 arc4_setup( &arc4, tmp, 32 );
257 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
258 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000259#endif
260
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200261#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200262 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200263 {
264 des3_context des3;
265 des3_set3key_enc( &des3, tmp );
266 TIME_AND_TSC( "3DES",
267 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
268 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200271 {
272 des_context des;
273 des_setkey_enc( &des, tmp );
274 TIME_AND_TSC( "DES",
275 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
276 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000277#endif
278
Paul Bakker40e46942009-01-03 21:51:57 +0000279#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200280#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200281 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200283 aes_context aes;
284 for( keysize = 128; keysize <= 256; keysize += 64 )
285 {
286 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200288 memset( buf, 0, sizeof( buf ) );
289 memset( tmp, 0, sizeof( tmp ) );
290 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200292 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200293 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200294 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200296#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000297#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200298 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000299 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200300 gcm_context gcm;
301 for( keysize = 128; keysize <= 256; keysize += 64 )
302 {
303 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000304
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200305 memset( buf, 0, sizeof( buf ) );
306 memset( tmp, 0, sizeof( tmp ) );
307 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000308
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200309 TIME_AND_TSC( title,
310 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
311 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100312
313 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200314 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000315 }
316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000317#endif
318
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200319#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200320 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000321 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200322 camellia_context camellia;
323 for( keysize = 128; keysize <= 256; keysize += 64 )
324 {
325 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000326
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200327 memset( buf, 0, sizeof( buf ) );
328 memset( tmp, 0, sizeof( tmp ) );
329 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000330
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200331 TIME_AND_TSC( title,
332 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
333 BUFSIZE, tmp, buf, buf ) );
334 }
Paul Bakker38119b12009-01-10 23:31:23 +0000335 }
336#endif
337
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200338#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200339 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000340 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200341 blowfish_context blowfish;
342 for( keysize = 128; keysize <= 256; keysize += 64 )
343 {
344 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000345
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200346 memset( buf, 0, sizeof( buf ) );
347 memset( tmp, 0, sizeof( tmp ) );
348 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000349
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200350 TIME_AND_TSC( title,
351 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
352 tmp, buf, buf ) );
353 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000354 }
355#endif
356
Paul Bakker02faf452011-11-29 11:23:58 +0000357#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200358 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200359 {
360 havege_state hs;
361 havege_init( &hs );
362 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
363 }
Paul Bakker02faf452011-11-29 11:23:58 +0000364#endif
365
366#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200367 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200368 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200369 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000370
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200371 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000372 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200373 TIME_AND_TSC( "CTR_DRBG (NOPR)",
374 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
375 exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000376
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200377 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000378 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200379 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
380 TIME_AND_TSC( "CTR_DRBG (PR)",
381 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
382 exit(1) );
383 }
Paul Bakker02faf452011-11-29 11:23:58 +0000384#endif
385
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100386#if defined(POLARSSL_HMAC_DRBG_C)
387 if( todo.hmac_drbg )
388 {
389 hmac_drbg_context hmac_drbg;
390 const md_info_t *md_info;
391
392#if defined(POLARSSL_SHA1_C)
393 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
394 exit(1);
395
396 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
397 exit(1);
398 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
399 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
400 exit(1) );
401 hmac_drbg_free( &hmac_drbg );
402
403 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
404 exit(1);
405 hmac_drbg_set_prediction_resistance( &hmac_drbg,
406 POLARSSL_HMAC_DRBG_PR_ON );
407 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
408 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
409 exit(1) );
410 hmac_drbg_free( &hmac_drbg );
411#endif
412
413#if defined(POLARSSL_SHA256_C)
414 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
415 exit(1);
416
417 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
418 exit(1);
419 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
420 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
421 exit(1) );
422 hmac_drbg_free( &hmac_drbg );
423
424 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
425 exit(1);
426 hmac_drbg_set_prediction_resistance( &hmac_drbg,
427 POLARSSL_HMAC_DRBG_PR_ON );
428 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
429 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
430 exit(1) );
431 hmac_drbg_free( &hmac_drbg );
432#endif
433 }
434#endif
435
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200436#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200437 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200439 rsa_context rsa;
440 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
441 {
442 snprintf( title, sizeof( title ), "RSA-%d", keysize );
443
444 rsa_init( &rsa, RSA_PKCS_V15, 0 );
445 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
446
447 TIME_PUBLIC( title, " public",
448 buf[0] = 0;
449 ret = rsa_public( &rsa, buf, buf ) );
450
451 TIME_PUBLIC( title, "private",
452 buf[0] = 0;
453 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
454
455 rsa_free( &rsa );
456 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000458#endif
459
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100460#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200461 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100462 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200463#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200464 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200465 const char *dhm_P[DHM_SIZES] = {
466 POLARSSL_DHM_RFC5114_MODP_1024_P,
467 POLARSSL_DHM_RFC3526_MODP_2048_P,
468 POLARSSL_DHM_RFC3526_MODP_3072_P,
469 };
470 const char *dhm_G[DHM_SIZES] = {
471 POLARSSL_DHM_RFC5114_MODP_1024_G,
472 POLARSSL_DHM_RFC3526_MODP_2048_G,
473 POLARSSL_DHM_RFC3526_MODP_3072_G,
474 };
475
476 dhm_context dhm;
477 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200478 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200479 {
480 memset( &dhm, 0, sizeof( dhm_context ) );
481
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200482 mpi_read_string( &dhm.P, 16, dhm_P[i] );
483 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200484 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100485 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200486 mpi_copy( &dhm.GY, &dhm.GX );
487
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200488 snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200489 TIME_PUBLIC( title, "handshake",
490 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100491 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200492 myrand, NULL );
493 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
494
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200495 snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200496 TIME_PUBLIC( title, "handshake",
497 olen = sizeof( buf );
498 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
499
500 dhm_free( &dhm );
501 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100502 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100503#endif
504
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200505#if defined(POLARSSL_ECDSA_C)
506 if( todo.ecdsa )
507 {
508 ecdsa_context ecdsa;
509 const ecp_curve_info *curve_info;
510 size_t sig_len;
511
512 memset( buf, 0x2A, sizeof( buf ) );
513
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200514 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200515 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
516 curve_info++ )
517 {
518 ecdsa_init( &ecdsa );
519
520 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
521 exit( 1 );
522
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200523 snprintf( title, sizeof( title ), "ECDSA-%s",
524 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200525 TIME_PUBLIC( title, "sign",
526 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200527 tmp, &sig_len, myrand, NULL ) );
528
529 TIME_PUBLIC( title, "verify",
530 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
531 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200532
533 ecdsa_free( &ecdsa );
534 }
535 }
536#endif
537
538#if defined(POLARSSL_ECDH_C)
539 if( todo.ecdh )
540 {
541 ecdh_context ecdh;
542 const ecp_curve_info *curve_info;
543 size_t olen;
544
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200545 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200546 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
547 curve_info++ )
548 {
549 ecdh_init( &ecdh );
550
551 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
552 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
553 myrand, NULL ) != 0 ||
554 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
555 {
556 exit( 1 );
557 }
558
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200559 snprintf( title, sizeof( title ), "ECDHE-%s",
560 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200561 TIME_PUBLIC( title, "handshake",
562 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
563 myrand, NULL );
564 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
565 myrand, NULL ) );
566
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200567 snprintf( title, sizeof( title ), "ECDH-%s",
568 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200569 TIME_PUBLIC( title, "handshake",
570 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
571 myrand, NULL ) );
572 ecdh_free( &ecdh );
573 }
574 }
575#endif
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000576 printf( "\n" );
577
Paul Bakkercce9d772011-11-18 14:26:47 +0000578#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 printf( " Press Enter to exit this program.\n" );
580 fflush( stdout ); getchar();
581#endif
582
583 return( 0 );
584}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200585
Paul Bakker5690efc2011-05-26 13:16:06 +0000586#endif /* POLARSSL_TIMING_C */