/src/libgcrypt/cipher/dsa.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* dsa.c - DSA signature algorithm |
2 | | * Copyright (C) 1998, 2000, 2001, 2002, 2003, |
3 | | * 2006, 2008 Free Software Foundation, Inc. |
4 | | * Copyright (C) 2013 g10 Code GmbH. |
5 | | * |
6 | | * This file is part of Libgcrypt. |
7 | | * |
8 | | * Libgcrypt is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as |
10 | | * published by the Free Software Foundation; either version 2.1 of |
11 | | * the License, or (at your option) any later version. |
12 | | * |
13 | | * Libgcrypt is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <config.h> |
23 | | #include <stdio.h> |
24 | | #include <stdlib.h> |
25 | | #include <string.h> |
26 | | |
27 | | #include "g10lib.h" |
28 | | #include "mpi.h" |
29 | | #include "cipher.h" |
30 | | #include "pubkey-internal.h" |
31 | | |
32 | | |
33 | | typedef struct |
34 | | { |
35 | | gcry_mpi_t p; /* prime */ |
36 | | gcry_mpi_t q; /* group order */ |
37 | | gcry_mpi_t g; /* group generator */ |
38 | | gcry_mpi_t y; /* g^x mod p */ |
39 | | } DSA_public_key; |
40 | | |
41 | | |
42 | | typedef struct |
43 | | { |
44 | | gcry_mpi_t p; /* prime */ |
45 | | gcry_mpi_t q; /* group order */ |
46 | | gcry_mpi_t g; /* group generator */ |
47 | | gcry_mpi_t y; /* g^x mod p */ |
48 | | gcry_mpi_t x; /* secret exponent */ |
49 | | } DSA_secret_key; |
50 | | |
51 | | |
52 | | /* A structure used to hold domain parameters. */ |
53 | | typedef struct |
54 | | { |
55 | | gcry_mpi_t p; /* prime */ |
56 | | gcry_mpi_t q; /* group order */ |
57 | | gcry_mpi_t g; /* group generator */ |
58 | | } dsa_domain_t; |
59 | | |
60 | | |
61 | | static const char *dsa_names[] = |
62 | | { |
63 | | "dsa", |
64 | | "openpgp-dsa", |
65 | | NULL, |
66 | | }; |
67 | | |
68 | | |
69 | | /* A sample 1024 bit DSA key used for the selftests. Not anymore |
70 | | * used, kept only for reference. */ |
71 | | #if 0 |
72 | | static const char sample_secret_key_1024[] = |
73 | | "(private-key" |
74 | | " (dsa" |
75 | | " (p #00AD7C0025BA1A15F775F3F2D673718391D00456978D347B33D7B49E7F32EDAB" |
76 | | " 96273899DD8B2BB46CD6ECA263FAF04A28903503D59062A8865D2AE8ADFB5191" |
77 | | " CF36FFB562D0E2F5809801A1F675DAE59698A9E01EFE8D7DCFCA084F4C6F5A44" |
78 | | " 44D499A06FFAEA5E8EF5E01F2FD20A7B7EF3F6968AFBA1FB8D91F1559D52D8777B#)" |
79 | | " (q #00EB7B5751D25EBBB7BD59D920315FD840E19AEBF9#)" |
80 | | " (g #1574363387FDFD1DDF38F4FBE135BB20C7EE4772FB94C337AF86EA8E49666503" |
81 | | " AE04B6BE81A2F8DD095311E0217ACA698A11E6C5D33CCDAE71498ED35D13991E" |
82 | | " B02F09AB40BD8F4C5ED8C75DA779D0AE104BC34C960B002377068AB4B5A1F984" |
83 | | " 3FBA91F537F1B7CAC4D8DD6D89B0D863AF7025D549F9C765D2FC07EE208F8D15#)" |
84 | | " (y #64B11EF8871BE4AB572AA810D5D3CA11A6CDBC637A8014602C72960DB135BF46" |
85 | | " A1816A724C34F87330FC9E187C5D66897A04535CC2AC9164A7150ABFA8179827" |
86 | | " 6E45831AB811EEE848EBB24D9F5F2883B6E5DDC4C659DEF944DCFD80BF4D0A20" |
87 | | " 42CAA7DC289F0C5A9D155F02D3D551DB741A81695B74D4C8F477F9C7838EB0FB#)" |
88 | | " (x #11D54E4ADBD3034160F2CED4B7CD292A4EBF3EC0#)))"; |
89 | | /* A sample 1024 bit DSA key used for the selftests (public only). */ |
90 | | static const char sample_public_key_1024[] = |
91 | | "(public-key" |
92 | | " (dsa" |
93 | | " (p #00AD7C0025BA1A15F775F3F2D673718391D00456978D347B33D7B49E7F32EDAB" |
94 | | " 96273899DD8B2BB46CD6ECA263FAF04A28903503D59062A8865D2AE8ADFB5191" |
95 | | " CF36FFB562D0E2F5809801A1F675DAE59698A9E01EFE8D7DCFCA084F4C6F5A44" |
96 | | " 44D499A06FFAEA5E8EF5E01F2FD20A7B7EF3F6968AFBA1FB8D91F1559D52D8777B#)" |
97 | | " (q #00EB7B5751D25EBBB7BD59D920315FD840E19AEBF9#)" |
98 | | " (g #1574363387FDFD1DDF38F4FBE135BB20C7EE4772FB94C337AF86EA8E49666503" |
99 | | " AE04B6BE81A2F8DD095311E0217ACA698A11E6C5D33CCDAE71498ED35D13991E" |
100 | | " B02F09AB40BD8F4C5ED8C75DA779D0AE104BC34C960B002377068AB4B5A1F984" |
101 | | " 3FBA91F537F1B7CAC4D8DD6D89B0D863AF7025D549F9C765D2FC07EE208F8D15#)" |
102 | | " (y #64B11EF8871BE4AB572AA810D5D3CA11A6CDBC637A8014602C72960DB135BF46" |
103 | | " A1816A724C34F87330FC9E187C5D66897A04535CC2AC9164A7150ABFA8179827" |
104 | | " 6E45831AB811EEE848EBB24D9F5F2883B6E5DDC4C659DEF944DCFD80BF4D0A20" |
105 | | " 42CAA7DC289F0C5A9D155F02D3D551DB741A81695B74D4C8F477F9C7838EB0FB#)))"; |
106 | | #endif /*0*/ |
107 | | |
108 | | /* 2048 DSA key from RFC 6979 A.2.2 */ |
109 | | static const char sample_public_key_2048[] = |
110 | | "(public-key" |
111 | | " (dsa" |
112 | | " (p #9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642F0B5C48C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757264E5A1A44FFE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F9716BFE6117C6B5B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091EB51743BF33050C38DE235567E1B34C3D6A5C0CEAA1A0F368213C3D19843D0B4B09DCB9FC72D39C8DE41F1BF14D4BB4563CA28371621CAD3324B6A2D392145BEBFAC748805236F5CA2FE92B871CD8F9C36D3292B5509CA8CAA77A2ADFC7BFD77DDA6F71125A7456FEA153E433256A2261C6A06ED3693797E7995FAD5AABBCFBE3EDA2741E375404AE25B#)" |
113 | | " (q #F2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F#)" |
114 | | " (g #5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E24809670716C613D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D1AA58C4328A06C46A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A338661D10461C0D135472085057F3494309FFA73C611F78B32ADBB5740C361C9F35BE90997DB2014E2EF5AA61782F52ABEB8BD6432C4DD097BC5423B285DAFB60DC364E8161F4A2A35ACA3A10B1C4D203CC76A470A33AFDCBDD92959859ABD8B56E1725252D78EAC66E71BA9AE3F1DD2487199874393CD4D832186800654760E1E34C09E4D155179F9EC0DC4473F996BDCE6EED1CABED8B6F116F7AD9CF505DF0F998E34AB27514B0FFE7#)" |
115 | | " (y #667098C654426C78D7F8201EAC6C203EF030D43605032C2F1FA937E5237DBD949F34A0A2564FE126DC8B715C5141802CE0979C8246463C40E6B6BDAA2513FA611728716C2E4FD53BC95B89E69949D96512E873B9C8F8DFD499CC312882561ADECB31F658E934C0C197F2C4D96B05CBAD67381E7B768891E4DA3843D24D94CDFB5126E9B8BF21E8358EE0E0A30EF13FD6A664C0DCE3731F7FB49A4845A4FD8254687972A2D382599C9BAC4E0ED7998193078913032558134976410B89D2C171D123AC35FD977219597AA7D15C1A9A428E59194F75C721EBCBCFAE44696A499AFA74E04299F132026601638CB87AB79190D4A0986315DA8EEC6561C938996BEADF#)))"; |
116 | | |
117 | | static const char sample_secret_key_2048[] = |
118 | | "(private-key" |
119 | | " (dsa" |
120 | | " (p #9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642F0B5C48C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757264E5A1A44FFE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F9716BFE6117C6B5B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091EB51743BF33050C38DE235567E1B34C3D6A5C0CEAA1A0F368213C3D19843D0B4B09DCB9FC72D39C8DE41F1BF14D4BB4563CA28371621CAD3324B6A2D392145BEBFAC748805236F5CA2FE92B871CD8F9C36D3292B5509CA8CAA77A2ADFC7BFD77DDA6F71125A7456FEA153E433256A2261C6A06ED3693797E7995FAD5AABBCFBE3EDA2741E375404AE25B#)" |
121 | | " (q #F2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F#)" |
122 | | " (g #5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E24809670716C613D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D1AA58C4328A06C46A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A338661D10461C0D135472085057F3494309FFA73C611F78B32ADBB5740C361C9F35BE90997DB2014E2EF5AA61782F52ABEB8BD6432C4DD097BC5423B285DAFB60DC364E8161F4A2A35ACA3A10B1C4D203CC76A470A33AFDCBDD92959859ABD8B56E1725252D78EAC66E71BA9AE3F1DD2487199874393CD4D832186800654760E1E34C09E4D155179F9EC0DC4473F996BDCE6EED1CABED8B6F116F7AD9CF505DF0F998E34AB27514B0FFE7#)" |
123 | | " (y #667098C654426C78D7F8201EAC6C203EF030D43605032C2F1FA937E5237DBD949F34A0A2564FE126DC8B715C5141802CE0979C8246463C40E6B6BDAA2513FA611728716C2E4FD53BC95B89E69949D96512E873B9C8F8DFD499CC312882561ADECB31F658E934C0C197F2C4D96B05CBAD67381E7B768891E4DA3843D24D94CDFB5126E9B8BF21E8358EE0E0A30EF13FD6A664C0DCE3731F7FB49A4845A4FD8254687972A2D382599C9BAC4E0ED7998193078913032558134976410B89D2C171D123AC35FD977219597AA7D15C1A9A428E59194F75C721EBCBCFAE44696A499AFA74E04299F132026601638CB87AB79190D4A0986315DA8EEC6561C938996BEADF#)" |
124 | | " (x #69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC#)))"; |
125 | | |
126 | | |
127 | | |
128 | | static int test_keys (DSA_secret_key *sk, unsigned int qbits); |
129 | | static int check_secret_key (DSA_secret_key *sk); |
130 | | static gpg_err_code_t generate (DSA_secret_key *sk, |
131 | | unsigned int nbits, |
132 | | unsigned int qbits, |
133 | | int transient_key, |
134 | | dsa_domain_t *domain, |
135 | | gcry_mpi_t **ret_factors); |
136 | | static gpg_err_code_t sign (gcry_mpi_t r, gcry_mpi_t s, |
137 | | gcry_mpi_t input, gcry_mpi_t k, |
138 | | DSA_secret_key *skey, int flags, int hashalgo); |
139 | | static gpg_err_code_t verify (gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t input, |
140 | | DSA_public_key *pkey, int flags, int hashalgo); |
141 | | static unsigned int dsa_get_nbits (gcry_sexp_t parms); |
142 | | |
143 | | |
144 | | static void (*progress_cb) (void *,const char *, int, int, int ); |
145 | | static void *progress_cb_data; |
146 | | |
147 | | |
148 | | /* Check the DSA key length is acceptable for key generation or usage */ |
149 | | static gpg_err_code_t |
150 | | dsa_check_keysize (unsigned int nbits) |
151 | 0 | { |
152 | 0 | if (fips_mode () && nbits < 2048) |
153 | 0 | return GPG_ERR_INV_VALUE; |
154 | | |
155 | 0 | return 0; |
156 | 0 | } |
157 | | |
158 | | |
159 | | void |
160 | | _gcry_register_pk_dsa_progress (void (*cb) (void *, const char *, |
161 | | int, int, int), |
162 | | void *cb_data) |
163 | 0 | { |
164 | 0 | progress_cb = cb; |
165 | 0 | progress_cb_data = cb_data; |
166 | 0 | } |
167 | | |
168 | | |
169 | | static void |
170 | | progress (int c) |
171 | 0 | { |
172 | 0 | if (progress_cb) |
173 | 0 | progress_cb (progress_cb_data, "pk_dsa", c, 0, 0); |
174 | 0 | } |
175 | | |
176 | | |
177 | | /* Check that a freshly generated key actually works. Returns 0 on success. */ |
178 | | static int |
179 | | test_keys (DSA_secret_key *sk, unsigned int qbits) |
180 | 0 | { |
181 | 0 | int result = -1; /* Default to failure. */ |
182 | 0 | DSA_public_key pk; |
183 | 0 | gcry_mpi_t data = mpi_new (qbits); |
184 | 0 | gcry_mpi_t sig_a = mpi_new (qbits); |
185 | 0 | gcry_mpi_t sig_b = mpi_new (qbits); |
186 | | |
187 | | /* Put the relevant parameters into a public key structure. */ |
188 | 0 | pk.p = sk->p; |
189 | 0 | pk.q = sk->q; |
190 | 0 | pk.g = sk->g; |
191 | 0 | pk.y = sk->y; |
192 | | |
193 | | /* Create a random plaintext. */ |
194 | 0 | _gcry_mpi_randomize (data, qbits, GCRY_WEAK_RANDOM); |
195 | | |
196 | | /* Sign DATA using the secret key. */ |
197 | 0 | sign (sig_a, sig_b, data, NULL, sk, 0, 0); |
198 | | |
199 | | /* Verify the signature using the public key. */ |
200 | 0 | if ( verify (sig_a, sig_b, data, &pk, 0, 0) ) |
201 | 0 | goto leave; /* Signature does not match. */ |
202 | | |
203 | | /* Modify the data and check that the signing fails. */ |
204 | 0 | mpi_add_ui (data, data, 1); |
205 | 0 | if ( !verify (sig_a, sig_b, data, &pk, 0, 0) ) |
206 | 0 | goto leave; /* Signature matches but should not. */ |
207 | | |
208 | 0 | result = 0; /* The test succeeded. */ |
209 | |
|
210 | 0 | leave: |
211 | 0 | _gcry_mpi_release (sig_b); |
212 | 0 | _gcry_mpi_release (sig_a); |
213 | 0 | _gcry_mpi_release (data); |
214 | 0 | return result; |
215 | 0 | } |
216 | | |
217 | | |
218 | | |
219 | | /* |
220 | | Generate a DSA key pair with a key of size NBITS. If transient_key |
221 | | is true the key is generated using the standard RNG and not the |
222 | | very secure one. |
223 | | |
224 | | Returns: 2 structures filled with all needed values |
225 | | and an array with the n-1 factors of (p-1) |
226 | | */ |
227 | | static gpg_err_code_t |
228 | | generate (DSA_secret_key *sk, unsigned int nbits, unsigned int qbits, |
229 | | int transient_key, dsa_domain_t *domain, gcry_mpi_t **ret_factors ) |
230 | 0 | { |
231 | 0 | gpg_err_code_t rc; |
232 | 0 | gcry_mpi_t p; /* the prime */ |
233 | 0 | gcry_mpi_t q; /* the 160 bit prime factor */ |
234 | 0 | gcry_mpi_t g; /* the generator */ |
235 | 0 | gcry_mpi_t y; /* g^x mod p */ |
236 | 0 | gcry_mpi_t x; /* the secret exponent */ |
237 | 0 | gcry_mpi_t h, e; /* helper */ |
238 | 0 | unsigned char *rndbuf; |
239 | 0 | gcry_random_level_t random_level; |
240 | |
|
241 | 0 | if (qbits) |
242 | 0 | ; /* Caller supplied qbits. Use this value. */ |
243 | 0 | else if ( nbits >= 512 && nbits <= 1024 ) |
244 | 0 | qbits = 160; |
245 | 0 | else if ( nbits == 2048 ) |
246 | 0 | qbits = 224; |
247 | 0 | else if ( nbits == 3072 ) |
248 | 0 | qbits = 256; |
249 | 0 | else if ( nbits == 7680 ) |
250 | 0 | qbits = 384; |
251 | 0 | else if ( nbits == 15360 ) |
252 | 0 | qbits = 512; |
253 | 0 | else |
254 | 0 | return GPG_ERR_INV_VALUE; |
255 | | |
256 | 0 | if (qbits < 160 || qbits > 512 || (qbits%8) ) |
257 | 0 | return GPG_ERR_INV_VALUE; |
258 | 0 | if (nbits < 2*qbits || nbits > 15360) |
259 | 0 | return GPG_ERR_INV_VALUE; |
260 | | |
261 | 0 | if (domain->p && domain->q && domain->g) |
262 | 0 | { |
263 | | /* Domain parameters are given; use them. */ |
264 | 0 | p = mpi_copy (domain->p); |
265 | 0 | q = mpi_copy (domain->q); |
266 | 0 | g = mpi_copy (domain->g); |
267 | 0 | gcry_assert (mpi_get_nbits (p) == nbits); |
268 | 0 | gcry_assert (mpi_get_nbits (q) == qbits); |
269 | 0 | h = mpi_alloc (0); |
270 | 0 | e = NULL; |
271 | 0 | } |
272 | 0 | else |
273 | 0 | { |
274 | | /* Generate new domain parameters. */ |
275 | 0 | rc = _gcry_generate_elg_prime (1, nbits, qbits, NULL, &p, ret_factors); |
276 | 0 | if (rc) |
277 | 0 | return rc; |
278 | | |
279 | | /* Get q out of factors. */ |
280 | 0 | q = mpi_copy ((*ret_factors)[0]); |
281 | 0 | gcry_assert (mpi_get_nbits (q) == qbits); |
282 | | |
283 | | /* Find a generator g (h and e are helpers). |
284 | | e = (p-1)/q */ |
285 | 0 | e = mpi_alloc (mpi_get_nlimbs (p)); |
286 | 0 | mpi_sub_ui (e, p, 1); |
287 | 0 | mpi_fdiv_q (e, e, q); |
288 | 0 | g = mpi_alloc (mpi_get_nlimbs (p)); |
289 | 0 | h = mpi_alloc_set_ui (1); /* (We start with 2.) */ |
290 | 0 | do |
291 | 0 | { |
292 | 0 | mpi_add_ui (h, h, 1); |
293 | | /* g = h^e mod p */ |
294 | 0 | mpi_powm (g, h, e, p); |
295 | 0 | } |
296 | 0 | while (!mpi_cmp_ui (g, 1)); /* Continue until g != 1. */ |
297 | 0 | } |
298 | | |
299 | | /* Select a random number X with the property: |
300 | | * 0 < x < q-1 |
301 | | * |
302 | | * FIXME: Why do we use the requirement x < q-1 ? It should be |
303 | | * sufficient to test for x < q. FIPS-186-3 check x < q-1 but it |
304 | | * does not check for 0 < x because it makes sure that Q is unsigned |
305 | | * and finally adds one to the result so that 0 will never be |
306 | | * returned. We should replace the code below with _gcry_dsa_gen_k. |
307 | | * |
308 | | * This must be a very good random number because this is the secret |
309 | | * part. The random quality depends on the transient_key flag. */ |
310 | 0 | random_level = transient_key ? GCRY_STRONG_RANDOM : GCRY_VERY_STRONG_RANDOM; |
311 | 0 | if (DBG_CIPHER) |
312 | 0 | log_debug("choosing a random x%s\n", transient_key? " (transient-key)":""); |
313 | 0 | gcry_assert( qbits >= 160 ); |
314 | 0 | x = mpi_alloc_secure( mpi_get_nlimbs(q) ); |
315 | 0 | mpi_sub_ui( h, q, 1 ); /* put q-1 into h */ |
316 | 0 | rndbuf = NULL; |
317 | 0 | do |
318 | 0 | { |
319 | 0 | if( DBG_CIPHER ) |
320 | 0 | progress('.'); |
321 | 0 | if( !rndbuf ) |
322 | 0 | rndbuf = _gcry_random_bytes_secure ((qbits+7)/8, random_level); |
323 | 0 | else |
324 | 0 | { /* Change only some of the higher bits (= 2 bytes)*/ |
325 | 0 | char *r = _gcry_random_bytes_secure (2, random_level); |
326 | 0 | memcpy(rndbuf, r, 2 ); |
327 | 0 | xfree(r); |
328 | 0 | } |
329 | |
|
330 | 0 | _gcry_mpi_set_buffer( x, rndbuf, (qbits+7)/8, 0 ); |
331 | 0 | mpi_clear_highbit( x, qbits+1 ); |
332 | 0 | } |
333 | 0 | while ( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, h )<0 ) ); |
334 | 0 | xfree(rndbuf); |
335 | 0 | mpi_free( e ); |
336 | 0 | mpi_free( h ); |
337 | | |
338 | | /* y = g^x mod p */ |
339 | 0 | y = mpi_alloc( mpi_get_nlimbs(p) ); |
340 | 0 | mpi_powm (y, g, x, p); |
341 | |
|
342 | 0 | if( DBG_CIPHER ) |
343 | 0 | { |
344 | 0 | progress('\n'); |
345 | 0 | log_mpidump("dsa p", p ); |
346 | 0 | log_mpidump("dsa q", q ); |
347 | 0 | log_mpidump("dsa g", g ); |
348 | 0 | log_mpidump("dsa y", y ); |
349 | 0 | log_mpidump("dsa x", x ); |
350 | 0 | } |
351 | | |
352 | | /* Copy the stuff to the key structures. */ |
353 | 0 | sk->p = p; |
354 | 0 | sk->q = q; |
355 | 0 | sk->g = g; |
356 | 0 | sk->y = y; |
357 | 0 | sk->x = x; |
358 | | |
359 | | /* Now we can test our keys (this should never fail!). */ |
360 | 0 | if ( test_keys (sk, qbits) ) |
361 | 0 | { |
362 | 0 | _gcry_mpi_release (sk->p); sk->p = NULL; |
363 | 0 | _gcry_mpi_release (sk->q); sk->q = NULL; |
364 | 0 | _gcry_mpi_release (sk->g); sk->g = NULL; |
365 | 0 | _gcry_mpi_release (sk->y); sk->y = NULL; |
366 | 0 | _gcry_mpi_release (sk->x); sk->x = NULL; |
367 | 0 | fips_signal_error ("self-test after key generation failed"); |
368 | 0 | return GPG_ERR_SELFTEST_FAILED; |
369 | 0 | } |
370 | 0 | return 0; |
371 | 0 | } |
372 | | |
373 | | |
374 | | /* Generate a DSA key pair with a key of size NBITS using the |
375 | | algorithm given in FIPS-186-3. If USE_FIPS186_2 is true, |
376 | | FIPS-186-2 is used and thus the length is restricted to 1024/160. |
377 | | If DERIVEPARMS is not NULL it may contain a seed value. If domain |
378 | | parameters are specified in DOMAIN, DERIVEPARMS may not be given |
379 | | and NBITS and QBITS must match the specified domain parameters. */ |
380 | | static gpg_err_code_t |
381 | | generate_fips186 (DSA_secret_key *sk, unsigned int nbits, unsigned int qbits, |
382 | | gcry_sexp_t deriveparms, int use_fips186_2, |
383 | | dsa_domain_t *domain, |
384 | | int *r_counter, void **r_seed, size_t *r_seedlen, |
385 | | gcry_mpi_t *r_h) |
386 | 0 | { |
387 | 0 | gpg_err_code_t ec; |
388 | 0 | struct { |
389 | 0 | gcry_sexp_t sexp; |
390 | 0 | const void *seed; |
391 | 0 | size_t seedlen; |
392 | 0 | } initial_seed = { NULL, NULL, 0 }; |
393 | 0 | gcry_mpi_t prime_q = NULL; |
394 | 0 | gcry_mpi_t prime_p = NULL; |
395 | 0 | gcry_mpi_t value_g = NULL; /* The generator. */ |
396 | 0 | gcry_mpi_t value_y = NULL; /* g^x mod p */ |
397 | 0 | gcry_mpi_t value_x = NULL; /* The secret exponent. */ |
398 | 0 | gcry_mpi_t value_h = NULL; /* Helper. */ |
399 | 0 | gcry_mpi_t value_e = NULL; /* Helper. */ |
400 | 0 | gcry_mpi_t value_c = NULL; /* helper for x */ |
401 | 0 | gcry_mpi_t value_qm2 = NULL; /* q - 2 */ |
402 | | |
403 | | /* Preset return values. */ |
404 | 0 | *r_counter = 0; |
405 | 0 | *r_seed = NULL; |
406 | 0 | *r_seedlen = 0; |
407 | 0 | *r_h = NULL; |
408 | | |
409 | | /* Derive QBITS from NBITS if requested */ |
410 | 0 | if (!qbits) |
411 | 0 | { |
412 | 0 | if (nbits == 1024) |
413 | 0 | qbits = 160; |
414 | 0 | else if (nbits == 2048) |
415 | 0 | qbits = 224; |
416 | 0 | else if (nbits == 3072) |
417 | 0 | qbits = 256; |
418 | 0 | } |
419 | | |
420 | | /* Check that QBITS and NBITS match the standard. Note that FIPS |
421 | | 186-3 uses N for QBITS and L for NBITS. */ |
422 | 0 | if (nbits == 1024 && qbits == 160 && use_fips186_2) |
423 | 0 | ; /* Allowed in FIPS 186-2 mode. */ |
424 | 0 | else if (nbits == 2048 && qbits == 224) |
425 | 0 | ; |
426 | 0 | else if (nbits == 2048 && qbits == 256) |
427 | 0 | ; |
428 | 0 | else if (nbits == 3072 && qbits == 256) |
429 | 0 | ; |
430 | 0 | else |
431 | 0 | return GPG_ERR_INV_VALUE; |
432 | | |
433 | 0 | ec = dsa_check_keysize (nbits); |
434 | 0 | if (ec) |
435 | 0 | return ec; |
436 | | |
437 | 0 | if (domain->p && domain->q && domain->g) |
438 | 0 | { |
439 | | /* Domain parameters are given; use them. */ |
440 | 0 | prime_p = mpi_copy (domain->p); |
441 | 0 | prime_q = mpi_copy (domain->q); |
442 | 0 | value_g = mpi_copy (domain->g); |
443 | 0 | gcry_assert (mpi_get_nbits (prime_p) == nbits); |
444 | 0 | gcry_assert (mpi_get_nbits (prime_q) == qbits); |
445 | 0 | gcry_assert (!deriveparms); |
446 | 0 | ec = 0; |
447 | 0 | } |
448 | 0 | else |
449 | 0 | { |
450 | | /* Generate new domain parameters. */ |
451 | | |
452 | | /* Get an initial seed value. */ |
453 | 0 | if (deriveparms) |
454 | 0 | { |
455 | 0 | initial_seed.sexp = sexp_find_token (deriveparms, "seed", 0); |
456 | 0 | if (initial_seed.sexp) |
457 | 0 | initial_seed.seed = sexp_nth_data (initial_seed.sexp, 1, |
458 | 0 | &initial_seed.seedlen); |
459 | 0 | } |
460 | |
|
461 | 0 | if (use_fips186_2) |
462 | 0 | ec = _gcry_generate_fips186_2_prime (nbits, qbits, |
463 | 0 | initial_seed.seed, |
464 | 0 | initial_seed.seedlen, |
465 | 0 | &prime_q, &prime_p, |
466 | 0 | r_counter, |
467 | 0 | r_seed, r_seedlen); |
468 | 0 | else |
469 | 0 | ec = _gcry_generate_fips186_3_prime (nbits, qbits, |
470 | 0 | initial_seed.seed, |
471 | 0 | initial_seed.seedlen, |
472 | 0 | &prime_q, &prime_p, |
473 | 0 | r_counter, |
474 | 0 | r_seed, r_seedlen, NULL); |
475 | 0 | sexp_release (initial_seed.sexp); |
476 | 0 | if (ec) |
477 | 0 | goto leave; |
478 | | |
479 | | /* Find a generator g (h and e are helpers). |
480 | | * e = (p-1)/q |
481 | | */ |
482 | 0 | value_e = mpi_alloc_like (prime_p); |
483 | 0 | mpi_sub_ui (value_e, prime_p, 1); |
484 | 0 | mpi_fdiv_q (value_e, value_e, prime_q ); |
485 | 0 | value_g = mpi_alloc_like (prime_p); |
486 | 0 | value_h = mpi_alloc_set_ui (1); |
487 | 0 | do |
488 | 0 | { |
489 | 0 | mpi_add_ui (value_h, value_h, 1); |
490 | | /* g = h^e mod p */ |
491 | 0 | mpi_powm (value_g, value_h, value_e, prime_p); |
492 | 0 | } |
493 | 0 | while (!mpi_cmp_ui (value_g, 1)); /* Continue until g != 1. */ |
494 | 0 | } |
495 | | |
496 | 0 | value_c = mpi_snew (qbits); |
497 | 0 | value_x = mpi_snew (qbits); |
498 | 0 | value_qm2 = mpi_snew (qbits); |
499 | 0 | mpi_sub_ui (value_qm2, prime_q, 2); |
500 | | |
501 | | /* FIPS 186-4 B.1.2 steps 4-6 */ |
502 | 0 | do |
503 | 0 | { |
504 | 0 | if( DBG_CIPHER ) |
505 | 0 | progress('.'); |
506 | 0 | _gcry_mpi_randomize (value_c, qbits, GCRY_VERY_STRONG_RANDOM); |
507 | 0 | mpi_clear_highbit (value_c, qbits+1); |
508 | 0 | } |
509 | 0 | while (!(mpi_cmp_ui (value_c, 0) > 0 && mpi_cmp (value_c, value_qm2) < 0)); |
510 | | /* while (mpi_cmp (value_c, value_qm2) > 0); */ |
511 | | |
512 | | /* x = c + 1 */ |
513 | 0 | mpi_add_ui(value_x, value_c, 1); |
514 | | |
515 | | /* y = g^x mod p */ |
516 | 0 | value_y = mpi_alloc_like (prime_p); |
517 | 0 | mpi_powm (value_y, value_g, value_x, prime_p); |
518 | |
|
519 | 0 | if (DBG_CIPHER) |
520 | 0 | { |
521 | 0 | progress('\n'); |
522 | 0 | log_mpidump("dsa p", prime_p ); |
523 | 0 | log_mpidump("dsa q", prime_q ); |
524 | 0 | log_mpidump("dsa g", value_g ); |
525 | 0 | log_mpidump("dsa y", value_y ); |
526 | 0 | log_mpidump("dsa x", value_x ); |
527 | 0 | log_mpidump("dsa h", value_h ); |
528 | 0 | } |
529 | | |
530 | | /* Copy the stuff to the key structures. */ |
531 | 0 | sk->p = prime_p; prime_p = NULL; |
532 | 0 | sk->q = prime_q; prime_q = NULL; |
533 | 0 | sk->g = value_g; value_g = NULL; |
534 | 0 | sk->y = value_y; value_y = NULL; |
535 | 0 | sk->x = value_x; value_x = NULL; |
536 | 0 | *r_h = value_h; value_h = NULL; |
537 | |
|
538 | 0 | leave: |
539 | 0 | _gcry_mpi_release (prime_p); |
540 | 0 | _gcry_mpi_release (prime_q); |
541 | 0 | _gcry_mpi_release (value_g); |
542 | 0 | _gcry_mpi_release (value_y); |
543 | 0 | _gcry_mpi_release (value_x); |
544 | 0 | _gcry_mpi_release (value_h); |
545 | 0 | _gcry_mpi_release (value_e); |
546 | 0 | _gcry_mpi_release (value_c); |
547 | 0 | _gcry_mpi_release (value_qm2); |
548 | | |
549 | | /* As a last step test this keys (this should never fail of course). */ |
550 | 0 | if (!ec && test_keys (sk, qbits) ) |
551 | 0 | { |
552 | 0 | _gcry_mpi_release (sk->p); sk->p = NULL; |
553 | 0 | _gcry_mpi_release (sk->q); sk->q = NULL; |
554 | 0 | _gcry_mpi_release (sk->g); sk->g = NULL; |
555 | 0 | _gcry_mpi_release (sk->y); sk->y = NULL; |
556 | 0 | _gcry_mpi_release (sk->x); sk->x = NULL; |
557 | 0 | fips_signal_error ("self-test after key generation failed"); |
558 | 0 | ec = GPG_ERR_SELFTEST_FAILED; |
559 | 0 | } |
560 | |
|
561 | 0 | if (ec) |
562 | 0 | { |
563 | 0 | *r_counter = 0; |
564 | 0 | xfree (*r_seed); *r_seed = NULL; |
565 | 0 | *r_seedlen = 0; |
566 | 0 | _gcry_mpi_release (*r_h); *r_h = NULL; |
567 | 0 | } |
568 | |
|
569 | 0 | return ec; |
570 | 0 | } |
571 | | |
572 | | |
573 | | |
574 | | /* |
575 | | Test whether the secret key is valid. |
576 | | Returns: if this is a valid key. |
577 | | */ |
578 | | static int |
579 | | check_secret_key( DSA_secret_key *sk ) |
580 | 0 | { |
581 | 0 | int rc; |
582 | 0 | gcry_mpi_t y = mpi_alloc( mpi_get_nlimbs(sk->y) ); |
583 | |
|
584 | 0 | mpi_powm( y, sk->g, sk->x, sk->p ); |
585 | 0 | rc = !mpi_cmp( y, sk->y ); |
586 | 0 | mpi_free( y ); |
587 | 0 | return rc; |
588 | 0 | } |
589 | | |
590 | | |
591 | | |
592 | | /* |
593 | | Make a DSA signature from INPUT and put it into r and s. |
594 | | |
595 | | INPUT may either be a plain MPI or an opaque MPI which is then |
596 | | internally converted to a plain MPI. FLAGS and HASHALGO may both |
597 | | be 0 for standard operation mode. |
598 | | |
599 | | The random value, K_SUPPLIED, may be supplied externally. If not, |
600 | | it is generated internally. |
601 | | |
602 | | The return value is 0 on success or an error code. Note that for |
603 | | backward compatibility the function will not return any error if |
604 | | FLAGS and HASHALGO are both 0 and INPUT is a plain MPI. |
605 | | */ |
606 | | static gpg_err_code_t |
607 | | sign (gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t input, gcry_mpi_t k_supplied, |
608 | | DSA_secret_key *skey, int flags, int hashalgo) |
609 | 0 | { |
610 | 0 | gpg_err_code_t rc; |
611 | 0 | gcry_mpi_t hash; |
612 | 0 | gcry_mpi_t k; |
613 | 0 | gcry_mpi_t kinv; |
614 | 0 | gcry_mpi_t tmp; |
615 | 0 | const void *abuf; |
616 | 0 | unsigned int abits, qbits; |
617 | 0 | int extraloops = 0; |
618 | 0 | gcry_mpi_t hash_computed_internally = NULL; |
619 | |
|
620 | 0 | qbits = mpi_get_nbits (skey->q); |
621 | |
|
622 | 0 | if ((flags & PUBKEY_FLAG_PREHASH)) |
623 | 0 | { |
624 | 0 | rc = _gcry_dsa_compute_hash (&hash_computed_internally, input, hashalgo); |
625 | 0 | if (rc) |
626 | 0 | return rc; |
627 | 0 | input = hash_computed_internally; |
628 | 0 | } |
629 | | |
630 | | /* Convert the INPUT into an MPI. */ |
631 | 0 | rc = _gcry_dsa_normalize_hash (input, &hash, qbits); |
632 | 0 | if (rc) |
633 | 0 | { |
634 | 0 | mpi_free (hash_computed_internally); |
635 | 0 | return rc; |
636 | 0 | } |
637 | | |
638 | 0 | again: |
639 | 0 | if (k_supplied) |
640 | 0 | k = k_supplied; |
641 | | /* Create the K value. */ |
642 | 0 | else if ((flags & PUBKEY_FLAG_RFC6979) && hashalgo) |
643 | 0 | { |
644 | | /* Use Pornin's method for deterministic DSA. If this flag is |
645 | | set, it is expected that HASH is an opaque MPI with the to be |
646 | | signed hash. That hash is also used as h1 from 3.2.a. */ |
647 | 0 | if (!mpi_is_opaque (input)) |
648 | 0 | { |
649 | 0 | rc = GPG_ERR_CONFLICT; |
650 | 0 | goto leave; |
651 | 0 | } |
652 | | |
653 | 0 | abuf = mpi_get_opaque (input, &abits); |
654 | 0 | rc = _gcry_dsa_gen_rfc6979_k (&k, skey->q, skey->x, |
655 | 0 | abuf, (abits+7)/8, hashalgo, extraloops); |
656 | 0 | if (rc) |
657 | 0 | goto leave; |
658 | 0 | } |
659 | 0 | else |
660 | 0 | { |
661 | | /* Select a random k with 0 < k < q */ |
662 | 0 | k = _gcry_dsa_gen_k (skey->q, GCRY_STRONG_RANDOM); |
663 | 0 | } |
664 | | |
665 | | /* kinv = k^(-1) mod q */ |
666 | 0 | kinv = mpi_alloc( mpi_get_nlimbs(k) ); |
667 | 0 | mpi_invm(kinv, k, skey->q ); |
668 | |
|
669 | 0 | _gcry_dsa_modify_k (k, skey->q, qbits); |
670 | | |
671 | | /* r = (a^k mod p) mod q */ |
672 | 0 | mpi_powm( r, skey->g, k, skey->p ); |
673 | 0 | mpi_fdiv_r( r, r, skey->q ); |
674 | | |
675 | | /* s = (kinv * ( hash + x * r)) mod q */ |
676 | 0 | tmp = mpi_alloc( mpi_get_nlimbs(skey->p) ); |
677 | 0 | mpi_mul( tmp, skey->x, r ); |
678 | 0 | mpi_add( tmp, tmp, hash ); |
679 | 0 | mpi_mulm( s , kinv, tmp, skey->q ); |
680 | |
|
681 | 0 | if (!k_supplied) |
682 | 0 | mpi_free(k); |
683 | 0 | mpi_free(kinv); |
684 | 0 | mpi_free(tmp); |
685 | |
|
686 | 0 | if (!mpi_cmp_ui (r, 0)) |
687 | 0 | { |
688 | 0 | if (k_supplied) |
689 | 0 | { |
690 | 0 | rc = GPG_ERR_INV_VALUE; |
691 | 0 | goto leave; |
692 | 0 | } |
693 | | |
694 | | /* This is a highly unlikely code path. */ |
695 | 0 | extraloops++; |
696 | 0 | goto again; |
697 | 0 | } |
698 | | |
699 | 0 | rc = 0; |
700 | |
|
701 | 0 | leave: |
702 | 0 | if (hash != input) |
703 | 0 | mpi_free (hash); |
704 | 0 | mpi_free (hash_computed_internally); |
705 | |
|
706 | 0 | return rc; |
707 | 0 | } |
708 | | |
709 | | |
710 | | /* |
711 | | Returns true if the signature composed from R and S is valid. |
712 | | */ |
713 | | static gpg_err_code_t |
714 | | verify (gcry_mpi_t r, gcry_mpi_t s, gcry_mpi_t input, DSA_public_key *pkey, |
715 | | int flags, int hashalgo) |
716 | 0 | { |
717 | 0 | gpg_err_code_t rc = 0; |
718 | 0 | gcry_mpi_t w, u1, u2, v; |
719 | 0 | gcry_mpi_t base[3]; |
720 | 0 | gcry_mpi_t ex[3]; |
721 | 0 | gcry_mpi_t hash; |
722 | 0 | unsigned int nbits; |
723 | 0 | gcry_mpi_t hash_computed_internally = NULL; |
724 | |
|
725 | 0 | if( !(mpi_cmp_ui( r, 0 ) > 0 && mpi_cmp( r, pkey->q ) < 0) ) |
726 | 0 | return GPG_ERR_BAD_SIGNATURE; /* Assertion 0 < r < n failed. */ |
727 | 0 | if( !(mpi_cmp_ui( s, 0 ) > 0 && mpi_cmp( s, pkey->q ) < 0) ) |
728 | 0 | return GPG_ERR_BAD_SIGNATURE; /* Assertion 0 < s < n failed. */ |
729 | | |
730 | 0 | nbits = mpi_get_nbits (pkey->q); |
731 | 0 | if ((flags & PUBKEY_FLAG_PREHASH)) |
732 | 0 | { |
733 | 0 | rc = _gcry_dsa_compute_hash (&hash_computed_internally, input, hashalgo); |
734 | 0 | if (rc) |
735 | 0 | return rc; |
736 | 0 | input = hash_computed_internally; |
737 | 0 | } |
738 | 0 | rc = _gcry_dsa_normalize_hash (input, &hash, nbits); |
739 | 0 | if (rc) |
740 | 0 | { |
741 | 0 | mpi_free (hash_computed_internally); |
742 | 0 | return rc; |
743 | 0 | } |
744 | | |
745 | 0 | w = mpi_alloc( mpi_get_nlimbs(pkey->q) ); |
746 | 0 | u1 = mpi_alloc( mpi_get_nlimbs(pkey->q) ); |
747 | 0 | u2 = mpi_alloc( mpi_get_nlimbs(pkey->q) ); |
748 | 0 | v = mpi_alloc( mpi_get_nlimbs(pkey->p) ); |
749 | | |
750 | | /* w = s^(-1) mod q */ |
751 | 0 | mpi_invm( w, s, pkey->q ); |
752 | | |
753 | | /* u1 = (hash * w) mod q */ |
754 | 0 | mpi_mulm( u1, hash, w, pkey->q ); |
755 | | |
756 | | /* u2 = r * w mod q */ |
757 | 0 | mpi_mulm( u2, r, w, pkey->q ); |
758 | | |
759 | | /* v = g^u1 * y^u2 mod p mod q */ |
760 | 0 | base[0] = pkey->g; ex[0] = u1; |
761 | 0 | base[1] = pkey->y; ex[1] = u2; |
762 | 0 | base[2] = NULL; ex[2] = NULL; |
763 | 0 | mpi_mulpowm( v, base, ex, pkey->p ); |
764 | 0 | mpi_fdiv_r( v, v, pkey->q ); |
765 | |
|
766 | 0 | if (mpi_cmp( v, r )) |
767 | 0 | { |
768 | 0 | if (DBG_CIPHER) |
769 | 0 | { |
770 | 0 | log_mpidump (" i", input); |
771 | 0 | log_mpidump (" h", hash); |
772 | 0 | log_mpidump (" v", v); |
773 | 0 | log_mpidump (" r", r); |
774 | 0 | log_mpidump (" s", s); |
775 | 0 | } |
776 | 0 | rc = GPG_ERR_BAD_SIGNATURE; |
777 | 0 | } |
778 | |
|
779 | 0 | mpi_free(w); |
780 | 0 | mpi_free(u1); |
781 | 0 | mpi_free(u2); |
782 | 0 | mpi_free(v); |
783 | 0 | if (hash != input) |
784 | 0 | mpi_free (hash); |
785 | 0 | mpi_free (hash_computed_internally); |
786 | |
|
787 | 0 | return rc; |
788 | 0 | } |
789 | | |
790 | | |
791 | | /********************************************* |
792 | | ************** interface ****************** |
793 | | *********************************************/ |
794 | | |
795 | | static gcry_err_code_t |
796 | | dsa_generate (const gcry_sexp_t genparms, gcry_sexp_t *r_skey) |
797 | 0 | { |
798 | 0 | gpg_err_code_t rc; |
799 | 0 | unsigned int nbits; |
800 | 0 | gcry_sexp_t domainsexp; |
801 | 0 | DSA_secret_key sk; |
802 | 0 | gcry_sexp_t l1; |
803 | 0 | unsigned int qbits = 0; |
804 | 0 | gcry_sexp_t deriveparms = NULL; |
805 | 0 | gcry_sexp_t seedinfo = NULL; |
806 | 0 | gcry_sexp_t misc_info = NULL; |
807 | 0 | int flags = 0; |
808 | 0 | dsa_domain_t domain; |
809 | 0 | gcry_mpi_t *factors = NULL; |
810 | |
|
811 | 0 | memset (&sk, 0, sizeof sk); |
812 | 0 | memset (&domain, 0, sizeof domain); |
813 | |
|
814 | 0 | rc = _gcry_pk_util_get_nbits (genparms, &nbits); |
815 | 0 | if (rc) |
816 | 0 | return rc; |
817 | | |
818 | | /* Parse the optional flags list. */ |
819 | 0 | l1 = sexp_find_token (genparms, "flags", 0); |
820 | 0 | if (l1) |
821 | 0 | { |
822 | 0 | rc = _gcry_pk_util_parse_flaglist (l1, &flags, NULL); |
823 | 0 | sexp_release (l1); |
824 | 0 | if (rc) |
825 | 0 | return rc;\ |
826 | 0 | } |
827 | | |
828 | | /* Parse the optional qbits element. */ |
829 | 0 | l1 = sexp_find_token (genparms, "qbits", 0); |
830 | 0 | if (l1) |
831 | 0 | { |
832 | 0 | char buf[50]; |
833 | 0 | const char *s; |
834 | 0 | size_t n; |
835 | |
|
836 | 0 | s = sexp_nth_data (l1, 1, &n); |
837 | 0 | if (!s || n >= DIM (buf) - 1 ) |
838 | 0 | { |
839 | 0 | sexp_release (l1); |
840 | 0 | return GPG_ERR_INV_OBJ; /* No value or value too large. */ |
841 | 0 | } |
842 | 0 | memcpy (buf, s, n); |
843 | 0 | buf[n] = 0; |
844 | 0 | qbits = (unsigned int)strtoul (buf, NULL, 0); |
845 | 0 | sexp_release (l1); |
846 | 0 | } |
847 | | |
848 | | /* Parse the optional transient-key flag. */ |
849 | 0 | if (!(flags & PUBKEY_FLAG_TRANSIENT_KEY)) |
850 | 0 | { |
851 | 0 | l1 = sexp_find_token (genparms, "transient-key", 0); |
852 | 0 | if (l1) |
853 | 0 | { |
854 | 0 | flags |= PUBKEY_FLAG_TRANSIENT_KEY; |
855 | 0 | sexp_release (l1); |
856 | 0 | } |
857 | 0 | } |
858 | | |
859 | | /* Get the optional derive parameters. */ |
860 | 0 | deriveparms = sexp_find_token (genparms, "derive-parms", 0); |
861 | | |
862 | | /* Parse the optional "use-fips186" flags. */ |
863 | 0 | if (!(flags & PUBKEY_FLAG_USE_FIPS186)) |
864 | 0 | { |
865 | 0 | l1 = sexp_find_token (genparms, "use-fips186", 0); |
866 | 0 | if (l1) |
867 | 0 | { |
868 | 0 | flags |= PUBKEY_FLAG_USE_FIPS186; |
869 | 0 | sexp_release (l1); |
870 | 0 | } |
871 | 0 | } |
872 | 0 | if (!(flags & PUBKEY_FLAG_USE_FIPS186_2)) |
873 | 0 | { |
874 | 0 | l1 = sexp_find_token (genparms, "use-fips186-2", 0); |
875 | 0 | if (l1) |
876 | 0 | { |
877 | 0 | flags |= PUBKEY_FLAG_USE_FIPS186_2; |
878 | 0 | sexp_release (l1); |
879 | 0 | } |
880 | 0 | } |
881 | | |
882 | | /* Check whether domain parameters are given. */ |
883 | 0 | domainsexp = sexp_find_token (genparms, "domain", 0); |
884 | 0 | if (domainsexp) |
885 | 0 | { |
886 | | /* DERIVEPARMS can't be used together with domain parameters. |
887 | | NBITS abnd QBITS may not be specified because there values |
888 | | are derived from the domain parameters. */ |
889 | 0 | if (deriveparms || qbits || nbits) |
890 | 0 | { |
891 | 0 | sexp_release (domainsexp); |
892 | 0 | sexp_release (deriveparms); |
893 | 0 | return GPG_ERR_INV_VALUE; |
894 | 0 | } |
895 | | |
896 | | /* Put all domain parameters into the domain object. */ |
897 | 0 | l1 = sexp_find_token (domainsexp, "p", 0); |
898 | 0 | domain.p = sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG); |
899 | 0 | sexp_release (l1); |
900 | 0 | l1 = sexp_find_token (domainsexp, "q", 0); |
901 | 0 | domain.q = sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG); |
902 | 0 | sexp_release (l1); |
903 | 0 | l1 = sexp_find_token (domainsexp, "g", 0); |
904 | 0 | domain.g = sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG); |
905 | 0 | sexp_release (l1); |
906 | 0 | sexp_release (domainsexp); |
907 | | |
908 | | /* Check that all domain parameters are available. */ |
909 | 0 | if (!domain.p || !domain.q || !domain.g) |
910 | 0 | { |
911 | 0 | _gcry_mpi_release (domain.p); |
912 | 0 | _gcry_mpi_release (domain.q); |
913 | 0 | _gcry_mpi_release (domain.g); |
914 | 0 | sexp_release (deriveparms); |
915 | 0 | return GPG_ERR_MISSING_VALUE; |
916 | 0 | } |
917 | | |
918 | | /* Get NBITS and QBITS from the domain parameters. */ |
919 | 0 | nbits = mpi_get_nbits (domain.p); |
920 | 0 | qbits = mpi_get_nbits (domain.q); |
921 | 0 | } |
922 | | |
923 | 0 | if (deriveparms |
924 | 0 | || (flags & PUBKEY_FLAG_USE_FIPS186) |
925 | 0 | || (flags & PUBKEY_FLAG_USE_FIPS186_2) |
926 | 0 | || fips_mode ()) |
927 | 0 | { |
928 | 0 | int counter; |
929 | 0 | void *seed; |
930 | 0 | size_t seedlen; |
931 | 0 | gcry_mpi_t h_value; |
932 | |
|
933 | 0 | rc = generate_fips186 (&sk, nbits, qbits, deriveparms, |
934 | 0 | !!(flags & PUBKEY_FLAG_USE_FIPS186_2), |
935 | 0 | &domain, |
936 | 0 | &counter, &seed, &seedlen, &h_value); |
937 | 0 | if (!rc && h_value) |
938 | 0 | { |
939 | | /* Format the seed-values unless domain parameters are used |
940 | | for which a H_VALUE of NULL is an indication. */ |
941 | 0 | rc = sexp_build (&seedinfo, NULL, |
942 | 0 | "(seed-values(counter %d)(seed %b)(h %m))", |
943 | 0 | counter, (int)seedlen, seed, h_value); |
944 | 0 | xfree (seed); |
945 | 0 | _gcry_mpi_release (h_value); |
946 | 0 | } |
947 | 0 | } |
948 | 0 | else |
949 | 0 | { |
950 | 0 | rc = generate (&sk, nbits, qbits, |
951 | 0 | !!(flags & PUBKEY_FLAG_TRANSIENT_KEY), |
952 | 0 | &domain, &factors); |
953 | 0 | } |
954 | |
|
955 | 0 | if (!rc) |
956 | 0 | { |
957 | | /* Put the factors into MISC_INFO. Note that the factors are |
958 | | not confidential thus we can store them in standard memory. */ |
959 | 0 | int nfactors, i, j; |
960 | 0 | char *p; |
961 | 0 | char *format = NULL; |
962 | 0 | void **arg_list = NULL; |
963 | |
|
964 | 0 | for (nfactors=0; factors && factors[nfactors]; nfactors++) |
965 | 0 | ; |
966 | | /* Allocate space for the format string: |
967 | | "(misc-key-info%S(pm1-factors%m))" |
968 | | with one "%m" for each factor and construct it. */ |
969 | 0 | format = xtrymalloc (50 + 2*nfactors); |
970 | 0 | if (!format) |
971 | 0 | rc = gpg_err_code_from_syserror (); |
972 | 0 | else |
973 | 0 | { |
974 | 0 | p = stpcpy (format, "(misc-key-info"); |
975 | 0 | if (seedinfo) |
976 | 0 | p = stpcpy (p, "%S"); |
977 | 0 | if (nfactors) |
978 | 0 | { |
979 | 0 | p = stpcpy (p, "(pm1-factors"); |
980 | 0 | for (i=0; i < nfactors; i++) |
981 | 0 | p = stpcpy (p, "%m"); |
982 | 0 | p = stpcpy (p, ")"); |
983 | 0 | } |
984 | 0 | p = stpcpy (p, ")"); |
985 | | |
986 | | /* Allocate space for the list of factors plus one for the |
987 | | seedinfo s-exp plus an extra NULL entry for safety and |
988 | | fill it with the factors. */ |
989 | 0 | arg_list = xtrycalloc (nfactors+1+1, sizeof *arg_list); |
990 | 0 | if (!arg_list) |
991 | 0 | rc = gpg_err_code_from_syserror (); |
992 | 0 | else |
993 | 0 | { |
994 | 0 | i = 0; |
995 | 0 | if (seedinfo) |
996 | 0 | arg_list[i++] = &seedinfo; |
997 | 0 | for (j=0; j < nfactors; j++) |
998 | 0 | arg_list[i++] = factors + j; |
999 | 0 | arg_list[i] = NULL; |
1000 | |
|
1001 | 0 | rc = sexp_build_array (&misc_info, NULL, format, arg_list); |
1002 | 0 | } |
1003 | 0 | } |
1004 | |
|
1005 | 0 | xfree (arg_list); |
1006 | 0 | xfree (format); |
1007 | 0 | } |
1008 | |
|
1009 | 0 | if (!rc) |
1010 | 0 | rc = sexp_build (r_skey, NULL, |
1011 | 0 | "(key-data" |
1012 | 0 | " (public-key" |
1013 | 0 | " (dsa(p%m)(q%m)(g%m)(y%m)))" |
1014 | 0 | " (private-key" |
1015 | 0 | " (dsa(p%m)(q%m)(g%m)(y%m)(x%m)))" |
1016 | 0 | " %S)", |
1017 | 0 | sk.p, sk.q, sk.g, sk.y, |
1018 | 0 | sk.p, sk.q, sk.g, sk.y, sk.x, |
1019 | 0 | misc_info); |
1020 | | |
1021 | |
|
1022 | 0 | _gcry_mpi_release (sk.p); |
1023 | 0 | _gcry_mpi_release (sk.q); |
1024 | 0 | _gcry_mpi_release (sk.g); |
1025 | 0 | _gcry_mpi_release (sk.y); |
1026 | 0 | _gcry_mpi_release (sk.x); |
1027 | |
|
1028 | 0 | _gcry_mpi_release (domain.p); |
1029 | 0 | _gcry_mpi_release (domain.q); |
1030 | 0 | _gcry_mpi_release (domain.g); |
1031 | |
|
1032 | 0 | sexp_release (seedinfo); |
1033 | 0 | sexp_release (misc_info); |
1034 | 0 | sexp_release (deriveparms); |
1035 | 0 | if (factors) |
1036 | 0 | { |
1037 | 0 | gcry_mpi_t *mp; |
1038 | 0 | for (mp = factors; *mp; mp++) |
1039 | 0 | mpi_free (*mp); |
1040 | 0 | xfree (factors); |
1041 | 0 | } |
1042 | 0 | return rc; |
1043 | 0 | } |
1044 | | |
1045 | | |
1046 | | |
1047 | | static gcry_err_code_t |
1048 | | dsa_check_secret_key (gcry_sexp_t keyparms) |
1049 | 0 | { |
1050 | 0 | gcry_err_code_t rc; |
1051 | 0 | DSA_secret_key sk = {NULL, NULL, NULL, NULL, NULL}; |
1052 | |
|
1053 | 0 | rc = _gcry_sexp_extract_param (keyparms, NULL, "pqgyx", |
1054 | 0 | &sk.p, &sk.q, &sk.g, &sk.y, &sk.x, |
1055 | 0 | NULL); |
1056 | 0 | if (rc) |
1057 | 0 | goto leave; |
1058 | | |
1059 | 0 | if (!check_secret_key (&sk)) |
1060 | 0 | rc = GPG_ERR_BAD_SECKEY; |
1061 | |
|
1062 | 0 | leave: |
1063 | 0 | _gcry_mpi_release (sk.p); |
1064 | 0 | _gcry_mpi_release (sk.q); |
1065 | 0 | _gcry_mpi_release (sk.g); |
1066 | 0 | _gcry_mpi_release (sk.y); |
1067 | 0 | _gcry_mpi_release (sk.x); |
1068 | 0 | if (DBG_CIPHER) |
1069 | 0 | log_debug ("dsa_testkey => %s\n", gpg_strerror (rc)); |
1070 | 0 | return rc; |
1071 | 0 | } |
1072 | | |
1073 | | |
1074 | | static gcry_err_code_t |
1075 | | dsa_sign (gcry_sexp_t *r_sig, gcry_sexp_t s_data, gcry_sexp_t keyparms) |
1076 | 0 | { |
1077 | 0 | gcry_err_code_t rc; |
1078 | 0 | struct pk_encoding_ctx ctx; |
1079 | 0 | gcry_mpi_t data = NULL; |
1080 | 0 | gcry_mpi_t k = NULL; |
1081 | 0 | DSA_secret_key sk = {NULL, NULL, NULL, NULL, NULL}; |
1082 | 0 | gcry_mpi_t sig_r = NULL; |
1083 | 0 | gcry_mpi_t sig_s = NULL; |
1084 | 0 | unsigned int nbits = dsa_get_nbits (keyparms); |
1085 | |
|
1086 | 0 | rc = dsa_check_keysize (nbits); |
1087 | 0 | if (rc) |
1088 | 0 | return rc; |
1089 | | |
1090 | 0 | _gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_SIGN, nbits); |
1091 | | |
1092 | | /* Extract the data. */ |
1093 | 0 | rc = _gcry_pk_util_data_to_mpi (s_data, &data, &ctx); |
1094 | 0 | if (rc) |
1095 | 0 | goto leave; |
1096 | 0 | if (DBG_CIPHER) |
1097 | 0 | log_mpidump ("dsa_sign data", data); |
1098 | |
|
1099 | 0 | if (ctx.label) |
1100 | 0 | rc = _gcry_mpi_scan (&k, GCRYMPI_FMT_USG, ctx.label, ctx.labellen, NULL); |
1101 | 0 | if (rc) |
1102 | 0 | goto leave; |
1103 | | |
1104 | | /* Extract the key. */ |
1105 | 0 | rc = _gcry_sexp_extract_param (keyparms, NULL, "pqgyx", |
1106 | 0 | &sk.p, &sk.q, &sk.g, &sk.y, &sk.x, NULL); |
1107 | 0 | if (rc) |
1108 | 0 | goto leave; |
1109 | 0 | if (DBG_CIPHER) |
1110 | 0 | { |
1111 | 0 | log_mpidump ("dsa_sign p", sk.p); |
1112 | 0 | log_mpidump ("dsa_sign q", sk.q); |
1113 | 0 | log_mpidump ("dsa_sign g", sk.g); |
1114 | 0 | log_mpidump ("dsa_sign y", sk.y); |
1115 | 0 | if (!fips_mode ()) |
1116 | 0 | log_mpidump ("dsa_sign x", sk.x); |
1117 | 0 | } |
1118 | |
|
1119 | 0 | sig_r = mpi_new (0); |
1120 | 0 | sig_s = mpi_new (0); |
1121 | 0 | rc = sign (sig_r, sig_s, data, k, &sk, ctx.flags, ctx.hash_algo); |
1122 | 0 | if (rc) |
1123 | 0 | goto leave; |
1124 | 0 | if (DBG_CIPHER) |
1125 | 0 | { |
1126 | 0 | log_mpidump ("dsa_sign sig_r", sig_r); |
1127 | 0 | log_mpidump ("dsa_sign sig_s", sig_s); |
1128 | 0 | } |
1129 | 0 | rc = sexp_build (r_sig, NULL, "(sig-val(dsa(r%M)(s%M)))", sig_r, sig_s); |
1130 | |
|
1131 | 0 | leave: |
1132 | 0 | _gcry_mpi_release (sig_r); |
1133 | 0 | _gcry_mpi_release (sig_s); |
1134 | 0 | _gcry_mpi_release (sk.p); |
1135 | 0 | _gcry_mpi_release (sk.q); |
1136 | 0 | _gcry_mpi_release (sk.g); |
1137 | 0 | _gcry_mpi_release (sk.y); |
1138 | 0 | _gcry_mpi_release (sk.x); |
1139 | 0 | _gcry_mpi_release (data); |
1140 | 0 | _gcry_mpi_release (k); |
1141 | 0 | _gcry_pk_util_free_encoding_ctx (&ctx); |
1142 | 0 | if (DBG_CIPHER) |
1143 | 0 | log_debug ("dsa_sign => %s\n", gpg_strerror (rc)); |
1144 | 0 | return rc; |
1145 | 0 | } |
1146 | | |
1147 | | |
1148 | | static gcry_err_code_t |
1149 | | dsa_verify (gcry_sexp_t s_sig, gcry_sexp_t s_data, gcry_sexp_t s_keyparms) |
1150 | 0 | { |
1151 | 0 | gcry_err_code_t rc; |
1152 | 0 | struct pk_encoding_ctx ctx; |
1153 | 0 | gcry_sexp_t l1 = NULL; |
1154 | 0 | gcry_mpi_t sig_r = NULL; |
1155 | 0 | gcry_mpi_t sig_s = NULL; |
1156 | 0 | gcry_mpi_t data = NULL; |
1157 | 0 | DSA_public_key pk = { NULL, NULL, NULL, NULL }; |
1158 | 0 | unsigned int nbits = dsa_get_nbits (s_keyparms); |
1159 | |
|
1160 | 0 | rc = dsa_check_keysize (nbits); |
1161 | 0 | if (rc) |
1162 | 0 | return rc; |
1163 | | |
1164 | 0 | _gcry_pk_util_init_encoding_ctx (&ctx, PUBKEY_OP_VERIFY, nbits); |
1165 | | |
1166 | | /* Extract the data. */ |
1167 | 0 | rc = _gcry_pk_util_data_to_mpi (s_data, &data, &ctx); |
1168 | 0 | if (rc) |
1169 | 0 | goto leave; |
1170 | 0 | if (DBG_CIPHER) |
1171 | 0 | log_mpidump ("dsa_verify data", data); |
1172 | | |
1173 | | /* Extract the signature value. */ |
1174 | 0 | rc = _gcry_pk_util_preparse_sigval (s_sig, dsa_names, &l1, NULL); |
1175 | 0 | if (rc) |
1176 | 0 | goto leave; |
1177 | 0 | rc = _gcry_sexp_extract_param (l1, NULL, "rs", &sig_r, &sig_s, NULL); |
1178 | 0 | if (rc) |
1179 | 0 | goto leave; |
1180 | 0 | if (DBG_CIPHER) |
1181 | 0 | { |
1182 | 0 | log_mpidump ("dsa_verify s_r", sig_r); |
1183 | 0 | log_mpidump ("dsa_verify s_s", sig_s); |
1184 | 0 | } |
1185 | | |
1186 | | /* Extract the key. */ |
1187 | 0 | rc = _gcry_sexp_extract_param (s_keyparms, NULL, "pqgy", |
1188 | 0 | &pk.p, &pk.q, &pk.g, &pk.y, NULL); |
1189 | 0 | if (rc) |
1190 | 0 | goto leave; |
1191 | 0 | if (DBG_CIPHER) |
1192 | 0 | { |
1193 | 0 | log_mpidump ("dsa_verify p", pk.p); |
1194 | 0 | log_mpidump ("dsa_verify q", pk.q); |
1195 | 0 | log_mpidump ("dsa_verify g", pk.g); |
1196 | 0 | log_mpidump ("dsa_verify y", pk.y); |
1197 | 0 | } |
1198 | | |
1199 | | /* Verify the signature. */ |
1200 | 0 | rc = verify (sig_r, sig_s, data, &pk, ctx.flags, ctx.hash_algo); |
1201 | |
|
1202 | 0 | leave: |
1203 | 0 | _gcry_mpi_release (pk.p); |
1204 | 0 | _gcry_mpi_release (pk.q); |
1205 | 0 | _gcry_mpi_release (pk.g); |
1206 | 0 | _gcry_mpi_release (pk.y); |
1207 | 0 | _gcry_mpi_release (data); |
1208 | 0 | _gcry_mpi_release (sig_r); |
1209 | 0 | _gcry_mpi_release (sig_s); |
1210 | 0 | sexp_release (l1); |
1211 | 0 | _gcry_pk_util_free_encoding_ctx (&ctx); |
1212 | 0 | if (DBG_CIPHER) |
1213 | 0 | log_debug ("dsa_verify => %s\n", rc?gpg_strerror (rc):"Good"); |
1214 | 0 | return rc; |
1215 | 0 | } |
1216 | | |
1217 | | |
1218 | | /* Return the number of bits for the key described by PARMS. On error |
1219 | | * 0 is returned. The format of PARMS starts with the algorithm name; |
1220 | | * for example: |
1221 | | * |
1222 | | * (dsa |
1223 | | * (p <mpi>) |
1224 | | * (q <mpi>) |
1225 | | * (g <mpi>) |
1226 | | * (y <mpi>)) |
1227 | | * |
1228 | | * More parameters may be given but we only need P here. |
1229 | | */ |
1230 | | static unsigned int |
1231 | | dsa_get_nbits (gcry_sexp_t parms) |
1232 | 0 | { |
1233 | 0 | gcry_sexp_t l1; |
1234 | 0 | gcry_mpi_t p; |
1235 | 0 | unsigned int nbits; |
1236 | |
|
1237 | 0 | l1 = sexp_find_token (parms, "p", 1); |
1238 | 0 | if (!l1) |
1239 | 0 | return 0; /* Parameter P not found. */ |
1240 | | |
1241 | 0 | p = sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG); |
1242 | 0 | sexp_release (l1); |
1243 | 0 | nbits = p? mpi_get_nbits (p) : 0; |
1244 | 0 | _gcry_mpi_release (p); |
1245 | 0 | return nbits; |
1246 | 0 | } |
1247 | | |
1248 | | |
1249 | | |
1250 | | /* |
1251 | | Self-test section. |
1252 | | */ |
1253 | | |
1254 | | static const char * |
1255 | | selftest_sign (gcry_sexp_t pkey, gcry_sexp_t skey) |
1256 | 0 | { |
1257 | | /* Sample data from RFC 6979 section A.2.2, hash is of message "sample" */ |
1258 | 0 | static const char sample_data[] = |
1259 | 0 | "(data (flags rfc6979 prehash)" |
1260 | 0 | " (hash-algo sha256)" |
1261 | 0 | " (value 6:sample))"; |
1262 | 0 | static const char sample_data_bad[] = |
1263 | 0 | "(data (flags rfc6979)" |
1264 | 0 | " (hash sha256 #bf2bdbe1aa9b6ec1e2ade1d694f41fc71a831d0268e9891562113d8a62add1bf#))"; |
1265 | 0 | static const char signature_r[] = |
1266 | 0 | "eace8bdbbe353c432a795d9ec556c6d021f7a03f42c36e9bc87e4ac7932cc809"; |
1267 | 0 | static const char signature_s[] = |
1268 | 0 | "7081e175455f9247b812b74583e9e94f9ea79bd640dc962533b0680793a38d53"; |
1269 | |
|
1270 | 0 | const char *errtxt = NULL; |
1271 | 0 | gcry_error_t err; |
1272 | 0 | gcry_sexp_t data = NULL; |
1273 | 0 | gcry_sexp_t data_bad = NULL; |
1274 | 0 | gcry_sexp_t sig = NULL; |
1275 | 0 | gcry_sexp_t l1 = NULL; |
1276 | 0 | gcry_sexp_t l2 = NULL; |
1277 | 0 | gcry_mpi_t r = NULL; |
1278 | 0 | gcry_mpi_t s = NULL; |
1279 | 0 | gcry_mpi_t calculated_r = NULL; |
1280 | 0 | gcry_mpi_t calculated_s = NULL; |
1281 | 0 | int cmp; |
1282 | |
|
1283 | 0 | err = sexp_sscan (&data, NULL, sample_data, strlen (sample_data)); |
1284 | 0 | if (!err) |
1285 | 0 | err = sexp_sscan (&data_bad, NULL, |
1286 | 0 | sample_data_bad, strlen (sample_data_bad)); |
1287 | 0 | if (!err) |
1288 | 0 | err = _gcry_mpi_scan (&r, GCRYMPI_FMT_HEX, signature_r, 0, NULL); |
1289 | 0 | if (!err) |
1290 | 0 | err = _gcry_mpi_scan (&s, GCRYMPI_FMT_HEX, signature_s, 0, NULL); |
1291 | |
|
1292 | 0 | if (err) |
1293 | 0 | { |
1294 | 0 | errtxt = "converting data failed"; |
1295 | 0 | goto leave; |
1296 | 0 | } |
1297 | | |
1298 | 0 | err = _gcry_pk_sign (&sig, data, skey); |
1299 | 0 | if (err) |
1300 | 0 | { |
1301 | 0 | errtxt = "signing failed"; |
1302 | 0 | goto leave; |
1303 | 0 | } |
1304 | | |
1305 | | /* check against known signature */ |
1306 | 0 | errtxt = "signature validity failed"; |
1307 | 0 | l1 = _gcry_sexp_find_token (sig, "sig-val", 0); |
1308 | 0 | if (!l1) |
1309 | 0 | goto leave; |
1310 | 0 | l2 = _gcry_sexp_find_token (l1, "dsa", 0); |
1311 | 0 | if (!l2) |
1312 | 0 | goto leave; |
1313 | | |
1314 | 0 | sexp_release (l1); |
1315 | 0 | l1 = l2; |
1316 | |
|
1317 | 0 | l2 = _gcry_sexp_find_token (l1, "r", 0); |
1318 | 0 | if (!l2) |
1319 | 0 | goto leave; |
1320 | 0 | calculated_r = _gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG); |
1321 | 0 | if (!calculated_r) |
1322 | 0 | goto leave; |
1323 | | |
1324 | 0 | sexp_release (l2); |
1325 | 0 | l2 = _gcry_sexp_find_token (l1, "s", 0); |
1326 | 0 | if (!l2) |
1327 | 0 | goto leave; |
1328 | 0 | calculated_s = _gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG); |
1329 | 0 | if (!calculated_s) |
1330 | 0 | goto leave; |
1331 | | |
1332 | 0 | errtxt = "known sig check failed"; |
1333 | |
|
1334 | 0 | cmp = _gcry_mpi_cmp (r, calculated_r); |
1335 | 0 | if (cmp) |
1336 | 0 | goto leave; |
1337 | 0 | cmp = _gcry_mpi_cmp (s, calculated_s); |
1338 | 0 | if (cmp) |
1339 | 0 | goto leave; |
1340 | | |
1341 | 0 | errtxt = NULL; |
1342 | | |
1343 | |
|
1344 | 0 | err = _gcry_pk_verify (sig, data, pkey); |
1345 | 0 | if (err) |
1346 | 0 | { |
1347 | 0 | errtxt = "verify failed"; |
1348 | 0 | goto leave; |
1349 | 0 | } |
1350 | 0 | err = _gcry_pk_verify (sig, data_bad, pkey); |
1351 | 0 | if (gcry_err_code (err) != GPG_ERR_BAD_SIGNATURE) |
1352 | 0 | { |
1353 | 0 | errtxt = "bad signature not detected"; |
1354 | 0 | goto leave; |
1355 | 0 | } |
1356 | | |
1357 | | |
1358 | 0 | leave: |
1359 | 0 | _gcry_mpi_release (calculated_s); |
1360 | 0 | _gcry_mpi_release (calculated_r); |
1361 | 0 | _gcry_mpi_release (s); |
1362 | 0 | _gcry_mpi_release (r); |
1363 | 0 | sexp_release (l2); |
1364 | 0 | sexp_release (l1); |
1365 | 0 | sexp_release (sig); |
1366 | 0 | sexp_release (data_bad); |
1367 | 0 | sexp_release (data); |
1368 | 0 | return errtxt; |
1369 | 0 | } |
1370 | | |
1371 | | |
1372 | | static gpg_err_code_t |
1373 | | selftests_dsa_2048 (selftest_report_func_t report) |
1374 | 0 | { |
1375 | 0 | const char *what; |
1376 | 0 | const char *errtxt; |
1377 | 0 | gcry_error_t err; |
1378 | 0 | gcry_sexp_t skey = NULL; |
1379 | 0 | gcry_sexp_t pkey = NULL; |
1380 | | |
1381 | | /* Convert the S-expressions into the internal representation. */ |
1382 | 0 | what = "convert"; |
1383 | 0 | err = sexp_sscan (&skey, NULL, sample_secret_key_2048, strlen (sample_secret_key_2048)); |
1384 | 0 | if (!err) |
1385 | 0 | err = sexp_sscan (&pkey, NULL, |
1386 | 0 | sample_public_key_2048, strlen (sample_public_key_2048)); |
1387 | 0 | if (err) |
1388 | 0 | { |
1389 | 0 | errtxt = _gcry_strerror (err); |
1390 | 0 | goto failed; |
1391 | 0 | } |
1392 | | |
1393 | 0 | what = "key consistency"; |
1394 | 0 | err = _gcry_pk_testkey (skey); |
1395 | 0 | if (err) |
1396 | 0 | { |
1397 | 0 | errtxt = _gcry_strerror (err); |
1398 | 0 | goto failed; |
1399 | 0 | } |
1400 | | |
1401 | 0 | what = "sign"; |
1402 | 0 | errtxt = selftest_sign (pkey, skey); |
1403 | 0 | if (errtxt) |
1404 | 0 | goto failed; |
1405 | | |
1406 | 0 | sexp_release (pkey); |
1407 | 0 | sexp_release (skey); |
1408 | 0 | return 0; /* Succeeded. */ |
1409 | | |
1410 | 0 | failed: |
1411 | 0 | sexp_release (pkey); |
1412 | 0 | sexp_release (skey); |
1413 | 0 | if (report) |
1414 | 0 | report ("pubkey", GCRY_PK_DSA, what, errtxt); |
1415 | 0 | return GPG_ERR_SELFTEST_FAILED; |
1416 | 0 | } |
1417 | | |
1418 | | |
1419 | | /* Run a full self-test for ALGO and return 0 on success. */ |
1420 | | static gpg_err_code_t |
1421 | | run_selftests (int algo, int extended, selftest_report_func_t report) |
1422 | 0 | { |
1423 | 0 | gpg_err_code_t ec; |
1424 | |
|
1425 | 0 | (void)extended; |
1426 | |
|
1427 | 0 | switch (algo) |
1428 | 0 | { |
1429 | 0 | case GCRY_PK_DSA: |
1430 | 0 | ec = selftests_dsa_2048 (report); |
1431 | 0 | break; |
1432 | 0 | default: |
1433 | 0 | ec = GPG_ERR_PUBKEY_ALGO; |
1434 | 0 | break; |
1435 | |
|
1436 | 0 | } |
1437 | 0 | return ec; |
1438 | 0 | } |
1439 | | |
1440 | | |
1441 | | |
1442 | | gcry_pk_spec_t _gcry_pubkey_spec_dsa = |
1443 | | { |
1444 | | GCRY_PK_DSA, { 0, 0 }, |
1445 | | GCRY_PK_USAGE_SIGN, |
1446 | | "DSA", dsa_names, |
1447 | | "pqgy", "pqgyx", "", "rs", "pqgy", |
1448 | | dsa_generate, |
1449 | | dsa_check_secret_key, |
1450 | | NULL, |
1451 | | NULL, |
1452 | | dsa_sign, |
1453 | | dsa_verify, |
1454 | | dsa_get_nbits, |
1455 | | run_selftests |
1456 | | }; |