Loading...
Searching...
No Matches
ecdh.h
Go to the documentation of this file.
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/
#ifndef MBEDTLS_ECDH_H
#define MBEDTLS_ECDH_H
#include "mbedtls/private_access.h"
#include "mbedtls/build_info.h"
#include "mbedtls/ecp.h"
/*
* Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
* defined in `ecdh.h`). For most applications, the choice of format makes
* no difference, since all library functions can work with either format,
* except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
* The new format used when this option is disabled is smaller
* (56 bytes on a 32-bit platform). In future versions of the library, it
* will support alternative implementations of ECDH operations.
* The new format is incompatible with applications that access
* context fields directly and with restartable ECP operations.
*/
#if defined(MBEDTLS_ECP_RESTARTABLE)
#define MBEDTLS_ECDH_LEGACY_CONTEXT
#else
#undef MBEDTLS_ECDH_LEGACY_CONTEXT
#endif
#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
#undef MBEDTLS_ECDH_LEGACY_CONTEXT
#include "everest/everest.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
MBEDTLS_ECDH_OURS,
MBEDTLS_ECDH_THEIRS,
} mbedtls_ecdh_side;
#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
typedef enum {
MBEDTLS_ECDH_VARIANT_NONE = 0,
MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,
#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
MBEDTLS_ECDH_VARIANT_EVEREST
#endif
} mbedtls_ecdh_variant;
typedef struct mbedtls_ecdh_context_mbed {
mbedtls_ecp_group MBEDTLS_PRIVATE(grp);
mbedtls_mpi MBEDTLS_PRIVATE(d);
mbedtls_ecp_point MBEDTLS_PRIVATE(Q);
mbedtls_ecp_point MBEDTLS_PRIVATE(Qp);
mbedtls_mpi MBEDTLS_PRIVATE(z);
#if defined(MBEDTLS_ECP_RESTARTABLE)
mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs);
#endif
} mbedtls_ecdh_context_mbed;
#endif
typedef struct mbedtls_ecdh_context {
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
mbedtls_ecp_group MBEDTLS_PRIVATE(grp);
mbedtls_mpi MBEDTLS_PRIVATE(d);
mbedtls_ecp_point MBEDTLS_PRIVATE(Q);
mbedtls_ecp_point MBEDTLS_PRIVATE(Qp);
mbedtls_mpi MBEDTLS_PRIVATE(z);
int MBEDTLS_PRIVATE(point_format);
mbedtls_ecp_point MBEDTLS_PRIVATE(Vi);
mbedtls_ecp_point MBEDTLS_PRIVATE(Vf);
mbedtls_mpi MBEDTLS_PRIVATE(_d);
#if defined(MBEDTLS_ECP_RESTARTABLE)
int MBEDTLS_PRIVATE(restart_enabled);
mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs);
#endif /* MBEDTLS_ECP_RESTARTABLE */
#else
uint8_t MBEDTLS_PRIVATE(point_format);
mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id);
mbedtls_ecdh_variant MBEDTLS_PRIVATE(var);
union {
mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh);
#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
#endif
} MBEDTLS_PRIVATE(ctx);
#if defined(MBEDTLS_ECP_RESTARTABLE)
uint8_t MBEDTLS_PRIVATE(restart_enabled);
#endif /* MBEDTLS_ECP_RESTARTABLE */
#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
}
mbedtls_ecdh_context;
mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx);
int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);
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);
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);
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx);
int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx,
mbedtls_ecp_group_id grp_id);
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);
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);
int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
const unsigned char **buf,
const unsigned char *end);
int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
const mbedtls_ecp_keypair *key,
mbedtls_ecdh_side side);
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);
int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
const unsigned char *buf, size_t blen);
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);
#if defined(MBEDTLS_ECP_RESTARTABLE)
void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);
#endif /* MBEDTLS_ECP_RESTARTABLE */
#ifdef __cplusplus
}
#endif
#endif /* ecdh.h */
mbedtls_ecdh_setup
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.
mbedtls_ecdh_get_params
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
mbedtls_ecdh_variant
Definition ecdh.h:71
MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0
@ MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0
Definition ecdh.h:73
MBEDTLS_ECDH_VARIANT_NONE
@ MBEDTLS_ECDH_VARIANT_NONE
Definition ecdh.h:72
mbedtls_ecdh_gen_public
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.
mbedtls_ecdh_make_public
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.
mbedtls_ecdh_calc_secret
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.
mbedtls_ecdh_can_do
int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
Check whether a given group can be used for ECDH.
mbedtls_ecdh_make_params
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 ...
mbedtls_ecdh_free
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
mbedtls_ecdh_init
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
mbedtls_ecdh_get_grp_id
mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx)
Return the ECP group for provided context.
mbedtls_ecdh_read_public
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.
mbedtls_ecdh_compute_shared
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
mbedtls_ecdh_side
Definition ecdh.h:59
MBEDTLS_ECDH_OURS
@ MBEDTLS_ECDH_OURS
Definition ecdh.h:60
MBEDTLS_ECDH_THEIRS
@ MBEDTLS_ECDH_THEIRS
Definition ecdh.h:61
mbedtls_ecdh_read_params
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.
ecp.h
This file provides an API for Elliptic Curves over GF(P) (ECP).
mbedtls_ecp_restart_ctx
void mbedtls_ecp_restart_ctx
Definition ecp.h:416
mbedtls_ecp_group_id
mbedtls_ecp_group_id
Definition ecp.h:102
build_info.h
Build-time configuration info.
private_access.h
Macro wrapper for struct's members.
MBEDTLS_PRIVATE
#define MBEDTLS_PRIVATE(member)
Definition private_access.h:15
mbedtls_ecdh_context_mbed
Definition ecdh.h:86
mbedtls_ecdh_context
The ECDH context structure.
Definition ecdh.h:105
mbedtls_ecp_group
The ECP group structure.
Definition ecp.h:233
mbedtls_ecp_keypair
The ECP key-pair structure.
Definition ecp.h:428
mbedtls_ecp_point
The ECP point structure, in Jacobian coordinates.
Definition ecp.h:158
mbedtls_mpi
MPI structure.
Definition bignum.h:207