• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Example ECDHE with Curve25519 program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52 
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_printf          printf
59 #define mbedtls_exit            exit
60 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
61 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
62 #endif /* MBEDTLS_PLATFORM_C */
63 
64 #if !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) || \
65     !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
66     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)67 int main( void )
68 {
69     mbedtls_printf( "MBEDTLS_ECDH_C and/or MBEDTLS_ECDH_LEGACY_CONTEXT and/or "
70                     "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
71                     "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
72                     "not defined\n" );
73     mbedtls_exit( 0 );
74 }
75 #else
76 
77 #include "mbedtls/entropy.h"
78 #include "mbedtls/ctr_drbg.h"
79 #include "mbedtls/ecdh.h"
80 
81 
main(int argc,char * argv[])82 int main( int argc, char *argv[] )
83 {
84     int ret = 1;
85     int exit_code = MBEDTLS_EXIT_FAILURE;
86     mbedtls_ecdh_context ctx_cli, ctx_srv;
87     mbedtls_entropy_context entropy;
88     mbedtls_ctr_drbg_context ctr_drbg;
89     unsigned char cli_to_srv[32], srv_to_cli[32];
90     const char pers[] = "ecdh";
91     ((void) argc);
92     ((void) argv);
93 
94     mbedtls_ecdh_init( &ctx_cli );
95     mbedtls_ecdh_init( &ctx_srv );
96     mbedtls_ctr_drbg_init( &ctr_drbg );
97 
98     /*
99      * Initialize random number generation
100      */
101     mbedtls_printf( "  . Seeding the random number generator..." );
102     fflush( stdout );
103 
104     mbedtls_entropy_init( &entropy );
105     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
106                                (const unsigned char *) pers,
107                                sizeof pers ) ) != 0 )
108     {
109         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
110         goto exit;
111     }
112 
113     mbedtls_printf( " ok\n" );
114 
115     /*
116      * Client: inialize context and generate keypair
117      */
118     mbedtls_printf( "  . Setting up client context..." );
119     fflush( stdout );
120 
121     ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
122     if( ret != 0 )
123     {
124         mbedtls_printf( " failed\n  ! mbedtls_ecp_group_load returned %d\n", ret );
125         goto exit;
126     }
127 
128     ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
129                                    mbedtls_ctr_drbg_random, &ctr_drbg );
130     if( ret != 0 )
131     {
132         mbedtls_printf( " failed\n  ! mbedtls_ecdh_gen_public returned %d\n", ret );
133         goto exit;
134     }
135 
136     ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
137     if( ret != 0 )
138     {
139         mbedtls_printf( " failed\n  ! mbedtls_mpi_write_binary returned %d\n", ret );
140         goto exit;
141     }
142 
143     mbedtls_printf( " ok\n" );
144 
145     /*
146      * Server: initialize context and generate keypair
147      */
148     mbedtls_printf( "  . Setting up server context..." );
149     fflush( stdout );
150 
151     ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
152     if( ret != 0 )
153     {
154         mbedtls_printf( " failed\n  ! mbedtls_ecp_group_load returned %d\n", ret );
155         goto exit;
156     }
157 
158     ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
159                                    mbedtls_ctr_drbg_random, &ctr_drbg );
160     if( ret != 0 )
161     {
162         mbedtls_printf( " failed\n  ! mbedtls_ecdh_gen_public returned %d\n", ret );
163         goto exit;
164     }
165 
166     ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
167     if( ret != 0 )
168     {
169         mbedtls_printf( " failed\n  ! mbedtls_mpi_write_binary returned %d\n", ret );
170         goto exit;
171     }
172 
173     mbedtls_printf( " ok\n" );
174 
175     /*
176      * Server: read peer's key and generate shared secret
177      */
178     mbedtls_printf( "  . Server reading client key and computing secret..." );
179     fflush( stdout );
180 
181     ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
182     if( ret != 0 )
183     {
184         mbedtls_printf( " failed\n  ! mbedtls_mpi_lset returned %d\n", ret );
185         goto exit;
186     }
187 
188     ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
189     if( ret != 0 )
190     {
191         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_binary returned %d\n", ret );
192         goto exit;
193     }
194 
195     ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
196                                        &ctx_srv.Qp, &ctx_srv.d,
197                                        mbedtls_ctr_drbg_random, &ctr_drbg );
198     if( ret != 0 )
199     {
200         mbedtls_printf( " failed\n  ! mbedtls_ecdh_compute_shared returned %d\n", ret );
201         goto exit;
202     }
203 
204     mbedtls_printf( " ok\n" );
205 
206     /*
207      * Client: read peer's key and generate shared secret
208      */
209     mbedtls_printf( "  . Client reading server key and computing secret..." );
210     fflush( stdout );
211 
212     ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
213     if( ret != 0 )
214     {
215         mbedtls_printf( " failed\n  ! mbedtls_mpi_lset returned %d\n", ret );
216         goto exit;
217     }
218 
219     ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
220     if( ret != 0 )
221     {
222         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_binary returned %d\n", ret );
223         goto exit;
224     }
225 
226     ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
227                                        &ctx_cli.Qp, &ctx_cli.d,
228                                        mbedtls_ctr_drbg_random, &ctr_drbg );
229     if( ret != 0 )
230     {
231         mbedtls_printf( " failed\n  ! mbedtls_ecdh_compute_shared returned %d\n", ret );
232         goto exit;
233     }
234 
235     mbedtls_printf( " ok\n" );
236 
237     /*
238      * Verification: are the computed secrets equal?
239      */
240     mbedtls_printf( "  . Checking if both computed secrets are equal..." );
241     fflush( stdout );
242 
243     ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
244     if( ret != 0 )
245     {
246         mbedtls_printf( " failed\n  ! mbedtls_ecdh_compute_shared returned %d\n", ret );
247         goto exit;
248     }
249 
250     mbedtls_printf( " ok\n" );
251 
252     exit_code = MBEDTLS_EXIT_SUCCESS;
253 
254 exit:
255 
256 #if defined(_WIN32)
257     mbedtls_printf( "  + Press Enter to exit this program.\n" );
258     fflush( stdout ); getchar();
259 #endif
260 
261     mbedtls_ecdh_free( &ctx_srv );
262     mbedtls_ecdh_free( &ctx_cli );
263     mbedtls_ctr_drbg_free( &ctr_drbg );
264     mbedtls_entropy_free( &entropy );
265 
266     mbedtls_exit( exit_code );
267 }
268 #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
269           MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
270