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.
 
 
 
 
 
 

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