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) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | int main(void) |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 32 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 33 | mbedtls_printf("MBEDTLS_ECDH_C and/or " |
| 34 | "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or " |
| 35 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C " |
| 36 | "not defined\n"); |
| 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | int main(int argc, char *argv[]) |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | mbedtls_ecdh_init(&ctx_cli); |
| 68 | mbedtls_ecdh_init(&ctx_srv); |
| 69 | mbedtls_ctr_drbg_init(&ctr_drbg); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Initialize random number generation |
| 73 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | mbedtls_printf(" . Seed the random number generator..."); |
| 75 | fflush(stdout); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 77 | mbedtls_entropy_init(&entropy); |
| 78 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, |
| 79 | &entropy, |
| 80 | (const unsigned char *) pers, |
Dave Rodgman | 1868870 | 2023-02-02 12:40:50 +0000 | [diff] [blame] | 81 | sizeof(pers))) != 0) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", |
| 83 | ret); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 84 | goto exit; |
| 85 | } |
| 86 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 87 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 88 | |
| 89 | /* |
HowJMay | ccbd622 | 2020-07-29 16:59:19 +0800 | [diff] [blame] | 90 | * Client: initialize context and generate keypair |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 91 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | mbedtls_printf(" . Set up client context, generate EC key pair..."); |
| 93 | fflush(stdout); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 94 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | ret = mbedtls_ecdh_setup(&ctx_cli, MBEDTLS_ECP_DP_CURVE25519); |
| 96 | if (ret != 0) { |
| 97 | 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] | 98 | goto exit; |
| 99 | } |
| 100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 101 | ret = mbedtls_ecdh_make_params(&ctx_cli, &cli_olen, cli_to_srv, |
| 102 | sizeof(cli_to_srv), |
| 103 | mbedtls_ctr_drbg_random, &ctr_drbg); |
| 104 | if (ret != 0) { |
| 105 | mbedtls_printf(" failed\n ! mbedtls_ecdh_make_params returned %d\n", |
| 106 | ret); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 107 | goto exit; |
| 108 | } |
| 109 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Server: initialize context and generate keypair |
| 114 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | mbedtls_printf(" . Server: read params, generate public key..."); |
| 116 | fflush(stdout); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | ret = mbedtls_ecdh_read_params(&ctx_srv, &p_cli_to_srv, |
| 119 | p_cli_to_srv + sizeof(cli_to_srv)); |
| 120 | if (ret != 0) { |
| 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | ret = mbedtls_ecdh_make_public(&ctx_srv, &srv_olen, srv_to_cli, |
| 127 | sizeof(srv_to_cli), |
| 128 | mbedtls_ctr_drbg_random, &ctr_drbg); |
| 129 | if (ret != 0) { |
| 130 | mbedtls_printf(" failed\n ! mbedtls_ecdh_make_public returned %d\n", |
| 131 | ret); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 132 | goto exit; |
| 133 | } |
| 134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 136 | |
| 137 | /* |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 138 | * Client: read public key |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 139 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | mbedtls_printf(" . Client: read public key..."); |
| 141 | fflush(stdout); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 142 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | ret = mbedtls_ecdh_read_public(&ctx_cli, srv_to_cli, |
| 144 | sizeof(srv_to_cli)); |
| 145 | if (ret != 0) { |
| 146 | mbedtls_printf(" failed\n ! mbedtls_ecdh_read_public returned %d\n", |
| 147 | ret); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 148 | goto exit; |
| 149 | } |
| 150 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 152 | |
| 153 | /* |
Thomas Daubney | d99f8b2 | 2022-05-18 15:13:31 +0100 | [diff] [blame] | 154 | * Calculate secrets |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 155 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | mbedtls_printf(" . Calculate secrets..."); |
| 157 | fflush(stdout); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | ret = mbedtls_ecdh_calc_secret(&ctx_cli, &cli_olen, secret_cli, |
| 160 | sizeof(secret_cli), |
| 161 | mbedtls_ctr_drbg_random, &ctr_drbg); |
| 162 | if (ret != 0) { |
| 163 | mbedtls_printf(" failed\n ! mbedtls_ecdh_calc_secret returned %d\n", |
| 164 | ret); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 165 | goto exit; |
| 166 | } |
| 167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | ret = mbedtls_ecdh_calc_secret(&ctx_srv, &srv_olen, secret_srv, |
| 169 | sizeof(secret_srv), |
| 170 | mbedtls_ctr_drbg_random, &ctr_drbg); |
| 171 | if (ret != 0) { |
| 172 | mbedtls_printf(" failed\n ! mbedtls_ecdh_calc_secret returned %d\n", |
| 173 | ret); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 174 | goto exit; |
| 175 | } |
| 176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 178 | |
| 179 | /* |
Ron Eldor | 2a47be5 | 2017-06-20 15:23:23 +0300 | [diff] [blame] | 180 | * Verification: are the computed secrets equal? |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 181 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | mbedtls_printf(" . Check if both calculated secrets are equal..."); |
| 183 | fflush(stdout); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | ret = memcmp(secret_srv, secret_cli, srv_olen); |
| 186 | if (ret != 0 || (cli_olen != srv_olen)) { |
| 187 | mbedtls_printf(" failed\n ! Shared secrets not equal.\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 188 | goto exit; |
| 189 | } |
| 190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 191 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 192 | |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 193 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 194 | |
| 195 | exit: |
| 196 | |
| 197 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 199 | fflush(stdout); getchar(); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 200 | #endif |
| 201 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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 */ |