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