Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  17.  *  SPDX-License-Identifier: GPL-2.0
  18.  *
  19.  *  This program is free software; you can redistribute it and/or modify
  20.  *  it under the terms of the GNU General Public License as published by
  21.  *  the Free Software Foundation; either version 2 of the License, or
  22.  *  (at your option) any later version.
  23.  *
  24.  *  This program is distributed in the hope that it will be useful,
  25.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.  *  GNU General Public License for more details.
  28.  *
  29.  *  You should have received a copy of the GNU General Public License along
  30.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  31.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  32.  *
  33.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  34.  */
  35.  
  36. #ifndef MBEDTLS_ECDH_H
  37. #define MBEDTLS_ECDH_H
  38.  
  39. #if !defined(MBEDTLS_CONFIG_FILE)
  40. #include "config.h"
  41. #else
  42. #include MBEDTLS_CONFIG_FILE
  43. #endif
  44.  
  45. #include "ecp.h"
  46.  
  47. /*
  48.  * Use a backward compatible ECDH context.
  49.  *
  50.  * This flag is always enabled for now and future versions might add a
  51.  * configuration option that conditionally undefines this flag.
  52.  * The configuration option in question may have a different name.
  53.  *
  54.  * Features undefining this flag, must have a warning in their description in
  55.  * config.h stating that the feature breaks backward compatibility.
  56.  */
  57. #define MBEDTLS_ECDH_LEGACY_CONTEXT
  58.  
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62.  
  63. /**
  64.  * Defines the source of the imported EC key.
  65.  */
  66. typedef enum
  67. {
  68.     MBEDTLS_ECDH_OURS,   /**< Our key. */
  69.     MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
  70. } mbedtls_ecdh_side;
  71.  
  72. #if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  73. /**
  74.  * Defines the ECDH implementation used.
  75.  *
  76.  * Later versions of the library may add new variants, therefore users should
  77.  * not make any assumptions about them.
  78.  */
  79. typedef enum
  80. {
  81.     MBEDTLS_ECDH_VARIANT_NONE = 0,   /*!< Implementation not defined. */
  82.     MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
  83. } mbedtls_ecdh_variant;
  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.  */
  92. typedef struct mbedtls_ecdh_context_mbed
  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
  102. } mbedtls_ecdh_context_mbed;
  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.  */
  112. typedef struct mbedtls_ecdh_context
  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.     {
  135.         mbedtls_ecdh_context_mbed   mbed_ecdh;
  136.     } ctx;                      /*!< Implementation-specific context. The
  137.                                   context in use is specified by the \c var
  138.                                   field. */
  139. #if defined(MBEDTLS_ECP_RESTARTABLE)
  140.     uint8_t restart_enabled;    /*!< The flag for restartable mode. Functions of
  141.                                   an alternative implementation not supporting
  142.                                   restartable mode must return
  143.                                   MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
  144.                                   if this flag is set. */
  145. #endif /* MBEDTLS_ECP_RESTARTABLE */
  146. #endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
  147. }
  148. mbedtls_ecdh_context;
  149.  
  150. /**
  151.  * \brief           This function generates an ECDH keypair on an elliptic
  152.  *                  curve.
  153.  *
  154.  *                  This function performs the first of two core computations
  155.  *                  implemented during the ECDH key exchange. The second core
  156.  *                  computation is performed by mbedtls_ecdh_compute_shared().
  157.  *
  158.  * \see             ecp.h
  159.  *
  160.  * \param grp       The ECP group to use. This must be initialized and have
  161.  *                  domain parameters loaded, for example through
  162.  *                  mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
  163.  * \param d         The destination MPI (private key).
  164.  *                  This must be initialized.
  165.  * \param Q         The destination point (public key).
  166.  *                  This must be initialized.
  167.  * \param f_rng     The RNG function to use. This must not be \c NULL.
  168.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  169.  *                  \c NULL in case \p f_rng doesn't need a context argument.
  170.  *
  171.  * \return          \c 0 on success.
  172.  * \return          Another \c MBEDTLS_ERR_ECP_XXX or
  173.  *                  \c MBEDTLS_MPI_XXX error code on failure.
  174.  */
  175. int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  176.                      int (*f_rng)(void *, unsigned char *, size_t),
  177.                      void *p_rng );
  178.  
  179. /**
  180.  * \brief           This function computes the shared secret.
  181.  *
  182.  *                  This function performs the second of two core computations
  183.  *                  implemented during the ECDH key exchange. The first core
  184.  *                  computation is performed by mbedtls_ecdh_gen_public().
  185.  *
  186.  * \see             ecp.h
  187.  *
  188.  * \note            If \p f_rng is not NULL, it is used to implement
  189.  *                  countermeasures against side-channel attacks.
  190.  *                  For more information, see mbedtls_ecp_mul().
  191.  *
  192.  * \param grp       The ECP group to use. This must be initialized and have
  193.  *                  domain parameters loaded, for example through
  194.  *                  mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
  195.  * \param z         The destination MPI (shared secret).
  196.  *                  This must be initialized.
  197.  * \param Q         The public key from another party.
  198.  *                  This must be initialized.
  199.  * \param d         Our secret exponent (private key).
  200.  *                  This must be initialized.
  201.  * \param f_rng     The RNG function. This may be \c NULL if randomization
  202.  *                  of intermediate results during the ECP computations is
  203.  *                  not needed (discouraged). See the documentation of
  204.  *                  mbedtls_ecp_mul() for more.
  205.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  206.  *                  \c NULL if \p f_rng is \c NULL or doesn't need a
  207.  *                  context argument.
  208.  *
  209.  * \return          \c 0 on success.
  210.  * \return          Another \c MBEDTLS_ERR_ECP_XXX or
  211.  *                  \c MBEDTLS_MPI_XXX error code on failure.
  212.  */
  213. int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
  214.                          const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  215.                          int (*f_rng)(void *, unsigned char *, size_t),
  216.                          void *p_rng );
  217.  
  218. /**
  219.  * \brief           This function initializes an ECDH context.
  220.  *
  221.  * \param ctx       The ECDH context to initialize. This must not be \c NULL.
  222.  */
  223. void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
  224.  
  225. /**
  226.  * \brief           This function sets up the ECDH context with the information
  227.  *                  given.
  228.  *
  229.  *                  This function should be called after mbedtls_ecdh_init() but
  230.  *                  before mbedtls_ecdh_make_params(). There is no need to call
  231.  *                  this function before mbedtls_ecdh_read_params().
  232.  *
  233.  *                  This is the first function used by a TLS server for ECDHE
  234.  *                  ciphersuites.
  235.  *
  236.  * \param ctx       The ECDH context to set up. This must be initialized.
  237.  * \param grp_id    The group id of the group to set up the context for.
  238.  *
  239.  * \return          \c 0 on success.
  240.  */
  241. int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
  242.                         mbedtls_ecp_group_id grp_id );
  243.  
  244. /**
  245.  * \brief           This function frees a context.
  246.  *
  247.  * \param ctx       The context to free. This may be \c NULL, in which
  248.  *                  case this function does nothing. If it is not \c NULL,
  249.  *                  it must point to an initialized ECDH context.
  250.  */
  251. void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
  252.  
  253. /**
  254.  * \brief           This function generates an EC key pair and exports its
  255.  *                  in the format used in a TLS ServerKeyExchange handshake
  256.  *                  message.
  257.  *
  258.  *                  This is the second function used by a TLS server for ECDHE
  259.  *                  ciphersuites. (It is called after mbedtls_ecdh_setup().)
  260.  *
  261.  * \see             ecp.h
  262.  *
  263.  * \param ctx       The ECDH context to use. This must be initialized
  264.  *                  and bound to a group, for example via mbedtls_ecdh_setup().
  265.  * \param olen      The address at which to store the number of Bytes written.
  266.  * \param buf       The destination buffer. This must be a writable buffer of
  267.  *                  length \p blen Bytes.
  268.  * \param blen      The length of the destination buffer \p buf in Bytes.
  269.  * \param f_rng     The RNG function to use. This must not be \c NULL.
  270.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  271.  *                  \c NULL in case \p f_rng doesn't need a context argument.
  272.  *
  273.  * \return          \c 0 on success.
  274.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  275.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  276.  * \return          Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
  277.  */
  278. int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
  279.                       unsigned char *buf, size_t blen,
  280.                       int (*f_rng)(void *, unsigned char *, size_t),
  281.                       void *p_rng );
  282.  
  283. /**
  284.  * \brief           This function parses the ECDHE parameters in a
  285.  *                  TLS ServerKeyExchange handshake message.
  286.  *
  287.  * \note            In a TLS handshake, this is the how the client
  288.  *                  sets up its ECDHE context from the server's public
  289.  *                  ECDHE key material.
  290.  *
  291.  * \see             ecp.h
  292.  *
  293.  * \param ctx       The ECDHE context to use. This must be initialized.
  294.  * \param buf       On input, \c *buf must be the start of the input buffer.
  295.  *                  On output, \c *buf is updated to point to the end of the
  296.  *                  data that has been read. On success, this is the first byte
  297.  *                  past the end of the ServerKeyExchange parameters.
  298.  *                  On error, this is the point at which an error has been
  299.  *                  detected, which is usually not useful except to debug
  300.  *                  failures.
  301.  * \param end       The end of the input buffer.
  302.  *
  303.  * \return          \c 0 on success.
  304.  * \return          An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  305.  *
  306.  */
  307. int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
  308.                               const unsigned char **buf,
  309.                               const unsigned char *end );
  310.  
  311. /**
  312.  * \brief           This function sets up an ECDH context from an EC key.
  313.  *
  314.  *                  It is used by clients and servers in place of the
  315.  *                  ServerKeyEchange for static ECDH, and imports ECDH
  316.  *                  parameters from the EC key information of a certificate.
  317.  *
  318.  * \see             ecp.h
  319.  *
  320.  * \param ctx       The ECDH context to set up. This must be initialized.
  321.  * \param key       The EC key to use. This must be initialized.
  322.  * \param side      Defines the source of the key. Possible values are:
  323.  *                  - #MBEDTLS_ECDH_OURS: The key is ours.
  324.  *                  - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
  325.  *
  326.  * \return          \c 0 on success.
  327.  * \return          Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
  328.  *
  329.  */
  330. int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
  331.                              const mbedtls_ecp_keypair *key,
  332.                              mbedtls_ecdh_side side );
  333.  
  334. /**
  335.  * \brief           This function generates a public key and exports it
  336.  *                  as a TLS ClientKeyExchange payload.
  337.  *
  338.  *                  This is the second function used by a TLS client for ECDH(E)
  339.  *                  ciphersuites.
  340.  *
  341.  * \see             ecp.h
  342.  *
  343.  * \param ctx       The ECDH context to use. This must be initialized
  344.  *                  and bound to a group, the latter usually by
  345.  *                  mbedtls_ecdh_read_params().
  346.  * \param olen      The address at which to store the number of Bytes written.
  347.  *                  This must not be \c NULL.
  348.  * \param buf       The destination buffer. This must be a writable buffer
  349.  *                  of length \p blen Bytes.
  350.  * \param blen      The size of the destination buffer \p buf in Bytes.
  351.  * \param f_rng     The RNG function to use. This must not be \c NULL.
  352.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  353.  *                  \c NULL in case \p f_rng doesn't need a context argument.
  354.  *
  355.  * \return          \c 0 on success.
  356.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  357.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  358.  * \return          Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
  359.  */
  360. int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
  361.                       unsigned char *buf, size_t blen,
  362.                       int (*f_rng)(void *, unsigned char *, size_t),
  363.                       void *p_rng );
  364.  
  365. /**
  366.  * \brief       This function parses and processes the ECDHE payload of a
  367.  *              TLS ClientKeyExchange message.
  368.  *
  369.  *              This is the third function used by a TLS server for ECDH(E)
  370.  *              ciphersuites. (It is called after mbedtls_ecdh_setup() and
  371.  *              mbedtls_ecdh_make_params().)
  372.  *
  373.  * \see         ecp.h
  374.  *
  375.  * \param ctx   The ECDH context to use. This must be initialized
  376.  *              and bound to a group, for example via mbedtls_ecdh_setup().
  377.  * \param buf   The pointer to the ClientKeyExchange payload. This must
  378.  *              be a readable buffer of length \p blen Bytes.
  379.  * \param blen  The length of the input buffer \p buf in Bytes.
  380.  *
  381.  * \return      \c 0 on success.
  382.  * \return      An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  383.  */
  384. int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
  385.                               const unsigned char *buf, size_t blen );
  386.  
  387. /**
  388.  * \brief           This function derives and exports the shared secret.
  389.  *
  390.  *                  This is the last function used by both TLS client
  391.  *                  and servers.
  392.  *
  393.  * \note            If \p f_rng is not NULL, it is used to implement
  394.  *                  countermeasures against side-channel attacks.
  395.  *                  For more information, see mbedtls_ecp_mul().
  396.  *
  397.  * \see             ecp.h
  398.  
  399.  * \param ctx       The ECDH context to use. This must be initialized
  400.  *                  and have its own private key generated and the peer's
  401.  *                  public key imported.
  402.  * \param olen      The address at which to store the total number of
  403.  *                  Bytes written on success. This must not be \c NULL.
  404.  * \param buf       The buffer to write the generated shared key to. This
  405.  *                  must be a writable buffer of size \p blen Bytes.
  406.  * \param blen      The length of the destination buffer \p buf in Bytes.
  407.  * \param f_rng     The RNG function, for blinding purposes. This may
  408.  *                  b \c NULL if blinding isn't needed.
  409.  * \param p_rng     The RNG context. This may be \c NULL if \p f_rng
  410.  *                  doesn't need a context argument.
  411.  *
  412.  * \return          \c 0 on success.
  413.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  414.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  415.  * \return          Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
  416.  */
  417. int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
  418.                       unsigned char *buf, size_t blen,
  419.                       int (*f_rng)(void *, unsigned char *, size_t),
  420.                       void *p_rng );
  421.  
  422. #if defined(MBEDTLS_ECP_RESTARTABLE)
  423. /**
  424.  * \brief           This function enables restartable EC computations for this
  425.  *                  context.  (Default: disabled.)
  426.  *
  427.  * \see             \c mbedtls_ecp_set_max_ops()
  428.  *
  429.  * \note            It is not possible to safely disable restartable
  430.  *                  computations once enabled, except by free-ing the context,
  431.  *                  which cancels possible in-progress operations.
  432.  *
  433.  * \param ctx       The ECDH context to use. This must be initialized.
  434.  */
  435. void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
  436. #endif /* MBEDTLS_ECP_RESTARTABLE */
  437.  
  438. #ifdef __cplusplus
  439. }
  440. #endif
  441.  
  442. #endif /* ecdh.h */
  443.