ecdsa.h 8.9 KB

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