Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Example ECDHE with Curve25519 program |
| 3 | * |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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: |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 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 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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 | * ********** |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 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> |
Andres Amaya Garcia | 81982c8 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 57 | #include <stdlib.h> |
| 58 | #define mbedtls_printf printf |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 59 | #define mbedtls_exit exit |
Andres Amaya Garcia | 2b0599b | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 60 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 81982c8 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 61 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 62 | #endif /* MBEDTLS_PLATFORM_C */ |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 63 | |
| 64 | #if !defined(MBEDTLS_ECDH_C) || \ |
| 65 | !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \ |
| 66 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) |
| 67 | int main( void ) |
| 68 | { |
| 69 | mbedtls_printf( "MBEDTLS_ECDH_C and/or " |
| 70 | "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or " |
| 71 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C " |
| 72 | "not defined\n" ); |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 73 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 74 | } |
| 75 | #else |
| 76 | |
| 77 | #include "mbedtls/entropy.h" |
| 78 | #include "mbedtls/ctr_drbg.h" |
| 79 | #include "mbedtls/ecdh.h" |
| 80 | |
| 81 | int main( int argc, char *argv[] ) |
| 82 | { |
Andres Amaya Garcia | 81982c8 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 83 | int ret = 1; |
| 84 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 85 | mbedtls_ecdh_context ctx_cli, ctx_srv; |
| 86 | mbedtls_entropy_context entropy; |
| 87 | mbedtls_ctr_drbg_context ctr_drbg; |
| 88 | unsigned char cli_to_srv[32], srv_to_cli[32]; |
| 89 | const char pers[] = "ecdh"; |
| 90 | ((void) argc); |
| 91 | ((void) argv); |
| 92 | |
| 93 | mbedtls_ecdh_init( &ctx_cli ); |
| 94 | mbedtls_ecdh_init( &ctx_srv ); |
| 95 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 96 | |
| 97 | /* |
| 98 | * Initialize random number generation |
| 99 | */ |
| 100 | mbedtls_printf( " . Seeding the random number generator..." ); |
| 101 | fflush( stdout ); |
| 102 | |
| 103 | mbedtls_entropy_init( &entropy ); |
| 104 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
| 105 | (const unsigned char *) pers, |
| 106 | sizeof pers ) ) != 0 ) |
| 107 | { |
| 108 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
| 109 | goto exit; |
| 110 | } |
| 111 | |
| 112 | mbedtls_printf( " ok\n" ); |
| 113 | |
| 114 | /* |
| 115 | * Client: inialize context and generate keypair |
| 116 | */ |
| 117 | mbedtls_printf( " . Setting up client context..." ); |
| 118 | fflush( stdout ); |
| 119 | |
| 120 | ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 ); |
| 121 | if( ret != 0 ) |
| 122 | { |
| 123 | mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret ); |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q, |
| 128 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 129 | if( ret != 0 ) |
| 130 | { |
| 131 | mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret ); |
| 132 | goto exit; |
| 133 | } |
| 134 | |
| 135 | ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 ); |
| 136 | if( ret != 0 ) |
| 137 | { |
| 138 | mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret ); |
| 139 | goto exit; |
| 140 | } |
| 141 | |
| 142 | mbedtls_printf( " ok\n" ); |
| 143 | |
| 144 | /* |
| 145 | * Server: initialize context and generate keypair |
| 146 | */ |
| 147 | mbedtls_printf( " . Setting up server context..." ); |
| 148 | fflush( stdout ); |
| 149 | |
| 150 | ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 ); |
| 151 | if( ret != 0 ) |
| 152 | { |
| 153 | mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret ); |
| 154 | goto exit; |
| 155 | } |
| 156 | |
| 157 | ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q, |
| 158 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 159 | if( ret != 0 ) |
| 160 | { |
| 161 | mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret ); |
| 162 | goto exit; |
| 163 | } |
| 164 | |
| 165 | ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 ); |
| 166 | if( ret != 0 ) |
| 167 | { |
| 168 | mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret ); |
| 169 | goto exit; |
| 170 | } |
| 171 | |
| 172 | mbedtls_printf( " ok\n" ); |
| 173 | |
| 174 | /* |
| 175 | * Server: read peer's key and generate shared secret |
| 176 | */ |
| 177 | mbedtls_printf( " . Server reading client key and computing secret..." ); |
| 178 | fflush( stdout ); |
| 179 | |
| 180 | ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 ); |
| 181 | if( ret != 0 ) |
| 182 | { |
| 183 | mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret ); |
| 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 ); |
| 188 | if( ret != 0 ) |
| 189 | { |
| 190 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret ); |
| 191 | goto exit; |
| 192 | } |
| 193 | |
| 194 | ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z, |
| 195 | &ctx_srv.Qp, &ctx_srv.d, |
| 196 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 197 | if( ret != 0 ) |
| 198 | { |
| 199 | mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | mbedtls_printf( " ok\n" ); |
| 204 | |
| 205 | /* |
| 206 | * Client: read peer's key and generate shared secret |
| 207 | */ |
| 208 | mbedtls_printf( " . Client reading server key and computing secret..." ); |
| 209 | fflush( stdout ); |
| 210 | |
| 211 | ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 ); |
| 212 | if( ret != 0 ) |
| 213 | { |
| 214 | mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret ); |
| 215 | goto exit; |
| 216 | } |
| 217 | |
| 218 | ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 ); |
| 219 | if( ret != 0 ) |
| 220 | { |
| 221 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret ); |
| 222 | goto exit; |
| 223 | } |
| 224 | |
| 225 | ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z, |
| 226 | &ctx_cli.Qp, &ctx_cli.d, |
| 227 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 228 | if( ret != 0 ) |
| 229 | { |
| 230 | mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); |
| 231 | goto exit; |
| 232 | } |
| 233 | |
| 234 | mbedtls_printf( " ok\n" ); |
| 235 | |
| 236 | /* |
Ron Eldor | 2a47be5 | 2017-06-20 15:23:23 +0300 | [diff] [blame] | 237 | * Verification: are the computed secrets equal? |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 238 | */ |
| 239 | mbedtls_printf( " . Checking if both computed secrets are equal..." ); |
| 240 | fflush( stdout ); |
| 241 | |
| 242 | ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z ); |
| 243 | if( ret != 0 ) |
| 244 | { |
| 245 | mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); |
| 246 | goto exit; |
| 247 | } |
| 248 | |
| 249 | mbedtls_printf( " ok\n" ); |
| 250 | |
Andres Amaya Garcia | 81982c8 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 251 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 252 | |
| 253 | exit: |
| 254 | |
| 255 | #if defined(_WIN32) |
| 256 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
| 257 | fflush( stdout ); getchar(); |
| 258 | #endif |
| 259 | |
| 260 | mbedtls_ecdh_free( &ctx_srv ); |
| 261 | mbedtls_ecdh_free( &ctx_cli ); |
| 262 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 263 | mbedtls_entropy_free( &entropy ); |
| 264 | |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 265 | mbedtls_exit( exit_code ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 266 | } |
| 267 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED && |
| 268 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |