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., 51 Franklin Street, Fifth Floor, Boston,
26 #ifndef NETTLE_DSA_H_INCLUDED
27 #define NETTLE_DSA_H_INCLUDED
31 #include "nettle-types.h"
41 #define dsa_public_key_init nettle_dsa_public_key_init
42 #define dsa_public_key_clear nettle_dsa_public_key_clear
43 #define dsa_private_key_init nettle_dsa_private_key_init
44 #define dsa_private_key_clear nettle_dsa_private_key_clear
45 #define dsa_signature_init nettle_dsa_signature_init
46 #define dsa_signature_clear nettle_dsa_signature_clear
47 #define dsa_sha1_sign nettle_dsa_sha1_sign
48 #define dsa_sha1_verify nettle_dsa_sha1_verify
49 #define dsa_sha256_sign nettle_dsa_sha256_sign
50 #define dsa_sha256_verify nettle_dsa_sha256_verify
51 #define dsa_sha1_sign_digest nettle_dsa_sha1_sign_digest
52 #define dsa_sha1_verify_digest nettle_dsa_sha1_verify_digest
53 #define dsa_sha256_sign_digest nettle_dsa_sha256_sign_digest
54 #define dsa_sha256_verify_digest nettle_dsa_sha256_verify_digest
55 #define dsa_generate_keypair nettle_dsa_generate_keypair
56 #define dsa_signature_from_sexp nettle_dsa_signature_from_sexp
57 #define dsa_keypair_to_sexp nettle_dsa_keypair_to_sexp
58 #define dsa_keypair_from_sexp_alist nettle_dsa_keypair_from_sexp_alist
59 #define dsa_sha1_keypair_from_sexp nettle_dsa_sha1_keypair_from_sexp
60 #define dsa_sha256_keypair_from_sexp nettle_dsa_sha256_keypair_from_sexp
61 #define dsa_params_from_der_iterator nettle_dsa_params_from_der_iterator
62 #define dsa_public_key_from_der_iterator nettle_dsa_public_key_from_der_iterator
63 #define dsa_openssl_private_key_from_der_iterator nettle_dsa_openssl_private_key_from_der_iterator
64 #define dsa_openssl_private_key_from_der nettle_openssl_provate_key_from_der
65 #define _dsa_sign _nettle_dsa_sign
66 #define _dsa_verify _nettle_dsa_verify
68 #define DSA_SHA1_MIN_P_BITS 512
69 #define DSA_SHA1_Q_OCTETS 20
70 #define DSA_SHA1_Q_BITS 160
72 #define DSA_SHA256_MIN_P_BITS 1024
73 #define DSA_SHA256_Q_OCTETS 32
74 #define DSA_SHA256_Q_BITS 256
91 struct dsa_private_key
93 /* Unlike an rsa public key, private key operations will need both
94 * the private and the public information. */
104 /* Signing a message works as follows:
106 * Store the private key in a dsa_private_key struct.
108 * Initialize a hashing context, by callling
111 * Hash the message by calling
114 * Create the signature by calling
117 * The signature is represented as a struct dsa_signature. This call also
118 * resets the hashing context.
120 * When done with the key and signature, don't forget to call
121 * dsa_signature_clear.
124 /* Calls mpz_init to initialize bignum storage. */
126 dsa_public_key_init(struct dsa_public_key
*key
);
128 /* Calls mpz_clear to deallocate bignum storage. */
130 dsa_public_key_clear(struct dsa_public_key
*key
);
133 /* Calls mpz_init to initialize bignum storage. */
135 dsa_private_key_init(struct dsa_private_key
*key
);
137 /* Calls mpz_clear to deallocate bignum storage. */
139 dsa_private_key_clear(struct dsa_private_key
*key
);
141 /* Calls mpz_init to initialize bignum storage. */
143 dsa_signature_init(struct dsa_signature
*signature
);
145 /* Calls mpz_clear to deallocate bignum storage. */
147 dsa_signature_clear(struct dsa_signature
*signature
);
151 dsa_sha1_sign(const struct dsa_public_key
*pub
,
152 const struct dsa_private_key
*key
,
153 void *random_ctx
, nettle_random_func
*random
,
154 struct sha1_ctx
*hash
,
155 struct dsa_signature
*signature
);
158 dsa_sha256_sign(const struct dsa_public_key
*pub
,
159 const struct dsa_private_key
*key
,
160 void *random_ctx
, nettle_random_func
*random
,
161 struct sha256_ctx
*hash
,
162 struct dsa_signature
*signature
);
165 dsa_sha1_verify(const struct dsa_public_key
*key
,
166 struct sha1_ctx
*hash
,
167 const struct dsa_signature
*signature
);
170 dsa_sha256_verify(const struct dsa_public_key
*key
,
171 struct sha256_ctx
*hash
,
172 const struct dsa_signature
*signature
);
175 dsa_sha1_sign_digest(const struct dsa_public_key
*pub
,
176 const struct dsa_private_key
*key
,
177 void *random_ctx
, nettle_random_func
*random
,
178 const uint8_t *digest
,
179 struct dsa_signature
*signature
);
181 dsa_sha256_sign_digest(const struct dsa_public_key
*pub
,
182 const struct dsa_private_key
*key
,
183 void *random_ctx
, nettle_random_func
*random
,
184 const uint8_t *digest
,
185 struct dsa_signature
*signature
);
188 dsa_sha1_verify_digest(const struct dsa_public_key
*key
,
189 const uint8_t *digest
,
190 const struct dsa_signature
*signature
);
193 dsa_sha256_verify_digest(const struct dsa_public_key
*key
,
194 const uint8_t *digest
,
195 const struct dsa_signature
*signature
);
200 dsa_generate_keypair(struct dsa_public_key
*pub
,
201 struct dsa_private_key
*key
,
203 void *random_ctx
, nettle_random_func
*random
,
205 void *progress_ctx
, nettle_progress_func
*progress
,
206 unsigned p_bits
, unsigned q_bits
);
208 /* Keys in sexp form. */
210 struct nettle_buffer
;
212 /* Generates a public-key expression if PRIV is NULL .*/
214 dsa_keypair_to_sexp(struct nettle_buffer
*buffer
,
215 const char *algorithm_name
, /* NULL means "dsa" */
216 const struct dsa_public_key
*pub
,
217 const struct dsa_private_key
*priv
);
219 struct sexp_iterator
;
222 dsa_signature_from_sexp(struct dsa_signature
*rs
,
223 struct sexp_iterator
*i
,
227 dsa_keypair_from_sexp_alist(struct dsa_public_key
*pub
,
228 struct dsa_private_key
*priv
,
231 struct sexp_iterator
*i
);
233 /* If PRIV is NULL, expect a public-key expression. If PUB is NULL,
234 * expect a private key expression and ignore the parts not needed for
236 /* Keys must be initialized before calling this function, as usual. */
238 dsa_sha1_keypair_from_sexp(struct dsa_public_key
*pub
,
239 struct dsa_private_key
*priv
,
241 unsigned length
, const uint8_t *expr
);
244 dsa_sha256_keypair_from_sexp(struct dsa_public_key
*pub
,
245 struct dsa_private_key
*priv
,
247 unsigned length
, const uint8_t *expr
);
249 /* Keys in X.509 andd OpenSSL format. */
250 struct asn1_der_iterator
;
253 dsa_params_from_der_iterator(struct dsa_public_key
*pub
,
255 struct asn1_der_iterator
*i
);
257 dsa_public_key_from_der_iterator(struct dsa_public_key
*pub
,
259 struct asn1_der_iterator
*i
);
262 dsa_openssl_private_key_from_der_iterator(struct dsa_public_key
*pub
,
263 struct dsa_private_key
*priv
,
265 struct asn1_der_iterator
*i
);
268 dsa_openssl_private_key_from_der(struct dsa_public_key
*pub
,
269 struct dsa_private_key
*priv
,
271 unsigned length
, const uint8_t *data
);
274 /* Internal functions. */
276 _dsa_sign(const struct dsa_public_key
*pub
,
277 const struct dsa_private_key
*key
,
278 void *random_ctx
, nettle_random_func
*random
,
279 unsigned digest_size
,
280 const uint8_t *digest
,
281 struct dsa_signature
*signature
);
284 _dsa_verify(const struct dsa_public_key
*key
,
285 unsigned digest_size
,
286 const uint8_t *digest
,
287 const struct dsa_signature
*signature
);
293 #endif /* NETTLE_DSA_H_INCLUDED */