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