blob: e36616dcd5ba8db0d7c4b2ea1ca346bfcb95c682 [file] [log] [blame]
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01001/*
2 * Example ECDHE with Curve25519 program
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010046 * This file is part of mbed TLS (https://tls.mbed.org)
47 */
48
49#if !defined(MBEDTLS_CONFIG_FILE)
50#include "mbedtls/config.h"
51#else
52#include MBEDTLS_CONFIG_FILE
53#endif
54
55#if defined(MBEDTLS_PLATFORM_C)
56#include "mbedtls/platform.h"
57#else
58#include <stdio.h>
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010065
Janos Follath52735ef2018-08-15 10:19:16 +010066#if !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010067 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
68 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
69int main( void )
70{
Janos Follath52735ef2018-08-15 10:19:16 +010071 mbedtls_printf( "MBEDTLS_ECDH_C and/or MBEDTLS_ECDH_LEGACY_CONTEXT and/or "
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010072 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
73 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
74 "not defined\n" );
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010076}
77#else
78
79#include "mbedtls/entropy.h"
80#include "mbedtls/ctr_drbg.h"
81#include "mbedtls/ecdh.h"
82
Simon Butcher63cb97e2018-12-06 17:43:31 +000083
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010084int main( int argc, char *argv[] )
85{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010086 int ret = 1;
87 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010088 mbedtls_ecdh_context ctx_cli, ctx_srv;
89 mbedtls_entropy_context entropy;
90 mbedtls_ctr_drbg_context ctr_drbg;
91 unsigned char cli_to_srv[32], srv_to_cli[32];
92 const char pers[] = "ecdh";
93 ((void) argc);
94 ((void) argv);
95
96 mbedtls_ecdh_init( &ctx_cli );
97 mbedtls_ecdh_init( &ctx_srv );
98 mbedtls_ctr_drbg_init( &ctr_drbg );
99
100 /*
101 * Initialize random number generation
102 */
103 mbedtls_printf( " . Seeding the random number generator..." );
104 fflush( stdout );
105
106 mbedtls_entropy_init( &entropy );
107 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
108 (const unsigned char *) pers,
109 sizeof pers ) ) != 0 )
110 {
111 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
112 goto exit;
113 }
114
115 mbedtls_printf( " ok\n" );
116
117 /*
118 * Client: inialize context and generate keypair
119 */
120 mbedtls_printf( " . Setting up client context..." );
121 fflush( stdout );
122
123 ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
124 if( ret != 0 )
125 {
126 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
127 goto exit;
128 }
129
130 ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
131 mbedtls_ctr_drbg_random, &ctr_drbg );
132 if( ret != 0 )
133 {
134 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
135 goto exit;
136 }
137
138 ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
139 if( ret != 0 )
140 {
141 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
142 goto exit;
143 }
144
145 mbedtls_printf( " ok\n" );
146
147 /*
148 * Server: initialize context and generate keypair
149 */
150 mbedtls_printf( " . Setting up server context..." );
151 fflush( stdout );
152
153 ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
154 if( ret != 0 )
155 {
156 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
157 goto exit;
158 }
159
160 ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
161 mbedtls_ctr_drbg_random, &ctr_drbg );
162 if( ret != 0 )
163 {
164 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
165 goto exit;
166 }
167
168 ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
169 if( ret != 0 )
170 {
171 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
172 goto exit;
173 }
174
175 mbedtls_printf( " ok\n" );
176
177 /*
178 * Server: read peer's key and generate shared secret
179 */
180 mbedtls_printf( " . Server reading client key and computing secret..." );
181 fflush( stdout );
182
183 ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
184 if( ret != 0 )
185 {
186 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
187 goto exit;
188 }
189
190 ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
191 if( ret != 0 )
192 {
193 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
194 goto exit;
195 }
196
197 ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
198 &ctx_srv.Qp, &ctx_srv.d,
199 mbedtls_ctr_drbg_random, &ctr_drbg );
200 if( ret != 0 )
201 {
202 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
203 goto exit;
204 }
205
206 mbedtls_printf( " ok\n" );
207
208 /*
209 * Client: read peer's key and generate shared secret
210 */
211 mbedtls_printf( " . Client reading server key and computing secret..." );
212 fflush( stdout );
213
214 ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
215 if( ret != 0 )
216 {
217 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
218 goto exit;
219 }
220
221 ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
222 if( ret != 0 )
223 {
224 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
225 goto exit;
226 }
227
228 ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
229 &ctx_cli.Qp, &ctx_cli.d,
230 mbedtls_ctr_drbg_random, &ctr_drbg );
231 if( ret != 0 )
232 {
233 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
234 goto exit;
235 }
236
237 mbedtls_printf( " ok\n" );
238
239 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300240 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100241 */
242 mbedtls_printf( " . Checking if both computed secrets are equal..." );
243 fflush( stdout );
244
245 ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
246 if( ret != 0 )
247 {
248 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
249 goto exit;
250 }
251
252 mbedtls_printf( " ok\n" );
253
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100254 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100255
256exit:
257
258#if defined(_WIN32)
259 mbedtls_printf( " + Press Enter to exit this program.\n" );
260 fflush( stdout ); getchar();
261#endif
262
263 mbedtls_ecdh_free( &ctx_srv );
264 mbedtls_ecdh_free( &ctx_cli );
265 mbedtls_ctr_drbg_free( &ctr_drbg );
266 mbedtls_entropy_free( &entropy );
267
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200268 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100269}
270#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
271 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */