blob: 555a2a0bb74ab0079034267d3bfcb82d3b73c8e3 [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;
Thomas Daubney306a8902022-05-18 14:22:08 +010064 unsigned char secret_cli[32] = { 0 };
65 unsigned char secret_srv[32] = { 0 };
Thomas Daubney88fed8e2022-04-12 09:03:22 +010066 const unsigned char *p_cli_to_srv = cli_to_srv;
67
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010068 ((void) argc);
69 ((void) argv);
70
71 mbedtls_ecdh_init( &ctx_cli );
72 mbedtls_ecdh_init( &ctx_srv );
73 mbedtls_ctr_drbg_init( &ctr_drbg );
74
75 /*
76 * Initialize random number generation
77 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010078 mbedtls_printf( " . Seed the random number generator..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010079 fflush( stdout );
80
81 mbedtls_entropy_init( &entropy );
Thomas Daubney88fed8e2022-04-12 09:03:22 +010082 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Thomas Daubneyec2ec422022-05-18 10:23:20 +010083 &entropy,
84 (const unsigned char *) pers,
85 sizeof pers ) ) != 0 )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010086 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +010087 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
88 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010089 goto exit;
90 }
91
92 mbedtls_printf( " ok\n" );
93
94 /*
HowJMayccbd6222020-07-29 16:59:19 +080095 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010096 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010097 mbedtls_printf( " . Set up client context, generate EC key pair..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010098 fflush( stdout );
99
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100100 ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100101 if( ret != 0 )
102 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100103 mbedtls_printf( " failed\n ! mbedtls_ecdh_setup returned %d\n", ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100104 goto exit;
105 }
106
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100107 ret = mbedtls_ecdh_make_params( &ctx_cli, &olen, cli_to_srv,
108 sizeof( cli_to_srv ),
109 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100110 if( ret != 0 )
111 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100112 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n",
113 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100114 goto exit;
115 }
116
117 mbedtls_printf( " ok\n" );
118
119 /*
120 * Server: initialize context and generate keypair
121 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100122 mbedtls_printf( " . Server: read params, generate public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100123 fflush( stdout );
124
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100125 ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv,
126 p_cli_to_srv + sizeof( cli_to_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100127 if( ret != 0 )
128 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100129 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n",
130 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100131 goto exit;
132 }
133
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100134 ret = mbedtls_ecdh_make_public( &ctx_srv, &olen, srv_to_cli,
135 sizeof( srv_to_cli ),
136 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100137 if( ret != 0 )
138 {
Thomas Daubney64042b82022-05-18 09:59:55 +0100139 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_public returned %d\n",
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100140 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100141 goto exit;
142 }
143
144 mbedtls_printf( " ok\n" );
145
146 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100147 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100148 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100149 mbedtls_printf( " . Client: read public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100150 fflush( stdout );
151
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100152 ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli,
153 sizeof( srv_to_cli ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100154 if( ret != 0 )
155 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100156 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n",
157 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100158 goto exit;
159 }
160
161 mbedtls_printf( " ok\n" );
162
163 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100164 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100165 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100166 mbedtls_printf( " . Calculate secrets..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100167 fflush( stdout );
168
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100169 ret = mbedtls_ecdh_calc_secret( &ctx_cli, &olen, secret_cli,
170 sizeof( secret_cli ),
171 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100172 if( ret != 0 )
173 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100174 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
175 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100176 goto exit;
177 }
178
Thomas Daubney306a8902022-05-18 14:22:08 +0100179 size_t secret_cli_olen = olen;
180
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100181 ret = mbedtls_ecdh_calc_secret( &ctx_srv, &olen, secret_srv,
182 sizeof( secret_srv ),
183 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100184 if( ret != 0 )
185 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100186 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
187 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100188 goto exit;
189 }
190
Thomas Daubney306a8902022-05-18 14:22:08 +0100191 size_t secret_srv_olen = olen;
192
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100193 mbedtls_printf( " ok\n" );
194
195 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300196 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100197 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100198 mbedtls_printf( " . Check if both calculated secrets are equal..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100199 fflush( stdout );
200
Thomas Daubney306a8902022-05-18 14:22:08 +0100201 ret = memcmp( secret_srv, secret_cli, sizeof( secret_srv_olen ) );
202 if( ret != 0 || ( secret_cli_olen != secret_srv_olen ) )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100203 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100204 mbedtls_printf( " failed\n ! Shared secrets not equal.\n" );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100205 goto exit;
206 }
207
208 mbedtls_printf( " ok\n" );
209
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100210 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100211
212exit:
213
214#if defined(_WIN32)
215 mbedtls_printf( " + Press Enter to exit this program.\n" );
216 fflush( stdout ); getchar();
217#endif
218
219 mbedtls_ecdh_free( &ctx_srv );
220 mbedtls_ecdh_free( &ctx_cli );
221 mbedtls_ctr_drbg_free( &ctr_drbg );
222 mbedtls_entropy_free( &entropy );
223
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200224 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100225}
226#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
227 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */