blob: 5dd6bddb40bc83620bbe3a7f549a06f9d43eb116 [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
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010022#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010023
Thomas Daubney88fed8e2022-04-12 09:03:22 +010024#if !defined(MBEDTLS_ECDH_C) || \
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010025 !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
27int main( void )
28{
Thomas Daubney88fed8e2022-04-12 09:03:22 +010029 mbedtls_printf( "MBEDTLS_ECDH_C and/or "
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010030 "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
31 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
32 "not defined\n" );
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +020033 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010034}
35#else
36
37#include "mbedtls/entropy.h"
38#include "mbedtls/ctr_drbg.h"
39#include "mbedtls/ecdh.h"
40
Thomas Daubney88fed8e2022-04-12 09:03:22 +010041#include <string.h>
42
Simon Butcher63cb97e2018-12-06 17:43:31 +000043
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010044int main( int argc, char *argv[] )
45{
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +010046 int ret = 1;
47 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010048 mbedtls_ecdh_context ctx_cli, ctx_srv;
49 mbedtls_entropy_context entropy;
50 mbedtls_ctr_drbg_context ctr_drbg;
Thomas Daubney88fed8e2022-04-12 09:03:22 +010051 unsigned char cli_to_srv[36], srv_to_cli[33];
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010052 const char pers[] = "ecdh";
Thomas Daubney88fed8e2022-04-12 09:03:22 +010053
Thomas Daubney70c00882022-05-20 18:43:09 +010054 size_t srv_olen;
55 size_t cli_olen;
Thomas Daubney306a8902022-05-18 14:22:08 +010056 unsigned char secret_cli[32] = { 0 };
57 unsigned char secret_srv[32] = { 0 };
Thomas Daubney88fed8e2022-04-12 09:03:22 +010058 const unsigned char *p_cli_to_srv = cli_to_srv;
59
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010060 ((void) argc);
61 ((void) argv);
62
63 mbedtls_ecdh_init( &ctx_cli );
64 mbedtls_ecdh_init( &ctx_srv );
65 mbedtls_ctr_drbg_init( &ctr_drbg );
66
67 /*
68 * Initialize random number generation
69 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010070 mbedtls_printf( " . Seed the random number generator..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010071 fflush( stdout );
72
73 mbedtls_entropy_init( &entropy );
Thomas Daubney88fed8e2022-04-12 09:03:22 +010074 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
Thomas Daubneyec2ec422022-05-18 10:23:20 +010075 &entropy,
76 (const unsigned char *) pers,
77 sizeof pers ) ) != 0 )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010078 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +010079 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
80 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010081 goto exit;
82 }
83
84 mbedtls_printf( " ok\n" );
85
86 /*
HowJMayccbd6222020-07-29 16:59:19 +080087 * Client: initialize context and generate keypair
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010088 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +010089 mbedtls_printf( " . Set up client context, generate EC key pair..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010090 fflush( stdout );
91
Thomas Daubney88fed8e2022-04-12 09:03:22 +010092 ret = mbedtls_ecdh_setup( &ctx_cli, MBEDTLS_ECP_DP_CURVE25519 );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010093 if( ret != 0 )
94 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +010095 mbedtls_printf( " failed\n ! mbedtls_ecdh_setup returned %d\n", ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +010096 goto exit;
97 }
98
Thomas Daubney70c00882022-05-20 18:43:09 +010099 ret = mbedtls_ecdh_make_params( &ctx_cli, &cli_olen, cli_to_srv,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100100 sizeof( cli_to_srv ),
101 mbedtls_ctr_drbg_random, &ctr_drbg );
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_make_params returned %d\n",
105 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100106 goto exit;
107 }
108
109 mbedtls_printf( " ok\n" );
110
111 /*
112 * Server: initialize context and generate keypair
113 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100114 mbedtls_printf( " . Server: read params, generate public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100115 fflush( stdout );
116
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100117 ret = mbedtls_ecdh_read_params( &ctx_srv, &p_cli_to_srv,
118 p_cli_to_srv + sizeof( cli_to_srv ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100119 if( ret != 0 )
120 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100121 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_params returned %d\n",
122 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100123 goto exit;
124 }
125
Thomas Daubney70c00882022-05-20 18:43:09 +0100126 ret = mbedtls_ecdh_make_public( &ctx_srv, &srv_olen, srv_to_cli,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100127 sizeof( srv_to_cli ),
128 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100129 if( ret != 0 )
130 {
Thomas Daubney64042b82022-05-18 09:59:55 +0100131 mbedtls_printf( " failed\n ! mbedtls_ecdh_make_public returned %d\n",
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100132 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100133 goto exit;
134 }
135
136 mbedtls_printf( " ok\n" );
137
138 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100139 * Client: read public key
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100140 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100141 mbedtls_printf( " . Client: read public key..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100142 fflush( stdout );
143
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100144 ret = mbedtls_ecdh_read_public( &ctx_cli, srv_to_cli,
145 sizeof( srv_to_cli ) );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100146 if( ret != 0 )
147 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100148 mbedtls_printf( " failed\n ! mbedtls_ecdh_read_public returned %d\n",
149 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100150 goto exit;
151 }
152
153 mbedtls_printf( " ok\n" );
154
155 /*
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100156 * Calculate secrets
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100157 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100158 mbedtls_printf( " . Calculate secrets..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100159 fflush( stdout );
160
Thomas Daubney70c00882022-05-20 18:43:09 +0100161 ret = mbedtls_ecdh_calc_secret( &ctx_cli, &cli_olen, secret_cli,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100162 sizeof( secret_cli ),
163 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100164 if( ret != 0 )
165 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100166 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
167 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100168 goto exit;
169 }
170
Thomas Daubney70c00882022-05-20 18:43:09 +0100171 ret = mbedtls_ecdh_calc_secret( &ctx_srv, &srv_olen, secret_srv,
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100172 sizeof( secret_srv ),
173 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100174 if( ret != 0 )
175 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100176 mbedtls_printf( " failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
177 ret );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100178 goto exit;
179 }
180
181 mbedtls_printf( " ok\n" );
182
183 /*
Ron Eldor2a47be52017-06-20 15:23:23 +0300184 * Verification: are the computed secrets equal?
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100185 */
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100186 mbedtls_printf( " . Check if both calculated secrets are equal..." );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100187 fflush( stdout );
188
Thomas Daubney413550c2022-05-23 16:11:31 +0100189 ret = memcmp( secret_srv, secret_cli, srv_olen );
Thomas Daubney70c00882022-05-20 18:43:09 +0100190 if( ret != 0 || ( cli_olen != srv_olen ) )
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100191 {
Thomas Daubney88fed8e2022-04-12 09:03:22 +0100192 mbedtls_printf( " failed\n ! Shared secrets not equal.\n" );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100193 goto exit;
194 }
195
196 mbedtls_printf( " ok\n" );
197
Andres Amaya Garciaf47c9c12018-04-29 22:16:23 +0100198 exit_code = MBEDTLS_EXIT_SUCCESS;
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100199
200exit:
201
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100202 mbedtls_ecdh_free( &ctx_srv );
203 mbedtls_ecdh_free( &ctx_cli );
204 mbedtls_ctr_drbg_free( &ctr_drbg );
205 mbedtls_entropy_free( &entropy );
206
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200207 mbedtls_exit( exit_code );
Manuel Pégourié-Gonnard3eb8c342015-10-09 12:11:14 +0100208}
209#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
210 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */