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