Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Example ECDHE with Curve25519 program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [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. |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 21 | #include "mbedtls/config.h" |
| 22 | #else |
| 23 | #include MBEDTLS_CONFIG_FILE |
| 24 | #endif |
| 25 | |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 27 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 28 | #if !defined(MBEDTLS_ECDH_C) || \ |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 29 | !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \ |
| 30 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) |
| 31 | int main( void ) |
| 32 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 33 | mbedtls_printf( "MBEDTLS_ECDH_C and/or " |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 34 | "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or " |
| 35 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C " |
| 36 | "not defined\n" ); |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 37 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 38 | } |
| 39 | #else |
| 40 | |
| 41 | #include "mbedtls/entropy.h" |
| 42 | #include "mbedtls/ctr_drbg.h" |
| 43 | #include "mbedtls/ecdh.h" |
| 44 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 45 | #include <string.h> |
| 46 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 48 | int main( int argc, char *argv[] ) |
| 49 | { |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 50 | int ret = 1; |
| 51 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 52 | mbedtls_ecdh_context ctx_cli, ctx_srv; |
| 53 | mbedtls_entropy_context entropy; |
| 54 | mbedtls_ctr_drbg_context ctr_drbg; |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 55 | unsigned char cli_to_srv[36], srv_to_cli[33]; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 56 | const char pers[] = "ecdh"; |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 57 | |
| 58 | size_t srv_olen; |
| 59 | size_t cli_olen; |
| 60 | unsigned char secret_cli[32] = { 0 }; |
| 61 | unsigned char secret_srv[32] = { 0 }; |
| 62 | const unsigned char *p_cli_to_srv = cli_to_srv; |
| 63 | |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 64 | ((void) argc); |
| 65 | ((void) argv); |
| 66 | |
| 67 | mbedtls_ecdh_init( &ctx_cli ); |
| 68 | mbedtls_ecdh_init( &ctx_srv ); |
| 69 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 70 | |
| 71 | /* |
| 72 | * Initialize random number generation |
| 73 | */ |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 74 | mbedtls_printf( " . Seed the random number generator..." ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 75 | fflush( stdout ); |
| 76 | |
| 77 | mbedtls_entropy_init( &entropy ); |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 78 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 79 | &entropy, |
| 80 | (const unsigned char *) pers, |
| 81 | sizeof pers ) ) != 0 ) |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 82 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 83 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", |
| 84 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 85 | goto exit; |
| 86 | } |
| 87 | |
| 88 | mbedtls_printf( " ok\n" ); |
| 89 | |
| 90 | /* |
HowJMay | ccbd622 | 2020-07-29 16:59:19 +0800 | [diff] [blame] | 91 | * Client: initialize context and generate keypair |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 92 | */ |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 93 | mbedtls_printf( " . Set up client context, generate EC key pair..." ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 94 | fflush( stdout ); |
| 95 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 96 | ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 97 | if( ret != 0 ) |
| 98 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 99 | mbedtls_printf( " failed\n ! mbedtls_ecdh_setup returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 100 | goto exit; |
| 101 | } |
| 102 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 103 | ret = mbedtls_ecdh_make_params( &ctx_cli, &cli_olen, cli_to_srv, |
| 104 | sizeof( cli_to_srv ), |
| 105 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 106 | if( ret != 0 ) |
| 107 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 108 | mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n", |
| 109 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | mbedtls_printf( " ok\n" ); |
| 114 | |
| 115 | /* |
| 116 | * Server: initialize context and generate keypair |
| 117 | */ |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 118 | mbedtls_printf( " . Server: read params, generate public key..." ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 119 | fflush( stdout ); |
| 120 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 121 | ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv, |
| 122 | p_cli_to_srv + sizeof( cli_to_srv ) ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 123 | if( ret != 0 ) |
| 124 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 125 | mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n", |
| 126 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 127 | goto exit; |
| 128 | } |
| 129 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 130 | ret = mbedtls_ecdh_make_public( &ctx_srv, &srv_olen, srv_to_cli, |
| 131 | sizeof( srv_to_cli ), |
| 132 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 133 | if( ret != 0 ) |
| 134 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 135 | mbedtls_printf( " failed\n ! mbedtls_ecdh_make_public returned %d\n", |
| 136 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 137 | goto exit; |
| 138 | } |
| 139 | |
| 140 | mbedtls_printf( " ok\n" ); |
| 141 | |
| 142 | /* |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 143 | * Client: read public key |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 144 | */ |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 145 | mbedtls_printf( " . Client: read public key..." ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 146 | fflush( stdout ); |
| 147 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 148 | ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli, |
| 149 | sizeof( srv_to_cli ) ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 150 | if( ret != 0 ) |
| 151 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 152 | mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n", |
| 153 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 154 | goto exit; |
| 155 | } |
| 156 | |
| 157 | mbedtls_printf( " ok\n" ); |
| 158 | |
| 159 | /* |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 160 | * Calculate secrets |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 161 | */ |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 162 | mbedtls_printf( " . Calculate secrets..." ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 163 | fflush( stdout ); |
| 164 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 165 | ret = mbedtls_ecdh_calc_secret( &ctx_cli, &cli_olen, secret_cli, |
| 166 | sizeof( secret_cli ), |
| 167 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 168 | if( ret != 0 ) |
| 169 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 170 | mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n", |
| 171 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
| 174 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 175 | ret = mbedtls_ecdh_calc_secret( &ctx_srv, &srv_olen, secret_srv, |
| 176 | sizeof( secret_srv ), |
| 177 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 178 | if( ret != 0 ) |
| 179 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 180 | mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n", |
| 181 | ret ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 182 | goto exit; |
| 183 | } |
| 184 | |
| 185 | mbedtls_printf( " ok\n" ); |
| 186 | |
| 187 | /* |
Ron Eldor | 2a47be5 | 2017-06-20 15:23:23 +0300 | [diff] [blame] | 188 | * Verification: are the computed secrets equal? |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 189 | */ |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 190 | mbedtls_printf( " . Check if both calculated secrets are equal..." ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 191 | fflush( stdout ); |
| 192 | |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 193 | ret = memcmp( secret_srv, secret_cli, srv_olen ); |
| 194 | if( ret != 0 || ( cli_olen != srv_olen ) ) |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 195 | { |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 196 | mbedtls_printf( " failed\n ! Shared secrets not equal.\n" ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 197 | goto exit; |
| 198 | } |
| 199 | |
| 200 | mbedtls_printf( " ok\n" ); |
| 201 | |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 202 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 203 | |
| 204 | exit: |
| 205 | |
| 206 | #if defined(_WIN32) |
| 207 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
| 208 | fflush( stdout ); getchar(); |
| 209 | #endif |
| 210 | |
| 211 | mbedtls_ecdh_free( &ctx_srv ); |
| 212 | mbedtls_ecdh_free( &ctx_cli ); |
| 213 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 214 | mbedtls_entropy_free( &entropy ); |
| 215 | |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 216 | mbedtls_exit( exit_code ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 217 | } |
| 218 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED && |
| 219 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |