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