blob: 735b443a80eac09ca115e967f087fb55c33a7e8c [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 Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000038#if !defined(POLARSSL_TIMING_C)
39int main( void )
40{
41 polarssl_printf("POLARSSL_TIMING_C not defined.\n");
42 return( 0 );
43}
44#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010045
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000046#include <string.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010047
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020048#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakker40e46942009-01-03 21:51:57 +000050#include "polarssl/md4.h"
51#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010052#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000053#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020054#include "polarssl/sha256.h"
55#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000056#include "polarssl/arc4.h"
57#include "polarssl/des.h"
58#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000059#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000060#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000061#include "polarssl/gcm.h"
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +020062#include "polarssl/ccm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020063#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020064#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010065#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000066#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010067#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020068#include "polarssl/ecdsa.h"
69#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010070#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +000072#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
73#include "polarssl/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000074#endif
75
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020076#if defined _MSC_VER && !defined snprintf
77#define snprintf _snprintf
78#endif
79
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000080/*
81 * For heap usage estimates, we need an estimate of the overhead per allocated
82 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
83 * so use that as our baseline.
84 */
85#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
86
87/*
88 * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
89 */
90#define HEAP_SIZE (1u << 16) // 64k
91
Paul Bakker02faf452011-11-29 11:23:58 +000092#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010093#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010094#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000095
Rich Evans85b05ec2015-02-12 11:37:29 +000096#define DHM_SIZES 3
97
98#define OPTIONS \
99 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
100 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
101 "havege, ctr_drbg, hmac_drbg\n" \
102 "rsa, dhm, ecdsa, ecdh.\n"
103
104#if defined(POLARSSL_ERROR_C)
105#define PRINT_ERROR \
106 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
107 polarssl_printf( "FAILED: %s\n", tmp );
108#else
109#define PRINT_ERROR \
110 polarssl_printf( "FAILED: -0x%04x\n", -ret );
111#endif
112
113#define TIME_AND_TSC( TITLE, CODE ) \
114do { \
115 unsigned long i, j, tsc; \
116 \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100117 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000118 fflush( stdout ); \
119 \
120 set_alarm( 1 ); \
121 for( i = 1; ! alarmed; i++ ) \
122 { \
123 CODE; \
124 } \
125 \
126 tsc = hardclock(); \
127 for( j = 0; j < 1024; j++ ) \
128 { \
129 CODE; \
130 } \
131 \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100132 polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", \
133 i * BUFSIZE / 1024, \
134 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000135} while( 0 )
136
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100137#if defined(POLARSSL_ERROR_C)
138#define PRINT_ERROR \
139 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
140 polarssl_printf( "FAILED: %s\n", tmp );
141#else
142#define PRINT_ERROR \
143 polarssl_printf( "FAILED: -0x%04x\n", -ret );
144#endif
145
146#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
147
148#define MEMORY_MEASURE_INIT \
149 size_t max_used, max_blocks, max_bytes; \
150 size_t prv_used, prv_blocks; \
151 memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
152 memory_buffer_alloc_max_reset( );
153
154#define MEMORY_MEASURE_PRINT( title_len ) \
155 memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
156 for( i = 12 - title_len; i != 0; i-- ) polarssl_printf( " " ); \
157 max_used -= prv_used; \
158 max_blocks -= prv_blocks; \
159 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
160 polarssl_printf( "%6u heap bytes", (unsigned) max_bytes );
161
162#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000163#define MEMORY_MEASURE_INIT
164#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100165#endif
166
Rich Evans85b05ec2015-02-12 11:37:29 +0000167#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
168do { \
169 unsigned long i; \
170 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100171 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000172 \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100173 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000174 fflush( stdout ); \
175 set_alarm( 3 ); \
176 \
177 ret = 0; \
178 for( i = 1; ! alarmed && ! ret ; i++ ) \
179 { \
180 CODE; \
181 } \
182 \
183 if( ret != 0 ) \
184 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100185 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000186 } \
187 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100188 { \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100189 polarssl_printf( "%6lu " TYPE "/s", i / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100190 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100191 polarssl_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100192 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000193} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Paul Bakkera3d195c2011-11-27 21:07:34 +0000195static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000196{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000197 size_t use_len;
198 int rnd;
199
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 if( rng_state != NULL )
201 rng_state = NULL;
202
Paul Bakkera3d195c2011-11-27 21:07:34 +0000203 while( len > 0 )
204 {
205 use_len = len;
206 if( use_len > sizeof(int) )
207 use_len = sizeof(int);
208
209 rnd = rand();
210 memcpy( output, &rnd, use_len );
211 output += use_len;
212 len -= use_len;
213 }
214
215 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216}
217
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100218/*
219 * Clear some memory that was used to prepare the context
220 */
221#if defined(POLARSSL_ECP_C)
222void ecp_clear_precomputed( ecp_group *grp )
223{
224 if( grp->T != NULL )
225 {
226 size_t i;
227 for( i = 0; i < grp->T_size; i++ )
228 ecp_point_free( &grp->T[i] );
229 polarssl_free( grp->T );
230 }
231 grp->T = NULL;
232 grp->T_size = 0;
233}
234#else
235#define ecp_clear_precomputed( g )
236#endif
237
Paul Bakker5121ce52009-01-03 21:22:43 +0000238unsigned char buf[BUFSIZE];
239
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200240typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100241 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200242 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100243 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200244 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200245} todo_list;
246
Paul Bakkercce9d772011-11-18 14:26:47 +0000247int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000248{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100249 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200250 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200251 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200252 todo_list todo;
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000253#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100254 unsigned char malloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000255#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000256
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200257 if( argc == 1 )
258 memset( &todo, 1, sizeof( todo ) );
259 else
260 {
261 memset( &todo, 0, sizeof( todo ) );
262
263 for( i = 1; i < argc; i++ )
264 {
265 if( strcmp( argv[i], "md4" ) == 0 )
266 todo.md4 = 1;
267 else if( strcmp( argv[i], "md5" ) == 0 )
268 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100269 else if( strcmp( argv[i], "ripemd160" ) == 0 )
270 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200271 else if( strcmp( argv[i], "sha1" ) == 0 )
272 todo.sha1 = 1;
273 else if( strcmp( argv[i], "sha256" ) == 0 )
274 todo.sha256 = 1;
275 else if( strcmp( argv[i], "sha512" ) == 0 )
276 todo.sha512 = 1;
277 else if( strcmp( argv[i], "arc4" ) == 0 )
278 todo.arc4 = 1;
279 else if( strcmp( argv[i], "des3" ) == 0 )
280 todo.des3 = 1;
281 else if( strcmp( argv[i], "des" ) == 0 )
282 todo.des = 1;
283 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
284 todo.aes_cbc = 1;
285 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
286 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200287 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
288 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200289 else if( strcmp( argv[i], "camellia" ) == 0 )
290 todo.camellia = 1;
291 else if( strcmp( argv[i], "blowfish" ) == 0 )
292 todo.blowfish = 1;
293 else if( strcmp( argv[i], "havege" ) == 0 )
294 todo.havege = 1;
295 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
296 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100297 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
298 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200299 else if( strcmp( argv[i], "rsa" ) == 0 )
300 todo.rsa = 1;
301 else if( strcmp( argv[i], "dhm" ) == 0 )
302 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200303 else if( strcmp( argv[i], "ecdsa" ) == 0 )
304 todo.ecdsa = 1;
305 else if( strcmp( argv[i], "ecdh" ) == 0 )
306 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200307 else
308 {
Rich Evansf90016a2015-01-19 14:26:37 +0000309 polarssl_printf( "Unrecognized option: %s\n", argv[i] );
310 polarssl_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200311 }
312 }
313 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Rich Evansf90016a2015-01-19 14:26:37 +0000315 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000317#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
318 memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
319#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200320 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200321 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200322
Paul Bakker40e46942009-01-03 21:51:57 +0000323#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200324 if( todo.md4 )
325 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326#endif
327
Paul Bakker40e46942009-01-03 21:51:57 +0000328#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200329 if( todo.md5 )
330 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331#endif
332
Paul Bakker61b699e2014-01-22 13:35:29 +0100333#if defined(POLARSSL_RIPEMD160_C)
334 if( todo.ripemd160 )
335 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100336#endif
337
Paul Bakker40e46942009-01-03 21:51:57 +0000338#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200339 if( todo.sha1 )
340 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341#endif
342
Paul Bakker9e36f042013-06-30 14:34:05 +0200343#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200344 if( todo.sha256 )
345 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346#endif
347
Paul Bakker9e36f042013-06-30 14:34:05 +0200348#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200349 if( todo.sha512 )
350 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000351#endif
352
Paul Bakker40e46942009-01-03 21:51:57 +0000353#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200354 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200355 {
356 arc4_context arc4;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200357 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200358 arc4_setup( &arc4, tmp, 32 );
359 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200360 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200361 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000362#endif
363
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200364#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200365 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200366 {
367 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200368 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200369 des3_set3key_enc( &des3, tmp );
370 TIME_AND_TSC( "3DES",
371 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200372 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200373 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200375 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 {
377 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200378 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200379 des_setkey_enc( &des, tmp );
380 TIME_AND_TSC( "DES",
381 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200382 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200383 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000384#endif
385
Paul Bakker40e46942009-01-03 21:51:57 +0000386#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200387#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200388 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100390 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200391 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200392 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200393 for( keysize = 128; keysize <= 256; keysize += 64 )
394 {
Rich Evans783d9d12015-01-30 11:11:57 +0000395 polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000396
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200397 memset( buf, 0, sizeof( buf ) );
398 memset( tmp, 0, sizeof( tmp ) );
399 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200401 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200402 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200403 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200404 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200406#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000407#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200408 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000409 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100410 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200411 gcm_context gcm;
412 for( keysize = 128; keysize <= 256; keysize += 64 )
413 {
Rich Evans783d9d12015-01-30 11:11:57 +0000414 polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000415
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200416 memset( buf, 0, sizeof( buf ) );
417 memset( tmp, 0, sizeof( tmp ) );
418 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000419
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200420 TIME_AND_TSC( title,
421 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
422 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100423
424 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200425 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000426 }
427#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200428#if defined(POLARSSL_CCM_C)
429 if( todo.aes_ccm )
430 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100431 int keysize;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200432 ccm_context ccm;
433 for( keysize = 128; keysize <= 256; keysize += 64 )
434 {
Rich Evans783d9d12015-01-30 11:11:57 +0000435 polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200436
437 memset( buf, 0, sizeof( buf ) );
438 memset( tmp, 0, sizeof( tmp ) );
439 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
440
441 TIME_AND_TSC( title,
442 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
443 12, NULL, 0, buf, buf, tmp, 16 ) );
444
445 ccm_free( &ccm );
446 }
447 }
448#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000449#endif
450
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200451#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200452 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000453 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100454 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200455 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200456 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200457 for( keysize = 128; keysize <= 256; keysize += 64 )
458 {
Rich Evans783d9d12015-01-30 11:11:57 +0000459 polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000460
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200461 memset( buf, 0, sizeof( buf ) );
462 memset( tmp, 0, sizeof( tmp ) );
463 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000464
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200465 TIME_AND_TSC( title,
466 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
467 BUFSIZE, tmp, buf, buf ) );
468 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200469 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000470 }
471#endif
472
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200473#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200474 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000475 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100476 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200477 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200478 blowfish_init( &blowfish );
479
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200480 for( keysize = 128; keysize <= 256; keysize += 64 )
481 {
Rich Evans783d9d12015-01-30 11:11:57 +0000482 polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000483
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200484 memset( buf, 0, sizeof( buf ) );
485 memset( tmp, 0, sizeof( tmp ) );
486 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000487
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200488 TIME_AND_TSC( title,
489 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
490 tmp, buf, buf ) );
491 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200492
493 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000494 }
495#endif
496
Paul Bakker02faf452011-11-29 11:23:58 +0000497#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200498 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200499 {
500 havege_state hs;
501 havege_init( &hs );
502 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200503 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200504 }
Paul Bakker02faf452011-11-29 11:23:58 +0000505#endif
506
507#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200508 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200509 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200510 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000511
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200512 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000513 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200514 TIME_AND_TSC( "CTR_DRBG (NOPR)",
515 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000516 polarssl_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000517
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200518 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000519 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200520 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
521 TIME_AND_TSC( "CTR_DRBG (PR)",
522 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000523 polarssl_exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200524 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200525 }
Paul Bakker02faf452011-11-29 11:23:58 +0000526#endif
527
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100528#if defined(POLARSSL_HMAC_DRBG_C)
529 if( todo.hmac_drbg )
530 {
531 hmac_drbg_context hmac_drbg;
532 const md_info_t *md_info;
533
534#if defined(POLARSSL_SHA1_C)
535 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000536 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100537
538 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000539 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100540 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
541 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000542 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100543 hmac_drbg_free( &hmac_drbg );
544
545 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000546 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100547 hmac_drbg_set_prediction_resistance( &hmac_drbg,
548 POLARSSL_HMAC_DRBG_PR_ON );
549 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
550 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000551 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100552 hmac_drbg_free( &hmac_drbg );
553#endif
554
555#if defined(POLARSSL_SHA256_C)
556 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000557 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100558
559 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000560 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100561 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
562 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000563 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100564 hmac_drbg_free( &hmac_drbg );
565
566 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000567 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100568 hmac_drbg_set_prediction_resistance( &hmac_drbg,
569 POLARSSL_HMAC_DRBG_PR_ON );
570 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
571 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000572 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100573 hmac_drbg_free( &hmac_drbg );
574#endif
575 }
576#endif
577
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200578#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200579 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100581 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200582 rsa_context rsa;
583 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
584 {
Rich Evans783d9d12015-01-30 11:11:57 +0000585 polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200586
587 rsa_init( &rsa, RSA_PKCS_V15, 0 );
588 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
589
590 TIME_PUBLIC( title, " public",
591 buf[0] = 0;
592 ret = rsa_public( &rsa, buf, buf ) );
593
594 TIME_PUBLIC( title, "private",
595 buf[0] = 0;
596 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
597
598 rsa_free( &rsa );
599 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000601#endif
602
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100603#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200604 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100605 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200606 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200607 const char *dhm_P[DHM_SIZES] = {
608 POLARSSL_DHM_RFC5114_MODP_1024_P,
609 POLARSSL_DHM_RFC3526_MODP_2048_P,
610 POLARSSL_DHM_RFC3526_MODP_3072_P,
611 };
612 const char *dhm_G[DHM_SIZES] = {
613 POLARSSL_DHM_RFC5114_MODP_1024_G,
614 POLARSSL_DHM_RFC3526_MODP_2048_G,
615 POLARSSL_DHM_RFC3526_MODP_3072_G,
616 };
617
618 dhm_context dhm;
619 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200620 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200621 {
Paul Bakkera317a982014-06-18 16:44:11 +0200622 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200623
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200624 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
625 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
626 {
Rich Evans77d36382015-01-30 12:12:11 +0000627 polarssl_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200628 }
629
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200630 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100631 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200632 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000633 polarssl_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200634
Rich Evans783d9d12015-01-30 11:11:57 +0000635 polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200636 TIME_PUBLIC( title, "handshake",
637 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100638 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200639 myrand, NULL );
640 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
641
Rich Evans783d9d12015-01-30 11:11:57 +0000642 polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200643 TIME_PUBLIC( title, "handshake",
644 olen = sizeof( buf );
645 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
646
647 dhm_free( &dhm );
648 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100649 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100650#endif
651
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200652#if defined(POLARSSL_ECDSA_C)
653 if( todo.ecdsa )
654 {
655 ecdsa_context ecdsa;
656 const ecp_curve_info *curve_info;
657 size_t sig_len;
658
659 memset( buf, 0x2A, sizeof( buf ) );
660
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200661 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200662 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
663 curve_info++ )
664 {
665 ecdsa_init( &ecdsa );
666
667 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000668 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100669 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200670
Rich Evans783d9d12015-01-30 11:11:57 +0000671 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200672 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200673 TIME_PUBLIC( title, "sign",
674 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200675 tmp, &sig_len, myrand, NULL ) );
676
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100677 ecdsa_free( &ecdsa );
678 }
679
680 for( curve_info = ecp_curve_list();
681 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
682 curve_info++ )
683 {
684 ecdsa_init( &ecdsa );
685
686 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
687 ecdsa_write_signature( &ecdsa, buf, curve_info->size,
688 tmp, &sig_len, myrand, NULL ) != 0 )
689 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000690 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100691 }
692 ecp_clear_precomputed( &ecdsa.grp );
693
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000694 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100695 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200696 TIME_PUBLIC( title, "verify",
697 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
698 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200699
700 ecdsa_free( &ecdsa );
701 }
702 }
703#endif
704
705#if defined(POLARSSL_ECDH_C)
706 if( todo.ecdh )
707 {
708 ecdh_context ecdh;
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000709#if defined(POLARSSL_ECP_DP_M255_ENABLED)
710 mpi z;
711#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200712 const ecp_curve_info *curve_info;
713 size_t olen;
714
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200715 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200716 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
717 curve_info++ )
718 {
719 ecdh_init( &ecdh );
720
721 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
722 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
723 myrand, NULL ) != 0 ||
724 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
725 {
Rich Evans77d36382015-01-30 12:12:11 +0000726 polarssl_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200727 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100728 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200729
Rich Evans783d9d12015-01-30 11:11:57 +0000730 polarssl_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200731 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200732 TIME_PUBLIC( title, "handshake",
733 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
734 myrand, NULL );
735 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
736 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100737 ecdh_free( &ecdh );
738 }
739
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000740 /* Curve25519 needs to be handled separately */
741#if defined(POLARSSL_ECP_DP_M255_ENABLED)
742 ecdh_init( &ecdh );
743 mpi_init( &z );
744
745 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
746 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
747 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000748 polarssl_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000749 }
750
751 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
752 ret |= ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
753 myrand, NULL );
754 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
755 myrand, NULL ) );
756
757 ecdh_free( &ecdh );
758 mpi_free( &z );
759#endif
760
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100761 for( curve_info = ecp_curve_list();
762 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
763 curve_info++ )
764 {
765 ecdh_init( &ecdh );
766
767 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
768 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
769 myrand, NULL ) != 0 ||
770 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
771 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
772 myrand, NULL ) != 0 )
773 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000774 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100775 }
776 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200777
Rich Evans783d9d12015-01-30 11:11:57 +0000778 polarssl_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200779 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200780 TIME_PUBLIC( title, "handshake",
781 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
782 myrand, NULL ) );
783 ecdh_free( &ecdh );
784 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000785
786 /* Curve25519 needs to be handled separately */
787#if defined(POLARSSL_ECP_DP_M255_ENABLED)
788 ecdh_init( &ecdh );
789 mpi_init( &z );
790
791 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
792 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
793 myrand, NULL ) != 0 ||
794 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
795 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000796 polarssl_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000797 }
798
799 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
800 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
801 myrand, NULL ) );
802
803 ecdh_free( &ecdh );
804 mpi_free( &z );
805#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200806 }
807#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100808
Rich Evansf90016a2015-01-19 14:26:37 +0000809 polarssl_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000810
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000811#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000812 memory_buffer_alloc_free();
813#endif
814
Paul Bakkercce9d772011-11-18 14:26:47 +0000815#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000816 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000817 fflush( stdout ); getchar();
818#endif
819
820 return( 0 );
821}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200822
Paul Bakker5690efc2011-05-26 13:16:06 +0000823#endif /* POLARSSL_TIMING_C */