ecdsa.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 The Mbed TLS Contributors
  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. #ifndef MBEDTLS_ECDSA_H
  30. #define MBEDTLS_ECDSA_H
  31. #if !defined(MBEDTLS_CONFIG_FILE)
  32. #include "mbedtls/config.h"
  33. #else
  34. #include MBEDTLS_CONFIG_FILE
  35. #endif
  36. #include "mbedtls/ecp.h"
  37. #include "mbedtls/md.h"
  38. /**
  39. * \brief Maximum ECDSA signature size for a given curve bit size
  40. *
  41. * \param bits Curve size in bits
  42. * \return Maximum signature size in bytes
  43. *
  44. * \note This macro returns a compile-time constant if its argument
  45. * is one. It may evaluate its argument multiple times.
  46. */
  47. /*
  48. * Ecdsa-Sig-Value ::= SEQUENCE {
  49. * r INTEGER,
  50. * s INTEGER
  51. * }
  52. *
  53. * For each of r and s, the value (V) may include an extra initial "0" bit.
  54. */
  55. #define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
  56. ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
  57. /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
  58. /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
  59. /** The maximal size of an ECDSA signature in Bytes. */
  60. #define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /* NXP added for HW accelerators support */
  65. #if !defined(MBEDTLS_ECDSA_ALT)
  66. /**
  67. * \brief The ECDSA context structure.
  68. *
  69. * \warning Performing multiple operations concurrently on the same
  70. * ECDSA context is not supported; objects of this type
  71. * should not be shared between multiple threads.
  72. */
  73. typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
  74. #else /* !MBEDTLS_ECDSA_ALT */
  75. #include "ecdsa_alt.h"
  76. #endif /* !MBEDTLS_ECDSA_ALT */
  77. #if defined(MBEDTLS_ECP_RESTARTABLE)
  78. /**
  79. * \brief Internal restart context for ecdsa_verify()
  80. *
  81. * \note Opaque struct, defined in ecdsa.c
  82. */
  83. typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
  84. /**
  85. * \brief Internal restart context for ecdsa_sign()
  86. *
  87. * \note Opaque struct, defined in ecdsa.c
  88. */
  89. typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
  90. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  91. /**
  92. * \brief Internal restart context for ecdsa_sign_det()
  93. *
  94. * \note Opaque struct, defined in ecdsa.c
  95. */
  96. typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
  97. #endif
  98. /**
  99. * \brief General context for resuming ECDSA operations
  100. */
  101. typedef struct
  102. {
  103. mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
  104. shared administrative info */
  105. mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
  106. mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
  107. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  108. mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
  109. #endif
  110. } mbedtls_ecdsa_restart_ctx;
  111. #else /* MBEDTLS_ECP_RESTARTABLE */
  112. /* Now we can declare functions that take a pointer to that */
  113. typedef void mbedtls_ecdsa_restart_ctx;
  114. #endif /* MBEDTLS_ECP_RESTARTABLE */
  115. /**
  116. * \brief This function checks whether a given group can be used
  117. * for ECDSA.
  118. *
  119. * \param gid The ECP group ID to check.
  120. *
  121. * \return \c 1 if the group can be used, \c 0 otherwise
  122. */
  123. int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid );
  124. /**
  125. * \brief This function computes the ECDSA signature of a
  126. * previously-hashed message.
  127. *
  128. * \note The deterministic version implemented in
  129. * mbedtls_ecdsa_sign_det() is usually preferred.
  130. *
  131. * \note If the bitlength of the message hash is larger than the
  132. * bitlength of the group order, then the hash is truncated
  133. * as defined in <em>Standards for Efficient Cryptography Group
  134. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  135. * 4.1.3, step 5.
  136. *
  137. * \see ecp.h
  138. *
  139. * \param grp The context for the elliptic curve to use.
  140. * This must be initialized and have group parameters
  141. * set, for example through mbedtls_ecp_group_load().
  142. * \param r The MPI context in which to store the first part
  143. * the signature. This must be initialized.
  144. * \param s The MPI context in which to store the second part
  145. * the signature. This must be initialized.
  146. * \param d The private signing key. This must be initialized.
  147. * \param buf The content to be signed. This is usually the hash of
  148. * the original data to be signed. This must be a readable
  149. * buffer of length \p blen Bytes. It may be \c NULL if
  150. * \p blen is zero.
  151. * \param blen The length of \p buf in Bytes.
  152. * \param f_rng The RNG function. This must not be \c NULL.
  153. * \param p_rng The RNG context to be passed to \p f_rng. This may be
  154. * \c NULL if \p f_rng doesn't need a context parameter.
  155. *
  156. * \return \c 0 on success.
  157. * \return An \c MBEDTLS_ERR_ECP_XXX
  158. * or \c MBEDTLS_MPI_XXX error code on failure.
  159. */
  160. int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  161. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  162. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  163. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  164. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  165. #if defined(MBEDTLS_DEPRECATED_WARNING)
  166. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  167. #else
  168. #define MBEDTLS_DEPRECATED
  169. #endif
  170. /**
  171. * \brief This function computes the ECDSA signature of a
  172. * previously-hashed message, deterministic version.
  173. *
  174. * For more information, see <em>RFC-6979: Deterministic
  175. * Usage of the Digital Signature Algorithm (DSA) and Elliptic
  176. * Curve Digital Signature Algorithm (ECDSA)</em>.
  177. *
  178. * \note If the bitlength of the message hash is larger than the
  179. * bitlength of the group order, then the hash is truncated as
  180. * defined in <em>Standards for Efficient Cryptography Group
  181. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  182. * 4.1.3, step 5.
  183. *
  184. * \warning Since the output of the internal RNG is always the same for
  185. * the same key and message, this limits the efficiency of
  186. * blinding and leaks information through side channels. For
  187. * secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
  188. *
  189. * (Optimally the blinding is a random value that is different
  190. * on every execution. In this case the blinding is still
  191. * random from the attackers perspective, but is the same on
  192. * each execution. This means that this blinding does not
  193. * prevent attackers from recovering secrets by combining
  194. * several measurement traces, but may prevent some attacks
  195. * that exploit relationships between secret data.)
  196. *
  197. * \see ecp.h
  198. *
  199. * \param grp The context for the elliptic curve to use.
  200. * This must be initialized and have group parameters
  201. * set, for example through mbedtls_ecp_group_load().
  202. * \param r The MPI context in which to store the first part
  203. * the signature. This must be initialized.
  204. * \param s The MPI context in which to store the second part
  205. * the signature. This must be initialized.
  206. * \param d The private signing key. This must be initialized
  207. * and setup, for example through mbedtls_ecp_gen_privkey().
  208. * \param buf The hashed content to be signed. This must be a readable
  209. * buffer of length \p blen Bytes. It may be \c NULL if
  210. * \p blen is zero.
  211. * \param blen The length of \p buf in Bytes.
  212. * \param md_alg The hash algorithm used to hash the original data.
  213. *
  214. * \return \c 0 on success.
  215. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  216. * error code on failure.
  217. */
  218. int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  219. mbedtls_mpi *s, const mbedtls_mpi *d,
  220. const unsigned char *buf, size_t blen,
  221. mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
  222. #undef MBEDTLS_DEPRECATED
  223. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  224. /**
  225. * \brief This function computes the ECDSA signature of a
  226. * previously-hashed message, deterministic version.
  227. *
  228. * For more information, see <em>RFC-6979: Deterministic
  229. * Usage of the Digital Signature Algorithm (DSA) and Elliptic
  230. * Curve Digital Signature Algorithm (ECDSA)</em>.
  231. *
  232. * \note If the bitlength of the message hash is larger than the
  233. * bitlength of the group order, then the hash is truncated as
  234. * defined in <em>Standards for Efficient Cryptography Group
  235. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  236. * 4.1.3, step 5.
  237. *
  238. * \see ecp.h
  239. *
  240. * \param grp The context for the elliptic curve to use.
  241. * This must be initialized and have group parameters
  242. * set, for example through mbedtls_ecp_group_load().
  243. * \param r The MPI context in which to store the first part
  244. * the signature. This must be initialized.
  245. * \param s The MPI context in which to store the second part
  246. * the signature. This must be initialized.
  247. * \param d The private signing key. This must be initialized
  248. * and setup, for example through mbedtls_ecp_gen_privkey().
  249. * \param buf The hashed content to be signed. This must be a readable
  250. * buffer of length \p blen Bytes. It may be \c NULL if
  251. * \p blen is zero.
  252. * \param blen The length of \p buf in Bytes.
  253. * \param md_alg The hash algorithm used to hash the original data.
  254. * \param f_rng_blind The RNG function used for blinding. This must not be
  255. * \c NULL.
  256. * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
  257. * \c NULL if \p f_rng doesn't need a context parameter.
  258. *
  259. * \return \c 0 on success.
  260. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  261. * error code on failure.
  262. */
  263. int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
  264. mbedtls_mpi *s, const mbedtls_mpi *d,
  265. const unsigned char *buf, size_t blen,
  266. mbedtls_md_type_t md_alg,
  267. int (*f_rng_blind)(void *, unsigned char *, size_t),
  268. void *p_rng_blind );
  269. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  270. /**
  271. * \brief This function verifies the ECDSA signature of a
  272. * previously-hashed message.
  273. *
  274. * \note If the bitlength of the message hash is larger than the
  275. * bitlength of the group order, then the hash is truncated as
  276. * defined in <em>Standards for Efficient Cryptography Group
  277. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  278. * 4.1.4, step 3.
  279. *
  280. * \see ecp.h
  281. *
  282. * \param grp The ECP group to use.
  283. * This must be initialized and have group parameters
  284. * set, for example through mbedtls_ecp_group_load().
  285. * \param buf The hashed content that was signed. This must be a readable
  286. * buffer of length \p blen Bytes. It may be \c NULL if
  287. * \p blen is zero.
  288. * \param blen The length of \p buf in Bytes.
  289. * \param Q The public key to use for verification. This must be
  290. * initialized and setup.
  291. * \param r The first integer of the signature.
  292. * This must be initialized.
  293. * \param s The second integer of the signature.
  294. * This must be initialized.
  295. *
  296. * \return \c 0 on success.
  297. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
  298. * is invalid.
  299. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
  300. * error code on failure for any other reason.
  301. */
  302. int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
  303. const unsigned char *buf, size_t blen,
  304. const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
  305. const mbedtls_mpi *s);
  306. /**
  307. * \brief This function computes the ECDSA signature and writes it
  308. * to a buffer, serialized as defined in <em>RFC-4492:
  309. * Elliptic Curve Cryptography (ECC) Cipher Suites for
  310. * Transport Layer Security (TLS)</em>.
  311. *
  312. * \warning It is not thread-safe to use the same context in
  313. * multiple threads.
  314. *
  315. * \note The deterministic version is used if
  316. * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
  317. * information, see <em>RFC-6979: Deterministic Usage
  318. * of the Digital Signature Algorithm (DSA) and Elliptic
  319. * Curve Digital Signature Algorithm (ECDSA)</em>.
  320. *
  321. * \note If the bitlength of the message hash is larger than the
  322. * bitlength of the group order, then the hash is truncated as
  323. * defined in <em>Standards for Efficient Cryptography Group
  324. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  325. * 4.1.3, step 5.
  326. *
  327. * \see ecp.h
  328. *
  329. * \param ctx The ECDSA context to use. This must be initialized
  330. * and have a group and private key bound to it, for example
  331. * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
  332. * \param md_alg The message digest that was used to hash the message.
  333. * \param hash The message hash to be signed. This must be a readable
  334. * buffer of length \p blen Bytes.
  335. * \param hlen The length of the hash \p hash in Bytes.
  336. * \param sig The buffer to which to write the signature. This must be a
  337. * writable buffer of length at least twice as large as the
  338. * size of the curve used, plus 9. For example, 73 Bytes if
  339. * a 256-bit curve is used. A buffer length of
  340. * #MBEDTLS_ECDSA_MAX_LEN is always safe.
  341. * \param slen The address at which to store the actual length of
  342. * the signature written. Must not be \c NULL.
  343. * \param f_rng The RNG function. This must not be \c NULL if
  344. * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
  345. * it is used only for blinding and may be set to \c NULL, but
  346. * doing so is DEPRECATED.
  347. * \param p_rng The RNG context to be passed to \p f_rng. This may be
  348. * \c NULL if \p f_rng is \c NULL or doesn't use a context.
  349. *
  350. * \return \c 0 on success.
  351. * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  352. * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  353. */
  354. int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
  355. mbedtls_md_type_t md_alg,
  356. const unsigned char *hash, size_t hlen,
  357. unsigned char *sig, size_t *slen,
  358. int (*f_rng)(void *, unsigned char *, size_t),
  359. void *p_rng );
  360. /**
  361. * \brief This function computes the ECDSA signature and writes it
  362. * to a buffer, in a restartable way.
  363. *
  364. * \see \c mbedtls_ecdsa_write_signature()
  365. *
  366. * \note This function is like \c mbedtls_ecdsa_write_signature()
  367. * but it can return early and restart according to the limit
  368. * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  369. *
  370. * \param ctx The ECDSA context to use. This must be initialized
  371. * and have a group and private key bound to it, for example
  372. * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
  373. * \param md_alg The message digest that was used to hash the message.
  374. * \param hash The message hash to be signed. This must be a readable
  375. * buffer of length \p blen Bytes.
  376. * \param hlen The length of the hash \p hash in Bytes.
  377. * \param sig The buffer to which to write the signature. This must be a
  378. * writable buffer of length at least twice as large as the
  379. * size of the curve used, plus 9. For example, 73 Bytes if
  380. * a 256-bit curve is used. A buffer length of
  381. * #MBEDTLS_ECDSA_MAX_LEN is always safe.
  382. * \param slen The address at which to store the actual length of
  383. * the signature written. Must not be \c NULL.
  384. * \param f_rng The RNG function. This must not be \c NULL if
  385. * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
  386. * it is unused and may be set to \c NULL.
  387. * \param p_rng The RNG context to be passed to \p f_rng. This may be
  388. * \c NULL if \p f_rng is \c NULL or doesn't use a context.
  389. * \param rs_ctx The restart context to use. This may be \c NULL to disable
  390. * restarting. If it is not \c NULL, it must point to an
  391. * initialized restart context.
  392. *
  393. * \return \c 0 on success.
  394. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  395. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  396. * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  397. * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  398. */
  399. int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
  400. mbedtls_md_type_t md_alg,
  401. const unsigned char *hash, size_t hlen,
  402. unsigned char *sig, size_t *slen,
  403. int (*f_rng)(void *, unsigned char *, size_t),
  404. void *p_rng,
  405. mbedtls_ecdsa_restart_ctx *rs_ctx );
  406. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  407. #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
  408. #if defined(MBEDTLS_DEPRECATED_WARNING)
  409. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  410. #else
  411. #define MBEDTLS_DEPRECATED
  412. #endif
  413. /**
  414. * \brief This function computes an ECDSA signature and writes
  415. * it to a buffer, serialized as defined in <em>RFC-4492:
  416. * Elliptic Curve Cryptography (ECC) Cipher Suites for
  417. * Transport Layer Security (TLS)</em>.
  418. *
  419. * The deterministic version is defined in <em>RFC-6979:
  420. * Deterministic Usage of the Digital Signature Algorithm (DSA)
  421. * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
  422. *
  423. * \warning It is not thread-safe to use the same context in
  424. * multiple threads.
  425. *
  426. * \note If the bitlength of the message hash is larger than the
  427. * bitlength of the group order, then the hash is truncated as
  428. * defined in <em>Standards for Efficient Cryptography Group
  429. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  430. * 4.1.3, step 5.
  431. *
  432. * \see ecp.h
  433. *
  434. * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
  435. * Mbed TLS version 2.0 and later.
  436. *
  437. * \param ctx The ECDSA context to use. This must be initialized
  438. * and have a group and private key bound to it, for example
  439. * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
  440. * \param hash The message hash to be signed. This must be a readable
  441. * buffer of length \p blen Bytes.
  442. * \param hlen The length of the hash \p hash in Bytes.
  443. * \param sig The buffer to which to write the signature. This must be a
  444. * writable buffer of length at least twice as large as the
  445. * size of the curve used, plus 9. For example, 73 Bytes if
  446. * a 256-bit curve is used. A buffer length of
  447. * #MBEDTLS_ECDSA_MAX_LEN is always safe.
  448. * \param slen The address at which to store the actual length of
  449. * the signature written. Must not be \c NULL.
  450. * \param md_alg The message digest that was used to hash the message.
  451. *
  452. * \return \c 0 on success.
  453. * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
  454. * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
  455. */
  456. int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
  457. const unsigned char *hash, size_t hlen,
  458. unsigned char *sig, size_t *slen,
  459. mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
  460. #undef MBEDTLS_DEPRECATED
  461. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  462. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  463. /**
  464. * \brief This function reads and verifies an ECDSA signature.
  465. *
  466. * \note If the bitlength of the message hash is larger than the
  467. * bitlength of the group order, then the hash is truncated as
  468. * defined in <em>Standards for Efficient Cryptography Group
  469. * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
  470. * 4.1.4, step 3.
  471. *
  472. * \see ecp.h
  473. *
  474. * \param ctx The ECDSA context to use. This must be initialized
  475. * and have a group and public key bound to it.
  476. * \param hash The message hash that was signed. This must be a readable
  477. * buffer of length \p size Bytes.
  478. * \param hlen The size of the hash \p hash.
  479. * \param sig The signature to read and verify. This must be a readable
  480. * buffer of length \p slen Bytes.
  481. * \param slen The size of \p sig in Bytes.
  482. *
  483. * \return \c 0 on success.
  484. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
  485. * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
  486. * signature in \p sig, but its length is less than \p siglen.
  487. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
  488. * error code on failure for any other reason.
  489. */
  490. int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
  491. const unsigned char *hash, size_t hlen,
  492. const unsigned char *sig, size_t slen );
  493. /**
  494. * \brief This function reads and verifies an ECDSA signature,
  495. * in a restartable way.
  496. *
  497. * \see \c mbedtls_ecdsa_read_signature()
  498. *
  499. * \note This function is like \c mbedtls_ecdsa_read_signature()
  500. * but it can return early and restart according to the limit
  501. * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  502. *
  503. * \param ctx The ECDSA context to use. This must be initialized
  504. * and have a group and public key bound to it.
  505. * \param hash The message hash that was signed. This must be a readable
  506. * buffer of length \p size Bytes.
  507. * \param hlen The size of the hash \p hash.
  508. * \param sig The signature to read and verify. This must be a readable
  509. * buffer of length \p slen Bytes.
  510. * \param slen The size of \p sig in Bytes.
  511. * \param rs_ctx The restart context to use. This may be \c NULL to disable
  512. * restarting. If it is not \c NULL, it must point to an
  513. * initialized restart context.
  514. *
  515. * \return \c 0 on success.
  516. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
  517. * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
  518. * signature in \p sig, but its length is less than \p siglen.
  519. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  520. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  521. * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
  522. * error code on failure for any other reason.
  523. */
  524. int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
  525. const unsigned char *hash, size_t hlen,
  526. const unsigned char *sig, size_t slen,
  527. mbedtls_ecdsa_restart_ctx *rs_ctx );
  528. /**
  529. * \brief This function generates an ECDSA keypair on the given curve.
  530. *
  531. * \see ecp.h
  532. *
  533. * \param ctx The ECDSA context to store the keypair in.
  534. * This must be initialized.
  535. * \param gid The elliptic curve to use. One of the various
  536. * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
  537. * \param f_rng The RNG function to use. This must not be \c NULL.
  538. * \param p_rng The RNG context to be passed to \p f_rng. This may be
  539. * \c NULL if \p f_rng doesn't need a context argument.
  540. *
  541. * \return \c 0 on success.
  542. * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
  543. */
  544. int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  545. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  546. /**
  547. * \brief This function sets up an ECDSA context from an EC key pair.
  548. *
  549. * \see ecp.h
  550. *
  551. * \param ctx The ECDSA context to setup. This must be initialized.
  552. * \param key The EC key to use. This must be initialized and hold
  553. * a private-public key pair or a public key. In the former
  554. * case, the ECDSA context may be used for signature creation
  555. * and verification after this call. In the latter case, it
  556. * may be used for signature verification.
  557. *
  558. * \return \c 0 on success.
  559. * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
  560. */
  561. int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
  562. const mbedtls_ecp_keypair *key );
  563. /**
  564. * \brief This function initializes an ECDSA context.
  565. *
  566. * \param ctx The ECDSA context to initialize.
  567. * This must not be \c NULL.
  568. */
  569. void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
  570. /**
  571. * \brief This function frees an ECDSA context.
  572. *
  573. * \param ctx The ECDSA context to free. This may be \c NULL,
  574. * in which case this function does nothing. If it
  575. * is not \c NULL, it must be initialized.
  576. */
  577. void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
  578. #if defined(MBEDTLS_ECP_RESTARTABLE)
  579. /**
  580. * \brief Initialize a restart context.
  581. *
  582. * \param ctx The restart context to initialize.
  583. * This must not be \c NULL.
  584. */
  585. void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
  586. /**
  587. * \brief Free the components of a restart context.
  588. *
  589. * \param ctx The restart context to free. This may be \c NULL,
  590. * in which case this function does nothing. If it
  591. * is not \c NULL, it must be initialized.
  592. */
  593. void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
  594. #endif /* MBEDTLS_ECP_RESTARTABLE */
  595. #ifdef __cplusplus
  596. }
  597. #endif
  598. #endif /* ecdsa.h */