Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Example ECDHE with Curve25519 program |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
| 5 | * 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. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PLATFORM_C) |
| 29 | #include "mbedtls/platform.h" |
| 30 | #else |
| 31 | #include <stdio.h> |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #define mbedtls_printf printf |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 34 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 36 | #endif /* MBEDTLS_PLATFORM_C */ |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 37 | |
Janos Follath | 52735ef | 2018-08-15 10:19:16 +0100 | [diff] [blame] | 38 | #if !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) || \ |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 39 | !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \ |
| 40 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) |
| 41 | int main( void ) |
| 42 | { |
Janos Follath | 52735ef | 2018-08-15 10:19:16 +0100 | [diff] [blame] | 43 | mbedtls_printf( "MBEDTLS_ECDH_C and/or MBEDTLS_ECDH_LEGACY_CONTEXT and/or " |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 44 | "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or " |
| 45 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C " |
| 46 | "not defined\n" ); |
| 47 | return( 0 ); |
| 48 | } |
| 49 | #else |
| 50 | |
| 51 | #include "mbedtls/entropy.h" |
| 52 | #include "mbedtls/ctr_drbg.h" |
| 53 | #include "mbedtls/ecdh.h" |
| 54 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame^] | 55 | #if defined( MBEDTLS_CHECK_PARAMS ) && defined(MBEDTLS_PLATFORM_C) |
| 56 | void mbedtls_param_failed( char* failure_condition, char* file, int line ) |
| 57 | { |
| 58 | mbedtls_printf("%s:%i: Input param failed - %sn", file, line, failure_condition ); |
| 59 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
| 60 | } |
| 61 | #endif |
| 62 | |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 63 | int main( int argc, char *argv[] ) |
| 64 | { |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 65 | int ret = 1; |
| 66 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 67 | mbedtls_ecdh_context ctx_cli, ctx_srv; |
| 68 | mbedtls_entropy_context entropy; |
| 69 | mbedtls_ctr_drbg_context ctr_drbg; |
| 70 | unsigned char cli_to_srv[32], srv_to_cli[32]; |
| 71 | const char pers[] = "ecdh"; |
| 72 | ((void) argc); |
| 73 | ((void) argv); |
| 74 | |
| 75 | mbedtls_ecdh_init( &ctx_cli ); |
| 76 | mbedtls_ecdh_init( &ctx_srv ); |
| 77 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 78 | |
| 79 | /* |
| 80 | * Initialize random number generation |
| 81 | */ |
| 82 | mbedtls_printf( " . Seeding the random number generator..." ); |
| 83 | fflush( stdout ); |
| 84 | |
| 85 | mbedtls_entropy_init( &entropy ); |
| 86 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
| 87 | (const unsigned char *) pers, |
| 88 | sizeof pers ) ) != 0 ) |
| 89 | { |
| 90 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
| 91 | goto exit; |
| 92 | } |
| 93 | |
| 94 | mbedtls_printf( " ok\n" ); |
| 95 | |
| 96 | /* |
| 97 | * Client: inialize context and generate keypair |
| 98 | */ |
| 99 | mbedtls_printf( " . Setting up client context..." ); |
| 100 | fflush( stdout ); |
| 101 | |
| 102 | ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 ); |
| 103 | if( ret != 0 ) |
| 104 | { |
| 105 | mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret ); |
| 106 | goto exit; |
| 107 | } |
| 108 | |
| 109 | ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q, |
| 110 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 111 | if( ret != 0 ) |
| 112 | { |
| 113 | mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret ); |
| 114 | goto exit; |
| 115 | } |
| 116 | |
| 117 | ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 ); |
| 118 | if( ret != 0 ) |
| 119 | { |
| 120 | mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret ); |
| 121 | goto exit; |
| 122 | } |
| 123 | |
| 124 | mbedtls_printf( " ok\n" ); |
| 125 | |
| 126 | /* |
| 127 | * Server: initialize context and generate keypair |
| 128 | */ |
| 129 | mbedtls_printf( " . Setting up server context..." ); |
| 130 | fflush( stdout ); |
| 131 | |
| 132 | ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 ); |
| 133 | if( ret != 0 ) |
| 134 | { |
| 135 | mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret ); |
| 136 | goto exit; |
| 137 | } |
| 138 | |
| 139 | ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q, |
| 140 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 141 | if( ret != 0 ) |
| 142 | { |
| 143 | mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret ); |
| 144 | goto exit; |
| 145 | } |
| 146 | |
| 147 | ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 ); |
| 148 | if( ret != 0 ) |
| 149 | { |
| 150 | mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret ); |
| 151 | goto exit; |
| 152 | } |
| 153 | |
| 154 | mbedtls_printf( " ok\n" ); |
| 155 | |
| 156 | /* |
| 157 | * Server: read peer's key and generate shared secret |
| 158 | */ |
| 159 | mbedtls_printf( " . Server reading client key and computing secret..." ); |
| 160 | fflush( stdout ); |
| 161 | |
| 162 | ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 ); |
| 163 | if( ret != 0 ) |
| 164 | { |
| 165 | mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret ); |
| 166 | goto exit; |
| 167 | } |
| 168 | |
| 169 | ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 ); |
| 170 | if( ret != 0 ) |
| 171 | { |
| 172 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret ); |
| 173 | goto exit; |
| 174 | } |
| 175 | |
| 176 | ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z, |
| 177 | &ctx_srv.Qp, &ctx_srv.d, |
| 178 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 179 | if( ret != 0 ) |
| 180 | { |
| 181 | mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); |
| 182 | goto exit; |
| 183 | } |
| 184 | |
| 185 | mbedtls_printf( " ok\n" ); |
| 186 | |
| 187 | /* |
| 188 | * Client: read peer's key and generate shared secret |
| 189 | */ |
| 190 | mbedtls_printf( " . Client reading server key and computing secret..." ); |
| 191 | fflush( stdout ); |
| 192 | |
| 193 | ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 ); |
| 194 | if( ret != 0 ) |
| 195 | { |
| 196 | mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret ); |
| 197 | goto exit; |
| 198 | } |
| 199 | |
| 200 | ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 ); |
| 201 | if( ret != 0 ) |
| 202 | { |
| 203 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret ); |
| 204 | goto exit; |
| 205 | } |
| 206 | |
| 207 | ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z, |
| 208 | &ctx_cli.Qp, &ctx_cli.d, |
| 209 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
| 210 | if( ret != 0 ) |
| 211 | { |
| 212 | mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); |
| 213 | goto exit; |
| 214 | } |
| 215 | |
| 216 | mbedtls_printf( " ok\n" ); |
| 217 | |
| 218 | /* |
Ron Eldor | 2a47be5 | 2017-06-20 15:23:23 +0300 | [diff] [blame] | 219 | * Verification: are the computed secrets equal? |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 220 | */ |
| 221 | mbedtls_printf( " . Checking if both computed secrets are equal..." ); |
| 222 | fflush( stdout ); |
| 223 | |
| 224 | ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z ); |
| 225 | if( ret != 0 ) |
| 226 | { |
| 227 | mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); |
| 228 | goto exit; |
| 229 | } |
| 230 | |
| 231 | mbedtls_printf( " ok\n" ); |
| 232 | |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 233 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 234 | |
| 235 | exit: |
| 236 | |
| 237 | #if defined(_WIN32) |
| 238 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
| 239 | fflush( stdout ); getchar(); |
| 240 | #endif |
| 241 | |
| 242 | mbedtls_ecdh_free( &ctx_srv ); |
| 243 | mbedtls_ecdh_free( &ctx_cli ); |
| 244 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 245 | mbedtls_entropy_free( &entropy ); |
| 246 | |
Andres Amaya Garcia | f47c9c1 | 2018-04-29 22:16:23 +0100 | [diff] [blame] | 247 | return( exit_code ); |
Manuel Pégourié-Gonnard | 3eb8c34 | 2015-10-09 12:11:14 +0100 | [diff] [blame] | 248 | } |
| 249 | #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED && |
| 250 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |