ecdsa.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * \file ecdsa.h
  3. *
  4. * \brief Elliptic curve DSA
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_ECDSA_H
  26. #define MBEDTLS_ECDSA_H
  27. #include "ecp.h"
  28. #include "md.h"
  29. /*
  30. * RFC 4492 page 20:
  31. *
  32. * Ecdsa-Sig-Value ::= SEQUENCE {
  33. * r INTEGER,
  34. * s INTEGER
  35. * }
  36. *
  37. * Size is at most
  38. * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
  39. * twice that + 1 (tag) + 2 (len) for the sequence
  40. * (assuming ECP_MAX_BYTES is less than 126 for r and s,
  41. * and less than 124 (total len <= 255) for the sequence)
  42. */
  43. #if MBEDTLS_ECP_MAX_BYTES > 124
  44. #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
  45. #endif
  46. /** Maximum size of an ECDSA signature in bytes */
  47. #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
  48. /**
  49. * \brief ECDSA context structure
  50. */
  51. typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**
  56. * \brief Compute ECDSA signature of a previously hashed message
  57. *
  58. * \note The deterministic version is usually prefered.
  59. *
  60. * \param grp ECP group
  61. * \param r First output integer
  62. * \param s Second output integer
  63. * \param d Private signing key
  64. * \param buf Message hash
  65. * \param blen Length of buf
  66. * \param f_rng RNG function
  67. * \param p_rng RNG parameter
  68. *
  69. * \return 0 if successful,
  70. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  71. */
  72. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  73. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  74. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  75. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  76. /**
  77. * \brief Compute ECDSA signature of a previously hashed message,
  78. * deterministic version (RFC 6979).
  79. *
  80. * \param grp ECP group
  81. * \param r First output integer
  82. * \param s Second output integer
  83. * \param d Private signing key
  84. * \param buf Message hash
  85. * \param blen Length of buf
  86. * \param md_alg MD algorithm used to hash the message
  87. *
  88. * \return 0 if successful,
  89. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  90. */
  91. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  92. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  93. mbedtls_md_type_t md_alg );
  94. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  95. /**
  96. * \brief Verify ECDSA signature of a previously hashed message
  97. *
  98. * \param grp ECP group
  99. * \param buf Message hash
  100. * \param blen Length of buf
  101. * \param Q Public key to use for verification
  102. * \param r First integer of the signature
  103. * \param s Second integer of the signature
  104. *
  105. * \return 0 if successful,
  106. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
  107. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  108. */
  109. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  110. const unsigned char *buf, size_t blen,
  111. const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
  112. /**
  113. * \brief Compute ECDSA signature and write it to buffer,
  114. * serialized as defined in RFC 4492 page 20.
  115. * (Not thread-safe to use same context in multiple threads)
  116. *
  117. * \note The deterministice version (RFC 6979) is used if
  118. * MBEDTLS_ECDSA_DETERMINISTIC is defined.
  119. *
  120. * \param ctx ECDSA context
  121. * \param md_alg Algorithm that was used to hash the message
  122. * \param hash Message hash
  123. * \param hlen Length of hash
  124. * \param sig Buffer that will hold the signature
  125. * \param slen Length of the signature written
  126. * \param f_rng RNG function
  127. * \param p_rng RNG parameter
  128. *
  129. * \note The "sig" buffer must be at least as large as twice the
  130. * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
  131. * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
  132. *
  133. * \return 0 if successful,
  134. * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
  135. * MBEDTLS_ERR_ASN1_XXX error code
  136. */
  137. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
  138. const unsigned char *hash, size_t hlen,
  139. unsigned char *sig, size_t *slen,
  140. int (*f_rng)(void *, unsigned char *, size_t),
  141. void *p_rng );
  142. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  143. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  144. #if defined(MBEDTLS_DEPRECATED_WARNING)
  145. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  146. #else
  147. #define MBEDTLS_DEPRECATED
  148. #endif
  149. /**
  150. * \brief Compute ECDSA signature and write it to buffer,
  151. * serialized as defined in RFC 4492 page 20.
  152. * Deterministic version, RFC 6979.
  153. * (Not thread-safe to use same context in multiple threads)
  154. *
  155. * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
  156. *
  157. * \param ctx ECDSA context
  158. * \param hash Message hash
  159. * \param hlen Length of hash
  160. * \param sig Buffer that will hold the signature
  161. * \param slen Length of the signature written
  162. * \param md_alg MD algorithm used to hash the message
  163. *
  164. * \note The "sig" buffer must be at least as large as twice the
  165. * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
  166. * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
  167. *
  168. * \return 0 if successful,
  169. * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
  170. * MBEDTLS_ERR_ASN1_XXX error code
  171. */
  172. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  173. const unsigned char *hash, size_t hlen,
  174. unsigned char *sig, size_t *slen,
  175. mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
  176. #undef MBEDTLS_DEPRECATED
  177. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  178. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  179. /**
  180. * \brief Read and verify an ECDSA signature
  181. *
  182. * \param ctx ECDSA context
  183. * \param hash Message hash
  184. * \param hlen Size of hash
  185. * \param sig Signature to read and verify
  186. * \param slen Size of sig
  187. *
  188. * \return 0 if successful,
  189. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
  190. * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
  191. * valid but its actual length is less than siglen,
  192. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
  193. */
  194. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  195. const unsigned char *hash, size_t hlen,
  196. const unsigned char *sig, size_t slen );
  197. /**
  198. * \brief Generate an ECDSA keypair on the given curve
  199. *
  200. * \param ctx ECDSA context in which the keypair should be stored
  201. * \param gid Group (elliptic curve) to use. One of the various
  202. * MBEDTLS_ECP_DP_XXX macros depending on configuration.
  203. * \param f_rng RNG function
  204. * \param p_rng RNG parameter
  205. *
  206. * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
  207. */
  208. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  209. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  210. /**
  211. * \brief Set an ECDSA context from an EC key pair
  212. *
  213. * \param ctx ECDSA context to set
  214. * \param key EC key to use
  215. *
  216. * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
  217. */
  218. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
  219. /**
  220. * \brief Initialize context
  221. *
  222. * \param ctx Context to initialize
  223. */
  224. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
  225. /**
  226. * \brief Free context
  227. *
  228. * \param ctx Context to free
  229. */
  230. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
  231. #ifdef __cplusplus
  232. }
  233. #endif
  234. #endif /* ecdsa.h */