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