gnunet-android

GNUnet for Android
Log | Files | Refs | README

dsa.h (16015B)


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