blob: 50c4ad85e20c4e449554d2ad51a8a7ec03c764c0 [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
Thomas Daubney70c00882022-05-20 18:43:09 +010063 size_t srv_olen;
64 size_t cli_olen;
Thomas Daubney306a8902022-05-18 14:22:08 +010065 unsigned char secret_cli[32] = { 0 };
66 unsigned char secret_srv[32] = { 0 };
Thomas Daubney88fed8e2022-04-12 09:03:22 +010067 const unsigned char *p_cli_to_srv = cli_to_srv;
68
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010069 ((void) argc);
70 ((void) argv);
71
72 mbedtls_ecdh_init( &ctx_cli );
73 mbedtls_ecdh_init( &ctx_srv );
74 mbedtls_ctr_drbg_init( &ctr_drbg );
75
76 /*
77 * Initialize random number generation
78 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010079 mbedtls_printf( " . Seed the random number generator..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010080 fflush( stdout );
81
82 mbedtls_entropy_init( &entropy );
Thomas Daubney88fed8e2022-04-12 09:03:22 +010083 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Thomas Daubneyec2ec422022-05-18 10:23:20 +010084 &entropy,
85 (const unsigned char *) pers,
86 sizeof pers ) ) != 0 )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010087 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +010088 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
89 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010090 goto exit;
91 }
92
93 mbedtls_printf( " ok\n" );
94
95 /*
HowJMayccbd6222020-07-29 16:59:19 +080096 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010097 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010098 mbedtls_printf( " . Set up client context, generate EC key pair..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010099 fflush( stdout );
100
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100101 ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100102 if( ret != 0 )
103 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100104 mbedtls_printf( " failed\n ! mbedtls_ecdh_setup returned %d\n", ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100105 goto exit;
106 }
107
Thomas Daubney70c00882022-05-20 18:43:09 +0100108 ret = mbedtls_ecdh_make_params( &ctx_cli, &cli_olen, cli_to_srv,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100109 sizeof( cli_to_srv ),
110 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100111 if( ret != 0 )
112 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100113 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n",
114 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100115 goto exit;
116 }
117
118 mbedtls_printf( " ok\n" );
119
120 /*
121 * Server: initialize context and generate keypair
122 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100123 mbedtls_printf( " . Server: read params, generate public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100124 fflush( stdout );
125
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100126 ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv,
127 p_cli_to_srv + sizeof( cli_to_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100128 if( ret != 0 )
129 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100130 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n",
131 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100132 goto exit;
133 }
134
Thomas Daubney70c00882022-05-20 18:43:09 +0100135 ret = mbedtls_ecdh_make_public( &ctx_srv, &srv_olen, srv_to_cli,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100136 sizeof( srv_to_cli ),
137 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100138 if( ret != 0 )
139 {
Thomas Daubney64042b82022-05-18 09:59:55 +0100140 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_public returned %d\n",
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100141 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100142 goto exit;
143 }
144
145 mbedtls_printf( " ok\n" );
146
147 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100148 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100149 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100150 mbedtls_printf( " . Client: read public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100151 fflush( stdout );
152
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100153 ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli,
154 sizeof( srv_to_cli ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100155 if( ret != 0 )
156 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100157 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n",
158 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100159 goto exit;
160 }
161
162 mbedtls_printf( " ok\n" );
163
164 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100165 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100166 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100167 mbedtls_printf( " . Calculate secrets..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100168 fflush( stdout );
169
Thomas Daubney70c00882022-05-20 18:43:09 +0100170 ret = mbedtls_ecdh_calc_secret( &ctx_cli, &cli_olen, secret_cli,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100171 sizeof( secret_cli ),
172 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100173 if( ret != 0 )
174 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100175 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
176 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100177 goto exit;
178 }
179
Thomas Daubney70c00882022-05-20 18:43:09 +0100180 ret = mbedtls_ecdh_calc_secret( &ctx_srv, &srv_olen, secret_srv,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100181 sizeof( secret_srv ),
182 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100183 if( ret != 0 )
184 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100185 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
186 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100187 goto exit;
188 }
189
190 mbedtls_printf( " ok\n" );
191
192 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300193 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100194 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100195 mbedtls_printf( " . Check if both calculated secrets are equal..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100196 fflush( stdout );
197
Thomas Daubney70c00882022-05-20 18:43:09 +0100198 ret = memcmp( secret_srv, secret_cli, sizeof( srv_olen ) );
199 if( ret != 0 || ( cli_olen != srv_olen ) )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100200 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100201 mbedtls_printf( " failed\n ! Shared secrets not equal.\n" );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100202 goto exit;
203 }
204
205 mbedtls_printf( " ok\n" );
206
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100207 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100208
209exit:
210
211#if defined(_WIN32)
212 mbedtls_printf( " + Press Enter to exit this program.\n" );
213 fflush( stdout ); getchar();
214#endif
215
216 mbedtls_ecdh_free( &ctx_srv );
217 mbedtls_ecdh_free( &ctx_cli );
218 mbedtls_ctr_drbg_free( &ctr_drbg );
219 mbedtls_entropy_free( &entropy );
220
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200221 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100222}
223#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
224 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */