Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
| 60 | #ifndef OPENSSL_HEADER_DSA_H |
| 61 | #define OPENSSL_HEADER_DSA_H |
| 62 | |
| 63 | #include <openssl/base.h> |
| 64 | |
| 65 | #include <openssl/engine.h> |
| 66 | #include <openssl/ex_data.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 67 | #include <openssl/thread.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 68 | |
| 69 | #if defined(__cplusplus) |
| 70 | extern "C" { |
| 71 | #endif |
| 72 | |
| 73 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 74 | // DSA contains functions for signing and verifying with the Digital Signature |
| 75 | // Algorithm. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 76 | // |
| 77 | // This module is deprecated and retained for legacy reasons only. It is not |
| 78 | // considered a priority for performance or hardening work. Do not use it in |
| 79 | // new code. Use Ed25519, ECDSA with P-256, or RSA instead. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 80 | |
| 81 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 82 | // Allocation and destruction. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 83 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 84 | // DSA_new returns a new, empty DSA object or NULL on error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | OPENSSL_EXPORT DSA *DSA_new(void); |
| 86 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 87 | // DSA_free decrements the reference count of |dsa| and frees it if the |
| 88 | // reference count drops to zero. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 89 | OPENSSL_EXPORT void DSA_free(DSA *dsa); |
| 90 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 91 | // DSA_up_ref increments the reference count of |dsa| and returns one. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 92 | OPENSSL_EXPORT int DSA_up_ref(DSA *dsa); |
| 93 | |
| 94 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 95 | // Properties. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 96 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 97 | // DSA_get0_key sets |*out_pub_key| and |*out_priv_key|, if non-NULL, to |dsa|'s |
| 98 | // public and private key, respectively. If |dsa| is a public key, the private |
| 99 | // key will be set to NULL. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 100 | OPENSSL_EXPORT void DSA_get0_key(const DSA *dsa, const BIGNUM **out_pub_key, |
| 101 | const BIGNUM **out_priv_key); |
| 102 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 103 | // DSA_get0_pqg sets |*out_p|, |*out_q|, and |*out_g|, if non-NULL, to |dsa|'s |
| 104 | // p, q, and g parameters, respectively. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 105 | OPENSSL_EXPORT void DSA_get0_pqg(const DSA *dsa, const BIGNUM **out_p, |
| 106 | const BIGNUM **out_q, const BIGNUM **out_g); |
| 107 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 108 | // DSA_set0_key sets |dsa|'s public and private key to |pub_key| and |priv_key|, |
| 109 | // respectively, if non-NULL. On success, it takes ownership of each argument |
| 110 | // and returns one. Otherwise, it returns zero. |
| 111 | // |
| 112 | // |priv_key| may be NULL, but |pub_key| must either be non-NULL or already |
| 113 | // configured on |dsa|. |
| 114 | OPENSSL_EXPORT int DSA_set0_key(DSA *dsa, BIGNUM *pub_key, BIGNUM *priv_key); |
| 115 | |
| 116 | // DSA_set0_pqg sets |dsa|'s parameters to |p|, |q|, and |g|, if non-NULL, and |
| 117 | // takes ownership of them. On success, it takes ownership of each argument and |
| 118 | // returns one. Otherwise, it returns zero. |
| 119 | // |
| 120 | // Each argument must either be non-NULL or already configured on |dsa|. |
| 121 | OPENSSL_EXPORT int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g); |
| 122 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 123 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 124 | // Parameter generation. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 125 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 126 | // DSA_generate_parameters_ex generates a set of DSA parameters by following |
| 127 | // the procedure given in FIPS 186-4, appendix A. |
| 128 | // (http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf) |
| 129 | // |
| 130 | // The larger prime will have a length of |bits| (e.g. 2048). The |seed| value |
| 131 | // allows others to generate and verify the same parameters and should be |
| 132 | // random input which is kept for reference. If |out_counter| or |out_h| are |
| 133 | // not NULL then the counter and h value used in the generation are written to |
| 134 | // them. |
| 135 | // |
| 136 | // The |cb| argument is passed to |BN_generate_prime_ex| and is thus called |
| 137 | // during the generation process in order to indicate progress. See the |
| 138 | // comments for that function for details. In addition to the calls made by |
| 139 | // |BN_generate_prime_ex|, |DSA_generate_parameters_ex| will call it with |
| 140 | // |event| equal to 2 and 3 at different stages of the process. |
| 141 | // |
| 142 | // It returns one on success and zero otherwise. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 143 | OPENSSL_EXPORT int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, |
| 144 | const uint8_t *seed, |
| 145 | size_t seed_len, int *out_counter, |
| 146 | unsigned long *out_h, |
| 147 | BN_GENCB *cb); |
| 148 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 149 | // DSAparams_dup returns a freshly allocated |DSA| that contains a copy of the |
| 150 | // parameters from |dsa|. It returns NULL on error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | OPENSSL_EXPORT DSA *DSAparams_dup(const DSA *dsa); |
| 152 | |
| 153 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 154 | // Key generation. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 155 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 156 | // DSA_generate_key generates a public/private key pair in |dsa|, which must |
| 157 | // already have parameters setup. It returns one on success and zero on |
| 158 | // error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | OPENSSL_EXPORT int DSA_generate_key(DSA *dsa); |
| 160 | |
| 161 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 162 | // Signatures. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 163 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 164 | // DSA_SIG_st (aka |DSA_SIG|) contains a DSA signature as a pair of integers. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 165 | struct DSA_SIG_st { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 166 | BIGNUM *r, *s; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 167 | }; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 168 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 169 | // DSA_SIG_new returns a freshly allocated, DIG_SIG structure or NULL on error. |
| 170 | // Both |r| and |s| in the signature will be NULL. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | OPENSSL_EXPORT DSA_SIG *DSA_SIG_new(void); |
| 172 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 173 | // DSA_SIG_free frees the contents of |sig| and then frees |sig| itself. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 174 | OPENSSL_EXPORT void DSA_SIG_free(DSA_SIG *sig); |
| 175 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 176 | // DSA_do_sign returns a signature of the hash in |digest| by the key in |dsa| |
| 177 | // and returns an allocated, DSA_SIG structure, or NULL on error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 178 | OPENSSL_EXPORT DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 179 | const DSA *dsa); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 180 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 181 | // DSA_do_verify verifies that |sig| is a valid signature, by the public key in |
| 182 | // |dsa|, of the hash in |digest|. It returns one if so, zero if invalid and -1 |
| 183 | // on error. |
| 184 | // |
| 185 | // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1 |
| 186 | // for valid. However, this is dangerously different to the usual OpenSSL |
| 187 | // convention and could be a disaster if a user did |if (DSA_do_verify(...))|. |
| 188 | // Because of this, |DSA_check_signature| is a safer version of this. |
| 189 | // |
| 190 | // TODO(fork): deprecate. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 191 | OPENSSL_EXPORT int DSA_do_verify(const uint8_t *digest, size_t digest_len, |
| 192 | DSA_SIG *sig, const DSA *dsa); |
| 193 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 194 | // DSA_do_check_signature sets |*out_valid| to zero. Then it verifies that |sig| |
| 195 | // is a valid signature, by the public key in |dsa| of the hash in |digest| |
| 196 | // and, if so, it sets |*out_valid| to one. |
| 197 | // |
| 198 | // It returns one if it was able to verify the signature as valid or invalid, |
| 199 | // and zero on error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 200 | OPENSSL_EXPORT int DSA_do_check_signature(int *out_valid, const uint8_t *digest, |
| 201 | size_t digest_len, DSA_SIG *sig, |
| 202 | const DSA *dsa); |
| 203 | |
| 204 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 205 | // ASN.1 signatures. |
| 206 | // |
| 207 | // These functions also perform DSA signature operations, but deal with ASN.1 |
| 208 | // encoded signatures as opposed to raw |BIGNUM|s. If you don't know what |
| 209 | // encoding a DSA signature is in, it's probably ASN.1. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 211 | // DSA_sign signs |digest| with the key in |dsa| and writes the resulting |
| 212 | // signature, in ASN.1 form, to |out_sig| and the length of the signature to |
| 213 | // |*out_siglen|. There must be, at least, |DSA_size(dsa)| bytes of space in |
| 214 | // |out_sig|. It returns one on success and zero otherwise. |
| 215 | // |
| 216 | // (The |type| argument is ignored.) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 217 | OPENSSL_EXPORT int DSA_sign(int type, const uint8_t *digest, size_t digest_len, |
| 218 | uint8_t *out_sig, unsigned int *out_siglen, |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 219 | const DSA *dsa); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 221 | // DSA_verify verifies that |sig| is a valid, ASN.1 signature, by the public |
| 222 | // key in |dsa|, of the hash in |digest|. It returns one if so, zero if invalid |
| 223 | // and -1 on error. |
| 224 | // |
| 225 | // (The |type| argument is ignored.) |
| 226 | // |
| 227 | // WARNING: do not use. This function returns -1 for error, 0 for invalid and 1 |
| 228 | // for valid. However, this is dangerously different to the usual OpenSSL |
| 229 | // convention and could be a disaster if a user did |if (DSA_do_verify(...))|. |
| 230 | // Because of this, |DSA_check_signature| is a safer version of this. |
| 231 | // |
| 232 | // TODO(fork): deprecate. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 233 | OPENSSL_EXPORT int DSA_verify(int type, const uint8_t *digest, |
| 234 | size_t digest_len, const uint8_t *sig, |
| 235 | size_t sig_len, const DSA *dsa); |
| 236 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 237 | // DSA_check_signature sets |*out_valid| to zero. Then it verifies that |sig| |
| 238 | // is a valid, ASN.1 signature, by the public key in |dsa|, of the hash in |
| 239 | // |digest|. If so, it sets |*out_valid| to one. |
| 240 | // |
| 241 | // It returns one if it was able to verify the signature as valid or invalid, |
| 242 | // and zero on error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | OPENSSL_EXPORT int DSA_check_signature(int *out_valid, const uint8_t *digest, |
| 244 | size_t digest_len, const uint8_t *sig, |
| 245 | size_t sig_len, const DSA *dsa); |
| 246 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 247 | // DSA_size returns the size, in bytes, of an ASN.1 encoded, DSA signature |
| 248 | // generated by |dsa|. Parameters must already have been setup in |dsa|. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 249 | OPENSSL_EXPORT int DSA_size(const DSA *dsa); |
| 250 | |
| 251 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 252 | // ASN.1 encoding. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 253 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 254 | // DSA_SIG_parse parses a DER-encoded DSA-Sig-Value structure from |cbs| and |
| 255 | // advances |cbs|. It returns a newly-allocated |DSA_SIG| or NULL on error. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 256 | OPENSSL_EXPORT DSA_SIG *DSA_SIG_parse(CBS *cbs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 258 | // DSA_SIG_marshal marshals |sig| as a DER-encoded DSA-Sig-Value and appends the |
| 259 | // result to |cbb|. It returns one on success and zero on error. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 260 | OPENSSL_EXPORT int DSA_SIG_marshal(CBB *cbb, const DSA_SIG *sig); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 261 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 262 | // DSA_parse_public_key parses a DER-encoded DSA public key from |cbs| and |
| 263 | // advances |cbs|. It returns a newly-allocated |DSA| or NULL on error. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 264 | OPENSSL_EXPORT DSA *DSA_parse_public_key(CBS *cbs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 266 | // DSA_marshal_public_key marshals |dsa| as a DER-encoded DSA public key and |
| 267 | // appends the result to |cbb|. It returns one on success and zero on |
| 268 | // failure. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 269 | OPENSSL_EXPORT int DSA_marshal_public_key(CBB *cbb, const DSA *dsa); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 270 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 271 | // DSA_parse_private_key parses a DER-encoded DSA private key from |cbs| and |
| 272 | // advances |cbs|. It returns a newly-allocated |DSA| or NULL on error. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 273 | OPENSSL_EXPORT DSA *DSA_parse_private_key(CBS *cbs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 274 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 275 | // DSA_marshal_private_key marshals |dsa| as a DER-encoded DSA private key and |
| 276 | // appends the result to |cbb|. It returns one on success and zero on |
| 277 | // failure. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 278 | OPENSSL_EXPORT int DSA_marshal_private_key(CBB *cbb, const DSA *dsa); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 279 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 280 | // DSA_parse_parameters parses a DER-encoded Dss-Parms structure (RFC 3279) |
| 281 | // from |cbs| and advances |cbs|. It returns a newly-allocated |DSA| or NULL on |
| 282 | // error. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 283 | OPENSSL_EXPORT DSA *DSA_parse_parameters(CBS *cbs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 284 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 285 | // DSA_marshal_parameters marshals |dsa| as a DER-encoded Dss-Parms structure |
| 286 | // (RFC 3447) and appends the result to |cbb|. It returns one on success and |
| 287 | // zero on failure. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 288 | OPENSSL_EXPORT int DSA_marshal_parameters(CBB *cbb, const DSA *dsa); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 289 | |
| 290 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 291 | // Conversion. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 292 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 293 | // DSA_dup_DH returns a |DH| constructed from the parameters of |dsa|. This is |
| 294 | // sometimes needed when Diffie-Hellman parameters are stored in the form of |
| 295 | // DSA parameters. It returns an allocated |DH| on success or NULL on error. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | OPENSSL_EXPORT DH *DSA_dup_DH(const DSA *dsa); |
| 297 | |
| 298 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 299 | // ex_data functions. |
| 300 | // |
| 301 | // See |ex_data.h| for details. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 302 | |
| 303 | OPENSSL_EXPORT int DSA_get_ex_new_index(long argl, void *argp, |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 304 | CRYPTO_EX_unused *unused, |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 305 | CRYPTO_EX_dup *dup_unused, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 306 | CRYPTO_EX_free *free_func); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 307 | OPENSSL_EXPORT int DSA_set_ex_data(DSA *dsa, int idx, void *arg); |
| 308 | OPENSSL_EXPORT void *DSA_get_ex_data(const DSA *dsa, int idx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 309 | |
| 310 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 311 | // Deprecated functions. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 312 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 313 | // d2i_DSA_SIG parses an ASN.1, DER-encoded, DSA signature from |len| bytes at |
| 314 | // |*inp|. If |out_sig| is not NULL then, on exit, a pointer to the result is |
| 315 | // in |*out_sig|. Note that, even if |*out_sig| is already non-NULL on entry, it |
| 316 | // will not be written to. Rather, a fresh |DSA_SIG| is allocated and the |
| 317 | // previous one is freed. On successful exit, |*inp| is advanced past the DER |
| 318 | // structure. It returns the result or NULL on error. |
| 319 | // |
| 320 | // Use |DSA_SIG_parse| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 321 | OPENSSL_EXPORT DSA_SIG *d2i_DSA_SIG(DSA_SIG **out_sig, const uint8_t **inp, |
| 322 | long len); |
| 323 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 324 | // i2d_DSA_SIG marshals |in| to an ASN.1, DER structure. If |outp| is not NULL |
| 325 | // then the result is written to |*outp| and |*outp| is advanced just past the |
| 326 | // output. It returns the number of bytes in the result, whether written or not, |
| 327 | // or a negative value on error. |
| 328 | // |
| 329 | // Use |DSA_SIG_marshal| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 330 | OPENSSL_EXPORT int i2d_DSA_SIG(const DSA_SIG *in, uint8_t **outp); |
| 331 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 332 | // d2i_DSAPublicKey parses an ASN.1, DER-encoded, DSA public key from |len| |
| 333 | // bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the result |
| 334 | // is in |*out|. Note that, even if |*ou| is already non-NULL on entry, it will |
| 335 | // not be written to. Rather, a fresh |DSA| is allocated and the previous one is |
| 336 | // freed. On successful exit, |*inp| is advanced past the DER structure. It |
| 337 | // returns the result or NULL on error. |
| 338 | // |
| 339 | // Use |DSA_parse_public_key| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 340 | OPENSSL_EXPORT DSA *d2i_DSAPublicKey(DSA **out, const uint8_t **inp, long len); |
| 341 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 342 | // i2d_DSAPublicKey marshals a public key from |in| to an ASN.1, DER structure. |
| 343 | // If |outp| is not NULL then the result is written to |*outp| and |*outp| is |
| 344 | // advanced just past the output. It returns the number of bytes in the result, |
| 345 | // whether written or not, or a negative value on error. |
| 346 | // |
| 347 | // Use |DSA_marshal_public_key| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 348 | OPENSSL_EXPORT int i2d_DSAPublicKey(const DSA *in, uint8_t **outp); |
| 349 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 350 | // d2i_DSAPrivateKey parses an ASN.1, DER-encoded, DSA private key from |len| |
| 351 | // bytes at |*inp|. If |out| is not NULL then, on exit, a pointer to the result |
| 352 | // is in |*out|. Note that, even if |*out| is already non-NULL on entry, it will |
| 353 | // not be written to. Rather, a fresh |DSA| is allocated and the previous one is |
| 354 | // freed. On successful exit, |*inp| is advanced past the DER structure. It |
| 355 | // returns the result or NULL on error. |
| 356 | // |
| 357 | // Use |DSA_parse_private_key| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 358 | OPENSSL_EXPORT DSA *d2i_DSAPrivateKey(DSA **out, const uint8_t **inp, long len); |
| 359 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 360 | // i2d_DSAPrivateKey marshals a private key from |in| to an ASN.1, DER |
| 361 | // structure. If |outp| is not NULL then the result is written to |*outp| and |
| 362 | // |*outp| is advanced just past the output. It returns the number of bytes in |
| 363 | // the result, whether written or not, or a negative value on error. |
| 364 | // |
| 365 | // Use |DSA_marshal_private_key| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 366 | OPENSSL_EXPORT int i2d_DSAPrivateKey(const DSA *in, uint8_t **outp); |
| 367 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 368 | // d2i_DSAparams parses ASN.1, DER-encoded, DSA parameters from |len| bytes at |
| 369 | // |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in |
| 370 | // |*out|. Note that, even if |*out| is already non-NULL on entry, it will not |
| 371 | // be written to. Rather, a fresh |DSA| is allocated and the previous one is |
| 372 | // freed. On successful exit, |*inp| is advanced past the DER structure. It |
| 373 | // returns the result or NULL on error. |
| 374 | // |
| 375 | // Use |DSA_parse_parameters| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 376 | OPENSSL_EXPORT DSA *d2i_DSAparams(DSA **out, const uint8_t **inp, long len); |
| 377 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 378 | // i2d_DSAparams marshals DSA parameters from |in| to an ASN.1, DER structure. |
| 379 | // If |outp| is not NULL then the result is written to |*outp| and |*outp| is |
| 380 | // advanced just past the output. It returns the number of bytes in the result, |
| 381 | // whether written or not, or a negative value on error. |
| 382 | // |
| 383 | // Use |DSA_marshal_parameters| instead. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 384 | OPENSSL_EXPORT int i2d_DSAparams(const DSA *in, uint8_t **outp); |
| 385 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 386 | // DSA_generate_parameters is a deprecated version of |
| 387 | // |DSA_generate_parameters_ex| that creates and returns a |DSA*|. Don't use |
| 388 | // it. |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 389 | OPENSSL_EXPORT DSA *DSA_generate_parameters(int bits, unsigned char *seed, |
| 390 | int seed_len, int *counter_ret, |
| 391 | unsigned long *h_ret, |
| 392 | void (*callback)(int, int, void *), |
| 393 | void *cb_arg); |
| 394 | |
| 395 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 396 | struct dsa_st { |
| 397 | long version; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 398 | BIGNUM *p; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 399 | BIGNUM *q; // == 20 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 400 | BIGNUM *g; |
| 401 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 402 | BIGNUM *pub_key; // y public key |
| 403 | BIGNUM *priv_key; // x private key |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 404 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 405 | int flags; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 406 | // Normally used to cache montgomery values |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 407 | CRYPTO_MUTEX method_mont_lock; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 408 | BN_MONT_CTX *method_mont_p; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 409 | BN_MONT_CTX *method_mont_q; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 410 | CRYPTO_refcount_t references; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 411 | CRYPTO_EX_DATA ex_data; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | |
| 415 | #if defined(__cplusplus) |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 416 | } // extern C |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 417 | |
| 418 | extern "C++" { |
| 419 | |
| 420 | namespace bssl { |
| 421 | |
| 422 | BORINGSSL_MAKE_DELETER(DSA, DSA_free) |
| 423 | BORINGSSL_MAKE_DELETER(DSA_SIG, DSA_SIG_free) |
| 424 | |
| 425 | } // namespace bssl |
| 426 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 427 | } // extern C++ |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 428 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 429 | #endif |
| 430 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 431 | #define DSA_R_BAD_Q_VALUE 100 |
| 432 | #define DSA_R_MISSING_PARAMETERS 101 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 433 | #define DSA_R_MODULUS_TOO_LARGE 102 |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 434 | #define DSA_R_NEED_NEW_SETUP_VALUES 103 |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 435 | #define DSA_R_BAD_VERSION 104 |
| 436 | #define DSA_R_DECODE_ERROR 105 |
| 437 | #define DSA_R_ENCODE_ERROR 106 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 438 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 439 | #endif // OPENSSL_HEADER_DSA_H |