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