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 | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.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 |
| 34 | #define mbedtls_snprintf snprintf |
| 35 | #define mbedtls_free free |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if !defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 39 | int main( void ) |
| 40 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | mbedtls_printf("MBEDTLS_TIMING_C not defined.\n"); |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 42 | return( 0 ); |
| 43 | } |
| 44 | #else |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 46 | #include <string.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 47 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 48 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 51 | #include "mbedtls/md4.h" |
| 52 | #include "mbedtls/md5.h" |
| 53 | #include "mbedtls/ripemd160.h" |
| 54 | #include "mbedtls/sha1.h" |
| 55 | #include "mbedtls/sha256.h" |
| 56 | #include "mbedtls/sha512.h" |
| 57 | #include "mbedtls/arc4.h" |
| 58 | #include "mbedtls/des.h" |
| 59 | #include "mbedtls/aes.h" |
| 60 | #include "mbedtls/blowfish.h" |
| 61 | #include "mbedtls/camellia.h" |
| 62 | #include "mbedtls/gcm.h" |
| 63 | #include "mbedtls/ccm.h" |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 64 | #include "mbedtls/cmac.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 65 | #include "mbedtls/havege.h" |
| 66 | #include "mbedtls/ctr_drbg.h" |
| 67 | #include "mbedtls/hmac_drbg.h" |
| 68 | #include "mbedtls/rsa.h" |
| 69 | #include "mbedtls/dhm.h" |
| 70 | #include "mbedtls/ecdsa.h" |
| 71 | #include "mbedtls/ecdh.h" |
| 72 | #include "mbedtls/error.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 75 | #include "mbedtls/memory_buffer_alloc.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
Manuel Pégourié-Gonnard | 714929b | 2015-02-16 17:32:47 +0000 | [diff] [blame] | 78 | /* |
| 79 | * For heap usage estimates, we need an estimate of the overhead per allocated |
| 80 | * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block, |
| 81 | * so use that as our baseline. |
| 82 | */ |
| 83 | #define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) ) |
| 84 | |
| 85 | /* |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 86 | * 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] | 87 | */ |
| 88 | #define HEAP_SIZE (1u << 16) // 64k |
| 89 | |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 90 | #define BUFSIZE 1024 |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 91 | #define HEADER_FORMAT " %-24s : " |
Gergely Budai | a5d336b | 2014-01-27 23:27:06 +0100 | [diff] [blame] | 92 | #define TITLE_LEN 25 |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 93 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 94 | #define OPTIONS \ |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 95 | "md4, md5, ripemd160, sha1, sha256, sha512,\n" \ |
Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame^] | 96 | "arc4, des3, des, camellia, blowfish,\n" \ |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 97 | "aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac,\n" \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 98 | "havege, ctr_drbg, hmac_drbg\n" \ |
| 99 | "rsa, dhm, ecdsa, ecdh.\n" |
| 100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | #if defined(MBEDTLS_ERROR_C) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 102 | #define PRINT_ERROR \ |
Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame^] | 103 | mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | mbedtls_printf( "FAILED: %s\n", tmp ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 105 | #else |
| 106 | #define PRINT_ERROR \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | mbedtls_printf( "FAILED: -0x%04x\n", -ret ); |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | #define TIME_AND_TSC( TITLE, CODE ) \ |
| 111 | do { \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 112 | unsigned long ii, jj, tsc; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 113 | \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 114 | mbedtls_printf( HEADER_FORMAT, TITLE ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 115 | fflush( stdout ); \ |
| 116 | \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 117 | mbedtls_set_alarm( 1 ); \ |
| 118 | for( ii = 1; ! mbedtls_timing_alarmed; ii++ ) \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 119 | { \ |
| 120 | CODE; \ |
| 121 | } \ |
| 122 | \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 123 | tsc = mbedtls_timing_hardclock(); \ |
| 124 | for( jj = 0; jj < 1024; jj++ ) \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 125 | { \ |
| 126 | CODE; \ |
| 127 | } \ |
| 128 | \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 129 | mbedtls_printf( "%9lu Kb/s, %9lu cycles/byte\n", \ |
| 130 | ii * BUFSIZE / 1024, \ |
| 131 | ( mbedtls_timing_hardclock() - tsc ) / ( jj * BUFSIZE ) ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 132 | } while( 0 ) |
| 133 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | #if defined(MBEDTLS_ERROR_C) |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 135 | #define PRINT_ERROR \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 136 | mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | mbedtls_printf( "FAILED: %s\n", tmp ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 138 | #else |
| 139 | #define PRINT_ERROR \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_printf( "FAILED: -0x%04x\n", -ret ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 141 | #endif |
| 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | #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] | 144 | |
| 145 | #define MEMORY_MEASURE_INIT \ |
| 146 | size_t max_used, max_blocks, max_bytes; \ |
| 147 | size_t prv_used, prv_blocks; \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 148 | mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | mbedtls_memory_buffer_alloc_max_reset( ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 150 | |
| 151 | #define MEMORY_MEASURE_PRINT( title_len ) \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 152 | mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \ |
| 153 | for( ii = 12 - title_len; ii != 0; ii-- ) mbedtls_printf( " " ); \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 154 | max_used -= prv_used; \ |
| 155 | max_blocks -= prv_blocks; \ |
| 156 | max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 158 | |
| 159 | #else |
Manuel Pégourié-Gonnard | e579dab | 2015-01-29 16:28:44 +0000 | [diff] [blame] | 160 | #define MEMORY_MEASURE_INIT |
| 161 | #define MEMORY_MEASURE_PRINT( title_len ) |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 162 | #endif |
| 163 | |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 164 | #define TIME_PUBLIC( TITLE, TYPE, CODE ) \ |
| 165 | do { \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 166 | unsigned long ii; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 167 | int ret; \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 168 | MEMORY_MEASURE_INIT; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 169 | \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 170 | mbedtls_printf( HEADER_FORMAT, TITLE ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 171 | fflush( stdout ); \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 172 | mbedtls_set_alarm( 3 ); \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 173 | \ |
| 174 | ret = 0; \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 175 | for( ii = 1; ! mbedtls_timing_alarmed && ! ret ; ii++ ) \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 176 | { \ |
| 177 | CODE; \ |
| 178 | } \ |
| 179 | \ |
| 180 | if( ret != 0 ) \ |
| 181 | { \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 182 | PRINT_ERROR; \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 183 | } \ |
| 184 | else \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 185 | { \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 186 | mbedtls_printf( "%6lu " TYPE "/s", ii / 3 ); \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 187 | MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \ |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 188 | mbedtls_printf( "\n" ); \ |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 189 | } \ |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 190 | } while( 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 192 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 194 | size_t use_len; |
| 195 | int rnd; |
| 196 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 197 | if( rng_state != NULL ) |
| 198 | rng_state = NULL; |
| 199 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 200 | while( len > 0 ) |
| 201 | { |
| 202 | use_len = len; |
| 203 | if( use_len > sizeof(int) ) |
| 204 | use_len = sizeof(int); |
| 205 | |
| 206 | rnd = rand(); |
| 207 | memcpy( output, &rnd, use_len ); |
| 208 | output += use_len; |
| 209 | len -= use_len; |
| 210 | } |
| 211 | |
| 212 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 215 | /* |
| 216 | * Clear some memory that was used to prepare the context |
| 217 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | #if defined(MBEDTLS_ECP_C) |
| 219 | void ecp_clear_precomputed( mbedtls_ecp_group *grp ) |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 220 | { |
| 221 | if( grp->T != NULL ) |
| 222 | { |
| 223 | size_t i; |
| 224 | for( i = 0; i < grp->T_size; i++ ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | mbedtls_ecp_point_free( &grp->T[i] ); |
| 226 | mbedtls_free( grp->T ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 227 | } |
| 228 | grp->T = NULL; |
| 229 | grp->T_size = 0; |
| 230 | } |
| 231 | #else |
| 232 | #define ecp_clear_precomputed( g ) |
| 233 | #endif |
| 234 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | unsigned char buf[BUFSIZE]; |
| 236 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 237 | typedef struct { |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 238 | char md4, md5, ripemd160, sha1, sha256, sha512, |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 239 | arc4, des3, des, |
| 240 | aes_cbc, aes_gcm, aes_ccm, aes_cmac, des3_cmac, |
| 241 | camellia, blowfish, |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 242 | havege, ctr_drbg, hmac_drbg, |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 243 | rsa, dhm, ecdsa, ecdh; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 244 | } todo_list; |
| 245 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 246 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 247 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 248 | int i; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 249 | unsigned char tmp[200]; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 250 | char title[TITLE_LEN]; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 251 | todo_list todo; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 253 | unsigned char alloc_buf[HEAP_SIZE] = { 0 }; |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 254 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 255 | |
Manuel Pégourié-Gonnard | c439e7b | 2015-03-03 13:12:00 +0000 | [diff] [blame] | 256 | if( argc <= 1 ) |
| 257 | { |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 258 | memset( &todo, 1, sizeof( todo ) ); |
Manuel Pégourié-Gonnard | c439e7b | 2015-03-03 13:12:00 +0000 | [diff] [blame] | 259 | } |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 260 | else |
| 261 | { |
| 262 | memset( &todo, 0, sizeof( todo ) ); |
| 263 | |
| 264 | for( i = 1; i < argc; i++ ) |
| 265 | { |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 266 | if( strcmp( argv[i], "md4" ) == 0 ) |
| 267 | todo.md4 = 1; |
| 268 | else if( strcmp( argv[i], "md5" ) == 0 ) |
| 269 | todo.md5 = 1; |
| 270 | else if( strcmp( argv[i], "ripemd160" ) == 0 ) |
| 271 | todo.ripemd160 = 1; |
| 272 | else if( strcmp( argv[i], "sha1" ) == 0 ) |
| 273 | todo.sha1 = 1; |
| 274 | else if( strcmp( argv[i], "sha256" ) == 0 ) |
| 275 | todo.sha256 = 1; |
| 276 | else if( strcmp( argv[i], "sha512" ) == 0 ) |
| 277 | todo.sha512 = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 278 | else if( strcmp( argv[i], "arc4" ) == 0 ) |
| 279 | todo.arc4 = 1; |
| 280 | else if( strcmp( argv[i], "des3" ) == 0 ) |
| 281 | todo.des3 = 1; |
| 282 | else if( strcmp( argv[i], "des" ) == 0 ) |
| 283 | todo.des = 1; |
| 284 | else if( strcmp( argv[i], "aes_cbc" ) == 0 ) |
| 285 | todo.aes_cbc = 1; |
| 286 | else if( strcmp( argv[i], "aes_gcm" ) == 0 ) |
| 287 | todo.aes_gcm = 1; |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 288 | else if( strcmp( argv[i], "aes_ccm" ) == 0 ) |
| 289 | todo.aes_ccm = 1; |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 290 | else if( strcmp( argv[i], "aes_cmac" ) == 0 ) |
| 291 | todo.aes_cmac = 1; |
| 292 | else if( strcmp( argv[i], "des3_cmac" ) == 0 ) |
| 293 | todo.des3_cmac = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 294 | else if( strcmp( argv[i], "camellia" ) == 0 ) |
| 295 | todo.camellia = 1; |
| 296 | else if( strcmp( argv[i], "blowfish" ) == 0 ) |
| 297 | todo.blowfish = 1; |
| 298 | else if( strcmp( argv[i], "havege" ) == 0 ) |
| 299 | todo.havege = 1; |
| 300 | else if( strcmp( argv[i], "ctr_drbg" ) == 0 ) |
| 301 | todo.ctr_drbg = 1; |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 302 | else if( strcmp( argv[i], "hmac_drbg" ) == 0 ) |
| 303 | todo.hmac_drbg = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 304 | else if( strcmp( argv[i], "rsa" ) == 0 ) |
| 305 | todo.rsa = 1; |
| 306 | else if( strcmp( argv[i], "dhm" ) == 0 ) |
| 307 | todo.dhm = 1; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 308 | else if( strcmp( argv[i], "ecdsa" ) == 0 ) |
| 309 | todo.ecdsa = 1; |
| 310 | else if( strcmp( argv[i], "ecdh" ) == 0 ) |
| 311 | todo.ecdh = 1; |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 312 | else |
| 313 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | mbedtls_printf( "Unrecognized option: %s\n", argv[i] ); |
| 315 | mbedtls_printf( "Available options: " OPTIONS ); |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | mbedtls_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 323 | mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) ); |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 324 | #endif |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 325 | memset( buf, 0xAA, sizeof( buf ) ); |
Paul Bakker | df71dd1 | 2014-04-17 16:03:48 +0200 | [diff] [blame] | 326 | memset( tmp, 0xBB, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | #if defined(MBEDTLS_MD4_C) |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 329 | if( todo.md4 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 331 | #endif |
| 332 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | #if defined(MBEDTLS_MD5_C) |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 334 | if( todo.md5 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | #endif |
| 337 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | #if defined(MBEDTLS_RIPEMD160_C) |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 339 | if( todo.ripemd160 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) ); |
Manuel Pégourié-Gonnard | 01b0b38 | 2014-01-17 14:29:46 +0100 | [diff] [blame] | 341 | #endif |
| 342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | #if defined(MBEDTLS_SHA1_C) |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 344 | if( todo.sha1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 346 | #endif |
| 347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 349 | if( todo.sha256 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 | #endif |
| 352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | e85fef1 | 2015-05-11 19:21:39 +0200 | [diff] [blame] | 354 | if( todo.sha512 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) ); |
Paul Bakker | 3a3c3c2 | 2009-02-09 22:33:30 +0000 | [diff] [blame] | 356 | #endif |
| 357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 359 | if( todo.arc4 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 360 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | mbedtls_arc4_context arc4; |
| 362 | mbedtls_arc4_init( &arc4 ); |
| 363 | mbedtls_arc4_setup( &arc4, tmp, 32 ); |
| 364 | TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) ); |
| 365 | mbedtls_arc4_free( &arc4 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 366 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 369 | #if defined(MBEDTLS_DES_C) |
| 370 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 371 | if( todo.des3 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 372 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | mbedtls_des3_context des3; |
| 374 | mbedtls_des3_init( &des3 ); |
| 375 | mbedtls_des3_set3key_enc( &des3, tmp ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 376 | TIME_AND_TSC( "3DES", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 378 | mbedtls_des3_free( &des3 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 379 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 380 | |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 381 | if( todo.des ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | mbedtls_des_context des; |
| 384 | mbedtls_des_init( &des ); |
| 385 | mbedtls_des_setkey_enc( &des, tmp ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 386 | TIME_AND_TSC( "DES", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 387 | mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) ); |
| 388 | mbedtls_des_free( &des ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 389 | } |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 390 | |
| 391 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 392 | #if defined(MBEDTLS_CMAC_C) |
| 393 | if( todo.des3_cmac ) |
| 394 | { |
| 395 | unsigned char output[8]; |
| 396 | const mbedtls_cipher_info_t *cipher_info; |
| 397 | |
| 398 | memset( buf, 0, sizeof( buf ) ); |
| 399 | memset( tmp, 0, sizeof( tmp ) ); |
| 400 | |
| 401 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB ); |
| 402 | |
| 403 | TIME_AND_TSC( "3DES-CMAC", |
Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame^] | 404 | mbedtls_cipher_cmac( cipher_info, tmp, 192, buf, |
| 405 | BUFSIZE, output ) ); |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 406 | } |
| 407 | #endif /* MBEDTLS_CMAC_C */ |
| 408 | #endif /* MBEDTLS_DES_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | #if defined(MBEDTLS_AES_C) |
| 411 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 412 | if( todo.aes_cbc ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 414 | int keysize; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_aes_context aes; |
| 416 | mbedtls_aes_init( &aes ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 417 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 418 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 420 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 421 | memset( buf, 0, sizeof( buf ) ); |
| 422 | memset( tmp, 0, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | mbedtls_aes_setkey_enc( &aes, tmp, keysize ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 424 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 425 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | 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] | 427 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | mbedtls_aes_free( &aes ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 429 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 430 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | #if defined(MBEDTLS_GCM_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 432 | if( todo.aes_gcm ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 433 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 434 | int keysize; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 435 | mbedtls_gcm_context gcm; |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 436 | |
| 437 | mbedtls_gcm_init( &gcm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 438 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 439 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 441 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 442 | memset( buf, 0, sizeof( buf ) ); |
| 443 | memset( tmp, 0, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 444 | mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 445 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 446 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | 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] | 448 | 12, NULL, 0, buf, buf, 16, tmp ) ); |
Paul Bakker | f70fe81 | 2013-12-16 16:43:10 +0100 | [diff] [blame] | 449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | mbedtls_gcm_free( &gcm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 451 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 452 | } |
| 453 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 454 | #if defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 455 | if( todo.aes_ccm ) |
| 456 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 457 | int keysize; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | mbedtls_ccm_context ccm; |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 459 | |
| 460 | mbedtls_ccm_init( &ccm ); |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 461 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 462 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize ); |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 464 | |
| 465 | memset( buf, 0, sizeof( buf ) ); |
| 466 | memset( tmp, 0, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | 6963ff0 | 2015-04-28 18:02:54 +0200 | [diff] [blame] | 467 | mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize ); |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 468 | |
| 469 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp, |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 471 | 12, NULL, 0, buf, buf, tmp, 16 ) ); |
| 472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | mbedtls_ccm_free( &ccm ); |
Manuel Pégourié-Gonnard | 58d78a8 | 2014-05-07 12:03:02 +0200 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | #endif |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 477 | #if defined(MBEDTLS_CMAC_C) |
| 478 | if( todo.aes_cmac ) |
| 479 | { |
| 480 | unsigned char output[16]; |
| 481 | const mbedtls_cipher_info_t *cipher_info; |
| 482 | mbedtls_cipher_type_t cipher_type; |
| 483 | int keysize; |
| 484 | |
| 485 | for( keysize = 128, cipher_type = MBEDTLS_CIPHER_AES_128_ECB; |
| 486 | keysize <= 256; |
| 487 | keysize += 64, cipher_type++ ) |
| 488 | { |
| 489 | mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize ); |
| 490 | |
| 491 | memset( buf, 0, sizeof( buf ) ); |
| 492 | memset( tmp, 0, sizeof( tmp ) ); |
| 493 | |
| 494 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
| 495 | |
| 496 | TIME_AND_TSC( title, |
| 497 | mbedtls_cipher_cmac( cipher_info, tmp, keysize, |
| 498 | buf, BUFSIZE, output ) ); |
| 499 | } |
| 500 | |
| 501 | memset( buf, 0, sizeof( buf ) ); |
| 502 | memset( tmp, 0, sizeof( tmp ) ); |
| 503 | TIME_AND_TSC( "AES-CMAC-PRF-128", |
Simon Butcher | b981b16 | 2016-10-06 10:27:22 +0100 | [diff] [blame^] | 504 | mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE, |
| 505 | output ) ); |
Simon Butcher | 549dc3d | 2016-10-05 14:14:19 +0100 | [diff] [blame] | 506 | } |
| 507 | #endif /* MBEDTLS_CMAC_C */ |
| 508 | #endif /* MBEDTLS_AES_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 511 | if( todo.camellia ) |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 512 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 513 | int keysize; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | mbedtls_camellia_context camellia; |
| 515 | mbedtls_camellia_init( &camellia ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 516 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 517 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 519 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 520 | memset( buf, 0, sizeof( buf ) ); |
| 521 | memset( tmp, 0, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | mbedtls_camellia_setkey_enc( &camellia, tmp, keysize ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 523 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 524 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT, |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 526 | BUFSIZE, tmp, buf, buf ) ); |
| 527 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | mbedtls_camellia_free( &camellia ); |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 529 | } |
| 530 | #endif |
| 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 533 | if( todo.blowfish ) |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 534 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 535 | int keysize; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | mbedtls_blowfish_context blowfish; |
| 537 | mbedtls_blowfish_init( &blowfish ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 538 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 539 | for( keysize = 128; keysize <= 256; keysize += 64 ) |
| 540 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 541 | mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 542 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 543 | memset( buf, 0, sizeof( buf ) ); |
| 544 | memset( tmp, 0, sizeof( tmp ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | mbedtls_blowfish_setkey( &blowfish, tmp, keysize ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 546 | |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 547 | TIME_AND_TSC( title, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE, |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 549 | tmp, buf, buf ) ); |
| 550 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | mbedtls_blowfish_free( &blowfish ); |
Paul Bakker | 3d58fe8 | 2012-07-04 17:15:31 +0000 | [diff] [blame] | 553 | } |
| 554 | #endif |
| 555 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_HAVEGE_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 557 | if( todo.havege ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | mbedtls_havege_state hs; |
| 560 | mbedtls_havege_init( &hs ); |
| 561 | TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) ); |
| 562 | mbedtls_havege_free( &hs ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 563 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 564 | #endif |
| 565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | #if defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 567 | if( todo.ctr_drbg ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 568 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 570 | |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 571 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 572 | |
| 573 | 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] | 574 | mbedtls_exit(1); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 575 | TIME_AND_TSC( "CTR_DRBG (NOPR)", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 577 | mbedtls_exit(1) ); |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 578 | |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 579 | 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] | 580 | mbedtls_exit(1); |
| 581 | 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] | 582 | TIME_AND_TSC( "CTR_DRBG (PR)", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 ) |
| 584 | mbedtls_exit(1) ); |
| 585 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 586 | } |
Paul Bakker | 02faf45 | 2011-11-29 11:23:58 +0000 | [diff] [blame] | 587 | #endif |
| 588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | #if defined(MBEDTLS_HMAC_DRBG_C) |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 590 | if( todo.hmac_drbg ) |
| 591 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | mbedtls_hmac_drbg_context hmac_drbg; |
| 593 | const mbedtls_md_info_t *md_info; |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 594 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 595 | mbedtls_hmac_drbg_init( &hmac_drbg ); |
| 596 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | #if defined(MBEDTLS_SHA1_C) |
| 598 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 599 | mbedtls_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 600 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 601 | 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] | 602 | mbedtls_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 603 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 604 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 605 | mbedtls_exit(1) ); |
| 606 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 607 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 608 | 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] | 609 | mbedtls_exit(1); |
| 610 | mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 611 | MBEDTLS_HMAC_DRBG_PR_ON ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 612 | TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 614 | mbedtls_exit(1) ); |
| 615 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 616 | #endif |
| 617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | #if defined(MBEDTLS_SHA256_C) |
| 619 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL ) |
| 620 | mbedtls_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 621 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 622 | 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] | 623 | mbedtls_exit(1); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 624 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 626 | mbedtls_exit(1) ); |
| 627 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 628 | |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 629 | 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] | 630 | mbedtls_exit(1); |
| 631 | mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg, |
| 632 | MBEDTLS_HMAC_DRBG_PR_ON ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 633 | TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 ) |
| 635 | mbedtls_exit(1) ); |
| 636 | mbedtls_hmac_drbg_free( &hmac_drbg ); |
Manuel Pégourié-Gonnard | fef0f8f | 2014-01-30 20:59:00 +0100 | [diff] [blame] | 637 | #endif |
| 638 | } |
| 639 | #endif |
| 640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 642 | if( todo.rsa ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 643 | { |
Manuel Pégourié-Gonnard | 71e75dc | 2014-12-19 18:05:43 +0100 | [diff] [blame] | 644 | int keysize; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | mbedtls_rsa_context rsa; |
Manuel Pégourié-Gonnard | a6dbddc | 2015-07-06 11:20:33 +0200 | [diff] [blame] | 646 | for( keysize = 2048; keysize <= 4096; keysize *= 2 ) |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 647 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 651 | mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 652 | |
| 653 | TIME_PUBLIC( title, " public", |
| 654 | buf[0] = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | ret = mbedtls_rsa_public( &rsa, buf, buf ) ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 656 | |
| 657 | TIME_PUBLIC( title, "private", |
| 658 | buf[0] = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 659 | ret = mbedtls_rsa_private( &rsa, myrand, NULL, buf, buf ) ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | mbedtls_rsa_free( &rsa ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 662 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 663 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 664 | #endif |
| 665 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 666 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C) |
Manuel Pégourié-Gonnard | ed7cbe9 | 2013-09-17 15:30:51 +0200 | [diff] [blame] | 667 | if( todo.dhm ) |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 668 | { |
Manuel Pégourié-Gonnard | 4f3368e | 2015-07-19 15:01:28 +0200 | [diff] [blame] | 669 | int dhm_sizes[] = { 2048, 3072 }; |
| 670 | const char *dhm_P[] = { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | MBEDTLS_DHM_RFC3526_MODP_2048_P, |
| 672 | MBEDTLS_DHM_RFC3526_MODP_3072_P, |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 673 | }; |
Manuel Pégourié-Gonnard | 4f3368e | 2015-07-19 15:01:28 +0200 | [diff] [blame] | 674 | const char *dhm_G[] = { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | MBEDTLS_DHM_RFC3526_MODP_2048_G, |
| 676 | MBEDTLS_DHM_RFC3526_MODP_3072_G, |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 677 | }; |
| 678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | mbedtls_dhm_context dhm; |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 680 | size_t olen; |
Manuel Pégourié-Gonnard | 4f3368e | 2015-07-19 15:01:28 +0200 | [diff] [blame] | 681 | 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] | 682 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 683 | mbedtls_dhm_init( &dhm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 684 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 685 | if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 || |
| 686 | mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | mbedtls_exit( 1 ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 689 | } |
| 690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | dhm.len = mbedtls_mpi_size( &dhm.P ); |
| 692 | mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL ); |
| 693 | if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 ) |
| 694 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 697 | TIME_PUBLIC( title, "handshake", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | 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] | 699 | myrand, NULL ); |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 700 | 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] | 701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 703 | TIME_PUBLIC( title, "handshake", |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 704 | 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] | 705 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | mbedtls_dhm_free( &dhm ); |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 707 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 708 | } |
Manuel Pégourié-Gonnard | e870c0a | 2012-11-08 11:31:48 +0100 | [diff] [blame] | 709 | #endif |
| 710 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 712 | if( todo.ecdsa ) |
| 713 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 714 | mbedtls_ecdsa_context ecdsa; |
| 715 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 716 | size_t sig_len; |
| 717 | |
| 718 | memset( buf, 0x2A, sizeof( buf ) ); |
| 719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | for( curve_info = mbedtls_ecp_curve_list(); |
| 721 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 722 | curve_info++ ) |
| 723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | mbedtls_ecdsa_init( &ecdsa ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 725 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ) |
| 727 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 728 | ecp_clear_precomputed( &ecdsa.grp ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 731 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 732 | TIME_PUBLIC( title, "sign", |
Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 733 | 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] | 734 | tmp, &sig_len, myrand, NULL ) ); |
| 735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | mbedtls_ecdsa_free( &ecdsa ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 739 | for( curve_info = mbedtls_ecp_curve_list(); |
| 740 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 741 | curve_info++ ) |
| 742 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 743 | mbedtls_ecdsa_init( &ecdsa ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | 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] | 746 | 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] | 747 | tmp, &sig_len, myrand, NULL ) != 0 ) |
| 748 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 750 | } |
| 751 | ecp_clear_precomputed( &ecdsa.grp ); |
| 752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 753 | mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s", |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 754 | curve_info->name ); |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 755 | TIME_PUBLIC( title, "verify", |
Manuel Pégourié-Gonnard | 797f48a | 2015-06-18 15:45:05 +0200 | [diff] [blame] | 756 | 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] | 757 | tmp, sig_len ) ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 759 | mbedtls_ecdsa_free( &ecdsa ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | #endif |
| 763 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 764 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 765 | if( todo.ecdh ) |
| 766 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | mbedtls_ecdh_context ecdh; |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 768 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 769 | mbedtls_mpi z; |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 770 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 772 | size_t olen; |
| 773 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 774 | for( curve_info = mbedtls_ecp_curve_list(); |
| 775 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 776 | curve_info++ ) |
| 777 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 778 | mbedtls_ecdh_init( &ecdh ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 779 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 780 | if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 781 | mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 782 | myrand, NULL ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ) |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 784 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 786 | } |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 787 | ecp_clear_precomputed( &ecdh.grp ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 788 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s", |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 790 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 791 | TIME_PUBLIC( title, "handshake", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 792 | ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 793 | myrand, NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 794 | ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 795 | myrand, NULL ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | mbedtls_ecdh_free( &ecdh ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 799 | /* Curve25519 needs to be handled separately */ |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 800 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 801 | mbedtls_ecdh_init( &ecdh ); |
| 802 | mbedtls_mpi_init( &z ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 803 | |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 804 | if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 805 | mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 ) |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 806 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | TIME_PUBLIC( "ECDHE-Curve25519", "handshake", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 811 | ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 812 | myrand, NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 814 | myrand, NULL ) ); |
| 815 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | mbedtls_ecdh_free( &ecdh ); |
| 817 | mbedtls_mpi_free( &z ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 818 | #endif |
| 819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | for( curve_info = mbedtls_ecp_curve_list(); |
| 821 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 822 | curve_info++ ) |
| 823 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | mbedtls_ecdh_init( &ecdh ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 825 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 826 | if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 828 | myrand, NULL ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 829 | mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 || |
| 830 | mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf), |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 831 | myrand, NULL ) != 0 ) |
| 832 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 834 | } |
| 835 | ecp_clear_precomputed( &ecdh.grp ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | mbedtls_snprintf( title, sizeof( title ), "ECDH-%s", |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 838 | curve_info->name ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 839 | TIME_PUBLIC( title, "handshake", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 840 | ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ), |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 841 | myrand, NULL ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | mbedtls_ecdh_free( &ecdh ); |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 843 | } |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 844 | |
| 845 | /* Curve25519 needs to be handled separately */ |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 846 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 847 | mbedtls_ecdh_init( &ecdh ); |
| 848 | mbedtls_mpi_init( &z ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 849 | |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 850 | if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 851 | mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 852 | myrand, NULL ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 ) |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 854 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 855 | mbedtls_exit( 1 ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | TIME_PUBLIC( "ECDH-Curve25519", "handshake", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 859 | ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d, |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 860 | myrand, NULL ) ); |
| 861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 862 | mbedtls_ecdh_free( &ecdh ); |
| 863 | mbedtls_mpi_free( &z ); |
Manuel Pégourié-Gonnard | 85391f2 | 2015-02-05 09:54:48 +0000 | [diff] [blame] | 864 | #endif |
Manuel Pégourié-Gonnard | cc34f95 | 2013-09-17 16:04:08 +0200 | [diff] [blame] | 865 | } |
| 866 | #endif |
Manuel Pégourié-Gonnard | 50da048 | 2014-12-19 12:10:37 +0100 | [diff] [blame] | 867 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 868 | mbedtls_printf( "\n" ); |
Paul Bakker | 1d4da2e | 2009-10-25 12:36:53 +0000 | [diff] [blame] | 869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 871 | mbedtls_memory_buffer_alloc_free(); |
Manuel Pégourié-Gonnard | 128657d | 2014-12-18 16:35:52 +0000 | [diff] [blame] | 872 | #endif |
| 873 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 874 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 876 | fflush( stdout ); getchar(); |
| 877 | #endif |
| 878 | |
| 879 | return( 0 ); |
| 880 | } |
Manuel Pégourié-Gonnard | 8271f2f | 2013-09-17 14:57:55 +0200 | [diff] [blame] | 881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | #endif /* MBEDTLS_TIMING_C */ |