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