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