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