4ec96ed3d9d19929aaba5a0dfb3b7845dcccc18e
[platform/upstream/nettle.git] / dsa-compat.h
1 /* dsa-compat.h
2
3    Old DSA publickey interface.
4
5    Copyright (C) 2002, 2013, 2014 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #ifndef NETTLE_DSA_COMPAT_H_INCLUDED
35 #define NETTLE_DSA_COMPAT_H_INCLUDED
36
37 #include "dsa.h"
38
39 #include "sha1.h"
40 #include "sha2.h"
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_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_compat_generate_keypair nettle_dsa_compat_generate_keypair
56
57 /* Switch meaning of dsa_generate_keypair */
58 #undef dsa_generate_keypair
59 #define dsa_generate_keypair nettle_dsa_compat_generate_keypair
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 struct dsa_public_key
66 {
67   /* Same as struct dsa_params, but can't use that struct here without
68      breaking backwards compatibility. Layout must be identical, since
69      this is cast to a struct dsa_param pointer for calling _dsa_sign
70      and _dsa_verify */
71   mpz_t p;
72   mpz_t q;
73   mpz_t g;
74
75   /* Public value */
76   mpz_t y;
77 };
78
79 struct dsa_private_key
80 {
81   /* Unlike an rsa public key, private key operations will need both
82    * the private and the public information. */
83   mpz_t x;
84 };
85
86 /* Signing a message works as follows:
87  *
88  * Store the private key in a dsa_private_key struct.
89  *
90  * Initialize a hashing context, by callling
91  *   sha1_init
92  *
93  * Hash the message by calling
94  *   sha1_update
95  *
96  * Create the signature by calling
97  *   dsa_sha1_sign
98  *
99  * The signature is represented as a struct dsa_signature. This call also
100  * resets the hashing context.
101  *
102  * When done with the key and signature, don't forget to call
103  * dsa_signature_clear.
104  */
105
106 /* Calls mpz_init to initialize bignum storage. */
107 void
108 dsa_public_key_init(struct dsa_public_key *key);
109
110 /* Calls mpz_clear to deallocate bignum storage. */
111 void
112 dsa_public_key_clear(struct dsa_public_key *key);
113
114
115 /* Calls mpz_init to initialize bignum storage. */
116 void
117 dsa_private_key_init(struct dsa_private_key *key);
118
119 /* Calls mpz_clear to deallocate bignum storage. */
120 void
121 dsa_private_key_clear(struct dsa_private_key *key);
122
123 int
124 dsa_sha1_sign(const struct dsa_public_key *pub,
125               const struct dsa_private_key *key,
126               void *random_ctx, nettle_random_func *random,
127               struct sha1_ctx *hash,
128               struct dsa_signature *signature);
129
130 int
131 dsa_sha256_sign(const struct dsa_public_key *pub,
132                 const struct dsa_private_key *key,
133                 void *random_ctx, nettle_random_func *random,
134                 struct sha256_ctx *hash,
135                 struct dsa_signature *signature);
136
137 int
138 dsa_sha1_verify(const struct dsa_public_key *key,
139                 struct sha1_ctx *hash,
140                 const struct dsa_signature *signature);
141
142 int
143 dsa_sha256_verify(const struct dsa_public_key *key,
144                   struct sha256_ctx *hash,
145                   const struct dsa_signature *signature);
146
147 int
148 dsa_sha1_sign_digest(const struct dsa_public_key *pub,
149                      const struct dsa_private_key *key,
150                      void *random_ctx, nettle_random_func *random,
151                      const uint8_t *digest,
152                      struct dsa_signature *signature);
153 int
154 dsa_sha256_sign_digest(const struct dsa_public_key *pub,
155                        const struct dsa_private_key *key,
156                        void *random_ctx, nettle_random_func *random,
157                        const uint8_t *digest,
158                        struct dsa_signature *signature);
159
160 int
161 dsa_sha1_verify_digest(const struct dsa_public_key *key,
162                        const uint8_t *digest,
163                        const struct dsa_signature *signature);
164
165 int
166 dsa_sha256_verify_digest(const struct dsa_public_key *key,
167                          const uint8_t *digest,
168                          const struct dsa_signature *signature);
169
170 /* Key generation */
171 int
172 dsa_generate_keypair(struct dsa_public_key *pub,
173                      struct dsa_private_key *key,
174
175                      void *random_ctx, nettle_random_func *random,
176                      void *progress_ctx, nettle_progress_func *progress,
177                      unsigned p_bits, unsigned q_bits);
178
179 #ifdef __cplusplus
180 }
181 #endif
182
183 #endif /* NETTLE_DSA_COMPAT_H_INCLUDED */