blob: 69ff897a860a4ba614dff010c36c43226002f274 [file] [log] [blame]
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01001/*
2 * Example ECDHE with Curve25519 program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +01005 * 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.
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010018 */
19
20#if !defined(MBEDTLS_CONFIG_FILE)
21#include "mbedtls/config.h"
22#else
23#include MBEDTLS_CONFIG_FILE
24#endif
25
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010026#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010027
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010028#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010029 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
30 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
31int main( void )
32{
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010033 mbedtls_printf( "MBEDTLS_ECDH_C and/or "
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010034 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
35 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
36 "not defined\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020037 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010038}
39#else
40
41#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
43#include "mbedtls/ecdh.h"
44
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010045#include <string.h>
46
Simon Butcher63cb97e2018-12-06 17:43:31 +000047
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010048int main( int argc, char *argv[] )
49{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010050 int ret = 1;
51 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010052 mbedtls_ecdh_context ctx_cli, ctx_srv;
53 mbedtls_entropy_context entropy;
54 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010055 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010056 const char pers[] = "ecdh";
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010057
58 size_t srv_olen;
59 size_t cli_olen;
60 unsigned char secret_cli[32] = { 0 };
61 unsigned char secret_srv[32] = { 0 };
62 const unsigned char *p_cli_to_srv = cli_to_srv;
63
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010064 ((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 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010074 mbedtls_printf( " . Seed the random number generator..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010075 fflush( stdout );
76
77 mbedtls_entropy_init( &entropy );
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010078 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
79 &entropy,
80 (const unsigned char *) pers,
81 sizeof pers ) ) != 0 )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010082 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010083 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
84 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010085 goto exit;
86 }
87
88 mbedtls_printf( " ok\n" );
89
90 /*
HowJMayccbd6222020-07-29 16:59:19 +080091 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010092 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010093 mbedtls_printf( " . Set up client context, generate EC key pair..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010094 fflush( stdout );
95
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010096 ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010097 if( ret != 0 )
98 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010099 mbedtls_printf( " failed\n ! mbedtls_ecdh_setup returned %d\n", ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100100 goto exit;
101 }
102
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100103 ret = mbedtls_ecdh_make_params( &ctx_cli, &cli_olen, cli_to_srv,
104 sizeof( cli_to_srv ),
105 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100106 if( ret != 0 )
107 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100108 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n",
109 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100110 goto exit;
111 }
112
113 mbedtls_printf( " ok\n" );
114
115 /*
116 * Server: initialize context and generate keypair
117 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100118 mbedtls_printf( " . Server: read params, generate public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100119 fflush( stdout );
120
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100121 ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv,
122 p_cli_to_srv + sizeof( cli_to_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100123 if( ret != 0 )
124 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100125 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n",
126 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100127 goto exit;
128 }
129
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100130 ret = mbedtls_ecdh_make_public( &ctx_srv, &srv_olen, srv_to_cli,
131 sizeof( srv_to_cli ),
132 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100133 if( ret != 0 )
134 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100135 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_public returned %d\n",
136 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100137 goto exit;
138 }
139
140 mbedtls_printf( " ok\n" );
141
142 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100143 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100144 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100145 mbedtls_printf( " . Client: read public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100146 fflush( stdout );
147
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100148 ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli,
149 sizeof( srv_to_cli ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100150 if( ret != 0 )
151 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100152 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n",
153 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100154 goto exit;
155 }
156
157 mbedtls_printf( " ok\n" );
158
159 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100160 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100161 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100162 mbedtls_printf( " . Calculate secrets..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100163 fflush( stdout );
164
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100165 ret = mbedtls_ecdh_calc_secret( &ctx_cli, &cli_olen, secret_cli,
166 sizeof( secret_cli ),
167 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100168 if( ret != 0 )
169 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100170 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
171 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100172 goto exit;
173 }
174
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100175 ret = mbedtls_ecdh_calc_secret( &ctx_srv, &srv_olen, secret_srv,
176 sizeof( secret_srv ),
177 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100178 if( ret != 0 )
179 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100180 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
181 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100182 goto exit;
183 }
184
185 mbedtls_printf( " ok\n" );
186
187 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300188 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100189 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100190 mbedtls_printf( " . Check if both calculated secrets are equal..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100191 fflush( stdout );
192
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100193 ret = memcmp( secret_srv, secret_cli, srv_olen );
194 if( ret != 0 || ( cli_olen != srv_olen ) )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100195 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100196 mbedtls_printf( " failed\n ! Shared secrets not equal.\n" );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100197 goto exit;
198 }
199
200 mbedtls_printf( " ok\n" );
201
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100202 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100203
204exit:
205
206#if defined(_WIN32)
207 mbedtls_printf( " + Press Enter to exit this program.\n" );
208 fflush( stdout ); getchar();
209#endif
210
211 mbedtls_ecdh_free( &ctx_srv );
212 mbedtls_ecdh_free( &ctx_cli );
213 mbedtls_ctr_drbg_free( &ctr_drbg );
214 mbedtls_entropy_free( &entropy );
215
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200216 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100217}
218#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
219 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */