You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

436 lines
18 KiB

  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. // Allocation and destruction.
  71. // DSA_new returns a new, empty DSA object or NULL on error.
  72. OPENSSL_EXPORT DSA *DSA_new(void);
  73. // DSA_free decrements the reference count of |dsa| and frees it if the
  74. // reference count drops to zero.
  75. OPENSSL_EXPORT void DSA_free(DSA *dsa);
  76. // DSA_up_ref increments the reference count of |dsa| and returns one.
  77. OPENSSL_EXPORT int DSA_up_ref(DSA *dsa);
  78. // Properties.
  79. // DSA_get0_key sets |*out_pub_key| and |*out_priv_key|, if non-NULL, to |dsa|'s
  80. // public and private key, respectively. If |dsa| is a public key, the private
  81. // key will be set to NULL.
  82. OPENSSL_EXPORT void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key,
  83. const BIGNUM **out_priv_key);
  84. // DSA_get0_pqg sets |*out_p|, |*out_q|, and |*out_g|, if non-NULL, to |dsa|'s
  85. // p, q, and g parameters, respectively.
  86. OPENSSL_EXPORT void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p,
  87. const BIGNUM **out_q, const BIGNUM **out_g);
  88. // DSA_set0_key sets |dsa|'s public and private key to |pub_key| and |priv_key|,
  89. // respectively, if non-NULL. On success, it takes ownership of each argument
  90. // and returns one. Otherwise, it returns zero.
  91. //
  92. // |priv_key| may be NULL, but |pub_key| must either be non-NULL or already
  93. // configured on |dsa|.
  94. OPENSSL_EXPORT int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key);
  95. // DSA_set0_pqg sets |dsa|'s parameters to |p|, |q|, and |g|, if non-NULL, and
  96. // takes ownership of them. On success, it takes ownership of each argument and
  97. // returns one. Otherwise, it returns zero.
  98. //
  99. // Each argument must either be non-NULL or already configured on |dsa|.
  100. OPENSSL_EXPORT int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  101. // Parameter generation.
  102. // DSA_generate_parameters_ex generates a set of DSA parameters by following
  103. // the procedure given in FIPS 186-4, appendix A.
  104. // (http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)
  105. //
  106. // The larger prime will have a length of |bits| (e.g. 2048). The |seed| value
  107. // allows others to generate and verify the same parameters and should be
  108. // random input which is kept for reference. If |out_counter| or |out_h| are
  109. // not NULL then the counter and h value used in the generation are written to
  110. // them.
  111. //
  112. // The |cb| argument is passed to |BN_generate_prime_ex| and is thus called
  113. // during the generation process in order to indicate progress. See the
  114. // comments for that function for details. In addition to the calls made by
  115. // |BN_generate_prime_ex|, |DSA_generate_parameters_ex| will call it with
  116. // |event| equal to 2 and 3 at different stages of the process.
  117. //
  118. // It returns one on success and zero otherwise.
  119. OPENSSL_EXPORT int DSA_generate_parameters_ex(DSA *dsa, unsigned bits,
  120. const uint8_t *seed,
  121. size_t seed_len, int *out_counter,
  122. unsigned long *out_h,
  123. BN_GENCB *cb);
  124. // DSAparams_dup returns a freshly allocated |DSA| that contains a copy of the
  125. // parameters from |dsa|. It returns NULL on error.
  126. OPENSSL_EXPORT DSA *DSAparams_dup(const DSA *dsa);
  127. // Key generation.
  128. // DSA_generate_key generates a public/private key pair in |dsa|, which must
  129. // already have parameters setup. It returns one on success and zero on
  130. // error.
  131. OPENSSL_EXPORT int DSA_generate_key(DSA *dsa);
  132. // Signatures.
  133. // DSA_SIG_st (aka |DSA_SIG|) contains a DSA signature as a pair of integers.
  134. struct DSA_SIG_st {
  135. BIGNUM *r, *s;
  136. };
  137. // DSA_SIG_new returns a freshly allocated, DIG_SIG structure or NULL on error.
  138. // Both |r| and |s| in the signature will be NULL.
  139. OPENSSL_EXPORT DSA_SIG *DSA_SIG_new(void);
  140. // DSA_SIG_free frees the contents of |sig| and then frees |sig| itself.
  141. OPENSSL_EXPORT void DSA_SIG_free(DSA_SIG *sig);
  142. // DSA_do_sign returns a signature of the hash in |digest| by the key in |dsa|
  143. // and returns an allocated, DSA_SIG structure, or NULL on error.
  144. OPENSSL_EXPORT DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len,
  145. const DSA *dsa);
  146. // DSA_do_verify verifies that |sig| is a valid signature, by the public key in
  147. // |dsa|, of the hash in |digest|. It returns one if so, zero if invalid and -1
  148. // on error.
  149. //
  150. // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1
  151. // for valid. However, this is dangerously different to the usual OpenSSL
  152. // convention and could be a disaster if a user did |if (DSA_do_verify(...))|.
  153. // Because of this, |DSA_check_signature| is a safer version of this.
  154. //
  155. // TODO(fork): deprecate.
  156. OPENSSL_EXPORT int DSA_do_verify(const uint8_t *digest, size_t digest_len,
  157. DSA_SIG *sig, const DSA *dsa);
  158. // DSA_do_check_signature sets |*out_valid| to zero. Then it verifies that |sig|
  159. // is a valid signature, by the public key in |dsa| of the hash in |digest|
  160. // and, if so, it sets |*out_valid| to one.
  161. //
  162. // It returns one if it was able to verify the signature as valid or invalid,
  163. // and zero on error.
  164. OPENSSL_EXPORT int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  165. size_t digest_len, DSA_SIG *sig,
  166. const DSA *dsa);
  167. // ASN.1 signatures.
  168. //
  169. // These functions also perform DSA signature operations, but deal with ASN.1
  170. // encoded signatures as opposed to raw |BIGNUM|s. If you don't know what
  171. // encoding a DSA signature is in, it's probably ASN.1.
  172. // DSA_sign signs |digest| with the key in |dsa| and writes the resulting
  173. // signature, in ASN.1 form, to |out_sig| and the length of the signature to
  174. // |*out_siglen|. There must be, at least, |DSA_size(dsa)| bytes of space in
  175. // |out_sig|. It returns one on success and zero otherwise.
  176. //
  177. // (The |type| argument is ignored.)
  178. OPENSSL_EXPORT int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  179. uint8_t *out_sig, unsigned int *out_siglen,
  180. const DSA *dsa);
  181. // DSA_verify verifies that |sig| is a valid, ASN.1 signature, by the public
  182. // key in |dsa|, of the hash in |digest|. It returns one if so, zero if invalid
  183. // and -1 on error.
  184. //
  185. // (The |type| argument is ignored.)
  186. //
  187. // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1
  188. // for valid. However, this is dangerously different to the usual OpenSSL
  189. // convention and could be a disaster if a user did |if (DSA_do_verify(...))|.
  190. // Because of this, |DSA_check_signature| is a safer version of this.
  191. //
  192. // TODO(fork): deprecate.
  193. OPENSSL_EXPORT int DSA_verify(int type, const uint8_t *digest,
  194. size_t digest_len, const uint8_t *sig,
  195. size_t sig_len, const DSA *dsa);
  196. // DSA_check_signature sets |*out_valid| to zero. Then it verifies that |sig|
  197. // is a valid, ASN.1 signature, by the public key in |dsa|, of the hash in
  198. // |digest|. If so, it sets |*out_valid| to one.
  199. //
  200. // It returns one if it was able to verify the signature as valid or invalid,
  201. // and zero on error.
  202. OPENSSL_EXPORT int DSA_check_signature(int *out_valid, const uint8_t *digest,
  203. size_t digest_len, const uint8_t *sig,
  204. size_t sig_len, const DSA *dsa);
  205. // DSA_size returns the size, in bytes, of an ASN.1 encoded, DSA signature
  206. // generated by |dsa|. Parameters must already have been setup in |dsa|.
  207. OPENSSL_EXPORT int DSA_size(const DSA *dsa);
  208. // ASN.1 encoding.
  209. // DSA_SIG_parse parses a DER-encoded DSA-Sig-Value structure from |cbs| and
  210. // advances |cbs|. It returns a newly-allocated |DSA_SIG| or NULL on error.
  211. OPENSSL_EXPORT DSA_SIG *DSA_SIG_parse(CBS *cbs);
  212. // DSA_SIG_marshal marshals |sig| as a DER-encoded DSA-Sig-Value and appends the
  213. // result to |cbb|. It returns one on success and zero on error.
  214. OPENSSL_EXPORT int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig);
  215. // DSA_parse_public_key parses a DER-encoded DSA public key from |cbs| and
  216. // advances |cbs|. It returns a newly-allocated |DSA| or NULL on error.
  217. OPENSSL_EXPORT DSA *DSA_parse_public_key(CBS *cbs);
  218. // DSA_marshal_public_key marshals |dsa| as a DER-encoded DSA public key and
  219. // appends the result to |cbb|. It returns one on success and zero on
  220. // failure.
  221. OPENSSL_EXPORT int DSA_marshal_public_key(CBB *cbb, const DSA *dsa);
  222. // DSA_parse_private_key parses a DER-encoded DSA private key from |cbs| and
  223. // advances |cbs|. It returns a newly-allocated |DSA| or NULL on error.
  224. OPENSSL_EXPORT DSA *DSA_parse_private_key(CBS *cbs);
  225. // DSA_marshal_private_key marshals |dsa| as a DER-encoded DSA private key and
  226. // appends the result to |cbb|. It returns one on success and zero on
  227. // failure.
  228. OPENSSL_EXPORT int DSA_marshal_private_key(CBB *cbb, const DSA *dsa);
  229. // DSA_parse_parameters parses a DER-encoded Dss-Parms structure (RFC 3279)
  230. // from |cbs| and advances |cbs|. It returns a newly-allocated |DSA| or NULL on
  231. // error.
  232. OPENSSL_EXPORT DSA *DSA_parse_parameters(CBS *cbs);
  233. // DSA_marshal_parameters marshals |dsa| as a DER-encoded Dss-Parms structure
  234. // (RFC 3447) and appends the result to |cbb|. It returns one on success and
  235. // zero on failure.
  236. OPENSSL_EXPORT int DSA_marshal_parameters(CBB *cbb, const DSA *dsa);
  237. // Conversion.
  238. // DSA_dup_DH returns a |DH| constructed from the parameters of |dsa|. This is
  239. // sometimes needed when Diffie-Hellman parameters are stored in the form of
  240. // DSA parameters. It returns an allocated |DH| on success or NULL on error.
  241. OPENSSL_EXPORT DH *DSA_dup_DH(const DSA *dsa);
  242. // ex_data functions.
  243. //
  244. // See |ex_data.h| for details.
  245. OPENSSL_EXPORT int DSA_get_ex_new_index(long argl, void *argp,
  246. CRYPTO_EX_unused *unused,
  247. CRYPTO_EX_dup *dup_unused,
  248. CRYPTO_EX_free *free_func);
  249. OPENSSL_EXPORT int DSA_set_ex_data(DSA *dsa, int idx, void *arg);
  250. OPENSSL_EXPORT void *DSA_get_ex_data(const DSA *dsa, int idx);
  251. // Deprecated functions.
  252. // d2i_DSA_SIG parses an ASN.1, DER-encoded, DSA signature from |len| bytes at
  253. // |*inp|. If |out_sig| is not NULL then, on exit, a pointer to the result is
  254. // in |*out_sig|. Note that, even if |*out_sig| is already non-NULL on entry, it
  255. // will not be written to. Rather, a fresh |DSA_SIG| is allocated and the
  256. // previous one is freed. On successful exit, |*inp| is advanced past the DER
  257. // structure. It returns the result or NULL on error.
  258. //
  259. // Use |DSA_SIG_parse| instead.
  260. OPENSSL_EXPORT DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp,
  261. long len);
  262. // i2d_DSA_SIG marshals |in| to an ASN.1, DER structure. If |outp| is not NULL
  263. // then the result is written to |*outp| and |*outp| is advanced just past the
  264. // output. It returns the number of bytes in the result, whether written or not,
  265. // or a negative value on error.
  266. //
  267. // Use |DSA_SIG_marshal| instead.
  268. OPENSSL_EXPORT int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp);
  269. // d2i_DSAPublicKey parses an ASN.1, DER-encoded, DSA public key from |len|
  270. // bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the result
  271. // is in |*out|. Note that, even if |*ou| is already non-NULL on entry, it will
  272. // not be written to. Rather, a fresh |DSA| is allocated and the previous one is
  273. // freed. On successful exit, |*inp| is advanced past the DER structure. It
  274. // returns the result or NULL on error.
  275. //
  276. // Use |DSA_parse_public_key| instead.
  277. OPENSSL_EXPORT DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len);
  278. // i2d_DSAPublicKey marshals a public key from |in| to an ASN.1, DER structure.
  279. // If |outp| is not NULL then the result is written to |*outp| and |*outp| is
  280. // advanced just past the output. It returns the number of bytes in the result,
  281. // whether written or not, or a negative value on error.
  282. //
  283. // Use |DSA_marshal_public_key| instead.
  284. OPENSSL_EXPORT int i2d_DSAPublicKey(const DSA *in, uint8_t **outp);
  285. // d2i_DSAPrivateKey parses an ASN.1, DER-encoded, DSA private key from |len|
  286. // bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the result
  287. // is in |*out|. Note that, even if |*out| is already non-NULL on entry, it will
  288. // not be written to. Rather, a fresh |DSA| is allocated and the previous one is
  289. // freed. On successful exit, |*inp| is advanced past the DER structure. It
  290. // returns the result or NULL on error.
  291. //
  292. // Use |DSA_parse_private_key| instead.
  293. OPENSSL_EXPORT DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len);
  294. // i2d_DSAPrivateKey marshals a private key from |in| to an ASN.1, DER
  295. // structure. If |outp| is not NULL then the result is written to |*outp| and
  296. // |*outp| is advanced just past the output. It returns the number of bytes in
  297. // the result, whether written or not, or a negative value on error.
  298. //
  299. // Use |DSA_marshal_private_key| instead.
  300. OPENSSL_EXPORT int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp);
  301. // d2i_DSAparams parses ASN.1, DER-encoded, DSA parameters from |len| bytes at
  302. // |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in
  303. // |*out|. Note that, even if |*out| is already non-NULL on entry, it will not
  304. // be written to. Rather, a fresh |DSA| is allocated and the previous one is
  305. // freed. On successful exit, |*inp| is advanced past the DER structure. It
  306. // returns the result or NULL on error.
  307. //
  308. // Use |DSA_parse_parameters| instead.
  309. OPENSSL_EXPORT DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len);
  310. // i2d_DSAparams marshals DSA parameters from |in| to an ASN.1, DER structure.
  311. // If |outp| is not NULL then the result is written to |*outp| and |*outp| is
  312. // advanced just past the output. It returns the number of bytes in the result,
  313. // whether written or not, or a negative value on error.
  314. //
  315. // Use |DSA_marshal_parameters| instead.
  316. OPENSSL_EXPORT int i2d_DSAparams(const DSA *in, uint8_t **outp);
  317. // DSA_generate_parameters is a deprecated version of
  318. // |DSA_generate_parameters_ex| that creates and returns a |DSA*|. Don't use
  319. // it.
  320. OPENSSL_EXPORT DSA *DSA_generate_parameters(int bits, unsigned char *seed,
  321. int seed_len, int *counter_ret,
  322. unsigned long *h_ret,
  323. void (*callback)(int, int, void *),
  324. void *cb_arg);
  325. struct dsa_st {
  326. long version;
  327. BIGNUM *p;
  328. BIGNUM *q; // == 20
  329. BIGNUM *g;
  330. BIGNUM *pub_key; // y public key
  331. BIGNUM *priv_key; // x private key
  332. int flags;
  333. // Normally used to cache montgomery values
  334. CRYPTO_MUTEX method_mont_lock;
  335. BN_MONT_CTX *method_mont_p;
  336. BN_MONT_CTX *method_mont_q;
  337. CRYPTO_refcount_t references;
  338. CRYPTO_EX_DATA ex_data;
  339. };
  340. #if defined(__cplusplus)
  341. } // extern C
  342. extern "C++" {
  343. namespace bssl {
  344. BORINGSSL_MAKE_DELETER(DSA, DSA_free)
  345. BORINGSSL_MAKE_DELETER(DSA_SIG, DSA_SIG_free)
  346. } // namespace bssl
  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. #endif // OPENSSL_HEADER_DSA_H