Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Benchmark demonstration program |
| 3 | * |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame^] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | #if !defined(TARGET_LIKE_MBED) |
| 48 | |
| 49 | #include <stdio.h> |
| 50 | |
| 51 | int main() { |
| 52 | printf("this version of this program only works on mbed OS\n"); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | #else |
| 57 | |
| 58 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 59 | #include "mbedtls/config.h" |
| 60 | #else |
| 61 | #include MBEDTLS_CONFIG_FILE |
| 62 | #endif |
| 63 | |
| 64 | #if defined(MBEDTLS_PLATFORM_C) |
| 65 | #include "mbedtls/platform.h" |
| 66 | #else |
| 67 | #include <stdio.h> |
| 68 | #define mbedtls_exit exit |
| 69 | #define mbedtls_printf printf |
| 70 | #define mbedtls_snprintf snprintf |
| 71 | #define mbedtls_free free |
| 72 | #endif |
| 73 | |
| 74 | #include <string.h> |
| 75 | |
| 76 | #include "mbedtls/md4.h" |
| 77 | #include "mbedtls/md5.h" |
| 78 | #include "mbedtls/ripemd160.h" |
| 79 | #include "mbedtls/sha1.h" |
| 80 | #include "mbedtls/sha256.h" |
| 81 | #include "mbedtls/sha512.h" |
| 82 | #include "mbedtls/arc4.h" |
| 83 | #include "mbedtls/des.h" |
| 84 | #include "mbedtls/aes.h" |
| 85 | #include "mbedtls/blowfish.h" |
| 86 | #include "mbedtls/camellia.h" |
| 87 | #include "mbedtls/gcm.h" |
| 88 | #include "mbedtls/ccm.h" |
| 89 | #include "mbedtls/havege.h" |
| 90 | #include "mbedtls/ctr_drbg.h" |
| 91 | #include "mbedtls/hmac_drbg.h" |
| 92 | #include "mbedtls/rsa.h" |
| 93 | #include "mbedtls/pk.h" |
| 94 | #include "mbedtls/dhm.h" |
| 95 | #include "mbedtls/ecdsa.h" |
| 96 | #include "mbedtls/ecdh.h" |
| 97 | #include "mbedtls/error.h" |
| 98 | |
Manuel Pégourié-Gonnard | 71956c9 | 2015-10-21 17:59:05 +0200 | [diff] [blame] | 99 | #include "mbed-drivers/mbed.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 100 | |
| 101 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 102 | #include "mbedtls/memory_buffer_alloc.h" |
| 103 | #endif |
| 104 | |
| 105 | #define RSA_PRIVATE_KEY_2048 \ |
| 106 | "-----BEGIN RSA PRIVATE KEY-----\r\n" \ |
| 107 | "MIIEogIBAAKCAQEA2dwVr+IMGEtA2/MCP6fA5eb/6B18Bq6e7gw8brNPkm3E6LyR\r\n" \ |
| 108 | "4DnMJVxZmw3bPDKBDoKzfntkMESi/Yw5UopLtVfjGfWeQWPClqffLZBsZ60BRAsg\r\n" \ |
| 109 | "/g+ID5tgzxSuxzftypK59uexOVCAm7hCKZHGO3DbI7bLY27j7VAgEP7d/yuaz5Fx\r\n" \ |
| 110 | "Kl/vu7shqrBoz6ABJVJD3KC8nUiMRUCXRINmxbyUUjA4DnicZv6+xrGKr36r6M8h\r\n" \ |
| 111 | "VYLa5msKc8WzbnBWzpUsrpb4/r7ML+qp92gdSfVJ8/bLiU7h2C7faDA59uaqrFK9\r\n" \ |
| 112 | "xmDdx7FaWhGQs3LWW6w1UNgkPS0FDYUslpsnsQIDAQABAoIBAC7IJNwM5V3+IuJY\r\n" \ |
| 113 | "T35Nzo1PyloUosJokvY5KGz5Ejg2XBdCDu0gXCcVqqQyGIbXrYDpLhQV+RCoXHun\r\n" \ |
| 114 | "tdN0oQdC5SB47s/J1Uo2qCUHo0+sBd6PqTkFKsl3KxWssk9TQjvCwC412IefMs69\r\n" \ |
| 115 | "hW+ZvwCanmQP56Lle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OSKC6am8V/MDF0h\r\n" \ |
| 129 | "kyN9AoGAT9oOiEXMolbkDZw/QCaBiRoAGlGlNYUkJ+58U6OjIZLISw6aFv+Y2uE0\r\n" \ |
| 130 | "mEImItjuYZtSYKblWikp6ldPoKlt9bwEFe3c6IZ8kJ3+xyEyAGrvjXjEY7PzP6dp\r\n" \ |
| 131 | "Ajbjp9X9uocEBv9W/KsBLdQ7yizcL/toHwdBO4vQqmqTvAc5IIw=\r\n" \ |
| 132 | "-----END RSA PRIVATE KEY-----\r\n" |
| 133 | |
| 134 | #define RSA_PRIVATE_KEY_4096 \ |
| 135 | "-----BEGIN RSA PRIVATE KEY-----\r\n" \ |
| 136 | "MIIJKgIBAAKCAgEAmkdGjoIshJuOt2NO47qB3Z3yyvmLg2j351isItSNuFQU3qr+\r\n" \ |
| 137 | "jXHIeANf03yw/K0Zvos8RPd+CqLjoxAQL3QDH4bZAl88bIo29i+SANbNSrKQmc0k\r\n" \ |
| 138 | "pH+yzw3alDzO0GZaOPZjsbo6AwBrno5msi0vRuC2aY8vGLPsZWSyLai7tneS1j/o\r\n" \ |
| 139 | "vYW6XIo8Cj61j2Ypy9HhVUW/4Wc+zAT25D/x7jTpkqJLWWT+YzibNbOY48M5eJcB\r\n" \ |
| 140 | "6/sMyUIeI3/u/wXyMrooNyLiCpedkuHRA0m7u5cWPTUISTunSRlVFij/NHJjuU8e\r\n" \ |
| 141 | "wA3B29yfZFsUqDEnyc+OxniIueAixTomVszxAaVn8zFEbYhFMPqziiFp99u3jfeG\r\n" \ |
| 142 | "k1q9mmUi/uCfUC4e2IC5rqq1ZbKSduH7Ug/Vn2bGQahww0sZFRHDXFrnBcotcW+M\r\n" \ |
| 143 | "bnC290VBDnYgzmdYrIOxuPb2aUwJo4ZlbKh5uBB1PigMuyhLKibQ1a+V5ZJGdpP6\r\n" \ |
| 144 | "SE9PGIdgYWSmh2QEMuLE6v+wTO2LQ5JgqsvFfi3GIZvkn0s8jTS72Jq2uMkFkMer\r\n" \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Lg\r\n" \ |
| 172 | "qeHvVmxrcXqZoFm44Ba7tN/pP0mi9HKyviZT4tmV3IYEbn3JyGGsfkUuVU9wEUfg\r\n" \ |
| 173 | "MCrgrVxrwfketAzooiHMjkVL2ASjzAJTmEvdAPETYXxzJD9LN0ovY3t8JfAC37IN\r\n" \ |
| 174 | "sVXS8/MCggEBALByOS59Y4Ktq1rLBQx8djwQyuneP0wZohUVAx7Gk7xZIfklQDyg\r\n" \ |
| 175 | "v/R4PrcVezstcPpDnykdjScCsGJR+uWc0v667I/ttP/e6utz5hVmmBGu965dPAzE\r\n" \ |
| 176 | "c1ggaSkOqFfRg/Nr2Qbf+fH0YPnHYSqHe/zSt0OMIvaaeXLcdKhEDSCUBRhE1HWB\r\n" \ |
| 177 | "kxR046WzgBeYzNQwycz9xwqsctJKGpeR9ute+5ANHPd3X9XtID0fqz8ctI5eZaSw\r\n" \ |
| 178 | "wApIW01ZQcAF8B+4WkkVuFXnpWW33yCOaRyPVOPHpnclr5WU1fS+3Q85QkW9rkej\r\n" \ |
| 179 | "97zlkl0QY9AHJqrXnoML1ywAK7ns+MVyNK8CggEAf62xcKZhOb1djeF72Ms+i/i/\r\n" \ |
| 180 | "WIAq4Q4YpsElgvJTHpNH2v9g4ngSTKe3ws3bGc502sWRlhcoTFMOW2rJNe/iqKkb\r\n" \ |
| 181 | "3cdeTkseDbpqozmJWz9dJWSVtXas2bZjzBEa//gQ7nHGVeQdqZJQ9rxPsoOAkfpi\r\n" \ |
| 182 | "qCFrmfUVUqC53e3XMt8+W+aSvKl+JZiB9ozkO9A6Q0vfQLKtjUMdQE3XaCFQT8DI\r\n" \ |
| 183 | "smaLBlBmeRaBpc02ENeC4ADlWosm1SwgxqMhuh2Alba/GrHOoPlVl4hDs9Fb5a6R\r\n" \ |
| 184 | "rmpXSt07GAxnG6j9jssA95E4rc1zO0CVKG5bvjVTxwi/sT0/VVX7VsJM4uTAQg==\r\n" \ |
| 185 | "-----END RSA PRIVATE KEY-----\r\n" |
| 186 | |
| 187 | #if defined _MSC_VER && !defined snprintf |
| 188 | #define snprintf _snprintf |
| 189 | #endif |
| 190 | |
| 191 | /* |
| 192 | * For heap usage estimates, we need an estimate of the overhead per allocated |
| 193 | * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block, |
| 194 | * so use that as our baseline. |
| 195 | */ |
| 196 | #define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) ) |
| 197 | |
| 198 | /* |
| 199 | * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined. |
| 200 | */ |
| 201 | #define HEAP_SIZE (1u << 16) // 64k |
| 202 | |
| 203 | #define BUFSIZE 1024 |
| 204 | #define HEADER_FORMAT " %-24s : " |
| 205 | #define TITLE_LEN 25 |
| 206 | |
| 207 | #define OPTIONS \ |
| 208 | "md4, md5, ripemd160, sha1, sha256, sha512,\r\n" \ |
| 209 | "arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish,\r\n" \ |
| 210 | "havege, ctr_drbg, hmac_drbg\r\n" \ |
| 211 | "rsa, dhm, ecdsa, ecdh.\r\n" |
| 212 | |
| 213 | #if defined(MBEDTLS_ERROR_C) |
| 214 | #define PRINT_ERROR \ |
| 215 | mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \ |
| 216 | mbedtls_printf( "FAILED: %s\r\n", tmp ); |
| 217 | #else |
| 218 | #define PRINT_ERROR \ |
| 219 | mbedtls_printf( "FAILED: -0x%04x\r\n", -ret ); |
| 220 | #endif |
| 221 | |
| 222 | static unsigned long mbedtls_timing_hardclock( void ) |
| 223 | { |
| 224 | static int dwt_started = 0; |
| 225 | |
| 226 | if( dwt_started == 0 ) |
| 227 | { |
| 228 | CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
| 229 | DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; |
| 230 | } |
| 231 | |
| 232 | return( DWT->CYCCNT ); |
| 233 | } |
| 234 | |
| 235 | static volatile int alarmed; |
| 236 | static void alarm() { alarmed = 1; } |
| 237 | |
| 238 | #define TIME_AND_TSC( TITLE, CODE ) \ |
| 239 | do { \ |
| 240 | unsigned long i, j, tsc; \ |
| 241 | Timeout t; \ |
| 242 | \ |
| 243 | mbedtls_printf( HEADER_FORMAT, TITLE ); \ |
| 244 | fflush( stdout ); \ |
| 245 | \ |
| 246 | for( i = 1, alarmed = 0, t.attach( alarm, 1.0 ); !alarmed; i++ ) \ |
| 247 | { \ |
| 248 | CODE; \ |
| 249 | } \ |
| 250 | \ |
| 251 | tsc = mbedtls_timing_hardclock(); \ |
| 252 | for( j = 0; j < 1024; j++ ) \ |
| 253 | { \ |
| 254 | CODE; \ |
| 255 | } \ |
| 256 | \ |
Ron Eldor | 0728d69 | 2017-11-29 12:08:35 +0200 | [diff] [blame] | 257 | mbedtls_printf( "%9lu KiB/s, %9lu cycles/byte\r\n", \ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 258 | i * BUFSIZE / 1024, \ |
| 259 | ( mbedtls_timing_hardclock() - tsc ) / ( j * BUFSIZE ) ); \ |
| 260 | } while( 0 ) |
| 261 | |
| 262 | #if defined(MBEDTLS_ERROR_C) |
| 263 | #define PRINT_ERROR \ |
| 264 | mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \ |
| 265 | mbedtls_printf( "FAILED: %s\r\n", tmp ); |
| 266 | #else |
| 267 | #define PRINT_ERROR \ |
| 268 | mbedtls_printf( "FAILED: -0x%04x\r\n", -ret ); |
| 269 | #endif |
| 270 | |
| 271 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG) |
| 272 | |
| 273 | #define MEMORY_MEASURE_INIT \ |
| 274 | size_t max_used, max_blocks, max_bytes; \ |
| 275 | size_t prv_used, prv_blocks; \ |
| 276 | mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \ |
| 277 | mbedtls_memory_buffer_alloc_max_reset( ); |
| 278 | |
| 279 | #define MEMORY_MEASURE_PRINT( title_len ) \ |
| 280 | mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \ |
| 281 | for( i = 12 - title_len; i != 0; i-- ) mbedtls_printf( " " ); \ |
| 282 | max_used -= prv_used; \ |
| 283 | max_blocks -= prv_blocks; \ |
| 284 | max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \ |
| 285 | mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes ); |
| 286 | |
| 287 | #else |
| 288 | #define MEMORY_MEASURE_INIT |
| 289 | #define MEMORY_MEASURE_PRINT( title_len ) |
| 290 | #endif |
| 291 | |
| 292 | #define TIME_PUBLIC( TITLE, TYPE, CODE ) \ |
| 293 | do { \ |
| 294 | unsigned long ms; \ |
| 295 | int ret = 0; \ |
| 296 | Timer t; \ |
| 297 | MEMORY_MEASURE_INIT; \ |
| 298 | \ |
| 299 | mbedtls_printf( HEADER_FORMAT, TITLE ); \ |
| 300 | fflush( stdout ); \ |
| 301 | \ |
| 302 | t.start(); \ |
| 303 | CODE; \ |
| 304 | t.stop(); \ |
| 305 | ms = t.read_ms(); \ |
| 306 | \ |
| 307 | if( ret != 0 ) \ |
| 308 | { \ |
| 309 | PRINT_ERROR; \ |
| 310 | } \ |
| 311 | else \ |
| 312 | { \ |
| 313 | mbedtls_printf( "%6lu ms/" TYPE, ms ); \ |
| 314 | MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \ |
| 315 | mbedtls_printf( "\r\n" ); \ |
| 316 | } \ |
| 317 | } while( 0 ) |
| 318 | |
| 319 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
| 320 | { |
| 321 | size_t use_len; |
| 322 | int rnd; |
| 323 | |
| 324 | if( rng_state != NULL ) |
| 325 | rng_state = NULL; |
| 326 | |
| 327 | while( len > 0 ) |
| 328 | { |
| 329 | use_len = len; |
| 330 | if( use_len > sizeof(int) ) |
| 331 | use_len = sizeof(int); |
| 332 | |
| 333 | rnd = rand(); |
| 334 | memcpy( output, &rnd, use_len ); |
| 335 | output += use_len; |
| 336 | len -= use_len; |
| 337 | } |
| 338 | |
| 339 | return( 0 ); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Clear some memory that was used to prepare the context |
| 344 | */ |
| 345 | #if defined(MBEDTLS_ECP_C) |
| 346 | void ecp_clear_precomputed( mbedtls_ecp_group *grp ) |
| 347 | { |
| 348 | if( grp->T != NULL ) |
| 349 | { |
| 350 | size_t i; |
| 351 | for( i = 0; i < grp->T_size; i++ ) |
| 352 | mbedtls_ecp_point_free( &grp->T[i] ); |
| 353 | mbedtls_free( grp->T ); |
| 354 | } |
| 355 | grp->T = NULL; |
| 356 | grp->T_size = 0; |
| 357 | } |
| 358 | #else |
| 359 | #define ecp_clear_precomputed( g ) |
| 360 | #endif |
| 361 | |
| 362 | unsigned char buf[BUFSIZE]; |
| 363 | |
| 364 | typedef struct { |
| 365 | char md4, md5, ripemd160, sha1, sha256, sha512, |
| 366 | arc4, des3, des, aes_cbc, aes_gcm, aes_ccm, camellia, blowfish, |
| 367 | havege, ctr_drbg, hmac_drbg, |
| 368 | rsa, dhm, ecdsa, ecdh; |
| 369 | } todo_list; |
| 370 | |
| 371 | int benchmark( int argc, char *argv[] ) |
| 372 | { |
| 373 | int i; |
| 374 | unsigned char tmp[200]; |
| 375 | char title[TITLE_LEN]; |
| 376 | todo_list todo; |
| 377 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 378 | unsigned char malloc_buf[HEAP_SIZE] = { 0 }; |
| 379 | #endif |
| 380 | |
| 381 | if( argc <= 1 ) |
| 382 | { |
| 383 | memset( &todo, 1, sizeof( todo ) ); |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | memset( &todo, 0, sizeof( todo ) ); |
| 388 | |
| 389 | for( i = 1; i < argc; i++ ) |
| 390 | { |
| 391 | if( strcmp( argv[i], "md4" ) == 0 ) |
| 392 | todo.md4 = 1; |
| 393 | else if( strcmp( argv[i], "md5" ) == 0 ) |
| 394 | todo.md5 = 1; |
| 395 | else if( strcmp( argv[i], "ripemd160" ) == 0 ) |
| 396 | todo.ripemd160 = 1; |
| 397 | else if( strcmp( argv[i], "sha1" ) == 0 ) |
| 398 | todo.sha1 = 1; |
| 399 | else if( strcmp( argv[i], "sha256" ) == 0 ) |
| 400 | todo.sha256 = 1; |
| 401 | else if( strcmp( argv[i], "sha512" ) == 0 ) |
| 402 | todo.sha512 = 1; |
| 403 | else if( strcmp( argv[i], "arc4" ) == 0 ) |
| 404 | todo.arc4 = 1; |
| 405 | else if( strcmp( argv[i], "des3" ) == 0 ) |
| 406 | todo.des3 = 1; |
| 407 | else if( strcmp( argv[i], "des" ) == 0 ) |
| 408 | todo.des = 1; |
| 409 | else if( strcmp( argv[i], "aes_cbc" ) == 0 ) |
| 410 | todo.aes_cbc = 1; |
| 411 | else if( strcmp( argv[i], "aes_gcm" ) == 0 ) |
| 412 | todo.aes_gcm = 1; |
| 413 | else if( strcmp( argv[i], "aes_ccm" ) == 0 ) |
| 414 | todo.aes_ccm = 1; |
| 415 | else if( strcmp( argv[i], "camellia" ) == 0 ) |
| 416 | todo.camellia = 1; |
| 417 | else if( strcmp( argv[i], "blowfish" ) == 0 ) |
| 418 | todo.blowfish = 1; |
| 419 | else if( strcmp( argv[i], "havege" ) == 0 ) |
| 420 | todo.havege = 1; |
| 421 | else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) |
| 422 | todo.ctr_drbg = 1; |
| 423 | else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) |
| 424 | todo.hmac_drbg = 1; |
| 425 | else if( strcmp( argv[i], "rsa" ) == 0 ) |
| 426 | todo.rsa = 1; |
| 427 | else if( strcmp( argv[i], "dhm" ) == 0 ) |
| 428 | todo.dhm = 1; |
| 429 | else if( strcmp( argv[i], "ecdsa" ) == 0 ) |
| 430 | todo.ecdsa = 1; |
| 431 | else if( strcmp( argv[i], "ecdh" ) == 0 ) |
| 432 | todo.ecdh = 1; |
| 433 | else |
| 434 | { |
| 435 | mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] ); |
| 436 | mbedtls_printf( "Available options: " OPTIONS ); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | mbedtls_printf( "\r\n\r\n" ); |
| 442 | |
| 443 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 444 | mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) ); |
| 445 | #endif |
| 446 | memset( buf, 0xAA, sizeof( buf ) ); |
| 447 | memset( tmp, 0xBB, sizeof( tmp ) ); |
| 448 | |
| 449 | #if defined(MBEDTLS_MD4_C) |
| 450 | if( todo.md4 ) |
| 451 | TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) ); |
| 452 | #endif |
| 453 | |
| 454 | #if defined(MBEDTLS_MD5_C) |
| 455 | if( todo.md5 ) |
| 456 | TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) ); |
| 457 | #endif |
| 458 | |
| 459 | #if defined(MBEDTLS_RIPEMD160_C) |
| 460 | if( todo.ripemd160 ) |
| 461 | TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) ); |
| 462 | #endif |
| 463 | |
| 464 | #if defined(MBEDTLS_SHA1_C) |
| 465 | if( todo.sha1 ) |
| 466 | TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) ); |
| 467 | #endif |
| 468 | |
| 469 | #if defined(MBEDTLS_SHA256_C) |
| 470 | if( todo.sha256 ) |
| 471 | TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) ); |
| 472 | #endif |
| 473 | |
| 474 | #if defined(MBEDTLS_SHA512_C) |
| 475 | if( todo.sha512 ) |
| 476 | TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) ); |
| 477 | #endif |
| 478 | |
| 479 | #if defined(MBEDTLS_ARC4_C) |
| 480 | if( todo.arc4 ) |
| 481 | { |
| 482 | mbedtls_arc4_context arc4; |
| 483 | mbedtls_arc4_init( &arc4 ); |
| 484 | mbedtls_arc4_setup( &arc4, tmp, 32 ); |
| 485 | TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); |
| 486 | mbedtls_arc4_free( &arc4 ); |
| 487 | } |
| 488 | #endif |
| 489 | |
| 490 | #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 491 | if( todo.des3 ) |
| 492 | { |
| 493 | mbedtls_des3_context des3; |
| 494 | mbedtls_des3_init( &des3 ); |
| 495 | mbedtls_des3_set3key_enc( &des3, tmp ); |
| 496 | TIME_AND_TSC( "3DES", |
| 497 | mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 498 | mbedtls_des3_free( &des3 ); |
| 499 | } |
| 500 | |
| 501 | if( todo.des ) |
| 502 | { |
| 503 | mbedtls_des_context des; |
| 504 | mbedtls_des_init( &des ); |
| 505 | mbedtls_des_setkey_enc( &des, tmp ); |
| 506 | TIME_AND_TSC( "DES", |
| 507 | mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 508 | mbedtls_des_free( &des ); |
| 509 | } |
| 510 | #endif |
| 511 | |
| 512 | #if defined(MBEDTLS_AES_C) |
| 513 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 514 | if( todo.aes_cbc ) |
| 515 | { |
| 516 | int keysize; |
| 517 | mbedtls_aes_context aes; |
| 518 | mbedtls_aes_init( &aes ); |
| 519 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 520 | { |
| 521 | mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); |
| 522 | |
| 523 | memset( buf, 0, sizeof( buf ) ); |
| 524 | memset( tmp, 0, sizeof( tmp ) ); |
| 525 | mbedtls_aes_setkey_enc( &aes, tmp, keysize ); |
| 526 | |
| 527 | TIME_AND_TSC( title, |
| 528 | mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 529 | } |
| 530 | mbedtls_aes_free( &aes ); |
| 531 | } |
| 532 | #endif |
| 533 | #if defined(MBEDTLS_GCM_C) |
| 534 | if( todo.aes_gcm ) |
| 535 | { |
| 536 | int keysize; |
| 537 | mbedtls_gcm_context gcm; |
| 538 | |
| 539 | mbedtls_gcm_init( &gcm ); |
| 540 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 541 | { |
| 542 | mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); |
| 543 | |
| 544 | memset( buf, 0, sizeof( buf ) ); |
| 545 | memset( tmp, 0, sizeof( tmp ) ); |
| 546 | mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize ); |
| 547 | |
| 548 | TIME_AND_TSC( title, |
| 549 | mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp, |
| 550 | 12, NULL, 0, buf, buf, 16, tmp ) ); |
| 551 | |
| 552 | mbedtls_gcm_free( &gcm ); |
| 553 | } |
| 554 | } |
| 555 | #endif |
| 556 | #if defined(MBEDTLS_CCM_C) |
| 557 | if( todo.aes_ccm ) |
| 558 | { |
| 559 | int keysize; |
| 560 | mbedtls_ccm_context ccm; |
| 561 | |
| 562 | mbedtls_ccm_init( &ccm ); |
| 563 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 564 | { |
| 565 | mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); |
| 566 | |
| 567 | memset( buf, 0, sizeof( buf ) ); |
| 568 | memset( tmp, 0, sizeof( tmp ) ); |
| 569 | mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize ); |
| 570 | |
| 571 | TIME_AND_TSC( title, |
| 572 | mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp, |
| 573 | 12, NULL, 0, buf, buf, tmp, 16 ) ); |
| 574 | |
| 575 | mbedtls_ccm_free( &ccm ); |
| 576 | } |
| 577 | } |
| 578 | #endif |
| 579 | #endif |
| 580 | |
| 581 | #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 582 | if( todo.camellia ) |
| 583 | { |
| 584 | int keysize; |
| 585 | mbedtls_camellia_context camellia; |
| 586 | mbedtls_camellia_init( &camellia ); |
| 587 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 588 | { |
| 589 | mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); |
| 590 | |
| 591 | memset( buf, 0, sizeof( buf ) ); |
| 592 | memset( tmp, 0, sizeof( tmp ) ); |
| 593 | mbedtls_camellia_setkey_enc( &camellia, tmp, keysize ); |
| 594 | |
| 595 | TIME_AND_TSC( title, |
| 596 | mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT, |
| 597 | BUFSIZE, tmp, buf, buf ) ); |
| 598 | } |
| 599 | mbedtls_camellia_free( &camellia ); |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC) |
| 604 | if( todo.blowfish ) |
| 605 | { |
| 606 | int keysize; |
| 607 | mbedtls_blowfish_context blowfish; |
| 608 | mbedtls_blowfish_init( &blowfish ); |
| 609 | |
| 610 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 611 | { |
| 612 | mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); |
| 613 | |
| 614 | memset( buf, 0, sizeof( buf ) ); |
| 615 | memset( tmp, 0, sizeof( tmp ) ); |
| 616 | mbedtls_blowfish_setkey( &blowfish, tmp, keysize ); |
| 617 | |
| 618 | TIME_AND_TSC( title, |
| 619 | mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE, |
| 620 | tmp, buf, buf ) ); |
| 621 | } |
| 622 | |
| 623 | mbedtls_blowfish_free( &blowfish ); |
| 624 | } |
| 625 | #endif |
| 626 | |
| 627 | #if defined(MBEDTLS_HAVEGE_C) |
| 628 | if( todo.havege ) |
| 629 | { |
| 630 | mbedtls_havege_state hs; |
| 631 | mbedtls_havege_init( &hs ); |
| 632 | TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) ); |
| 633 | mbedtls_havege_free( &hs ); |
| 634 | } |
| 635 | #endif |
| 636 | |
| 637 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 638 | if( todo.ctr_drbg ) |
| 639 | { |
| 640 | mbedtls_ctr_drbg_context ctr_drbg; |
| 641 | |
| 642 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 643 | |
| 644 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
| 645 | mbedtls_exit(1); |
| 646 | TIME_AND_TSC( "CTR_DRBG (NOPR)", |
| 647 | if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 648 | mbedtls_exit(1) ); |
| 649 | |
| 650 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) |
| 651 | mbedtls_exit(1); |
| 652 | mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON ); |
| 653 | TIME_AND_TSC( "CTR_DRBG (PR)", |
| 654 | if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 655 | mbedtls_exit(1) ); |
| 656 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 657 | } |
| 658 | #endif |
| 659 | |
| 660 | #if defined(MBEDTLS_HMAC_DRBG_C) |
| 661 | if( todo.hmac_drbg ) |
| 662 | { |
| 663 | mbedtls_hmac_drbg_context hmac_drbg; |
| 664 | const mbedtls_md_info_t *md_info; |
| 665 | |
| 666 | mbedtls_hmac_drbg_init( &hmac_drbg ); |
| 667 | |
| 668 | #if defined(MBEDTLS_SHA1_C) |
| 669 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 670 | mbedtls_exit(1); |
| 671 | |
| 672 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 673 | mbedtls_exit(1); |
| 674 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", |
| 675 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 676 | mbedtls_exit(1) ); |
| 677 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
| 678 | |
| 679 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 680 | mbedtls_exit(1); |
| 681 | mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 682 | MBEDTLS_HMAC_DRBG_PR_ON ); |
| 683 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", |
| 684 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 685 | mbedtls_exit(1) ); |
| 686 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
| 687 | #endif |
| 688 | |
| 689 | #if defined(MBEDTLS_SHA256_C) |
| 690 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL ) |
| 691 | mbedtls_exit(1); |
| 692 | |
| 693 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 694 | mbedtls_exit(1); |
| 695 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", |
| 696 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 697 | mbedtls_exit(1) ); |
| 698 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
| 699 | |
| 700 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) |
| 701 | mbedtls_exit(1); |
| 702 | mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 703 | MBEDTLS_HMAC_DRBG_PR_ON ); |
| 704 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", |
| 705 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 706 | mbedtls_exit(1) ); |
| 707 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
| 708 | #endif |
| 709 | } |
| 710 | #endif |
| 711 | |
| 712 | #if defined(MBEDTLS_RSA_C) && \ |
| 713 | defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C) |
| 714 | if( todo.rsa ) |
| 715 | { |
| 716 | mbedtls_pk_context pk; |
| 717 | mbedtls_rsa_context *rsa; |
| 718 | const char *rsa_keys[] = { RSA_PRIVATE_KEY_2048, RSA_PRIVATE_KEY_4096 }; |
| 719 | size_t i; |
| 720 | |
| 721 | for( i = 0; i < sizeof( rsa_keys ) / sizeof( rsa_keys[0] ); i++ ) |
| 722 | { |
| 723 | mbedtls_pk_init( &pk ); |
| 724 | mbedtls_pk_parse_key( &pk, (const unsigned char *) rsa_keys[i], |
| 725 | strlen( rsa_keys[i] ) + 1, NULL, 0 ); |
| 726 | rsa = mbedtls_pk_rsa( pk ); |
| 727 | |
| 728 | mbedtls_snprintf( title, sizeof( title ), "RSA-%d", mbedtls_pk_get_bitlen( &pk ) ); |
| 729 | |
| 730 | TIME_PUBLIC( title, " public", |
| 731 | buf[0] = 0; |
| 732 | ret = mbedtls_rsa_public( rsa, buf, buf ) ); |
| 733 | |
| 734 | TIME_PUBLIC( title, "private", |
| 735 | buf[0] = 0; |
| 736 | ret = mbedtls_rsa_private( rsa, myrand, NULL, buf, buf ) ); |
| 737 | |
| 738 | mbedtls_pk_free( &pk ); |
| 739 | } |
| 740 | } |
| 741 | #endif |
| 742 | |
| 743 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C) |
| 744 | if( todo.dhm ) |
| 745 | { |
| 746 | int dhm_sizes[] = { 2048, 3072 }; |
| 747 | const char *dhm_P[] = { |
| 748 | MBEDTLS_DHM_RFC3526_MODP_2048_P, |
| 749 | MBEDTLS_DHM_RFC3526_MODP_3072_P, |
| 750 | }; |
| 751 | const char *dhm_G[] = { |
| 752 | MBEDTLS_DHM_RFC3526_MODP_2048_G, |
| 753 | MBEDTLS_DHM_RFC3526_MODP_3072_G, |
| 754 | }; |
| 755 | |
| 756 | mbedtls_dhm_context dhm; |
| 757 | size_t olen; |
| 758 | for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ ) |
| 759 | { |
| 760 | mbedtls_dhm_init( &dhm ); |
| 761 | |
| 762 | if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || |
| 763 | mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) |
| 764 | { |
| 765 | mbedtls_exit( 1 ); |
| 766 | } |
| 767 | |
| 768 | dhm.len = mbedtls_mpi_size( &dhm.P ); |
| 769 | mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); |
| 770 | if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) |
| 771 | mbedtls_exit( 1 ); |
| 772 | |
| 773 | mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); |
| 774 | TIME_PUBLIC( title, "handshake", |
| 775 | ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, |
| 776 | myrand, NULL ); |
| 777 | ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) ); |
| 778 | |
| 779 | mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); |
| 780 | TIME_PUBLIC( title, "handshake", |
| 781 | ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) ); |
| 782 | |
| 783 | mbedtls_dhm_free( &dhm ); |
| 784 | } |
| 785 | } |
| 786 | #endif |
| 787 | |
| 788 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) |
| 789 | if( todo.ecdsa ) |
| 790 | { |
| 791 | mbedtls_ecdsa_context ecdsa; |
| 792 | const mbedtls_ecp_curve_info *curve_info; |
| 793 | size_t sig_len; |
| 794 | |
| 795 | memset( buf, 0x2A, sizeof( buf ) ); |
| 796 | |
| 797 | for( curve_info = mbedtls_ecp_curve_list(); |
| 798 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 799 | curve_info++ ) |
| 800 | { |
| 801 | mbedtls_ecdsa_init( &ecdsa ); |
| 802 | |
| 803 | if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) |
| 804 | mbedtls_exit( 1 ); |
| 805 | ecp_clear_precomputed( &ecdsa.grp ); |
| 806 | |
| 807 | mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", |
| 808 | curve_info->name ); |
| 809 | TIME_PUBLIC( title, "sign", |
| 810 | ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, |
| 811 | tmp, &sig_len, myrand, NULL ) ); |
| 812 | |
| 813 | mbedtls_ecdsa_free( &ecdsa ); |
| 814 | } |
| 815 | |
| 816 | for( curve_info = mbedtls_ecp_curve_list(); |
| 817 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 818 | curve_info++ ) |
| 819 | { |
| 820 | mbedtls_ecdsa_init( &ecdsa ); |
| 821 | |
| 822 | if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 || |
| 823 | mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, |
| 824 | tmp, &sig_len, myrand, NULL ) != 0 ) |
| 825 | { |
| 826 | mbedtls_exit( 1 ); |
| 827 | } |
| 828 | ecp_clear_precomputed( &ecdsa.grp ); |
| 829 | |
| 830 | mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", |
| 831 | curve_info->name ); |
| 832 | TIME_PUBLIC( title, "verify", |
| 833 | ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size, |
| 834 | tmp, sig_len ) ); |
| 835 | |
| 836 | mbedtls_ecdsa_free( &ecdsa ); |
| 837 | } |
| 838 | } |
| 839 | #endif |
| 840 | |
| 841 | #if defined(MBEDTLS_ECDH_C) |
| 842 | if( todo.ecdh ) |
| 843 | { |
| 844 | mbedtls_ecdh_context ecdh; |
| 845 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 846 | mbedtls_mpi z; |
| 847 | #endif |
| 848 | const mbedtls_ecp_curve_info *curve_info; |
| 849 | size_t olen; |
| 850 | |
| 851 | for( curve_info = mbedtls_ecp_curve_list(); |
| 852 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 853 | curve_info++ ) |
| 854 | { |
| 855 | mbedtls_ecdh_init( &ecdh ); |
| 856 | |
| 857 | if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 || |
| 858 | mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 859 | myrand, NULL ) != 0 || |
| 860 | mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) |
| 861 | { |
| 862 | mbedtls_exit( 1 ); |
| 863 | } |
| 864 | ecp_clear_precomputed( &ecdh.grp ); |
| 865 | |
| 866 | mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", |
| 867 | curve_info->name ); |
| 868 | TIME_PUBLIC( title, "handshake", |
| 869 | ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 870 | myrand, NULL ); |
| 871 | ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
| 872 | myrand, NULL ) ); |
| 873 | mbedtls_ecdh_free( &ecdh ); |
| 874 | } |
| 875 | |
| 876 | /* Curve25519 needs to be handled separately */ |
| 877 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 878 | mbedtls_ecdh_init( &ecdh ); |
| 879 | mbedtls_mpi_init( &z ); |
| 880 | |
| 881 | if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 || |
| 882 | mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 ) |
| 883 | { |
| 884 | mbedtls_exit( 1 ); |
| 885 | } |
| 886 | |
| 887 | TIME_PUBLIC( "ECDHE-Curve25519", "handshake", |
| 888 | ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, |
| 889 | myrand, NULL ); |
| 890 | ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, |
| 891 | myrand, NULL ) ); |
| 892 | |
| 893 | mbedtls_ecdh_free( &ecdh ); |
| 894 | mbedtls_mpi_free( &z ); |
| 895 | #endif |
| 896 | |
| 897 | for( curve_info = mbedtls_ecp_curve_list(); |
| 898 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 899 | curve_info++ ) |
| 900 | { |
| 901 | mbedtls_ecdh_init( &ecdh ); |
| 902 | |
| 903 | if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 || |
| 904 | mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 905 | myrand, NULL ) != 0 || |
| 906 | mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 || |
| 907 | mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
| 908 | myrand, NULL ) != 0 ) |
| 909 | { |
| 910 | mbedtls_exit( 1 ); |
| 911 | } |
| 912 | ecp_clear_precomputed( &ecdh.grp ); |
| 913 | |
| 914 | mbedtls_snprintf( title, sizeof( title ), "ECDH-%s", |
| 915 | curve_info->name ); |
| 916 | TIME_PUBLIC( title, "handshake", |
| 917 | ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
| 918 | myrand, NULL ) ); |
| 919 | mbedtls_ecdh_free( &ecdh ); |
| 920 | } |
| 921 | |
| 922 | /* Curve25519 needs to be handled separately */ |
| 923 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 924 | mbedtls_ecdh_init( &ecdh ); |
| 925 | mbedtls_mpi_init( &z ); |
| 926 | |
| 927 | if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 || |
| 928 | mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, |
| 929 | myrand, NULL ) != 0 || |
| 930 | mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 ) |
| 931 | { |
| 932 | mbedtls_exit( 1 ); |
| 933 | } |
| 934 | |
| 935 | TIME_PUBLIC( "ECDH-Curve25519", "handshake", |
| 936 | ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, |
| 937 | myrand, NULL ) ); |
| 938 | |
| 939 | mbedtls_ecdh_free( &ecdh ); |
| 940 | mbedtls_mpi_free( &z ); |
| 941 | #endif |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | mbedtls_printf( "\r\n" ); |
| 946 | |
| 947 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 948 | mbedtls_memory_buffer_alloc_free(); |
| 949 | #endif |
| 950 | |
| 951 | #if defined(_WIN32) |
| 952 | mbedtls_printf( " Press Enter to exit this program.\r\n" ); |
| 953 | fflush( stdout ); getchar(); |
| 954 | #endif |
| 955 | |
| 956 | return( 0 ); |
| 957 | } |
| 958 | |
Manuel Pégourié-Gonnard | 71956c9 | 2015-10-21 17:59:05 +0200 | [diff] [blame] | 959 | #include "mbed-drivers/test_env.h" |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 960 | #include "minar/minar.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 961 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 962 | static void run() { |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 963 | MBED_HOSTTEST_TIMEOUT(150); |
| 964 | MBED_HOSTTEST_SELECT(default); |
| 965 | MBED_HOSTTEST_DESCRIPTION(mbed TLS benchmark program); |
| 966 | MBED_HOSTTEST_START("MBEDTLS_BENCHMARK"); |
| 967 | MBED_HOSTTEST_RESULT(benchmark(0, NULL) == 0); |
| 968 | } |
| 969 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 970 | void app_start(int, char*[]) { |
Janos Follath | 60ddf16 | 2016-03-17 13:55:07 +0000 | [diff] [blame] | 971 | /* Use 115200 bps for consistency with other examples */ |
| 972 | get_stdio_serial().baud(115200); |
Manuel Pégourié-Gonnard | ca4fb71 | 2015-09-18 14:36:57 +0200 | [diff] [blame] | 973 | minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind()); |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 976 | #endif /* TARGET_LIKE_MBED */ |