Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / dsa.h
1 /* dsa.h
2  *
3  * The DSA publickey algorithm.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2002 Niels Möller
9  *  
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.
14  * 
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.
19  * 
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,
23  * MA 02111-1301, USA.
24  */
25  
26 #ifndef NETTLE_DSA_H_INCLUDED
27 #define NETTLE_DSA_H_INCLUDED
28
29 #include <gmp.h>
30
31 #include "nettle-types.h"
32
33 #include "sha1.h"
34 #include "sha2.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /* Name mangling */
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
67
68 #define DSA_SHA1_MIN_P_BITS 512
69 #define DSA_SHA1_Q_OCTETS 20
70 #define DSA_SHA1_Q_BITS 160
71
72 #define DSA_SHA256_MIN_P_BITS 1024
73 #define DSA_SHA256_Q_OCTETS 32
74 #define DSA_SHA256_Q_BITS 256
75   
76 struct dsa_public_key
77 {  
78   /* Modulo */
79   mpz_t p;
80
81   /* Group order */
82   mpz_t q;
83
84   /* Generator */
85   mpz_t g;
86   
87   /* Public value */
88   mpz_t y;
89 };
90
91 struct dsa_private_key
92 {
93   /* Unlike an rsa public key, private key operations will need both
94    * the private and the public information. */
95   mpz_t x;
96 };
97
98 struct dsa_signature
99 {
100   mpz_t r;
101   mpz_t s;
102 };
103
104 /* Signing a message works as follows:
105  *
106  * Store the private key in a dsa_private_key struct.
107  *
108  * Initialize a hashing context, by callling
109  *   sha1_init
110  *
111  * Hash the message by calling
112  *   sha1_update
113  *
114  * Create the signature by calling
115  *   dsa_sha1_sign
116  *
117  * The signature is represented as a struct dsa_signature. This call also
118  * resets the hashing context.
119  *
120  * When done with the key and signature, don't forget to call
121  * dsa_signature_clear.
122  */
123
124 /* Calls mpz_init to initialize bignum storage. */
125 void
126 dsa_public_key_init(struct dsa_public_key *key);
127
128 /* Calls mpz_clear to deallocate bignum storage. */
129 void
130 dsa_public_key_clear(struct dsa_public_key *key);
131
132
133 /* Calls mpz_init to initialize bignum storage. */
134 void
135 dsa_private_key_init(struct dsa_private_key *key);
136
137 /* Calls mpz_clear to deallocate bignum storage. */
138 void
139 dsa_private_key_clear(struct dsa_private_key *key);
140
141 /* Calls mpz_init to initialize bignum storage. */
142 void
143 dsa_signature_init(struct dsa_signature *signature);
144
145 /* Calls mpz_clear to deallocate bignum storage. */
146 void
147 dsa_signature_clear(struct dsa_signature *signature);
148
149
150 int
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);
156
157 int
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);
163
164 int
165 dsa_sha1_verify(const struct dsa_public_key *key,
166                 struct sha1_ctx *hash,
167                 const struct dsa_signature *signature);
168
169 int
170 dsa_sha256_verify(const struct dsa_public_key *key,
171                   struct sha256_ctx *hash,
172                   const struct dsa_signature *signature);
173
174 int
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);
180 int
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);
186
187 int
188 dsa_sha1_verify_digest(const struct dsa_public_key *key,
189                        const uint8_t *digest,
190                        const struct dsa_signature *signature);
191
192 int
193 dsa_sha256_verify_digest(const struct dsa_public_key *key,
194                          const uint8_t *digest,
195                          const struct dsa_signature *signature);
196
197 /* Key generation */
198
199 int
200 dsa_generate_keypair(struct dsa_public_key *pub,
201                      struct dsa_private_key *key,
202
203                      void *random_ctx, nettle_random_func *random,
204
205                      void *progress_ctx, nettle_progress_func *progress,
206                      unsigned p_bits, unsigned q_bits);
207
208 /* Keys in sexp form. */
209
210 struct nettle_buffer;
211
212 /* Generates a public-key expression if PRIV is NULL .*/
213 int
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);
218
219 struct sexp_iterator;
220
221 int
222 dsa_signature_from_sexp(struct dsa_signature *rs,
223                         struct sexp_iterator *i,
224                         unsigned q_bits);
225
226 int
227 dsa_keypair_from_sexp_alist(struct dsa_public_key *pub,
228                             struct dsa_private_key *priv,
229                             unsigned p_max_bits,
230                             unsigned q_bits,
231                             struct sexp_iterator *i);
232
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
235  * the public key. */
236 /* Keys must be initialized before calling this function, as usual. */
237 int
238 dsa_sha1_keypair_from_sexp(struct dsa_public_key *pub,
239                            struct dsa_private_key *priv,
240                            unsigned p_max_bits,
241                            unsigned length, const uint8_t *expr);
242
243 int
244 dsa_sha256_keypair_from_sexp(struct dsa_public_key *pub,
245                              struct dsa_private_key *priv,
246                              unsigned p_max_bits,
247                              unsigned length, const uint8_t *expr);
248
249 /* Keys in X.509 andd OpenSSL format. */
250 struct asn1_der_iterator;
251
252 int
253 dsa_params_from_der_iterator(struct dsa_public_key *pub,
254                              unsigned p_max_bits,
255                              struct asn1_der_iterator *i);
256 int
257 dsa_public_key_from_der_iterator(struct dsa_public_key *pub,
258                                  unsigned p_max_bits,
259                                  struct asn1_der_iterator *i);
260
261 int
262 dsa_openssl_private_key_from_der_iterator(struct dsa_public_key *pub,
263                                           struct dsa_private_key *priv,
264                                           unsigned p_max_bits,
265                                           struct asn1_der_iterator *i);
266
267 int
268 dsa_openssl_private_key_from_der(struct dsa_public_key *pub,
269                                  struct dsa_private_key *priv,
270                                  unsigned p_max_bits, 
271                                  unsigned length, const uint8_t *data);
272
273
274 /* Internal functions. */
275 int
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);
282
283 int
284 _dsa_verify(const struct dsa_public_key *key,
285             unsigned digest_size,
286             const uint8_t *digest,
287             const struct dsa_signature *signature);
288
289 #ifdef __cplusplus
290 }
291 #endif
292
293 #endif /* NETTLE_DSA_H_INCLUDED */