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