Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecdh.h |
| 3 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 4 | * \brief This file contains ECDH definitions and functions. |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 5 | * |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 6 | * The Elliptic Curve Diffie-Hellman (ECDH) protocol is an anonymous |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 7 | * key agreement protocol allowing two parties to establish a shared |
| 8 | * secret over an insecure channel. Each party must have an |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 9 | * elliptic-curve public–private key pair. |
| 10 | * |
| 11 | * For more information, see <em>NIST SP 800-56A Rev. 2: Recommendation for |
| 12 | * Pair-Wise Key Establishment Schemes Using Discrete Logarithm |
| 13 | * Cryptography</em>. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 14 | */ |
| 15 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 16 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 17 | * 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] | 18 | */ |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #ifndef MBEDTLS_ECDH_H |
| 21 | #define MBEDTLS_ECDH_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 22 | #include "mbedtls/private_access.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 23 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 24 | #include "mbedtls/build_info.h" |
Ron Eldor | 9cbd1b2 | 2018-12-16 12:14:37 +0200 | [diff] [blame] | 25 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 26 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 27 | |
Thomas Daubney | 4e9fb39 | 2021-06-03 11:51:08 +0100 | [diff] [blame] | 28 | /* |
Thomas Daubney | 416c46f | 2021-05-27 15:51:44 +0100 | [diff] [blame] | 29 | * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context |
| 30 | * defined in `ecdh.h`). For most applications, the choice of format makes |
| 31 | * no difference, since all library functions can work with either format, |
| 32 | * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE. |
| 33 | |
| 34 | * The new format used when this option is disabled is smaller |
| 35 | * (56 bytes on a 32-bit platform). In future versions of the library, it |
| 36 | * will support alternative implementations of ECDH operations. |
| 37 | * The new format is incompatible with applications that access |
| 38 | * context fields directly and with restartable ECP operations. |
Thomas Daubney | 416c46f | 2021-05-27 15:51:44 +0100 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 42 | #define MBEDTLS_ECDH_LEGACY_CONTEXT |
| 43 | #else |
| 44 | #undef MBEDTLS_ECDH_LEGACY_CONTEXT |
| 45 | #endif |
| 46 | |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 47 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
Christoph M. Wintersteiger | c3cbdde | 2018-12-14 11:03:02 +0000 | [diff] [blame] | 48 | #undef MBEDTLS_ECDH_LEGACY_CONTEXT |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 49 | #include "everest/everest.h" |
| 50 | #endif |
| 51 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 56 | /** |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 57 | * Defines the source of the imported EC key. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 58 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | typedef enum { |
Rose Zadik | 7375b0f | 2018-04-16 16:04:57 +0100 | [diff] [blame] | 60 | MBEDTLS_ECDH_OURS, /**< Our key. */ |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 61 | MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | } mbedtls_ecdh_side; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 63 | |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 64 | #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 65 | /** |
| 66 | * Defines the ECDH implementation used. |
| 67 | * |
| 68 | * Later versions of the library may add new variants, therefore users should |
| 69 | * not make any assumptions about them. |
| 70 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | typedef enum { |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 72 | MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */ |
| 73 | MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */ |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 74 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 75 | MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */ |
| 76 | #endif |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 77 | } mbedtls_ecdh_variant; |
| 78 | |
| 79 | /** |
| 80 | * The context used by the default ECDH implementation. |
| 81 | * |
| 82 | * Later versions might change the structure of this context, therefore users |
| 83 | * should not make any assumptions about the structure of |
| 84 | * mbedtls_ecdh_context_mbed. |
| 85 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | typedef struct mbedtls_ecdh_context_mbed { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 87 | mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */ |
| 88 | mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */ |
| 89 | mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */ |
| 90 | mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */ |
| 91 | mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 92 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 93 | mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 94 | #endif |
| 95 | } mbedtls_ecdh_context_mbed; |
| 96 | #endif |
| 97 | |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 98 | /** |
Manuel Pégourié-Gonnard | eaf55be | 2017-08-23 14:40:21 +0200 | [diff] [blame] | 99 | * |
| 100 | * \warning Performing multiple operations concurrently on the same |
| 101 | * ECDSA context is not supported; objects of this type |
| 102 | * should not be shared between multiple threads. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 103 | * \brief The ECDH context structure. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 104 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | typedef struct mbedtls_ecdh_context { |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 106 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 107 | mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */ |
| 108 | mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */ |
| 109 | mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */ |
| 110 | mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */ |
| 111 | mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */ |
| 112 | int MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages. */ |
| 113 | mbedtls_ecp_point MBEDTLS_PRIVATE(Vi); /*!< The blinding value. */ |
| 114 | mbedtls_ecp_point MBEDTLS_PRIVATE(Vf); /*!< The unblinding value. */ |
| 115 | mbedtls_mpi MBEDTLS_PRIVATE(_d); /*!< The previous \p d. */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 116 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Mateusz Starzyk | 2abe51c | 2021-06-07 11:08:01 +0200 | [diff] [blame] | 117 | int MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. */ |
| 118 | mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 119 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 120 | #else |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 121 | uint8_t MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | as defined in RFC 4492. */ |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 123 | mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id);/*!< The elliptic curve used. */ |
| 124 | mbedtls_ecdh_variant MBEDTLS_PRIVATE(var); /*!< The ECDH implementation/structure used. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | union { |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 126 | mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 127 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 128 | mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh); |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame] | 129 | #endif |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 130 | } MBEDTLS_PRIVATE(ctx); /*!< Implementation-specific context. The |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | context in use is specified by the \c var |
| 132 | field. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 133 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Mateusz Starzyk | 363eb29 | 2021-05-19 17:32:44 +0200 | [diff] [blame] | 134 | uint8_t MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. Functions of |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | an alternative implementation not supporting |
| 136 | restartable mode must return |
| 137 | MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error |
| 138 | if this flag is set. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 139 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 140 | #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 141 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | mbedtls_ecdh_context; |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 144 | /** |
Minos Galanakis | d753738 | 2024-02-23 12:17:59 +0000 | [diff] [blame] | 145 | * \brief Return the ECP group for provided context. |
| 146 | * |
| 147 | * \note To access group specific fields, users should use |
| 148 | * `mbedtls_ecp_curve_info_from_grp_id` or |
| 149 | * `mbedtls_ecp_group_load` on the extracted `group_id`. |
| 150 | * |
| 151 | * \param ctx The ECDH context to parse. This must not be \c NULL. |
| 152 | * |
| 153 | * \return The \c mbedtls_ecp_group_id of the context. |
| 154 | */ |
| 155 | mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx); |
| 156 | |
| 157 | /** |
Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 158 | * \brief Check whether a given group can be used for ECDH. |
| 159 | * |
| 160 | * \param gid The ECP group ID to check. |
| 161 | * |
| 162 | * \return \c 1 if the group can be used, \c 0 otherwise |
| 163 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid); |
Gilles Peskine | 20b3ef3 | 2019-02-11 18:41:27 +0100 | [diff] [blame] | 165 | |
| 166 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 167 | * \brief This function generates an ECDH keypair on an elliptic |
| 168 | * curve. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 169 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 170 | * This function performs the first of two core computations |
| 171 | * implemented during the ECDH key exchange. The second core |
| 172 | * computation is performed by mbedtls_ecdh_compute_shared(). |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 173 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 174 | * \see ecp.h |
| 175 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 176 | * \param grp The ECP group to use. This must be initialized and have |
| 177 | * domain parameters loaded, for example through |
| 178 | * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group(). |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 179 | * \param d The destination MPI (private key). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 180 | * This must be initialized. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 181 | * \param Q The destination point (public key). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 182 | * This must be initialized. |
| 183 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 184 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 185 | * \c NULL in case \p f_rng doesn't need a context argument. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 186 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 187 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 188 | * \return Another \c MBEDTLS_ERR_ECP_XXX or |
| 189 | * \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 190 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 192 | int (*f_rng)(void *, unsigned char *, size_t), |
| 193 | void *p_rng); |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 194 | |
| 195 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 196 | * \brief This function computes the shared secret. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 197 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 198 | * This function performs the second of two core computations |
| 199 | * implemented during the ECDH key exchange. The first core |
| 200 | * computation is performed by mbedtls_ecdh_gen_public(). |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 201 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 202 | * \see ecp.h |
| 203 | * |
| 204 | * \note If \p f_rng is not NULL, it is used to implement |
Rose Zadik | 7375b0f | 2018-04-16 16:04:57 +0100 | [diff] [blame] | 205 | * countermeasures against side-channel attacks. |
| 206 | * For more information, see mbedtls_ecp_mul(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 207 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 208 | * \param grp The ECP group to use. This must be initialized and have |
| 209 | * domain parameters loaded, for example through |
| 210 | * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 211 | * \param z The destination MPI (shared secret). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 212 | * This must be initialized. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 213 | * \param Q The public key from another party. |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 214 | * This must be initialized. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 215 | * \param d Our secret exponent (private key). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 216 | * This must be initialized. |
Manuel Pégourié-Gonnard | 7861ecf | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 217 | * \param f_rng The RNG function to use. This must not be \c NULL. |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 218 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 219 | * \c NULL if \p f_rng is \c NULL or doesn't need a |
| 220 | * context argument. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 221 | * |
| 222 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 223 | * \return Another \c MBEDTLS_ERR_ECP_XXX or |
| 224 | * \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 225 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 227 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
| 228 | int (*f_rng)(void *, unsigned char *, size_t), |
| 229 | void *p_rng); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 230 | |
| 231 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 232 | * \brief This function initializes an ECDH context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 233 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 234 | * \param ctx The ECDH context to initialize. This must not be \c NULL. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 235 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 237 | |
| 238 | /** |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 239 | * \brief This function sets up the ECDH context with the information |
| 240 | * given. |
| 241 | * |
| 242 | * This function should be called after mbedtls_ecdh_init() but |
| 243 | * before mbedtls_ecdh_make_params(). There is no need to call |
| 244 | * this function before mbedtls_ecdh_read_params(). |
| 245 | * |
| 246 | * This is the first function used by a TLS server for ECDHE |
| 247 | * ciphersuites. |
| 248 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 249 | * \param ctx The ECDH context to set up. This must be initialized. |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 250 | * \param grp_id The group id of the group to set up the context for. |
| 251 | * |
| 252 | * \return \c 0 on success. |
| 253 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, |
| 255 | mbedtls_ecp_group_id grp_id); |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 256 | |
| 257 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 258 | * \brief This function frees a context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 259 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 260 | * \param ctx The context to free. This may be \c NULL, in which |
| 261 | * case this function does nothing. If it is not \c NULL, |
| 262 | * it must point to an initialized ECDH context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 263 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 265 | |
| 266 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 267 | * \brief This function generates an EC key pair and exports its |
| 268 | * in the format used in a TLS ServerKeyExchange handshake |
| 269 | * message. |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 270 | * |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 271 | * This is the second function used by a TLS server for ECDHE |
| 272 | * ciphersuites. (It is called after mbedtls_ecdh_setup().) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 273 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 274 | * \see ecp.h |
| 275 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 276 | * \param ctx The ECDH context to use. This must be initialized |
| 277 | * and bound to a group, for example via mbedtls_ecdh_setup(). |
| 278 | * \param olen The address at which to store the number of Bytes written. |
| 279 | * \param buf The destination buffer. This must be a writable buffer of |
| 280 | * length \p blen Bytes. |
| 281 | * \param blen The length of the destination buffer \p buf in Bytes. |
| 282 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 283 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 284 | * \c NULL in case \p f_rng doesn't need a context argument. |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 285 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 286 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 287 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 288 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 289 | * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 290 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, |
| 292 | unsigned char *buf, size_t blen, |
| 293 | int (*f_rng)(void *, unsigned char *, size_t), |
| 294 | void *p_rng); |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 295 | |
| 296 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 297 | * \brief This function parses the ECDHE parameters in a |
| 298 | * TLS ServerKeyExchange handshake message. |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 299 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 300 | * \note In a TLS handshake, this is the how the client |
| 301 | * sets up its ECDHE context from the server's public |
| 302 | * ECDHE key material. |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 303 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 304 | * \see ecp.h |
| 305 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 306 | * \param ctx The ECDHE context to use. This must be initialized. |
Hanno Becker | 60b6504 | 2018-12-17 22:59:13 +0000 | [diff] [blame] | 307 | * \param buf On input, \c *buf must be the start of the input buffer. |
| 308 | * On output, \c *buf is updated to point to the end of the |
| 309 | * data that has been read. On success, this is the first byte |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 310 | * past the end of the ServerKeyExchange parameters. |
| 311 | * On error, this is the point at which an error has been |
| 312 | * detected, which is usually not useful except to debug |
| 313 | * failures. |
| 314 | * \param end The end of the input buffer. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 315 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 316 | * \return \c 0 on success. |
| 317 | * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 318 | * |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 319 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, |
| 321 | const unsigned char **buf, |
| 322 | const unsigned char *end); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 323 | |
| 324 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 325 | * \brief This function sets up an ECDH context from an EC key. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 326 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 327 | * It is used by clients and servers in place of the |
Wenxing Hou | 848bccf | 2024-06-19 11:04:13 +0800 | [diff] [blame] | 328 | * ServerKeyExchange for static ECDH, and imports ECDH |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 329 | * parameters from the EC key information of a certificate. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 330 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 331 | * \see ecp.h |
| 332 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 333 | * \param ctx The ECDH context to set up. This must be initialized. |
| 334 | * \param key The EC key to use. This must be initialized. |
| 335 | * \param side Defines the source of the key. Possible values are: |
Hanno Becker | c81cfec | 2018-12-18 23:32:42 +0000 | [diff] [blame] | 336 | * - #MBEDTLS_ECDH_OURS: The key is ours. |
| 337 | * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 338 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 339 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 340 | * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 341 | * |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 342 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, |
| 344 | const mbedtls_ecp_keypair *key, |
| 345 | mbedtls_ecdh_side side); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 346 | |
| 347 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 348 | * \brief This function generates a public key and exports it |
| 349 | * as a TLS ClientKeyExchange payload. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 350 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 351 | * This is the second function used by a TLS client for ECDH(E) |
| 352 | * ciphersuites. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 353 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 354 | * \see ecp.h |
| 355 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 356 | * \param ctx The ECDH context to use. This must be initialized |
| 357 | * and bound to a group, the latter usually by |
| 358 | * mbedtls_ecdh_read_params(). |
| 359 | * \param olen The address at which to store the number of Bytes written. |
| 360 | * This must not be \c NULL. |
| 361 | * \param buf The destination buffer. This must be a writable buffer |
Hanno Becker | c81cfec | 2018-12-18 23:32:42 +0000 | [diff] [blame] | 362 | * of length \p blen Bytes. |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 363 | * \param blen The size of the destination buffer \p buf in Bytes. |
| 364 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 365 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 366 | * \c NULL in case \p f_rng doesn't need a context argument. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 367 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 368 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 369 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 370 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 371 | * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 372 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, |
| 374 | unsigned char *buf, size_t blen, |
| 375 | int (*f_rng)(void *, unsigned char *, size_t), |
| 376 | void *p_rng); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 377 | |
| 378 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 379 | * \brief This function parses and processes the ECDHE payload of a |
| 380 | * TLS ClientKeyExchange message. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 381 | * |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 382 | * This is the third function used by a TLS server for ECDH(E) |
| 383 | * ciphersuites. (It is called after mbedtls_ecdh_setup() and |
| 384 | * mbedtls_ecdh_make_params().) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 385 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 386 | * \see ecp.h |
| 387 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 388 | * \param ctx The ECDH context to use. This must be initialized |
| 389 | * and bound to a group, for example via mbedtls_ecdh_setup(). |
| 390 | * \param buf The pointer to the ClientKeyExchange payload. This must |
| 391 | * be a readable buffer of length \p blen Bytes. |
| 392 | * \param blen The length of the input buffer \p buf in Bytes. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 393 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 394 | * \return \c 0 on success. |
| 395 | * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 396 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, |
| 398 | const unsigned char *buf, size_t blen); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 399 | |
| 400 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 401 | * \brief This function derives and exports the shared secret. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 402 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 403 | * This is the last function used by both TLS client |
| 404 | * and servers. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 405 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 406 | * \note If \p f_rng is not NULL, it is used to implement |
Rose Zadik | 7375b0f | 2018-04-16 16:04:57 +0100 | [diff] [blame] | 407 | * countermeasures against side-channel attacks. |
| 408 | * For more information, see mbedtls_ecp_mul(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 409 | * |
| 410 | * \see ecp.h |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 411 | |
| 412 | * \param ctx The ECDH context to use. This must be initialized |
| 413 | * and have its own private key generated and the peer's |
| 414 | * public key imported. |
| 415 | * \param olen The address at which to store the total number of |
| 416 | * Bytes written on success. This must not be \c NULL. |
| 417 | * \param buf The buffer to write the generated shared key to. This |
| 418 | * must be a writable buffer of size \p blen Bytes. |
| 419 | * \param blen The length of the destination buffer \p buf in Bytes. |
Manuel Pégourié-Gonnard | 7861ecf | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 420 | * \param f_rng The RNG function to use. This must not be \c NULL. |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 421 | * \param p_rng The RNG context. This may be \c NULL if \p f_rng |
| 422 | * doesn't need a context argument. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 423 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 424 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 425 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 426 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 427 | * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 428 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, |
| 430 | unsigned char *buf, size_t blen, |
| 431 | int (*f_rng)(void *, unsigned char *, size_t), |
| 432 | void *p_rng); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 433 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 434 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 435 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 436 | * \brief This function enables restartable EC computations for this |
| 437 | * context. (Default: disabled.) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 438 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 439 | * \see \c mbedtls_ecp_set_max_ops() |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 440 | * |
| 441 | * \note It is not possible to safely disable restartable |
| 442 | * computations once enabled, except by free-ing the context, |
| 443 | * which cancels possible in-progress operations. |
| 444 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 445 | * \param ctx The ECDH context to use. This must be initialized. |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 446 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx); |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 448 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 449 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 450 | #ifdef __cplusplus |
| 451 | } |
| 452 | #endif |
| 453 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 454 | #endif /* ecdh.h */ |