blob: 1c101390a81198a47494f2e989d11ec123f8dfe4 [file] [log] [blame]
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01001/*
2 * Example ECDHE with Curve25519 program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010045 */
46
47#if !defined(MBEDTLS_CONFIG_FILE)
48#include "mbedtls/config.h"
49#else
50#include MBEDTLS_CONFIG_FILE
51#endif
52
53#if defined(MBEDTLS_PLATFORM_C)
54#include "mbedtls/platform.h"
55#else
56#include <stdio.h>
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010063
Janos Follath52735ef2018-08-15 10:19:16 +010064#if !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010065 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
66 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
67int main( void )
68{
Janos Follath52735ef2018-08-15 10:19:16 +010069 mbedtls_printf( "MBEDTLS_ECDH_C and/or MBEDTLS_ECDH_LEGACY_CONTEXT and/or "
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010070 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
71 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
72 "not defined\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020073 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010074}
75#else
76
77#include "mbedtls/entropy.h"
78#include "mbedtls/ctr_drbg.h"
79#include "mbedtls/ecdh.h"
80
Simon Butcher63cb97e2018-12-06 17:43:31 +000081
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010082int main( int argc, char *argv[] )
83{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010084 int ret = 1;
85 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010086 mbedtls_ecdh_context ctx_cli, ctx_srv;
87 mbedtls_entropy_context entropy;
88 mbedtls_ctr_drbg_context ctr_drbg;
89 unsigned char cli_to_srv[32], srv_to_cli[32];
90 const char pers[] = "ecdh";
91 ((void) argc);
92 ((void) argv);
93
94 mbedtls_ecdh_init( &ctx_cli );
95 mbedtls_ecdh_init( &ctx_srv );
96 mbedtls_ctr_drbg_init( &ctr_drbg );
97
98 /*
99 * Initialize random number generation
100 */
101 mbedtls_printf( " . Seeding the random number generator..." );
102 fflush( stdout );
103
104 mbedtls_entropy_init( &entropy );
105 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
106 (const unsigned char *) pers,
107 sizeof pers ) ) != 0 )
108 {
109 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
110 goto exit;
111 }
112
113 mbedtls_printf( " ok\n" );
114
115 /*
116 * Client: inialize context and generate keypair
117 */
118 mbedtls_printf( " . Setting up client context..." );
119 fflush( stdout );
120
121 ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
122 if( ret != 0 )
123 {
124 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
125 goto exit;
126 }
127
128 ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
129 mbedtls_ctr_drbg_random, &ctr_drbg );
130 if( ret != 0 )
131 {
132 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
133 goto exit;
134 }
135
136 ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
137 if( ret != 0 )
138 {
139 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
140 goto exit;
141 }
142
143 mbedtls_printf( " ok\n" );
144
145 /*
146 * Server: initialize context and generate keypair
147 */
148 mbedtls_printf( " . Setting up server context..." );
149 fflush( stdout );
150
151 ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
152 if( ret != 0 )
153 {
154 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
155 goto exit;
156 }
157
158 ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
159 mbedtls_ctr_drbg_random, &ctr_drbg );
160 if( ret != 0 )
161 {
162 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
163 goto exit;
164 }
165
166 ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
167 if( ret != 0 )
168 {
169 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
170 goto exit;
171 }
172
173 mbedtls_printf( " ok\n" );
174
175 /*
176 * Server: read peer's key and generate shared secret
177 */
178 mbedtls_printf( " . Server reading client key and computing secret..." );
179 fflush( stdout );
180
181 ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
182 if( ret != 0 )
183 {
184 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
185 goto exit;
186 }
187
188 ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
189 if( ret != 0 )
190 {
191 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
192 goto exit;
193 }
194
195 ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
196 &ctx_srv.Qp, &ctx_srv.d,
197 mbedtls_ctr_drbg_random, &ctr_drbg );
198 if( ret != 0 )
199 {
200 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
201 goto exit;
202 }
203
204 mbedtls_printf( " ok\n" );
205
206 /*
207 * Client: read peer's key and generate shared secret
208 */
209 mbedtls_printf( " . Client reading server key and computing secret..." );
210 fflush( stdout );
211
212 ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
213 if( ret != 0 )
214 {
215 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
216 goto exit;
217 }
218
219 ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
220 if( ret != 0 )
221 {
222 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
223 goto exit;
224 }
225
226 ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
227 &ctx_cli.Qp, &ctx_cli.d,
228 mbedtls_ctr_drbg_random, &ctr_drbg );
229 if( ret != 0 )
230 {
231 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
232 goto exit;
233 }
234
235 mbedtls_printf( " ok\n" );
236
237 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300238 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100239 */
240 mbedtls_printf( " . Checking if both computed secrets are equal..." );
241 fflush( stdout );
242
243 ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
244 if( ret != 0 )
245 {
246 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
247 goto exit;
248 }
249
250 mbedtls_printf( " ok\n" );
251
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100252 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100253
254exit:
255
256#if defined(_WIN32)
257 mbedtls_printf( " + Press Enter to exit this program.\n" );
258 fflush( stdout ); getchar();
259#endif
260
261 mbedtls_ecdh_free( &ctx_srv );
262 mbedtls_ecdh_free( &ctx_cli );
263 mbedtls_ctr_drbg_free( &ctr_drbg );
264 mbedtls_entropy_free( &entropy );
265
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200266 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100267}
268#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
269 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */