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