Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:31

0001 /**
0002  * \file ecdsa.h
0003  *
0004  * \brief This file contains ECDSA definitions and functions.
0005  *
0006  * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in
0007  * <em>Standards for Efficient Cryptography Group (SECG):
0008  * SEC1 Elliptic Curve Cryptography</em>.
0009  * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve
0010  * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
0011  *
0012  */
0013 /*
0014  *  Copyright The Mbed TLS Contributors
0015  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0016  */
0017 
0018 #ifndef MBEDTLS_ECDSA_H
0019 #define MBEDTLS_ECDSA_H
0020 #include "mbedtls/private_access.h"
0021 
0022 #include "mbedtls/build_info.h"
0023 
0024 #include "mbedtls/ecp.h"
0025 #include "mbedtls/md.h"
0026 
0027 /**
0028  * \brief           Maximum ECDSA signature size for a given curve bit size
0029  *
0030  * \param bits      Curve size in bits
0031  * \return          Maximum signature size in bytes
0032  *
0033  * \note            This macro returns a compile-time constant if its argument
0034  *                  is one. It may evaluate its argument multiple times.
0035  */
0036 /*
0037  *     Ecdsa-Sig-Value ::= SEQUENCE {
0038  *         r       INTEGER,
0039  *         s       INTEGER
0040  *     }
0041  *
0042  * For each of r and s, the value (V) may include an extra initial "0" bit.
0043  */
0044 #define MBEDTLS_ECDSA_MAX_SIG_LEN(bits)                               \
0045     (/*T,L of SEQUENCE*/ ((bits) >= 61 * 8 ? 3 : 2) +              \
0046      /*T,L of r,s*/ 2 * (((bits) >= 127 * 8 ? 3 : 2) +     \
0047                          /*V of r,s*/ ((bits) + 8) / 8))
0048 
0049 /** The maximal size of an ECDSA signature in Bytes. */
0050 #define MBEDTLS_ECDSA_MAX_LEN  MBEDTLS_ECDSA_MAX_SIG_LEN(MBEDTLS_ECP_MAX_BITS)
0051 
0052 #ifdef __cplusplus
0053 extern "C" {
0054 #endif
0055 
0056 /**
0057  * \brief           The ECDSA context structure.
0058  *
0059  * \warning         Performing multiple operations concurrently on the same
0060  *                  ECDSA context is not supported; objects of this type
0061  *                  should not be shared between multiple threads.
0062  *
0063  * \note            pk_wrap module assumes that "ecdsa_context" is identical
0064  *                  to "ecp_keypair" (see for example structure
0065  *                  "mbedtls_eckey_info" where ECDSA sign/verify functions
0066  *                  are used also for EC key)
0067  */
0068 typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
0069 
0070 #if defined(MBEDTLS_ECP_RESTARTABLE)
0071 
0072 /**
0073  * \brief           Internal restart context for ecdsa_verify()
0074  *
0075  * \note            Opaque struct, defined in ecdsa.c
0076  */
0077 typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
0078 
0079 /**
0080  * \brief           Internal restart context for ecdsa_sign()
0081  *
0082  * \note            Opaque struct, defined in ecdsa.c
0083  */
0084 typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
0085 
0086 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
0087 /**
0088  * \brief           Internal restart context for ecdsa_sign_det()
0089  *
0090  * \note            Opaque struct, defined in ecdsa.c
0091  */
0092 typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
0093 #endif
0094 
0095 /**
0096  * \brief           General context for resuming ECDSA operations
0097  */
0098 typedef struct {
0099     mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(ecp);        /*!<  base context for ECP restart and
0100                                                             shared administrative info    */
0101     mbedtls_ecdsa_restart_ver_ctx *MBEDTLS_PRIVATE(ver); /*!<  ecdsa_verify() sub-context    */
0102     mbedtls_ecdsa_restart_sig_ctx *MBEDTLS_PRIVATE(sig); /*!<  ecdsa_sign() sub-context      */
0103 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
0104     mbedtls_ecdsa_restart_det_ctx *MBEDTLS_PRIVATE(det); /*!<  ecdsa_sign_det() sub-context  */
0105 #endif
0106 } mbedtls_ecdsa_restart_ctx;
0107 
0108 #else /* MBEDTLS_ECP_RESTARTABLE */
0109 
0110 /* Now we can declare functions that take a pointer to that */
0111 typedef void mbedtls_ecdsa_restart_ctx;
0112 
0113 #endif /* MBEDTLS_ECP_RESTARTABLE */
0114 
0115 /**
0116  * \brief          This function checks whether a given group can be used
0117  *                 for ECDSA.
0118  *
0119  * \param gid      The ECP group ID to check.
0120  *
0121  * \return         \c 1 if the group can be used, \c 0 otherwise
0122  */
0123 int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid);
0124 
0125 /**
0126  * \brief           This function computes the ECDSA signature of a
0127  *                  previously-hashed message.
0128  *
0129  * \note            The deterministic version implemented in
0130  *                  mbedtls_ecdsa_sign_det_ext() is usually preferred.
0131  *
0132  * \note            If the bitlength of the message hash is larger than the
0133  *                  bitlength of the group order, then the hash is truncated
0134  *                  as defined in <em>Standards for Efficient Cryptography Group
0135  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
0136  *                  4.1.3, step 5.
0137  *
0138  * \see             ecp.h
0139  *
0140  * \param grp       The context for the elliptic curve to use.
0141  *                  This must be initialized and have group parameters
0142  *                  set, for example through mbedtls_ecp_group_load().
0143  * \param r         The MPI context in which to store the first part
0144  *                  the signature. This must be initialized.
0145  * \param s         The MPI context in which to store the second part
0146  *                  the signature. This must be initialized.
0147  * \param d         The private signing key. This must be initialized.
0148  * \param buf       The content to be signed. This is usually the hash of
0149  *                  the original data to be signed. This must be a readable
0150  *                  buffer of length \p blen Bytes. It may be \c NULL if
0151  *                  \p blen is zero.
0152  * \param blen      The length of \p buf in Bytes.
0153  * \param f_rng     The RNG function. This must not be \c NULL.
0154  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
0155  *                  \c NULL if \p f_rng doesn't need a context parameter.
0156  *
0157  * \return          \c 0 on success.
0158  * \return          An \c MBEDTLS_ERR_ECP_XXX
0159  *                  or \c MBEDTLS_MPI_XXX error code on failure.
0160  */
0161 int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
0162                        const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
0163                        int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
0164 
0165 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
0166 /**
0167  * \brief           This function computes the ECDSA signature of a
0168  *                  previously-hashed message, deterministic version.
0169  *
0170  *                  For more information, see <em>RFC-6979: Deterministic
0171  *                  Usage of the Digital Signature Algorithm (DSA) and Elliptic
0172  *                  Curve Digital Signature Algorithm (ECDSA)</em>.
0173  *
0174  * \note            If the bitlength of the message hash is larger than the
0175  *                  bitlength of the group order, then the hash is truncated as
0176  *                  defined in <em>Standards for Efficient Cryptography Group
0177  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
0178  *                  4.1.3, step 5.
0179  *
0180  * \see             ecp.h
0181  *
0182  * \param grp           The context for the elliptic curve to use.
0183  *                      This must be initialized and have group parameters
0184  *                      set, for example through mbedtls_ecp_group_load().
0185  * \param r             The MPI context in which to store the first part
0186  *                      the signature. This must be initialized.
0187  * \param s             The MPI context in which to store the second part
0188  *                      the signature. This must be initialized.
0189  * \param d             The private signing key. This must be initialized
0190  *                      and setup, for example through mbedtls_ecp_gen_privkey().
0191  * \param buf           The hashed content to be signed. This must be a readable
0192  *                      buffer of length \p blen Bytes. It may be \c NULL if
0193  *                      \p blen is zero.
0194  * \param blen          The length of \p buf in Bytes.
0195  * \param md_alg        The hash algorithm used to hash the original data.
0196  * \param f_rng_blind   The RNG function used for blinding. This must not be
0197  *                      \c NULL.
0198  * \param p_rng_blind   The RNG context to be passed to \p f_rng_blind. This
0199  *                      may be \c NULL if \p f_rng_blind doesn't need a context
0200  *                      parameter.
0201  *
0202  * \return          \c 0 on success.
0203  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
0204  *                  error code on failure.
0205  */
0206 int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
0207                                mbedtls_mpi *s, const mbedtls_mpi *d,
0208                                const unsigned char *buf, size_t blen,
0209                                mbedtls_md_type_t md_alg,
0210                                int (*f_rng_blind)(void *, unsigned char *, size_t),
0211                                void *p_rng_blind);
0212 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
0213 
0214 #if !defined(MBEDTLS_ECDSA_SIGN_ALT)
0215 /**
0216  * \brief               This function computes the ECDSA signature of a
0217  *                      previously-hashed message, in a restartable way.
0218  *
0219  * \note                The deterministic version implemented in
0220  *                      mbedtls_ecdsa_sign_det_restartable() is usually
0221  *                      preferred.
0222  *
0223  * \note                This function is like \c mbedtls_ecdsa_sign() but
0224  *                      it can return early and restart according to the
0225  *                      limit set with \c mbedtls_ecp_set_max_ops() to
0226  *                      reduce blocking.
0227  *
0228  * \note                If the bitlength of the message hash is larger
0229  *                      than the bitlength of the group order, then the
0230  *                      hash is truncated as defined in <em>Standards for
0231  *                      Efficient Cryptography Group (SECG): SEC1 Elliptic
0232  *                      Curve Cryptography</em>, section 4.1.3, step 5.
0233  *
0234  * \see                 ecp.h
0235  *
0236  * \param grp           The context for the elliptic curve to use.
0237  *                      This must be initialized and have group parameters
0238  *                      set, for example through mbedtls_ecp_group_load().
0239  * \param r             The MPI context in which to store the first part
0240  *                      the signature. This must be initialized.
0241  * \param s             The MPI context in which to store the second part
0242  *                      the signature. This must be initialized.
0243  * \param d             The private signing key. This must be initialized
0244  *                      and setup, for example through
0245  *                      mbedtls_ecp_gen_privkey().
0246  * \param buf           The hashed content to be signed. This must be a readable
0247  *                      buffer of length \p blen Bytes. It may be \c NULL if
0248  *                      \p blen is zero.
0249  * \param blen          The length of \p buf in Bytes.
0250  * \param f_rng         The RNG function. This must not be \c NULL.
0251  * \param p_rng         The RNG context to be passed to \p f_rng. This may be
0252  *                      \c NULL if \p f_rng doesn't need a context parameter.
0253  * \param f_rng_blind   The RNG function used for blinding. This must not be
0254  *                      \c NULL.
0255  * \param p_rng_blind   The RNG context to be passed to \p f_rng. This may be
0256  *                      \c NULL if \p f_rng doesn't need a context parameter.
0257  * \param rs_ctx        The restart context to use. This may be \c NULL
0258  *                      to disable restarting. If it is not \c NULL, it
0259  *                      must point to an initialized restart context.
0260  *
0261  * \return              \c 0 on success.
0262  * \return              #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
0263  *                      operations was reached: see \c
0264  *                      mbedtls_ecp_set_max_ops().
0265  * \return              Another \c MBEDTLS_ERR_ECP_XXX, \c
0266  *                      MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
0267  *                      error code on failure.
0268  */
0269 int mbedtls_ecdsa_sign_restartable(
0270     mbedtls_ecp_group *grp,
0271     mbedtls_mpi *r, mbedtls_mpi *s,
0272     const mbedtls_mpi *d,
0273     const unsigned char *buf, size_t blen,
0274     int (*f_rng)(void *, unsigned char *, size_t),
0275     void *p_rng,
0276     int (*f_rng_blind)(void *, unsigned char *, size_t),
0277     void *p_rng_blind,
0278     mbedtls_ecdsa_restart_ctx *rs_ctx);
0279 
0280 #endif /* !MBEDTLS_ECDSA_SIGN_ALT */
0281 
0282 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
0283 
0284 /**
0285  * \brief               This function computes the ECDSA signature of a
0286  *                      previously-hashed message, in a restartable way.
0287  *
0288  * \note                This function is like \c
0289  *                      mbedtls_ecdsa_sign_det_ext() but it can return
0290  *                      early and restart according to the limit set with
0291  *                      \c mbedtls_ecp_set_max_ops() to reduce blocking.
0292  *
0293  * \note                If the bitlength of the message hash is larger
0294  *                      than the bitlength of the group order, then the
0295  *                      hash is truncated as defined in <em>Standards for
0296  *                      Efficient Cryptography Group (SECG): SEC1 Elliptic
0297  *                      Curve Cryptography</em>, section 4.1.3, step 5.
0298  *
0299  * \see                 ecp.h
0300  *
0301  * \param grp           The context for the elliptic curve to use.
0302  *                      This must be initialized and have group parameters
0303  *                      set, for example through mbedtls_ecp_group_load().
0304  * \param r             The MPI context in which to store the first part
0305  *                      the signature. This must be initialized.
0306  * \param s             The MPI context in which to store the second part
0307  *                      the signature. This must be initialized.
0308  * \param d             The private signing key. This must be initialized
0309  *                      and setup, for example through
0310  *                      mbedtls_ecp_gen_privkey().
0311  * \param buf           The hashed content to be signed. This must be a readable
0312  *                      buffer of length \p blen Bytes. It may be \c NULL if
0313  *                      \p blen is zero.
0314  * \param blen          The length of \p buf in Bytes.
0315  * \param md_alg        The hash algorithm used to hash the original data.
0316  * \param f_rng_blind   The RNG function used for blinding. This must not be
0317  *                      \c NULL.
0318  * \param p_rng_blind   The RNG context to be passed to \p f_rng_blind. This may be
0319  *                      \c NULL if \p f_rng_blind doesn't need a context parameter.
0320  * \param rs_ctx        The restart context to use. This may be \c NULL
0321  *                      to disable restarting. If it is not \c NULL, it
0322  *                      must point to an initialized restart context.
0323  *
0324  * \return              \c 0 on success.
0325  * \return              #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
0326  *                      operations was reached: see \c
0327  *                      mbedtls_ecp_set_max_ops().
0328  * \return              Another \c MBEDTLS_ERR_ECP_XXX, \c
0329  *                      MBEDTLS_ERR_MPI_XXX or \c MBEDTLS_ERR_ASN1_XXX
0330  *                      error code on failure.
0331  */
0332 int mbedtls_ecdsa_sign_det_restartable(
0333     mbedtls_ecp_group *grp,
0334     mbedtls_mpi *r, mbedtls_mpi *s,
0335     const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
0336     mbedtls_md_type_t md_alg,
0337     int (*f_rng_blind)(void *, unsigned char *, size_t),
0338     void *p_rng_blind,
0339     mbedtls_ecdsa_restart_ctx *rs_ctx);
0340 
0341 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
0342 
0343 /**
0344  * \brief           This function verifies the ECDSA signature of a
0345  *                  previously-hashed message.
0346  *
0347  * \note            If the bitlength of the message hash is larger than the
0348  *                  bitlength of the group order, then the hash is truncated as
0349  *                  defined in <em>Standards for Efficient Cryptography Group
0350  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
0351  *                  4.1.4, step 3.
0352  *
0353  * \see             ecp.h
0354  *
0355  * \param grp       The ECP group to use.
0356  *                  This must be initialized and have group parameters
0357  *                  set, for example through mbedtls_ecp_group_load().
0358  * \param buf       The hashed content that was signed. This must be a readable
0359  *                  buffer of length \p blen Bytes. It may be \c NULL if
0360  *                  \p blen is zero.
0361  * \param blen      The length of \p buf in Bytes.
0362  * \param Q         The public key to use for verification. This must be
0363  *                  initialized and setup.
0364  * \param r         The first integer of the signature.
0365  *                  This must be initialized.
0366  * \param s         The second integer of the signature.
0367  *                  This must be initialized.
0368  *
0369  * \return          \c 0 on success.
0370  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
0371  *                  error code on failure.
0372  */
0373 int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
0374                          const unsigned char *buf, size_t blen,
0375                          const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
0376                          const mbedtls_mpi *s);
0377 
0378 #if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
0379 /**
0380  * \brief           This function verifies the ECDSA signature of a
0381  *                  previously-hashed message, in a restartable manner
0382  *
0383  * \note            If the bitlength of the message hash is larger than the
0384  *                  bitlength of the group order, then the hash is truncated as
0385  *                  defined in <em>Standards for Efficient Cryptography Group
0386  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
0387  *                  4.1.4, step 3.
0388  *
0389  * \see             ecp.h
0390  *
0391  * \param grp       The ECP group to use.
0392  *                  This must be initialized and have group parameters
0393  *                  set, for example through mbedtls_ecp_group_load().
0394  * \param buf       The hashed content that was signed. This must be a readable
0395  *                  buffer of length \p blen Bytes. It may be \c NULL if
0396  *                  \p blen is zero.
0397  * \param blen      The length of \p buf in Bytes.
0398  * \param Q         The public key to use for verification. This must be
0399  *                  initialized and setup.
0400  * \param r         The first integer of the signature.
0401  *                  This must be initialized.
0402  * \param s         The second integer of the signature.
0403  *                  This must be initialized.
0404  * \param rs_ctx    The restart context to use. This may be \c NULL to disable
0405  *                  restarting. If it is not \c NULL, it must point to an
0406  *                  initialized restart context.
0407  *
0408  * \return          \c 0 on success.
0409  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
0410  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
0411  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
0412  *                  error code on failure.
0413  */
0414 int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
0415                                      const unsigned char *buf, size_t blen,
0416                                      const mbedtls_ecp_point *Q,
0417                                      const mbedtls_mpi *r,
0418                                      const mbedtls_mpi *s,
0419                                      mbedtls_ecdsa_restart_ctx *rs_ctx);
0420 
0421 #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
0422 
0423 /**
0424  * \brief           This function computes the ECDSA signature and writes it
0425  *                  to a buffer, serialized as defined in <em>RFC-4492:
0426  *                  Elliptic Curve Cryptography (ECC) Cipher Suites for
0427  *                  Transport Layer Security (TLS)</em>.
0428  *
0429  * \warning         It is not thread-safe to use the same context in
0430  *                  multiple threads.
0431  *
0432  * \note            The deterministic version is used if
0433  *                  #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
0434  *                  information, see <em>RFC-6979: Deterministic Usage
0435  *                  of the Digital Signature Algorithm (DSA) and Elliptic
0436  *                  Curve Digital Signature Algorithm (ECDSA)</em>.
0437  *
0438  * \note            If the bitlength of the message hash is larger than the
0439  *                  bitlength of the group order, then the hash is truncated as
0440  *                  defined in <em>Standards for Efficient Cryptography Group
0441  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
0442  *                  4.1.3, step 5.
0443  *
0444  * \see             ecp.h
0445  *
0446  * \param ctx       The ECDSA context to use. This must be initialized
0447  *                  and have a group and private key bound to it, for example
0448  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
0449  * \param md_alg    The message digest that was used to hash the message.
0450  * \param hash      The message hash to be signed. This must be a readable
0451  *                  buffer of length \p hlen Bytes.
0452  * \param hlen      The length of the hash \p hash in Bytes.
0453  * \param sig       The buffer to which to write the signature. This must be a
0454  *                  writable buffer of length at least twice as large as the
0455  *                  size of the curve used, plus 9. For example, 73 Bytes if
0456  *                  a 256-bit curve is used. A buffer length of
0457  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe.
0458  * \param sig_size  The size of the \p sig buffer in bytes.
0459  * \param slen      The address at which to store the actual length of
0460  *                  the signature written. Must not be \c NULL.
0461  * \param f_rng     The RNG function. This must not be \c NULL if
0462  *                  #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
0463  *                  it is used only for blinding and may be set to \c NULL, but
0464  *                  doing so is DEPRECATED.
0465  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
0466  *                  \c NULL if \p f_rng is \c NULL or doesn't use a context.
0467  *
0468  * \return          \c 0 on success.
0469  * \return          An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
0470  *                  \c MBEDTLS_ERR_ASN1_XXX error code on failure.
0471  */
0472 int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
0473                                   mbedtls_md_type_t md_alg,
0474                                   const unsigned char *hash, size_t hlen,
0475                                   unsigned char *sig, size_t sig_size, size_t *slen,
0476                                   int (*f_rng)(void *, unsigned char *, size_t),
0477                                   void *p_rng);
0478 
0479 /**
0480  * \brief           This function computes the ECDSA signature and writes it
0481  *                  to a buffer, in a restartable way.
0482  *
0483  * \see             \c mbedtls_ecdsa_write_signature()
0484  *
0485  * \note            This function is like \c mbedtls_ecdsa_write_signature()
0486  *                  but it can return early and restart according to the limit
0487  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
0488  *
0489  * \param ctx       The ECDSA context to use. This must be initialized
0490  *                  and have a group and private key bound to it, for example
0491  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
0492  * \param md_alg    The message digest that was used to hash the message.
0493  * \param hash      The message hash to be signed. This must be a readable
0494  *                  buffer of length \p hlen Bytes.
0495  * \param hlen      The length of the hash \p hash in Bytes.
0496  * \param sig       The buffer to which to write the signature. This must be a
0497  *                  writable buffer of length at least twice as large as the
0498  *                  size of the curve used, plus 9. For example, 73 Bytes if
0499  *                  a 256-bit curve is used. A buffer length of
0500  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe.
0501  * \param sig_size  The size of the \p sig buffer in bytes.
0502  * \param slen      The address at which to store the actual length of
0503  *                  the signature written. Must not be \c NULL.
0504  * \param f_rng     The RNG function. This must not be \c NULL if
0505  *                  #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
0506  *                  it is unused and may be set to \c NULL.
0507  * \param p_rng     The RNG context to be passed to \p f_rng. This may be
0508  *                  \c NULL if \p f_rng is \c NULL or doesn't use a context.
0509  * \param rs_ctx    The restart context to use. This may be \c NULL to disable
0510  *                  restarting. If it is not \c NULL, it must point to an
0511  *                  initialized restart context.
0512  *
0513  * \return          \c 0 on success.
0514  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
0515  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
0516  * \return          Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
0517  *                  \c MBEDTLS_ERR_ASN1_XXX error code on failure.
0518  */
0519 int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
0520                                               mbedtls_md_type_t md_alg,
0521                                               const unsigned char *hash, size_t hlen,
0522                                               unsigned char *sig, size_t sig_size, size_t *slen,
0523                                               int (*f_rng)(void *, unsigned char *, size_t),
0524                                               void *p_rng,
0525                                               mbedtls_ecdsa_restart_ctx *rs_ctx);
0526 
0527 /**
0528  * \brief           This function reads and verifies an ECDSA signature.
0529  *
0530  * \note            If the bitlength of the message hash is larger than the
0531  *                  bitlength of the group order, then the hash is truncated as
0532  *                  defined in <em>Standards for Efficient Cryptography Group
0533  *                  (SECG): SEC1 Elliptic Curve Cryptography</em>, section
0534  *                  4.1.4, step 3.
0535  *
0536  * \see             ecp.h
0537  *
0538  * \param ctx       The ECDSA context to use. This must be initialized
0539  *                  and have a group and public key bound to it.
0540  * \param hash      The message hash that was signed. This must be a readable
0541  *                  buffer of length \p hlen Bytes.
0542  * \param hlen      The size of the hash \p hash.
0543  * \param sig       The signature to read and verify. This must be a readable
0544  *                  buffer of length \p slen Bytes.
0545  * \param slen      The size of \p sig in Bytes.
0546  *
0547  * \return          \c 0 on success.
0548  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
0549  * \return          #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
0550  *                  signature in \p sig, but its length is less than \p siglen.
0551  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
0552  *                  error code on failure for any other reason.
0553  */
0554 int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
0555                                  const unsigned char *hash, size_t hlen,
0556                                  const unsigned char *sig, size_t slen);
0557 
0558 /**
0559  * \brief           This function reads and verifies an ECDSA signature,
0560  *                  in a restartable way.
0561  *
0562  * \see             \c mbedtls_ecdsa_read_signature()
0563  *
0564  * \note            This function is like \c mbedtls_ecdsa_read_signature()
0565  *                  but it can return early and restart according to the limit
0566  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
0567  *
0568  * \param ctx       The ECDSA context to use. This must be initialized
0569  *                  and have a group and public key bound to it.
0570  * \param hash      The message hash that was signed. This must be a readable
0571  *                  buffer of length \p hlen Bytes.
0572  * \param hlen      The size of the hash \p hash.
0573  * \param sig       The signature to read and verify. This must be a readable
0574  *                  buffer of length \p slen Bytes.
0575  * \param slen      The size of \p sig in Bytes.
0576  * \param rs_ctx    The restart context to use. This may be \c NULL to disable
0577  *                  restarting. If it is not \c NULL, it must point to an
0578  *                  initialized restart context.
0579  *
0580  * \return          \c 0 on success.
0581  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
0582  * \return          #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
0583  *                  signature in \p sig, but its length is less than \p siglen.
0584  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
0585  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
0586  * \return          Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
0587  *                  error code on failure for any other reason.
0588  */
0589 int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
0590                                              const unsigned char *hash, size_t hlen,
0591                                              const unsigned char *sig, size_t slen,
0592                                              mbedtls_ecdsa_restart_ctx *rs_ctx);
0593 
0594 /**
0595  * \brief          This function generates an ECDSA keypair on the given curve.
0596  *
0597  * \see            ecp.h
0598  *
0599  * \param ctx      The ECDSA context to store the keypair in.
0600  *                 This must be initialized.
0601  * \param gid      The elliptic curve to use. One of the various
0602  *                 \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
0603  * \param f_rng    The RNG function to use. This must not be \c NULL.
0604  * \param p_rng    The RNG context to be passed to \p f_rng. This may be
0605  *                 \c NULL if \p f_rng doesn't need a context argument.
0606  *
0607  * \return         \c 0 on success.
0608  * \return         An \c MBEDTLS_ERR_ECP_XXX code on failure.
0609  */
0610 int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
0611                          int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
0612 
0613 /**
0614  * \brief           This function sets up an ECDSA context from an EC key pair.
0615  *
0616  * \see             ecp.h
0617  *
0618  * \param ctx       The ECDSA context to setup. This must be initialized.
0619  * \param key       The EC key to use. This must be initialized and hold
0620  *                  a private-public key pair or a public key. In the former
0621  *                  case, the ECDSA context may be used for signature creation
0622  *                  and verification after this call. In the latter case, it
0623  *                  may be used for signature verification.
0624  *
0625  * \return          \c 0 on success.
0626  * \return          An \c MBEDTLS_ERR_ECP_XXX code on failure.
0627  */
0628 int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx,
0629                                const mbedtls_ecp_keypair *key);
0630 
0631 /**
0632  * \brief           This function initializes an ECDSA context.
0633  *
0634  * \param ctx       The ECDSA context to initialize.
0635  *                  This must not be \c NULL.
0636  */
0637 void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx);
0638 
0639 /**
0640  * \brief           This function frees an ECDSA context.
0641  *
0642  * \param ctx       The ECDSA context to free. This may be \c NULL,
0643  *                  in which case this function does nothing. If it
0644  *                  is not \c NULL, it must be initialized.
0645  */
0646 void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx);
0647 
0648 #if defined(MBEDTLS_ECP_RESTARTABLE)
0649 /**
0650  * \brief           Initialize a restart context.
0651  *
0652  * \param ctx       The restart context to initialize.
0653  *                  This must not be \c NULL.
0654  */
0655 void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx);
0656 
0657 /**
0658  * \brief           Free the components of a restart context.
0659  *
0660  * \param ctx       The restart context to free. This may be \c NULL,
0661  *                  in which case this function does nothing. If it
0662  *                  is not \c NULL, it must be initialized.
0663  */
0664 void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx);
0665 #endif /* MBEDTLS_ECP_RESTARTABLE */
0666 
0667 #ifdef __cplusplus
0668 }
0669 #endif
0670 
0671 #endif /* ecdsa.h */