ecdsa.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* ====================================================================
  2. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #ifndef OPENSSL_HEADER_ECDSA_H
  53. #define OPENSSL_HEADER_ECDSA_H
  54. #include <openssl/base.h>
  55. #include <openssl/ec_key.h>
  56. #if defined(__cplusplus)
  57. extern "C" {
  58. #endif
  59. // ECDSA contains functions for signing and verifying with the Digital Signature
  60. // Algorithm over elliptic curves.
  61. // Signing and verifying.
  62. // ECDSA_sign signs |digest_len| bytes from |digest| with |key| and writes the
  63. // resulting signature to |sig|, which must have |ECDSA_size(key)| bytes of
  64. // space. On successful exit, |*sig_len| is set to the actual number of bytes
  65. // written. The |type| argument should be zero. It returns one on success and
  66. // zero otherwise.
  67. //
  68. // WARNING: |digest| must be the output of some hash function on the data to be
  69. // signed. Passing unhashed inputs will not result in a secure signature scheme.
  70. OPENSSL_EXPORT int ECDSA_sign(int type, const uint8_t *digest,
  71. size_t digest_len, uint8_t *sig,
  72. unsigned int *sig_len, const EC_KEY *key);
  73. // ECDSA_verify verifies that |sig_len| bytes from |sig| constitute a valid
  74. // signature by |key| of |digest|. (The |type| argument should be zero.) It
  75. // returns one on success or zero if the signature is invalid or an error
  76. // occurred.
  77. //
  78. // WARNING: |digest| must be the output of some hash function on the data to be
  79. // verified. Passing unhashed inputs will not result in a secure signature
  80. // scheme.
  81. OPENSSL_EXPORT int ECDSA_verify(int type, const uint8_t *digest,
  82. size_t digest_len, const uint8_t *sig,
  83. size_t sig_len, const EC_KEY *key);
  84. // ECDSA_size returns the maximum size of an ECDSA signature using |key|. It
  85. // returns zero if |key| is NULL or if it doesn't have a group set.
  86. OPENSSL_EXPORT size_t ECDSA_size(const EC_KEY *key);
  87. // Low-level signing and verification.
  88. //
  89. // Low-level functions handle signatures as |ECDSA_SIG| structures which allow
  90. // the two values in an ECDSA signature to be handled separately.
  91. struct ecdsa_sig_st {
  92. BIGNUM *r;
  93. BIGNUM *s;
  94. };
  95. // ECDSA_SIG_new returns a fresh |ECDSA_SIG| structure or NULL on error.
  96. OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_new(void);
  97. // ECDSA_SIG_free frees |sig| its member |BIGNUM|s.
  98. OPENSSL_EXPORT void ECDSA_SIG_free(ECDSA_SIG *sig);
  99. // ECDSA_SIG_get0_r returns the r component of |sig|.
  100. OPENSSL_EXPORT const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
  101. // ECDSA_SIG_get0_s returns the s component of |sig|.
  102. OPENSSL_EXPORT const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
  103. // ECDSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two
  104. // components of |sig|.
  105. OPENSSL_EXPORT void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r,
  106. const BIGNUM **out_s);
  107. // ECDSA_SIG_set0 sets |sig|'s components to |r| and |s|, neither of which may
  108. // be NULL. On success, it takes ownership of each argument and returns one.
  109. // Otherwise, it returns zero.
  110. OPENSSL_EXPORT int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  111. // ECDSA_do_sign signs |digest_len| bytes from |digest| with |key| and returns
  112. // the resulting signature structure, or NULL on error.
  113. //
  114. // WARNING: |digest| must be the output of some hash function on the data to be
  115. // signed. Passing unhashed inputs will not result in a secure signature scheme.
  116. OPENSSL_EXPORT ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest,
  117. size_t digest_len, const EC_KEY *key);
  118. // ECDSA_do_verify verifies that |sig| constitutes a valid signature by |key|
  119. // of |digest|. It returns one on success or zero if the signature is invalid
  120. // or on error.
  121. //
  122. // WARNING: |digest| must be the output of some hash function on the data to be
  123. // verified. Passing unhashed inputs will not result in a secure signature
  124. // scheme.
  125. OPENSSL_EXPORT int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
  126. const ECDSA_SIG *sig, const EC_KEY *key);
  127. // ASN.1 functions.
  128. // ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from |cbs| and
  129. // advances |cbs|. It returns a newly-allocated |ECDSA_SIG| or NULL on error.
  130. OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_parse(CBS *cbs);
  131. // ECDSA_SIG_from_bytes parses |in| as a DER-encoded ECDSA-Sig-Value structure.
  132. // It returns a newly-allocated |ECDSA_SIG| structure or NULL on error.
  133. OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_from_bytes(const uint8_t *in,
  134. size_t in_len);
  135. // ECDSA_SIG_marshal marshals |sig| as a DER-encoded ECDSA-Sig-Value and appends
  136. // the result to |cbb|. It returns one on success and zero on error.
  137. OPENSSL_EXPORT int ECDSA_SIG_marshal(CBB *cbb, const ECDSA_SIG *sig);
  138. // ECDSA_SIG_to_bytes marshals |sig| as a DER-encoded ECDSA-Sig-Value and, on
  139. // success, sets |*out_bytes| to a newly allocated buffer containing the result
  140. // and returns one. Otherwise, it returns zero. The result should be freed with
  141. // |OPENSSL_free|.
  142. OPENSSL_EXPORT int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len,
  143. const ECDSA_SIG *sig);
  144. // ECDSA_SIG_max_len returns the maximum length of a DER-encoded ECDSA-Sig-Value
  145. // structure for a group whose order is represented in |order_len| bytes, or
  146. // zero on overflow.
  147. OPENSSL_EXPORT size_t ECDSA_SIG_max_len(size_t order_len);
  148. // Testing-only functions.
  149. // ECDSA_sign_with_nonce_and_leak_private_key_for_testing behaves like
  150. // |ECDSA_do_sign| but uses |nonce| for the ECDSA nonce 'k', instead of a random
  151. // value. |nonce| is interpreted as a big-endian integer. It must be reduced
  152. // modulo the group order and padded with zeros up to |BN_num_bytes(order)|
  153. // bytes.
  154. //
  155. // WARNING: This function is only exported for testing purposes, when using test
  156. // vectors or fuzzing strategies. It must not be used outside tests and may leak
  157. // any private keys it is used with.
  158. OPENSSL_EXPORT ECDSA_SIG *
  159. ECDSA_sign_with_nonce_and_leak_private_key_for_testing(const uint8_t *digest,
  160. size_t digest_len,
  161. const EC_KEY *eckey,
  162. const uint8_t *nonce,
  163. size_t nonce_len);
  164. // Deprecated functions.
  165. // d2i_ECDSA_SIG parses aa DER-encoded ECDSA-Sig-Value structure from |len|
  166. // bytes at |*inp|, as described in |d2i_SAMPLE|.
  167. //
  168. // Use |ECDSA_SIG_parse| instead.
  169. OPENSSL_EXPORT ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **out, const uint8_t **inp,
  170. long len);
  171. // i2d_ECDSA_SIG marshals |sig| as a DER-encoded ECDSA-Sig-Value, as described
  172. // in |i2d_SAMPLE|.
  173. //
  174. // Use |ECDSA_SIG_marshal| instead.
  175. OPENSSL_EXPORT int i2d_ECDSA_SIG(const ECDSA_SIG *sig, uint8_t **outp);
  176. #if defined(__cplusplus)
  177. } // extern C
  178. extern "C++" {
  179. BSSL_NAMESPACE_BEGIN
  180. BORINGSSL_MAKE_DELETER(ECDSA_SIG, ECDSA_SIG_free)
  181. BSSL_NAMESPACE_END
  182. } // extern C++
  183. #endif
  184. #define ECDSA_R_BAD_SIGNATURE 100
  185. #define ECDSA_R_MISSING_PARAMETERS 101
  186. #define ECDSA_R_NEED_NEW_SETUP_VALUES 102
  187. #define ECDSA_R_NOT_IMPLEMENTED 103
  188. #define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED 104
  189. #define ECDSA_R_ENCODE_ERROR 105
  190. #endif // OPENSSL_HEADER_ECDSA_H