blob: 5dff3e98c64d4b4309d56bc3adff384a3c6bdcb4 [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
Manuel Pégourié-Gonnardc3e33952015-02-16 18:46:20 +000036#define polarssl_free free
Rich Evansf90016a2015-01-19 14:26:37 +000037#endif
38
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000039#if !defined(POLARSSL_TIMING_C)
40int main( void )
41{
42 polarssl_printf("POLARSSL_TIMING_C not defined.\n");
43 return( 0 );
44}
45#else
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010046
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000047#include <string.h>
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +010048
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020049#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakker40e46942009-01-03 21:51:57 +000051#include "polarssl/md4.h"
52#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010053#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000054#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020055#include "polarssl/sha256.h"
56#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000057#include "polarssl/arc4.h"
58#include "polarssl/des.h"
59#include "polarssl/aes.h"
Paul Bakker3d58fe82012-07-04 17:15:31 +000060#include "polarssl/blowfish.h"
Paul Bakker38119b12009-01-10 23:31:23 +000061#include "polarssl/camellia.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000062#include "polarssl/gcm.h"
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +020063#include "polarssl/ccm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020064#include "polarssl/havege.h"
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +020065#include "polarssl/ctr_drbg.h"
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010066#include "polarssl/hmac_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000067#include "polarssl/rsa.h"
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +010068#include "polarssl/dhm.h"
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +020069#include "polarssl/ecdsa.h"
70#include "polarssl/ecdh.h"
Gergely Budaia5d336b2014-01-27 23:27:06 +010071#include "polarssl/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +000073#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
74#include "polarssl/memory_buffer_alloc.h"
Rich Evans18b78c72015-02-11 14:06:19 +000075#endif
76
Manuel Pégourié-Gonnard2f77ce32013-10-03 11:59:57 +020077#if defined _MSC_VER && !defined snprintf
78#define snprintf _snprintf
79#endif
80
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +000081/*
82 * For heap usage estimates, we need an estimate of the overhead per allocated
83 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
84 * so use that as our baseline.
85 */
86#define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
87
88/*
89 * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
90 */
91#define HEAP_SIZE (1u << 16) // 64k
92
Paul Bakker02faf452011-11-29 11:23:58 +000093#define BUFSIZE 1024
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +010094#define HEADER_FORMAT " %-24s : "
Gergely Budaia5d336b2014-01-27 23:27:06 +010095#define TITLE_LEN 25
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000096
Rich Evans85b05ec2015-02-12 11:37:29 +000097#define DHM_SIZES 3
98
99#define OPTIONS \
100 "md4, md5, ripemd160, sha1, sha256, sha512,\n" \
101 "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\n" \
102 "havege, ctr_drbg, hmac_drbg\n" \
103 "rsa, dhm, ecdsa, ecdh.\n"
104
105#if defined(POLARSSL_ERROR_C)
106#define PRINT_ERROR \
107 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
108 polarssl_printf( "FAILED: %s\n", tmp );
109#else
110#define PRINT_ERROR \
111 polarssl_printf( "FAILED: -0x%04x\n", -ret );
112#endif
113
114#define TIME_AND_TSC( TITLE, CODE ) \
115do { \
116 unsigned long i, j, tsc; \
117 \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100118 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000119 fflush( stdout ); \
120 \
121 set_alarm( 1 ); \
122 for( i = 1; ! alarmed; i++ ) \
123 { \
124 CODE; \
125 } \
126 \
127 tsc = hardclock(); \
128 for( j = 0; j < 1024; j++ ) \
129 { \
130 CODE; \
131 } \
132 \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100133 polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", \
134 i * BUFSIZE / 1024, \
135 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000136} while( 0 )
137
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100138#if defined(POLARSSL_ERROR_C)
139#define PRINT_ERROR \
140 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
141 polarssl_printf( "FAILED: %s\n", tmp );
142#else
143#define PRINT_ERROR \
144 polarssl_printf( "FAILED: -0x%04x\n", -ret );
145#endif
146
147#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
148
149#define MEMORY_MEASURE_INIT \
150 size_t max_used, max_blocks, max_bytes; \
151 size_t prv_used, prv_blocks; \
152 memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
153 memory_buffer_alloc_max_reset( );
154
155#define MEMORY_MEASURE_PRINT( title_len ) \
156 memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
157 for( i = 12 - title_len; i != 0; i-- ) polarssl_printf( " " ); \
158 max_used -= prv_used; \
159 max_blocks -= prv_blocks; \
160 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
161 polarssl_printf( "%6u heap bytes", (unsigned) max_bytes );
162
163#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000164#define MEMORY_MEASURE_INIT
165#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100166#endif
167
Rich Evans85b05ec2015-02-12 11:37:29 +0000168#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
169do { \
170 unsigned long i; \
171 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100172 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000173 \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100174 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000175 fflush( stdout ); \
176 set_alarm( 3 ); \
177 \
178 ret = 0; \
179 for( i = 1; ! alarmed && ! ret ; i++ ) \
180 { \
181 CODE; \
182 } \
183 \
184 if( ret != 0 ) \
185 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100186 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000187 } \
188 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100189 { \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100190 polarssl_printf( "%6lu " TYPE "/s", i / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100191 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100192 polarssl_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100193 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000194} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000197{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000198 size_t use_len;
199 int rnd;
200
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 if( rng_state != NULL )
202 rng_state = NULL;
203
Paul Bakkera3d195c2011-11-27 21:07:34 +0000204 while( len > 0 )
205 {
206 use_len = len;
207 if( use_len > sizeof(int) )
208 use_len = sizeof(int);
209
210 rnd = rand();
211 memcpy( output, &rnd, use_len );
212 output += use_len;
213 len -= use_len;
214 }
215
216 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217}
218
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100219/*
220 * Clear some memory that was used to prepare the context
221 */
222#if defined(POLARSSL_ECP_C)
223void ecp_clear_precomputed( ecp_group *grp )
224{
225 if( grp->T != NULL )
226 {
227 size_t i;
228 for( i = 0; i < grp->T_size; i++ )
229 ecp_point_free( &grp->T[i] );
230 polarssl_free( grp->T );
231 }
232 grp->T = NULL;
233 grp->T_size = 0;
234}
235#else
236#define ecp_clear_precomputed( g )
237#endif
238
Paul Bakker5121ce52009-01-03 21:22:43 +0000239unsigned char buf[BUFSIZE];
240
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200241typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100242 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200243 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100244 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200245 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200246} todo_list;
247
Paul Bakkercce9d772011-11-18 14:26:47 +0000248int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000249{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100250 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200251 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200252 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200253 todo_list todo;
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000254#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100255 unsigned char malloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000256#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000257
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000258 if( argc <= 1 )
259 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200260 memset( &todo, 1, sizeof( todo ) );
Manuel Pégourié-Gonnardc439e7b2015-03-03 13:12:00 +0000261 }
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200262 else
263 {
264 memset( &todo, 0, sizeof( todo ) );
265
266 for( i = 1; i < argc; i++ )
267 {
268 if( strcmp( argv[i], "md4" ) == 0 )
269 todo.md4 = 1;
270 else if( strcmp( argv[i], "md5" ) == 0 )
271 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100272 else if( strcmp( argv[i], "ripemd160" ) == 0 )
273 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200274 else if( strcmp( argv[i], "sha1" ) == 0 )
275 todo.sha1 = 1;
276 else if( strcmp( argv[i], "sha256" ) == 0 )
277 todo.sha256 = 1;
278 else if( strcmp( argv[i], "sha512" ) == 0 )
279 todo.sha512 = 1;
280 else if( strcmp( argv[i], "arc4" ) == 0 )
281 todo.arc4 = 1;
282 else if( strcmp( argv[i], "des3" ) == 0 )
283 todo.des3 = 1;
284 else if( strcmp( argv[i], "des" ) == 0 )
285 todo.des = 1;
286 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
287 todo.aes_cbc = 1;
288 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
289 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200290 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
291 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200292 else if( strcmp( argv[i], "camellia" ) == 0 )
293 todo.camellia = 1;
294 else if( strcmp( argv[i], "blowfish" ) == 0 )
295 todo.blowfish = 1;
296 else if( strcmp( argv[i], "havege" ) == 0 )
297 todo.havege = 1;
298 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
299 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100300 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
301 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200302 else if( strcmp( argv[i], "rsa" ) == 0 )
303 todo.rsa = 1;
304 else if( strcmp( argv[i], "dhm" ) == 0 )
305 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200306 else if( strcmp( argv[i], "ecdsa" ) == 0 )
307 todo.ecdsa = 1;
308 else if( strcmp( argv[i], "ecdh" ) == 0 )
309 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200310 else
311 {
Rich Evansf90016a2015-01-19 14:26:37 +0000312 polarssl_printf( "Unrecognized option: %s\n", argv[i] );
313 polarssl_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200314 }
315 }
316 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Rich Evansf90016a2015-01-19 14:26:37 +0000318 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000320#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
321 memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
322#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200323 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200324 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200325
Paul Bakker40e46942009-01-03 21:51:57 +0000326#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200327 if( todo.md4 )
328 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329#endif
330
Paul Bakker40e46942009-01-03 21:51:57 +0000331#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200332 if( todo.md5 )
333 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334#endif
335
Paul Bakker61b699e2014-01-22 13:35:29 +0100336#if defined(POLARSSL_RIPEMD160_C)
337 if( todo.ripemd160 )
338 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100339#endif
340
Paul Bakker40e46942009-01-03 21:51:57 +0000341#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200342 if( todo.sha1 )
343 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344#endif
345
Paul Bakker9e36f042013-06-30 14:34:05 +0200346#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200347 if( todo.sha256 )
348 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349#endif
350
Paul Bakker9e36f042013-06-30 14:34:05 +0200351#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200352 if( todo.sha512 )
353 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000354#endif
355
Paul Bakker40e46942009-01-03 21:51:57 +0000356#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200357 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200358 {
359 arc4_context arc4;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200360 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200361 arc4_setup( &arc4, tmp, 32 );
362 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200363 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200364 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000365#endif
366
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200367#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200368 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200369 {
370 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200371 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200372 des3_set3key_enc( &des3, tmp );
373 TIME_AND_TSC( "3DES",
374 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200375 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200376 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200378 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200379 {
380 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200381 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200382 des_setkey_enc( &des, tmp );
383 TIME_AND_TSC( "DES",
384 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200385 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200386 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000387#endif
388
Paul Bakker40e46942009-01-03 21:51:57 +0000389#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200390#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200391 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100393 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200394 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200395 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200396 for( keysize = 128; keysize <= 256; keysize += 64 )
397 {
Rich Evans783d9d12015-01-30 11:11:57 +0000398 polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200400 memset( buf, 0, sizeof( buf ) );
401 memset( tmp, 0, sizeof( tmp ) );
402 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200404 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200405 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200406 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200407 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200409#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000410#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200411 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000412 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100413 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200414 gcm_context gcm;
415 for( keysize = 128; keysize <= 256; keysize += 64 )
416 {
Rich Evans783d9d12015-01-30 11:11:57 +0000417 polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000418
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 memset( buf, 0, sizeof( buf ) );
420 memset( tmp, 0, sizeof( tmp ) );
421 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000422
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200423 TIME_AND_TSC( title,
424 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
425 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100426
427 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200428 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000429 }
430#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200431#if defined(POLARSSL_CCM_C)
432 if( todo.aes_ccm )
433 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100434 int keysize;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200435 ccm_context ccm;
436 for( keysize = 128; keysize <= 256; keysize += 64 )
437 {
Rich Evans783d9d12015-01-30 11:11:57 +0000438 polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200439
440 memset( buf, 0, sizeof( buf ) );
441 memset( tmp, 0, sizeof( tmp ) );
442 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
443
444 TIME_AND_TSC( title,
445 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
446 12, NULL, 0, buf, buf, tmp, 16 ) );
447
448 ccm_free( &ccm );
449 }
450 }
451#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000452#endif
453
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200454#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200455 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000456 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100457 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200458 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200459 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200460 for( keysize = 128; keysize <= 256; keysize += 64 )
461 {
Rich Evans783d9d12015-01-30 11:11:57 +0000462 polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000463
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200464 memset( buf, 0, sizeof( buf ) );
465 memset( tmp, 0, sizeof( tmp ) );
466 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000467
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200468 TIME_AND_TSC( title,
469 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
470 BUFSIZE, tmp, buf, buf ) );
471 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200472 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000473 }
474#endif
475
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200476#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200477 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000478 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100479 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200480 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200481 blowfish_init( &blowfish );
482
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200483 for( keysize = 128; keysize <= 256; keysize += 64 )
484 {
Rich Evans783d9d12015-01-30 11:11:57 +0000485 polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000486
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200487 memset( buf, 0, sizeof( buf ) );
488 memset( tmp, 0, sizeof( tmp ) );
489 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000490
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200491 TIME_AND_TSC( title,
492 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
493 tmp, buf, buf ) );
494 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200495
496 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000497 }
498#endif
499
Paul Bakker02faf452011-11-29 11:23:58 +0000500#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200501 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200502 {
503 havege_state hs;
504 havege_init( &hs );
505 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200506 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200507 }
Paul Bakker02faf452011-11-29 11:23:58 +0000508#endif
509
510#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200511 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200512 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200513 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000514
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200515 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000516 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200517 TIME_AND_TSC( "CTR_DRBG (NOPR)",
518 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000519 polarssl_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000520
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200521 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000522 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200523 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
524 TIME_AND_TSC( "CTR_DRBG (PR)",
525 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000526 polarssl_exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200527 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200528 }
Paul Bakker02faf452011-11-29 11:23:58 +0000529#endif
530
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100531#if defined(POLARSSL_HMAC_DRBG_C)
532 if( todo.hmac_drbg )
533 {
534 hmac_drbg_context hmac_drbg;
535 const md_info_t *md_info;
536
537#if defined(POLARSSL_SHA1_C)
538 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000539 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100540
541 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000542 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100543 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
544 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000545 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100546 hmac_drbg_free( &hmac_drbg );
547
548 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000549 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100550 hmac_drbg_set_prediction_resistance( &hmac_drbg,
551 POLARSSL_HMAC_DRBG_PR_ON );
552 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
553 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000554 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100555 hmac_drbg_free( &hmac_drbg );
556#endif
557
558#if defined(POLARSSL_SHA256_C)
559 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000560 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100561
562 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000563 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100564 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
565 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000566 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100567 hmac_drbg_free( &hmac_drbg );
568
569 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000570 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100571 hmac_drbg_set_prediction_resistance( &hmac_drbg,
572 POLARSSL_HMAC_DRBG_PR_ON );
573 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
574 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000575 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100576 hmac_drbg_free( &hmac_drbg );
577#endif
578 }
579#endif
580
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200581#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200582 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100584 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200585 rsa_context rsa;
586 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
587 {
Rich Evans783d9d12015-01-30 11:11:57 +0000588 polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200589
590 rsa_init( &rsa, RSA_PKCS_V15, 0 );
591 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
592
593 TIME_PUBLIC( title, " public",
594 buf[0] = 0;
595 ret = rsa_public( &rsa, buf, buf ) );
596
597 TIME_PUBLIC( title, "private",
598 buf[0] = 0;
599 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
600
601 rsa_free( &rsa );
602 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000604#endif
605
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100606#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200607 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100608 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200609 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200610 const char *dhm_P[DHM_SIZES] = {
611 POLARSSL_DHM_RFC5114_MODP_1024_P,
612 POLARSSL_DHM_RFC3526_MODP_2048_P,
613 POLARSSL_DHM_RFC3526_MODP_3072_P,
614 };
615 const char *dhm_G[DHM_SIZES] = {
616 POLARSSL_DHM_RFC5114_MODP_1024_G,
617 POLARSSL_DHM_RFC3526_MODP_2048_G,
618 POLARSSL_DHM_RFC3526_MODP_3072_G,
619 };
620
621 dhm_context dhm;
622 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200623 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200624 {
Paul Bakkera317a982014-06-18 16:44:11 +0200625 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200626
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200627 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
628 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
629 {
Rich Evans77d36382015-01-30 12:12:11 +0000630 polarssl_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200631 }
632
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200633 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100634 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200635 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000636 polarssl_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200637
Rich Evans783d9d12015-01-30 11:11:57 +0000638 polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200639 TIME_PUBLIC( title, "handshake",
640 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100641 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200642 myrand, NULL );
643 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
644
Rich Evans783d9d12015-01-30 11:11:57 +0000645 polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200646 TIME_PUBLIC( title, "handshake",
647 olen = sizeof( buf );
648 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
649
650 dhm_free( &dhm );
651 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100652 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100653#endif
654
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200655#if defined(POLARSSL_ECDSA_C)
656 if( todo.ecdsa )
657 {
658 ecdsa_context ecdsa;
659 const ecp_curve_info *curve_info;
660 size_t sig_len;
661
662 memset( buf, 0x2A, sizeof( buf ) );
663
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200664 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200665 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
666 curve_info++ )
667 {
668 ecdsa_init( &ecdsa );
669
670 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000671 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100672 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200673
Rich Evans783d9d12015-01-30 11:11:57 +0000674 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200675 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200676 TIME_PUBLIC( title, "sign",
677 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200678 tmp, &sig_len, myrand, NULL ) );
679
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100680 ecdsa_free( &ecdsa );
681 }
682
683 for( curve_info = ecp_curve_list();
684 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
685 curve_info++ )
686 {
687 ecdsa_init( &ecdsa );
688
689 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
690 ecdsa_write_signature( &ecdsa, buf, curve_info->size,
691 tmp, &sig_len, myrand, NULL ) != 0 )
692 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000693 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100694 }
695 ecp_clear_precomputed( &ecdsa.grp );
696
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000697 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100698 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200699 TIME_PUBLIC( title, "verify",
700 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
701 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200702
703 ecdsa_free( &ecdsa );
704 }
705 }
706#endif
707
708#if defined(POLARSSL_ECDH_C)
709 if( todo.ecdh )
710 {
711 ecdh_context ecdh;
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000712#if defined(POLARSSL_ECP_DP_M255_ENABLED)
713 mpi z;
714#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200715 const ecp_curve_info *curve_info;
716 size_t olen;
717
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200718 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200719 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
720 curve_info++ )
721 {
722 ecdh_init( &ecdh );
723
724 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
725 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
726 myrand, NULL ) != 0 ||
727 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
728 {
Rich Evans77d36382015-01-30 12:12:11 +0000729 polarssl_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200730 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100731 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200732
Rich Evans783d9d12015-01-30 11:11:57 +0000733 polarssl_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200734 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200735 TIME_PUBLIC( title, "handshake",
736 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
737 myrand, NULL );
738 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
739 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100740 ecdh_free( &ecdh );
741 }
742
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000743 /* Curve25519 needs to be handled separately */
744#if defined(POLARSSL_ECP_DP_M255_ENABLED)
745 ecdh_init( &ecdh );
746 mpi_init( &z );
747
748 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
749 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
750 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000751 polarssl_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000752 }
753
754 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
755 ret |= ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
756 myrand, NULL );
757 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
758 myrand, NULL ) );
759
760 ecdh_free( &ecdh );
761 mpi_free( &z );
762#endif
763
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100764 for( curve_info = ecp_curve_list();
765 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
766 curve_info++ )
767 {
768 ecdh_init( &ecdh );
769
770 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
771 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
772 myrand, NULL ) != 0 ||
773 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
774 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
775 myrand, NULL ) != 0 )
776 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000777 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100778 }
779 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200780
Rich Evans783d9d12015-01-30 11:11:57 +0000781 polarssl_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200782 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200783 TIME_PUBLIC( title, "handshake",
784 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
785 myrand, NULL ) );
786 ecdh_free( &ecdh );
787 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000788
789 /* Curve25519 needs to be handled separately */
790#if defined(POLARSSL_ECP_DP_M255_ENABLED)
791 ecdh_init( &ecdh );
792 mpi_init( &z );
793
794 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
795 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
796 myrand, NULL ) != 0 ||
797 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
798 {
Manuel Pégourié-Gonnard714929b2015-02-16 17:32:47 +0000799 polarssl_exit( 1 );
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000800 }
801
802 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
803 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
804 myrand, NULL ) );
805
806 ecdh_free( &ecdh );
807 mpi_free( &z );
808#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200809 }
810#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100811
Rich Evansf90016a2015-01-19 14:26:37 +0000812 polarssl_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000813
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000814#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000815 memory_buffer_alloc_free();
816#endif
817
Paul Bakkercce9d772011-11-18 14:26:47 +0000818#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000819 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 fflush( stdout ); getchar();
821#endif
822
823 return( 0 );
824}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200825
Paul Bakker5690efc2011-05-26 13:16:06 +0000826#endif /* POLARSSL_TIMING_C */