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