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