blob: 65580ec67d736a8c2d2641ce4d1cce0a79ebe198 [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 \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100109 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000110 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 \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100124 polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", \
125 i * BUFSIZE / 1024, \
126 ( hardclock() - tsc ) / ( j * BUFSIZE ) ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000127} while( 0 )
128
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100129#if defined(POLARSSL_ERROR_C)
130#define PRINT_ERROR \
131 polarssl_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
132 polarssl_printf( "FAILED: %s\n", tmp );
133#else
134#define PRINT_ERROR \
135 polarssl_printf( "FAILED: -0x%04x\n", -ret );
136#endif
137
138#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
139
140#define MEMORY_MEASURE_INIT \
141 size_t max_used, max_blocks, max_bytes; \
142 size_t prv_used, prv_blocks; \
143 memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
144 memory_buffer_alloc_max_reset( );
145
146#define MEMORY_MEASURE_PRINT( title_len ) \
147 memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
148 for( i = 12 - title_len; i != 0; i-- ) polarssl_printf( " " ); \
149 max_used -= prv_used; \
150 max_blocks -= prv_blocks; \
151 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
152 polarssl_printf( "%6u heap bytes", (unsigned) max_bytes );
153
154#else
Manuel Pégourié-Gonnarde579dab2015-01-29 16:28:44 +0000155#define MEMORY_MEASURE_INIT
156#define MEMORY_MEASURE_PRINT( title_len )
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100157#endif
158
Rich Evans85b05ec2015-02-12 11:37:29 +0000159#define TIME_PUBLIC( TITLE, TYPE, CODE ) \
160do { \
161 unsigned long i; \
162 int ret; \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100163 MEMORY_MEASURE_INIT; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000164 \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100165 polarssl_printf( HEADER_FORMAT, TITLE ); \
Rich Evans85b05ec2015-02-12 11:37:29 +0000166 fflush( stdout ); \
167 set_alarm( 3 ); \
168 \
169 ret = 0; \
170 for( i = 1; ! alarmed && ! ret ; i++ ) \
171 { \
172 CODE; \
173 } \
174 \
175 if( ret != 0 ) \
176 { \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100177 PRINT_ERROR; \
Rich Evans85b05ec2015-02-12 11:37:29 +0000178 } \
179 else \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100180 { \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100181 polarssl_printf( "%6lu " TYPE "/s", i / 3 ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100182 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Manuel Pégourié-Gonnard7defc772015-02-05 11:42:42 +0100183 polarssl_printf( "\n" ); \
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100184 } \
Rich Evans85b05ec2015-02-12 11:37:29 +0000185} while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200187#if !defined(POLARSSL_TIMING_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000188int main( void )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200189{
Rich Evansf90016a2015-01-19 14:26:37 +0000190 polarssl_printf("POLARSSL_TIMING_C not defined.\n");
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200191 return( 0 );
192}
193#else
Paul Bakkera3d195c2011-11-27 21:07:34 +0000194static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000196 size_t use_len;
197 int rnd;
198
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 if( rng_state != NULL )
200 rng_state = NULL;
201
Paul Bakkera3d195c2011-11-27 21:07:34 +0000202 while( len > 0 )
203 {
204 use_len = len;
205 if( use_len > sizeof(int) )
206 use_len = sizeof(int);
207
208 rnd = rand();
209 memcpy( output, &rnd, use_len );
210 output += use_len;
211 len -= use_len;
212 }
213
214 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215}
216
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100217/*
218 * Clear some memory that was used to prepare the context
219 */
220#if defined(POLARSSL_ECP_C)
221void ecp_clear_precomputed( ecp_group *grp )
222{
223 if( grp->T != NULL )
224 {
225 size_t i;
226 for( i = 0; i < grp->T_size; i++ )
227 ecp_point_free( &grp->T[i] );
228 polarssl_free( grp->T );
229 }
230 grp->T = NULL;
231 grp->T_size = 0;
232}
233#else
234#define ecp_clear_precomputed( g )
235#endif
236
Paul Bakker5121ce52009-01-03 21:22:43 +0000237unsigned char buf[BUFSIZE];
238
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200239typedef struct {
Paul Bakker61b699e2014-01-22 13:35:29 +0100240 char md4, md5, ripemd160, sha1, sha256, sha512,
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200241 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100242 havege, ctr_drbg, hmac_drbg,
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200243 rsa, dhm, ecdsa, ecdh;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200244} todo_list;
245
Paul Bakkercce9d772011-11-18 14:26:47 +0000246int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +0000247{
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100248 int i;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200249 unsigned char tmp[200];
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200250 char title[TITLE_LEN];
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200251 todo_list todo;
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000252#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100253 unsigned char malloc_buf[HEAP_SIZE] = { 0 };
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000254#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000255
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200256 if( argc == 1 )
257 memset( &todo, 1, sizeof( todo ) );
258 else
259 {
260 memset( &todo, 0, sizeof( todo ) );
261
262 for( i = 1; i < argc; i++ )
263 {
264 if( strcmp( argv[i], "md4" ) == 0 )
265 todo.md4 = 1;
266 else if( strcmp( argv[i], "md5" ) == 0 )
267 todo.md5 = 1;
Paul Bakker61b699e2014-01-22 13:35:29 +0100268 else if( strcmp( argv[i], "ripemd160" ) == 0 )
269 todo.ripemd160 = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200270 else if( strcmp( argv[i], "sha1" ) == 0 )
271 todo.sha1 = 1;
272 else if( strcmp( argv[i], "sha256" ) == 0 )
273 todo.sha256 = 1;
274 else if( strcmp( argv[i], "sha512" ) == 0 )
275 todo.sha512 = 1;
276 else if( strcmp( argv[i], "arc4" ) == 0 )
277 todo.arc4 = 1;
278 else if( strcmp( argv[i], "des3" ) == 0 )
279 todo.des3 = 1;
280 else if( strcmp( argv[i], "des" ) == 0 )
281 todo.des = 1;
282 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
283 todo.aes_cbc = 1;
284 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
285 todo.aes_gcm = 1;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200286 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
287 todo.aes_ccm = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200288 else if( strcmp( argv[i], "camellia" ) == 0 )
289 todo.camellia = 1;
290 else if( strcmp( argv[i], "blowfish" ) == 0 )
291 todo.blowfish = 1;
292 else if( strcmp( argv[i], "havege" ) == 0 )
293 todo.havege = 1;
294 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
295 todo.ctr_drbg = 1;
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100296 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
297 todo.hmac_drbg = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200298 else if( strcmp( argv[i], "rsa" ) == 0 )
299 todo.rsa = 1;
300 else if( strcmp( argv[i], "dhm" ) == 0 )
301 todo.dhm = 1;
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200302 else if( strcmp( argv[i], "ecdsa" ) == 0 )
303 todo.ecdsa = 1;
304 else if( strcmp( argv[i], "ecdh" ) == 0 )
305 todo.ecdh = 1;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200306 else
307 {
Rich Evansf90016a2015-01-19 14:26:37 +0000308 polarssl_printf( "Unrecognized option: %s\n", argv[i] );
309 polarssl_printf( "Available options: " OPTIONS );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200310 }
311 }
312 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
Rich Evansf90016a2015-01-19 14:26:37 +0000314 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000316#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
317 memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
318#endif
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200319 memset( buf, 0xAA, sizeof( buf ) );
Paul Bakkerdf71dd12014-04-17 16:03:48 +0200320 memset( tmp, 0xBB, sizeof( tmp ) );
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200321
Paul Bakker40e46942009-01-03 21:51:57 +0000322#if defined(POLARSSL_MD4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200323 if( todo.md4 )
324 TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325#endif
326
Paul Bakker40e46942009-01-03 21:51:57 +0000327#if defined(POLARSSL_MD5_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200328 if( todo.md5 )
329 TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330#endif
331
Paul Bakker61b699e2014-01-22 13:35:29 +0100332#if defined(POLARSSL_RIPEMD160_C)
333 if( todo.ripemd160 )
334 TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) );
Manuel Pégourié-Gonnard01b0b382014-01-17 14:29:46 +0100335#endif
336
Paul Bakker40e46942009-01-03 21:51:57 +0000337#if defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200338 if( todo.sha1 )
339 TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340#endif
341
Paul Bakker9e36f042013-06-30 14:34:05 +0200342#if defined(POLARSSL_SHA256_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200343 if( todo.sha256 )
344 TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345#endif
346
Paul Bakker9e36f042013-06-30 14:34:05 +0200347#if defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200348 if( todo.sha512 )
349 TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) );
Paul Bakker3a3c3c22009-02-09 22:33:30 +0000350#endif
351
Paul Bakker40e46942009-01-03 21:51:57 +0000352#if defined(POLARSSL_ARC4_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200353 if( todo.arc4 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200354 {
355 arc4_context arc4;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200356 arc4_init( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200357 arc4_setup( &arc4, tmp, 32 );
358 TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200359 arc4_free( &arc4 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200360 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000361#endif
362
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200363#if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200364 if( todo.des3 )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200365 {
366 des3_context des3;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200367 des3_init( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200368 des3_set3key_enc( &des3, tmp );
369 TIME_AND_TSC( "3DES",
370 des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200371 des3_free( &des3 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200372 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200374 if( todo.des )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200375 {
376 des_context des;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200377 des_init( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200378 des_setkey_enc( &des, tmp );
379 TIME_AND_TSC( "DES",
380 des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200381 des_free( &des );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200382 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000383#endif
384
Paul Bakker40e46942009-01-03 21:51:57 +0000385#if defined(POLARSSL_AES_C)
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200386#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200387 if( todo.aes_cbc )
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100389 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200390 aes_context aes;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200391 aes_init( &aes );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200392 for( keysize = 128; keysize <= 256; keysize += 64 )
393 {
Rich Evans783d9d12015-01-30 11:11:57 +0000394 polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200396 memset( buf, 0, sizeof( buf ) );
397 memset( tmp, 0, sizeof( tmp ) );
398 aes_setkey_enc( &aes, tmp, keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200400 TIME_AND_TSC( title,
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200401 aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200402 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200403 aes_free( &aes );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 }
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200405#endif
Paul Bakker89e80c92012-03-20 13:50:09 +0000406#if defined(POLARSSL_GCM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200407 if( todo.aes_gcm )
Paul Bakker89e80c92012-03-20 13:50:09 +0000408 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100409 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200410 gcm_context gcm;
411 for( keysize = 128; keysize <= 256; keysize += 64 )
412 {
Rich Evans783d9d12015-01-30 11:11:57 +0000413 polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000414
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200415 memset( buf, 0, sizeof( buf ) );
416 memset( tmp, 0, sizeof( tmp ) );
417 gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
Paul Bakker89e80c92012-03-20 13:50:09 +0000418
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200419 TIME_AND_TSC( title,
420 gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp,
421 12, NULL, 0, buf, buf, 16, tmp ) );
Paul Bakkerf70fe812013-12-16 16:43:10 +0100422
423 gcm_free( &gcm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200424 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000425 }
426#endif
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200427#if defined(POLARSSL_CCM_C)
428 if( todo.aes_ccm )
429 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100430 int keysize;
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200431 ccm_context ccm;
432 for( keysize = 128; keysize <= 256; keysize += 64 )
433 {
Rich Evans783d9d12015-01-30 11:11:57 +0000434 polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Manuel Pégourié-Gonnard58d78a82014-05-07 12:03:02 +0200435
436 memset( buf, 0, sizeof( buf ) );
437 memset( tmp, 0, sizeof( tmp ) );
438 ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize );
439
440 TIME_AND_TSC( title,
441 ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
442 12, NULL, 0, buf, buf, tmp, 16 ) );
443
444 ccm_free( &ccm );
445 }
446 }
447#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000448#endif
449
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200450#if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200451 if( todo.camellia )
Paul Bakker38119b12009-01-10 23:31:23 +0000452 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100453 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200454 camellia_context camellia;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200455 camellia_init( &camellia );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200456 for( keysize = 128; keysize <= 256; keysize += 64 )
457 {
Rich Evans783d9d12015-01-30 11:11:57 +0000458 polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000459
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200460 memset( buf, 0, sizeof( buf ) );
461 memset( tmp, 0, sizeof( tmp ) );
462 camellia_setkey_enc( &camellia, tmp, keysize );
Paul Bakker38119b12009-01-10 23:31:23 +0000463
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200464 TIME_AND_TSC( title,
465 camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT,
466 BUFSIZE, tmp, buf, buf ) );
467 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200468 camellia_free( &camellia );
Paul Bakker38119b12009-01-10 23:31:23 +0000469 }
470#endif
471
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200472#if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200473 if( todo.blowfish )
Paul Bakker3d58fe82012-07-04 17:15:31 +0000474 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100475 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200476 blowfish_context blowfish;
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200477 blowfish_init( &blowfish );
478
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200479 for( keysize = 128; keysize <= 256; keysize += 64 )
480 {
Rich Evans783d9d12015-01-30 11:11:57 +0000481 polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000482
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200483 memset( buf, 0, sizeof( buf ) );
484 memset( tmp, 0, sizeof( tmp ) );
485 blowfish_setkey( &blowfish, tmp, keysize );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000486
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200487 TIME_AND_TSC( title,
488 blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE,
489 tmp, buf, buf ) );
490 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200491
492 blowfish_free( &blowfish );
Paul Bakker3d58fe82012-07-04 17:15:31 +0000493 }
494#endif
495
Paul Bakker02faf452011-11-29 11:23:58 +0000496#if defined(POLARSSL_HAVEGE_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200497 if( todo.havege )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200498 {
499 havege_state hs;
500 havege_init( &hs );
501 TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) );
Paul Bakkera317a982014-06-18 16:44:11 +0200502 havege_free( &hs );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200503 }
Paul Bakker02faf452011-11-29 11:23:58 +0000504#endif
505
506#if defined(POLARSSL_CTR_DRBG_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200507 if( todo.ctr_drbg )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200508 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200509 ctr_drbg_context ctr_drbg;
Paul Bakker02faf452011-11-29 11:23:58 +0000510
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200511 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000512 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200513 TIME_AND_TSC( "CTR_DRBG (NOPR)",
514 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000515 polarssl_exit(1) );
Paul Bakker02faf452011-11-29 11:23:58 +0000516
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200517 if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000518 polarssl_exit(1);
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200519 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON );
520 TIME_AND_TSC( "CTR_DRBG (PR)",
521 if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000522 polarssl_exit(1) );
Paul Bakkera317a982014-06-18 16:44:11 +0200523 ctr_drbg_free( &ctr_drbg );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200524 }
Paul Bakker02faf452011-11-29 11:23:58 +0000525#endif
526
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100527#if defined(POLARSSL_HMAC_DRBG_C)
528 if( todo.hmac_drbg )
529 {
530 hmac_drbg_context hmac_drbg;
531 const md_info_t *md_info;
532
533#if defined(POLARSSL_SHA1_C)
534 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000535 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100536
537 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000538 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100539 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
540 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000541 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100542 hmac_drbg_free( &hmac_drbg );
543
544 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000545 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100546 hmac_drbg_set_prediction_resistance( &hmac_drbg,
547 POLARSSL_HMAC_DRBG_PR_ON );
548 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
549 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000550 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100551 hmac_drbg_free( &hmac_drbg );
552#endif
553
554#if defined(POLARSSL_SHA256_C)
555 if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL )
Rich Evans77d36382015-01-30 12:12:11 +0000556 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100557
558 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000559 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100560 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
561 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000562 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100563 hmac_drbg_free( &hmac_drbg );
564
565 if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000566 polarssl_exit(1);
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100567 hmac_drbg_set_prediction_resistance( &hmac_drbg,
568 POLARSSL_HMAC_DRBG_PR_ON );
569 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
570 if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000571 polarssl_exit(1) );
Manuel Pégourié-Gonnardfef0f8f2014-01-30 20:59:00 +0100572 hmac_drbg_free( &hmac_drbg );
573#endif
574 }
575#endif
576
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200577#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200578 if( todo.rsa )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 {
Manuel Pégourié-Gonnard71e75dc2014-12-19 18:05:43 +0100580 int keysize;
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200581 rsa_context rsa;
582 for( keysize = 1024; keysize <= 4096; keysize *= 2 )
583 {
Rich Evans783d9d12015-01-30 11:11:57 +0000584 polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200585
586 rsa_init( &rsa, RSA_PKCS_V15, 0 );
587 rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
588
589 TIME_PUBLIC( title, " public",
590 buf[0] = 0;
591 ret = rsa_public( &rsa, buf, buf ) );
592
593 TIME_PUBLIC( title, "private",
594 buf[0] = 0;
595 ret = rsa_private( &rsa, myrand, NULL, buf, buf ) );
596
597 rsa_free( &rsa );
598 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000600#endif
601
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100602#if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C)
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200603 if( todo.dhm )
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100604 {
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200605 int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 };
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200606 const char *dhm_P[DHM_SIZES] = {
607 POLARSSL_DHM_RFC5114_MODP_1024_P,
608 POLARSSL_DHM_RFC3526_MODP_2048_P,
609 POLARSSL_DHM_RFC3526_MODP_3072_P,
610 };
611 const char *dhm_G[DHM_SIZES] = {
612 POLARSSL_DHM_RFC5114_MODP_1024_G,
613 POLARSSL_DHM_RFC3526_MODP_2048_G,
614 POLARSSL_DHM_RFC3526_MODP_3072_G,
615 };
616
617 dhm_context dhm;
618 size_t olen;
Manuel Pégourié-Gonnarded7cbe92013-09-17 15:30:51 +0200619 for( i = 0; i < DHM_SIZES; i++ )
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200620 {
Paul Bakkera317a982014-06-18 16:44:11 +0200621 dhm_init( &dhm );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200622
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200623 if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
624 mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
625 {
Rich Evans77d36382015-01-30 12:12:11 +0000626 polarssl_exit( 1 );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200627 }
628
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200629 dhm.len = mpi_size( &dhm.P );
Paul Bakker840ab202013-11-30 15:14:38 +0100630 dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200631 if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000632 polarssl_exit( 1 );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200633
Rich Evans783d9d12015-01-30 11:11:57 +0000634 polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200635 TIME_PUBLIC( title, "handshake",
636 olen = sizeof( buf );
Paul Bakker840ab202013-11-30 15:14:38 +0100637 ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200638 myrand, NULL );
639 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
640
Rich Evans783d9d12015-01-30 11:11:57 +0000641 polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200642 TIME_PUBLIC( title, "handshake",
643 olen = sizeof( buf );
644 ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) );
645
646 dhm_free( &dhm );
647 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100648 }
Manuel Pégourié-Gonnarde870c0a2012-11-08 11:31:48 +0100649#endif
650
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200651#if defined(POLARSSL_ECDSA_C)
652 if( todo.ecdsa )
653 {
654 ecdsa_context ecdsa;
655 const ecp_curve_info *curve_info;
656 size_t sig_len;
657
658 memset( buf, 0x2A, sizeof( buf ) );
659
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200660 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200661 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
662 curve_info++ )
663 {
664 ecdsa_init( &ecdsa );
665
666 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Rich Evans77d36382015-01-30 12:12:11 +0000667 polarssl_exit( 1 );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100668 ecp_clear_precomputed( &ecdsa.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200669
Rich Evans783d9d12015-01-30 11:11:57 +0000670 polarssl_snprintf( title, sizeof( title ), "ECDSA-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200671 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200672 TIME_PUBLIC( title, "sign",
673 ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size,
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200674 tmp, &sig_len, myrand, NULL ) );
675
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100676 ecdsa_free( &ecdsa );
677 }
678
679 for( curve_info = ecp_curve_list();
680 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
681 curve_info++ )
682 {
683 ecdsa_init( &ecdsa );
684
685 if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
686 ecdsa_write_signature( &ecdsa, buf, curve_info->size,
687 tmp, &sig_len, myrand, NULL ) != 0 )
688 {
689 exit( 1 );
690 }
691 ecp_clear_precomputed( &ecdsa.grp );
692
693 snprintf( title, sizeof( title ), "ECDSA-%s",
694 curve_info->name );
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200695 TIME_PUBLIC( title, "verify",
696 ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size,
697 tmp, sig_len ) );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200698
699 ecdsa_free( &ecdsa );
700 }
701 }
702#endif
703
704#if defined(POLARSSL_ECDH_C)
705 if( todo.ecdh )
706 {
707 ecdh_context ecdh;
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000708#if defined(POLARSSL_ECP_DP_M255_ENABLED)
709 mpi z;
710#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200711 const ecp_curve_info *curve_info;
712 size_t olen;
713
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200714 for( curve_info = ecp_curve_list();
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200715 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
716 curve_info++ )
717 {
718 ecdh_init( &ecdh );
719
720 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
721 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
722 myrand, NULL ) != 0 ||
723 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
724 {
Rich Evans77d36382015-01-30 12:12:11 +0000725 polarssl_exit( 1 );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200726 }
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100727 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200728
Rich Evans783d9d12015-01-30 11:11:57 +0000729 polarssl_snprintf( title, sizeof( title ), "ECDHE-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200730 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200731 TIME_PUBLIC( title, "handshake",
732 ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
733 myrand, NULL );
734 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
735 myrand, NULL ) );
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100736 ecdh_free( &ecdh );
737 }
738
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000739 /* Curve25519 needs to be handled separately */
740#if defined(POLARSSL_ECP_DP_M255_ENABLED)
741 ecdh_init( &ecdh );
742 mpi_init( &z );
743
744 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
745 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
746 {
747 exit( 1 );
748 }
749
750 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
751 ret |= ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
752 myrand, NULL );
753 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
754 myrand, NULL ) );
755
756 ecdh_free( &ecdh );
757 mpi_free( &z );
758#endif
759
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100760 for( curve_info = ecp_curve_list();
761 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
762 curve_info++ )
763 {
764 ecdh_init( &ecdh );
765
766 if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 ||
767 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
768 myrand, NULL ) != 0 ||
769 ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
770 ecdh_make_public( &ecdh, &olen, buf, sizeof( buf),
771 myrand, NULL ) != 0 )
772 {
773 exit( 1 );
774 }
775 ecp_clear_precomputed( &ecdh.grp );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200776
Rich Evans783d9d12015-01-30 11:11:57 +0000777 polarssl_snprintf( title, sizeof( title ), "ECDH-%s",
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +0200778 curve_info->name );
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200779 TIME_PUBLIC( title, "handshake",
780 ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
781 myrand, NULL ) );
782 ecdh_free( &ecdh );
783 }
Manuel Pégourié-Gonnard85391f22015-02-05 09:54:48 +0000784
785 /* Curve25519 needs to be handled separately */
786#if defined(POLARSSL_ECP_DP_M255_ENABLED)
787 ecdh_init( &ecdh );
788 mpi_init( &z );
789
790 if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 ||
791 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
792 myrand, NULL ) != 0 ||
793 ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
794 {
795 exit( 1 );
796 }
797
798 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
799 ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
800 myrand, NULL ) );
801
802 ecdh_free( &ecdh );
803 mpi_free( &z );
804#endif
Manuel Pégourié-Gonnardcc34f952013-09-17 16:04:08 +0200805 }
806#endif
Manuel Pégourié-Gonnard50da0482014-12-19 12:10:37 +0100807
Rich Evansf90016a2015-01-19 14:26:37 +0000808 polarssl_printf( "\n" );
Paul Bakker1d4da2e2009-10-25 12:36:53 +0000809
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000810#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard128657d2014-12-18 16:35:52 +0000811 memory_buffer_alloc_free();
812#endif
813
Paul Bakkercce9d772011-11-18 14:26:47 +0000814#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000815 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 fflush( stdout ); getchar();
817#endif
818
819 return( 0 );
820}
Manuel Pégourié-Gonnard8271f2f2013-09-17 14:57:55 +0200821
Paul Bakker5690efc2011-05-26 13:16:06 +0000822#endif /* POLARSSL_TIMING_C */