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