3 * The DSA publickey algorithm.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 #ifndef NETTLE_DSA_H_INCLUDED
27 #define NETTLE_DSA_H_INCLUDED
31 #include "nettle-types.h"
35 /* For nettle_random_func */
36 #include "nettle-meta.h"
43 #define dsa_public_key_init nettle_dsa_public_key_init
44 #define dsa_public_key_clear nettle_dsa_public_key_clear
45 #define dsa_private_key_init nettle_dsa_private_key_init
46 #define dsa_private_key_clear nettle_dsa_private_key_clear
47 #define dsa_signature_init nettle_dsa_signature_init
48 #define dsa_signature_clear nettle_dsa_signature_clear
49 #define dsa_sha1_sign nettle_dsa_sha1_sign
50 #define dsa_sha1_verify nettle_dsa_sha1_verify
51 #define dsa_sha256_sign nettle_dsa_sha256_sign
52 #define dsa_sha256_verify nettle_dsa_sha256_verify
53 #define dsa_sha1_sign_digest nettle_dsa_sha1_sign_digest
54 #define dsa_sha1_verify_digest nettle_dsa_sha1_verify_digest
55 #define dsa_sha256_sign_digest nettle_dsa_sha256_sign_digest
56 #define dsa_sha256_verify_digest nettle_dsa_sha256_verify_digest
57 #define dsa_generate_keypair nettle_dsa_generate_keypair
58 #define dsa_signature_from_sexp nettle_dsa_signature_from_sexp
59 #define dsa_keypair_to_sexp nettle_dsa_keypair_to_sexp
60 #define dsa_keypair_from_sexp_alist nettle_dsa_keypair_from_sexp_alist
61 #define dsa_sha1_keypair_from_sexp nettle_dsa_sha1_keypair_from_sexp
62 #define dsa_sha256_keypair_from_sexp nettle_dsa_sha256_keypair_from_sexp
63 #define dsa_params_from_der_iterator nettle_dsa_params_from_der_iterator
64 #define dsa_public_key_from_der_iterator nettle_dsa_public_key_from_der_iterator
65 #define dsa_openssl_private_key_from_der_iterator nettle_dsa_openssl_private_key_from_der_iterator
66 #define dsa_openssl_private_key_from_der nettle_openssl_provate_key_from_der
67 #define _dsa_sign _nettle_dsa_sign
68 #define _dsa_verify _nettle_dsa_verify
70 #define DSA_SHA1_MIN_P_BITS 512
71 #define DSA_SHA1_Q_OCTETS 20
72 #define DSA_SHA1_Q_BITS 160
74 #define DSA_SHA256_MIN_P_BITS 1024
75 #define DSA_SHA256_Q_OCTETS 32
76 #define DSA_SHA256_Q_BITS 256
93 struct dsa_private_key
95 /* Unlike an rsa public key, private key operations will need both
96 * the private and the public information. */
106 /* Signing a message works as follows:
108 * Store the private key in a dsa_private_key struct.
110 * Initialize a hashing context, by callling
113 * Hash the message by calling
116 * Create the signature by calling
119 * The signature is represented as a struct dsa_signature. This call also
120 * resets the hashing context.
122 * When done with the key and signature, don't forget to call
123 * dsa_signature_clear.
126 /* Calls mpz_init to initialize bignum storage. */
128 dsa_public_key_init(struct dsa_public_key *key);
130 /* Calls mpz_clear to deallocate bignum storage. */
132 dsa_public_key_clear(struct dsa_public_key *key);
135 /* Calls mpz_init to initialize bignum storage. */
137 dsa_private_key_init(struct dsa_private_key *key);
139 /* Calls mpz_clear to deallocate bignum storage. */
141 dsa_private_key_clear(struct dsa_private_key *key);
143 /* Calls mpz_init to initialize bignum storage. */
145 dsa_signature_init(struct dsa_signature *signature);
147 /* Calls mpz_clear to deallocate bignum storage. */
149 dsa_signature_clear(struct dsa_signature *signature);
153 dsa_sha1_sign(const struct dsa_public_key *pub,
154 const struct dsa_private_key *key,
155 void *random_ctx, nettle_random_func random,
156 struct sha1_ctx *hash,
157 struct dsa_signature *signature);
160 dsa_sha256_sign(const struct dsa_public_key *pub,
161 const struct dsa_private_key *key,
162 void *random_ctx, nettle_random_func random,
163 struct sha256_ctx *hash,
164 struct dsa_signature *signature);
167 dsa_sha1_verify(const struct dsa_public_key *key,
168 struct sha1_ctx *hash,
169 const struct dsa_signature *signature);
172 dsa_sha256_verify(const struct dsa_public_key *key,
173 struct sha256_ctx *hash,
174 const struct dsa_signature *signature);
177 dsa_sha1_sign_digest(const struct dsa_public_key *pub,
178 const struct dsa_private_key *key,
179 void *random_ctx, nettle_random_func random,
180 const uint8_t *digest,
181 struct dsa_signature *signature);
183 dsa_sha256_sign_digest(const struct dsa_public_key *pub,
184 const struct dsa_private_key *key,
185 void *random_ctx, nettle_random_func random,
186 const uint8_t *digest,
187 struct dsa_signature *signature);
190 dsa_sha1_verify_digest(const struct dsa_public_key *key,
191 const uint8_t *digest,
192 const struct dsa_signature *signature);
195 dsa_sha256_verify_digest(const struct dsa_public_key *key,
196 const uint8_t *digest,
197 const struct dsa_signature *signature);
202 dsa_generate_keypair(struct dsa_public_key *pub,
203 struct dsa_private_key *key,
205 void *random_ctx, nettle_random_func random,
207 void *progress_ctx, nettle_progress_func progress,
208 unsigned p_bits, unsigned q_bits);
210 /* Keys in sexp form. */
212 struct nettle_buffer;
214 /* Generates a public-key expression if PRIV is NULL .*/
216 dsa_keypair_to_sexp(struct nettle_buffer *buffer,
217 const char *algorithm_name, /* NULL means "dsa" */
218 const struct dsa_public_key *pub,
219 const struct dsa_private_key *priv);
221 struct sexp_iterator;
224 dsa_signature_from_sexp(struct dsa_signature *rs,
225 struct sexp_iterator *i,
229 dsa_keypair_from_sexp_alist(struct dsa_public_key *pub,
230 struct dsa_private_key *priv,
233 struct sexp_iterator *i);
235 /* If PRIV is NULL, expect a public-key expression. If PUB is NULL,
236 * expect a private key expression and ignore the parts not needed for
238 /* Keys must be initialized before calling this function, as usual. */
240 dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
241 struct dsa_private_key *priv,
243 unsigned length, const uint8_t *expr);
246 dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
247 struct dsa_private_key *priv,
249 unsigned length, const uint8_t *expr);
251 /* Keys in X.509 andd OpenSSL format. */
252 struct asn1_der_iterator;
255 dsa_params_from_der_iterator(struct dsa_public_key *pub,
257 struct asn1_der_iterator *i);
259 dsa_public_key_from_der_iterator(struct dsa_public_key *pub,
261 struct asn1_der_iterator *i);
264 dsa_openssl_private_key_from_der_iterator(struct dsa_public_key *pub,
265 struct dsa_private_key *priv,
267 struct asn1_der_iterator *i);
270 dsa_openssl_private_key_from_der(struct dsa_public_key *pub,
271 struct dsa_private_key *priv,
273 unsigned length, const uint8_t *data);
276 /* Internal functions. */
278 _dsa_sign(const struct dsa_public_key *pub,
279 const struct dsa_private_key *key,
280 void *random_ctx, nettle_random_func random,
281 unsigned digest_size,
282 const uint8_t *digest,
283 struct dsa_signature *signature);
286 _dsa_verify(const struct dsa_public_key *key,
287 unsigned digest_size,
288 const uint8_t *digest,
289 const struct dsa_signature *signature);
295 #endif /* NETTLE_DSA_H_INCLUDED */