Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file ecdsa.h
  3.  *
  4.  * \brief This file contains ECDSA definitions and functions.
  5.  *
  6.  * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in
  7.  * <em>Standards for Efficient Cryptography Group (SECG):
  8.  * SEC1 Elliptic Curve Cryptography</em>.
  9.  * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve
  10.  * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
  11.  *
  12.  */
  13. /*
  14.  *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  15.  *  SPDX-License-Identifier: GPL-2.0
  16.  *
  17.  *  This program is free software; you can redistribute it and/or modify
  18.  *  it under the terms of the GNU General Public License as published by
  19.  *  the Free Software Foundation; either version 2 of the License, or
  20.  *  (at your option) any later version.
  21.  *
  22.  *  This program is distributed in the hope that it will be useful,
  23.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25.  *  GNU General Public License for more details.
  26.  *
  27.  *  You should have received a copy of the GNU General Public License along
  28.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  29.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  30.  *
  31.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  32.  */
  33.  
  34. #ifndef MBEDTLS_ECDSA_H
  35. #define MBEDTLS_ECDSA_H
  36.  
  37. #if !defined(MBEDTLS_CONFIG_FILE)
  38. #include "config.h"
  39. #else
  40. #include MBEDTLS_CONFIG_FILE
  41. #endif
  42.  
  43. #include "ecp.h"
  44. #include "md.h"
  45.  
  46. /*
  47.  * RFC-4492 page 20:
  48.  *
  49.  *     Ecdsa-Sig-Value ::= SEQUENCE {
  50.  *         r       INTEGER,
  51.  *         s       INTEGER
  52.  *     }
  53.  *
  54.  * Size is at most
  55.  *    1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
  56.  *    twice that + 1 (tag) + 2 (len) for the sequence
  57.  * (assuming ECP_MAX_BYTES is less than 126 for r and s,
  58.  * and less than 124 (total len <= 255) for the sequence)
  59.  */
  60. #if MBEDTLS_ECP_MAX_BYTES > 124
  61. #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
  62. #endif
  63. /** The maximal size of an ECDSA signature in Bytes. */
  64. #define MBEDTLS_ECDSA_MAX_LEN  ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
  65.  
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69.  
  70. /**
  71.  * \brief           The ECDSA context structure.
  72.  *
  73.  * \warning         Performing multiple operations concurrently on the same
  74.  *                  ECDSA context is not supported; objects of this type
  75.  *                  should not be shared between multiple threads.
  76.  */
  77. typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
  78.  
  79. #if defined(MBEDTLS_ECP_RESTARTABLE)
  80.  
  81. /**
  82.  * \brief           Internal restart context for ecdsa_verify()
  83.  *
  84.  * \note            Opaque struct, defined in ecdsa.c
  85.  */
  86. typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
  87.  
  88. /**
  89.  * \brief           Internal restart context for ecdsa_sign()
  90.  *
  91.  * \note            Opaque struct, defined in ecdsa.c
  92.  */
  93. typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
  94.  
  95. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  96. /**
  97.  * \brief           Internal restart context for ecdsa_sign_det()
  98.  *
  99.  * \note            Opaque struct, defined in ecdsa.c
  100.  */
  101. typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
  102. #endif
  103.  
  104. /**
  105.  * \brief           General context for resuming ECDSA operations
  106.  */
  107. typedef struct
  108. {
  109.     mbedtls_ecp_restart_ctx ecp;        /*!<  base context for ECP restart and
  110.                                               shared administrative info    */
  111.     mbedtls_ecdsa_restart_ver_ctx *ver; /*!<  ecdsa_verify() sub-context    */
  112.     mbedtls_ecdsa_restart_sig_ctx *sig; /*!<  ecdsa_sign() sub-context      */
  113. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  114.     mbedtls_ecdsa_restart_det_ctx *det; /*!<  ecdsa_sign_det() sub-context  */
  115. #endif
  116. } mbedtls_ecdsa_restart_ctx;
  117.  
  118. #else /* MBEDTLS_ECP_RESTARTABLE */
  119.  
  120. /* Now we can declare functions that take a pointer to that */
  121. typedef void mbedtls_ecdsa_restart_ctx;
  122.  
  123. #endif /* MBEDTLS_ECP_RESTARTABLE */
  124.  
  125. /**
  126.  * \brief           This function computes the ECDSA signature of a
  127.  *                  previously-hashed message.
  128.  *
  129.  * \note            The deterministic version implemented in
  130.  *                  mbedtls_ecdsa_sign_det() is usually preferred.
  131.  *
  132.  * \note            If the bitlength of the message hash is larger than the
  133.  *                  bitlength of the group order, then the hash is truncated
  134.  *                  as defined in <em>Standards for Efficient Cryptography Group
  135.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  136.  *                  4.1.3, step 5.
  137.  *
  138.  * \see             ecp.h
  139.  *
  140.  * \param grp       The context for the elliptic curve to use.
  141.  *                  This must be initialized and have group parameters
  142.  *                  set, for example through mbedtls_ecp_group_load().
  143.  * \param r         The MPI context in which to store the first part
  144.  *                  the signature. This must be initialized.
  145.  * \param s         The MPI context in which to store the second part
  146.  *                  the signature. This must be initialized.
  147.  * \param d         The private signing key. This must be initialized.
  148.  * \param buf       The content to be signed. This is usually the hash of
  149.  *                  the original data to be signed. This must be a readable
  150.  *                  buffer of length \p blen Bytes. It may be \c NULL if
  151.  *                  \p blen is zero.
  152.  * \param blen      The length of \p buf in Bytes.
  153.  * \param f_rng     The RNG function. This must not be \c NULL.
  154.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  155.  *                  \c NULL if \p f_rng doesn't need a context parameter.
  156.  *
  157.  * \return          \c 0 on success.
  158.  * \return          An \c MBEDTLS_ERR_ECP_XXX
  159.  *                  or \c MBEDTLS_MPI_XXX error code on failure.
  160.  */
  161. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  162.                 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  163.                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  164.  
  165. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  166. /**
  167.  * \brief           This function computes the ECDSA signature of a
  168.  *                  previously-hashed message, deterministic version.
  169.  *
  170.  *                  For more information, see <em>RFC-6979: Deterministic
  171.  *                  Usage of the Digital Signature Algorithm (DSA) and Elliptic
  172.  *                  Curve Digital Signature Algorithm (ECDSA)</em>.
  173.  *
  174.  * \note            If the bitlength of the message hash is larger than the
  175.  *                  bitlength of the group order, then the hash is truncated as
  176.  *                  defined in <em>Standards for Efficient Cryptography Group
  177.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  178.  *                  4.1.3, step 5.
  179.  *
  180.  * \warning         Since the output of the internal RNG is always the same for
  181.  *                  the same key and message, this limits the efficiency of
  182.  *                  blinding and leaks information through side channels. For
  183.  *                  secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
  184.  *
  185.  *                  (Optimally the blinding is a random value that is different
  186.  *                  on every execution. In this case the blinding is still
  187.  *                  random from the attackers perspective, but is the same on
  188.  *                  each execution. This means that this blinding does not
  189.  *                  prevent attackers from recovering secrets by combining
  190.  *                  several measurement traces, but may prevent some attacks
  191.  *                  that exploit relationships between secret data.)
  192.  *
  193.  * \see             ecp.h
  194.  *
  195.  * \param grp       The context for the elliptic curve to use.
  196.  *                  This must be initialized and have group parameters
  197.  *                  set, for example through mbedtls_ecp_group_load().
  198.  * \param r         The MPI context in which to store the first part
  199.  *                  the signature. This must be initialized.
  200.  * \param s         The MPI context in which to store the second part
  201.  *                  the signature. This must be initialized.
  202.  * \param d         The private signing key. This must be initialized
  203.  *                  and setup, for example through mbedtls_ecp_gen_privkey().
  204.  * \param buf       The hashed content to be signed. This must be a readable
  205.  *                  buffer of length \p blen Bytes. It may be \c NULL if
  206.  *                  \p blen is zero.
  207.  * \param blen      The length of \p buf in Bytes.
  208.  * \param md_alg    The hash algorithm used to hash the original data.
  209.  *
  210.  * \return          \c 0 on success.
  211.  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  212.  *                  error code on failure.
  213.  */
  214. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  215.                             mbedtls_mpi *s, const mbedtls_mpi *d,
  216.                             const unsigned char *buf, size_t blen,
  217.                             mbedtls_md_type_t md_alg );
  218. /**
  219.  * \brief           This function computes the ECDSA signature of a
  220.  *                  previously-hashed message, deterministic version.
  221.  *
  222.  *                  For more information, see <em>RFC-6979: Deterministic
  223.  *                  Usage of the Digital Signature Algorithm (DSA) and Elliptic
  224.  *                  Curve Digital Signature Algorithm (ECDSA)</em>.
  225.  *
  226.  * \note            If the bitlength of the message hash is larger than the
  227.  *                  bitlength of the group order, then the hash is truncated as
  228.  *                  defined in <em>Standards for Efficient Cryptography Group
  229.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  230.  *                  4.1.3, step 5.
  231.  *
  232.  * \see             ecp.h
  233.  *
  234.  * \param grp           The context for the elliptic curve to use.
  235.  *                      This must be initialized and have group parameters
  236.  *                      set, for example through mbedtls_ecp_group_load().
  237.  * \param r             The MPI context in which to store the first part
  238.  *                      the signature. This must be initialized.
  239.  * \param s             The MPI context in which to store the second part
  240.  *                      the signature. This must be initialized.
  241.  * \param d             The private signing key. This must be initialized
  242.  *                      and setup, for example through mbedtls_ecp_gen_privkey().
  243.  * \param buf           The hashed content to be signed. This must be a readable
  244.  *                      buffer of length \p blen Bytes. It may be \c NULL if
  245.  *                      \p blen is zero.
  246.  * \param blen          The length of \p buf in Bytes.
  247.  * \param md_alg        The hash algorithm used to hash the original data.
  248.  * \param f_rng_blind   The RNG function used for blinding. This must not be
  249.  *                      \c NULL.
  250.  * \param p_rng_blind   The RNG context to be passed to \p f_rng. This may be
  251.  *                      \c NULL if \p f_rng doesn't need a context parameter.
  252.  *
  253.  * \return          \c 0 on success.
  254.  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  255.  *                  error code on failure.
  256.  */
  257. int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  258.                                 mbedtls_mpi *s, const mbedtls_mpi *d,
  259.                                 const unsigned char *buf, size_t blen,
  260.                                 mbedtls_md_type_t md_alg,
  261.                                 int (*f_rng_blind)(void *, unsigned char *,
  262.                                                    size_t),
  263.                                 void *p_rng_blind );
  264. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  265.  
  266. /**
  267.  * \brief           This function verifies the ECDSA signature of a
  268.  *                  previously-hashed message.
  269.  *
  270.  * \note            If the bitlength of the message hash is larger than the
  271.  *                  bitlength of the group order, then the hash is truncated as
  272.  *                  defined in <em>Standards for Efficient Cryptography Group
  273.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  274.  *                  4.1.4, step 3.
  275.  *
  276.  * \see             ecp.h
  277.  *
  278.  * \param grp       The ECP group to use.
  279.  *                  This must be initialized and have group parameters
  280.  *                  set, for example through mbedtls_ecp_group_load().
  281.  * \param buf       The hashed content that was signed. This must be a readable
  282.  *                  buffer of length \p blen Bytes. It may be \c NULL if
  283.  *                  \p blen is zero.
  284.  * \param blen      The length of \p buf in Bytes.
  285.  * \param Q         The public key to use for verification. This must be
  286.  *                  initialized and setup.
  287.  * \param r         The first integer of the signature.
  288.  *                  This must be initialized.
  289.  * \param s         The second integer of the signature.
  290.  *                  This must be initialized.
  291.  *
  292.  * \return          \c 0 on success.
  293.  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
  294.  *                  is invalid.
  295.  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  296.  *                  error code on failure for any other reason.
  297.  */
  298. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  299.                           const unsigned char *buf, size_t blen,
  300.                           const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
  301.                           const mbedtls_mpi *s);
  302.  
  303. /**
  304.  * \brief           This function computes the ECDSA signature and writes it
  305.  *                  to a buffer, serialized as defined in <em>RFC-4492:
  306.  *                  Elliptic Curve Cryptography (ECC) Cipher Suites for
  307.  *                  Transport Layer Security (TLS)</em>.
  308.  *
  309.  * \warning         It is not thread-safe to use the same context in
  310.  *                  multiple threads.
  311.  *
  312.  * \note            The deterministic version is used if
  313.  *                  #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
  314.  *                  information, see <em>RFC-6979: Deterministic Usage
  315.  *                  of the Digital Signature Algorithm (DSA) and Elliptic
  316.  *                  Curve Digital Signature Algorithm (ECDSA)</em>.
  317.  *
  318.  * \note            If the bitlength of the message hash is larger than the
  319.  *                  bitlength of the group order, then the hash is truncated as
  320.  *                  defined in <em>Standards for Efficient Cryptography Group
  321.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  322.  *                  4.1.3, step 5.
  323.  *
  324.  * \see             ecp.h
  325.  *
  326.  * \param ctx       The ECDSA context to use. This must be initialized
  327.  *                  and have a group and private key bound to it, for example
  328.  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
  329.  * \param md_alg    The message digest that was used to hash the message.
  330.  * \param hash      The message hash to be signed. This must be a readable
  331.  *                  buffer of length \p blen Bytes.
  332.  * \param hlen      The length of the hash \p hash in Bytes.
  333.  * \param sig       The buffer to which to write the signature. This must be a
  334.  *                  writable buffer of length at least twice as large as the
  335.  *                  size of the curve used, plus 9. For example, 73 Bytes if
  336.  *                  a 256-bit curve is used. A buffer length of
  337.  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe.
  338.  * \param slen      The address at which to store the actual length of
  339.  *                  the signature written. Must not be \c NULL.
  340.  * \param f_rng     The RNG function. This must not be \c NULL if
  341.  *                  #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
  342.  *                  it is unused and may be set to \c NULL.
  343.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  344.  *                  \c NULL if \p f_rng is \c NULL or doesn't use a context.
  345.  *
  346.  * \return          \c 0 on success.
  347.  * \return          An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  348.  *                  \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  349.  */
  350. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
  351.                                    mbedtls_md_type_t md_alg,
  352.                            const unsigned char *hash, size_t hlen,
  353.                            unsigned char *sig, size_t *slen,
  354.                            int (*f_rng)(void *, unsigned char *, size_t),
  355.                            void *p_rng );
  356.  
  357. /**
  358.  * \brief           This function computes the ECDSA signature and writes it
  359.  *                  to a buffer, in a restartable way.
  360.  *
  361.  * \see             \c mbedtls_ecdsa_write_signature()
  362.  *
  363.  * \note            This function is like \c mbedtls_ecdsa_write_signature()
  364.  *                  but it can return early and restart according to the limit
  365.  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  366.  *
  367.  * \param ctx       The ECDSA context to use. This must be initialized
  368.  *                  and have a group and private key bound to it, for example
  369.  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
  370.  * \param md_alg    The message digest that was used to hash the message.
  371.  * \param hash      The message hash to be signed. This must be a readable
  372.  *                  buffer of length \p blen Bytes.
  373.  * \param hlen      The length of the hash \p hash in Bytes.
  374.  * \param sig       The buffer to which to write the signature. This must be a
  375.  *                  writable buffer of length at least twice as large as the
  376.  *                  size of the curve used, plus 9. For example, 73 Bytes if
  377.  *                  a 256-bit curve is used. A buffer length of
  378.  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe.
  379.  * \param slen      The address at which to store the actual length of
  380.  *                  the signature written. Must not be \c NULL.
  381.  * \param f_rng     The RNG function. This must not be \c NULL if
  382.  *                  #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
  383.  *                  it is unused and may be set to \c NULL.
  384.  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
  385.  *                  \c NULL if \p f_rng is \c NULL or doesn't use a context.
  386.  * \param rs_ctx    The restart context to use. This may be \c NULL to disable
  387.  *                  restarting. If it is not \c NULL, it must point to an
  388.  *                  initialized restart context.
  389.  *
  390.  * \return          \c 0 on success.
  391.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  392.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  393.  * \return          Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  394.  *                  \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  395.  */
  396. int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
  397.                            mbedtls_md_type_t md_alg,
  398.                            const unsigned char *hash, size_t hlen,
  399.                            unsigned char *sig, size_t *slen,
  400.                            int (*f_rng)(void *, unsigned char *, size_t),
  401.                            void *p_rng,
  402.                            mbedtls_ecdsa_restart_ctx *rs_ctx );
  403.  
  404. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  405. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  406. #if defined(MBEDTLS_DEPRECATED_WARNING)
  407. #define MBEDTLS_DEPRECATED    __attribute__((deprecated))
  408. #else
  409. #define MBEDTLS_DEPRECATED
  410. #endif
  411. /**
  412.  * \brief           This function computes an ECDSA signature and writes
  413.  *                  it to a buffer, serialized as defined in <em>RFC-4492:
  414.  *                  Elliptic Curve Cryptography (ECC) Cipher Suites for
  415.  *                  Transport Layer Security (TLS)</em>.
  416.  *
  417.  *                  The deterministic version is defined in <em>RFC-6979:
  418.  *                  Deterministic Usage of the Digital Signature Algorithm (DSA)
  419.  *                  and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
  420.  *
  421.  * \warning         It is not thread-safe to use the same context in
  422.  *                  multiple threads.
  423.  *
  424.  * \note            If the bitlength of the message hash is larger than the
  425.  *                  bitlength of the group order, then the hash is truncated as
  426.  *                  defined in <em>Standards for Efficient Cryptography Group
  427.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  428.  *                  4.1.3, step 5.
  429.  *
  430.  * \see             ecp.h
  431.  *
  432.  * \deprecated      Superseded by mbedtls_ecdsa_write_signature() in
  433.  *                  Mbed TLS version 2.0 and later.
  434.  *
  435.  * \param ctx       The ECDSA context to use. This must be initialized
  436.  *                  and have a group and private key bound to it, for example
  437.  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
  438.  * \param hash      The message hash to be signed. This must be a readable
  439.  *                  buffer of length \p blen Bytes.
  440.  * \param hlen      The length of the hash \p hash in Bytes.
  441.  * \param sig       The buffer to which to write the signature. This must be a
  442.  *                  writable buffer of length at least twice as large as the
  443.  *                  size of the curve used, plus 9. For example, 73 Bytes if
  444.  *                  a 256-bit curve is used. A buffer length of
  445.  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe.
  446.  * \param slen      The address at which to store the actual length of
  447.  *                  the signature written. Must not be \c NULL.
  448.  * \param md_alg    The message digest that was used to hash the message.
  449.  *
  450.  * \return          \c 0 on success.
  451.  * \return          An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  452.  *                  \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  453.  */
  454. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  455.                                const unsigned char *hash, size_t hlen,
  456.                                unsigned char *sig, size_t *slen,
  457.                                mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
  458. #undef MBEDTLS_DEPRECATED
  459. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  460. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  461.  
  462. /**
  463.  * \brief           This function reads and verifies an ECDSA signature.
  464.  *
  465.  * \note            If the bitlength of the message hash is larger than the
  466.  *                  bitlength of the group order, then the hash is truncated as
  467.  *                  defined in <em>Standards for Efficient Cryptography Group
  468.  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  469.  *                  4.1.4, step 3.
  470.  *
  471.  * \see             ecp.h
  472.  *
  473.  * \param ctx       The ECDSA context to use. This must be initialized
  474.  *                  and have a group and public key bound to it.
  475.  * \param hash      The message hash that was signed. This must be a readable
  476.  *                  buffer of length \p size Bytes.
  477.  * \param hlen      The size of the hash \p hash.
  478.  * \param sig       The signature to read and verify. This must be a readable
  479.  *                  buffer of length \p slen Bytes.
  480.  * \param slen      The size of \p sig in Bytes.
  481.  *
  482.  * \return          \c 0 on success.
  483.  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
  484.  * \return          #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
  485.  *                  signature in \p sig, but its length is less than \p siglen.
  486.  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
  487.  *                  error code on failure for any other reason.
  488.  */
  489. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  490.                           const unsigned char *hash, size_t hlen,
  491.                           const unsigned char *sig, size_t slen );
  492.  
  493. /**
  494.  * \brief           This function reads and verifies an ECDSA signature,
  495.  *                  in a restartable way.
  496.  *
  497.  * \see             \c mbedtls_ecdsa_read_signature()
  498.  *
  499.  * \note            This function is like \c mbedtls_ecdsa_read_signature()
  500.  *                  but it can return early and restart according to the limit
  501.  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  502.  *
  503.  * \param ctx       The ECDSA context to use. This must be initialized
  504.  *                  and have a group and public key bound to it.
  505.  * \param hash      The message hash that was signed. This must be a readable
  506.  *                  buffer of length \p size Bytes.
  507.  * \param hlen      The size of the hash \p hash.
  508.  * \param sig       The signature to read and verify. This must be a readable
  509.  *                  buffer of length \p slen Bytes.
  510.  * \param slen      The size of \p sig in Bytes.
  511.  * \param rs_ctx    The restart context to use. This may be \c NULL to disable
  512.  *                  restarting. If it is not \c NULL, it must point to an
  513.  *                  initialized restart context.
  514.  *
  515.  * \return          \c 0 on success.
  516.  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
  517.  * \return          #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
  518.  *                  signature in \p sig, but its length is less than \p siglen.
  519.  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  520.  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
  521.  * \return          Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
  522.  *                  error code on failure for any other reason.
  523.  */
  524. int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
  525.                           const unsigned char *hash, size_t hlen,
  526.                           const unsigned char *sig, size_t slen,
  527.                           mbedtls_ecdsa_restart_ctx *rs_ctx );
  528.  
  529. /**
  530.  * \brief          This function generates an ECDSA keypair on the given curve.
  531.  *
  532.  * \see            ecp.h
  533.  *
  534.  * \param ctx      The ECDSA context to store the keypair in.
  535.  *                 This must be initialized.
  536.  * \param gid      The elliptic curve to use. One of the various
  537.  *                 \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
  538.  * \param f_rng    The RNG function to use. This must not be \c NULL.
  539.  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
  540.  *                 \c NULL if \p f_rng doesn't need a context argument.
  541.  *
  542.  * \return         \c 0 on success.
  543.  * \return         An \c MBEDTLS_ERR_ECP_XXX code on failure.
  544.  */
  545. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  546.                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  547.  
  548. /**
  549.  * \brief           This function sets up an ECDSA context from an EC key pair.
  550.  *
  551.  * \see             ecp.h
  552.  *
  553.  * \param ctx       The ECDSA context to setup. This must be initialized.
  554.  * \param key       The EC key to use. This must be initialized and hold
  555.  *                  a private-public key pair or a public key. In the former
  556.  *                  case, the ECDSA context may be used for signature creation
  557.  *                  and verification after this call. In the latter case, it
  558.  *                  may be used for signature verification.
  559.  *
  560.  * \return          \c 0 on success.
  561.  * \return          An \c MBEDTLS_ERR_ECP_XXX code on failure.
  562.  */
  563. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
  564.                                 const mbedtls_ecp_keypair *key );
  565.  
  566. /**
  567.  * \brief           This function initializes an ECDSA context.
  568.  *
  569.  * \param ctx       The ECDSA context to initialize.
  570.  *                  This must not be \c NULL.
  571.  */
  572. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
  573.  
  574. /**
  575.  * \brief           This function frees an ECDSA context.
  576.  *
  577.  * \param ctx       The ECDSA context to free. This may be \c NULL,
  578.  *                  in which case this function does nothing. If it
  579.  *                  is not \c NULL, it must be initialized.
  580.  */
  581. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
  582.  
  583. #if defined(MBEDTLS_ECP_RESTARTABLE)
  584. /**
  585.  * \brief           Initialize a restart context.
  586.  *
  587.  * \param ctx       The restart context to initialize.
  588.  *                  This must not be \c NULL.
  589.  */
  590. void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
  591.  
  592. /**
  593.  * \brief           Free the components of a restart context.
  594.  *
  595.  * \param ctx       The restart context to free. This may be \c NULL,
  596.  *                  in which case this function does nothing. If it
  597.  *                  is not \c NULL, it must be initialized.
  598.  */
  599. void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
  600. #endif /* MBEDTLS_ECP_RESTARTABLE */
  601.  
  602. #ifdef __cplusplus
  603. }
  604. #endif
  605.  
  606. #endif /* ecdsa.h */
  607.