| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Benchmark demonstration program | 
|  | 3 | * | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 4 | *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/platform.h" | 
| Christoph M. Wintersteiger | cf5603f | 2019-02-20 18:06:00 +0000 | [diff] [blame] | 29 | #if !defined(MBEDTLS_PLATFORM_C) | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 30 | #include <stdio.h> | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 31 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #define mbedtls_exit       exit | 
|  | 33 | #define mbedtls_printf     printf | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #define mbedtls_free       free | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif | 
|  | 36 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if !defined(MBEDTLS_TIMING_C) | 
| Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 38 | int main( void ) | 
|  | 39 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_printf("MBEDTLS_TIMING_C not defined.\n"); | 
| Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 41 | return( 0 ); | 
|  | 42 | } | 
|  | 43 | #else | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 44 |  | 
| Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 45 | #include <string.h> | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 46 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 47 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/timing.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/md4.h" | 
|  | 51 | #include "mbedtls/md5.h" | 
|  | 52 | #include "mbedtls/ripemd160.h" | 
|  | 53 | #include "mbedtls/sha1.h" | 
|  | 54 | #include "mbedtls/sha256.h" | 
|  | 55 | #include "mbedtls/sha512.h" | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 56 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/arc4.h" | 
|  | 58 | #include "mbedtls/des.h" | 
|  | 59 | #include "mbedtls/aes.h" | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 60 | #include "mbedtls/aria.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/blowfish.h" | 
|  | 62 | #include "mbedtls/camellia.h" | 
| Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 63 | #include "mbedtls/chacha20.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 64 | #include "mbedtls/gcm.h" | 
|  | 65 | #include "mbedtls/ccm.h" | 
| Manuel Pégourié-Gonnard | d6aea18 | 2018-05-09 10:21:28 +0200 | [diff] [blame] | 66 | #include "mbedtls/chachapoly.h" | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 67 | #include "mbedtls/cmac.h" | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 68 | #include "mbedtls/poly1305.h" | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 69 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 70 | #include "mbedtls/havege.h" | 
|  | 71 | #include "mbedtls/ctr_drbg.h" | 
|  | 72 | #include "mbedtls/hmac_drbg.h" | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 73 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 74 | #include "mbedtls/rsa.h" | 
|  | 75 | #include "mbedtls/dhm.h" | 
|  | 76 | #include "mbedtls/ecdsa.h" | 
|  | 77 | #include "mbedtls/ecdh.h" | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 78 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 79 | #include "mbedtls/error.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 82 | #include "mbedtls/memory_buffer_alloc.h" | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 83 | #endif | 
|  | 84 |  | 
| Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 85 | /* | 
|  | 86 | * For heap usage estimates, we need an estimate of the overhead per allocated | 
|  | 87 | * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block, | 
|  | 88 | * so use that as our baseline. | 
|  | 89 | */ | 
|  | 90 | #define MEM_BLOCK_OVERHEAD  ( 2 * sizeof( size_t ) ) | 
|  | 91 |  | 
|  | 92 | /* | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 93 | * Size to use for the alloc buffer if MEMORY_BUFFER_ALLOC_C is defined. | 
| Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 94 | */ | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 95 | #define HEAP_SIZE       (1u << 16)  /* 64k */ | 
| Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 96 |  | 
| Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 97 | #define BUFSIZE         1024 | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 98 | #define HEADER_FORMAT   "  %-24s :  " | 
| Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 99 | #define TITLE_LEN       25 | 
| Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 100 |  | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 101 | #define OPTIONS                                                         \ | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 102 | "md4, md5, ripemd160, sha1, sha256, sha512,\n"                      \ | 
| Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 103 | "arc4, des3, des, camellia, blowfish, chacha20,\n"                  \ | 
| Manuel Pégourié-Gonnard | 0dadba2 | 2018-06-19 11:11:15 +0200 | [diff] [blame] | 104 | "aes_cbc, aes_gcm, aes_ccm, aes_ctx, chachapoly,\n"                 \ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 105 | "aes_cmac, des3_cmac, poly1305\n"                                   \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 106 | "havege, ctr_drbg, hmac_drbg\n"                                     \ | 
|  | 107 | "rsa, dhm, ecdsa, ecdh.\n" | 
|  | 108 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | #if defined(MBEDTLS_ERROR_C) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 110 | #define PRINT_ERROR                                                     \ | 
| Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame] | 111 | mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) );          \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_printf( "FAILED: %s\n", tmp ); | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 113 | #else | 
|  | 114 | #define PRINT_ERROR                                                     \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_printf( "FAILED: -0x%04x\n", -ret ); | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 116 | #endif | 
|  | 117 |  | 
|  | 118 | #define TIME_AND_TSC( TITLE, CODE )                                     \ | 
|  | 119 | do {                                                                    \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 120 | unsigned long ii, jj, tsc;                                          \ | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 121 | int ret = 0;                                                        \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 122 | \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 123 | mbedtls_printf( HEADER_FORMAT, TITLE );                             \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 124 | fflush( stdout );                                                   \ | 
|  | 125 | \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 126 | mbedtls_set_alarm( 1 );                                             \ | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 127 | for( ii = 1; ret == 0 && ! mbedtls_timing_alarmed; ii++ )           \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 128 | {                                                                   \ | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 129 | ret = CODE;                                                     \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 130 | }                                                                   \ | 
|  | 131 | \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 132 | tsc = mbedtls_timing_hardclock();                                   \ | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 133 | for( jj = 0; ret == 0 && jj < 1024; jj++ )                          \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 134 | {                                                                   \ | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 135 | ret = CODE;                                                     \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 136 | }                                                                   \ | 
|  | 137 | \ | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 138 | if( ret != 0 )                                                      \ | 
|  | 139 | {                                                                   \ | 
|  | 140 | PRINT_ERROR;                                                    \ | 
|  | 141 | }                                                                   \ | 
|  | 142 | else                                                                \ | 
|  | 143 | {                                                                   \ | 
|  | 144 | mbedtls_printf( "%9lu KiB/s,  %9lu cycles/byte\n",              \ | 
|  | 145 | ii * BUFSIZE / 1024,                           \ | 
|  | 146 | ( mbedtls_timing_hardclock() - tsc )           \ | 
|  | 147 | / ( jj * BUFSIZE ) );                          \ | 
|  | 148 | }                                                                   \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 149 | } while( 0 ) | 
|  | 150 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG) | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 152 |  | 
|  | 153 | #define MEMORY_MEASURE_INIT                                             \ | 
|  | 154 | size_t max_used, max_blocks, max_bytes;                             \ | 
|  | 155 | size_t prv_used, prv_blocks;                                        \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 156 | mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks );      \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | mbedtls_memory_buffer_alloc_max_reset( ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 158 |  | 
|  | 159 | #define MEMORY_MEASURE_PRINT( title_len )                               \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 160 | mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks );      \ | 
| Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 161 | for( ii = 12 - (title_len); ii != 0; ii-- ) mbedtls_printf( " " );  \ | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 162 | max_used -= prv_used;                                               \ | 
|  | 163 | max_blocks -= prv_blocks;                                           \ | 
|  | 164 | max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks;             \ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 166 |  | 
|  | 167 | #else | 
| Manuel Pégourié-Gonnard | e579dab | 2015-01-29 16:28:44 +0000 | [diff] [blame] | 168 | #define MEMORY_MEASURE_INIT | 
|  | 169 | #define MEMORY_MEASURE_PRINT( title_len ) | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 170 | #endif | 
|  | 171 |  | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 172 | #define TIME_PUBLIC( TITLE, TYPE, CODE )                                \ | 
|  | 173 | do {                                                                    \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 174 | unsigned long ii;                                                   \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 175 | int ret;                                                            \ | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 176 | MEMORY_MEASURE_INIT;                                                \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 177 | \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 178 | mbedtls_printf( HEADER_FORMAT, TITLE );                             \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 179 | fflush( stdout );                                                   \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 180 | mbedtls_set_alarm( 3 );                                             \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 181 | \ | 
|  | 182 | ret = 0;                                                            \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 183 | for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ )             \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 184 | {                                                                   \ | 
|  | 185 | CODE;                                                           \ | 
|  | 186 | }                                                                   \ | 
|  | 187 | \ | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 188 | if( ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED )               \ | 
|  | 189 | {                                                                   \ | 
|  | 190 | mbedtls_printf( "Feature Not Supported. Skipping.\n" );         \ | 
|  | 191 | ret = 0;                                                        \ | 
|  | 192 | }                                                                   \ | 
|  | 193 | else if( ret != 0 )                                                 \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 194 | {                                                                   \ | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 195 | PRINT_ERROR;                                                    \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 196 | }                                                                   \ | 
|  | 197 | else                                                                \ | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 198 | {                                                                   \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 199 | mbedtls_printf( "%6lu " TYPE "/s", ii / 3 );                    \ | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 200 | MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 );                     \ | 
| Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 201 | mbedtls_printf( "\n" );                                         \ | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 202 | }                                                                   \ | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 203 | } while( 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 205 | static int myrand( void *rng_state, unsigned char *output, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | { | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 207 | size_t use_len; | 
|  | 208 | int rnd; | 
|  | 209 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | if( rng_state != NULL ) | 
|  | 211 | rng_state  = NULL; | 
|  | 212 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 213 | while( len > 0 ) | 
|  | 214 | { | 
|  | 215 | use_len = len; | 
|  | 216 | if( use_len > sizeof(int) ) | 
|  | 217 | use_len = sizeof(int); | 
|  | 218 |  | 
|  | 219 | rnd = rand(); | 
|  | 220 | memcpy( output, &rnd, use_len ); | 
|  | 221 | output += use_len; | 
|  | 222 | len -= use_len; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 228 | #define CHECK_AND_CONTINUE( R )                                         \ | 
|  | 229 | {                                                                   \ | 
| Gilles Peskine | 1fdf2c2 | 2019-02-11 18:23:42 +0100 | [diff] [blame] | 230 | int CHECK_AND_CONTINUE_ret = ( R );                             \ | 
|  | 231 | if( CHECK_AND_CONTINUE_ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ) { \ | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 232 | mbedtls_printf( "Feature not supported. Skipping.\n" );     \ | 
|  | 233 | continue;                                                   \ | 
|  | 234 | }                                                               \ | 
| Gilles Peskine | 1fdf2c2 | 2019-02-11 18:23:42 +0100 | [diff] [blame] | 235 | else if( CHECK_AND_CONTINUE_ret != 0 ) {                        \ | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 236 | mbedtls_exit( 1 );                                          \ | 
|  | 237 | }                                                               \ | 
|  | 238 | } | 
| Christoph M. Wintersteiger | 3c44962 | 2018-12-14 11:54:59 +0000 | [diff] [blame] | 239 |  | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 240 | /* | 
|  | 241 | * Clear some memory that was used to prepare the context | 
|  | 242 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | #if defined(MBEDTLS_ECP_C) | 
|  | 244 | void ecp_clear_precomputed( mbedtls_ecp_group *grp ) | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 245 | { | 
|  | 246 | if( grp->T != NULL ) | 
|  | 247 | { | 
|  | 248 | size_t i; | 
|  | 249 | for( i = 0; i < grp->T_size; i++ ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_ecp_point_free( &grp->T[i] ); | 
|  | 251 | mbedtls_free( grp->T ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 252 | } | 
|  | 253 | grp->T = NULL; | 
|  | 254 | grp->T_size = 0; | 
|  | 255 | } | 
|  | 256 | #else | 
|  | 257 | #define ecp_clear_precomputed( g ) | 
|  | 258 | #endif | 
|  | 259 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | unsigned char buf[BUFSIZE]; | 
|  | 261 |  | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 262 | typedef struct { | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 263 | char md4, md5, ripemd160, sha1, sha256, sha512, | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 264 | arc4, des3, des, | 
| Manuel Pégourié-Gonnard | 0dadba2 | 2018-06-19 11:11:15 +0200 | [diff] [blame] | 265 | aes_cbc, aes_gcm, aes_ccm, aes_xts, chachapoly, | 
| Manuel Pégourié-Gonnard | d6aea18 | 2018-05-09 10:21:28 +0200 | [diff] [blame] | 266 | aes_cmac, des3_cmac, | 
| Manuel Pégourié-Gonnard | 39b1904 | 2018-06-07 12:01:33 +0200 | [diff] [blame] | 267 | aria, camellia, blowfish, chacha20, | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 268 | poly1305, | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 269 | havege, ctr_drbg, hmac_drbg, | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 270 | rsa, dhm, ecdsa, ecdh; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 271 | } todo_list; | 
|  | 272 |  | 
| Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 273 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 274 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 275 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 276 | int i; | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 277 | unsigned char tmp[200]; | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 278 | char title[TITLE_LEN]; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 279 | todo_list todo; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 281 | unsigned char alloc_buf[HEAP_SIZE] = { 0 }; | 
| Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 282 | #endif | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 283 |  | 
| Manuel Pégourié-Gonnard | c439e7b | 2015-03-03 13:12:00 +0000 | [diff] [blame] | 284 | if( argc <= 1 ) | 
|  | 285 | { | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 286 | memset( &todo, 1, sizeof( todo ) ); | 
| Manuel Pégourié-Gonnard | c439e7b | 2015-03-03 13:12:00 +0000 | [diff] [blame] | 287 | } | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 288 | else | 
|  | 289 | { | 
|  | 290 | memset( &todo, 0, sizeof( todo ) ); | 
|  | 291 |  | 
|  | 292 | for( i = 1; i < argc; i++ ) | 
|  | 293 | { | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 294 | if( strcmp( argv[i], "md4" ) == 0 ) | 
|  | 295 | todo.md4 = 1; | 
|  | 296 | else if( strcmp( argv[i], "md5" ) == 0 ) | 
|  | 297 | todo.md5 = 1; | 
|  | 298 | else if( strcmp( argv[i], "ripemd160" ) == 0 ) | 
|  | 299 | todo.ripemd160 = 1; | 
|  | 300 | else if( strcmp( argv[i], "sha1" ) == 0 ) | 
|  | 301 | todo.sha1 = 1; | 
|  | 302 | else if( strcmp( argv[i], "sha256" ) == 0 ) | 
|  | 303 | todo.sha256 = 1; | 
|  | 304 | else if( strcmp( argv[i], "sha512" ) == 0 ) | 
|  | 305 | todo.sha512 = 1; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 306 | else if( strcmp( argv[i], "arc4" ) == 0 ) | 
|  | 307 | todo.arc4 = 1; | 
|  | 308 | else if( strcmp( argv[i], "des3" ) == 0 ) | 
|  | 309 | todo.des3 = 1; | 
|  | 310 | else if( strcmp( argv[i], "des" ) == 0 ) | 
|  | 311 | todo.des = 1; | 
|  | 312 | else if( strcmp( argv[i], "aes_cbc" ) == 0 ) | 
|  | 313 | todo.aes_cbc = 1; | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 314 | else if( strcmp( argv[i], "aes_xts" ) == 0 ) | 
|  | 315 | todo.aes_xts = 1; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 316 | else if( strcmp( argv[i], "aes_gcm" ) == 0 ) | 
|  | 317 | todo.aes_gcm = 1; | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 318 | else if( strcmp( argv[i], "aes_ccm" ) == 0 ) | 
|  | 319 | todo.aes_ccm = 1; | 
| Manuel Pégourié-Gonnard | d6aea18 | 2018-05-09 10:21:28 +0200 | [diff] [blame] | 320 | else if( strcmp( argv[i], "chachapoly" ) == 0 ) | 
|  | 321 | todo.chachapoly = 1; | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 322 | else if( strcmp( argv[i], "aes_cmac" ) == 0 ) | 
|  | 323 | todo.aes_cmac = 1; | 
|  | 324 | else if( strcmp( argv[i], "des3_cmac" ) == 0 ) | 
|  | 325 | todo.des3_cmac = 1; | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 326 | else if( strcmp( argv[i], "aria" ) == 0 ) | 
|  | 327 | todo.aria = 1; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 328 | else if( strcmp( argv[i], "camellia" ) == 0 ) | 
|  | 329 | todo.camellia = 1; | 
|  | 330 | else if( strcmp( argv[i], "blowfish" ) == 0 ) | 
|  | 331 | todo.blowfish = 1; | 
| Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 332 | else if( strcmp( argv[i], "chacha20" ) == 0 ) | 
|  | 333 | todo.chacha20 = 1; | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 334 | else if( strcmp( argv[i], "poly1305" ) == 0 ) | 
|  | 335 | todo.poly1305 = 1; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 336 | else if( strcmp( argv[i], "havege" ) == 0 ) | 
|  | 337 | todo.havege = 1; | 
|  | 338 | else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) | 
|  | 339 | todo.ctr_drbg = 1; | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 340 | else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) | 
|  | 341 | todo.hmac_drbg = 1; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 342 | else if( strcmp( argv[i], "rsa" ) == 0 ) | 
|  | 343 | todo.rsa = 1; | 
|  | 344 | else if( strcmp( argv[i], "dhm" ) == 0 ) | 
|  | 345 | todo.dhm = 1; | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 346 | else if( strcmp( argv[i], "ecdsa" ) == 0 ) | 
|  | 347 | todo.ecdsa = 1; | 
|  | 348 | else if( strcmp( argv[i], "ecdh" ) == 0 ) | 
|  | 349 | todo.ecdh = 1; | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 350 | else | 
|  | 351 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | mbedtls_printf( "Unrecognized option: %s\n", argv[i] ); | 
|  | 353 | mbedtls_printf( "Available options: " OPTIONS ); | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 354 | } | 
|  | 355 | } | 
|  | 356 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 357 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | mbedtls_printf( "\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 359 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) | 
| Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 361 | mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) ); | 
| Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 362 | #endif | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 363 | memset( buf, 0xAA, sizeof( buf ) ); | 
| Paul Bakker | df71dd1 | 2014-04-17 16:03:48 +0200 | [diff] [blame] | 364 | memset( tmp, 0xBB, sizeof( tmp ) ); | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 365 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | #if defined(MBEDTLS_MD4_C) | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 367 | if( todo.md4 ) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 368 | TIME_AND_TSC( "MD4", mbedtls_md4_ret( buf, BUFSIZE, tmp ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 369 | #endif | 
|  | 370 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 371 | #if defined(MBEDTLS_MD5_C) | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 372 | if( todo.md5 ) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 373 | TIME_AND_TSC( "MD5", mbedtls_md5_ret( buf, BUFSIZE, tmp ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | #endif | 
|  | 375 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 376 | #if defined(MBEDTLS_RIPEMD160_C) | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 377 | if( todo.ripemd160 ) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 378 | TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ret( buf, BUFSIZE, tmp ) ); | 
| Manuel Pégourié-Gonnard | 01b0b38 | 2014-01-17 14:29:46 +0100 | [diff] [blame] | 379 | #endif | 
|  | 380 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | #if defined(MBEDTLS_SHA1_C) | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 382 | if( todo.sha1 ) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 383 | TIME_AND_TSC( "SHA-1", mbedtls_sha1_ret( buf, BUFSIZE, tmp ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 384 | #endif | 
|  | 385 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | #if defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 387 | if( todo.sha256 ) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 388 | TIME_AND_TSC( "SHA-256", mbedtls_sha256_ret( buf, BUFSIZE, tmp, 0 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 389 | #endif | 
|  | 390 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #if defined(MBEDTLS_SHA512_C) | 
| Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 392 | if( todo.sha512 ) | 
| Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 393 | TIME_AND_TSC( "SHA-512", mbedtls_sha512_ret( buf, BUFSIZE, tmp, 0 ) ); | 
| Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 394 | #endif | 
|  | 395 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | #if defined(MBEDTLS_ARC4_C) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 397 | if( todo.arc4 ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 398 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | mbedtls_arc4_context arc4; | 
|  | 400 | mbedtls_arc4_init( &arc4 ); | 
|  | 401 | mbedtls_arc4_setup( &arc4, tmp, 32 ); | 
|  | 402 | TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); | 
|  | 403 | mbedtls_arc4_free( &arc4 ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 404 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 405 | #endif | 
|  | 406 |  | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 407 | #if defined(MBEDTLS_DES_C) | 
|  | 408 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 409 | if( todo.des3 ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 410 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | mbedtls_des3_context des3; | 
|  | 412 | mbedtls_des3_init( &des3 ); | 
|  | 413 | mbedtls_des3_set3key_enc( &des3, tmp ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 414 | TIME_AND_TSC( "3DES", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); | 
|  | 416 | mbedtls_des3_free( &des3 ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 417 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 418 |  | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 419 | if( todo.des ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 420 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | mbedtls_des_context des; | 
|  | 422 | mbedtls_des_init( &des ); | 
|  | 423 | mbedtls_des_setkey_enc( &des, tmp ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 424 | TIME_AND_TSC( "DES", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 425 | mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); | 
|  | 426 | mbedtls_des_free( &des ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 427 | } | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 428 |  | 
|  | 429 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
|  | 430 | #if defined(MBEDTLS_CMAC_C) | 
|  | 431 | if( todo.des3_cmac ) | 
|  | 432 | { | 
|  | 433 | unsigned char output[8]; | 
|  | 434 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 435 |  | 
|  | 436 | memset( buf, 0, sizeof( buf ) ); | 
|  | 437 | memset( tmp, 0, sizeof( tmp ) ); | 
|  | 438 |  | 
|  | 439 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB ); | 
|  | 440 |  | 
|  | 441 | TIME_AND_TSC( "3DES-CMAC", | 
| Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame] | 442 | mbedtls_cipher_cmac( cipher_info, tmp, 192, buf, | 
|  | 443 | BUFSIZE, output ) ); | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 444 | } | 
|  | 445 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 446 | #endif /* MBEDTLS_DES_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 447 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | #if defined(MBEDTLS_AES_C) | 
|  | 449 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 450 | if( todo.aes_cbc ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 451 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 452 | int keysize; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | mbedtls_aes_context aes; | 
|  | 454 | mbedtls_aes_init( &aes ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 455 | for( keysize = 128; keysize <= 256; keysize += 64 ) | 
|  | 456 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 458 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 459 | memset( buf, 0, sizeof( buf ) ); | 
|  | 460 | memset( tmp, 0, sizeof( tmp ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 461 | mbedtls_aes_setkey_enc( &aes, tmp, keysize ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 462 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 463 | TIME_AND_TSC( title, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 465 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | mbedtls_aes_free( &aes ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 467 | } | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 468 | #endif | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 469 | #if defined(MBEDTLS_CIPHER_MODE_XTS) | 
|  | 470 | if( todo.aes_xts ) | 
|  | 471 | { | 
|  | 472 | int keysize; | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 473 | mbedtls_aes_xts_context ctx; | 
|  | 474 |  | 
|  | 475 | mbedtls_aes_xts_init( &ctx ); | 
|  | 476 | for( keysize = 128; keysize <= 256; keysize += 128 ) | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 477 | { | 
|  | 478 | mbedtls_snprintf( title, sizeof( title ), "AES-XTS-%d", keysize ); | 
|  | 479 |  | 
|  | 480 | memset( buf, 0, sizeof( buf ) ); | 
|  | 481 | memset( tmp, 0, sizeof( tmp ) ); | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 482 | mbedtls_aes_xts_setkey_enc( &ctx, tmp, keysize * 2 ); | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 483 |  | 
|  | 484 | TIME_AND_TSC( title, | 
| Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 485 | mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, BUFSIZE, | 
|  | 486 | tmp, buf, buf ) ); | 
|  | 487 |  | 
|  | 488 | mbedtls_aes_xts_free( &ctx ); | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 489 | } | 
| Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 490 | } | 
|  | 491 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | #if defined(MBEDTLS_GCM_C) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 493 | if( todo.aes_gcm ) | 
| Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 494 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 495 | int keysize; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | mbedtls_gcm_context gcm; | 
| Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 497 |  | 
|  | 498 | mbedtls_gcm_init( &gcm ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 499 | for( keysize = 128; keysize <= 256; keysize += 64 ) | 
|  | 500 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); | 
| Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 502 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 503 | memset( buf, 0, sizeof( buf ) ); | 
|  | 504 | memset( tmp, 0, sizeof( tmp ) ); | 
| Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 505 | mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize ); | 
| Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 506 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 507 | TIME_AND_TSC( title, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp, | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 509 | 12, NULL, 0, buf, buf, 16, tmp ) ); | 
| Paul Bakker | f70fe81 | 2013-12-16 16:43:10 +0100 | [diff] [blame] | 510 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | mbedtls_gcm_free( &gcm ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 512 | } | 
| Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 513 | } | 
|  | 514 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | #if defined(MBEDTLS_CCM_C) | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 516 | if( todo.aes_ccm ) | 
|  | 517 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 518 | int keysize; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | mbedtls_ccm_context ccm; | 
| Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 520 |  | 
|  | 521 | mbedtls_ccm_init( &ccm ); | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 522 | for( keysize = 128; keysize <= 256; keysize += 64 ) | 
|  | 523 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 525 |  | 
|  | 526 | memset( buf, 0, sizeof( buf ) ); | 
|  | 527 | memset( tmp, 0, sizeof( tmp ) ); | 
| Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 528 | mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize ); | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 529 |  | 
|  | 530 | TIME_AND_TSC( title, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp, | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 532 | 12, NULL, 0, buf, buf, tmp, 16 ) ); | 
|  | 533 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | mbedtls_ccm_free( &ccm ); | 
| Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 535 | } | 
|  | 536 | } | 
|  | 537 | #endif | 
| Manuel Pégourié-Gonnard | d6aea18 | 2018-05-09 10:21:28 +0200 | [diff] [blame] | 538 | #if defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 539 | if( todo.chachapoly ) | 
|  | 540 | { | 
|  | 541 | mbedtls_chachapoly_context chachapoly; | 
|  | 542 |  | 
|  | 543 | mbedtls_chachapoly_init( &chachapoly ); | 
|  | 544 | memset( buf, 0, sizeof( buf ) ); | 
|  | 545 | memset( tmp, 0, sizeof( tmp ) ); | 
|  | 546 |  | 
|  | 547 | mbedtls_snprintf( title, sizeof( title ), "ChaCha20-Poly1305" ); | 
|  | 548 |  | 
|  | 549 | mbedtls_chachapoly_setkey( &chachapoly, tmp ); | 
|  | 550 |  | 
|  | 551 | TIME_AND_TSC( title, | 
| Manuel Pégourié-Gonnard | 3dc62a0 | 2018-06-04 12:18:19 +0200 | [diff] [blame] | 552 | mbedtls_chachapoly_encrypt_and_tag( &chachapoly, | 
|  | 553 | BUFSIZE, tmp, NULL, 0, buf, buf, tmp ) ); | 
| Manuel Pégourié-Gonnard | d6aea18 | 2018-05-09 10:21:28 +0200 | [diff] [blame] | 554 |  | 
|  | 555 | mbedtls_chachapoly_free( &chachapoly ); | 
|  | 556 | } | 
|  | 557 | #endif | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 558 | #if defined(MBEDTLS_CMAC_C) | 
|  | 559 | if( todo.aes_cmac ) | 
|  | 560 | { | 
|  | 561 | unsigned char output[16]; | 
|  | 562 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 563 | mbedtls_cipher_type_t cipher_type; | 
|  | 564 | int keysize; | 
|  | 565 |  | 
|  | 566 | for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB; | 
|  | 567 | keysize <= 256; | 
|  | 568 | keysize += 64, cipher_type++ ) | 
|  | 569 | { | 
|  | 570 | mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize ); | 
|  | 571 |  | 
|  | 572 | memset( buf, 0, sizeof( buf ) ); | 
|  | 573 | memset( tmp, 0, sizeof( tmp ) ); | 
|  | 574 |  | 
|  | 575 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); | 
|  | 576 |  | 
|  | 577 | TIME_AND_TSC( title, | 
| Andres AG | a592dcc | 2016-10-06 15:23:39 +0100 | [diff] [blame] | 578 | mbedtls_cipher_cmac( cipher_info, tmp, keysize, | 
|  | 579 | buf, BUFSIZE, output ) ); | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 580 | } | 
|  | 581 |  | 
|  | 582 | memset( buf, 0, sizeof( buf ) ); | 
|  | 583 | memset( tmp, 0, sizeof( tmp ) ); | 
|  | 584 | TIME_AND_TSC( "AES-CMAC-PRF-128", | 
| Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame] | 585 | mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE, | 
|  | 586 | output ) ); | 
| Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 587 | } | 
|  | 588 | #endif /* MBEDTLS_CMAC_C */ | 
|  | 589 | #endif /* MBEDTLS_AES_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 590 |  | 
| Manuel Pégourié-Gonnard | 62e813c | 2018-02-21 10:47:47 +0100 | [diff] [blame] | 591 | #if defined(MBEDTLS_ARIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 592 | if( todo.aria ) | 
|  | 593 | { | 
|  | 594 | int keysize; | 
|  | 595 | mbedtls_aria_context aria; | 
|  | 596 | mbedtls_aria_init( &aria ); | 
|  | 597 | for( keysize = 128; keysize <= 256; keysize += 64 ) | 
|  | 598 | { | 
|  | 599 | mbedtls_snprintf( title, sizeof( title ), "ARIA-CBC-%d", keysize ); | 
|  | 600 |  | 
|  | 601 | memset( buf, 0, sizeof( buf ) ); | 
|  | 602 | memset( tmp, 0, sizeof( tmp ) ); | 
|  | 603 | mbedtls_aria_setkey_enc( &aria, tmp, keysize ); | 
|  | 604 |  | 
|  | 605 | TIME_AND_TSC( title, | 
|  | 606 | mbedtls_aria_crypt_cbc( &aria, MBEDTLS_ARIA_ENCRYPT, | 
|  | 607 | BUFSIZE, tmp, buf, buf ) ); | 
|  | 608 | } | 
|  | 609 | mbedtls_aria_free( &aria ); | 
|  | 610 | } | 
|  | 611 | #endif | 
|  | 612 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 614 | if( todo.camellia ) | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 615 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 616 | int keysize; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | mbedtls_camellia_context camellia; | 
|  | 618 | mbedtls_camellia_init( &camellia ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 619 | for( keysize = 128; keysize <= 256; keysize += 64 ) | 
|  | 620 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 622 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 623 | memset( buf, 0, sizeof( buf ) ); | 
|  | 624 | memset( tmp, 0, sizeof( tmp ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | mbedtls_camellia_setkey_enc( &camellia, tmp, keysize ); | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 626 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 627 | TIME_AND_TSC( title, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT, | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 629 | BUFSIZE, tmp, buf, buf ) ); | 
|  | 630 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | mbedtls_camellia_free( &camellia ); | 
| Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 632 | } | 
|  | 633 | #endif | 
|  | 634 |  | 
| Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 635 | #if defined(MBEDTLS_CHACHA20_C) | 
|  | 636 | if ( todo.chacha20 ) | 
|  | 637 | { | 
|  | 638 | TIME_AND_TSC( "ChaCha20", mbedtls_chacha20_crypt( buf, buf, 0U, BUFSIZE, buf, buf ) ); | 
|  | 639 | } | 
|  | 640 | #endif | 
|  | 641 |  | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 642 | #if defined(MBEDTLS_POLY1305_C) | 
|  | 643 | if ( todo.poly1305 ) | 
|  | 644 | { | 
| Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 645 | TIME_AND_TSC( "Poly1305", mbedtls_poly1305_mac( buf, buf, BUFSIZE, buf ) ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 646 | } | 
|  | 647 | #endif | 
|  | 648 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 650 | if( todo.blowfish ) | 
| Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 651 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 652 | int keysize; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | mbedtls_blowfish_context blowfish; | 
|  | 654 | mbedtls_blowfish_init( &blowfish ); | 
| Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 655 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 656 | for( keysize = 128; keysize <= 256; keysize += 64 ) | 
|  | 657 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); | 
| Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 659 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 660 | memset( buf, 0, sizeof( buf ) ); | 
|  | 661 | memset( tmp, 0, sizeof( tmp ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | mbedtls_blowfish_setkey( &blowfish, tmp, keysize ); | 
| Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 663 |  | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 664 | TIME_AND_TSC( title, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE, | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 666 | tmp, buf, buf ) ); | 
|  | 667 | } | 
| Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 668 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | mbedtls_blowfish_free( &blowfish ); | 
| Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 670 | } | 
|  | 671 | #endif | 
|  | 672 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | #if defined(MBEDTLS_HAVEGE_C) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 674 | if( todo.havege ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 675 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 676 | mbedtls_havege_state hs; | 
|  | 677 | mbedtls_havege_init( &hs ); | 
|  | 678 | TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) ); | 
|  | 679 | mbedtls_havege_free( &hs ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 680 | } | 
| Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 681 | #endif | 
|  | 682 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 683 | #if defined(MBEDTLS_CTR_DRBG_C) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 684 | if( todo.ctr_drbg ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 685 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_ctr_drbg_context ctr_drbg; | 
| Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 687 |  | 
| Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 688 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 
| Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 689 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | mbedtls_exit(1); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 691 | TIME_AND_TSC( "CTR_DRBG (NOPR)", | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 692 | mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) ); | 
| Gilles Peskine | f0ebbfb | 2019-10-28 17:28:46 +0100 | [diff] [blame] | 693 | mbedtls_ctr_drbg_free( &ctr_drbg ); | 
| Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 694 |  | 
| Gilles Peskine | f0ebbfb | 2019-10-28 17:28:46 +0100 | [diff] [blame] | 695 | mbedtls_ctr_drbg_init( &ctr_drbg ); | 
| Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 696 | if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | mbedtls_exit(1); | 
|  | 698 | mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 699 | TIME_AND_TSC( "CTR_DRBG (PR)", | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 700 | mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | mbedtls_ctr_drbg_free( &ctr_drbg ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 702 | } | 
| Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 703 | #endif | 
|  | 704 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | #if defined(MBEDTLS_HMAC_DRBG_C) | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 706 | if( todo.hmac_drbg ) | 
|  | 707 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | mbedtls_hmac_drbg_context hmac_drbg; | 
|  | 709 | const mbedtls_md_info_t *md_info; | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 710 |  | 
| Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 711 | mbedtls_hmac_drbg_init( &hmac_drbg ); | 
|  | 712 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | #if defined(MBEDTLS_SHA1_C) | 
|  | 714 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) | 
|  | 715 | mbedtls_exit(1); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 716 |  | 
| Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 717 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | mbedtls_exit(1); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 719 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 720 | mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 721 |  | 
| Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 722 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 723 | mbedtls_exit(1); | 
|  | 724 | mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, | 
|  | 725 | MBEDTLS_HMAC_DRBG_PR_ON ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 726 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 727 | mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 728 | #endif | 
|  | 729 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | #if defined(MBEDTLS_SHA256_C) | 
|  | 731 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL ) | 
|  | 732 | mbedtls_exit(1); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 733 |  | 
| Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 734 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | mbedtls_exit(1); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 736 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 737 | mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 738 |  | 
| Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 739 | if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | mbedtls_exit(1); | 
|  | 741 | mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, | 
|  | 742 | MBEDTLS_HMAC_DRBG_PR_ON ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 743 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", | 
| Manuel Pégourié-Gonnard | 51d7cfe | 2018-06-25 11:19:51 +0200 | [diff] [blame] | 744 | mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 745 | #endif | 
| Ron Eldor | 278af45 | 2018-06-20 18:40:21 +0300 | [diff] [blame] | 746 | mbedtls_hmac_drbg_free( &hmac_drbg ); | 
| Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 747 | } | 
|  | 748 | #endif | 
|  | 749 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 751 | if( todo.rsa ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 752 | { | 
| Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 753 | int keysize; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | mbedtls_rsa_context rsa; | 
| Manuel Pégourié-Gonnard | a6dbddc | 2015-07-06 11:20:33 +0200 | [diff] [blame] | 755 | for( keysize = 2048; keysize <= 4096; keysize *= 2 ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 756 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 758 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); | 
|  | 760 | mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 761 |  | 
|  | 762 | TIME_PUBLIC( title, " public", | 
|  | 763 | buf[0] = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 764 | ret = mbedtls_rsa_public( &rsa, buf, buf ) ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 765 |  | 
|  | 766 | TIME_PUBLIC( title, "private", | 
|  | 767 | buf[0] = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 768 | ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 769 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | mbedtls_rsa_free( &rsa ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 771 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 772 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 773 | #endif | 
|  | 774 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C) | 
| Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 776 | if( todo.dhm ) | 
| Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 777 | { | 
| Manuel Pégourié-Gonnard | 4f3368e | 2015-07-19 15:01:28 +0200 | [diff] [blame] | 778 | int dhm_sizes[] = { 2048, 3072 }; | 
| Brendan Shanks | e61514d | 2018-03-08 17:40:56 -0800 | [diff] [blame] | 779 | static const unsigned char dhm_P_2048[] = | 
| Hanno Becker | b953921 | 2017-10-04 13:13:34 +0100 | [diff] [blame] | 780 | MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; | 
| Brendan Shanks | e61514d | 2018-03-08 17:40:56 -0800 | [diff] [blame] | 781 | static const unsigned char dhm_P_3072[] = | 
| Hanno Becker | b953921 | 2017-10-04 13:13:34 +0100 | [diff] [blame] | 782 | MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN; | 
| Brendan Shanks | e61514d | 2018-03-08 17:40:56 -0800 | [diff] [blame] | 783 | static const unsigned char dhm_G_2048[] = | 
| Hanno Becker | b953921 | 2017-10-04 13:13:34 +0100 | [diff] [blame] | 784 | MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; | 
| Brendan Shanks | e61514d | 2018-03-08 17:40:56 -0800 | [diff] [blame] | 785 | static const unsigned char dhm_G_3072[] = | 
| Hanno Becker | b953921 | 2017-10-04 13:13:34 +0100 | [diff] [blame] | 786 | MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN; | 
|  | 787 |  | 
|  | 788 | const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 }; | 
|  | 789 | const size_t dhm_P_size[] = { sizeof( dhm_P_2048 ), | 
|  | 790 | sizeof( dhm_P_3072 ) }; | 
|  | 791 |  | 
|  | 792 | const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 }; | 
|  | 793 | const size_t dhm_G_size[] = { sizeof( dhm_G_2048 ), | 
|  | 794 | sizeof( dhm_G_3072 ) }; | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 795 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | mbedtls_dhm_context dhm; | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 797 | size_t olen; | 
| Manuel Pégourié-Gonnard | 4f3368e | 2015-07-19 15:01:28 +0200 | [diff] [blame] | 798 | for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ ) | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 799 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 800 | mbedtls_dhm_init( &dhm ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 801 |  | 
| Hanno Becker | b953921 | 2017-10-04 13:13:34 +0100 | [diff] [blame] | 802 | if( mbedtls_mpi_read_binary( &dhm.P, dhm_P[i], | 
|  | 803 | dhm_P_size[i] ) != 0 || | 
|  | 804 | mbedtls_mpi_read_binary( &dhm.G, dhm_G[i], | 
|  | 805 | dhm_G_size[i] ) != 0 ) | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 806 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | mbedtls_exit( 1 ); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 808 | } | 
|  | 809 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 810 | dhm.len = mbedtls_mpi_size( &dhm.P ); | 
|  | 811 | mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); | 
|  | 812 | if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) | 
|  | 813 | mbedtls_exit( 1 ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 814 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 816 | TIME_PUBLIC( title, "handshake", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 817 | ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 818 | myrand, NULL ); | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 819 | ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 820 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 822 | TIME_PUBLIC( title, "handshake", | 
| Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 823 | ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 824 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | mbedtls_dhm_free( &dhm ); | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 826 | } | 
| Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 827 | } | 
| Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 828 | #endif | 
|  | 829 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 830 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 831 | if( todo.ecdsa ) | 
|  | 832 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | mbedtls_ecdsa_context ecdsa; | 
|  | 834 | const mbedtls_ecp_curve_info *curve_info; | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 835 | size_t sig_len; | 
|  | 836 |  | 
|  | 837 | memset( buf, 0x2A, sizeof( buf ) ); | 
|  | 838 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | for( curve_info = mbedtls_ecp_curve_list(); | 
|  | 840 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 841 | curve_info++ ) | 
|  | 842 | { | 
| Christoph M. Wintersteiger | 41fb2c0 | 2019-02-15 12:34:40 +0000 | [diff] [blame] | 843 | if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) ) | 
| Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 844 | continue; | 
|  | 845 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 846 | mbedtls_ecdsa_init( &ecdsa ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 847 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 848 | if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) | 
|  | 849 | mbedtls_exit( 1 ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 850 | ecp_clear_precomputed( &ecdsa.grp ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 851 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 852 | mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", | 
| Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 853 | curve_info->name ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 854 | TIME_PUBLIC( title, "sign", | 
| Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 855 | ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, | 
| Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 856 | tmp, &sig_len, myrand, NULL ) ); | 
|  | 857 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 858 | mbedtls_ecdsa_free( &ecdsa ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 859 | } | 
|  | 860 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 861 | for( curve_info = mbedtls_ecp_curve_list(); | 
|  | 862 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 863 | curve_info++ ) | 
|  | 864 | { | 
| Christoph M. Wintersteiger | 41fb2c0 | 2019-02-15 12:34:40 +0000 | [diff] [blame] | 865 | if( ! mbedtls_ecdsa_can_do( curve_info->grp_id ) ) | 
| Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 866 | continue; | 
|  | 867 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 868 | mbedtls_ecdsa_init( &ecdsa ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 869 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 || | 
| Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 871 | mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size, | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 872 | tmp, &sig_len, myrand, NULL ) != 0 ) | 
|  | 873 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | mbedtls_exit( 1 ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 875 | } | 
|  | 876 | ecp_clear_precomputed( &ecdsa.grp ); | 
|  | 877 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 879 | curve_info->name ); | 
| Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 880 | TIME_PUBLIC( title, "verify", | 
| Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 881 | ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size, | 
| Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 882 | tmp, sig_len ) ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 883 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | mbedtls_ecdsa_free( &ecdsa ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 885 | } | 
|  | 886 | } | 
|  | 887 | #endif | 
|  | 888 |  | 
| Janos Follath | 52735ef | 2018-08-15 10:19:16 +0100 | [diff] [blame] | 889 | #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 890 | if( todo.ecdh ) | 
|  | 891 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | mbedtls_ecdh_context ecdh; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 893 | mbedtls_mpi z; | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 894 | const mbedtls_ecp_curve_info montgomery_curve_list[] = { | 
|  | 895 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) | 
|  | 896 | { MBEDTLS_ECP_DP_CURVE25519, 0, 0, "Curve25519" }, | 
| Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 897 | #endif | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 898 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) | 
|  | 899 | { MBEDTLS_ECP_DP_CURVE448, 0, 0, "Curve448" }, | 
|  | 900 | #endif | 
|  | 901 | { MBEDTLS_ECP_DP_NONE, 0, 0, 0 } | 
|  | 902 | }; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | const mbedtls_ecp_curve_info *curve_info; | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 904 | size_t olen; | 
|  | 905 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 906 | for( curve_info = mbedtls_ecp_curve_list(); | 
|  | 907 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 908 | curve_info++ ) | 
|  | 909 | { | 
| Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 910 | if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) ) | 
|  | 911 | continue; | 
|  | 912 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | mbedtls_ecdh_init( &ecdh ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 914 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 915 | CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) ); | 
|  | 916 | CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), | 
|  | 917 | myrand, NULL ) ); | 
|  | 918 | CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 919 | ecp_clear_precomputed( &ecdh.grp ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 920 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 921 | mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", | 
| Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 922 | curve_info->name ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 923 | TIME_PUBLIC( title, "handshake", | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 924 | CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 925 | myrand, NULL ) ); | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 926 | CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), | 
|  | 927 | myrand, NULL ) ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 928 | mbedtls_ecdh_free( &ecdh ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 929 | } | 
|  | 930 |  | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 931 | /* Montgomery curves need to be handled separately */ | 
|  | 932 | for ( curve_info = montgomery_curve_list; | 
|  | 933 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
|  | 934 | curve_info++ ) | 
| Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 935 | { | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 936 | mbedtls_ecdh_init( &ecdh ); | 
|  | 937 | mbedtls_mpi_init( &z ); | 
|  | 938 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 939 | CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) ); | 
|  | 940 | CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) ); | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 941 |  | 
|  | 942 | mbedtls_snprintf( title, sizeof(title), "ECDHE-%s", | 
|  | 943 | curve_info->name ); | 
|  | 944 | TIME_PUBLIC(  title, "handshake", | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 945 | CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, | 
|  | 946 | myrand, NULL ) ); | 
|  | 947 | CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, | 
|  | 948 | myrand, NULL ) ) ); | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 949 |  | 
|  | 950 | mbedtls_ecdh_free( &ecdh ); | 
|  | 951 | mbedtls_mpi_free( &z ); | 
| Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 952 | } | 
|  | 953 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | for( curve_info = mbedtls_ecp_curve_list(); | 
|  | 955 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 956 | curve_info++ ) | 
|  | 957 | { | 
| Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 958 | if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) ) | 
|  | 959 | continue; | 
|  | 960 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 961 | mbedtls_ecdh_init( &ecdh ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 962 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 963 | CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) ); | 
|  | 964 | CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), | 
|  | 965 | myrand, NULL ) ); | 
|  | 966 | CHECK_AND_CONTINUE( mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) ); | 
|  | 967 | CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), | 
|  | 968 | myrand, NULL ) ); | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 969 | ecp_clear_precomputed( &ecdh.grp ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 970 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 971 | mbedtls_snprintf( title, sizeof( title ), "ECDH-%s", | 
| Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 972 | curve_info->name ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 973 | TIME_PUBLIC( title, "handshake", | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 974 | CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), | 
|  | 975 | myrand, NULL ) ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | mbedtls_ecdh_free( &ecdh ); | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 977 | } | 
| Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 978 |  | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 979 | /* Montgomery curves need to be handled separately */ | 
|  | 980 | for ( curve_info = montgomery_curve_list; | 
|  | 981 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
|  | 982 | curve_info++) | 
| Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 983 | { | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 984 | mbedtls_ecdh_init( &ecdh ); | 
|  | 985 | mbedtls_mpi_init( &z ); | 
|  | 986 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 987 | CHECK_AND_CONTINUE( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) ); | 
|  | 988 | CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, | 
|  | 989 | myrand, NULL ) ); | 
|  | 990 | CHECK_AND_CONTINUE( mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) ); | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 991 |  | 
|  | 992 | mbedtls_snprintf( title, sizeof(title), "ECDH-%s", | 
|  | 993 | curve_info->name ); | 
|  | 994 | TIME_PUBLIC(  title, "handshake", | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 995 | CHECK_AND_CONTINUE( mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, | 
|  | 996 | myrand, NULL ) ) ); | 
| Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 997 |  | 
|  | 998 | mbedtls_ecdh_free( &ecdh ); | 
|  | 999 | mbedtls_mpi_free( &z ); | 
| Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 1000 | } | 
| Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 1001 | } | 
|  | 1002 | #endif | 
| Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 1003 |  | 
| Christoph M. Wintersteiger | c3cbdde | 2018-12-14 11:03:02 +0000 | [diff] [blame] | 1004 | #if defined(MBEDTLS_ECDH_C) | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1005 | if( todo.ecdh ) | 
|  | 1006 | { | 
|  | 1007 | mbedtls_ecdh_context ecdh_srv, ecdh_cli; | 
|  | 1008 | unsigned char buf_srv[BUFSIZE], buf_cli[BUFSIZE]; | 
|  | 1009 | const mbedtls_ecp_curve_info * curve_list = mbedtls_ecp_curve_list(); | 
|  | 1010 | const mbedtls_ecp_curve_info *curve_info; | 
|  | 1011 | size_t olen; | 
|  | 1012 |  | 
|  | 1013 | for( curve_info = curve_list; | 
|  | 1014 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | 
|  | 1015 | curve_info++ ) | 
|  | 1016 | { | 
| Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 1017 | if( ! mbedtls_ecdh_can_do( curve_info->grp_id ) ) | 
|  | 1018 | continue; | 
|  | 1019 |  | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1020 | mbedtls_ecdh_init( &ecdh_srv ); | 
| Christoph M. Wintersteiger | 26b98e1 | 2019-02-26 12:26:04 +0000 | [diff] [blame] | 1021 | mbedtls_ecdh_init( &ecdh_cli ); | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1022 |  | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1023 | mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", curve_info->name ); | 
| Christoph M. Wintersteiger | 65bab97 | 2018-12-06 17:15:12 +0000 | [diff] [blame] | 1024 | TIME_PUBLIC( title, "full handshake", | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1025 | const unsigned char * p_srv = buf_srv; | 
| Christoph M. Wintersteiger | 26b98e1 | 2019-02-26 12:26:04 +0000 | [diff] [blame] | 1026 |  | 
| Christoph M. Wintersteiger | 3b58700 | 2019-02-20 17:26:42 +0000 | [diff] [blame] | 1027 | CHECK_AND_CONTINUE( mbedtls_ecdh_setup( &ecdh_srv, curve_info->grp_id ) ); | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 1028 | CHECK_AND_CONTINUE( mbedtls_ecdh_make_params( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) ); | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1029 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 1030 | CHECK_AND_CONTINUE( mbedtls_ecdh_read_params( &ecdh_cli, &p_srv, p_srv + olen ) ); | 
|  | 1031 | CHECK_AND_CONTINUE( mbedtls_ecdh_make_public( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) ); | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1032 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 1033 | CHECK_AND_CONTINUE( mbedtls_ecdh_read_public( &ecdh_srv, buf_cli, olen ) ); | 
|  | 1034 | CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_srv, &olen, buf_srv, sizeof( buf_srv ), myrand, NULL ) ); | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1035 |  | 
| Christoph M. Wintersteiger | 9597238 | 2019-02-06 18:06:15 +0000 | [diff] [blame] | 1036 | CHECK_AND_CONTINUE( mbedtls_ecdh_calc_secret( &ecdh_cli, &olen, buf_cli, sizeof( buf_cli ), myrand, NULL ) ); | 
| Christoph M. Wintersteiger | 3b58700 | 2019-02-20 17:26:42 +0000 | [diff] [blame] | 1037 | mbedtls_ecdh_free( &ecdh_cli ); | 
| Christoph M. Wintersteiger | 26b98e1 | 2019-02-26 12:26:04 +0000 | [diff] [blame] | 1038 |  | 
| Christoph M. Wintersteiger | 3b58700 | 2019-02-20 17:26:42 +0000 | [diff] [blame] | 1039 | mbedtls_ecdh_free( &ecdh_srv ); | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1040 | ); | 
|  | 1041 |  | 
| Christoph M. Wintersteiger | 696deda | 2018-10-25 12:47:18 +0100 | [diff] [blame] | 1042 | } | 
|  | 1043 | } | 
|  | 1044 | #endif | 
|  | 1045 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1046 | mbedtls_printf( "\n" ); | 
| Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 1047 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1048 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) | 
|  | 1049 | mbedtls_memory_buffer_alloc_free(); | 
| Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 1050 | #endif | 
|  | 1051 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 1052 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | mbedtls_printf( "  Press Enter to exit this program.\n" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1054 | fflush( stdout ); getchar(); | 
|  | 1055 | #endif | 
|  | 1056 |  | 
|  | 1057 | return( 0 ); | 
|  | 1058 | } | 
| Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 1059 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1060 | #endif /* MBEDTLS_TIMING_C */ |