blob: 26db68390f02a22d7351d3f74727393ff40aa1bc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Benchmark demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evans77d36382015-01-30 12:12:11 +000033#define polarssl_exit exit
Rich Evansb92965b2015-01-30 11:11:57 +000034#define polarssl_printf printf
35#define polarssl_snprintf snprintf
Rich Evans012acfc2015-01-30 12:12:11 +000036#define polarssl_exit exit
Rich Evansf90016a2015-01-19 14:26:37 +000037#endif
38
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010039/*
40 * For heap usage estimates, we need an estimate of the overhead per allocated
41 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
42 * so use that as our baseline.
43 */
44#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
45
46/*
47 * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
48 */
49#define HEAP_SIZE (1u << 16) // 64k
50
Rich Evans18b78c72015-02-11 14:06:19 +000051#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020052#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakker40e46942009-01-03 21:51:57 +000054#include "polarssl/md4.h"
55#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010056#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000057#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020058#include "polarssl/sha256.h"
59#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000060#include "polarssl/arc4.h"
61#include "polarssl/des.h"
62#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000063#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000064#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000065#include "polarssl/gcm.h"
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +020066#include "polarssl/ccm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020067#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020068#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010069#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000070#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010071#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020072#include "polarssl/ecdsa.h"
73#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010074#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +000076#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
77#include "polarssl/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000078#endif
79
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020080#if defined _MSC_VER && !defined snprintf
81#define snprintf _snprintf
82#endif
83
Paul Bakker02faf452011-11-29 11:23:58 +000084#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010085#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010086#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000087
Rich Evans85b05ec2015-02-12 11:37:29 +000088#define DHM_SIZES 3
89
90#define OPTIONS \
91 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
92 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
93 "havege, ctr_drbg, hmac_drbg\n" \
94 "rsa, dhm, ecdsa, ecdh.\n"
95
96#if defined(POLARSSL_ERROR_C)
97#define PRINT_ERROR \
98 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
99 polarssl_printf( "FAILED: %s\n", tmp );
100#else
101#define PRINT_ERROR \
102 polarssl_printf( "FAILED: -0x%04x\n", -ret );
103#endif
104
105#define TIME_AND_TSC( TITLE, CODE ) \
106do { \
107 unsigned long i, j, tsc; \
108 \
109 polarssl_printf( HEADER_FORMAT, TITLE ); \
110 fflush( stdout ); \
111 \
112 set_alarm( 1 ); \
113 for( i = 1; ! alarmed; i++ ) \
114 { \
115 CODE; \
116 } \
117 \
118 tsc = hardclock(); \
119 for( j = 0; j < 1024; j++ ) \
120 { \
121 CODE; \
122 } \
123 \
124 polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", i * BUFSIZE / 1024, \
125 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
126} while( 0 )
127
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100128#if defined(POLARSSL_ERROR_C)
129#define PRINT_ERROR \
130 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
131 polarssl_printf( "FAILED: %s\n", tmp );
132#else
133#define PRINT_ERROR \
134 polarssl_printf( "FAILED: -0x%04x\n", -ret );
135#endif
136
137#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
138
139#define MEMORY_MEASURE_INIT \
140 size_t max_used, max_blocks, max_bytes; \
141 size_t prv_used, prv_blocks; \
142 memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
143 memory_buffer_alloc_max_reset( );
144
145#define MEMORY_MEASURE_PRINT( title_len ) \
146 memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
147 for( i = 12 - title_len; i != 0; i-- ) polarssl_printf( " " ); \
148 max_used -= prv_used; \
149 max_blocks -= prv_blocks; \
150 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
151 polarssl_printf( "%6u heap bytes", (unsigned) max_bytes );
152
153#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000154#define MEMORY_MEASURE_INIT
155#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100156#endif
157
Rich Evans85b05ec2015-02-12 11:37:29 +0000158#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
159do { \
160 unsigned long i; \
161 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100162 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000163 \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100164 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000165 fflush( stdout ); \
166 set_alarm( 3 ); \
167 \
168 ret = 0; \
169 for( i = 1; ! alarmed && ! ret ; i++ ) \
170 { \
171 CODE; \
172 } \
173 \
174 if( ret != 0 ) \
175 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100176 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000177 } \
178 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100179 { \
180 polarssl_printf( "%6lu " TYPE "/s", i / 3 ); \
181 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
182 polarssl_printf( "\n" ); \
183 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000184} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200186#if !defined(POLARSSL_TIMING_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000187int main( void )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200188{
Rich Evansf90016a2015-01-19 14:26:37 +0000189 polarssl_printf("POLARSSL_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200190 return( 0 );
191}
192#else
Paul Bakkera3d195c2011-11-27 21:07:34 +0000193static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000195 size_t use_len;
196 int rnd;
197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 if( rng_state != NULL )
199 rng_state = NULL;
200
Paul Bakkera3d195c2011-11-27 21:07:34 +0000201 while( len > 0 )
202 {
203 use_len = len;
204 if( use_len > sizeof(int) )
205 use_len = sizeof(int);
206
207 rnd = rand();
208 memcpy( output, &rnd, use_len );
209 output += use_len;
210 len -= use_len;
211 }
212
213 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214}
215
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100216/*
217 * Clear some memory that was used to prepare the context
218 */
219#if defined(POLARSSL_ECP_C)
220void ecp_clear_precomputed( ecp_group *grp )
221{
222 if( grp->T != NULL )
223 {
224 size_t i;
225 for( i = 0; i < grp->T_size; i++ )
226 ecp_point_free( &grp->T[i] );
227 polarssl_free( grp->T );
228 }
229 grp->T = NULL;
230 grp->T_size = 0;
231}
232#else
233#define ecp_clear_precomputed( g )
234#endif
235
Paul Bakker5121ce52009-01-03 21:22:43 +0000236unsigned char buf[BUFSIZE];
237
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200238typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100239 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200240 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100241 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200242 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200243} todo_list;
244
Paul Bakkercce9d772011-11-18 14:26:47 +0000245int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000246{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100247 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200248 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200249 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200250 todo_list todo;
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000251#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100252 unsigned char malloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000253#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000254
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200255 if( argc == 1 )
256 memset( &todo, 1, sizeof( todo ) );
257 else
258 {
259 memset( &todo, 0, sizeof( todo ) );
260
261 for( i = 1; i < argc; i++ )
262 {
263 if( strcmp( argv[i], "md4" ) == 0 )
264 todo.md4 = 1;
265 else if( strcmp( argv[i], "md5" ) == 0 )
266 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100267 else if( strcmp( argv[i], "ripemd160" ) == 0 )
268 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200269 else if( strcmp( argv[i], "sha1" ) == 0 )
270 todo.sha1 = 1;
271 else if( strcmp( argv[i], "sha256" ) == 0 )
272 todo.sha256 = 1;
273 else if( strcmp( argv[i], "sha512" ) == 0 )
274 todo.sha512 = 1;
275 else if( strcmp( argv[i], "arc4" ) == 0 )
276 todo.arc4 = 1;
277 else if( strcmp( argv[i], "des3" ) == 0 )
278 todo.des3 = 1;
279 else if( strcmp( argv[i], "des" ) == 0 )
280 todo.des = 1;
281 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
282 todo.aes_cbc = 1;
283 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
284 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200285 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
286 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200287 else if( strcmp( argv[i], "camellia" ) == 0 )
288 todo.camellia = 1;
289 else if( strcmp( argv[i], "blowfish" ) == 0 )
290 todo.blowfish = 1;
291 else if( strcmp( argv[i], "havege" ) == 0 )
292 todo.havege = 1;
293 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
294 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100295 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
296 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200297 else if( strcmp( argv[i], "rsa" ) == 0 )
298 todo.rsa = 1;
299 else if( strcmp( argv[i], "dhm" ) == 0 )
300 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200301 else if( strcmp( argv[i], "ecdsa" ) == 0 )
302 todo.ecdsa = 1;
303 else if( strcmp( argv[i], "ecdh" ) == 0 )
304 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200305 else
306 {
Rich Evansf90016a2015-01-19 14:26:37 +0000307 polarssl_printf( "Unrecognized option: %s\n", argv[i] );
308 polarssl_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200309 }
310 }
311 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Rich Evansf90016a2015-01-19 14:26:37 +0000313 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000315#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
316 memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
317#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200318 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200319 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200320
Paul Bakker40e46942009-01-03 21:51:57 +0000321#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200322 if( todo.md4 )
323 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324#endif
325
Paul Bakker40e46942009-01-03 21:51:57 +0000326#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 if( todo.md5 )
328 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329#endif
330
Paul Bakker61b699e2014-01-22 13:35:29 +0100331#if defined(POLARSSL_RIPEMD160_C)
332 if( todo.ripemd160 )
333 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100334#endif
335
Paul Bakker40e46942009-01-03 21:51:57 +0000336#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200337 if( todo.sha1 )
338 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339#endif
340
Paul Bakker9e36f042013-06-30 14:34:05 +0200341#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 if( todo.sha256 )
343 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344#endif
345
Paul Bakker9e36f042013-06-30 14:34:05 +0200346#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200347 if( todo.sha512 )
348 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000349#endif
350
Paul Bakker40e46942009-01-03 21:51:57 +0000351#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200352 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200353 {
354 arc4_context arc4;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200355 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200356 arc4_setup( &arc4, tmp, 32 );
357 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200358 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200359 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000360#endif
361
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200362#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200363 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200364 {
365 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200366 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200367 des3_set3key_enc( &des3, tmp );
368 TIME_AND_TSC( "3DES",
369 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200370 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200371 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200373 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200374 {
375 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200376 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200377 des_setkey_enc( &des, tmp );
378 TIME_AND_TSC( "DES",
379 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200380 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200381 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000382#endif
383
Paul Bakker40e46942009-01-03 21:51:57 +0000384#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200385#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200386 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100388 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200389 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200390 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200391 for( keysize = 128; keysize <= 256; keysize += 64 )
392 {
Rich Evans783d9d12015-01-30 11:11:57 +0000393 polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200395 memset( buf, 0, sizeof( buf ) );
396 memset( tmp, 0, sizeof( tmp ) );
397 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200399 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200400 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200401 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200402 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200404#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000405#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200406 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000407 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100408 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409 gcm_context gcm;
410 for( keysize = 128; keysize <= 256; keysize += 64 )
411 {
Rich Evans783d9d12015-01-30 11:11:57 +0000412 polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000413
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 memset( buf, 0, sizeof( buf ) );
415 memset( tmp, 0, sizeof( tmp ) );
416 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000417
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200418 TIME_AND_TSC( title,
419 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
420 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100421
422 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000424 }
425#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200426#if defined(POLARSSL_CCM_C)
427 if( todo.aes_ccm )
428 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100429 int keysize;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200430 ccm_context ccm;
431 for( keysize = 128; keysize <= 256; keysize += 64 )
432 {
Rich Evans783d9d12015-01-30 11:11:57 +0000433 polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200434
435 memset( buf, 0, sizeof( buf ) );
436 memset( tmp, 0, sizeof( tmp ) );
437 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
438
439 TIME_AND_TSC( title,
440 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
441 12, NULL, 0, buf, buf, tmp, 16 ) );
442
443 ccm_free( &ccm );
444 }
445 }
446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000447#endif
448
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200449#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200450 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000451 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100452 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200453 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200454 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200455 for( keysize = 128; keysize <= 256; keysize += 64 )
456 {
Rich Evans783d9d12015-01-30 11:11:57 +0000457 polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000458
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200459 memset( buf, 0, sizeof( buf ) );
460 memset( tmp, 0, sizeof( tmp ) );
461 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000462
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200463 TIME_AND_TSC( title,
464 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
465 BUFSIZE, tmp, buf, buf ) );
466 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200467 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000468 }
469#endif
470
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200471#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200472 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000473 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100474 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200475 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200476 blowfish_init( &blowfish );
477
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200478 for( keysize = 128; keysize <= 256; keysize += 64 )
479 {
Rich Evans783d9d12015-01-30 11:11:57 +0000480 polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000481
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200482 memset( buf, 0, sizeof( buf ) );
483 memset( tmp, 0, sizeof( tmp ) );
484 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000485
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200486 TIME_AND_TSC( title,
487 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
488 tmp, buf, buf ) );
489 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200490
491 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000492 }
493#endif
494
Paul Bakker02faf452011-11-29 11:23:58 +0000495#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200496 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200497 {
498 havege_state hs;
499 havege_init( &hs );
500 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200501 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200502 }
Paul Bakker02faf452011-11-29 11:23:58 +0000503#endif
504
505#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200506 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200507 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200508 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000509
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200510 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000511 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200512 TIME_AND_TSC( "CTR_DRBG (NOPR)",
513 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000514 polarssl_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000515
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200516 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000517 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200518 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
519 TIME_AND_TSC( "CTR_DRBG (PR)",
520 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000521 polarssl_exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200522 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 }
Paul Bakker02faf452011-11-29 11:23:58 +0000524#endif
525
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100526#if defined(POLARSSL_HMAC_DRBG_C)
527 if( todo.hmac_drbg )
528 {
529 hmac_drbg_context hmac_drbg;
530 const md_info_t *md_info;
531
532#if defined(POLARSSL_SHA1_C)
533 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000534 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100535
536 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000537 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100538 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
539 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000540 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100541 hmac_drbg_free( &hmac_drbg );
542
543 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000544 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100545 hmac_drbg_set_prediction_resistance( &hmac_drbg,
546 POLARSSL_HMAC_DRBG_PR_ON );
547 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
548 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000549 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100550 hmac_drbg_free( &hmac_drbg );
551#endif
552
553#if defined(POLARSSL_SHA256_C)
554 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000555 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100556
557 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000558 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100559 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
560 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000561 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100562 hmac_drbg_free( &hmac_drbg );
563
564 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000565 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100566 hmac_drbg_set_prediction_resistance( &hmac_drbg,
567 POLARSSL_HMAC_DRBG_PR_ON );
568 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
569 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000570 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100571 hmac_drbg_free( &hmac_drbg );
572#endif
573 }
574#endif
575
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200576#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200577 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100579 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200580 rsa_context rsa;
581 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
582 {
Rich Evans783d9d12015-01-30 11:11:57 +0000583 polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200584
585 rsa_init( &rsa, RSA_PKCS_V15, 0 );
586 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
587
588 TIME_PUBLIC( title, " public",
589 buf[0] = 0;
590 ret = rsa_public( &rsa, buf, buf ) );
591
592 TIME_PUBLIC( title, "private",
593 buf[0] = 0;
594 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
595
596 rsa_free( &rsa );
597 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000599#endif
600
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100601#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200602 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100603 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200604 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200605 const char *dhm_P[DHM_SIZES] = {
606 POLARSSL_DHM_RFC5114_MODP_1024_P,
607 POLARSSL_DHM_RFC3526_MODP_2048_P,
608 POLARSSL_DHM_RFC3526_MODP_3072_P,
609 };
610 const char *dhm_G[DHM_SIZES] = {
611 POLARSSL_DHM_RFC5114_MODP_1024_G,
612 POLARSSL_DHM_RFC3526_MODP_2048_G,
613 POLARSSL_DHM_RFC3526_MODP_3072_G,
614 };
615
616 dhm_context dhm;
617 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200618 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200619 {
Paul Bakkera317a982014-06-18 16:44:11 +0200620 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200621
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200622 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
623 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
624 {
Rich Evans77d36382015-01-30 12:12:11 +0000625 polarssl_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200626 }
627
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200628 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100629 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200630 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000631 polarssl_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200632
Rich Evans783d9d12015-01-30 11:11:57 +0000633 polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200634 TIME_PUBLIC( title, "handshake",
635 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100636 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200637 myrand, NULL );
638 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
639
Rich Evans783d9d12015-01-30 11:11:57 +0000640 polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200641 TIME_PUBLIC( title, "handshake",
642 olen = sizeof( buf );
643 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
644
645 dhm_free( &dhm );
646 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100647 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100648#endif
649
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200650#if defined(POLARSSL_ECDSA_C)
651 if( todo.ecdsa )
652 {
653 ecdsa_context ecdsa;
654 const ecp_curve_info *curve_info;
655 size_t sig_len;
656
657 memset( buf, 0x2A, sizeof( buf ) );
658
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200659 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200660 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
661 curve_info++ )
662 {
663 ecdsa_init( &ecdsa );
664
665 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000666 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100667 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200668
Rich Evans783d9d12015-01-30 11:11:57 +0000669 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200670 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200671 TIME_PUBLIC( title, "sign",
672 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200673 tmp, &sig_len, myrand, NULL ) );
674
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100675 ecdsa_free( &ecdsa );
676 }
677
678 for( curve_info = ecp_curve_list();
679 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
680 curve_info++ )
681 {
682 ecdsa_init( &ecdsa );
683
684 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
685 ecdsa_write_signature( &ecdsa, buf, curve_info->size,
686 tmp, &sig_len, myrand, NULL ) != 0 )
687 {
688 exit( 1 );
689 }
690 ecp_clear_precomputed( &ecdsa.grp );
691
692 snprintf( title, sizeof( title ), "ECDSA-%s",
693 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200694 TIME_PUBLIC( title, "verify",
695 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
696 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200697
698 ecdsa_free( &ecdsa );
699 }
700 }
701#endif
702
703#if defined(POLARSSL_ECDH_C)
704 if( todo.ecdh )
705 {
706 ecdh_context ecdh;
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000707#if defined(POLARSSL_ECP_DP_M255_ENABLED)
708 mpi z;
709#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200710 const ecp_curve_info *curve_info;
711 size_t olen;
712
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200713 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200714 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
715 curve_info++ )
716 {
717 ecdh_init( &ecdh );
718
719 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
720 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
721 myrand, NULL ) != 0 ||
722 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
723 {
Rich Evans77d36382015-01-30 12:12:11 +0000724 polarssl_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200725 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100726 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200727
Rich Evans783d9d12015-01-30 11:11:57 +0000728 polarssl_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200729 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200730 TIME_PUBLIC( title, "handshake",
731 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
732 myrand, NULL );
733 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
734 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100735 ecdh_free( &ecdh );
736 }
737
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000738 /* Curve25519 needs to be handled separately */
739#if defined(POLARSSL_ECP_DP_M255_ENABLED)
740 ecdh_init( &ecdh );
741 mpi_init( &z );
742
743 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
744 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
745 {
746 exit( 1 );
747 }
748
749 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
750 ret |= ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
751 myrand, NULL );
752 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
753 myrand, NULL ) );
754
755 ecdh_free( &ecdh );
756 mpi_free( &z );
757#endif
758
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100759 for( curve_info = ecp_curve_list();
760 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
761 curve_info++ )
762 {
763 ecdh_init( &ecdh );
764
765 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
766 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
767 myrand, NULL ) != 0 ||
768 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
769 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
770 myrand, NULL ) != 0 )
771 {
772 exit( 1 );
773 }
774 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200775
Rich Evans783d9d12015-01-30 11:11:57 +0000776 polarssl_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200777 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200778 TIME_PUBLIC( title, "handshake",
779 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
780 myrand, NULL ) );
781 ecdh_free( &ecdh );
782 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000783
784 /* Curve25519 needs to be handled separately */
785#if defined(POLARSSL_ECP_DP_M255_ENABLED)
786 ecdh_init( &ecdh );
787 mpi_init( &z );
788
789 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
790 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
791 myrand, NULL ) != 0 ||
792 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
793 {
794 exit( 1 );
795 }
796
797 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
798 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
799 myrand, NULL ) );
800
801 ecdh_free( &ecdh );
802 mpi_free( &z );
803#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200804 }
805#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100806
Rich Evansf90016a2015-01-19 14:26:37 +0000807 polarssl_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000808
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000809#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000810 memory_buffer_alloc_free();
811#endif
812
Paul Bakkercce9d772011-11-18 14:26:47 +0000813#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000814 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 fflush( stdout ); getchar();
816#endif
817
818 return( 0 );
819}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200820
Paul Bakker5690efc2011-05-26 13:16:06 +0000821#endif /* POLARSSL_TIMING_C */