blob: 2a21af0d65e4983afa1fb360d072dd9707924f1c [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
5 * 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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PLATFORM_C)
29#include "mbedtls/platform.h"
30#else
31#include <stdio.h>
Andres Amaya Garcia81982c82018-04-29 22:16:23 +010032#include <stdlib.h>
33#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020034#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010035#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia81982c82018-04-29 22:16:23 +010036#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010038
39#if !defined(MBEDTLS_ECDH_C) || \
40 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
41 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
42int main( void )
43{
44 mbedtls_printf( "MBEDTLS_ECDH_C and/or "
45 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
46 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
47 "not defined\n" );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020048 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010049}
50#else
51
52#include "mbedtls/entropy.h"
53#include "mbedtls/ctr_drbg.h"
54#include "mbedtls/ecdh.h"
55
56int main( int argc, char *argv[] )
57{
Andres Amaya Garcia81982c82018-04-29 22:16:23 +010058 int ret = 1;
59 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010060 mbedtls_ecdh_context ctx_cli, ctx_srv;
61 mbedtls_entropy_context entropy;
62 mbedtls_ctr_drbg_context ctr_drbg;
63 unsigned char cli_to_srv[32], srv_to_cli[32];
64 const char pers[] = "ecdh";
65 ((void) argc);
66 ((void) argv);
67
68 mbedtls_ecdh_init( &ctx_cli );
69 mbedtls_ecdh_init( &ctx_srv );
70 mbedtls_ctr_drbg_init( &ctr_drbg );
71
72 /*
73 * Initialize random number generation
74 */
75 mbedtls_printf( " . Seeding the random number generator..." );
76 fflush( stdout );
77
78 mbedtls_entropy_init( &entropy );
79 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
80 (const unsigned char *) pers,
81 sizeof pers ) ) != 0 )
82 {
83 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
84 goto exit;
85 }
86
87 mbedtls_printf( " ok\n" );
88
89 /*
90 * Client: inialize context and generate keypair
91 */
92 mbedtls_printf( " . Setting up client context..." );
93 fflush( stdout );
94
95 ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
96 if( ret != 0 )
97 {
98 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
99 goto exit;
100 }
101
102 ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
103 mbedtls_ctr_drbg_random, &ctr_drbg );
104 if( ret != 0 )
105 {
106 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
107 goto exit;
108 }
109
110 ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
111 if( ret != 0 )
112 {
113 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
114 goto exit;
115 }
116
117 mbedtls_printf( " ok\n" );
118
119 /*
120 * Server: initialize context and generate keypair
121 */
122 mbedtls_printf( " . Setting up server context..." );
123 fflush( stdout );
124
125 ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
126 if( ret != 0 )
127 {
128 mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
129 goto exit;
130 }
131
132 ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
133 mbedtls_ctr_drbg_random, &ctr_drbg );
134 if( ret != 0 )
135 {
136 mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
137 goto exit;
138 }
139
140 ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
141 if( ret != 0 )
142 {
143 mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
144 goto exit;
145 }
146
147 mbedtls_printf( " ok\n" );
148
149 /*
150 * Server: read peer's key and generate shared secret
151 */
152 mbedtls_printf( " . Server reading client key and computing secret..." );
153 fflush( stdout );
154
155 ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
156 if( ret != 0 )
157 {
158 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
159 goto exit;
160 }
161
162 ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
163 if( ret != 0 )
164 {
165 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
166 goto exit;
167 }
168
169 ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
170 &ctx_srv.Qp, &ctx_srv.d,
171 mbedtls_ctr_drbg_random, &ctr_drbg );
172 if( ret != 0 )
173 {
174 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
175 goto exit;
176 }
177
178 mbedtls_printf( " ok\n" );
179
180 /*
181 * Client: read peer's key and generate shared secret
182 */
183 mbedtls_printf( " . Client reading server key and computing secret..." );
184 fflush( stdout );
185
186 ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
187 if( ret != 0 )
188 {
189 mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
190 goto exit;
191 }
192
193 ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
194 if( ret != 0 )
195 {
196 mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
197 goto exit;
198 }
199
200 ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
201 &ctx_cli.Qp, &ctx_cli.d,
202 mbedtls_ctr_drbg_random, &ctr_drbg );
203 if( ret != 0 )
204 {
205 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
206 goto exit;
207 }
208
209 mbedtls_printf( " ok\n" );
210
211 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300212 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100213 */
214 mbedtls_printf( " . Checking if both computed secrets are equal..." );
215 fflush( stdout );
216
217 ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
218 if( ret != 0 )
219 {
220 mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
221 goto exit;
222 }
223
224 mbedtls_printf( " ok\n" );
225
Andres Amaya Garcia81982c82018-04-29 22:16:23 +0100226 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100227
228exit:
229
230#if defined(_WIN32)
231 mbedtls_printf( " + Press Enter to exit this program.\n" );
232 fflush( stdout ); getchar();
233#endif
234
235 mbedtls_ecdh_free( &ctx_srv );
236 mbedtls_ecdh_free( &ctx_cli );
237 mbedtls_ctr_drbg_free( &ctr_drbg );
238 mbedtls_entropy_free( &entropy );
239
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200240 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100241}
242#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
243 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */