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