ecdsa.h 29 KB

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