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 | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame^] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * References: |
| 49 | * |
| 50 | * 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] | 51 | * RFC 4492 |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 52 | */ |
| 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 56 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 58 | #endif |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | #include "mbedtls/ecdh.h" |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 63 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 64 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 65 | #include <string.h> |
| 66 | |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 67 | /* Parameter validation macros based on platform_util.h */ |
| 68 | #define ECDH_VALIDATE_RET( cond ) \ |
| 69 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) |
| 70 | #define ECDH_VALIDATE( cond ) \ |
| 71 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 72 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 73 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 74 | typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed; |
| 75 | #endif |
| 76 | |
Gilles Peskine | 05fcf4f | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 77 | static mbedtls_ecp_group_id mbedtls_ecdh_grp_id( |
| 78 | const mbedtls_ecdh_context *ctx ) |
| 79 | { |
| 80 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 81 | return( ctx->grp.id ); |
| 82 | #else |
| 83 | return( ctx->grp_id ); |
| 84 | #endif |
| 85 | } |
| 86 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 87 | #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 88 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 89 | * Generate public key (restartable version) |
Manuel Pégourié-Gonnard | c0edc96 | 2018-10-16 10:38:19 +0200 | [diff] [blame] | 90 | * |
| 91 | * 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] | 92 | * 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] | 93 | * 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] | 94 | */ |
| 95 | static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp, |
| 96 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 97 | int (*f_rng)(void *, unsigned char *, size_t), |
| 98 | void *p_rng, |
| 99 | mbedtls_ecp_restart_ctx *rs_ctx ) |
| 100 | { |
| 101 | int ret; |
| 102 | |
| 103 | /* If multiplication is in progress, we already generated a privkey */ |
| 104 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 105 | if( rs_ctx == NULL || rs_ctx->rsm == NULL ) |
| 106 | #endif |
| 107 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) ); |
| 108 | |
| 109 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G, |
| 110 | f_rng, p_rng, rs_ctx ) ); |
| 111 | |
| 112 | cleanup: |
| 113 | return( ret ); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Generate public key |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 118 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | 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] | 120 | int (*f_rng)(void *, unsigned char *, size_t), |
| 121 | void *p_rng ) |
| 122 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 123 | ECDH_VALIDATE_RET( grp != NULL ); |
| 124 | ECDH_VALIDATE_RET( d != NULL ); |
| 125 | ECDH_VALIDATE_RET( Q != NULL ); |
| 126 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 127 | 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] | 128 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 129 | #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */ |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 130 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 131 | #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 132 | /* |
| 133 | * Compute shared secret (SEC1 3.3.1) |
| 134 | */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 135 | static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp, |
| 136 | mbedtls_mpi *z, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 138 | int (*f_rng)(void *, unsigned char *, size_t), |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 139 | void *p_rng, |
| 140 | mbedtls_ecp_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 141 | { |
| 142 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | mbedtls_ecp_point P; |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 147 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q, |
| 148 | f_rng, p_rng, rs_ctx ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | if( mbedtls_ecp_is_zero( &P ) ) |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Paul Bakker | b548d77 | 2013-07-26 14:21:34 +0200 | [diff] [blame] | 153 | goto cleanup; |
| 154 | } |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 155 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 157 | |
| 158 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 160 | |
| 161 | return( ret ); |
| 162 | } |
| 163 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 164 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 165 | * Compute shared secret (SEC1 3.3.1) |
| 166 | */ |
| 167 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 168 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
| 169 | int (*f_rng)(void *, unsigned char *, size_t), |
| 170 | void *p_rng ) |
| 171 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 172 | ECDH_VALIDATE_RET( grp != NULL ); |
| 173 | ECDH_VALIDATE_RET( Q != NULL ); |
| 174 | ECDH_VALIDATE_RET( d != NULL ); |
| 175 | ECDH_VALIDATE_RET( z != NULL ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 176 | return( ecdh_compute_shared_restartable( grp, z, Q, d, |
| 177 | f_rng, p_rng, NULL ) ); |
| 178 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 179 | #endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 180 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 181 | static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx ) |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 182 | { |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 183 | mbedtls_ecp_group_init( &ctx->grp ); |
| 184 | mbedtls_mpi_init( &ctx->d ); |
| 185 | mbedtls_ecp_point_init( &ctx->Q ); |
| 186 | mbedtls_ecp_point_init( &ctx->Qp ); |
| 187 | mbedtls_mpi_init( &ctx->z ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 188 | |
| 189 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 190 | mbedtls_ecp_restart_init( &ctx->rs ); |
| 191 | #endif |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 194 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 195 | * Initialize context |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 196 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 197 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) |
| 198 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 199 | ECDH_VALIDATE( ctx != NULL ); |
| 200 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 201 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 202 | ecdh_init_internal( ctx ); |
| 203 | mbedtls_ecp_point_init( &ctx->Vi ); |
| 204 | mbedtls_ecp_point_init( &ctx->Vf ); |
| 205 | mbedtls_mpi_init( &ctx->_d ); |
| 206 | #else |
| 207 | memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); |
| 208 | |
| 209 | ctx->var = MBEDTLS_ECDH_VARIANT_NONE; |
| 210 | #endif |
| 211 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 212 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 213 | ctx->restart_enabled = 0; |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx, |
| 218 | mbedtls_ecp_group_id grp_id ) |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 219 | { |
| 220 | int ret; |
| 221 | |
| 222 | ret = mbedtls_ecp_group_load( &ctx->grp, grp_id ); |
| 223 | if( ret != 0 ) |
| 224 | { |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 225 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 226 | } |
| 227 | |
| 228 | return( 0 ); |
| 229 | } |
| 230 | |
| 231 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 232 | * Setup context |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 233 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 234 | 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] | 235 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 236 | ECDH_VALIDATE_RET( ctx != NULL ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 237 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 238 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 239 | return( ecdh_setup_internal( ctx, grp_id ) ); |
| 240 | #else |
| 241 | switch( grp_id ) |
| 242 | { |
| 243 | default: |
| 244 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 245 | ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; |
| 246 | ctx->grp_id = grp_id; |
| 247 | ecdh_init_internal( &ctx->ctx.mbed_ecdh ); |
| 248 | return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) ); |
| 249 | } |
| 250 | #endif |
| 251 | } |
| 252 | |
| 253 | static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx ) |
| 254 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | mbedtls_ecp_group_free( &ctx->grp ); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 256 | mbedtls_mpi_free( &ctx->d ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_ecp_point_free( &ctx->Q ); |
| 258 | mbedtls_ecp_point_free( &ctx->Qp ); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 259 | mbedtls_mpi_free( &ctx->z ); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 260 | |
| 261 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 262 | mbedtls_ecp_restart_free( &ctx->rs ); |
| 263 | #endif |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 266 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 267 | /* |
| 268 | * Enable restartable operations for context |
| 269 | */ |
| 270 | void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ) |
| 271 | { |
Hanno Becker | a7634e8 | 2018-12-18 18:45:00 +0000 | [diff] [blame] | 272 | ECDH_VALIDATE( ctx != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 273 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 274 | ctx->restart_enabled = 1; |
| 275 | } |
| 276 | #endif |
| 277 | |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 278 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 279 | * Free context |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 280 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 281 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) |
| 282 | { |
| 283 | if( ctx == NULL ) |
| 284 | return; |
| 285 | |
| 286 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 287 | mbedtls_ecp_point_free( &ctx->Vi ); |
| 288 | mbedtls_ecp_point_free( &ctx->Vf ); |
| 289 | mbedtls_mpi_free( &ctx->_d ); |
| 290 | ecdh_free_internal( ctx ); |
| 291 | #else |
| 292 | switch( ctx->var ) |
| 293 | { |
| 294 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 295 | ecdh_free_internal( &ctx->ctx.mbed_ecdh ); |
| 296 | break; |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
| 302 | ctx->var = MBEDTLS_ECDH_VARIANT_NONE; |
| 303 | ctx->grp_id = MBEDTLS_ECP_DP_NONE; |
| 304 | #endif |
| 305 | } |
| 306 | |
| 307 | static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 308 | size_t *olen, int point_format, |
| 309 | unsigned char *buf, size_t blen, |
| 310 | int (*f_rng)(void *, |
| 311 | unsigned char *, |
| 312 | size_t), |
| 313 | void *p_rng, |
| 314 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 315 | { |
| 316 | int ret; |
| 317 | size_t grp_len, pt_len; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 318 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 319 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 320 | #endif |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 321 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 322 | if( ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 324 | |
Ron Eldor | 19779c4 | 2018-11-05 16:58:13 +0200 | [diff] [blame] | 325 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 326 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 327 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 328 | #else |
| 329 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 330 | #endif |
| 331 | |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 332 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 333 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 334 | 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] | 335 | f_rng, p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 336 | return( ret ); |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 337 | #else |
| 338 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, |
| 339 | f_rng, p_rng ) ) != 0 ) |
| 340 | return( ret ); |
| 341 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 342 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 343 | if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, |
| 344 | blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 345 | return( ret ); |
| 346 | |
| 347 | buf += grp_len; |
| 348 | blen -= grp_len; |
| 349 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 350 | 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] | 351 | &pt_len, buf, blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 352 | return( ret ); |
| 353 | |
| 354 | *olen = grp_len + pt_len; |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 355 | return( 0 ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 358 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 359 | * Setup and write the ServerKeyExhange parameters (RFC 4492) |
| 360 | * struct { |
| 361 | * ECParameters curve_params; |
| 362 | * ECPoint public; |
| 363 | * } ServerECDHParams; |
| 364 | */ |
| 365 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, |
| 366 | unsigned char *buf, size_t blen, |
| 367 | int (*f_rng)(void *, unsigned char *, size_t), |
| 368 | void *p_rng ) |
| 369 | { |
| 370 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 371 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 372 | ECDH_VALIDATE_RET( olen != NULL ); |
| 373 | ECDH_VALIDATE_RET( buf != NULL ); |
| 374 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 375 | |
| 376 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 377 | restart_enabled = ctx->restart_enabled; |
| 378 | #else |
| 379 | (void) restart_enabled; |
| 380 | #endif |
| 381 | |
| 382 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 383 | return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen, |
| 384 | f_rng, p_rng, restart_enabled ) ); |
| 385 | #else |
| 386 | switch( ctx->var ) |
| 387 | { |
| 388 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 389 | return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen, |
| 390 | ctx->point_format, buf, blen, |
| 391 | f_rng, p_rng, |
| 392 | restart_enabled ) ); |
| 393 | default: |
| 394 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 395 | } |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx, |
| 400 | const unsigned char **buf, |
| 401 | const unsigned char *end ) |
| 402 | { |
| 403 | return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, |
| 404 | end - *buf ) ); |
| 405 | } |
| 406 | |
| 407 | /* |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 408 | * Read the ServerKeyExhange parameters (RFC 4492) |
| 409 | * struct { |
| 410 | * ECParameters curve_params; |
| 411 | * ECPoint public; |
| 412 | * } ServerECDHParams; |
| 413 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 415 | const unsigned char **buf, |
| 416 | const unsigned char *end ) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 417 | { |
| 418 | int ret; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 419 | mbedtls_ecp_group_id grp_id; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 420 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 421 | ECDH_VALIDATE_RET( buf != NULL ); |
| 422 | ECDH_VALIDATE_RET( *buf != NULL ); |
| 423 | ECDH_VALIDATE_RET( end != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 424 | |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 425 | if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) ) |
| 426 | != 0 ) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 427 | return( ret ); |
| 428 | |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 429 | if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 ) |
| 430 | return( ret ); |
| 431 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 432 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 433 | return( ecdh_read_params_internal( ctx, buf, end ) ); |
| 434 | #else |
| 435 | switch( ctx->var ) |
| 436 | { |
| 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 | { |
| 450 | int ret; |
| 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 | { |
| 474 | int ret; |
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 | 05fcf4f | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 480 | if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE ) |
Gilles Peskine | b47045a | 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 | 05fcf4f | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 492 | if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id ) |
Gilles Peskine | b47045a | 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 | { |
| 501 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 502 | return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh, |
| 503 | key, side ) ); |
| 504 | default: |
| 505 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 506 | } |
| 507 | #endif |
| 508 | } |
| 509 | |
| 510 | static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx, |
| 511 | size_t *olen, int point_format, |
| 512 | unsigned char *buf, size_t blen, |
| 513 | int (*f_rng)(void *, |
| 514 | unsigned char *, |
| 515 | size_t), |
| 516 | void *p_rng, |
| 517 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 518 | { |
| 519 | int ret; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 520 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 521 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 522 | #endif |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 523 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 524 | if( ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 526 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 527 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 528 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 529 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 530 | #else |
| 531 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 532 | #endif |
| 533 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 534 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 535 | if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 536 | f_rng, p_rng, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 537 | return( ret ); |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 538 | #else |
| 539 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, |
| 540 | f_rng, p_rng ) ) != 0 ) |
| 541 | return( ret ); |
| 542 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 543 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 544 | return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen, |
| 545 | buf, blen ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 549 | * Setup and export the client public value |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 550 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 551 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
| 552 | unsigned char *buf, size_t blen, |
| 553 | int (*f_rng)(void *, unsigned char *, size_t), |
| 554 | void *p_rng ) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 555 | { |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 556 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 557 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 558 | ECDH_VALIDATE_RET( olen != NULL ); |
| 559 | ECDH_VALIDATE_RET( buf != NULL ); |
Hanno Becker | c81cfec | 2018-12-18 23:32:42 +0000 | [diff] [blame] | 560 | ECDH_VALIDATE_RET( f_rng != NULL ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 561 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 562 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 563 | restart_enabled = ctx->restart_enabled; |
| 564 | #endif |
| 565 | |
| 566 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 567 | return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen, |
| 568 | f_rng, p_rng, restart_enabled ) ); |
| 569 | #else |
| 570 | switch( ctx->var ) |
| 571 | { |
| 572 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 573 | return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen, |
| 574 | ctx->point_format, buf, blen, |
| 575 | f_rng, p_rng, |
| 576 | restart_enabled ) ); |
| 577 | default: |
| 578 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 579 | } |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx, |
| 584 | const unsigned char *buf, size_t blen ) |
| 585 | { |
| 586 | int ret; |
| 587 | const unsigned char *p = buf; |
| 588 | |
| 589 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, |
| 590 | blen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 591 | return( ret ); |
| 592 | |
| 593 | if( (size_t)( p - buf ) != blen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 595 | |
| 596 | return( 0 ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 599 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 600 | * Parse and import the client's public value |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 601 | */ |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 602 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
| 603 | const unsigned char *buf, size_t blen ) |
| 604 | { |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 605 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 606 | ECDH_VALIDATE_RET( buf != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 607 | |
| 608 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 609 | return( ecdh_read_public_internal( ctx, buf, blen ) ); |
| 610 | #else |
| 611 | switch( ctx->var ) |
| 612 | { |
| 613 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 614 | return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh, |
| 615 | buf, blen ) ); |
| 616 | default: |
| 617 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 618 | } |
| 619 | #endif |
| 620 | } |
| 621 | |
| 622 | static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx, |
| 623 | size_t *olen, unsigned char *buf, |
| 624 | size_t blen, |
| 625 | int (*f_rng)(void *, |
| 626 | unsigned char *, |
| 627 | size_t), |
| 628 | void *p_rng, |
| 629 | int restart_enabled ) |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 630 | { |
| 631 | int ret; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 632 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 633 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 634 | #endif |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 635 | |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 636 | if( ctx == NULL || ctx->grp.pbits == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 638 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 639 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 640 | if( restart_enabled ) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 641 | rs_ctx = &ctx->rs; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 642 | #else |
| 643 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 644 | #endif |
| 645 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 646 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 647 | if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp, |
| 648 | &ctx->d, f_rng, p_rng, |
| 649 | rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 650 | { |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 651 | return( ret ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 652 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 653 | #else |
| 654 | if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, |
| 655 | &ctx->d, f_rng, p_rng ) ) != 0 ) |
| 656 | { |
| 657 | return( ret ); |
| 658 | } |
| 659 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | if( mbedtls_mpi_size( &ctx->z ) > blen ) |
| 662 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 663 | |
Manuel Pégourié-Gonnard | 0a56c2c | 2014-01-17 21:24:04 +0100 | [diff] [blame] | 664 | *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | return mbedtls_mpi_write_binary( &ctx->z, buf, *olen ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 668 | /* |
| 669 | * Derive and export the shared secret |
| 670 | */ |
| 671 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
| 672 | unsigned char *buf, size_t blen, |
| 673 | int (*f_rng)(void *, unsigned char *, size_t), |
| 674 | void *p_rng ) |
| 675 | { |
| 676 | int restart_enabled = 0; |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 677 | ECDH_VALIDATE_RET( ctx != NULL ); |
| 678 | ECDH_VALIDATE_RET( olen != NULL ); |
| 679 | ECDH_VALIDATE_RET( buf != NULL ); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 680 | |
| 681 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 682 | restart_enabled = ctx->restart_enabled; |
| 683 | #endif |
| 684 | |
| 685 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 686 | return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng, |
| 687 | restart_enabled ) ); |
| 688 | #else |
| 689 | switch( ctx->var ) |
| 690 | { |
| 691 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
| 692 | return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf, |
| 693 | blen, f_rng, p_rng, |
| 694 | restart_enabled ) ); |
| 695 | default: |
| 696 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 697 | } |
| 698 | #endif |
| 699 | } |
| 700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | #endif /* MBEDTLS_ECDH_C */ |