blob: a348eff8a0a0bf478be9b110355aeaefa6770a52 [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
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01005 * 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é-Gonnard3eb8c342015-10-09 12:11:14 +010018 */
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é-Gonnard3eb8c342015-10-09 12:11:14 +010026#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010027
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010028#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010029 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
30 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031int main(void)
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010032{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033 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é-Gonnard3eb8c342015-10-09 12:11:14 +010038}
39#else
40
41#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
43#include "mbedtls/ecdh.h"
44
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010045#include <string.h>
46
Simon Butcher63cb97e2018-12-06 17:43:31 +000047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048int main(int argc, char *argv[])
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010049{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010050 int ret = 1;
51 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010052 mbedtls_ecdh_context ctx_cli, ctx_srv;
53 mbedtls_entropy_context entropy;
54 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010055 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010056 const char pers[] = "ecdh";
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010057
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é-Gonnard3eb8c342015-10-09 12:11:14 +010064 ((void) argc);
65 ((void) argv);
66
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 mbedtls_ecdh_init(&ctx_cli);
68 mbedtls_ecdh_init(&ctx_srv);
69 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010070
71 /*
72 * Initialize random number generation
73 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 mbedtls_printf(" . Seed the random number generator...");
75 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mbedtls_entropy_init(&entropy);
78 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
79 &entropy,
80 (const unsigned char *) pers,
Dave Rodgman18688702023-02-02 12:40:50 +000081 sizeof(pers))) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
83 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010084 goto exit;
85 }
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010088
89 /*
HowJMayccbd6222020-07-29 16:59:19 +080090 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010091 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 mbedtls_printf(" . Set up client context, generate EC key pair...");
93 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 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é-Gonnard3eb8c342015-10-09 12:11:14 +010098 goto exit;
99 }
100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100107 goto exit;
108 }
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100111
112 /*
113 * Server: initialize context and generate keypair
114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 mbedtls_printf(" . Server: read params, generate public key...");
116 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100123 goto exit;
124 }
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100132 goto exit;
133 }
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100136
137 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100138 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100139 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 mbedtls_printf(" . Client: read public key...");
141 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100148 goto exit;
149 }
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100152
153 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100154 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100155 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 mbedtls_printf(" . Calculate secrets...");
157 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100165 goto exit;
166 }
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100174 goto exit;
175 }
176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100178
179 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300180 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 mbedtls_printf(" . Check if both calculated secrets are equal...");
183 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100188 goto exit;
189 }
190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100192
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100193 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100194
195exit:
196
197#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_printf(" + Press Enter to exit this program.\n");
199 fflush(stdout); getchar();
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100200#endif
201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 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é-Gonnard3eb8c342015-10-09 12:11:14 +0100206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100208}
209#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
210 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */