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