Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve Diffie-Hellman |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | */ |
| 19 | |
| 20 | /* |
| 21 | * References: |
| 22 | * |
| 23 | * 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] | 24 | * RFC 4492 |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 25 | */ |
| 26 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 27 | #include "common.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_ECDH_C) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/ecdh.h" |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 32 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 33 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 34 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 37 | /* Parameter validation macros based on platform_util.h */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 38 | #define ECDH_VALIDATE_RET(cond) \ |
| 39 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA) |
| 40 | #define ECDH_VALIDATE(cond) \ |
| 41 | MBEDTLS_INTERNAL_VALIDATE(cond) |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 42 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 43 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 44 | typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed; |
| 45 | #endif |
| 46 | |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 47 | static mbedtls_ecp_group_id mbedtls_ecdh_grp_id( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 48 | const mbedtls_ecdh_context *ctx) |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 49 | { |
| 50 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 51 | return ctx->grp.id; |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 52 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 53 | return ctx->grp_id; |
Gilles Peskine | 3081629 | 2019-02-22 12:31:25 +0100 | [diff] [blame] | 54 | #endif |
| 55 | } |
| 56 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 57 | int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid) |
Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 58 | { |
| 59 | /* At this time, all groups support ECDH. */ |
| 60 | (void) gid; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 61 | return 1; |
Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 64 | #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 65 | /* |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 66 | * Generate public key (restartable version) |
Manuel Pégourié-Gonnard | c0edc96 | 2018-10-16 10:38:19 +0200 | [diff] [blame] | 67 | * |
| 68 | * 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] | 69 | * 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] | 70 | * 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] | 71 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 72 | static int ecdh_gen_public_restartable(mbedtls_ecp_group *grp, |
| 73 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 74 | int (*f_rng)(void *, unsigned char *, size_t), |
| 75 | void *p_rng, |
| 76 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 77 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 78 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 79 | |
David Horstmann | d209197 | 2022-10-06 19:11:28 +0100 | [diff] [blame] | 80 | int restarting = 0; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 81 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 82 | restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 83 | #endif |
David Horstmann | d209197 | 2022-10-06 19:11:28 +0100 | [diff] [blame] | 84 | /* If multiplication is in progress, we already generated a privkey */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 85 | if (!restarting) { |
| 86 | MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng)); |
| 87 | } |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 88 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 89 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, Q, d, &grp->G, |
| 90 | f_rng, p_rng, rs_ctx)); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 91 | |
| 92 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 93 | return ret; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Generate public key |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 98 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 99 | int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 100 | int (*f_rng)(void *, unsigned char *, size_t), |
| 101 | void *p_rng) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 102 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 103 | ECDH_VALIDATE_RET(grp != NULL); |
| 104 | ECDH_VALIDATE_RET(d != NULL); |
| 105 | ECDH_VALIDATE_RET(Q != NULL); |
| 106 | ECDH_VALIDATE_RET(f_rng != NULL); |
| 107 | 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] | 108 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 109 | #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */ |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 110 | |
Ron Eldor | a84c1cb | 2017-10-10 19:04:27 +0300 | [diff] [blame] | 111 | #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 112 | /* |
| 113 | * Compute shared secret (SEC1 3.3.1) |
| 114 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 115 | static int ecdh_compute_shared_restartable(mbedtls_ecp_group *grp, |
| 116 | mbedtls_mpi *z, |
| 117 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
| 118 | int (*f_rng)(void *, unsigned char *, size_t), |
| 119 | void *p_rng, |
| 120 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 121 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 122 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | mbedtls_ecp_point P; |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 125 | mbedtls_ecp_point_init(&P); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 126 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 127 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &P, d, Q, |
| 128 | f_rng, p_rng, rs_ctx)); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 129 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 130 | if (mbedtls_ecp_is_zero(&P)) { |
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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 138 | mbedtls_ecp_point_free(&P); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 139 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 140 | return ret; |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 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 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 150 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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); |
| 155 | return ecdh_compute_shared_restartable(grp, z, Q, d, |
| 156 | f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 169 | mbedtls_ecp_restart_init(&ctx->rs); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 176 | void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 177 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 178 | ECDH_VALIDATE(ctx != NULL); |
Hanno Becker | 91796d7 | 2018-12-17 18:10:51 +0000 | [diff] [blame] | 179 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 180 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 185 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 186 | memset(ctx, 0, sizeof(mbedtls_ecdh_context)); |
Christoph M. Wintersteiger | d8c45d5 | 2019-02-20 17:16:53 +0000 | [diff] [blame] | 187 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 199 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 201 | ret = mbedtls_ecp_group_load(&ctx->grp, grp_id); |
| 202 | if (ret != 0) { |
| 203 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 206 | return 0; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 210 | * Setup context |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 211 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 212 | 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] | 213 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 214 | ECDH_VALIDATE_RET(ctx != NULL); |
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 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 217 | return ecdh_setup_internal(ctx, grp_id); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 218 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 219 | switch (grp_id) { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 220 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
Christoph M. Wintersteiger | 3ff60bc | 2019-02-15 12:59:59 +0000 | [diff] [blame] | 221 | case MBEDTLS_ECP_DP_CURVE25519: |
| 222 | ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED; |
| 223 | ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST; |
| 224 | ctx->grp_id = grp_id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 225 | return mbedtls_everest_setup(&ctx->ctx.everest_ecdh, grp_id); |
Christoph M. Wintersteiger | 78c9c46 | 2018-12-06 17:16:32 +0000 | [diff] [blame] | 226 | #endif |
Christoph M. Wintersteiger | 3ff60bc | 2019-02-15 12:59:59 +0000 | [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; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 231 | ecdh_init_internal(&ctx->ctx.mbed_ecdh); |
| 232 | return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | } |
| 236 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 237 | static void ecdh_free_internal(mbedtls_ecdh_context_mbed *ctx) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 238 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 239 | mbedtls_ecp_group_free(&ctx->grp); |
| 240 | mbedtls_mpi_free(&ctx->d); |
| 241 | mbedtls_ecp_point_free(&ctx->Q); |
| 242 | mbedtls_ecp_point_free(&ctx->Qp); |
| 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) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 246 | mbedtls_ecp_restart_free(&ctx->rs); |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 254 | void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 255 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 265 | void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 266 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 267 | if (ctx == NULL) { |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 268 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 269 | } |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 270 | |
| 271 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 272 | mbedtls_ecp_point_free(&ctx->Vi); |
| 273 | mbedtls_ecp_point_free(&ctx->Vf); |
| 274 | mbedtls_mpi_free(&ctx->_d); |
| 275 | ecdh_free_internal(ctx); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 276 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 277 | switch (ctx->var) { |
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: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 280 | mbedtls_everest_free(&ctx->ctx.everest_ecdh); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 281 | break; |
| 282 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 283 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 284 | ecdh_free_internal(&ctx->ctx.mbed_ecdh); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 305 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 311 | if (ctx->grp.pbits == 0) { |
| 312 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 313 | } |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 314 | |
Ron Eldor | 19779c4 | 2018-11-05 16:58:13 +0200 | [diff] [blame] | 315 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 316 | if (restart_enabled) { |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 317 | rs_ctx = &ctx->rs; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 318 | } |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 319 | #else |
| 320 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 321 | #endif |
| 322 | |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 323 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 324 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 325 | if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q, |
| 326 | f_rng, p_rng, rs_ctx)) != 0) { |
| 327 | return ret; |
| 328 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 329 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 330 | if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q, |
| 331 | f_rng, p_rng)) != 0) { |
| 332 | return ret; |
| 333 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 334 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 335 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 336 | if ((ret = mbedtls_ecp_tls_write_group(&ctx->grp, &grp_len, buf, |
| 337 | blen)) != 0) { |
| 338 | return ret; |
| 339 | } |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 340 | |
| 341 | buf += grp_len; |
| 342 | blen -= grp_len; |
| 343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 344 | if ((ret = mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format, |
| 345 | &pt_len, buf, blen)) != 0) { |
| 346 | return ret; |
| 347 | } |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 348 | |
| 349 | *olen = grp_len + pt_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 350 | return 0; |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 353 | /* |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 354 | * Setup and write the ServerKeyExchange parameters (RFC 4492) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 355 | * struct { |
| 356 | * ECParameters curve_params; |
| 357 | * ECPoint public; |
| 358 | * } ServerECDHParams; |
| 359 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 360 | int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, |
| 361 | unsigned char *buf, size_t blen, |
| 362 | int (*f_rng)(void *, unsigned char *, size_t), |
| 363 | void *p_rng) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 364 | { |
| 365 | int restart_enabled = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 366 | ECDH_VALIDATE_RET(ctx != NULL); |
| 367 | ECDH_VALIDATE_RET(olen != NULL); |
| 368 | ECDH_VALIDATE_RET(buf != NULL); |
| 369 | ECDH_VALIDATE_RET(f_rng != NULL); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 370 | |
| 371 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 372 | restart_enabled = ctx->restart_enabled; |
| 373 | #else |
| 374 | (void) restart_enabled; |
| 375 | #endif |
| 376 | |
| 377 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 378 | return ecdh_make_params_internal(ctx, olen, ctx->point_format, buf, blen, |
| 379 | f_rng, p_rng, restart_enabled); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 380 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 381 | switch (ctx->var) { |
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: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 392 | default: |
| 393 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 394 | } |
| 395 | #endif |
| 396 | } |
| 397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 398 | static int ecdh_read_params_internal(mbedtls_ecdh_context_mbed *ctx, |
| 399 | const unsigned char **buf, |
| 400 | const unsigned char *end) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 401 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 402 | return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf, |
| 403 | end - *buf); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* |
bootstrap-prime | 7ef96ea | 2022-05-18 14:08:33 -0400 | [diff] [blame] | 407 | * Read the ServerKeyExchange parameters (RFC 4492) |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 408 | * struct { |
| 409 | * ECParameters curve_params; |
| 410 | * ECPoint public; |
| 411 | * } ServerECDHParams; |
| 412 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 413 | int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, |
| 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 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 417 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 418 | mbedtls_ecp_group_id grp_id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 424 | if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, end - *buf)) |
| 425 | != 0) { |
| 426 | return ret; |
| 427 | } |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 429 | if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) { |
| 430 | return ret; |
| 431 | } |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 432 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 433 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 434 | return ecdh_read_params_internal(ctx, buf, end); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 435 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 436 | switch (ctx->var) { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 437 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 438 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 439 | return mbedtls_everest_read_params(&ctx->ctx.everest_ecdh, |
| 440 | buf, end); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 441 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 442 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 443 | return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh, |
| 444 | buf, end); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 445 | default: |
| 446 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 447 | } |
| 448 | #endif |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 449 | } |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 450 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 451 | static int ecdh_get_params_internal(mbedtls_ecdh_context_mbed *ctx, |
| 452 | const mbedtls_ecp_keypair *key, |
| 453 | mbedtls_ecdh_side side) |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 454 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 455 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 456 | |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 457 | /* If it's not our key, just import the public part as Qp */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 458 | if (side == MBEDTLS_ECDH_THEIRS) { |
| 459 | return mbedtls_ecp_copy(&ctx->Qp, &key->Q); |
| 460 | } |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 461 | |
| 462 | /* Our key: import public (as Q) and private parts */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 463 | if (side != MBEDTLS_ECDH_OURS) { |
| 464 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 465 | } |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 466 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 467 | if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 || |
| 468 | (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) { |
| 469 | return ret; |
| 470 | } |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 471 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 472 | return 0; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 476 | * Get parameters from a keypair |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 477 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 478 | int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, |
| 479 | const mbedtls_ecp_keypair *key, |
| 480 | mbedtls_ecdh_side side) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 481 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 482 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 483 | ECDH_VALIDATE_RET(ctx != NULL); |
| 484 | ECDH_VALIDATE_RET(key != NULL); |
| 485 | ECDH_VALIDATE_RET(side == MBEDTLS_ECDH_OURS || |
| 486 | side == MBEDTLS_ECDH_THEIRS); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 487 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 488 | if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) { |
Gilles Peskine | 0b1b71d | 2018-11-07 22:10:59 +0100 | [diff] [blame] | 489 | /* This is the first call to get_params(). Set up the context |
| 490 | * for use with the group. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 491 | if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) { |
| 492 | return ret; |
| 493 | } |
| 494 | } else { |
Gilles Peskine | 0b1b71d | 2018-11-07 22:10:59 +0100 | [diff] [blame] | 495 | /* This is not the first call to get_params(). Check that the |
| 496 | * current key's group is the same as the context's, which was set |
| 497 | * from the first key's group. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 498 | if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) { |
| 499 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 500 | } |
Gilles Peskine | 0b1b71d | 2018-11-07 22:10:59 +0100 | [diff] [blame] | 501 | } |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 502 | |
| 503 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 504 | return ecdh_get_params_internal(ctx, key, side); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 505 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 506 | switch (ctx->var) { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 507 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 508 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 509 | { |
Christoph M. Wintersteiger | 2e724a1 | 2019-01-07 14:19:41 +0000 | [diff] [blame] | 510 | mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ? |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 511 | MBEDTLS_EVEREST_ECDH_OURS : |
| 512 | MBEDTLS_EVEREST_ECDH_THEIRS; |
| 513 | return mbedtls_everest_get_params(&ctx->ctx.everest_ecdh, |
| 514 | key, s); |
Christoph M. Wintersteiger | 4936beb | 2018-12-12 17:26:41 +0000 | [diff] [blame] | 515 | } |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 516 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 517 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 518 | return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh, |
| 519 | key, side); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 520 | default: |
| 521 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 522 | } |
| 523 | #endif |
| 524 | } |
| 525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 526 | static int ecdh_make_public_internal(mbedtls_ecdh_context_mbed *ctx, |
| 527 | size_t *olen, int point_format, |
| 528 | unsigned char *buf, size_t blen, |
| 529 | int (*f_rng)(void *, |
| 530 | unsigned char *, |
| 531 | size_t), |
| 532 | void *p_rng, |
| 533 | int restart_enabled) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 534 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 535 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 536 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 537 | mbedtls_ecp_restart_ctx *rs_ctx = NULL; |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 538 | #endif |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 539 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 540 | if (ctx->grp.pbits == 0) { |
| 541 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 542 | } |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 543 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 544 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 545 | if (restart_enabled) { |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 546 | rs_ctx = &ctx->rs; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 547 | } |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 548 | #else |
| 549 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 550 | #endif |
| 551 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 552 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 553 | if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q, |
| 554 | f_rng, p_rng, rs_ctx)) != 0) { |
| 555 | return ret; |
| 556 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 557 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 558 | if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q, |
| 559 | f_rng, p_rng)) != 0) { |
| 560 | return ret; |
| 561 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 562 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 563 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 577 | ECDH_VALIDATE_RET(ctx != NULL); |
| 578 | ECDH_VALIDATE_RET(olen != NULL); |
| 579 | ECDH_VALIDATE_RET(buf != NULL); |
| 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) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 587 | return ecdh_make_public_internal(ctx, olen, ctx->point_format, buf, blen, |
| 588 | f_rng, p_rng, restart_enabled); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 589 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 590 | switch (ctx->var) { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 591 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 592 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 593 | return mbedtls_everest_make_public(&ctx->ctx.everest_ecdh, olen, |
| 594 | buf, blen, f_rng, p_rng); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 595 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 596 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 597 | return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen, |
| 598 | ctx->point_format, buf, blen, |
| 599 | f_rng, p_rng, |
| 600 | restart_enabled); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 601 | default: |
| 602 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 603 | } |
| 604 | #endif |
| 605 | } |
| 606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 607 | static int ecdh_read_public_internal(mbedtls_ecdh_context_mbed *ctx, |
| 608 | const unsigned char *buf, size_t blen) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 609 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 610 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 611 | const unsigned char *p = buf; |
| 612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 613 | if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p, |
| 614 | blen)) != 0) { |
| 615 | return ret; |
| 616 | } |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 617 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 618 | if ((size_t) (p - buf) != blen) { |
| 619 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 620 | } |
Manuel Pégourié-Gonnard | 969ccc6 | 2014-03-26 19:53:25 +0100 | [diff] [blame] | 621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 622 | return 0; |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 625 | /* |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 626 | * Parse and import the client's public value |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 627 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 628 | int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, |
| 629 | const unsigned char *buf, size_t blen) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 630 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 631 | ECDH_VALIDATE_RET(ctx != NULL); |
| 632 | ECDH_VALIDATE_RET(buf != NULL); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 633 | |
| 634 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 635 | return ecdh_read_public_internal(ctx, buf, blen); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 636 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 637 | switch (ctx->var) { |
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: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 644 | return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh, |
| 645 | buf, blen); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 646 | default: |
| 647 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 648 | } |
| 649 | #endif |
| 650 | } |
| 651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 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 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 661 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 666 | if (ctx == NULL || ctx->grp.pbits == 0) { |
| 667 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 668 | } |
Manuel Pégourié-Gonnard | f35b739 | 2013-02-11 22:12:39 +0100 | [diff] [blame] | 669 | |
Ron Eldor | b430d9f | 2018-11-05 17:18:29 +0200 | [diff] [blame] | 670 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 671 | if (restart_enabled) { |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 672 | rs_ctx = &ctx->rs; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 673 | } |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 674 | #else |
| 675 | (void) restart_enabled; |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 676 | #endif |
| 677 | |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 678 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 679 | if ((ret = ecdh_compute_shared_restartable(&ctx->grp, &ctx->z, &ctx->Qp, |
| 680 | &ctx->d, f_rng, p_rng, |
| 681 | rs_ctx)) != 0) { |
| 682 | return ret; |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 683 | } |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 684 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 685 | if ((ret = mbedtls_ecdh_compute_shared(&ctx->grp, &ctx->z, &ctx->Qp, |
| 686 | &ctx->d, f_rng, p_rng)) != 0) { |
| 687 | return ret; |
Ron Eldor | 2981d8f | 2018-11-05 18:07:10 +0200 | [diff] [blame] | 688 | } |
| 689 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 691 | if (mbedtls_mpi_size(&ctx->z) > blen) { |
| 692 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 693 | } |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 694 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 695 | *olen = ctx->grp.pbits / 8 + ((ctx->grp.pbits % 8) != 0); |
Janos Follath | ab0f71a | 2019-02-20 10:48:49 +0000 | [diff] [blame] | 696 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 697 | if (mbedtls_ecp_get_type(&ctx->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 698 | return mbedtls_mpi_write_binary_le(&ctx->z, buf, *olen); |
| 699 | } |
Janos Follath | ab0f71a | 2019-02-20 10:48:49 +0000 | [diff] [blame] | 700 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 701 | return mbedtls_mpi_write_binary(&ctx->z, buf, *olen); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 704 | /* |
| 705 | * Derive and export the shared secret |
| 706 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 707 | int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, |
| 708 | unsigned char *buf, size_t blen, |
| 709 | int (*f_rng)(void *, unsigned char *, size_t), |
| 710 | void *p_rng) |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 711 | { |
| 712 | int restart_enabled = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 713 | ECDH_VALIDATE_RET(ctx != NULL); |
| 714 | ECDH_VALIDATE_RET(olen != NULL); |
| 715 | ECDH_VALIDATE_RET(buf != NULL); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 716 | |
| 717 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 718 | restart_enabled = ctx->restart_enabled; |
| 719 | #endif |
| 720 | |
| 721 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 722 | return ecdh_calc_secret_internal(ctx, olen, buf, blen, f_rng, p_rng, |
| 723 | restart_enabled); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 724 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 725 | switch (ctx->var) { |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 726 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 727 | case MBEDTLS_ECDH_VARIANT_EVEREST: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 728 | return mbedtls_everest_calc_secret(&ctx->ctx.everest_ecdh, olen, |
| 729 | buf, blen, f_rng, p_rng); |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 730 | #endif |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 731 | case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 732 | return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf, |
| 733 | blen, f_rng, p_rng, |
| 734 | restart_enabled); |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 735 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 736 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Janos Follath | 5a3e1bf | 2018-08-13 15:54:22 +0100 | [diff] [blame] | 737 | } |
| 738 | #endif |
| 739 | } |
| 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | #endif /* MBEDTLS_ECDH_C */ |