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