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