blob: e435b03501834a811b2abb1096bd25999aa81031 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010021
22#if defined(MBEDTLS_PLATFORM_C)
23#include "mbedtls/platform.h"
24#else
25#include <stdio.h>
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010026#include <stdlib.h>
27#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010029#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010030#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
31#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010032
Thomas Daubney88fed8e2022-04-12 09:03:22 +010033#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010034 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
36int main( void )
37{
Thomas Daubney88fed8e2022-04-12 09:03:22 +010038 mbedtls_printf( "MBEDTLS_ECDH_C and/or "
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010039 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
40 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
41 "not defined\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020042 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010043}
44#else
45
46#include "mbedtls/entropy.h"
47#include "mbedtls/ctr_drbg.h"
48#include "mbedtls/ecdh.h"
49
Thomas Daubney88fed8e2022-04-12 09:03:22 +010050#include <string.h>
51
Simon Butcher63cb97e2018-12-06 17:43:31 +000052
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010053int main( int argc, char *argv[] )
54{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010055 int ret = 1;
56 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010057 mbedtls_ecdh_context ctx_cli, ctx_srv;
58 mbedtls_entropy_context entropy;
59 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubney88fed8e2022-04-12 09:03:22 +010060 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010061 const char pers[] = "ecdh";
Thomas Daubney88fed8e2022-04-12 09:03:22 +010062
63 size_t olen;
64 unsigned char secret_cli[32], secret_srv[32];
65 const unsigned char *p_cli_to_srv = cli_to_srv;
66
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010067 ((void) argc);
68 ((void) argv);
69
70 mbedtls_ecdh_init( &ctx_cli );
71 mbedtls_ecdh_init( &ctx_srv );
72 mbedtls_ctr_drbg_init( &ctr_drbg );
73
74 /*
75 * Initialize random number generation
76 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010077 mbedtls_printf( " . Seed the random number generator..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010078 fflush( stdout );
79
80 mbedtls_entropy_init( &entropy );
Thomas Daubney88fed8e2022-04-12 09:03:22 +010081 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
82 &entropy,
83 (const unsigned char *) pers,
84 sizeof pers ) ) != 0 )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010085 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +010086 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
87 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010088 goto exit;
89 }
90
91 mbedtls_printf( " ok\n" );
92
93 /*
HowJMayccbd6222020-07-29 16:59:19 +080094 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010095 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010096 mbedtls_printf( " . Set up client context, generate EC key pair..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010097 fflush( stdout );
98
Thomas Daubney88fed8e2022-04-12 09:03:22 +010099 ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100100 if( ret != 0 )
101 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100102 mbedtls_printf( " failed\n ! mbedtls_ecdh_setup returned %d\n", ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100103 goto exit;
104 }
105
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100106 ret = mbedtls_ecdh_make_params( &ctx_cli, &olen, cli_to_srv,
107 sizeof( cli_to_srv ),
108 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100109 if( ret != 0 )
110 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100111 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n",
112 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100113 goto exit;
114 }
115
116 mbedtls_printf( " ok\n" );
117
118 /*
119 * Server: initialize context and generate keypair
120 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100121 mbedtls_printf( " . Server: read params, generate public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100122 fflush( stdout );
123
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100124 ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv,
125 p_cli_to_srv + sizeof( cli_to_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100126 if( ret != 0 )
127 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100128 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n",
129 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100130 goto exit;
131 }
132
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100133 ret = mbedtls_ecdh_make_public( &ctx_srv, &olen, srv_to_cli,
134 sizeof( srv_to_cli ),
135 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100136 if( ret != 0 )
137 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100138 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n",
139 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100140 goto exit;
141 }
142
143 mbedtls_printf( " ok\n" );
144
145 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100146 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100147 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100148 mbedtls_printf( " . Client: read public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100149 fflush( stdout );
150
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100151 ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli,
152 sizeof( srv_to_cli ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100153 if( ret != 0 )
154 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100155 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n",
156 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100157 goto exit;
158 }
159
160 mbedtls_printf( " ok\n" );
161
162 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100163 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100164 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100165 mbedtls_printf( " . Calculate secrets..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100166 fflush( stdout );
167
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100168 ret = mbedtls_ecdh_calc_secret( &ctx_cli, &olen, secret_cli,
169 sizeof( secret_cli ),
170 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100171 if( ret != 0 )
172 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100173 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
174 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100175 goto exit;
176 }
177
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100178 ret = mbedtls_ecdh_calc_secret( &ctx_srv, &olen, secret_srv,
179 sizeof( secret_srv ),
180 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100181 if( ret != 0 )
182 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100183 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
184 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100185 goto exit;
186 }
187
188 mbedtls_printf( " ok\n" );
189
190 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300191 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100192 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100193 mbedtls_printf( " . Check if both calculated secrets are equal..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100194 fflush( stdout );
195
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100196 ret = memcmp( secret_srv, secret_cli, sizeof( secret_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100197 if( ret != 0 )
198 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100199 mbedtls_printf( " failed\n ! Shared secrets not equal.\n" );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100200 goto exit;
201 }
202
203 mbedtls_printf( " ok\n" );
204
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100205 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100206
207exit:
208
209#if defined(_WIN32)
210 mbedtls_printf( " + Press Enter to exit this program.\n" );
211 fflush( stdout ); getchar();
212#endif
213
214 mbedtls_ecdh_free( &ctx_srv );
215 mbedtls_ecdh_free( &ctx_cli );
216 mbedtls_ctr_drbg_free( &ctr_drbg );
217 mbedtls_entropy_free( &entropy );
218
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200219 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100220}
221#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
222 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */