blob: f79b1659654a0331701e745b11f461b393ff97a4 [file] [log] [blame]
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01001/*
2 * Example ECDHE with Curve25519 program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01006 */
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é-Gonnard3eb8c342015-10-09 12:11:14 +010014#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010015
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010016#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010017 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
18 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010019int main(void)
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010020{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010021 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é-Gonnard3eb8c342015-10-09 12:11:14 +010026}
27#else
28
29#include "mbedtls/entropy.h"
30#include "mbedtls/ctr_drbg.h"
31#include "mbedtls/ecdh.h"
32
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010033#include <string.h>
34
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036int main(int argc, char *argv[])
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010037{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010038 int ret = 1;
39 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010040 mbedtls_ecdh_context ctx_cli, ctx_srv;
41 mbedtls_entropy_context entropy;
42 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010043 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010044 const char pers[] = "ecdh";
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010045
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é-Gonnard3eb8c342015-10-09 12:11:14 +010052 ((void) argc);
53 ((void) argv);
54
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 mbedtls_ecdh_init(&ctx_cli);
56 mbedtls_ecdh_init(&ctx_srv);
57 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010058
59 /*
60 * Initialize random number generation
61 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 mbedtls_printf(" . Seed the random number generator...");
63 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mbedtls_entropy_init(&entropy);
66 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
67 &entropy,
68 (const unsigned char *) pers,
Dave Rodgman18688702023-02-02 12:40:50 +000069 sizeof(pers))) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
71 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010072 goto exit;
73 }
74
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010076
77 /*
HowJMayccbd6222020-07-29 16:59:19 +080078 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010079 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 mbedtls_printf(" . Set up client context, generate EC key pair...");
81 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 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é-Gonnard3eb8c342015-10-09 12:11:14 +010086 goto exit;
87 }
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 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é-Gonnard3eb8c342015-10-09 12:11:14 +010095 goto exit;
96 }
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010099
100 /*
101 * Server: initialize context and generate keypair
102 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 mbedtls_printf(" . Server: read params, generate public key...");
104 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100111 goto exit;
112 }
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100120 goto exit;
121 }
122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100124
125 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100126 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100127 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 mbedtls_printf(" . Client: read public key...");
129 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100136 goto exit;
137 }
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100140
141 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100142 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100143 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 mbedtls_printf(" . Calculate secrets...");
145 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100153 goto exit;
154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100162 goto exit;
163 }
164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100166
167 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300168 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100169 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_printf(" . Check if both calculated secrets are equal...");
171 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100176 goto exit;
177 }
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100180
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100181 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100182
183exit:
184
185#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 mbedtls_printf(" + Press Enter to exit this program.\n");
187 fflush(stdout); getchar();
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100188#endif
189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100196}
197#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
198 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */