Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:15

0001 /* dsa.h
0002 
0003    The DSA publickey algorithm.
0004 
0005    Copyright (C) 2002, 2013, 2014 Niels Möller
0006 
0007    This file is part of GNU Nettle.
0008 
0009    GNU Nettle is free software: you can redistribute it and/or
0010    modify it under the terms of either:
0011 
0012      * the GNU Lesser General Public License as published by the Free
0013        Software Foundation; either version 3 of the License, or (at your
0014        option) any later version.
0015 
0016    or
0017 
0018      * the GNU General Public License as published by the Free
0019        Software Foundation; either version 2 of the License, or (at your
0020        option) any later version.
0021 
0022    or both in parallel, as here.
0023 
0024    GNU Nettle is distributed in the hope that it will be useful,
0025    but WITHOUT ANY WARRANTY; without even the implied warranty of
0026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027    General Public License for more details.
0028 
0029    You should have received copies of the GNU General Public License and
0030    the GNU Lesser General Public License along with this program.  If
0031    not, see http://www.gnu.org/licenses/.
0032 */
0033  
0034 #ifndef NETTLE_DSA_H_INCLUDED
0035 #define NETTLE_DSA_H_INCLUDED
0036 
0037 #include "nettle-types.h"
0038 #include "bignum.h"
0039 
0040 #ifdef __cplusplus
0041 extern "C" {
0042 #endif
0043 
0044 /* Name mangling */
0045 #define dsa_params_init nettle_dsa_params_init
0046 #define dsa_params_clear nettle_dsa_params_clear
0047 #define dsa_signature_init nettle_dsa_signature_init
0048 #define dsa_signature_clear nettle_dsa_signature_clear
0049 #define dsa_sign nettle_dsa_sign
0050 #define dsa_verify nettle_dsa_verify
0051 #define dsa_generate_params nettle_dsa_generate_params
0052 #define dsa_generate_keypair nettle_dsa_generate_keypair
0053 #define dsa_signature_from_sexp nettle_dsa_signature_from_sexp
0054 #define dsa_keypair_to_sexp nettle_dsa_keypair_to_sexp
0055 #define dsa_keypair_from_sexp_alist nettle_dsa_keypair_from_sexp_alist
0056 #define dsa_sha1_keypair_from_sexp nettle_dsa_sha1_keypair_from_sexp
0057 #define dsa_sha256_keypair_from_sexp nettle_dsa_sha256_keypair_from_sexp
0058 #define dsa_params_from_der_iterator nettle_dsa_params_from_der_iterator
0059 #define dsa_public_key_from_der_iterator nettle_dsa_public_key_from_der_iterator
0060 #define dsa_openssl_private_key_from_der_iterator nettle_dsa_openssl_private_key_from_der_iterator 
0061 #define dsa_openssl_private_key_from_der nettle_openssl_provate_key_from_der
0062 
0063 /* For FIPS approved parameters */
0064 #define DSA_SHA1_MIN_P_BITS 512
0065 #define DSA_SHA1_Q_OCTETS 20
0066 #define DSA_SHA1_Q_BITS 160
0067 
0068 #define DSA_SHA256_MIN_P_BITS 1024
0069 #define DSA_SHA256_Q_OCTETS 32
0070 #define DSA_SHA256_Q_BITS 256
0071 
0072 struct dsa_params
0073 {  
0074   /* Modulo */
0075   mpz_t p;
0076 
0077   /* Group order */
0078   mpz_t q;
0079 
0080   /* Generator */
0081   mpz_t g;
0082 };
0083 
0084 void
0085 dsa_params_init (struct dsa_params *params);
0086 
0087 void
0088 dsa_params_clear (struct dsa_params *params);
0089 
0090 struct dsa_signature
0091 {
0092   mpz_t r;
0093   mpz_t s;
0094 };
0095 
0096 /* Calls mpz_init to initialize bignum storage. */
0097 void
0098 dsa_signature_init(struct dsa_signature *signature);
0099 
0100 /* Calls mpz_clear to deallocate bignum storage. */
0101 void
0102 dsa_signature_clear(struct dsa_signature *signature);
0103 
0104 int
0105 dsa_sign(const struct dsa_params *params,
0106      const mpz_t x,
0107      void *random_ctx, nettle_random_func *random,
0108      size_t digest_size,
0109      const uint8_t *digest,
0110      struct dsa_signature *signature);
0111 
0112 int
0113 dsa_verify(const struct dsa_params *params,
0114        const mpz_t y,
0115        size_t digest_size,
0116        const uint8_t *digest,
0117        const struct dsa_signature *signature);
0118 
0119 
0120 /* Key generation */
0121 
0122 int
0123 dsa_generate_params(struct dsa_params *params,
0124             void *random_ctx, nettle_random_func *random,
0125             void *progress_ctx, nettle_progress_func *progress,
0126             unsigned p_bits, unsigned q_bits);
0127 
0128 void
0129 dsa_generate_keypair (const struct dsa_params *params,
0130               mpz_t pub, mpz_t key,
0131               void *random_ctx, nettle_random_func *random);
0132 
0133 /* Keys in sexp form. */
0134 
0135 struct nettle_buffer;
0136 
0137 /* Generates a public-key expression if PRIV is NULL .*/
0138 int
0139 dsa_keypair_to_sexp(struct nettle_buffer *buffer,
0140             const char *algorithm_name, /* NULL means "dsa" */
0141             const struct dsa_params *params,
0142             const mpz_t pub,
0143             const mpz_t priv);
0144 
0145 struct sexp_iterator;
0146 
0147 int
0148 dsa_signature_from_sexp(struct dsa_signature *rs,
0149             struct sexp_iterator *i,
0150             unsigned q_bits);
0151 
0152 int
0153 dsa_keypair_from_sexp_alist(struct dsa_params *params,
0154                 mpz_t pub,
0155                 mpz_t priv,
0156                 unsigned p_max_bits,
0157                 unsigned q_bits,
0158                 struct sexp_iterator *i);
0159 
0160 /* If PRIV is NULL, expect a public-key expression. If PUB is NULL,
0161  * expect a private key expression and ignore the parts not needed for
0162  * the public key. */
0163 /* Keys must be initialized before calling this function, as usual. */
0164 int
0165 dsa_sha1_keypair_from_sexp(struct dsa_params *params,
0166                mpz_t pub,
0167                mpz_t priv,
0168                unsigned p_max_bits,
0169                size_t length, const uint8_t *expr);
0170 
0171 int
0172 dsa_sha256_keypair_from_sexp(struct dsa_params *params,
0173                  mpz_t pub,
0174                  mpz_t priv,
0175                  unsigned p_max_bits,
0176                  size_t length, const uint8_t *expr);
0177 
0178 /* Keys in X.509 andd OpenSSL format. */
0179 struct asn1_der_iterator;
0180 
0181 int
0182 dsa_params_from_der_iterator(struct dsa_params *params,
0183                  unsigned max_bits, unsigned q_bits,
0184                  struct asn1_der_iterator *i);
0185 
0186 int
0187 dsa_public_key_from_der_iterator(const struct dsa_params *params,
0188                  mpz_t pub,
0189                  struct asn1_der_iterator *i);
0190 
0191 int
0192 dsa_openssl_private_key_from_der_iterator(struct dsa_params *params,
0193                       mpz_t pub,
0194                       mpz_t priv,
0195                       unsigned p_max_bits,
0196                       struct asn1_der_iterator *i);
0197 
0198 int
0199 dsa_openssl_private_key_from_der(struct dsa_params *params,
0200                  mpz_t pub,
0201                  mpz_t priv,
0202                  unsigned p_max_bits,
0203                  size_t length, const uint8_t *data);
0204 
0205 
0206 #ifdef __cplusplus
0207 }
0208 #endif
0209 
0210 #endif /* NETTLE_DSA_H_INCLUDED */