blob: 8c6de350601945eb1e01e02fa05f2a3cbde3601c [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úti4e9f7122020-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úti4e9f7122020-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 Garcia81982c82018-04-29 22:16:23 +010059#include <stdlib.h>
60#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020061#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia81982c82018-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
66#if !defined(MBEDTLS_ECDH_C) || \
67 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
68 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
69int main( void )
70{
71 mbedtls_printf( "MBEDTLS_ECDH_C and/or "
72 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
73 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
74 "not defined\n" );
Krzysztof Stachowiaka0865222019-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
83int main( int argc, char *argv[] )
84{
Andres Amaya Garcia81982c82018-04-29 22:16:23 +010085 int ret = 1;
86 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010087 mbedtls_ecdh_context ctx_cli, ctx_srv;
88 mbedtls_entropy_context entropy;
89 mbedtls_ctr_drbg_context ctr_drbg;
90 unsigned char cli_to_srv[32], srv_to_cli[32];
91 const char pers[] = "ecdh";
92 ((void) argc);
93 ((void) argv);
94
95 mbedtls_ecdh_init( &ctx_cli );
96 mbedtls_ecdh_init( &ctx_srv );
97 mbedtls_ctr_drbg_init( &ctr_drbg );
98
99 /*
100 * Initialize random number generation
101 */
102 mbedtls_printf( " . Seeding the random number generator..." );
103 fflush( stdout );
104
105 mbedtls_entropy_init( &entropy );
106 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
107 (const unsigned char *) pers,
108 sizeof pers ) ) != 0 )
109 {
110 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
111 goto exit;
112 }
113
114 mbedtls_printf( " ok\n" );
115
116 /*
117 * Client: inialize context and generate keypair
118 */
119 mbedtls_printf( " . Setting up client context..." );
120 fflush( stdout );
121
122 ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
123 if( ret != 0 )
124 {
125 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
126 goto exit;
127 }
128
129 ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
130 mbedtls_ctr_drbg_random, &ctr_drbg );
131 if( ret != 0 )
132 {
133 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
134 goto exit;
135 }
136
137 ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
138 if( ret != 0 )
139 {
140 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
141 goto exit;
142 }
143
144 mbedtls_printf( " ok\n" );
145
146 /*
147 * Server: initialize context and generate keypair
148 */
149 mbedtls_printf( " . Setting up server context..." );
150 fflush( stdout );
151
152 ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
153 if( ret != 0 )
154 {
155 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
156 goto exit;
157 }
158
159 ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
160 mbedtls_ctr_drbg_random, &ctr_drbg );
161 if( ret != 0 )
162 {
163 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
164 goto exit;
165 }
166
167 ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
168 if( ret != 0 )
169 {
170 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
171 goto exit;
172 }
173
174 mbedtls_printf( " ok\n" );
175
176 /*
177 * Server: read peer's key and generate shared secret
178 */
179 mbedtls_printf( " . Server reading client key and computing secret..." );
180 fflush( stdout );
181
182 ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
183 if( ret != 0 )
184 {
185 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
186 goto exit;
187 }
188
189 ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
190 if( ret != 0 )
191 {
192 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
193 goto exit;
194 }
195
196 ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
197 &ctx_srv.Qp, &ctx_srv.d,
198 mbedtls_ctr_drbg_random, &ctr_drbg );
199 if( ret != 0 )
200 {
201 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
202 goto exit;
203 }
204
205 mbedtls_printf( " ok\n" );
206
207 /*
208 * Client: read peer's key and generate shared secret
209 */
210 mbedtls_printf( " . Client reading server key and computing secret..." );
211 fflush( stdout );
212
213 ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
214 if( ret != 0 )
215 {
216 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
217 goto exit;
218 }
219
220 ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
221 if( ret != 0 )
222 {
223 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
224 goto exit;
225 }
226
227 ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
228 &ctx_cli.Qp, &ctx_cli.d,
229 mbedtls_ctr_drbg_random, &ctr_drbg );
230 if( ret != 0 )
231 {
232 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
233 goto exit;
234 }
235
236 mbedtls_printf( " ok\n" );
237
238 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300239 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100240 */
241 mbedtls_printf( " . Checking if both computed secrets are equal..." );
242 fflush( stdout );
243
244 ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
245 if( ret != 0 )
246 {
247 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
248 goto exit;
249 }
250
251 mbedtls_printf( " ok\n" );
252
Andres Amaya Garcia81982c82018-04-29 22:16:23 +0100253 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100254
255exit:
256
257#if defined(_WIN32)
258 mbedtls_printf( " + Press Enter to exit this program.\n" );
259 fflush( stdout ); getchar();
260#endif
261
262 mbedtls_ecdh_free( &ctx_srv );
263 mbedtls_ecdh_free( &ctx_cli );
264 mbedtls_ctr_drbg_free( &ctr_drbg );
265 mbedtls_entropy_free( &entropy );
266
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200267 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100268}
269#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
270 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */