Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:50

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