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.

dsa.h 15 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 verifing 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|. */
  77. OPENSSL_EXPORT int DSA_up_ref(DSA *dsa);
  78. /* Parameter generation. */
  79. /* DSA_generate_parameters_ex generates a set of DSA parameters by following
  80. * the procedure given in FIPS 186-4, appendix A.
  81. * (http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf)
  82. *
  83. * The larger prime will have a length of |bits| (e.g. 2048). The |seed| value
  84. * allows others to generate and verify the same parameters and should be
  85. * random input which is kept for reference. If |out_counter| or |out_h| are
  86. * not NULL then the counter and h value used in the generation are written to
  87. * them.
  88. *
  89. * The |cb| argument is passed to |BN_generate_prime_ex| and is thus called
  90. * during the generation process in order to indicate progress. See the
  91. * comments for that function for details. In addition to the calls made by
  92. * |BN_generate_prime_ex|, |DSA_generate_parameters_ex| will call it with
  93. * |event| equal to 2 and 3 at different stages of the process.
  94. *
  95. * It returns one on success and zero otherwise. */
  96. OPENSSL_EXPORT int DSA_generate_parameters_ex(DSA *dsa, unsigned bits,
  97. const uint8_t *seed,
  98. size_t seed_len, int *out_counter,
  99. unsigned long *out_h,
  100. BN_GENCB *cb);
  101. /* DSAparams_dup returns a freshly allocated |DSA| that contains a copy of the
  102. * parameters from |dsa|. It returns NULL on error. */
  103. OPENSSL_EXPORT DSA *DSAparams_dup(const DSA *dsa);
  104. /* Key generation. */
  105. /* DSA_generate_key generates a public/private key pair in |dsa|, which must
  106. * already have parameters setup. It returns one on success and zero on
  107. * error. */
  108. OPENSSL_EXPORT int DSA_generate_key(DSA *dsa);
  109. /* Signatures. */
  110. /* DSA_SIG contains a DSA signature as a pair of integers. */
  111. typedef struct DSA_SIG_st {
  112. BIGNUM *r, *s;
  113. } DSA_SIG;
  114. /* DSA_SIG_new returns a freshly allocated, DIG_SIG structure or NULL on error.
  115. * Both |r| and |s| in the signature will be NULL. */
  116. OPENSSL_EXPORT DSA_SIG *DSA_SIG_new(void);
  117. /* DSA_SIG_free frees the contents of |sig| and then frees |sig| itself. */
  118. OPENSSL_EXPORT void DSA_SIG_free(DSA_SIG *sig);
  119. /* DSA_do_sign returns a signature of the hash in |digest| by the key in |dsa|
  120. * and returns an allocated, DSA_SIG structure, or NULL on error. */
  121. OPENSSL_EXPORT DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len,
  122. DSA *dsa);
  123. /* DSA_do_verify verifies that |sig| is a valid signature, by the public key in
  124. * |dsa|, of the hash in |digest|. It returns one if so, zero if invalid and -1
  125. * on error.
  126. *
  127. * WARNING: do not use. This function returns -1 for error, 0 for invalid and 1
  128. * for valid. However, this is dangerously different to the usual OpenSSL
  129. * convention and could be a disaster if a user did |if (DSA_do_verify(...))|.
  130. * Because of this, |DSA_check_signature| is a safer version of this.
  131. *
  132. * TODO(fork): deprecate. */
  133. OPENSSL_EXPORT int DSA_do_verify(const uint8_t *digest, size_t digest_len,
  134. DSA_SIG *sig, const DSA *dsa);
  135. /* DSA_do_check_signature sets |*out_valid| to zero. Then it verifies that |sig|
  136. * is a valid signature, by the public key in |dsa| of the hash in |digest|
  137. * and, if so, it sets |*out_valid| to one.
  138. *
  139. * It returns one if it was able to verify the signature as valid or invalid,
  140. * and zero on error. */
  141. OPENSSL_EXPORT int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
  142. size_t digest_len, DSA_SIG *sig,
  143. const DSA *dsa);
  144. /* ASN.1 signatures.
  145. *
  146. * These functions also perform DSA signature operations, but deal with ASN.1
  147. * encoded signatures as opposed to raw |BIGNUM|s. If you don't know what
  148. * encoding a DSA signature is in, it's probably ASN.1. */
  149. /* DSA_sign signs |digest| with the key in |dsa| and writes the resulting
  150. * signature, in ASN.1 form, to |out_sig| and the length of the signature to
  151. * |*out_siglen|. There must be, at least, |DSA_size(dsa)| bytes of space in
  152. * |out_sig|. It returns one on success and zero otherwise.
  153. *
  154. * (The |type| argument is ignored.) */
  155. OPENSSL_EXPORT int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
  156. uint8_t *out_sig, unsigned int *out_siglen,
  157. DSA *dsa);
  158. /* DSA_verify verifies that |sig| is a valid, ASN.1 signature, by the public
  159. * key in |dsa|, of the hash in |digest|. It returns one if so, zero if invalid
  160. * and -1 on error.
  161. *
  162. * (The |type| argument is ignored.)
  163. *
  164. * WARNING: do not use. This function returns -1 for error, 0 for invalid and 1
  165. * for valid. However, this is dangerously different to the usual OpenSSL
  166. * convention and could be a disaster if a user did |if (DSA_do_verify(...))|.
  167. * Because of this, |DSA_check_signature| is a safer version of this.
  168. *
  169. * TODO(fork): deprecate. */
  170. OPENSSL_EXPORT int DSA_verify(int type, const uint8_t *digest,
  171. size_t digest_len, const uint8_t *sig,
  172. size_t sig_len, const DSA *dsa);
  173. /* DSA_check_signature sets |*out_valid| to zero. Then it verifies that |sig|
  174. * is a valid, ASN.1 signature, by the public key in |dsa|, of the hash in
  175. * |digest|. If so, it sets |*out_valid| to one.
  176. *
  177. * It returns one if it was able to verify the signature as valid or invalid,
  178. * and zero on error. */
  179. OPENSSL_EXPORT int DSA_check_signature(int *out_valid, const uint8_t *digest,
  180. size_t digest_len, const uint8_t *sig,
  181. size_t sig_len, const DSA *dsa);
  182. /* DSA_size returns the size, in bytes, of an ASN.1 encoded, DSA signature
  183. * generated by |dsa|. Parameters must already have been setup in |dsa|. */
  184. OPENSSL_EXPORT int DSA_size(const DSA *dsa);
  185. /* ASN.1 encoding. */
  186. /* d2i_DSA_SIG parses an ASN.1, DER-encoded, DSA signature from |len| bytes at
  187. * |*inp|. If |out_sig| is not NULL then, on exit, a pointer to the result is
  188. * in |*out_sig|. If |*out_sig| is already non-NULL on entry then the result is
  189. * written directly into |*out_sig|, otherwise a fresh |DSA_SIG| is allocated.
  190. * On successful exit, |*inp| is advanced past the DER structure. It returns
  191. * the result or NULL on error. */
  192. OPENSSL_EXPORT DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp,
  193. long len);
  194. /* i2d_DSA_SIG marshals |in| to an ASN.1, DER structure. If |outp| is not NULL
  195. * then the result is written to |*outp| and |*outp| is advanced just past the
  196. * output. It returns the number of bytes in the result, whether written or not,
  197. * or a negative value on error. */
  198. OPENSSL_EXPORT int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp);
  199. /* d2i_DSAPublicKey parses an ASN.1, DER-encoded, DSA public key from |len|
  200. * bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the result
  201. * is in |*out|. If |*out| is already non-NULL on entry then the result is
  202. * written directly into |*out|, otherwise a fresh |DSA| is allocated. On
  203. * successful exit, |*inp| is advanced past the DER structure. It returns the
  204. * result or NULL on error. */
  205. OPENSSL_EXPORT DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len);
  206. /* i2d_DSAPublicKey marshals a public key from |in| to an ASN.1, DER structure.
  207. * If |outp| is not NULL then the result is written to |*outp| and |*outp| is
  208. * advanced just past the output. It returns the number of bytes in the result,
  209. * whether written or not, or a negative value on error. */
  210. OPENSSL_EXPORT int i2d_DSAPublicKey(const DSA *in, unsigned char **outp);
  211. /* d2i_DSAPrivateKey parses an ASN.1, DER-encoded, DSA private key from |len|
  212. * bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the result
  213. * is in |*out|. If |*out| is already non-NULL on entry then the result is
  214. * written directly into |*out|, otherwise a fresh |DSA| is allocated. On
  215. * successful exit, |*inp| is advanced past the DER structure. It returns the
  216. * result or NULL on error. */
  217. OPENSSL_EXPORT DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len);
  218. /* i2d_DSAPrivateKey marshals a private key from |in| to an ASN.1, DER structure.
  219. * If |outp| is not NULL then the result is written to |*outp| and |*outp| is
  220. * advanced just past the output. It returns the number of bytes in the result,
  221. * whether written or not, or a negative value on error. */
  222. OPENSSL_EXPORT int i2d_DSAPrivateKey(const DSA *in, unsigned char **outp);
  223. /* d2i_DSAparams parses ASN.1, DER-encoded, DSA parameters from |len| bytes at
  224. * |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in
  225. * |*out|. If |*out| is already non-NULL on entry then the result is written
  226. * directly into |*out|, otherwise a fresh |DSA| is allocated. On successful
  227. * exit, |*inp| is advanced past the DER structure. It returns the result or
  228. * NULL on error. */
  229. OPENSSL_EXPORT DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len);
  230. /* i2d_DSAparams marshals DSA parameters from |in| to an ASN.1, DER structure.
  231. * If |outp| is not NULL then the result is written to |*outp| and |*outp| is
  232. * advanced just past the output. It returns the number of bytes in the result,
  233. * whether written or not, or a negative value on error. */
  234. OPENSSL_EXPORT int i2d_DSAparams(const DSA *in, unsigned char **outp);
  235. /* Precomputation. */
  236. /* DSA_sign_setup precomputes the message independent part of the DSA signature
  237. * and writes them to |*out_kinv| and |*out_r|. Returns one on success, zero on
  238. * error.
  239. *
  240. * TODO(fork): decide what to do with this. Since making DSA* opaque there's no
  241. * way for the user to install them. Also, it forces the DSA* not to be const
  242. * when passing to the signing function. */
  243. OPENSSL_EXPORT int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx,
  244. BIGNUM **out_kinv, BIGNUM **out_r);
  245. /* Conversion. */
  246. /* DSA_dup_DH returns a |DH| constructed from the parameters of |dsa|. This is
  247. * sometimes needed when Diffie-Hellman parameters are stored in the form of
  248. * DSA parameters. It returns an allocated |DH| on success or NULL on error. */
  249. OPENSSL_EXPORT DH *DSA_dup_DH(const DSA *dsa);
  250. /* ex_data functions.
  251. *
  252. * See |ex_data.h| for details. */
  253. OPENSSL_EXPORT int DSA_get_ex_new_index(long argl, void *argp,
  254. CRYPTO_EX_new *new_func,
  255. CRYPTO_EX_dup *dup_func,
  256. CRYPTO_EX_free *free_func);
  257. OPENSSL_EXPORT int DSA_set_ex_data(DSA *d, int idx, void *arg);
  258. OPENSSL_EXPORT void *DSA_get_ex_data(const DSA *d, int idx);
  259. struct dsa_st {
  260. long version;
  261. int write_params;
  262. BIGNUM *p;
  263. BIGNUM *q; /* == 20 */
  264. BIGNUM *g;
  265. BIGNUM *pub_key; /* y public key */
  266. BIGNUM *priv_key; /* x private key */
  267. BIGNUM *kinv; /* Signing pre-calc */
  268. BIGNUM *r; /* Signing pre-calc */
  269. int flags;
  270. /* Normally used to cache montgomery values */
  271. CRYPTO_MUTEX method_mont_p_lock;
  272. BN_MONT_CTX *method_mont_p;
  273. CRYPTO_refcount_t references;
  274. CRYPTO_EX_DATA ex_data;
  275. };
  276. #if defined(__cplusplus)
  277. } /* extern C */
  278. #endif
  279. #define DSA_R_BAD_Q_VALUE 100
  280. #define DSA_R_MISSING_PARAMETERS 101
  281. #define DSA_R_MODULUS_TOO_LARGE 102
  282. #define DSA_R_NEED_NEW_SETUP_VALUES 103
  283. #endif /* OPENSSL_HEADER_DSA_H */