blob: 65b206a247690d9e3b0061e3e117d5227a74a1d1 [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
26#if defined(MBEDTLS_PLATFORM_C)
27#include "mbedtls/platform.h"
28#else
29#include <stdio.h>
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010030#include <stdlib.h>
31#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010032#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010033#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010034#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
35#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010036
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010037#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010038 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
39 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
40int main( void )
41{
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010042 mbedtls_printf( "MBEDTLS_ECDH_C and/or "
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010043 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
44 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
45 "not defined\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020046 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010047}
48#else
49
50#include "mbedtls/entropy.h"
51#include "mbedtls/ctr_drbg.h"
52#include "mbedtls/ecdh.h"
53
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010054#include <string.h>
55
Simon Butcher63cb97e2018-12-06 17:43:31 +000056
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010057int main( int argc, char *argv[] )
58{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010059 int ret = 1;
60 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010061 mbedtls_ecdh_context ctx_cli, ctx_srv;
62 mbedtls_entropy_context entropy;
63 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010064 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010065 const char pers[] = "ecdh";
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010066
67 size_t srv_olen;
68 size_t cli_olen;
69 unsigned char secret_cli[32] = { 0 };
70 unsigned char secret_srv[32] = { 0 };
71 const unsigned char *p_cli_to_srv = cli_to_srv;
72
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010073 ((void) argc);
74 ((void) argv);
75
76 mbedtls_ecdh_init( &ctx_cli );
77 mbedtls_ecdh_init( &ctx_srv );
78 mbedtls_ctr_drbg_init( &ctr_drbg );
79
80 /*
81 * Initialize random number generation
82 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010083 mbedtls_printf( " . Seed the random number generator..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010084 fflush( stdout );
85
86 mbedtls_entropy_init( &entropy );
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010087 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
88 &entropy,
89 (const unsigned char *) pers,
90 sizeof pers ) ) != 0 )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010091 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +010092 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
93 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010094 goto exit;
95 }
96
97 mbedtls_printf( " ok\n" );
98
99 /*
HowJMayccbd6222020-07-29 16:59:19 +0800100 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100101 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100102 mbedtls_printf( " . Set up client context, generate EC key pair..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100103 fflush( stdout );
104
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100105 ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 );
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_setup returned %d\n", ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100109 goto exit;
110 }
111
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100112 ret = mbedtls_ecdh_make_params( &ctx_cli, &cli_olen, cli_to_srv,
113 sizeof( cli_to_srv ),
114 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100115 if( ret != 0 )
116 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100117 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_params returned %d\n",
118 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100119 goto exit;
120 }
121
122 mbedtls_printf( " ok\n" );
123
124 /*
125 * Server: initialize context and generate keypair
126 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100127 mbedtls_printf( " . Server: read params, generate public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100128 fflush( stdout );
129
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100130 ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv,
131 p_cli_to_srv + sizeof( cli_to_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100132 if( ret != 0 )
133 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100134 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n",
135 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100136 goto exit;
137 }
138
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100139 ret = mbedtls_ecdh_make_public( &ctx_srv, &srv_olen, srv_to_cli,
140 sizeof( srv_to_cli ),
141 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100142 if( ret != 0 )
143 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100144 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_public returned %d\n",
145 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100146 goto exit;
147 }
148
149 mbedtls_printf( " ok\n" );
150
151 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100152 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100153 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100154 mbedtls_printf( " . Client: read public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100155 fflush( stdout );
156
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100157 ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli,
158 sizeof( srv_to_cli ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100159 if( ret != 0 )
160 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100161 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n",
162 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100163 goto exit;
164 }
165
166 mbedtls_printf( " ok\n" );
167
168 /*
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100169 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100170 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100171 mbedtls_printf( " . Calculate secrets..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100172 fflush( stdout );
173
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100174 ret = mbedtls_ecdh_calc_secret( &ctx_cli, &cli_olen, secret_cli,
175 sizeof( secret_cli ),
176 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100177 if( ret != 0 )
178 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100179 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
180 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100181 goto exit;
182 }
183
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100184 ret = mbedtls_ecdh_calc_secret( &ctx_srv, &srv_olen, secret_srv,
185 sizeof( secret_srv ),
186 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100187 if( ret != 0 )
188 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100189 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
190 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100191 goto exit;
192 }
193
194 mbedtls_printf( " ok\n" );
195
196 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300197 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100198 */
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100199 mbedtls_printf( " . Check if both calculated secrets are equal..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100200 fflush( stdout );
201
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100202 ret = memcmp( secret_srv, secret_cli, srv_olen );
203 if( ret != 0 || ( cli_olen != srv_olen ) )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100204 {
Thomas Daubneyd99f8b22022-05-18 15:13:31 +0100205 mbedtls_printf( " failed\n ! Shared secrets not equal.\n" );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100206 goto exit;
207 }
208
209 mbedtls_printf( " ok\n" );
210
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100211 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100212
213exit:
214
215#if defined(_WIN32)
216 mbedtls_printf( " + Press Enter to exit this program.\n" );
217 fflush( stdout ); getchar();
218#endif
219
220 mbedtls_ecdh_free( &ctx_srv );
221 mbedtls_ecdh_free( &ctx_cli );
222 mbedtls_ctr_drbg_free( &ctr_drbg );
223 mbedtls_entropy_free( &entropy );
224
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200225 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100226}
227#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
228 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */