blob: d880a1a0a81c77af70098f1e2e7d99aba34bbb94 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010021
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010022#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010023
Thomas Daubney88fed8e2022-04-12 09:03:22 +010024#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010025 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010028{
Gilles Peskine449bd832023-01-11 14:50:10 +010029 mbedtls_printf("MBEDTLS_ECDH_C and/or "
30 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
31 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
32 "not defined\n");
33 mbedtls_exit(0);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010034}
35#else
36
37#include "mbedtls/entropy.h"
38#include "mbedtls/ctr_drbg.h"
39#include "mbedtls/ecdh.h"
40
Thomas Daubney88fed8e2022-04-12 09:03:22 +010041#include <string.h>
42
Simon Butcher63cb97e2018-12-06 17:43:31 +000043
Gilles Peskine449bd832023-01-11 14:50:10 +010044int main(int argc, char *argv[])
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010045{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010046 int ret = 1;
47 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010048 mbedtls_ecdh_context ctx_cli, ctx_srv;
49 mbedtls_entropy_context entropy;
50 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubney88fed8e2022-04-12 09:03:22 +010051 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010052 const char pers[] = "ecdh";
Thomas Daubney88fed8e2022-04-12 09:03:22 +010053
Thomas Daubney70c00882022-05-20 18:43:09 +010054 size_t srv_olen;
55 size_t cli_olen;
Thomas Daubney306a8902022-05-18 14:22:08 +010056 unsigned char secret_cli[32] = { 0 };
57 unsigned char secret_srv[32] = { 0 };
Thomas Daubney88fed8e2022-04-12 09:03:22 +010058 const unsigned char *p_cli_to_srv = cli_to_srv;
59
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010060 ((void) argc);
61 ((void) argv);
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063 mbedtls_ecdh_init(&ctx_cli);
64 mbedtls_ecdh_init(&ctx_srv);
65 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010066
67 /*
68 * Initialize random number generation
69 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_printf(" . Seed the random number generator...");
71 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_entropy_init(&entropy);
74 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
75 &entropy,
76 (const unsigned char *) pers,
77 sizeof pers)) != 0) {
78 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
79 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010080 goto exit;
81 }
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010084
85 /*
HowJMayccbd6222020-07-29 16:59:19 +080086 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010087 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_printf(" . Set up client context, generate EC key pair...");
89 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 ret = mbedtls_ecdh_setup(&ctx_cli, MBEDTLS_ECP_DP_CURVE25519);
92 if (ret != 0) {
93 mbedtls_printf(" failed\n ! mbedtls_ecdh_setup returned %d\n", ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010094 goto exit;
95 }
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097 ret = mbedtls_ecdh_make_params(&ctx_cli, &cli_olen, cli_to_srv,
98 sizeof(cli_to_srv),
99 mbedtls_ctr_drbg_random, &ctr_drbg);
100 if (ret != 0) {
101 mbedtls_printf(" failed\n ! mbedtls_ecdh_make_params returned %d\n",
102 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100103 goto exit;
104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100107
108 /*
109 * Server: initialize context and generate keypair
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_printf(" . Server: read params, generate public key...");
112 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 ret = mbedtls_ecdh_read_params(&ctx_srv, &p_cli_to_srv,
115 p_cli_to_srv + sizeof(cli_to_srv));
116 if (ret != 0) {
117 mbedtls_printf(" failed\n ! mbedtls_ecdh_read_params returned %d\n",
118 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 ret = mbedtls_ecdh_make_public(&ctx_srv, &srv_olen, srv_to_cli,
123 sizeof(srv_to_cli),
124 mbedtls_ctr_drbg_random, &ctr_drbg);
125 if (ret != 0) {
126 mbedtls_printf(" failed\n ! mbedtls_ecdh_make_public returned %d\n",
127 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100128 goto exit;
129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100132
133 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100134 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_printf(" . Client: read public key...");
137 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 ret = mbedtls_ecdh_read_public(&ctx_cli, srv_to_cli,
140 sizeof(srv_to_cli));
141 if (ret != 0) {
142 mbedtls_printf(" failed\n ! mbedtls_ecdh_read_public returned %d\n",
143 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100144 goto exit;
145 }
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100148
149 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100150 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100151 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_printf(" . Calculate secrets...");
153 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 ret = mbedtls_ecdh_calc_secret(&ctx_cli, &cli_olen, secret_cli,
156 sizeof(secret_cli),
157 mbedtls_ctr_drbg_random, &ctr_drbg);
158 if (ret != 0) {
159 mbedtls_printf(" failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
160 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100161 goto exit;
162 }
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 ret = mbedtls_ecdh_calc_secret(&ctx_srv, &srv_olen, secret_srv,
165 sizeof(secret_srv),
166 mbedtls_ctr_drbg_random, &ctr_drbg);
167 if (ret != 0) {
168 mbedtls_printf(" failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
169 ret);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100170 goto exit;
171 }
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100174
175 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300176 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_printf(" . Check if both calculated secrets are equal...");
179 fflush(stdout);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 ret = memcmp(secret_srv, secret_cli, srv_olen);
182 if (ret != 0 || (cli_olen != srv_olen)) {
183 mbedtls_printf(" failed\n ! Shared secrets not equal.\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100184 goto exit;
185 }
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100188
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100189 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100190
191exit:
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_ecdh_free(&ctx_srv);
194 mbedtls_ecdh_free(&ctx_cli);
195 mbedtls_ctr_drbg_free(&ctr_drbg);
196 mbedtls_entropy_free(&entropy);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_exit(exit_code);
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100199}
200#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
201 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */