Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve Diffie-Hellman |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * References: |
| 22 | * |
| 23 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 24 | * RFC 4492 |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 27 | #include "common.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/ecdh.h" |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 32 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 33 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 34 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 37 | /* Parameter validation macros based on platform_util.h */ |
| 38 | #define ECDH_VALIDATE_RET( cond ) \ |
| 39 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) |
| 40 | #define ECDH_VALIDATE( cond ) \ |
| 41 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 42 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 43 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 44 | typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed; |
| 45 | #endif |
| 46 | |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 47 | static mbedtls_ecp_group_id mbedtls_ecdh_grp_id( |
| 48 | const mbedtls_ecdh_context *ctx ) |
| 49 | { |
| 50 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 51 | return( ctx->grp.id ); |
| 52 | #else |
| 53 | return( ctx->grp_id ); |
| 54 | #endif |
| 55 | } |
| 56 | |
Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 57 | int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid ) |
| 58 | { |
| 59 | /* At this time, all groups support ECDH. */ |
| 60 | (void) gid; |
Christoph M. Wintersteiger | c25df68 | 2019-04-16 12:54:56 +0100 | [diff] [blame] | 61 | return( 1 ); |
Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 64 | #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 65 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 66 | * Generate public key (restartable version) |
Manuel Pégourié-Gonnard | c0edc96 | 2018-10-16 10:38:19 +0200 | [diff] [blame] | 67 | * |
| 68 | * Note: this internal function relies on its caller preserving the value of |
Manuel Pégourié-Gonnard | ca29fdf | 2018-10-22 09:56:53 +0200 | [diff] [blame] | 69 | * the output parameter 'd' across continuation calls. This would not be |
Manuel Pégourié-Gonnard | c0edc96 | 2018-10-16 10:38:19 +0200 | [diff] [blame] | 70 | * acceptable for a public function but is OK here as we control call sites. |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 71 | */ |
| 72 | static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp, |
| 73 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 74 | int (*f_rng)(void *, unsigned char *, size_t), |
| 75 | void *p_rng, |
| 76 | mbedtls_ecp_restart_ctx *rs_ctx ) |
| 77 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 79 | |
| 80 | /* If multiplication is in progress, we already generated a privkey */ |
| 81 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 82 | if( rs_ctx == NULL || rs_ctx->rsm == NULL ) |
| 83 | #endif |
| 84 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) ); |
| 85 | |
| 86 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G, |
| 87 | f_rng, p_rng, rs_ctx ) ); |
| 88 | |
| 89 | cleanup: |
| 90 | return( ret ); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Generate public key |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 95 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 97 | int (*f_rng)(void *, unsigned char *, size_t), |
| 98 | void *p_rng ) |
| 99 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 100 | ECDH_VALIDATE_RET( grp != NULL ); |
| 101 | ECDH_VALIDATE_RET( d != NULL ); |
| 102 | ECDH_VALIDATE_RET( Q != NULL ); |
| 103 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 104 | return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 105 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 106 | #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */ |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 107 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 108 | #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Compute shared secret (SEC1 3.3.1) |
| 111 | */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 112 | static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp, |
| 113 | mbedtls_mpi *z, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 115 | int (*f_rng)(void *, unsigned char *, size_t), |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 116 | void *p_rng, |
| 117 | mbedtls_ecp_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 118 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 119 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | mbedtls_ecp_point P; |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | mbedtls_ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 123 | |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 124 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q, |
| 125 | f_rng, p_rng, rs_ctx ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | if( mbedtls_ecp_is_zero( &P ) ) |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 128 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 130 | goto cleanup; |
| 131 | } |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 134 | |
| 135 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | mbedtls_ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 137 | |
| 138 | return( ret ); |
| 139 | } |
| 140 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 141 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 142 | * Compute shared secret (SEC1 3.3.1) |
| 143 | */ |
| 144 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 145 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
| 146 | int (*f_rng)(void *, unsigned char *, size_t), |
| 147 | void *p_rng ) |
| 148 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 149 | ECDH_VALIDATE_RET( grp != NULL ); |
| 150 | ECDH_VALIDATE_RET( Q != NULL ); |
| 151 | ECDH_VALIDATE_RET( d != NULL ); |
| 152 | ECDH_VALIDATE_RET( z != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 153 | return( ecdh_compute_shared_restartable( grp, z, Q, d, |
| 154 | f_rng, p_rng, NULL ) ); |
| 155 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 156 | #endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 157 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 158 | static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx ) |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 159 | { |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 160 | mbedtls_ecp_group_init( &ctx->grp ); |
| 161 | mbedtls_mpi_init( &ctx->d ); |
| 162 | mbedtls_ecp_point_init( &ctx->Q ); |
| 163 | mbedtls_ecp_point_init( &ctx->Qp ); |
| 164 | mbedtls_mpi_init( &ctx->z ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 165 | |
| 166 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 167 | mbedtls_ecp_restart_init( &ctx->rs ); |
| 168 | #endif |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 171 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 172 | * Initialize context |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 173 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 174 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) |
| 175 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 176 | ECDH_VALIDATE( ctx != NULL ); |
| 177 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 178 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 179 | ecdh_init_internal( ctx ); |
| 180 | mbedtls_ecp_point_init( &ctx->Vi ); |
| 181 | mbedtls_ecp_point_init( &ctx->Vf ); |
| 182 | mbedtls_mpi_init( &ctx->_d ); |
| 183 | #else |
Christoph M. Wintersteiger | d8c45d5 | 2019-02-20 17:16:53 +0000 | [diff] [blame] | 184 | memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); |
| 185 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 186 | ctx->var = MBEDTLS_ECDH_VARIANT_NONE; |
| 187 | #endif |
| 188 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 189 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 190 | ctx->restart_enabled = 0; |
| 191 | #endif |
| 192 | } |
| 193 | |
| 194 | static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx, |
| 195 | mbedtls_ecp_group_id grp_id ) |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 196 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 198 | |
| 199 | ret = mbedtls_ecp_group_load( &ctx->grp, grp_id ); |
| 200 | if( ret != 0 ) |
| 201 | { |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 202 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 203 | } |
| 204 | |
| 205 | return( 0 ); |
| 206 | } |
| 207 | |
| 208 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 209 | * Setup context |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 210 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 211 | int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 212 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 213 | ECDH_VALIDATE_RET( ctx != NULL ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 214 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 215 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 216 | return( ecdh_setup_internal( ctx, grp_id ) ); |
| 217 | #else |
| 218 | switch( grp_id ) |
| 219 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 220 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
Christoph M. Wintersteiger | 3ff60bc | 2019-02-15 12:59:59 +0000 | [diff] [blame] | 221 | case MBEDTLS_ECP_DP_CURVE25519: |
| 222 | ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED; |
| 223 | ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST; |
| 224 | ctx->grp_id = grp_id; |
| 225 | return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) ); |
Christoph M. Wintersteiger | 78c9c46 | 2018-12-06 17:16:32 +0000 | [diff] [blame] | 226 | #endif |
Christoph M. Wintersteiger | 3ff60bc | 2019-02-15 12:59:59 +0000 | [diff] [blame] | 227 | default: |
| 228 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 229 | ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; |
| 230 | ctx->grp_id = grp_id; |
| 231 | ecdh_init_internal( &ctx->ctx.mbed_ecdh ); |
| 232 | return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx ) |
| 238 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | mbedtls_ecp_group_free( &ctx->grp ); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 240 | mbedtls_mpi_free( &ctx->d ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | mbedtls_ecp_point_free( &ctx->Q ); |
| 242 | mbedtls_ecp_point_free( &ctx->Qp ); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 243 | mbedtls_mpi_free( &ctx->z ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 244 | |
| 245 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 246 | mbedtls_ecp_restart_free( &ctx->rs ); |
| 247 | #endif |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 250 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 251 | /* |
| 252 | * Enable restartable operations for context |
| 253 | */ |
| 254 | void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ) |
| 255 | { |
Hanno Becker | a7634e8 | 2018-12-18 18:45:00 +0000 | [diff] [blame] | 256 | ECDH_VALIDATE( ctx != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 258 | ctx->restart_enabled = 1; |
| 259 | } |
| 260 | #endif |
| 261 | |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 262 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 263 | * Free context |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 264 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 265 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) |
| 266 | { |
| 267 | if( ctx == NULL ) |
| 268 | return; |
| 269 | |
| 270 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 271 | mbedtls_ecp_point_free( &ctx->Vi ); |
| 272 | mbedtls_ecp_point_free( &ctx->Vf ); |
| 273 | mbedtls_mpi_free( &ctx->_d ); |
| 274 | ecdh_free_internal( ctx ); |
| 275 | #else |
| 276 | switch( ctx->var ) |
| 277 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 278 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 279 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 280 | mbedtls_everest_free( &ctx->ctx.everest_ecdh ); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 281 | break; |
| 282 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 283 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 284 | ecdh_free_internal( &ctx->ctx.mbed_ecdh ); |
| 285 | break; |
| 286 | default: |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 291 | ctx->var = MBEDTLS_ECDH_VARIANT_NONE; |
| 292 | ctx->grp_id = MBEDTLS_ECP_DP_NONE; |
| 293 | #endif |
| 294 | } |
| 295 | |
| 296 | static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 297 | size_t *olen, int point_format, |
| 298 | unsigned char *buf, size_t blen, |
| 299 | int (*f_rng)(void *, |
| 300 | unsigned char *, |
| 301 | size_t), |
| 302 | void *p_rng, |
| 303 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 304 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 305 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 306 | size_t grp_len, pt_len; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 307 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 308 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 309 | #endif |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 310 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 311 | if( ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 313 | |
Ron Eldor | 19779c4 | 2018-11-05 16:58:13 +0200 | [diff] [blame] | 314 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 315 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 316 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 317 | #else |
| 318 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 319 | #endif |
| 320 | |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 321 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 322 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 323 | if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 324 | f_rng, p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 325 | return( ret ); |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 326 | #else |
| 327 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, |
| 328 | f_rng, p_rng ) ) != 0 ) |
| 329 | return( ret ); |
| 330 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 331 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 332 | if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, |
| 333 | blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 334 | return( ret ); |
| 335 | |
| 336 | buf += grp_len; |
| 337 | blen -= grp_len; |
| 338 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 339 | if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 340 | &pt_len, buf, blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 341 | return( ret ); |
| 342 | |
| 343 | *olen = grp_len + pt_len; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 344 | return( 0 ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 347 | /* |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 348 | * Setup and write the ServerKeyExchange parameters (RFC 4492) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 349 | * struct { |
| 350 | * ECParameters curve_params; |
| 351 | * ECPoint public; |
| 352 | * } ServerECDHParams; |
| 353 | */ |
| 354 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, |
| 355 | unsigned char *buf, size_t blen, |
| 356 | int (*f_rng)(void *, unsigned char *, size_t), |
| 357 | void *p_rng ) |
| 358 | { |
| 359 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 360 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 361 | ECDH_VALIDATE_RET( olen != NULL ); |
| 362 | ECDH_VALIDATE_RET( buf != NULL ); |
| 363 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 364 | |
| 365 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 366 | restart_enabled = ctx->restart_enabled; |
| 367 | #else |
| 368 | (void) restart_enabled; |
| 369 | #endif |
| 370 | |
| 371 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 372 | return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen, |
| 373 | f_rng, p_rng, restart_enabled ) ); |
| 374 | #else |
| 375 | switch( ctx->var ) |
| 376 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 377 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 378 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 379 | return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen, |
| 380 | buf, blen, f_rng, p_rng ) ); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 381 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 382 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 383 | return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen, |
| 384 | ctx->point_format, buf, blen, |
| 385 | f_rng, p_rng, |
| 386 | restart_enabled ) ); |
| 387 | default: |
| 388 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 389 | } |
| 390 | #endif |
| 391 | } |
| 392 | |
| 393 | static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 394 | const unsigned char **buf, |
| 395 | const unsigned char *end ) |
| 396 | { |
| 397 | return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, |
| 398 | end - *buf ) ); |
| 399 | } |
| 400 | |
| 401 | /* |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 402 | * Read the ServerKeyExhange parameters (RFC 4492) |
| 403 | * struct { |
| 404 | * ECParameters curve_params; |
| 405 | * ECPoint public; |
| 406 | * } ServerECDHParams; |
| 407 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 409 | const unsigned char **buf, |
| 410 | const unsigned char *end ) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 411 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 412 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 413 | mbedtls_ecp_group_id grp_id; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 414 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 415 | ECDH_VALIDATE_RET( buf != NULL ); |
| 416 | ECDH_VALIDATE_RET( *buf != NULL ); |
| 417 | ECDH_VALIDATE_RET( end != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 418 | |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 419 | if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) ) |
| 420 | != 0 ) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 421 | return( ret ); |
| 422 | |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 423 | if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 ) |
| 424 | return( ret ); |
| 425 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 426 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 427 | return( ecdh_read_params_internal( ctx, buf, end ) ); |
| 428 | #else |
| 429 | switch( ctx->var ) |
| 430 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 431 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 432 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 433 | return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh, |
| 434 | buf, end) ); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 435 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 436 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 437 | return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh, |
| 438 | buf, end ) ); |
| 439 | default: |
| 440 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 441 | } |
| 442 | #endif |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 443 | } |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 444 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 445 | static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 446 | const mbedtls_ecp_keypair *key, |
| 447 | mbedtls_ecdh_side side ) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 448 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 449 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 450 | |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 451 | /* If it's not our key, just import the public part as Qp */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | if( side == MBEDTLS_ECDH_THEIRS ) |
| 453 | return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 454 | |
| 455 | /* Our key: import public (as Q) and private parts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | if( side != MBEDTLS_ECDH_OURS ) |
| 457 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 || |
| 460 | ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 461 | return( ret ); |
| 462 | |
| 463 | return( 0 ); |
| 464 | } |
| 465 | |
| 466 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 467 | * Get parameters from a keypair |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 468 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 469 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, |
| 470 | const mbedtls_ecp_keypair *key, |
| 471 | mbedtls_ecdh_side side ) |
| 472 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 473 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 474 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 475 | ECDH_VALIDATE_RET( key != NULL ); |
| 476 | ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS || |
| 477 | side == MBEDTLS_ECDH_THEIRS ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 478 | |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 479 | if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE ) |
Gilles Peskine | 0b1b71d | 2018-11-07 22:10:59 +0100 | [diff] [blame] | 480 | { |
| 481 | /* This is the first call to get_params(). Set up the context |
| 482 | * for use with the group. */ |
| 483 | if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 ) |
| 484 | return( ret ); |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | /* This is not the first call to get_params(). Check that the |
| 489 | * current key's group is the same as the context's, which was set |
| 490 | * from the first key's group. */ |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 491 | if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id ) |
Gilles Peskine | 0b1b71d | 2018-11-07 22:10:59 +0100 | [diff] [blame] | 492 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 493 | } |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 494 | |
| 495 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 496 | return( ecdh_get_params_internal( ctx, key, side ) ); |
| 497 | #else |
| 498 | switch( ctx->var ) |
| 499 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 500 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 501 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 502 | { |
Christoph M. Wintersteiger | 2e724a1 | 2019-01-07 14:19:41 +0000 | [diff] [blame] | 503 | mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ? |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 504 | MBEDTLS_EVEREST_ECDH_OURS : |
| 505 | MBEDTLS_EVEREST_ECDH_THEIRS; |
| 506 | return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh, |
| 507 | key, s) ); |
| 508 | } |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 509 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 510 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 511 | return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh, |
| 512 | key, side ) ); |
| 513 | default: |
| 514 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 515 | } |
| 516 | #endif |
| 517 | } |
| 518 | |
| 519 | static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx, |
| 520 | size_t *olen, int point_format, |
| 521 | unsigned char *buf, size_t blen, |
| 522 | int (*f_rng)(void *, |
| 523 | unsigned char *, |
| 524 | size_t), |
| 525 | void *p_rng, |
| 526 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 527 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 528 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 529 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 530 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 531 | #endif |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 532 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 533 | if( ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 535 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 536 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 537 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 538 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 539 | #else |
| 540 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 541 | #endif |
| 542 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 543 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 544 | if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 545 | f_rng, p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 546 | return( ret ); |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 547 | #else |
| 548 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, |
| 549 | f_rng, p_rng ) ) != 0 ) |
| 550 | return( ret ); |
| 551 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 552 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 553 | return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen, |
| 554 | buf, blen ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 558 | * Setup and export the client public value |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 559 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 560 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
| 561 | unsigned char *buf, size_t blen, |
| 562 | int (*f_rng)(void *, unsigned char *, size_t), |
| 563 | void *p_rng ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 564 | { |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 565 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 566 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 567 | ECDH_VALIDATE_RET( olen != NULL ); |
| 568 | ECDH_VALIDATE_RET( buf != NULL ); |
Hanno Becker | c81cfec | 2018-12-18 23:32:42 +0000 | [diff] [blame] | 569 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 570 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 571 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 572 | restart_enabled = ctx->restart_enabled; |
| 573 | #endif |
| 574 | |
| 575 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 576 | return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen, |
| 577 | f_rng, p_rng, restart_enabled ) ); |
| 578 | #else |
| 579 | switch( ctx->var ) |
| 580 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 581 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 582 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 583 | return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen, |
| 584 | buf, blen, f_rng, p_rng ) ); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 585 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 586 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 587 | return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen, |
| 588 | ctx->point_format, buf, blen, |
| 589 | f_rng, p_rng, |
| 590 | restart_enabled ) ); |
| 591 | default: |
| 592 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 593 | } |
| 594 | #endif |
| 595 | } |
| 596 | |
| 597 | static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx, |
| 598 | const unsigned char *buf, size_t blen ) |
| 599 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 600 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 601 | const unsigned char *p = buf; |
| 602 | |
| 603 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, |
| 604 | blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 605 | return( ret ); |
| 606 | |
| 607 | if( (size_t)( p - buf ) != blen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 609 | |
| 610 | return( 0 ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 613 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 614 | * Parse and import the client's public value |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 615 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 616 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
| 617 | const unsigned char *buf, size_t blen ) |
| 618 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 619 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 620 | ECDH_VALIDATE_RET( buf != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 621 | |
| 622 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 623 | return( ecdh_read_public_internal( ctx, buf, blen ) ); |
| 624 | #else |
| 625 | switch( ctx->var ) |
| 626 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 627 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 628 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 629 | return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh, |
| 630 | buf, blen ) ); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 631 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 632 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 633 | return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh, |
| 634 | buf, blen ) ); |
| 635 | default: |
| 636 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 637 | } |
| 638 | #endif |
| 639 | } |
| 640 | |
| 641 | static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx, |
| 642 | size_t *olen, unsigned char *buf, |
| 643 | size_t blen, |
| 644 | int (*f_rng)(void *, |
| 645 | unsigned char *, |
| 646 | size_t), |
| 647 | void *p_rng, |
| 648 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 649 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 650 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 651 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 652 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 653 | #endif |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 654 | |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 655 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 657 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 658 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 659 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 660 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 661 | #else |
| 662 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 663 | #endif |
| 664 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 665 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 666 | if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp, |
| 667 | &ctx->d, f_rng, p_rng, |
| 668 | rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 669 | { |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 670 | return( ret ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 671 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 672 | #else |
| 673 | if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, |
| 674 | &ctx->d, f_rng, p_rng ) ) != 0 ) |
| 675 | { |
| 676 | return( ret ); |
| 677 | } |
| 678 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | if( mbedtls_mpi_size( &ctx->z ) > blen ) |
| 681 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 682 | |
Manuel Pégourié-Gonnard | 0a56c2c | 2014-01-17 21:24:04 +0100 | [diff] [blame] | 683 | *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); |
Janos Follath | ab0f71a | 2019-02-20 10:48:49 +0000 | [diff] [blame] | 684 | |
Janos Follath | 52ff8e9 | 2019-02-26 13:56:04 +0000 | [diff] [blame] | 685 | if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY ) |
Janos Follath | ab0f71a | 2019-02-20 10:48:49 +0000 | [diff] [blame] | 686 | return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen ); |
| 687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | return mbedtls_mpi_write_binary( &ctx->z, buf, *olen ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 691 | /* |
| 692 | * Derive and export the shared secret |
| 693 | */ |
| 694 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
| 695 | unsigned char *buf, size_t blen, |
| 696 | int (*f_rng)(void *, unsigned char *, size_t), |
| 697 | void *p_rng ) |
| 698 | { |
| 699 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 700 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 701 | ECDH_VALIDATE_RET( olen != NULL ); |
| 702 | ECDH_VALIDATE_RET( buf != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 703 | |
| 704 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 705 | restart_enabled = ctx->restart_enabled; |
| 706 | #endif |
| 707 | |
| 708 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 709 | return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng, |
| 710 | restart_enabled ) ); |
| 711 | #else |
| 712 | switch( ctx->var ) |
| 713 | { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 714 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 715 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 716 | return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen, |
| 717 | buf, blen, f_rng, p_rng ) ); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 718 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 719 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 720 | return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf, |
| 721 | blen, f_rng, p_rng, |
| 722 | restart_enabled ) ); |
| 723 | default: |
| 724 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 725 | } |
| 726 | #endif |
| 727 | } |
| 728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 729 | #endif /* MBEDTLS_ECDH_C */ |