Mbed OS Reference
Loading...
Searching...
No Matches
ecdh.h
Go to the documentation of this file.
1/**
2 * \file ecdh.h
3 *
4 * \brief This file contains ECDH definitions and functions.
5 *
6 * The Elliptic Curve Diffie-Hellman (ECDH) protocol is an anonymous
7 * key agreement protocol allowing two parties to establish a shared
8 * secret over an insecure channel. Each party must have an
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>.
14 */
15/*
16 * Copyright The Mbed TLS Contributors
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.
30 */
31
32#ifndef MBEDTLS_ECDH_H
33#define MBEDTLS_ECDH_H
34
35#if !defined(MBEDTLS_CONFIG_FILE)
36#include "mbedtls/config.h"
37#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
41#include "mbedtls/ecp.h"
42
43#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
44#undef MBEDTLS_ECDH_LEGACY_CONTEXT
45#include "everest/everest.h"
46#endif
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/**
53* \addtogroup mbedtls
54* \{
55* \defgroup mbedtls_ecdh_module Elliptic Curve Diffie-Hellman
56* \{
57*/
58
59
60/**
61 * Defines the source of the imported EC key.
62 */
63typedef enum
64{
65 MBEDTLS_ECDH_OURS, /**< Our key. */
66 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
68
69#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
70/**
71 * Defines the ECDH implementation used.
72 *
73 * Later versions of the library may add new variants, therefore users should
74 * not make any assumptions about them.
75 */
76typedef enum
77{
78 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
79 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
80#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
81 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
82#endif
84
85/**
86 * The context used by the default ECDH implementation.
87 *
88 * Later versions might change the structure of this context, therefore users
89 * should not make any assumptions about the structure of
90 * mbedtls_ecdh_context_mbed.
91 */
93{
94 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
95 mbedtls_mpi d; /*!< The private key. */
96 mbedtls_ecp_point Q; /*!< The public key. */
97 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
98 mbedtls_mpi z; /*!< The shared secret. */
99#if defined(MBEDTLS_ECP_RESTARTABLE)
100 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
101#endif
103#endif
104
105/**
106 *
107 * \warning Performing multiple operations concurrently on the same
108 * ECDSA context is not supported; objects of this type
109 * should not be shared between multiple threads.
110 * \brief The ECDH context structure.
111 */
113{
114#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
115 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
116 mbedtls_mpi d; /*!< The private key. */
117 mbedtls_ecp_point Q; /*!< The public key. */
118 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
119 mbedtls_mpi z; /*!< The shared secret. */
120 int point_format; /*!< The format of point export in TLS messages. */
121 mbedtls_ecp_point Vi; /*!< The blinding value. */
122 mbedtls_ecp_point Vf; /*!< The unblinding value. */
123 mbedtls_mpi _d; /*!< The previous \p d. */
124#if defined(MBEDTLS_ECP_RESTARTABLE)
125 int restart_enabled; /*!< The flag for restartable mode. */
126 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
127#endif /* MBEDTLS_ECP_RESTARTABLE */
128#else
129 uint8_t point_format; /*!< The format of point export in TLS messages
130 as defined in RFC 4492. */
131 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
132 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
133 union
134 {
136#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
137 mbedtls_ecdh_context_everest everest_ecdh;
138#endif
139 } ctx; /*!< Implementation-specific context. The
140 context in use is specified by the \c var
141 field. */
142#if defined(MBEDTLS_ECP_RESTARTABLE)
143 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
144 an alternative implementation not supporting
145 restartable mode must return
146 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
147 if this flag is set. */
148#endif /* MBEDTLS_ECP_RESTARTABLE */
149#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
150}
152
153/**
154 * \brief Check whether a given group can be used for ECDH.
155 *
156 * \param gid The ECP group ID to check.
157 *
158 * \return \c 1 if the group can be used, \c 0 otherwise
159 */
161
162/**
163 * \brief This function generates an ECDH keypair on an elliptic
164 * curve.
165 *
166 * This function performs the first of two core computations
167 * implemented during the ECDH key exchange. The second core
168 * computation is performed by mbedtls_ecdh_compute_shared().
169 *
170 * \see ecp.h
171 *
172 * \param grp The ECP group to use. This must be initialized and have
173 * domain parameters loaded, for example through
174 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
175 * \param d The destination MPI (private key).
176 * This must be initialized.
177 * \param Q The destination point (public key).
178 * This must be initialized.
179 * \param f_rng The RNG function to use. This must not be \c NULL.
180 * \param p_rng The RNG context to be passed to \p f_rng. This may be
181 * \c NULL in case \p f_rng doesn't need a context argument.
182 *
183 * \return \c 0 on success.
184 * \return Another \c MBEDTLS_ERR_ECP_XXX or
185 * \c MBEDTLS_MPI_XXX error code on failure.
186 */
188 int (*f_rng)(void *, unsigned char *, size_t),
189 void *p_rng );
190
191/**
192 * \brief This function computes the shared secret.
193 *
194 * This function performs the second of two core computations
195 * implemented during the ECDH key exchange. The first core
196 * computation is performed by mbedtls_ecdh_gen_public().
197 *
198 * \see ecp.h
199 *
200 * \note If \p f_rng is not NULL, it is used to implement
201 * countermeasures against side-channel attacks.
202 * For more information, see mbedtls_ecp_mul().
203 *
204 * \param grp The ECP group to use. This must be initialized and have
205 * domain parameters loaded, for example through
206 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
207 * \param z The destination MPI (shared secret).
208 * This must be initialized.
209 * \param Q The public key from another party.
210 * This must be initialized.
211 * \param d Our secret exponent (private key).
212 * This must be initialized.
213 * \param f_rng The RNG function. This may be \c NULL if randomization
214 * of intermediate results during the ECP computations is
215 * not needed (discouraged). See the documentation of
216 * mbedtls_ecp_mul() for more.
217 * \param p_rng The RNG context to be passed to \p f_rng. This may be
218 * \c NULL if \p f_rng is \c NULL or doesn't need a
219 * context argument.
220 *
221 * \return \c 0 on success.
222 * \return Another \c MBEDTLS_ERR_ECP_XXX or
223 * \c MBEDTLS_MPI_XXX error code on failure.
224 */
226 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
227 int (*f_rng)(void *, unsigned char *, size_t),
228 void *p_rng );
229
230/**
231 * \brief This function initializes an ECDH context.
232 *
233 * \param ctx The ECDH context to initialize. This must not be \c NULL.
234 */
236
237/**
238 * \brief This function sets up the ECDH context with the information
239 * given.
240 *
241 * This function should be called after mbedtls_ecdh_init() but
242 * before mbedtls_ecdh_make_params(). There is no need to call
243 * this function before mbedtls_ecdh_read_params().
244 *
245 * This is the first function used by a TLS server for ECDHE
246 * ciphersuites.
247 *
248 * \param ctx The ECDH context to set up. This must be initialized.
249 * \param grp_id The group id of the group to set up the context for.
250 *
251 * \return \c 0 on success.
252 */
254 mbedtls_ecp_group_id grp_id );
255
256/**
257 * \brief This function frees a context.
258 *
259 * \param ctx The context to free. This may be \c NULL, in which
260 * case this function does nothing. If it is not \c NULL,
261 * it must point to an initialized ECDH context.
262 */
264
265/**
266 * \brief This function generates an EC key pair and exports its
267 * in the format used in a TLS ServerKeyExchange handshake
268 * message.
269 *
270 * This is the second function used by a TLS server for ECDHE
271 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
272 *
273 * \see ecp.h
274 *
275 * \param ctx The ECDH context to use. This must be initialized
276 * and bound to a group, for example via mbedtls_ecdh_setup().
277 * \param olen The address at which to store the number of Bytes written.
278 * \param buf The destination buffer. This must be a writable buffer of
279 * length \p blen Bytes.
280 * \param blen The length of the destination buffer \p buf in Bytes.
281 * \param f_rng The RNG function to use. This must not be \c NULL.
282 * \param p_rng The RNG context to be passed to \p f_rng. This may be
283 * \c NULL in case \p f_rng doesn't need a context argument.
284 *
285 * \return \c 0 on success.
286 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
287 * operations was reached: see \c mbedtls_ecp_set_max_ops().
288 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
289 */
291 unsigned char *buf, size_t blen,
292 int (*f_rng)(void *, unsigned char *, size_t),
293 void *p_rng );
294
295/**
296 * \brief This function parses the ECDHE parameters in a
297 * TLS ServerKeyExchange handshake message.
298 *
299 * \note In a TLS handshake, this is the how the client
300 * sets up its ECDHE context from the server's public
301 * ECDHE key material.
302 *
303 * \see ecp.h
304 *
305 * \param ctx The ECDHE context to use. This must be initialized.
306 * \param buf On input, \c *buf must be the start of the input buffer.
307 * On output, \c *buf is updated to point to the end of the
308 * data that has been read. On success, this is the first byte
309 * past the end of the ServerKeyExchange parameters.
310 * On error, this is the point at which an error has been
311 * detected, which is usually not useful except to debug
312 * failures.
313 * \param end The end of the input buffer.
314 *
315 * \return \c 0 on success.
316 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
317 *
318 */
320 const unsigned char **buf,
321 const unsigned char *end );
322
323/**
324 * \brief This function sets up an ECDH context from an EC key.
325 *
326 * It is used by clients and servers in place of the
327 * ServerKeyEchange for static ECDH, and imports ECDH
328 * parameters from the EC key information of a certificate.
329 *
330 * \see ecp.h
331 *
332 * \param ctx The ECDH context to set up. This must be initialized.
333 * \param key The EC key to use. This must be initialized.
334 * \param side Defines the source of the key. Possible values are:
335 * - #MBEDTLS_ECDH_OURS: The key is ours.
336 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
337 *
338 * \return \c 0 on success.
339 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
340 *
341 */
343 const mbedtls_ecp_keypair *key,
344 mbedtls_ecdh_side side );
345
346/**
347 * \brief This function generates a public key and exports it
348 * as a TLS ClientKeyExchange payload.
349 *
350 * This is the second function used by a TLS client for ECDH(E)
351 * ciphersuites.
352 *
353 * \see ecp.h
354 *
355 * \param ctx The ECDH context to use. This must be initialized
356 * and bound to a group, the latter usually by
357 * mbedtls_ecdh_read_params().
358 * \param olen The address at which to store the number of Bytes written.
359 * This must not be \c NULL.
360 * \param buf The destination buffer. This must be a writable buffer
361 * of length \p blen Bytes.
362 * \param blen The size of the destination buffer \p buf in Bytes.
363 * \param f_rng The RNG function to use. This must not be \c NULL.
364 * \param p_rng The RNG context to be passed to \p f_rng. This may be
365 * \c NULL in case \p f_rng doesn't need a context argument.
366 *
367 * \return \c 0 on success.
368 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
369 * operations was reached: see \c mbedtls_ecp_set_max_ops().
370 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
371 */
373 unsigned char *buf, size_t blen,
374 int (*f_rng)(void *, unsigned char *, size_t),
375 void *p_rng );
376
377/**
378 * \brief This function parses and processes the ECDHE payload of a
379 * TLS ClientKeyExchange message.
380 *
381 * This is the third function used by a TLS server for ECDH(E)
382 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
383 * mbedtls_ecdh_make_params().)
384 *
385 * \see ecp.h
386 *
387 * \param ctx The ECDH context to use. This must be initialized
388 * and bound to a group, for example via mbedtls_ecdh_setup().
389 * \param buf The pointer to the ClientKeyExchange payload. This must
390 * be a readable buffer of length \p blen Bytes.
391 * \param blen The length of the input buffer \p buf in Bytes.
392 *
393 * \return \c 0 on success.
394 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
395 */
397 const unsigned char *buf, size_t blen );
398
399/**
400 * \brief This function derives and exports the shared secret.
401 *
402 * This is the last function used by both TLS client
403 * and servers.
404 *
405 * \note If \p f_rng is not NULL, it is used to implement
406 * countermeasures against side-channel attacks.
407 * For more information, see mbedtls_ecp_mul().
408 *
409 * \see ecp.h
410
411 * \param ctx The ECDH context to use. This must be initialized
412 * and have its own private key generated and the peer's
413 * public key imported.
414 * \param olen The address at which to store the total number of
415 * Bytes written on success. This must not be \c NULL.
416 * \param buf The buffer to write the generated shared key to. This
417 * must be a writable buffer of size \p blen Bytes.
418 * \param blen The length of the destination buffer \p buf in Bytes.
419 * \param f_rng The RNG function, for blinding purposes. This may
420 * b \c NULL if blinding isn't needed.
421 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
422 * doesn't need a context argument.
423 *
424 * \return \c 0 on success.
425 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
426 * operations was reached: see \c mbedtls_ecp_set_max_ops().
427 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
428 */
430 unsigned char *buf, size_t blen,
431 int (*f_rng)(void *, unsigned char *, size_t),
432 void *p_rng );
433
434#if defined(MBEDTLS_ECP_RESTARTABLE)
435/**
436 * \brief This function enables restartable EC computations for this
437 * context. (Default: disabled.)
438 *
439 * \see \c mbedtls_ecp_set_max_ops()
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 *
445 * \param ctx The ECDH context to use. This must be initialized.
446 */
447void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
448#endif /* MBEDTLS_ECP_RESTARTABLE */
449
450/// \}
451/// \}
452
453#ifdef __cplusplus
454}
455#endif
456
457#endif /* ecdh.h */
Configuration options (set of defines)
int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
This function sets up the ECDH context with the information given.
int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, mbedtls_ecdh_side side)
This function sets up an ECDH context from an EC key.
mbedtls_ecdh_variant
Defines the ECDH implementation used.
Definition: ecdh.h:77
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECDH keypair on an elliptic curve.
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a public key and exports it as a TLS ClientKeyExchange payload.
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function derives and exports the shared secret.
int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
Check whether a given group can be used for ECDH.
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an EC key pair and exports its in the format used in a TLS ServerKeyExchange ...
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, const unsigned char *buf, size_t blen)
This function parses and processes the ECDHE payload of a TLS ClientKeyExchange message.
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z, const mbedtls_ecp_point *Q, const mbedtls_mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the shared secret.
mbedtls_ecdh_side
Defines the source of the imported EC key.
Definition: ecdh.h:64
int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
This function parses the ECDHE parameters in a TLS ServerKeyExchange handshake message.
@ MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0
Definition: ecdh.h:79
@ MBEDTLS_ECDH_VARIANT_NONE
Definition: ecdh.h:78
@ MBEDTLS_ECDH_OURS
Our key.
Definition: ecdh.h:65
@ MBEDTLS_ECDH_THEIRS
The key of the peer.
Definition: ecdh.h:66
mbedtls_ecp_group_id
Domain-parameter identifiers: curve, subgroup, and generator.
This file provides an API for Elliptic Curves over GF(P) (ECP).
The context used by the default ECDH implementation.
Definition: ecdh.h:93
mbedtls_ecp_point Qp
Definition: ecdh.h:97
mbedtls_mpi d
Definition: ecdh.h:95
mbedtls_ecp_point Q
Definition: ecdh.h:96
mbedtls_ecp_group grp
Definition: ecdh.h:94
mbedtls_mpi z
Definition: ecdh.h:98
The ECDH context structure.
Definition: ecdh.h:113
union mbedtls_ecdh_context::@57 ctx
uint8_t point_format
Definition: ecdh.h:129
mbedtls_ecdh_variant var
Definition: ecdh.h:132
mbedtls_ecp_group_id grp_id
Definition: ecdh.h:131
The ECP group structure.
The ECP key-pair structure.
The ECP point structure, in Jacobian coordinates.
MPI structure.
Definition: bignum.h:185