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) |
| 38 | #include "config.h" |
| 39 | #else |
| 40 | #include MBEDTLS_CONFIG_FILE |
| 41 | #endif |
| 42 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 43 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 44 | |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 45 | /* |
| 46 | * Use a backward compatible ECDH context. |
| 47 | * |
| 48 | * This flag is always enabled for now and future versions might add a |
| 49 | * configuration option that conditionally undefines this flag. |
| 50 | * The configuration option in question may have a different name. |
| 51 | * |
| 52 | * Features undefining this flag, must have a warning in their description in |
| 53 | * config.h stating that the feature breaks backward compatibility. |
| 54 | */ |
| 55 | #define MBEDTLS_ECDH_LEGACY_CONTEXT |
| 56 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 61 | /** |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 62 | * Defines the source of the imported EC key. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 63 | */ |
| 64 | typedef enum |
| 65 | { |
Rose Zadik | 7375b0f | 2018-04-16 16:04:57 +0100 | [diff] [blame] | 66 | MBEDTLS_ECDH_OURS, /**< Our key. */ |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 67 | MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | } mbedtls_ecdh_side; |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 69 | |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 70 | #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
| 71 | /** |
| 72 | * Defines the ECDH implementation used. |
| 73 | * |
| 74 | * Later versions of the library may add new variants, therefore users should |
| 75 | * not make any assumptions about them. |
| 76 | */ |
| 77 | typedef enum |
| 78 | { |
| 79 | MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */ |
| 80 | MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */ |
| 81 | } mbedtls_ecdh_variant; |
| 82 | |
| 83 | /** |
| 84 | * The context used by the default ECDH implementation. |
| 85 | * |
| 86 | * Later versions might change the structure of this context, therefore users |
| 87 | * should not make any assumptions about the structure of |
| 88 | * mbedtls_ecdh_context_mbed. |
| 89 | */ |
| 90 | typedef struct mbedtls_ecdh_context_mbed |
| 91 | { |
| 92 | mbedtls_ecp_group grp; /*!< The elliptic curve used. */ |
| 93 | mbedtls_mpi d; /*!< The private key. */ |
| 94 | mbedtls_ecp_point Q; /*!< The public key. */ |
| 95 | mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */ |
| 96 | mbedtls_mpi z; /*!< The shared secret. */ |
| 97 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 98 | mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */ |
| 99 | #endif |
| 100 | } mbedtls_ecdh_context_mbed; |
| 101 | #endif |
| 102 | |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 103 | /** |
Manuel Pégourié-Gonnard | eaf55be | 2017-08-23 14:40:21 +0200 | [diff] [blame] | 104 | * |
| 105 | * \warning Performing multiple operations concurrently on the same |
| 106 | * ECDSA context is not supported; objects of this type |
| 107 | * should not be shared between multiple threads. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 108 | * \brief The ECDH context structure. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 109 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 110 | typedef struct mbedtls_ecdh_context |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 111 | { |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 112 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 113 | mbedtls_ecp_group grp; /*!< The elliptic curve used. */ |
| 114 | mbedtls_mpi d; /*!< The private key. */ |
| 115 | mbedtls_ecp_point Q; /*!< The public key. */ |
| 116 | mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */ |
| 117 | mbedtls_mpi z; /*!< The shared secret. */ |
| 118 | int point_format; /*!< The format of point export in TLS messages. */ |
| 119 | mbedtls_ecp_point Vi; /*!< The blinding value. */ |
| 120 | mbedtls_ecp_point Vf; /*!< The unblinding value. */ |
| 121 | mbedtls_mpi _d; /*!< The previous \p d. */ |
Manuel Pégourié-Gonnard | 66ba48a | 2017-04-27 11:38:26 +0200 | [diff] [blame] | 122 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 123 | int restart_enabled; /*!< The flag for restartable mode. */ |
| 124 | mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */ |
Janos Follath | c9c32f3 | 2018-08-13 15:52:45 +0100 | [diff] [blame] | 125 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 126 | #else |
| 127 | uint8_t point_format; /*!< The format of point export in TLS messages |
| 128 | as defined in RFC 4492. */ |
| 129 | mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */ |
| 130 | mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */ |
| 131 | union |
| 132 | { |
| 133 | mbedtls_ecdh_context_mbed mbed_ecdh; |
| 134 | } ctx; /*!< Implementation-specific context. The |
| 135 | context in use is specified by the \c var |
| 136 | field. */ |
| 137 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 138 | uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of |
| 139 | an alternative implementation not supporting |
| 140 | restartable mode must return |
| 141 | MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error |
| 142 | if this flag is set. */ |
| 143 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 144 | #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */ |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 145 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | mbedtls_ecdh_context; |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 148 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 149 | * \brief This function generates an ECDH keypair on an elliptic |
| 150 | * curve. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 151 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 152 | * This function performs the first of two core computations |
| 153 | * implemented during the ECDH key exchange. The second core |
| 154 | * computation is performed by mbedtls_ecdh_compute_shared(). |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 155 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 156 | * \see ecp.h |
| 157 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 158 | * \param grp The ECP group. |
| 159 | * \param d The destination MPI (private key). |
| 160 | * \param Q The destination point (public key). |
| 161 | * \param f_rng The RNG function. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 162 | * \param p_rng The RNG context. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 163 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 164 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 165 | * \return Another \c MBEDTLS_ERR_ECP_XXX or |
| 166 | * \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 167 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | 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] | 169 | int (*f_rng)(void *, unsigned char *, size_t), |
| 170 | void *p_rng ); |
| 171 | |
| 172 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 173 | * \brief This function computes the shared secret. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 174 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 175 | * This function performs the second of two core computations |
| 176 | * implemented during the ECDH key exchange. The first core |
| 177 | * computation is performed by mbedtls_ecdh_gen_public(). |
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 | * \see ecp.h |
| 180 | * |
| 181 | * \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] | 182 | * countermeasures against side-channel attacks. |
| 183 | * For more information, see mbedtls_ecp_mul(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 184 | * |
| 185 | * \param grp The ECP group. |
| 186 | * \param z The destination MPI (shared secret). |
| 187 | * \param Q The public key from another party. |
| 188 | * \param d Our secret exponent (private key). |
| 189 | * \param f_rng The RNG function. |
| 190 | * \param p_rng The RNG context. |
| 191 | * |
| 192 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 193 | * \return Another \c MBEDTLS_ERR_ECP_XXX or |
| 194 | * \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 195 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, |
| 197 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 198 | int (*f_rng)(void *, unsigned char *, size_t), |
| 199 | void *p_rng ); |
Manuel Pégourié-Gonnard | 0bad5c2 | 2013-01-26 15:30:46 +0100 | [diff] [blame] | 200 | |
| 201 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 202 | * \brief This function initializes an ECDH context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 203 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 204 | * \param ctx The ECDH context to initialize. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 205 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 207 | |
| 208 | /** |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 209 | * \brief This function sets up the ECDH context with the information |
| 210 | * given. |
| 211 | * |
| 212 | * This function should be called after mbedtls_ecdh_init() but |
| 213 | * before mbedtls_ecdh_make_params(). There is no need to call |
| 214 | * this function before mbedtls_ecdh_read_params(). |
| 215 | * |
| 216 | * This is the first function used by a TLS server for ECDHE |
| 217 | * ciphersuites. |
| 218 | * |
| 219 | * \param ctx The ECDH context to set up. |
| 220 | * \param grp_id The group id of the group to set up the context for. |
| 221 | * |
| 222 | * \return \c 0 on success. |
| 223 | */ |
| 224 | int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id ); |
| 225 | |
| 226 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 227 | * \brief This function frees a context. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 228 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 229 | * \param ctx The context to free. |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 230 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ); |
Manuel Pégourié-Gonnard | 63533e4 | 2013-02-10 14:21:04 +0100 | [diff] [blame] | 232 | |
| 233 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 234 | * \brief This function generates a public key and a TLS |
| 235 | * ServerKeyExchange payload. |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 236 | * |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 237 | * This is the second function used by a TLS server for ECDHE |
| 238 | * ciphersuites. (It is called after mbedtls_ecdh_setup().) |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 239 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 240 | * \note This function assumes that the ECP group (grp) of the |
| 241 | * \p ctx context has already been properly set, |
| 242 | * for example, using mbedtls_ecp_group_load(). |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 243 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 244 | * \see ecp.h |
| 245 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 246 | * \param ctx The ECDH context. |
| 247 | * \param olen The number of characters written. |
| 248 | * \param buf The destination buffer. |
| 249 | * \param blen The length of the destination buffer. |
| 250 | * \param f_rng The RNG function. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 251 | * \param p_rng The RNG context. |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 252 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 253 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 254 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 255 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 256 | * \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] | 257 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 258 | 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] | 259 | unsigned char *buf, size_t blen, |
| 260 | int (*f_rng)(void *, unsigned char *, size_t), |
| 261 | void *p_rng ); |
| 262 | |
| 263 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 264 | * \brief This function parses and processes a TLS ServerKeyExhange |
| 265 | * payload. |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 266 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 267 | * This is the first function used by a TLS client for ECDHE |
| 268 | * ciphersuites. |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 269 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 270 | * \see ecp.h |
| 271 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 272 | * \param ctx The ECDH context. |
| 273 | * \param buf The pointer to the start of the input buffer. |
| 274 | * \param end The address for one Byte past the end of the buffer. |
| 275 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 276 | * \return \c 0 on success. |
| 277 | * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 278 | * |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 279 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, |
Manuel Pégourié-Gonnard | 854fbd7 | 2013-02-11 20:28:55 +0100 | [diff] [blame] | 281 | const unsigned char **buf, const unsigned char *end ); |
Manuel Pégourié-Gonnard | 1372476 | 2013-02-10 15:01:54 +0100 | [diff] [blame] | 282 | |
| 283 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 284 | * \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] | 285 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 286 | * It is used by clients and servers in place of the |
| 287 | * ServerKeyEchange for static ECDH, and imports ECDH |
| 288 | * parameters from the EC key information of a certificate. |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 289 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 290 | * \see ecp.h |
| 291 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 292 | * \param ctx The ECDH context to set up. |
| 293 | * \param key The EC key to use. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 294 | * \param side Defines the source of the key: 1: Our key, or |
| 295 | * 0: The key of the peer. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 296 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 297 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 298 | * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 299 | * |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 300 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, |
| 302 | mbedtls_ecdh_side side ); |
Manuel Pégourié-Gonnard | cdff3cf | 2013-12-12 09:55:52 +0100 | [diff] [blame] | 303 | |
| 304 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 305 | * \brief This function generates a public key and a TLS |
| 306 | * ClientKeyExchange payload. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 307 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 308 | * This is the second function used by a TLS client for ECDH(E) |
| 309 | * ciphersuites. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 310 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 311 | * \see ecp.h |
| 312 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 313 | * \param ctx The ECDH context. |
| 314 | * \param olen The number of Bytes written. |
| 315 | * \param buf The destination buffer. |
| 316 | * \param blen The size of the destination buffer. |
| 317 | * \param f_rng The RNG function. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 318 | * \param p_rng The RNG context. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 319 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 320 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 321 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 322 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 323 | * \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] | 324 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | 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] | 326 | unsigned char *buf, size_t blen, |
| 327 | int (*f_rng)(void *, unsigned char *, size_t), |
| 328 | void *p_rng ); |
| 329 | |
| 330 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 331 | * \brief This function parses and processes a TLS ClientKeyExchange |
| 332 | * payload. |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 333 | * |
Janos Follath | f61e486 | 2018-10-30 11:53:25 +0000 | [diff] [blame] | 334 | * This is the third function used by a TLS server for ECDH(E) |
| 335 | * ciphersuites. (It is called after mbedtls_ecdh_setup() and |
| 336 | * mbedtls_ecdh_make_params().) |
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 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 340 | * \param ctx The ECDH context. |
| 341 | * \param buf The start of the input buffer. |
| 342 | * \param blen The length of the input buffer. |
| 343 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 344 | * \return \c 0 on success. |
| 345 | * \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] | 346 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, |
Manuel Pégourié-Gonnard | 5cceb41 | 2013-02-11 21:51:45 +0100 | [diff] [blame] | 348 | const unsigned char *buf, size_t blen ); |
| 349 | |
| 350 | /** |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 351 | * \brief This function derives and exports the shared secret. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 352 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 353 | * This is the last function used by both TLS client |
| 354 | * and servers. |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 355 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 356 | * \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] | 357 | * countermeasures against side-channel attacks. |
| 358 | * For more information, see mbedtls_ecp_mul(). |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 359 | * |
| 360 | * \see ecp.h |
| 361 | * |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 362 | * \param ctx The ECDH context. |
| 363 | * \param olen The number of Bytes written. |
| 364 | * \param buf The destination buffer. |
| 365 | * \param blen The length of the destination buffer. |
| 366 | * \param f_rng The RNG function. |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 367 | * \param p_rng The RNG context. |
Rose Zadik | de2d622 | 2018-01-25 21:57:43 +0000 | [diff] [blame] | 368 | * |
Rose Zadik | 6899328 | 2018-03-27 11:12:25 +0100 | [diff] [blame] | 369 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 370 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | c90d3b0 | 2017-04-27 10:48:29 +0200 | [diff] [blame] | 371 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 372 | * \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] | 373 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | 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] | 375 | unsigned char *buf, size_t blen, |
| 376 | int (*f_rng)(void *, unsigned char *, size_t), |
| 377 | void *p_rng ); |
Manuel Pégourié-Gonnard | 424fda5 | 2013-02-11 22:05:42 +0100 | [diff] [blame] | 378 | |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 379 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 380 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 381 | * \brief This function enables restartable EC computations for this |
| 382 | * context. (Default: disabled.) |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 383 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 384 | * \see \c mbedtls_ecp_set_max_ops() |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 385 | * |
| 386 | * \note It is not possible to safely disable restartable |
| 387 | * computations once enabled, except by free-ing the context, |
| 388 | * which cancels possible in-progress operations. |
| 389 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 390 | * \param ctx The ECDH context. |
Manuel Pégourié-Gonnard | 23e4162 | 2017-05-18 12:35:37 +0200 | [diff] [blame] | 391 | */ |
| 392 | void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ); |
| 393 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 394 | |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 395 | #ifdef __cplusplus |
| 396 | } |
| 397 | #endif |
| 398 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 399 | #endif /* ecdh.h */ |