Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 33 | #define polarssl_exit exit |
Rich Evans | b92965b | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
| 35 | #define polarssl_snprintf snprintf |
Manuel Pégourié-Gonnard | c3e3395 | 2015-02-16 18:46:20 +0000 | [diff] [blame] | 36 | #define polarssl_free free |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 39 | #if !defined(POLARSSL_TIMING_C) |
| 40 | int main( void ) |
| 41 | { |
| 42 | polarssl_printf("POLARSSL_TIMING_C not defined.\n"); |
| 43 | return( 0 ); |
| 44 | } |
| 45 | #else |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 47 | #include <string.h> |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 48 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 49 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 51 | #include "polarssl/md4.h" |
| 52 | #include "polarssl/md5.h" |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 53 | #include "polarssl/ripemd160.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 54 | #include "polarssl/sha1.h" |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 55 | #include "polarssl/sha256.h" |
| 56 | #include "polarssl/sha512.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 57 | #include "polarssl/arc4.h" |
| 58 | #include "polarssl/des.h" |
| 59 | #include "polarssl/aes.h" |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 60 | #include "polarssl/blowfish.h" |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 61 | #include "polarssl/camellia.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 62 | #include "polarssl/gcm.h" |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 63 | #include "polarssl/ccm.h" |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 64 | #include "polarssl/havege.h" |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 65 | #include "polarssl/ctr_drbg.h" |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 66 | #include "polarssl/hmac_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 67 | #include "polarssl/rsa.h" |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 68 | #include "polarssl/dhm.h" |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 69 | #include "polarssl/ecdsa.h" |
| 70 | #include "polarssl/ecdh.h" |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 71 | #include "polarssl/error.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 73 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 74 | #include "polarssl/memory_buffer_alloc.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Manuel Pégourié-Gonnard | 2f77ce3 | 2013-10-03 11:59:57 +0200 | [diff] [blame] | 77 | #if defined _MSC_VER && !defined snprintf |
| 78 | #define snprintf _snprintf |
| 79 | #endif |
| 80 | |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 81 | /* |
| 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 Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 93 | #define BUFSIZE 1024 |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 94 | #define HEADER_FORMAT " %-24s : " |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 95 | #define TITLE_LEN 25 |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 96 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 97 | #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 ) \ |
| 115 | do { \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 116 | unsigned long ii, jj, tsc; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 117 | \ |
Manuel Pégourié-Gonnard | 7defc77 | 2015-02-05 11:42:42 +0100 | [diff] [blame] | 118 | polarssl_printf( HEADER_FORMAT, TITLE ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 119 | fflush( stdout ); \ |
| 120 | \ |
| 121 | set_alarm( 1 ); \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 122 | for( ii = 1; ! alarmed; ii++ ) \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 123 | { \ |
| 124 | CODE; \ |
| 125 | } \ |
| 126 | \ |
| 127 | tsc = hardclock(); \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 128 | for( jj = 0; jj < 1024; jj++ ) \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 129 | { \ |
| 130 | CODE; \ |
| 131 | } \ |
| 132 | \ |
Manuel Pégourié-Gonnard | 7defc77 | 2015-02-05 11:42:42 +0100 | [diff] [blame] | 133 | polarssl_printf( "%9lu Kb/s, %9lu cycles/byte\n", \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 134 | ii * BUFSIZE / 1024, \ |
| 135 | ( hardclock() - tsc ) / ( jj * BUFSIZE ) ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 136 | } while( 0 ) |
| 137 | |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 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 ); \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 148 | for( ii = 12 - title_len; ii != 0; ii-- ) polarssl_printf( " " ); \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 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é-Gonnard | e579dab | 2015-01-29 16:28:44 +0000 | [diff] [blame] | 155 | #define MEMORY_MEASURE_INIT |
| 156 | #define MEMORY_MEASURE_PRINT( title_len ) |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 157 | #endif |
| 158 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 159 | #define TIME_PUBLIC( TITLE, TYPE, CODE ) \ |
| 160 | do { \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 161 | unsigned long ii; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 162 | int ret; \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 163 | MEMORY_MEASURE_INIT; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 164 | \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 165 | polarssl_printf( HEADER_FORMAT, TITLE ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 166 | fflush( stdout ); \ |
| 167 | set_alarm( 3 ); \ |
| 168 | \ |
| 169 | ret = 0; \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 170 | for( ii = 1; ! alarmed && ! ret ; ii++ ) \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 171 | { \ |
| 172 | CODE; \ |
| 173 | } \ |
| 174 | \ |
| 175 | if( ret != 0 ) \ |
| 176 | { \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 177 | PRINT_ERROR; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 178 | } \ |
| 179 | else \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 180 | { \ |
Manuel Pégourié-Gonnard | a67fd79 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 181 | polarssl_printf( "%6lu " TYPE "/s", ii / 3 ); \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 182 | MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \ |
Manuel Pégourié-Gonnard | 7defc77 | 2015-02-05 11:42:42 +0100 | [diff] [blame] | 183 | polarssl_printf( "\n" ); \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 184 | } \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 185 | } while( 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 187 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 189 | size_t use_len; |
| 190 | int rnd; |
| 191 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | if( rng_state != NULL ) |
| 193 | rng_state = NULL; |
| 194 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 195 | while( len > 0 ) |
| 196 | { |
| 197 | use_len = len; |
| 198 | if( use_len > sizeof(int) ) |
| 199 | use_len = sizeof(int); |
| 200 | |
| 201 | rnd = rand(); |
| 202 | memcpy( output, &rnd, use_len ); |
| 203 | output += use_len; |
| 204 | len -= use_len; |
| 205 | } |
| 206 | |
| 207 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 210 | /* |
| 211 | * Clear some memory that was used to prepare the context |
| 212 | */ |
| 213 | #if defined(POLARSSL_ECP_C) |
| 214 | void ecp_clear_precomputed( ecp_group *grp ) |
| 215 | { |
| 216 | if( grp->T != NULL ) |
| 217 | { |
| 218 | size_t i; |
| 219 | for( i = 0; i < grp->T_size; i++ ) |
| 220 | ecp_point_free( &grp->T[i] ); |
| 221 | polarssl_free( grp->T ); |
| 222 | } |
| 223 | grp->T = NULL; |
| 224 | grp->T_size = 0; |
| 225 | } |
| 226 | #else |
| 227 | #define ecp_clear_precomputed( g ) |
| 228 | #endif |
| 229 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | unsigned char buf[BUFSIZE]; |
| 231 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 232 | typedef struct { |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 233 | char md4, md5, ripemd160, sha1, sha256, sha512, |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 234 | arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish, |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 235 | havege, ctr_drbg, hmac_drbg, |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 236 | rsa, dhm, ecdsa, ecdh; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 237 | } todo_list; |
| 238 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 239 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 240 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 241 | int i; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 242 | unsigned char tmp[200]; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 243 | char title[TITLE_LEN]; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 244 | todo_list todo; |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 245 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 246 | unsigned char malloc_buf[HEAP_SIZE] = { 0 }; |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 247 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 248 | |
Manuel Pégourié-Gonnard | c439e7b | 2015-03-03 13:12:00 +0000 | [diff] [blame] | 249 | if( argc <= 1 ) |
| 250 | { |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 251 | memset( &todo, 1, sizeof( todo ) ); |
Manuel Pégourié-Gonnard | c439e7b | 2015-03-03 13:12:00 +0000 | [diff] [blame] | 252 | } |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 253 | else |
| 254 | { |
| 255 | memset( &todo, 0, sizeof( todo ) ); |
| 256 | |
| 257 | for( i = 1; i < argc; i++ ) |
| 258 | { |
| 259 | if( strcmp( argv[i], "md4" ) == 0 ) |
| 260 | todo.md4 = 1; |
| 261 | else if( strcmp( argv[i], "md5" ) == 0 ) |
| 262 | todo.md5 = 1; |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 263 | else if( strcmp( argv[i], "ripemd160" ) == 0 ) |
| 264 | todo.ripemd160 = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 265 | else if( strcmp( argv[i], "sha1" ) == 0 ) |
| 266 | todo.sha1 = 1; |
| 267 | else if( strcmp( argv[i], "sha256" ) == 0 ) |
| 268 | todo.sha256 = 1; |
| 269 | else if( strcmp( argv[i], "sha512" ) == 0 ) |
| 270 | todo.sha512 = 1; |
| 271 | else if( strcmp( argv[i], "arc4" ) == 0 ) |
| 272 | todo.arc4 = 1; |
| 273 | else if( strcmp( argv[i], "des3" ) == 0 ) |
| 274 | todo.des3 = 1; |
| 275 | else if( strcmp( argv[i], "des" ) == 0 ) |
| 276 | todo.des = 1; |
| 277 | else if( strcmp( argv[i], "aes_cbc" ) == 0 ) |
| 278 | todo.aes_cbc = 1; |
| 279 | else if( strcmp( argv[i], "aes_gcm" ) == 0 ) |
| 280 | todo.aes_gcm = 1; |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 281 | else if( strcmp( argv[i], "aes_ccm" ) == 0 ) |
| 282 | todo.aes_ccm = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 283 | else if( strcmp( argv[i], "camellia" ) == 0 ) |
| 284 | todo.camellia = 1; |
| 285 | else if( strcmp( argv[i], "blowfish" ) == 0 ) |
| 286 | todo.blowfish = 1; |
| 287 | else if( strcmp( argv[i], "havege" ) == 0 ) |
| 288 | todo.havege = 1; |
| 289 | else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) |
| 290 | todo.ctr_drbg = 1; |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 291 | else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) |
| 292 | todo.hmac_drbg = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 293 | else if( strcmp( argv[i], "rsa" ) == 0 ) |
| 294 | todo.rsa = 1; |
| 295 | else if( strcmp( argv[i], "dhm" ) == 0 ) |
| 296 | todo.dhm = 1; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 297 | else if( strcmp( argv[i], "ecdsa" ) == 0 ) |
| 298 | todo.ecdsa = 1; |
| 299 | else if( strcmp( argv[i], "ecdh" ) == 0 ) |
| 300 | todo.ecdh = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 301 | else |
| 302 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 303 | polarssl_printf( "Unrecognized option: %s\n", argv[i] ); |
| 304 | polarssl_printf( "Available options: " OPTIONS ); |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 309 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 310 | |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 311 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 312 | memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) ); |
| 313 | #endif |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 314 | memset( buf, 0xAA, sizeof( buf ) ); |
Paul Bakker | df71dd1 | 2014-04-17 16:03:48 +0200 | [diff] [blame] | 315 | memset( tmp, 0xBB, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 316 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 317 | #if defined(POLARSSL_MD4_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 318 | if( todo.md4 ) |
| 319 | TIME_AND_TSC( "MD4", md4( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | #endif |
| 321 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 322 | #if defined(POLARSSL_MD5_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 323 | if( todo.md5 ) |
| 324 | TIME_AND_TSC( "MD5", md5( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 325 | #endif |
| 326 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 327 | #if defined(POLARSSL_RIPEMD160_C) |
| 328 | if( todo.ripemd160 ) |
| 329 | TIME_AND_TSC( "RIPEMD160", ripemd160( buf, BUFSIZE, tmp ) ); |
Manuel Pégourié-Gonnard | 01b0b38 | 2014-01-17 14:29:46 +0100 | [diff] [blame] | 330 | #endif |
| 331 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 332 | #if defined(POLARSSL_SHA1_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 333 | if( todo.sha1 ) |
| 334 | TIME_AND_TSC( "SHA-1", sha1( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 335 | #endif |
| 336 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 337 | #if defined(POLARSSL_SHA256_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 338 | if( todo.sha256 ) |
| 339 | TIME_AND_TSC( "SHA-256", sha256( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | #endif |
| 341 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 342 | #if defined(POLARSSL_SHA512_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 343 | if( todo.sha512 ) |
| 344 | TIME_AND_TSC( "SHA-512", sha512( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 345 | #endif |
| 346 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 347 | #if defined(POLARSSL_ARC4_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 348 | if( todo.arc4 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 349 | { |
| 350 | arc4_context arc4; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 351 | arc4_init( &arc4 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 352 | arc4_setup( &arc4, tmp, 32 ); |
| 353 | TIME_AND_TSC( "ARC4", arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 354 | arc4_free( &arc4 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 355 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | #endif |
| 357 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 358 | #if defined(POLARSSL_DES_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 359 | if( todo.des3 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 360 | { |
| 361 | des3_context des3; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 362 | des3_init( &des3 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 363 | des3_set3key_enc( &des3, tmp ); |
| 364 | TIME_AND_TSC( "3DES", |
| 365 | des3_crypt_cbc( &des3, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 366 | des3_free( &des3 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 367 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 369 | if( todo.des ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 370 | { |
| 371 | des_context des; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 372 | des_init( &des ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 373 | des_setkey_enc( &des, tmp ); |
| 374 | TIME_AND_TSC( "DES", |
| 375 | des_crypt_cbc( &des, DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 376 | des_free( &des ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 377 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | #endif |
| 379 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 380 | #if defined(POLARSSL_AES_C) |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 381 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 382 | if( todo.aes_cbc ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 383 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 384 | int keysize; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 385 | aes_context aes; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 386 | aes_init( &aes ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 387 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 388 | { |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 389 | polarssl_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 390 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 391 | memset( buf, 0, sizeof( buf ) ); |
| 392 | memset( tmp, 0, sizeof( tmp ) ); |
| 393 | aes_setkey_enc( &aes, tmp, keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 394 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 395 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 396 | aes_crypt_cbc( &aes, AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 397 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 398 | aes_free( &aes ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 399 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 400 | #endif |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 401 | #if defined(POLARSSL_GCM_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 402 | if( todo.aes_gcm ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 403 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 404 | int keysize; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 405 | gcm_context gcm; |
| 406 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 407 | { |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 408 | polarssl_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 409 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 410 | memset( buf, 0, sizeof( buf ) ); |
| 411 | memset( tmp, 0, sizeof( tmp ) ); |
| 412 | gcm_init( &gcm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 413 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 414 | TIME_AND_TSC( title, |
| 415 | gcm_crypt_and_tag( &gcm, GCM_ENCRYPT, BUFSIZE, tmp, |
| 416 | 12, NULL, 0, buf, buf, 16, tmp ) ); |
Paul Bakker | f70fe81 | 2013-12-16 16:43:10 +0100 | [diff] [blame] | 417 | |
| 418 | gcm_free( &gcm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 419 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 420 | } |
| 421 | #endif |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 422 | #if defined(POLARSSL_CCM_C) |
| 423 | if( todo.aes_ccm ) |
| 424 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 425 | int keysize; |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 426 | ccm_context ccm; |
| 427 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 428 | { |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 429 | polarssl_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 430 | |
| 431 | memset( buf, 0, sizeof( buf ) ); |
| 432 | memset( tmp, 0, sizeof( tmp ) ); |
| 433 | ccm_init( &ccm, POLARSSL_CIPHER_ID_AES, tmp, keysize ); |
| 434 | |
| 435 | TIME_AND_TSC( title, |
| 436 | ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp, |
| 437 | 12, NULL, 0, buf, buf, tmp, 16 ) ); |
| 438 | |
| 439 | ccm_free( &ccm ); |
| 440 | } |
| 441 | } |
| 442 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 443 | #endif |
| 444 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 445 | #if defined(POLARSSL_CAMELLIA_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 446 | if( todo.camellia ) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 447 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 448 | int keysize; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 449 | camellia_context camellia; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 450 | camellia_init( &camellia ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 451 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 452 | { |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 453 | polarssl_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 454 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 455 | memset( buf, 0, sizeof( buf ) ); |
| 456 | memset( tmp, 0, sizeof( tmp ) ); |
| 457 | camellia_setkey_enc( &camellia, tmp, keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 458 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 459 | TIME_AND_TSC( title, |
| 460 | camellia_crypt_cbc( &camellia, CAMELLIA_ENCRYPT, |
| 461 | BUFSIZE, tmp, buf, buf ) ); |
| 462 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 463 | camellia_free( &camellia ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 464 | } |
| 465 | #endif |
| 466 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 467 | #if defined(POLARSSL_BLOWFISH_C) && defined(POLARSSL_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 468 | if( todo.blowfish ) |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 469 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 470 | int keysize; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 471 | blowfish_context blowfish; |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 472 | blowfish_init( &blowfish ); |
| 473 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 474 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 475 | { |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 476 | polarssl_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 477 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 478 | memset( buf, 0, sizeof( buf ) ); |
| 479 | memset( tmp, 0, sizeof( tmp ) ); |
| 480 | blowfish_setkey( &blowfish, tmp, keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 481 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 482 | TIME_AND_TSC( title, |
| 483 | blowfish_crypt_cbc( &blowfish, BLOWFISH_ENCRYPT, BUFSIZE, |
| 484 | tmp, buf, buf ) ); |
| 485 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 486 | |
| 487 | blowfish_free( &blowfish ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 488 | } |
| 489 | #endif |
| 490 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 491 | #if defined(POLARSSL_HAVEGE_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 492 | if( todo.havege ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 493 | { |
| 494 | havege_state hs; |
| 495 | havege_init( &hs ); |
| 496 | TIME_AND_TSC( "HAVEGE", havege_random( &hs, buf, BUFSIZE ) ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 497 | havege_free( &hs ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 498 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 499 | #endif |
| 500 | |
| 501 | #if defined(POLARSSL_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 502 | if( todo.ctr_drbg ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 503 | { |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 504 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 505 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 506 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 507 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 508 | TIME_AND_TSC( "CTR_DRBG (NOPR)", |
| 509 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 510 | polarssl_exit(1) ); |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 511 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 512 | if( ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 513 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 514 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_ON ); |
| 515 | TIME_AND_TSC( "CTR_DRBG (PR)", |
| 516 | if( ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 517 | polarssl_exit(1) ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 518 | ctr_drbg_free( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 519 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 520 | #endif |
| 521 | |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 522 | #if defined(POLARSSL_HMAC_DRBG_C) |
| 523 | if( todo.hmac_drbg ) |
| 524 | { |
| 525 | hmac_drbg_context hmac_drbg; |
| 526 | const md_info_t *md_info; |
| 527 | |
| 528 | #if defined(POLARSSL_SHA1_C) |
| 529 | if( ( md_info = md_info_from_type( POLARSSL_MD_SHA1 ) ) == NULL ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 530 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 531 | |
| 532 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 533 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 534 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", |
| 535 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 536 | polarssl_exit(1) ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 537 | hmac_drbg_free( &hmac_drbg ); |
| 538 | |
| 539 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 540 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 541 | hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 542 | POLARSSL_HMAC_DRBG_PR_ON ); |
| 543 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", |
| 544 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 545 | polarssl_exit(1) ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 546 | hmac_drbg_free( &hmac_drbg ); |
| 547 | #endif |
| 548 | |
| 549 | #if defined(POLARSSL_SHA256_C) |
| 550 | if( ( md_info = md_info_from_type( POLARSSL_MD_SHA256 ) ) == NULL ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 551 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 552 | |
| 553 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 554 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 555 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", |
| 556 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 557 | polarssl_exit(1) ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 558 | hmac_drbg_free( &hmac_drbg ); |
| 559 | |
| 560 | if( hmac_drbg_init( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 561 | polarssl_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 562 | hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 563 | POLARSSL_HMAC_DRBG_PR_ON ); |
| 564 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", |
| 565 | if( hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 566 | polarssl_exit(1) ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 567 | hmac_drbg_free( &hmac_drbg ); |
| 568 | #endif |
| 569 | } |
| 570 | #endif |
| 571 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 572 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 573 | if( todo.rsa ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 574 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 575 | int keysize; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 576 | rsa_context rsa; |
| 577 | for( keysize = 1024; keysize <= 4096; keysize *= 2 ) |
| 578 | { |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 579 | polarssl_snprintf( title, sizeof( title ), "RSA-%d", keysize ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 580 | |
| 581 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 582 | rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 ); |
| 583 | |
| 584 | TIME_PUBLIC( title, " public", |
| 585 | buf[0] = 0; |
| 586 | ret = rsa_public( &rsa, buf, buf ) ); |
| 587 | |
| 588 | TIME_PUBLIC( title, "private", |
| 589 | buf[0] = 0; |
| 590 | ret = rsa_private( &rsa, myrand, NULL, buf, buf ) ); |
| 591 | |
| 592 | rsa_free( &rsa ); |
| 593 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 594 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 595 | #endif |
| 596 | |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 597 | #if defined(POLARSSL_DHM_C) && defined(POLARSSL_BIGNUM_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 598 | if( todo.dhm ) |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 599 | { |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 600 | int dhm_sizes[DHM_SIZES] = { 1024, 2048, 3072 }; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 601 | const char *dhm_P[DHM_SIZES] = { |
| 602 | POLARSSL_DHM_RFC5114_MODP_1024_P, |
| 603 | POLARSSL_DHM_RFC3526_MODP_2048_P, |
| 604 | POLARSSL_DHM_RFC3526_MODP_3072_P, |
| 605 | }; |
| 606 | const char *dhm_G[DHM_SIZES] = { |
| 607 | POLARSSL_DHM_RFC5114_MODP_1024_G, |
| 608 | POLARSSL_DHM_RFC3526_MODP_2048_G, |
| 609 | POLARSSL_DHM_RFC3526_MODP_3072_G, |
| 610 | }; |
| 611 | |
| 612 | dhm_context dhm; |
| 613 | size_t olen; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 614 | for( i = 0; i < DHM_SIZES; i++ ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 615 | { |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 616 | dhm_init( &dhm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 617 | |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 618 | if( mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || |
| 619 | mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) |
| 620 | { |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 621 | polarssl_exit( 1 ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 624 | dhm.len = mpi_size( &dhm.P ); |
Paul Bakker | 840ab20 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 625 | dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 626 | if( mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 627 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 628 | |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 629 | polarssl_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 630 | TIME_PUBLIC( title, "handshake", |
| 631 | olen = sizeof( buf ); |
Paul Bakker | 840ab20 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 632 | ret |= dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 633 | myrand, NULL ); |
| 634 | ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); |
| 635 | |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 636 | polarssl_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 637 | TIME_PUBLIC( title, "handshake", |
| 638 | olen = sizeof( buf ); |
| 639 | ret |= dhm_calc_secret( &dhm, buf, &olen, myrand, NULL ) ); |
| 640 | |
| 641 | dhm_free( &dhm ); |
| 642 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 643 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 644 | #endif |
| 645 | |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 646 | #if defined(POLARSSL_ECDSA_C) |
| 647 | if( todo.ecdsa ) |
| 648 | { |
| 649 | ecdsa_context ecdsa; |
| 650 | const ecp_curve_info *curve_info; |
| 651 | size_t sig_len; |
| 652 | |
| 653 | memset( buf, 0x2A, sizeof( buf ) ); |
| 654 | |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 655 | for( curve_info = ecp_curve_list(); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 656 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 657 | curve_info++ ) |
| 658 | { |
| 659 | ecdsa_init( &ecdsa ); |
| 660 | |
| 661 | if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 662 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 663 | ecp_clear_precomputed( &ecdsa.grp ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 664 | |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 665 | polarssl_snprintf( title, sizeof( title ), "ECDSA-%s", |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 666 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 667 | TIME_PUBLIC( title, "sign", |
| 668 | ret = ecdsa_write_signature( &ecdsa, buf, curve_info->size, |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 669 | tmp, &sig_len, myrand, NULL ) ); |
| 670 | |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 671 | ecdsa_free( &ecdsa ); |
| 672 | } |
| 673 | |
| 674 | for( curve_info = ecp_curve_list(); |
| 675 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 676 | curve_info++ ) |
| 677 | { |
| 678 | ecdsa_init( &ecdsa ); |
| 679 | |
| 680 | if( ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 || |
| 681 | ecdsa_write_signature( &ecdsa, buf, curve_info->size, |
| 682 | tmp, &sig_len, myrand, NULL ) != 0 ) |
| 683 | { |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 684 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 685 | } |
| 686 | ecp_clear_precomputed( &ecdsa.grp ); |
| 687 | |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 688 | polarssl_snprintf( title, sizeof( title ), "ECDSA-%s", |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 689 | curve_info->name ); |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 690 | TIME_PUBLIC( title, "verify", |
| 691 | ret = ecdsa_read_signature( &ecdsa, buf, curve_info->size, |
| 692 | tmp, sig_len ) ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 693 | |
| 694 | ecdsa_free( &ecdsa ); |
| 695 | } |
| 696 | } |
| 697 | #endif |
| 698 | |
| 699 | #if defined(POLARSSL_ECDH_C) |
| 700 | if( todo.ecdh ) |
| 701 | { |
| 702 | ecdh_context ecdh; |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 703 | #if defined(POLARSSL_ECP_DP_M255_ENABLED) |
| 704 | mpi z; |
| 705 | #endif |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 706 | const ecp_curve_info *curve_info; |
| 707 | size_t olen; |
| 708 | |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 709 | for( curve_info = ecp_curve_list(); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 710 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 711 | curve_info++ ) |
| 712 | { |
| 713 | ecdh_init( &ecdh ); |
| 714 | |
| 715 | if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 || |
| 716 | ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 717 | myrand, NULL ) != 0 || |
| 718 | ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) |
| 719 | { |
Rich Evans | 77d3638 | 2015-01-30 12:12:11 +0000 | [diff] [blame] | 720 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 721 | } |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 722 | ecp_clear_precomputed( &ecdh.grp ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 723 | |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 724 | polarssl_snprintf( title, sizeof( title ), "ECDHE-%s", |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 725 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 726 | TIME_PUBLIC( title, "handshake", |
| 727 | ret |= ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 728 | myrand, NULL ); |
| 729 | ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
| 730 | myrand, NULL ) ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 731 | ecdh_free( &ecdh ); |
| 732 | } |
| 733 | |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 734 | /* Curve25519 needs to be handled separately */ |
| 735 | #if defined(POLARSSL_ECP_DP_M255_ENABLED) |
| 736 | ecdh_init( &ecdh ); |
| 737 | mpi_init( &z ); |
| 738 | |
| 739 | if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 || |
| 740 | ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 ) |
| 741 | { |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 742 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | TIME_PUBLIC( "ECDHE-Curve25519", "handshake", |
| 746 | ret |= ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, |
| 747 | myrand, NULL ); |
| 748 | ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, |
| 749 | myrand, NULL ) ); |
| 750 | |
| 751 | ecdh_free( &ecdh ); |
| 752 | mpi_free( &z ); |
| 753 | #endif |
| 754 | |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 755 | for( curve_info = ecp_curve_list(); |
| 756 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 757 | curve_info++ ) |
| 758 | { |
| 759 | ecdh_init( &ecdh ); |
| 760 | |
| 761 | if( ecp_use_known_dp( &ecdh.grp, curve_info->grp_id ) != 0 || |
| 762 | ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 763 | myrand, NULL ) != 0 || |
| 764 | ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 || |
| 765 | ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 766 | myrand, NULL ) != 0 ) |
| 767 | { |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 768 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 769 | } |
| 770 | ecp_clear_precomputed( &ecdh.grp ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 771 | |
Rich Evans | 783d9d1 | 2015-01-30 11:11:57 +0000 | [diff] [blame] | 772 | polarssl_snprintf( title, sizeof( title ), "ECDH-%s", |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 773 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 774 | TIME_PUBLIC( title, "handshake", |
| 775 | ret |= ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
| 776 | myrand, NULL ) ); |
| 777 | ecdh_free( &ecdh ); |
| 778 | } |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 779 | |
| 780 | /* Curve25519 needs to be handled separately */ |
| 781 | #if defined(POLARSSL_ECP_DP_M255_ENABLED) |
| 782 | ecdh_init( &ecdh ); |
| 783 | mpi_init( &z ); |
| 784 | |
| 785 | if( ecp_use_known_dp( &ecdh.grp, POLARSSL_ECP_DP_M255 ) != 0 || |
| 786 | ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, |
| 787 | myrand, NULL ) != 0 || |
| 788 | ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 ) |
| 789 | { |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 790 | polarssl_exit( 1 ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | TIME_PUBLIC( "ECDH-Curve25519", "handshake", |
| 794 | ret |= ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, |
| 795 | myrand, NULL ) ); |
| 796 | |
| 797 | ecdh_free( &ecdh ); |
| 798 | mpi_free( &z ); |
| 799 | #endif |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 800 | } |
| 801 | #endif |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 802 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 803 | polarssl_printf( "\n" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 804 | |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 805 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 806 | memory_buffer_alloc_free(); |
| 807 | #endif |
| 808 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 809 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 810 | polarssl_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 811 | fflush( stdout ); getchar(); |
| 812 | #endif |
| 813 | |
| 814 | return( 0 ); |
| 815 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 816 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 817 | #endif /* POLARSSL_TIMING_C */ |