blob: 11df50d8051dc1ed8716d70351b5fcb98802339b [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é-Gonnard8271f2f2013-09-17 14:57:55 +020048#include "polarssl/ctr_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"
Paul Bakker02faf452011-11-29 11:23:58 +000051#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakker02faf452011-11-29 11:23:58 +000053#define BUFSIZE 1024
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020054#define HEADER_FORMAT " %-16s : "
55#define TITLE_LEN 17
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020057#if !defined(POLARSSL_TIMING_C)
58int main( int argc, char *argv[] )
59{
60 ((void) argc);
61 ((void) argv);
62
63 printf("POLARSSL_TIMING_C not defined.\n");
64 return( 0 );
65}
66#else
67
Paul Bakkera3d195c2011-11-27 21:07:34 +000068static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
Paul Bakkera3d195c2011-11-27 21:07:34 +000070 size_t use_len;
71 int rnd;
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073 if( rng_state != NULL )
74 rng_state = NULL;
75
Paul Bakkera3d195c2011-11-27 21:07:34 +000076 while( len > 0 )
77 {
78 use_len = len;
79 if( use_len > sizeof(int) )
80 use_len = sizeof(int);
81
82 rnd = rand();
83 memcpy( output, &rnd, use_len );
84 output += use_len;
85 len -= use_len;
86 }
87
88 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000089}
90
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020091#define TIME_AND_TSC( TITLE, CODE ) \
92do { \
93 unsigned long i, j, tsc; \
94 \
95 printf( HEADER_FORMAT, TITLE ); \
96 fflush( stdout ); \
97 \
98 set_alarm( 1 ); \
99 for( i = 1; ! alarmed; i++ ) \
100 { \
101 CODE; \
102 } \
103 \
104 tsc = hardclock(); \
105 for( j = 0; j < 1024; j++ ) \
106 { \
107 CODE; \
108 } \
109 \
110 printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
111 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
112} while( 0 )
113
114#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
115do { \
116 unsigned long i; \
117 int ret; \
118 \
119 printf( HEADER_FORMAT, TITLE ); \
120 fflush( stdout ); \
121 set_alarm( 3 ); \
122 \
123 ret = 0; \
124 for( i = 1; ! alarmed && ! ret ; i++ ) \
125 { \
126 CODE; \
127 } \
128 \
129 if( ret != 0 ) \
130 printf( "FAILED\n" ); \
131 else \
132 printf( "%9lu " TYPE "/s\n", i / 3 ); \
133} while( 0 )
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135unsigned char buf[BUFSIZE];
136
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200137typedef struct {
138 char md4, md5, sha1, sha256, sha512,
139 arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,
140 havege, ctr_drbg,
141 rsa, dhm;
142} todo_list;
143
144#define OPTIONS \
145 "md4, md5, sha1, sha256, sha512,\n" \
146 "arc4, des3, des, aes_cbc, aes_gcm, camellia, blowfish,\n" \
147 "havege, ctr_drbg,\n" \
148 "rsa, dhm.\n"
149
Paul Bakkercce9d772011-11-18 14:26:47 +0000150int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000151{
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200152 int keysize, i;
Paul Bakker5a0aa772009-02-09 22:38:52 +0000153 unsigned char tmp[64];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200154 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200155 todo_list todo;
Paul Bakkercce9d772011-11-18 14:26:47 +0000156
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200157 if( argc == 1 )
158 memset( &todo, 1, sizeof( todo ) );
159 else
160 {
161 memset( &todo, 0, sizeof( todo ) );
162
163 for( i = 1; i < argc; i++ )
164 {
165 if( strcmp( argv[i], "md4" ) == 0 )
166 todo.md4 = 1;
167 else if( strcmp( argv[i], "md5" ) == 0 )
168 todo.md5 = 1;
169 else if( strcmp( argv[i], "sha1" ) == 0 )
170 todo.sha1 = 1;
171 else if( strcmp( argv[i], "sha256" ) == 0 )
172 todo.sha256 = 1;
173 else if( strcmp( argv[i], "sha512" ) == 0 )
174 todo.sha512 = 1;
175 else if( strcmp( argv[i], "arc4" ) == 0 )
176 todo.arc4 = 1;
177 else if( strcmp( argv[i], "des3" ) == 0 )
178 todo.des3 = 1;
179 else if( strcmp( argv[i], "des" ) == 0 )
180 todo.des = 1;
181 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
182 todo.aes_cbc = 1;
183 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
184 todo.aes_gcm = 1;
185 else if( strcmp( argv[i], "camellia" ) == 0 )
186 todo.camellia = 1;
187 else if( strcmp( argv[i], "blowfish" ) == 0 )
188 todo.blowfish = 1;
189 else if( strcmp( argv[i], "havege" ) == 0 )
190 todo.havege = 1;
191 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
192 todo.ctr_drbg = 1;
193 else if( strcmp( argv[i], "rsa" ) == 0 )
194 todo.rsa = 1;
195 else if( strcmp( argv[i], "dhm" ) == 0 )
196 todo.dhm = 1;
197 else
198 {
199 printf( "Unrecognized option: %s\n", argv[i] );
200 printf( "Available options:" OPTIONS );
201 }
202 }
203 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205 printf( "\n" );
206
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200207 memset( buf, 0xAA, sizeof( buf ) );
208
Paul Bakker40e46942009-01-03 21:51:57 +0000209#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200210 if( todo.md4 )
211 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212#endif
213
Paul Bakker40e46942009-01-03 21:51:57 +0000214#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200215 if( todo.md5 )
216 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217#endif
218
Paul Bakker40e46942009-01-03 21:51:57 +0000219#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200220 if( todo.sha1 )
221 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222#endif
223
Paul Bakker9e36f042013-06-30 14:34:05 +0200224#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200225 if( todo.sha256 )
226 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227#endif
228
Paul Bakker9e36f042013-06-30 14:34:05 +0200229#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200230 if( todo.sha512 )
231 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000232#endif
233
Paul Bakker40e46942009-01-03 21:51:57 +0000234#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200235 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200236 {
237 arc4_context arc4;
238 arc4_setup( &arc4, tmp, 32 );
239 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
240 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000241#endif
242
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200243#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200244 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200245 {
246 des3_context des3;
247 des3_set3key_enc( &des3, tmp );
248 TIME_AND_TSC( "3DES",
249 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
250 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200252 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200253 {
254 des_context des;
255 des_setkey_enc( &des, tmp );
256 TIME_AND_TSC( "DES",
257 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
258 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000259#endif
260
Paul Bakker40e46942009-01-03 21:51:57 +0000261#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200262#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200263 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200265 aes_context aes;
266 for( keysize = 128; keysize <= 256; keysize += 64 )
267 {
268 snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200270 memset( buf, 0, sizeof( buf ) );
271 memset( tmp, 0, sizeof( tmp ) );
272 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200274 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200275 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200276 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200278#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000279#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200280 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000281 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200282 gcm_context gcm;
283 for( keysize = 128; keysize <= 256; keysize += 64 )
284 {
285 snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000286
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200287 memset( buf, 0, sizeof( buf ) );
288 memset( tmp, 0, sizeof( tmp ) );
289 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000290
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200291 TIME_AND_TSC( title,
292 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
293 12, NULL, 0, buf, buf, 16, tmp ) );
294 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000295 }
296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000297#endif
298
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200299#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200300 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000301 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200302 camellia_context camellia;
303 for( keysize = 128; keysize <= 256; keysize += 64 )
304 {
305 snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000306
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200307 memset( buf, 0, sizeof( buf ) );
308 memset( tmp, 0, sizeof( tmp ) );
309 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000310
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200311 TIME_AND_TSC( title,
312 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
313 BUFSIZE, tmp, buf, buf ) );
314 }
Paul Bakker38119b12009-01-10 23:31:23 +0000315 }
316#endif
317
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200318#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200319 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000320 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200321 blowfish_context blowfish;
322 for( keysize = 128; keysize <= 256; keysize += 64 )
323 {
324 snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000325
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200326 memset( buf, 0, sizeof( buf ) );
327 memset( tmp, 0, sizeof( tmp ) );
328 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000329
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200330 TIME_AND_TSC( title,
331 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
332 tmp, buf, buf ) );
333 }
Paul Bakker3d58fe82012-07-04 17:15:31 +0000334 }
335#endif
336
Paul Bakker02faf452011-11-29 11:23:58 +0000337#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200338 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200339 {
340 havege_state hs;
341 havege_init( &hs );
342 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
343 }
Paul Bakker02faf452011-11-29 11:23:58 +0000344#endif
345
346#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200347 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200348 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200349 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000350
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200351 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Paul Bakker02faf452011-11-29 11:23:58 +0000352 exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200353 TIME_AND_TSC( "CTR_DRBG (NOPR)",
354 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
355 exit(1) );
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 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
360 TIME_AND_TSC( "CTR_DRBG (PR)",
361 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
362 exit(1) );
363 }
Paul Bakker02faf452011-11-29 11:23:58 +0000364#endif
365
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200366#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200367 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200369 rsa_context rsa;
370 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
371 {
372 snprintf( title, sizeof( title ), "RSA-%d", keysize );
373
374 rsa_init( &rsa, RSA_PKCS_V15, 0 );
375 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
376
377 TIME_PUBLIC( title, " public",
378 buf[0] = 0;
379 ret = rsa_public( &rsa, buf, buf ) );
380
381 TIME_PUBLIC( title, "private",
382 buf[0] = 0;
383 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
384
385 rsa_free( &rsa );
386 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000388#endif
389
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100390#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200391 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100392 {
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200393#define DHM_SIZES 3
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200394 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200395 const char *dhm_P[DHM_SIZES] = {
396 POLARSSL_DHM_RFC5114_MODP_1024_P,
397 POLARSSL_DHM_RFC3526_MODP_2048_P,
398 POLARSSL_DHM_RFC3526_MODP_3072_P,
399 };
400 const char *dhm_G[DHM_SIZES] = {
401 POLARSSL_DHM_RFC5114_MODP_1024_G,
402 POLARSSL_DHM_RFC3526_MODP_2048_G,
403 POLARSSL_DHM_RFC3526_MODP_3072_G,
404 };
405
406 dhm_context dhm;
407 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200408 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 {
410 memset( &dhm, 0, sizeof( dhm_context ) );
411
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200412 mpi_read_string( &dhm.P, 16, dhm_P[i] );
413 mpi_read_string( &dhm.G, 16, dhm_G[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 dhm.len = mpi_size( &dhm.P );
415 dhm_make_public( &dhm, dhm.len, buf, dhm.len, myrand, NULL );
416 mpi_copy( &dhm.GY, &dhm.GX );
417
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200418 snprintf( title, sizeof( title ), "DHM-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 TIME_PUBLIC( title, "handshake",
420 olen = sizeof( buf );
421 ret |= dhm_make_public( &dhm, dhm.len, buf, dhm.len,
422 myrand, NULL );
423 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
424
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200425 snprintf( title, sizeof( title ), "DHM-%d-fixed", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200426 TIME_PUBLIC( title, "handshake",
427 olen = sizeof( buf );
428 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
429
430 dhm_free( &dhm );
431 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100432 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100433#endif
434
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000435 printf( "\n" );
436
Paul Bakkercce9d772011-11-18 14:26:47 +0000437#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 printf( " Press Enter to exit this program.\n" );
439 fflush( stdout ); getchar();
440#endif
441
442 return( 0 );
443}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200444
Paul Bakker5690efc2011-05-26 13:16:06 +0000445#endif /* POLARSSL_TIMING_C */