ecdsa.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *
  29. * This file is part of Mbed TLS (https://tls.mbed.org)
  30. */
  31. #ifndef MBEDTLS_ECDSA_H
  32. #define MBEDTLS_ECDSA_H
  33. #include "ecp.h"
  34. #include "md.h"
  35. /*
  36. * RFC-4492 page 20:
  37. *
  38. * Ecdsa-Sig-Value ::= SEQUENCE {
  39. * r INTEGER,
  40. * s INTEGER
  41. * }
  42. *
  43. * Size is at most
  44. * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
  45. * twice that + 1 (tag) + 2 (len) for the sequence
  46. * (assuming ECP_MAX_BYTES is less than 126 for r and s,
  47. * and less than 124 (total len <= 255) for the sequence)
  48. */
  49. #if MBEDTLS_ECP_MAX_BYTES > 124
  50. #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
  51. #endif
  52. /** The maximal size of an ECDSA signature in Bytes. */
  53. #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
  54. /**
  55. * \brief The ECDSA context structure.
  56. */
  57. typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * \brief This function computes the ECDSA signature of a
  63. * previously-hashed message.
  64. *
  65. * \note The deterministic version is usually preferred.
  66. *
  67. * \note If the bitlength of the message hash is larger than the
  68. * bitlength of the group order, then the hash is truncated
  69. * as defined in <em>Standards for Efficient Cryptography Group
  70. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  71. * 4.1.3, step 5.
  72. *
  73. * \see ecp.h
  74. *
  75. * \param grp The ECP group.
  76. * \param r The first output integer.
  77. * \param s The second output integer.
  78. * \param d The private signing key.
  79. * \param buf The message hash.
  80. * \param blen The length of \p buf.
  81. * \param f_rng The RNG function.
  82. * \param p_rng The RNG context.
  83. *
  84. * \return \c 0 on success.
  85. * \return An \c MBEDTLS_ERR_ECP_XXX
  86. * or \c MBEDTLS_MPI_XXX error code on failure.
  87. */
  88. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  89. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  90. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  91. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  92. /**
  93. * \brief This function computes the ECDSA signature of a
  94. * previously-hashed message, deterministic version.
  95. *
  96. * For more information, see <em>RFC-6979: Deterministic
  97. * Usage of the Digital Signature Algorithm (DSA) and Elliptic
  98. * Curve Digital Signature Algorithm (ECDSA)</em>.
  99. *
  100. * \note If the bitlength of the message hash is larger than the
  101. * bitlength of the group order, then the hash is truncated as
  102. * defined in <em>Standards for Efficient Cryptography Group
  103. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  104. * 4.1.3, step 5.
  105. *
  106. * \see ecp.h
  107. *
  108. * \param grp The ECP group.
  109. * \param r The first output integer.
  110. * \param s The second output integer.
  111. * \param d The private signing key.
  112. * \param buf The message hash.
  113. * \param blen The length of \p buf.
  114. * \param md_alg The MD algorithm used to hash the message.
  115. *
  116. * \return \c 0 on success.
  117. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  118. * error code on failure.
  119. */
  120. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  121. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  122. mbedtls_md_type_t md_alg );
  123. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  124. /**
  125. * \brief This function verifies the ECDSA signature of a
  126. * previously-hashed message.
  127. *
  128. * \note If the bitlength of the message hash is larger than the
  129. * bitlength of the group order, then the hash is truncated as
  130. * defined in <em>Standards for Efficient Cryptography Group
  131. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  132. * 4.1.4, step 3.
  133. *
  134. * \see ecp.h
  135. *
  136. * \param grp The ECP group.
  137. * \param buf The message hash.
  138. * \param blen The length of \p buf.
  139. * \param Q The public key to use for verification.
  140. * \param r The first integer of the signature.
  141. * \param s The second integer of the signature.
  142. *
  143. * \return \c 0 on success.
  144. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
  145. * is invalid.
  146. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  147. * error code on failure for any other reason.
  148. */
  149. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  150. const unsigned char *buf, size_t blen,
  151. const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
  152. /**
  153. * \brief This function computes the ECDSA signature and writes it
  154. * to a buffer, serialized as defined in <em>RFC-4492:
  155. * Elliptic Curve Cryptography (ECC) Cipher Suites for
  156. * Transport Layer Security (TLS)</em>.
  157. *
  158. * \warning It is not thread-safe to use the same context in
  159. * multiple threads.
  160. *
  161. * \note The deterministic version is used if
  162. * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
  163. * information, see <em>RFC-6979: Deterministic Usage
  164. * of the Digital Signature Algorithm (DSA) and Elliptic
  165. * Curve Digital Signature Algorithm (ECDSA)</em>.
  166. *
  167. * \note The \p sig buffer must be at least twice as large as the
  168. * size of the curve used, plus 9. For example, 73 Bytes if
  169. * a 256-bit curve is used. A buffer length of
  170. * #MBEDTLS_ECDSA_MAX_LEN is always safe.
  171. *
  172. * \note If the bitlength of the message hash is larger than the
  173. * bitlength of the group order, then the hash is truncated as
  174. * defined in <em>Standards for Efficient Cryptography Group
  175. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  176. * 4.1.3, step 5.
  177. *
  178. * \see ecp.h
  179. *
  180. * \param ctx The ECDSA context.
  181. * \param md_alg The message digest that was used to hash the message.
  182. * \param hash The message hash.
  183. * \param hlen The length of the hash.
  184. * \param sig The buffer that holds the signature.
  185. * \param slen The length of the signature written.
  186. * \param f_rng The RNG function.
  187. * \param p_rng The RNG context.
  188. *
  189. * \return \c 0 on success.
  190. * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  191. * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  192. */
  193. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
  194. const unsigned char *hash, size_t hlen,
  195. unsigned char *sig, size_t *slen,
  196. int (*f_rng)(void *, unsigned char *, size_t),
  197. void *p_rng );
  198. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  199. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  200. #if defined(MBEDTLS_DEPRECATED_WARNING)
  201. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  202. #else
  203. #define MBEDTLS_DEPRECATED
  204. #endif
  205. /**
  206. * \brief This function computes an ECDSA signature and writes
  207. * it to a buffer, serialized as defined in <em>RFC-4492:
  208. * Elliptic Curve Cryptography (ECC) Cipher Suites for
  209. * Transport Layer Security (TLS)</em>.
  210. *
  211. * The deterministic version is defined in <em>RFC-6979:
  212. * Deterministic Usage of the Digital Signature Algorithm (DSA)
  213. * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
  214. *
  215. * \warning It is not thread-safe to use the same context in
  216. * multiple threads.
  217. *
  218. * \note The \p sig buffer must be at least twice as large as the
  219. * size of the curve used, plus 9. For example, 73 Bytes if a
  220. * 256-bit curve is used. A buffer length of
  221. * #MBEDTLS_ECDSA_MAX_LEN is always safe.
  222. *
  223. * \note If the bitlength of the message hash is larger than the
  224. * bitlength of the group order, then the hash is truncated as
  225. * defined in <em>Standards for Efficient Cryptography Group
  226. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  227. * 4.1.3, step 5.
  228. *
  229. * \see ecp.h
  230. *
  231. * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
  232. * Mbed TLS version 2.0 and later.
  233. *
  234. * \param ctx The ECDSA context.
  235. * \param hash The message hash.
  236. * \param hlen The length of the hash.
  237. * \param sig The buffer that holds the signature.
  238. * \param slen The length of the signature written.
  239. * \param md_alg The MD algorithm used to hash the message.
  240. *
  241. * \return \c 0 on success.
  242. * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  243. * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  244. */
  245. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  246. const unsigned char *hash, size_t hlen,
  247. unsigned char *sig, size_t *slen,
  248. mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
  249. #undef MBEDTLS_DEPRECATED
  250. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  251. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  252. /**
  253. * \brief This function reads and verifies an ECDSA signature.
  254. *
  255. * \note If the bitlength of the message hash is larger than the
  256. * bitlength of the group order, then the hash is truncated as
  257. * defined in <em>Standards for Efficient Cryptography Group
  258. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  259. * 4.1.4, step 3.
  260. *
  261. * \see ecp.h
  262. *
  263. * \param ctx The ECDSA context.
  264. * \param hash The message hash.
  265. * \param hlen The size of the hash.
  266. * \param sig The signature to read and verify.
  267. * \param slen The size of \p sig.
  268. *
  269. * \return \c 0 on success.
  270. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
  271. * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
  272. * signature in \p sig, but its length is less than \p siglen.
  273. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
  274. * error code on failure for any other reason.
  275. */
  276. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  277. const unsigned char *hash, size_t hlen,
  278. const unsigned char *sig, size_t slen );
  279. /**
  280. * \brief This function generates an ECDSA keypair on the given curve.
  281. *
  282. * \see ecp.h
  283. *
  284. * \param ctx The ECDSA context to store the keypair in.
  285. * \param gid The elliptic curve to use. One of the various
  286. * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
  287. * \param f_rng The RNG function.
  288. * \param p_rng The RNG context.
  289. *
  290. * \return \c 0 on success.
  291. * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
  292. */
  293. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  294. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  295. /**
  296. * \brief This function sets an ECDSA context from an EC key pair.
  297. *
  298. * \see ecp.h
  299. *
  300. * \param ctx The ECDSA context to set.
  301. * \param key The EC key to use.
  302. *
  303. * \return \c 0 on success.
  304. * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
  305. */
  306. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
  307. /**
  308. * \brief This function initializes an ECDSA context.
  309. *
  310. * \param ctx The ECDSA context to initialize.
  311. */
  312. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
  313. /**
  314. * \brief This function frees an ECDSA context.
  315. *
  316. * \param ctx The ECDSA context to free.
  317. */
  318. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
  319. #ifdef __cplusplus
  320. }
  321. #endif
  322. #endif /* ecdsa.h */