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