Mbed TLS v3.6.4
ecdh.h
Go to the documentation of this file.
1 
15 /*
16  * Copyright The Mbed TLS Contributors
17  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
18  */
19 
20 #ifndef MBEDTLS_ECDH_H
21 #define MBEDTLS_ECDH_H
22 #include "mbedtls/private_access.h"
23 
24 #include "mbedtls/build_info.h"
25 
26 #include "mbedtls/ecp.h"
27 
28 /*
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.
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 
47 #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
48 #undef MBEDTLS_ECDH_LEGACY_CONTEXT
49 #include "everest/everest.h"
50 #endif
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
59 typedef enum {
63 
64 #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
65 
71 typedef enum {
74 #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
75  MBEDTLS_ECDH_VARIANT_EVEREST
76 #endif
78 
86 typedef struct mbedtls_ecdh_context_mbed {
92 #if defined(MBEDTLS_ECP_RESTARTABLE)
94 #endif
96 #endif
97 
105 typedef struct mbedtls_ecdh_context {
106 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
112  int MBEDTLS_PRIVATE(point_format);
116 #if defined(MBEDTLS_ECP_RESTARTABLE)
117  int MBEDTLS_PRIVATE(restart_enabled);
119 #endif /* MBEDTLS_ECP_RESTARTABLE */
120 #else
121  uint8_t MBEDTLS_PRIVATE(point_format);
125  union {
127 #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
128  mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
129 #endif
130  } MBEDTLS_PRIVATE(ctx);
133 #if defined(MBEDTLS_ECP_RESTARTABLE)
134  uint8_t MBEDTLS_PRIVATE(restart_enabled);
139 #endif /* MBEDTLS_ECP_RESTARTABLE */
140 #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
141 }
143 
156 
165 
192  mbedtls_f_rng_t *f_rng,
193  void *p_rng);
194 
227  const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
228  mbedtls_f_rng_t *f_rng,
229  void *p_rng);
230 
237 
255  mbedtls_ecp_group_id grp_id);
256 
265 
291 int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
292  unsigned char *buf, size_t blen,
293  mbedtls_f_rng_t *f_rng,
294  void *p_rng);
295 
321  const unsigned char **buf,
322  const unsigned char *end);
323 
344  const mbedtls_ecp_keypair *key,
345  mbedtls_ecdh_side side);
346 
373 int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
374  unsigned char *buf, size_t blen,
375  mbedtls_f_rng_t *f_rng,
376  void *p_rng);
377 
398  const unsigned char *buf, size_t blen);
399 
429 int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
430  unsigned char *buf, size_t blen,
431  mbedtls_f_rng_t *f_rng,
432  void *p_rng);
433 
434 #if defined(MBEDTLS_ECP_RESTARTABLE)
435 
447 void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);
448 #endif /* MBEDTLS_ECP_RESTARTABLE */
449 
450 #ifdef __cplusplus
451 }
452 #endif
453 
454 #endif /* ecdh.h */
struct mbedtls_ecdh_context_mbed mbedtls_ecdh_context_mbed
int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size)
The type of custom random generator (RNG) callbacks.
mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx)
Return the ECP group for provided context.
This file provides an API for Elliptic Curves over GF(P) (ECP).
The ECP key-pair structure.
Definition: ecp.h:428
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
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.
#define MBEDTLS_PRIVATE(member)
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.
The ECP group structure.
Definition: ecp.h:233
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, mbedtls_f_rng_t *f_rng, void *p_rng)
This function derives and exports the shared secret.
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, mbedtls_f_rng_t *f_rng, void *p_rng)
This function generates a public key and exports it as a TLS ClientKeyExchange payload.
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
mbedtls_ecdh_side
Definition: ecdh.h:59
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z, const mbedtls_ecp_point *Q, const mbedtls_mpi *d, mbedtls_f_rng_t *f_rng, void *p_rng)
This function computes the shared secret.
Macro wrapper for struct's members.
void mbedtls_ecp_restart_ctx
Definition: ecp.h:416
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, mbedtls_f_rng_t *f_rng, void *p_rng)
This function generates an ECDH keypair on an elliptic curve.
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_ecp_group_id
Definition: ecp.h:102
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, mbedtls_f_rng_t *f_rng, void *p_rng)
This function generates an EC key pair and exports its in the format used in a TLS ServerKeyExchange ...
struct mbedtls_ecdh_context mbedtls_ecdh_context
The ECDH context structure.
The ECDH context structure.
Definition: ecdh.h:105
Build-time configuration info.
MPI structure.
Definition: bignum.h:208
int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
Check whether a given group can be used for ECDH.
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:158
mbedtls_ecdh_variant
Definition: ecdh.h:71
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.