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 | /* |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 16 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 30 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 31 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 32 | */ |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #ifndef MBEDTLS_ECDH_H |
| 35 | #define MBEDTLS_ECDH_H |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 36 | |
Ron Eldor | 9cbd1b2 | 2018-12-16 12:14:37 +0200 | [diff] [blame] | 37 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 38 | #include "mbedtls/config.h" |
Ron Eldor | 9cbd1b2 | 2018-12-16 12:14:37 +0200 | [diff] [blame] | 39 | #else |
| 40 | #include MBEDTLS_CONFIG_FILE |
| 41 | #endif |
| 42 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 43 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 44 | |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame^] | 45 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 46 | #undef MBEDTLS_ECDH_LEGACY_CONTEXT |
| 47 | #include "everest/everest.h" |
| 48 | #endif |
| 49 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 54 | /** |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 55 | * Defines the source of the imported EC key. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 56 | */ |
| 57 | typedef enum |
| 58 | { |
Rose Zadik | 7375b0f | 2018-04-16 16:04:57 +0100 | [diff] [blame] | 59 | MBEDTLS_ECDH_OURS, /**< Our key. */ |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 60 | MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | } mbedtls_ecdh_side; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 62 | |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 63 | #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 64 | /** |
| 65 | * Defines the ECDH implementation used. |
| 66 | * |
| 67 | * Later versions of the library may add new variants, therefore users should |
| 68 | * not make any assumptions about them. |
| 69 | */ |
| 70 | typedef enum |
| 71 | { |
| 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 | */ |
| 86 | typedef struct mbedtls_ecdh_context_mbed |
| 87 | { |
| 88 | mbedtls_ecp_group grp; /*!< The elliptic curve used. */ |
| 89 | mbedtls_mpi d; /*!< The private key. */ |
| 90 | mbedtls_ecp_point Q; /*!< The public key. */ |
| 91 | mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */ |
| 92 | mbedtls_mpi z; /*!< The shared secret. */ |
| 93 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 94 | mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */ |
| 95 | #endif |
| 96 | } mbedtls_ecdh_context_mbed; |
| 97 | #endif |
| 98 | |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 99 | /** |
Manuel Pégourié-Gonnard | eaf55be | 2017-08-23 14:40:21 +0200 | [diff] [blame] | 100 | * |
| 101 | * \warning Performing multiple operations concurrently on the same |
| 102 | * ECDSA context is not supported; objects of this type |
| 103 | * should not be shared between multiple threads. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 104 | * \brief The ECDH context structure. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 105 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 106 | typedef struct mbedtls_ecdh_context |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 107 | { |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 108 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 109 | mbedtls_ecp_group grp; /*!< The elliptic curve used. */ |
| 110 | mbedtls_mpi d; /*!< The private key. */ |
| 111 | mbedtls_ecp_point Q; /*!< The public key. */ |
| 112 | mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */ |
| 113 | mbedtls_mpi z; /*!< The shared secret. */ |
| 114 | int point_format; /*!< The format of point export in TLS messages. */ |
| 115 | mbedtls_ecp_point Vi; /*!< The blinding value. */ |
| 116 | mbedtls_ecp_point Vf; /*!< The unblinding value. */ |
| 117 | mbedtls_mpi _d; /*!< The previous \p d. */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 118 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 119 | int restart_enabled; /*!< The flag for restartable mode. */ |
| 120 | mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 121 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 122 | #else |
| 123 | uint8_t point_format; /*!< The format of point export in TLS messages |
| 124 | as defined in RFC 4492. */ |
| 125 | mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */ |
| 126 | mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */ |
| 127 | union |
| 128 | { |
| 129 | mbedtls_ecdh_context_mbed mbed_ecdh; |
Christoph M. Wintersteiger | de4fcf2 | 2018-10-25 12:41:04 +0100 | [diff] [blame^] | 130 | #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) |
| 131 | mbedtls_ecdh_context_everest everest_ecdh; |
| 132 | #endif |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 133 | } ctx; /*!< Implementation-specific context. The |
| 134 | context in use is specified by the \c var |
| 135 | field. */ |
| 136 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 137 | uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of |
| 138 | an alternative implementation not supporting |
| 139 | restartable mode must return |
| 140 | MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error |
| 141 | if this flag is set. */ |
| 142 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 143 | #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 144 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_ecdh_context; |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 147 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 148 | * \brief This function generates an ECDH keypair on an elliptic |
| 149 | * curve. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 150 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 151 | * This function performs the first of two core computations |
| 152 | * implemented during the ECDH key exchange. The second core |
| 153 | * computation is performed by mbedtls_ecdh_compute_shared(). |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 154 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 155 | * \see ecp.h |
| 156 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 157 | * \param grp The ECP group to use. This must be initialized and have |
| 158 | * domain parameters loaded, for example through |
| 159 | * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group(). |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 160 | * \param d The destination MPI (private key). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 161 | * This must be initialized. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 162 | * \param Q The destination point (public key). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 163 | * This must be initialized. |
| 164 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 165 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 166 | * \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] | 167 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 168 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 169 | * \return Another \c MBEDTLS_ERR_ECP_XXX or |
| 170 | * \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 171 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 173 | int (*f_rng)(void *, unsigned char *, size_t), |
| 174 | void *p_rng ); |
| 175 | |
| 176 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 177 | * \brief This function computes the shared secret. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 178 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 179 | * This function performs the second of two core computations |
| 180 | * implemented during the ECDH key exchange. The first core |
| 181 | * computation is performed by mbedtls_ecdh_gen_public(). |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 182 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 183 | * \see ecp.h |
| 184 | * |
| 185 | * \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] | 186 | * countermeasures against side-channel attacks. |
| 187 | * For more information, see mbedtls_ecp_mul(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 188 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 189 | * \param grp The ECP group to use. This must be initialized and have |
| 190 | * domain parameters loaded, for example through |
| 191 | * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 192 | * \param z The destination MPI (shared secret). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 193 | * This must be initialized. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 194 | * \param Q The public key from another party. |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 195 | * This must be initialized. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 196 | * \param d Our secret exponent (private key). |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 197 | * This must be initialized. |
| 198 | * \param f_rng The RNG function. This may be \c NULL if randomization |
| 199 | * of intermediate results during the ECP computations is |
| 200 | * not needed (discouraged). See the documentation of |
| 201 | * mbedtls_ecp_mul() for more. |
| 202 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 203 | * \c NULL if \p f_rng is \c NULL or doesn't need a |
| 204 | * context argument. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 205 | * |
| 206 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 207 | * \return Another \c MBEDTLS_ERR_ECP_XXX or |
| 208 | * \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 209 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 211 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 212 | int (*f_rng)(void *, unsigned char *, size_t), |
| 213 | void *p_rng ); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 214 | |
| 215 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 216 | * \brief This function initializes an ECDH context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 217 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 218 | * \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] | 219 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 221 | |
| 222 | /** |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 223 | * \brief This function sets up the ECDH context with the information |
| 224 | * given. |
| 225 | * |
| 226 | * This function should be called after mbedtls_ecdh_init() but |
| 227 | * before mbedtls_ecdh_make_params(). There is no need to call |
| 228 | * this function before mbedtls_ecdh_read_params(). |
| 229 | * |
| 230 | * This is the first function used by a TLS server for ECDHE |
| 231 | * ciphersuites. |
| 232 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 233 | * \param ctx The ECDH context to set up. This must be initialized. |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 234 | * \param grp_id The group id of the group to set up the context for. |
| 235 | * |
| 236 | * \return \c 0 on success. |
| 237 | */ |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 238 | int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, |
| 239 | mbedtls_ecp_group_id grp_id ); |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 240 | |
| 241 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 242 | * \brief This function frees a context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 243 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 244 | * \param ctx The context to free. This may be \c NULL, in which |
| 245 | * case this function does nothing. If it is not \c NULL, |
| 246 | * it must point to an initialized ECDH context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 247 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 249 | |
| 250 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 251 | * \brief This function generates an EC key pair and exports its |
| 252 | * in the format used in a TLS ServerKeyExchange handshake |
| 253 | * message. |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 254 | * |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 255 | * This is the second function used by a TLS server for ECDHE |
| 256 | * ciphersuites. (It is called after mbedtls_ecdh_setup().) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 257 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 258 | * \see ecp.h |
| 259 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 260 | * \param ctx The ECDH context to use. This must be initialized |
| 261 | * and bound to a group, for example via mbedtls_ecdh_setup(). |
| 262 | * \param olen The address at which to store the number of Bytes written. |
| 263 | * \param buf The destination buffer. This must be a writable buffer of |
| 264 | * length \p blen Bytes. |
| 265 | * \param blen The length of the destination buffer \p buf in Bytes. |
| 266 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 267 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 268 | * \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] | 269 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 270 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 271 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 272 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 273 | * \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] | 274 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 276 | unsigned char *buf, size_t blen, |
| 277 | int (*f_rng)(void *, unsigned char *, size_t), |
| 278 | void *p_rng ); |
| 279 | |
| 280 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 281 | * \brief This function parses the ECDHE parameters in a |
| 282 | * TLS ServerKeyExchange handshake message. |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 283 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 284 | * \note In a TLS handshake, this is the how the client |
| 285 | * sets up its ECDHE context from the server's public |
| 286 | * ECDHE key material. |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 287 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 288 | * \see ecp.h |
| 289 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 290 | * \param ctx The ECDHE context to use. This must be initialized. |
Hanno Becker | 60b6504 | 2018-12-17 22:59:13 +0000 | [diff] [blame] | 291 | * \param buf On input, \c *buf must be the start of the input buffer. |
| 292 | * On output, \c *buf is updated to point to the end of the |
| 293 | * data that has been read. On success, this is the first byte |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 294 | * past the end of the ServerKeyExchange parameters. |
| 295 | * On error, this is the point at which an error has been |
| 296 | * detected, which is usually not useful except to debug |
| 297 | * failures. |
| 298 | * \param end The end of the input buffer. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 299 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 300 | * \return \c 0 on success. |
| 301 | * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 302 | * |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 303 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 305 | const unsigned char **buf, |
| 306 | const unsigned char *end ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 307 | |
| 308 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 309 | * \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] | 310 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 311 | * It is used by clients and servers in place of the |
| 312 | * ServerKeyEchange for static ECDH, and imports ECDH |
| 313 | * parameters from the EC key information of a certificate. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 314 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 315 | * \see ecp.h |
| 316 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 317 | * \param ctx The ECDH context to set up. This must be initialized. |
| 318 | * \param key The EC key to use. This must be initialized. |
| 319 | * \param side Defines the source of the key. Possible values are: |
Hanno Becker | c81cfec | 2018-12-18 23:32:42 +0000 | [diff] [blame] | 320 | * - #MBEDTLS_ECDH_OURS: The key is ours. |
| 321 | * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 322 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 323 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 324 | * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 325 | * |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 326 | */ |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 327 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, |
| 328 | const mbedtls_ecp_keypair *key, |
| 329 | mbedtls_ecdh_side side ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 330 | |
| 331 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 332 | * \brief This function generates a public key and exports it |
| 333 | * as a TLS ClientKeyExchange payload. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 334 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 335 | * This is the second function used by a TLS client for ECDH(E) |
| 336 | * ciphersuites. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 337 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 338 | * \see ecp.h |
| 339 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 340 | * \param ctx The ECDH context to use. This must be initialized |
| 341 | * and bound to a group, the latter usually by |
| 342 | * mbedtls_ecdh_read_params(). |
| 343 | * \param olen The address at which to store the number of Bytes written. |
| 344 | * This must not be \c NULL. |
| 345 | * \param buf The destination buffer. This must be a writable buffer |
Hanno Becker | c81cfec | 2018-12-18 23:32:42 +0000 | [diff] [blame] | 346 | * of length \p blen Bytes. |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 347 | * \param blen The size of the destination buffer \p buf in Bytes. |
| 348 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 349 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 350 | * \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] | 351 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 352 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 353 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 354 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 355 | * \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] | 356 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 358 | unsigned char *buf, size_t blen, |
| 359 | int (*f_rng)(void *, unsigned char *, size_t), |
| 360 | void *p_rng ); |
| 361 | |
| 362 | /** |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 363 | * \brief This function parses and processes the ECDHE payload of a |
| 364 | * TLS ClientKeyExchange message. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 365 | * |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 366 | * This is the third function used by a TLS server for ECDH(E) |
| 367 | * ciphersuites. (It is called after mbedtls_ecdh_setup() and |
| 368 | * mbedtls_ecdh_make_params().) |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 369 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 370 | * \see ecp.h |
| 371 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 372 | * \param ctx The ECDH context to use. This must be initialized |
| 373 | * and bound to a group, for example via mbedtls_ecdh_setup(). |
| 374 | * \param buf The pointer to the ClientKeyExchange payload. This must |
| 375 | * be a readable buffer of length \p blen Bytes. |
| 376 | * \param blen The length of the input buffer \p buf in Bytes. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 377 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 378 | * \return \c 0 on success. |
| 379 | * \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] | 380 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 382 | const unsigned char *buf, size_t blen ); |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 383 | |
| 384 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 385 | * \brief This function derives and exports the shared secret. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 386 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 387 | * This is the last function used by both TLS client |
| 388 | * and servers. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 389 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 390 | * \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] | 391 | * countermeasures against side-channel attacks. |
| 392 | * For more information, see mbedtls_ecp_mul(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 393 | * |
| 394 | * \see ecp.h |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 395 | |
| 396 | * \param ctx The ECDH context to use. This must be initialized |
| 397 | * and have its own private key generated and the peer's |
| 398 | * public key imported. |
| 399 | * \param olen The address at which to store the total number of |
| 400 | * Bytes written on success. This must not be \c NULL. |
| 401 | * \param buf The buffer to write the generated shared key to. This |
| 402 | * must be a writable buffer of size \p blen Bytes. |
| 403 | * \param blen The length of the destination buffer \p buf in Bytes. |
| 404 | * \param f_rng The RNG function, for blinding purposes. This may |
| 405 | * b \c NULL if blinding isn't needed. |
| 406 | * \param p_rng The RNG context. This may be \c NULL if \p f_rng |
| 407 | * doesn't need a context argument. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 408 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 409 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 410 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 411 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 412 | * \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] | 413 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 415 | unsigned char *buf, size_t blen, |
| 416 | int (*f_rng)(void *, unsigned char *, size_t), |
| 417 | void *p_rng ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 418 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 419 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 420 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 421 | * \brief This function enables restartable EC computations for this |
| 422 | * context. (Default: disabled.) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 423 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 424 | * \see \c mbedtls_ecp_set_max_ops() |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 425 | * |
| 426 | * \note It is not possible to safely disable restartable |
| 427 | * computations once enabled, except by free-ing the context, |
| 428 | * which cancels possible in-progress operations. |
| 429 | * |
Hanno Becker | e77ef2a | 2018-12-17 18:10:43 +0000 | [diff] [blame] | 430 | * \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] | 431 | */ |
| 432 | void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ); |
| 433 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 434 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 435 | #ifdef __cplusplus |
| 436 | } |
| 437 | #endif |
| 438 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 439 | #endif /* ecdh.h */ |