Path: blob/master/thirdparty/mbedtls/include/mbedtls/ecdh.h
9903 views
/**1* \file ecdh.h2*3* \brief This file contains ECDH definitions and functions.4*5* The Elliptic Curve Diffie-Hellman (ECDH) protocol is an anonymous6* key agreement protocol allowing two parties to establish a shared7* secret over an insecure channel. Each party must have an8* elliptic-curve public–private key pair.9*10* For more information, see <em>NIST SP 800-56A Rev. 2: Recommendation for11* Pair-Wise Key Establishment Schemes Using Discrete Logarithm12* Cryptography</em>.13*/14/*15* Copyright The Mbed TLS Contributors16* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later17*/1819#ifndef MBEDTLS_ECDH_H20#define MBEDTLS_ECDH_H21#include "mbedtls/private_access.h"2223#include "mbedtls/build_info.h"2425#include "mbedtls/ecp.h"2627/*28* Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context29* defined in `ecdh.h`). For most applications, the choice of format makes30* no difference, since all library functions can work with either format,31* except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.3233* The new format used when this option is disabled is smaller34* (56 bytes on a 32-bit platform). In future versions of the library, it35* will support alternative implementations of ECDH operations.36* The new format is incompatible with applications that access37* context fields directly and with restartable ECP operations.38*/3940#if defined(MBEDTLS_ECP_RESTARTABLE)41#define MBEDTLS_ECDH_LEGACY_CONTEXT42#else43#undef MBEDTLS_ECDH_LEGACY_CONTEXT44#endif4546#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)47#undef MBEDTLS_ECDH_LEGACY_CONTEXT48#include "everest/everest.h"49#endif5051#ifdef __cplusplus52extern "C" {53#endif5455/**56* Defines the source of the imported EC key.57*/58typedef enum {59MBEDTLS_ECDH_OURS, /**< Our key. */60MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */61} mbedtls_ecdh_side;6263#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 should68* not make any assumptions about them.69*/70typedef enum {71MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */72MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */73#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)74MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */75#endif76} mbedtls_ecdh_variant;7778/**79* The context used by the default ECDH implementation.80*81* Later versions might change the structure of this context, therefore users82* should not make any assumptions about the structure of83* mbedtls_ecdh_context_mbed.84*/85typedef struct mbedtls_ecdh_context_mbed {86mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */87mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */88mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */89mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */90mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */91#if defined(MBEDTLS_ECP_RESTARTABLE)92mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */93#endif94} mbedtls_ecdh_context_mbed;95#endif9697/**98*99* \warning Performing multiple operations concurrently on the same100* ECDSA context is not supported; objects of this type101* should not be shared between multiple threads.102* \brief The ECDH context structure.103*/104typedef struct mbedtls_ecdh_context {105#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)106mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */107mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */108mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */109mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */110mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */111int MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages. */112mbedtls_ecp_point MBEDTLS_PRIVATE(Vi); /*!< The blinding value. */113mbedtls_ecp_point MBEDTLS_PRIVATE(Vf); /*!< The unblinding value. */114mbedtls_mpi MBEDTLS_PRIVATE(_d); /*!< The previous \p d. */115#if defined(MBEDTLS_ECP_RESTARTABLE)116int MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. */117mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */118#endif /* MBEDTLS_ECP_RESTARTABLE */119#else120uint8_t MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages121as defined in RFC 4492. */122mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id);/*!< The elliptic curve used. */123mbedtls_ecdh_variant MBEDTLS_PRIVATE(var); /*!< The ECDH implementation/structure used. */124union {125mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh);126#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)127mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);128#endif129} MBEDTLS_PRIVATE(ctx); /*!< Implementation-specific context. The130context in use is specified by the \c var131field. */132#if defined(MBEDTLS_ECP_RESTARTABLE)133uint8_t MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. Functions of134an alternative implementation not supporting135restartable mode must return136MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error137if this flag is set. */138#endif /* MBEDTLS_ECP_RESTARTABLE */139#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */140}141mbedtls_ecdh_context;142143/**144* \brief Return the ECP group for provided context.145*146* \note To access group specific fields, users should use147* `mbedtls_ecp_curve_info_from_grp_id` or148* `mbedtls_ecp_group_load` on the extracted `group_id`.149*150* \param ctx The ECDH context to parse. This must not be \c NULL.151*152* \return The \c mbedtls_ecp_group_id of the context.153*/154mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx);155156/**157* \brief Check whether a given group can be used for ECDH.158*159* \param gid The ECP group ID to check.160*161* \return \c 1 if the group can be used, \c 0 otherwise162*/163int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);164165/**166* \brief This function generates an ECDH keypair on an elliptic167* curve.168*169* This function performs the first of two core computations170* implemented during the ECDH key exchange. The second core171* computation is performed by mbedtls_ecdh_compute_shared().172*173* \see ecp.h174*175* \param grp The ECP group to use. This must be initialized and have176* domain parameters loaded, for example through177* mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().178* \param d The destination MPI (private key).179* This must be initialized.180* \param Q The destination point (public key).181* This must be initialized.182* \param f_rng The RNG function to use. This must not be \c NULL.183* \param p_rng The RNG context to be passed to \p f_rng. This may be184* \c NULL in case \p f_rng doesn't need a context argument.185*186* \return \c 0 on success.187* \return Another \c MBEDTLS_ERR_ECP_XXX or188* \c MBEDTLS_MPI_XXX error code on failure.189*/190int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,191mbedtls_f_rng_t *f_rng,192void *p_rng);193194/**195* \brief This function computes the shared secret.196*197* This function performs the second of two core computations198* implemented during the ECDH key exchange. The first core199* computation is performed by mbedtls_ecdh_gen_public().200*201* \see ecp.h202*203* \note If \p f_rng is not NULL, it is used to implement204* countermeasures against side-channel attacks.205* For more information, see mbedtls_ecp_mul().206*207* \param grp The ECP group to use. This must be initialized and have208* domain parameters loaded, for example through209* mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().210* \param z The destination MPI (shared secret).211* This must be initialized.212* \param Q The public key from another party.213* This must be initialized.214* \param d Our secret exponent (private key).215* This must be initialized.216* \param f_rng The RNG function to use. This must not be \c NULL.217* \param p_rng The RNG context to be passed to \p f_rng. This may be218* \c NULL if \p f_rng is \c NULL or doesn't need a219* context argument.220*221* \return \c 0 on success.222* \return Another \c MBEDTLS_ERR_ECP_XXX or223* \c MBEDTLS_MPI_XXX error code on failure.224*/225int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,226const mbedtls_ecp_point *Q, const mbedtls_mpi *d,227mbedtls_f_rng_t *f_rng,228void *p_rng);229230/**231* \brief This function initializes an ECDH context.232*233* \param ctx The ECDH context to initialize. This must not be \c NULL.234*/235void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx);236237/**238* \brief This function sets up the ECDH context with the information239* given.240*241* This function should be called after mbedtls_ecdh_init() but242* before mbedtls_ecdh_make_params(). There is no need to call243* this function before mbedtls_ecdh_read_params().244*245* This is the first function used by a TLS server for ECDHE246* 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*/253int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx,254mbedtls_ecp_group_id grp_id);255256/**257* \brief This function frees a context.258*259* \param ctx The context to free. This may be \c NULL, in which260* case this function does nothing. If it is not \c NULL,261* it must point to an initialized ECDH context.262*/263void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);264265/**266* \brief This function generates an EC key pair and exports its267* in the format used in a TLS ServerKeyExchange handshake268* message.269*270* This is the second function used by a TLS server for ECDHE271* ciphersuites. (It is called after mbedtls_ecdh_setup().)272*273* \see ecp.h274*275* \param ctx The ECDH context to use. This must be initialized276* 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 of279* 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 be283* \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 of287* operations was reached: see \c mbedtls_ecp_set_max_ops().288* \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.289*/290int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,291unsigned char *buf, size_t blen,292mbedtls_f_rng_t *f_rng,293void *p_rng);294295/**296* \brief This function parses the ECDHE parameters in a297* TLS ServerKeyExchange handshake message.298*299* \note In a TLS handshake, this is the how the client300* sets up its ECDHE context from the server's public301* ECDHE key material.302*303* \see ecp.h304*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 the308* data that has been read. On success, this is the first byte309* past the end of the ServerKeyExchange parameters.310* On error, this is the point at which an error has been311* detected, which is usually not useful except to debug312* 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*/319int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,320const unsigned char **buf,321const unsigned char *end);322323/**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 the327* ServerKeyExchange for static ECDH, and imports ECDH328* parameters from the EC key information of a certificate.329*330* \see ecp.h331*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*/342int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,343const mbedtls_ecp_keypair *key,344mbedtls_ecdh_side side);345346/**347* \brief This function generates a public key and exports it348* 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.h354*355* \param ctx The ECDH context to use. This must be initialized356* and bound to a group, the latter usually by357* 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 buffer361* 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 be365* \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 of369* operations was reached: see \c mbedtls_ecp_set_max_ops().370* \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.371*/372int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,373unsigned char *buf, size_t blen,374mbedtls_f_rng_t *f_rng,375void *p_rng);376377/**378* \brief This function parses and processes the ECDHE payload of a379* 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() and383* mbedtls_ecdh_make_params().)384*385* \see ecp.h386*387* \param ctx The ECDH context to use. This must be initialized388* and bound to a group, for example via mbedtls_ecdh_setup().389* \param buf The pointer to the ClientKeyExchange payload. This must390* 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*/396int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,397const unsigned char *buf, size_t blen);398399/**400* \brief This function derives and exports the shared secret.401*402* This is the last function used by both TLS client403* and servers.404*405* \note If \p f_rng is not NULL, it is used to implement406* countermeasures against side-channel attacks.407* For more information, see mbedtls_ecp_mul().408*409* \see ecp.h410411* \param ctx The ECDH context to use. This must be initialized412* and have its own private key generated and the peer's413* public key imported.414* \param olen The address at which to store the total number of415* Bytes written on success. This must not be \c NULL.416* \param buf The buffer to write the generated shared key to. This417* 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 to use. This must not be \c NULL.420* \param p_rng The RNG context. This may be \c NULL if \p f_rng421* doesn't need a context argument.422*423* \return \c 0 on success.424* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of425* operations was reached: see \c mbedtls_ecp_set_max_ops().426* \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.427*/428int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,429unsigned char *buf, size_t blen,430mbedtls_f_rng_t *f_rng,431void *p_rng);432433#if defined(MBEDTLS_ECP_RESTARTABLE)434/**435* \brief This function enables restartable EC computations for this436* context. (Default: disabled.)437*438* \see \c mbedtls_ecp_set_max_ops()439*440* \note It is not possible to safely disable restartable441* computations once enabled, except by free-ing the context,442* which cancels possible in-progress operations.443*444* \param ctx The ECDH context to use. This must be initialized.445*/446void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);447#endif /* MBEDTLS_ECP_RESTARTABLE */448449#ifdef __cplusplus450}451#endif452453#endif /* ecdh.h */454455456