dsa.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. *
  57. * The DSS routines are based on patches supplied by
  58. * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
  59. #ifndef OPENSSL_HEADER_DSA_H
  60. #define OPENSSL_HEADER_DSA_H
  61. #include <openssl/base.h>
  62. #include <openssl/engine.h>
  63. #include <openssl/ex_data.h>
  64. #include <openssl/thread.h>
  65. #if defined(__cplusplus)
  66. extern "C" {
  67. #endif
  68. // DSA contains functions for signing and verifying with the Digital Signature
  69. // Algorithm.
  70. //
  71. // This module is deprecated and retained for legacy reasons only. It is not
  72. // considered a priority for performance or hardening work. Do not use it in
  73. // new code. Use Ed25519, ECDSA with P-256, or RSA instead.
  74. // Allocation and destruction.
  75. // DSA_new returns a new, empty DSA object or NULL on error.
  76. OPENSSL_EXPORT DSA *DSA_new(void);
  77. // DSA_free decrements the reference count of |dsa| and frees it if the
  78. // reference count drops to zero.
  79. OPENSSL_EXPORT void DSA_free(DSA *dsa);
  80. // DSA_up_ref increments the reference count of |dsa| and returns one.
  81. OPENSSL_EXPORT int DSA_up_ref(DSA *dsa);
  82. // Properties.
  83. // DSA_get0_pub_key returns |dsa|'s public key.
  84. OPENSSL_EXPORT const BIGNUM *DSA_get0_pub_key(const DSA *dsa);
  85. // DSA_get0_priv_key returns |dsa|'s private key, or NULL if |dsa| is a public
  86. // key.
  87. OPENSSL_EXPORT const BIGNUM *DSA_get0_priv_key(const DSA *dsa);
  88. // DSA_get0_p returns |dsa|'s group modulus.
  89. OPENSSL_EXPORT const BIGNUM *DSA_get0_p(const DSA *dsa);
  90. // DSA_get0_q returns the size of |dsa|'s subgroup.
  91. OPENSSL_EXPORT const BIGNUM *DSA_get0_q(const DSA *dsa);
  92. // DSA_get0_g returns |dsa|'s group generator.
  93. OPENSSL_EXPORT const BIGNUM *DSA_get0_g(const DSA *dsa);
  94. // DSA_get0_key sets |*out_pub_key| and |*out_priv_key|, if non-NULL, to |dsa|'s
  95. // public and private key, respectively. If |dsa| is a public key, the private
  96. // key will be set to NULL.
  97. OPENSSL_EXPORT void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
  98. const BIGNUM **out_priv_key);
  99. // DSA_get0_pqg sets |*out_p|, |*out_q|, and |*out_g|, if non-NULL, to |dsa|'s
  100. // p, q, and g parameters, respectively.
  101. OPENSSL_EXPORT void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p,
  102. const BIGNUM **out_q, const BIGNUM **out_g);
  103. // DSA_set0_key sets |dsa|'s public and private key to |pub_key| and |priv_key|,
  104. // respectively, if non-NULL. On success, it takes ownership of each argument
  105. // and returns one. Otherwise, it returns zero.
  106. //
  107. // |priv_key| may be NULL, but |pub_key| must either be non-NULL or already
  108. // configured on |dsa|.
  109. OPENSSL_EXPORT int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key);
  110. // DSA_set0_pqg sets |dsa|'s parameters to |p|, |q|, and |g|, if non-NULL, and
  111. // takes ownership of them. On success, it takes ownership of each argument and
  112. // returns one. Otherwise, it returns zero.
  113. //
  114. // Each argument must either be non-NULL or already configured on |dsa|.
  115. OPENSSL_EXPORT int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  116. // Parameter generation.
  117. // DSA_generate_parameters_ex generates a set of DSA parameters by following
  118. // the procedure given in FIPS 186-4, appendix A.
  119. // (http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)
  120. //
  121. // The larger prime will have a length of |bits| (e.g. 2048). The |seed| value
  122. // allows others to generate and verify the same parameters and should be
  123. // random input which is kept for reference. If |out_counter| or |out_h| are
  124. // not NULL then the counter and h value used in the generation are written to
  125. // them.
  126. //
  127. // The |cb| argument is passed to |BN_generate_prime_ex| and is thus called
  128. // during the generation process in order to indicate progress. See the
  129. // comments for that function for details. In addition to the calls made by
  130. // |BN_generate_prime_ex|, |DSA_generate_parameters_ex| will call it with
  131. // |event| equal to 2 and 3 at different stages of the process.
  132. //
  133. // It returns one on success and zero otherwise.
  134. OPENSSL_EXPORT int DSA_generate_parameters_ex(DSA *dsa, unsigned bits,
  135. const uint8_t *seed,
  136. size_t seed_len, int *out_counter,
  137. unsigned long *out_h,
  138. BN_GENCB *cb);
  139. // DSAparams_dup returns a freshly allocated |DSA| that contains a copy of the
  140. // parameters from |dsa|. It returns NULL on error.
  141. OPENSSL_EXPORT DSA *DSAparams_dup(const DSA *dsa);
  142. // Key generation.
  143. // DSA_generate_key generates a public/private key pair in |dsa|, which must
  144. // already have parameters setup. It returns one on success and zero on
  145. // error.
  146. OPENSSL_EXPORT int DSA_generate_key(DSA *dsa);
  147. // Signatures.
  148. // DSA_SIG_st (aka |DSA_SIG|) contains a DSA signature as a pair of integers.
  149. struct DSA_SIG_st {
  150. BIGNUM *r, *s;
  151. };
  152. // DSA_SIG_new returns a freshly allocated, DIG_SIG structure or NULL on error.
  153. // Both |r| and |s| in the signature will be NULL.
  154. OPENSSL_EXPORT DSA_SIG *DSA_SIG_new(void);
  155. // DSA_SIG_free frees the contents of |sig| and then frees |sig| itself.
  156. OPENSSL_EXPORT void DSA_SIG_free(DSA_SIG *sig);
  157. // DSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two components
  158. // of |sig|.
  159. OPENSSL_EXPORT void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **out_r,
  160. const BIGNUM **out_s);
  161. // DSA_SIG_set0 sets |sig|'s components to |r| and |s|, neither of which may be
  162. // NULL. On success, it takes ownership of each argument and returns one.
  163. // Otherwise, it returns zero.
  164. OPENSSL_EXPORT int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  165. // DSA_do_sign returns a signature of the hash in |digest| by the key in |dsa|
  166. // and returns an allocated, DSA_SIG structure, or NULL on error.
  167. OPENSSL_EXPORT DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len,
  168. const DSA *dsa);
  169. // DSA_do_verify verifies that |sig| is a valid signature, by the public key in
  170. // |dsa|, of the hash in |digest|. It returns one if so, zero if invalid and -1
  171. // on error.
  172. //
  173. // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1
  174. // for valid. However, this is dangerously different to the usual OpenSSL
  175. // convention and could be a disaster if a user did |if (DSA_do_verify(...))|.
  176. // Because of this, |DSA_check_signature| is a safer version of this.
  177. //
  178. // TODO(fork): deprecate.
  179. OPENSSL_EXPORT int DSA_do_verify(const uint8_t *digest, size_t digest_len,
  180. DSA_SIG *sig, const DSA *dsa);
  181. // DSA_do_check_signature sets |*out_valid| to zero. Then it verifies that |sig|
  182. // is a valid signature, by the public key in |dsa| of the hash in |digest|
  183. // and, if so, it sets |*out_valid| to one.
  184. //
  185. // It returns one if it was able to verify the signature as valid or invalid,
  186. // and zero on error.
  187. OPENSSL_EXPORT int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  188. size_t digest_len, DSA_SIG *sig,
  189. const DSA *dsa);
  190. // ASN.1 signatures.
  191. //
  192. // These functions also perform DSA signature operations, but deal with ASN.1
  193. // encoded signatures as opposed to raw |BIGNUM|s. If you don't know what
  194. // encoding a DSA signature is in, it's probably ASN.1.
  195. // DSA_sign signs |digest| with the key in |dsa| and writes the resulting
  196. // signature, in ASN.1 form, to |out_sig| and the length of the signature to
  197. // |*out_siglen|. There must be, at least, |DSA_size(dsa)| bytes of space in
  198. // |out_sig|. It returns one on success and zero otherwise.
  199. //
  200. // (The |type| argument is ignored.)
  201. OPENSSL_EXPORT int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  202. uint8_t *out_sig, unsigned int *out_siglen,
  203. const DSA *dsa);
  204. // DSA_verify verifies that |sig| is a valid, ASN.1 signature, by the public
  205. // key in |dsa|, of the hash in |digest|. It returns one if so, zero if invalid
  206. // and -1 on error.
  207. //
  208. // (The |type| argument is ignored.)
  209. //
  210. // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1
  211. // for valid. However, this is dangerously different to the usual OpenSSL
  212. // convention and could be a disaster if a user did |if (DSA_do_verify(...))|.
  213. // Because of this, |DSA_check_signature| is a safer version of this.
  214. //
  215. // TODO(fork): deprecate.
  216. OPENSSL_EXPORT int DSA_verify(int type, const uint8_t *digest,
  217. size_t digest_len, const uint8_t *sig,
  218. size_t sig_len, const DSA *dsa);
  219. // DSA_check_signature sets |*out_valid| to zero. Then it verifies that |sig|
  220. // is a valid, ASN.1 signature, by the public key in |dsa|, of the hash in
  221. // |digest|. If so, it sets |*out_valid| to one.
  222. //
  223. // It returns one if it was able to verify the signature as valid or invalid,
  224. // and zero on error.
  225. OPENSSL_EXPORT int DSA_check_signature(int *out_valid, const uint8_t *digest,
  226. size_t digest_len, const uint8_t *sig,
  227. size_t sig_len, const DSA *dsa);
  228. // DSA_size returns the size, in bytes, of an ASN.1 encoded, DSA signature
  229. // generated by |dsa|. Parameters must already have been setup in |dsa|.
  230. OPENSSL_EXPORT int DSA_size(const DSA *dsa);
  231. // ASN.1 encoding.
  232. // DSA_SIG_parse parses a DER-encoded DSA-Sig-Value structure from |cbs| and
  233. // advances |cbs|. It returns a newly-allocated |DSA_SIG| or NULL on error.
  234. OPENSSL_EXPORT DSA_SIG *DSA_SIG_parse(CBS *cbs);
  235. // DSA_SIG_marshal marshals |sig| as a DER-encoded DSA-Sig-Value and appends the
  236. // result to |cbb|. It returns one on success and zero on error.
  237. OPENSSL_EXPORT int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig);
  238. // DSA_parse_public_key parses a DER-encoded DSA public key from |cbs| and
  239. // advances |cbs|. It returns a newly-allocated |DSA| or NULL on error.
  240. OPENSSL_EXPORT DSA *DSA_parse_public_key(CBS *cbs);
  241. // DSA_marshal_public_key marshals |dsa| as a DER-encoded DSA public key and
  242. // appends the result to |cbb|. It returns one on success and zero on
  243. // failure.
  244. OPENSSL_EXPORT int DSA_marshal_public_key(CBB *cbb, const DSA *dsa);
  245. // DSA_parse_private_key parses a DER-encoded DSA private key from |cbs| and
  246. // advances |cbs|. It returns a newly-allocated |DSA| or NULL on error.
  247. OPENSSL_EXPORT DSA *DSA_parse_private_key(CBS *cbs);
  248. // DSA_marshal_private_key marshals |dsa| as a DER-encoded DSA private key and
  249. // appends the result to |cbb|. It returns one on success and zero on
  250. // failure.
  251. OPENSSL_EXPORT int DSA_marshal_private_key(CBB *cbb, const DSA *dsa);
  252. // DSA_parse_parameters parses a DER-encoded Dss-Parms structure (RFC 3279)
  253. // from |cbs| and advances |cbs|. It returns a newly-allocated |DSA| or NULL on
  254. // error.
  255. OPENSSL_EXPORT DSA *DSA_parse_parameters(CBS *cbs);
  256. // DSA_marshal_parameters marshals |dsa| as a DER-encoded Dss-Parms structure
  257. // (RFC 3279) and appends the result to |cbb|. It returns one on success and
  258. // zero on failure.
  259. OPENSSL_EXPORT int DSA_marshal_parameters(CBB *cbb, const DSA *dsa);
  260. // Conversion.
  261. // DSA_dup_DH returns a |DH| constructed from the parameters of |dsa|. This is
  262. // sometimes needed when Diffie-Hellman parameters are stored in the form of
  263. // DSA parameters. It returns an allocated |DH| on success or NULL on error.
  264. OPENSSL_EXPORT DH *DSA_dup_DH(const DSA *dsa);
  265. // ex_data functions.
  266. //
  267. // See |ex_data.h| for details.
  268. OPENSSL_EXPORT int DSA_get_ex_new_index(long argl, void *argp,
  269. CRYPTO_EX_unused *unused,
  270. CRYPTO_EX_dup *dup_unused,
  271. CRYPTO_EX_free *free_func);
  272. OPENSSL_EXPORT int DSA_set_ex_data(DSA *dsa, int idx, void *arg);
  273. OPENSSL_EXPORT void *DSA_get_ex_data(const DSA *dsa, int idx);
  274. // Deprecated functions.
  275. // d2i_DSA_SIG parses a DER-encoded DSA-Sig-Value structure from |len| bytes at
  276. // |*inp|, as described in |d2i_SAMPLE|.
  277. //
  278. // Use |DSA_SIG_parse| instead.
  279. OPENSSL_EXPORT DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp,
  280. long len);
  281. // i2d_DSA_SIG marshals |in| to a DER-encoded DSA-Sig-Value structure, as
  282. // described in |i2d_SAMPLE|.
  283. //
  284. // Use |DSA_SIG_marshal| instead.
  285. OPENSSL_EXPORT int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp);
  286. // d2i_DSAPublicKey parses a DER-encoded DSA public key from |len| bytes at
  287. // |*inp|, as described in |d2i_SAMPLE|.
  288. //
  289. // Use |DSA_parse_public_key| instead.
  290. OPENSSL_EXPORT DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len);
  291. // i2d_DSAPublicKey marshals |in| as a DER-encoded DSA public key, as described
  292. // in |i2d_SAMPLE|.
  293. //
  294. // Use |DSA_marshal_public_key| instead.
  295. OPENSSL_EXPORT int i2d_DSAPublicKey(const DSA *in, uint8_t **outp);
  296. // d2i_DSAPrivateKey parses a DER-encoded DSA private key from |len| bytes at
  297. // |*inp|, as described in |d2i_SAMPLE|.
  298. //
  299. // Use |DSA_parse_private_key| instead.
  300. OPENSSL_EXPORT DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len);
  301. // i2d_DSAPrivateKey marshals |in| as a DER-encoded DSA private key, as
  302. // described in |i2d_SAMPLE|.
  303. //
  304. // Use |DSA_marshal_private_key| instead.
  305. OPENSSL_EXPORT int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp);
  306. // d2i_DSAparams parses a DER-encoded Dss-Parms structure (RFC 3279) from |len|
  307. // bytes at |*inp|, as described in |d2i_SAMPLE|.
  308. //
  309. // Use |DSA_parse_parameters| instead.
  310. OPENSSL_EXPORT DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len);
  311. // i2d_DSAparams marshals |in|'s parameters as a DER-encoded Dss-Parms structure
  312. // (RFC 3279), as described in |i2d_SAMPLE|.
  313. //
  314. // Use |DSA_marshal_parameters| instead.
  315. OPENSSL_EXPORT int i2d_DSAparams(const DSA *in, uint8_t **outp);
  316. // DSA_generate_parameters is a deprecated version of
  317. // |DSA_generate_parameters_ex| that creates and returns a |DSA*|. Don't use
  318. // it.
  319. OPENSSL_EXPORT DSA *DSA_generate_parameters(int bits, unsigned char *seed,
  320. int seed_len, int *counter_ret,
  321. unsigned long *h_ret,
  322. void (*callback)(int, int, void *),
  323. void *cb_arg);
  324. struct dsa_st {
  325. long version;
  326. BIGNUM *p;
  327. BIGNUM *q; // == 20
  328. BIGNUM *g;
  329. BIGNUM *pub_key; // y public key
  330. BIGNUM *priv_key; // x private key
  331. int flags;
  332. // Normally used to cache montgomery values
  333. CRYPTO_MUTEX method_mont_lock;
  334. BN_MONT_CTX *method_mont_p;
  335. BN_MONT_CTX *method_mont_q;
  336. CRYPTO_refcount_t references;
  337. CRYPTO_EX_DATA ex_data;
  338. };
  339. #if defined(__cplusplus)
  340. } // extern C
  341. extern "C++" {
  342. BSSL_NAMESPACE_BEGIN
  343. BORINGSSL_MAKE_DELETER(DSA, DSA_free)
  344. BORINGSSL_MAKE_UP_REF(DSA, DSA_up_ref)
  345. BORINGSSL_MAKE_DELETER(DSA_SIG, DSA_SIG_free)
  346. BSSL_NAMESPACE_END
  347. } // extern C++
  348. #endif
  349. #define DSA_R_BAD_Q_VALUE 100
  350. #define DSA_R_MISSING_PARAMETERS 101
  351. #define DSA_R_MODULUS_TOO_LARGE 102
  352. #define DSA_R_NEED_NEW_SETUP_VALUES 103
  353. #define DSA_R_BAD_VERSION 104
  354. #define DSA_R_DECODE_ERROR 105
  355. #define DSA_R_ENCODE_ERROR 106
  356. #define DSA_R_INVALID_PARAMETERS 107
  357. #endif // OPENSSL_HEADER_DSA_H