Line data Source code
1 : /* fips186-dsa.c - FIPS 186 DSA tests
2 : * Copyright (C) 2008 Free Software Foundation, Inc.
3 : *
4 : * This file is part of Libgcrypt.
5 : *
6 : * Libgcrypt is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU Lesser General Public License as
8 : * published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * Libgcrypt is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #ifdef HAVE_CONFIG_H
21 : # include <config.h>
22 : #endif
23 : #include <stdio.h>
24 : #include <stdlib.h>
25 : #include <string.h>
26 : #include <stdarg.h>
27 :
28 : #ifdef _GCRYPT_IN_LIBGCRYPT
29 : # include "../src/gcrypt-int.h"
30 : #else
31 : # include <gcrypt.h>
32 : #endif
33 :
34 : #define PGM "fips186-dsa"
35 : #include "t-common.h"
36 :
37 :
38 : static void
39 0 : show_sexp (const char *prefix, gcry_sexp_t a)
40 : {
41 : char *buf;
42 : size_t size;
43 :
44 0 : if (prefix)
45 0 : fputs (prefix, stderr);
46 0 : size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
47 0 : buf = gcry_xmalloc (size);
48 :
49 0 : gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
50 0 : fprintf (stderr, "%.*s", (int)size, buf);
51 0 : gcry_free (buf);
52 0 : }
53 :
54 : static gcry_mpi_t
55 40 : mpi_from_string (const char *string)
56 : {
57 : gpg_error_t err;
58 : gcry_mpi_t a;
59 :
60 40 : err = gcry_mpi_scan (&a, GCRYMPI_FMT_HEX, string, 0, NULL);
61 40 : if (err)
62 0 : die ("error converting string to mpi: %s\n", gpg_strerror (err));
63 40 : return a;
64 : }
65 :
66 : /* Convert STRING consisting of hex characters into its binary
67 : representation and return it as an allocated buffer. The valid
68 : length of the buffer is returned at R_LENGTH. The string is
69 : delimited by end of string. The function returns NULL on
70 : error. */
71 : static void *
72 20 : data_from_hex (const char *string, size_t *r_length)
73 : {
74 : const char *s;
75 : unsigned char *buffer;
76 : size_t length;
77 :
78 20 : buffer = gcry_xmalloc (strlen(string)/2+1);
79 20 : length = 0;
80 420 : for (s=string; *s; s +=2 )
81 : {
82 400 : if (!hexdigitp (s) || !hexdigitp (s+1))
83 0 : die ("error parsing hex string `%s'\n", string);
84 400 : ((unsigned char*)buffer)[length++] = xtoi_2 (s);
85 : }
86 20 : *r_length = length;
87 20 : return buffer;
88 : }
89 :
90 :
91 : static void
92 40 : extract_cmp_mpi (gcry_sexp_t sexp, const char *name, const char *expected)
93 : {
94 : gcry_sexp_t l1;
95 : gcry_mpi_t a, b;
96 :
97 40 : l1 = gcry_sexp_find_token (sexp, name, 0);
98 40 : a = gcry_sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG);
99 40 : b = mpi_from_string (expected);
100 40 : if (!a)
101 0 : fail ("parameter \"%s\" missing in key\n", name);
102 40 : else if ( gcry_mpi_cmp (a, b) )
103 0 : fail ("parameter \"%s\" does not match expected value\n", name);
104 40 : gcry_mpi_release (b);
105 40 : gcry_mpi_release (a);
106 40 : gcry_sexp_release (l1);
107 40 : }
108 :
109 :
110 : static void
111 10 : extract_cmp_data (gcry_sexp_t sexp, const char *name, const char *expected)
112 : {
113 : gcry_sexp_t l1;
114 : const void *a;
115 : size_t alen;
116 : void *b;
117 : size_t blen;
118 :
119 10 : l1 = gcry_sexp_find_token (sexp, name, 0);
120 10 : a = gcry_sexp_nth_data (l1, 1, &alen);
121 10 : b = data_from_hex (expected, &blen);
122 10 : if (!a)
123 0 : fail ("parameter \"%s\" missing in key\n", name);
124 10 : else if ( alen != blen || memcmp (a, b, alen) )
125 0 : fail ("parameter \"%s\" does not match expected value\n", name);
126 10 : gcry_free (b);
127 10 : gcry_sexp_release (l1);
128 10 : }
129 :
130 : static void
131 10 : extract_cmp_int (gcry_sexp_t sexp, const char *name, int expected)
132 : {
133 : gcry_sexp_t l1;
134 : char *a;
135 :
136 10 : l1 = gcry_sexp_find_token (sexp, name, 0);
137 10 : a = gcry_sexp_nth_string (l1, 1);
138 10 : if (!a)
139 0 : fail ("parameter \"%s\" missing in key\n", name);
140 10 : else if ( strtoul (a, NULL, 10) != expected )
141 0 : fail ("parameter \"%s\" does not match expected value\n", name);
142 10 : gcry_free (a);
143 10 : gcry_sexp_release (l1);
144 10 : }
145 :
146 :
147 : static void
148 1 : check_dsa_gen_186_2 (void)
149 : {
150 : static struct {
151 : int nbits;
152 : const char *p, *q, *g;
153 : const char *seed;
154 : int counter;
155 : const char *h;
156 : } tbl[] = {
157 : /* These tests are from FIPS 186-2, B.3.1. */
158 : {
159 : 1024,
160 : "d3aed1876054db831d0c1348fbb1ada72507e5fbf9a62cbd47a63aeb7859d6921"
161 : "4adeb9146a6ec3f43520f0fd8e3125dd8bbc5d87405d1ac5f82073cd762a3f8d7"
162 : "74322657c9da88a7d2f0e1a9ceb84a39cb40876179e6a76e400498de4bb9379b0"
163 : "5f5feb7b91eb8fea97ee17a955a0a8a37587a272c4719d6feb6b54ba4ab69",
164 : "9c916d121de9a03f71fb21bc2e1c0d116f065a4f",
165 : "8157c5f68ca40b3ded11c353327ab9b8af3e186dd2e8dade98761a0996dda99ab"
166 : "0250d3409063ad99efae48b10c6ab2bba3ea9a67b12b911a372a2bba260176fad"
167 : "b4b93247d9712aad13aa70216c55da9858f7a298deb670a403eb1e7c91b847f1e"
168 : "ccfbd14bd806fd42cf45dbb69cd6d6b43add2a78f7d16928eaa04458dea44",
169 : "0cb1990c1fd3626055d7a0096f8fa99807399871",
170 : 98,
171 : "00000000000000000000000000000000000000000000000000000000000000000"
172 : "00000000000000000000000000000000000000000000000000000000000000000"
173 : "00000000000000000000000000000000000000000000000000000000000000000"
174 : "0000000000000000000000000000000000000000000000000000000000002"
175 : },
176 : {
177 : 1024,
178 : "f5c73304080353357de1b5967597c27d65f70aa2fe9b6aed1d0afc2b499adf22f"
179 : "8e37937096d88548ac36c4a067f8353c7fed73f96f0d688b19b0624aedbae5dbb"
180 : "0ee8835a4c269288c0e1d69479e701ee266bb767af39d748fe7d6afc73fdf44be"
181 : "3eb6e661e599670061203e75fc8b3dbd59e40b54f358d0097013a0f3867f9",
182 : "f8751166cf4f6f3b07c081fd2a9071f23ca1988d",
183 : "1e288a442e02461c418ed67a66d24cacbeb8936fbde62ff995f5fd569dee6be62"
184 : "4e4f0f9f8c8093f5d192ab3b3f9ae3f2665d95d27fb10e382f45cd356e7f4eb7a"
185 : "665db432113ed06478f93b7cf188ec7a1ee97aec8f91ea7bfceaf8b6e7e5a349c"
186 : "4ad3225362ef440c57cbc6e69df15b6699caac85f733555075f04781b2b33",
187 : "34b3520d45d240a8861b82c8b61ffa16e67b5cce",
188 : 622,
189 : "00000000000000000000000000000000000000000000000000000000000000000"
190 : "00000000000000000000000000000000000000000000000000000000000000000"
191 : "00000000000000000000000000000000000000000000000000000000000000000"
192 : "0000000000000000000000000000000000000000000000000000000000002",
193 : },
194 : {
195 : 1024,
196 : "c6c6f4f4eed927fb1c3b0c81010967e530658e6f9698ebe058b4f47b2dc8fcbc7"
197 : "b69296b9e8b6cf55681181fe72492668061b262b0046a0d409902e269b0cb69a4"
198 : "55ed1a086caf41927f5912bf0e0cbc45ee81a4f98bf6146f6168a228aec80e9cc"
199 : "1162d6f6aa412efe82d4f18b95e34ab790daac5bd7aef0b22fa08ba5dbaad",
200 : "d32b29f065c1394a30490b6fcbf812a32a8634ab",
201 : "06f973c879e2e89345d0ac04f9c34ad69b9eff1680f18d1c8f3e1596c2e8fa8e1"
202 : "ecef6830409e9012d4788bef6ec7414d09c981b47c941b77f39dfc49caff5e714"
203 : "c97abe25a7a8b5d1fe88700bb96eff91cca64d53700a28b1146d81bad1212d231"
204 : "80154c95a01f5aeebb553a8365c38a5ebe05539b51734233776ce9aff98b2",
205 : "b6ec750da2f824cb42c5f7e28c81350d97f75125",
206 : 185,
207 : "00000000000000000000000000000000000000000000000000000000000000000"
208 : "00000000000000000000000000000000000000000000000000000000000000000"
209 : "00000000000000000000000000000000000000000000000000000000000000000"
210 : "0000000000000000000000000000000000000000000000000000000000002",
211 : },
212 : {
213 : 1024,
214 : "b827a9dc9221a6ed1bec7b64d61232aacb2812f888b0a0b3a95033d7a22e77d0b"
215 : "ff23bfeed0fb1281b21b8ff7421f0c727d1fb8aa2b843d6885f067e763f83d41f"
216 : "d800ab15a7e2b12f71ec2058ee7bd62cd72c26989b272e519785da57bfa1f974b"
217 : "c652e1a2d6cfb68477de5635fd019b37add656cff0b802558b31b6d2851e5",
218 : "de822c03445b77cec4ad3a6fb0ca39ff97059ddf",
219 : "65a9e2d43a378d7063813104586868cacf2fccd51aec1e0b6af8ba3e66dee6371"
220 : "681254c3fb5e3929d65e3c4bcd20abd4ddc7cf815623e17b9fc92f02b8d44278b"
221 : "848480ffd193104cf5612639511e45bd247708ff6028bd3824f8844c263b46c69"
222 : "1f2076f8cd13c5d0be95f1f2a1a17ab1f7e5bc73500bac27d57b473ba9748",
223 : "cd2221dd73815a75224e9fde7faf52829b81ac7a",
224 : 62,
225 : "00000000000000000000000000000000000000000000000000000000000000000"
226 : "00000000000000000000000000000000000000000000000000000000000000000"
227 : "00000000000000000000000000000000000000000000000000000000000000000"
228 : "0000000000000000000000000000000000000000000000000000000000002",
229 : },
230 : {
231 : 1024,
232 : "898a8d93e295c8ef2ffd46976225a1543640640d155a576fafa0be32136165803"
233 : "ba2eff2782a2be75cc9ec65db6bd3238cca695b3a5a14726a2a314775c377d891"
234 : "354b3de6c89e714a05599ca04132c987f889f72c4fe298ccb31f711c03b07e1d9"
235 : "8d72af590754cf3847398b60cecd55a4611692b308809560a83880404c227",
236 : "c6d786643d2acfc6b8d576863fda8cfbfbd5e03f",
237 : "2fd38b8d21c58e8fb5315a177b8d5dc4c450d574e69348b7b9da367c26e72438d"
238 : "af8372e7f0bee84ef5dcbbc3727194a2228431192f1779be24837f22a0e14d10d"
239 : "5344da1b8b403df9f9b2655095b3d0f67418ed6cd989f35aa4232e4b7001764fb"
240 : "e85d6b2c716980f13272fc4271ac1e234f7e24c023cfc2d2dc0aa1e9af2fb",
241 : "73483e697599871af983a281e3afa22e0ed86b68",
242 : 272,
243 : "00000000000000000000000000000000000000000000000000000000000000000"
244 : "00000000000000000000000000000000000000000000000000000000000000000"
245 : "00000000000000000000000000000000000000000000000000000000000000000"
246 : "0000000000000000000000000000000000000000000000000000000000002",
247 : },
248 :
249 : /* These tests are generated by the OpenSSL FIPS version. */
250 : {
251 : 1024,
252 : "A404363903FDCE86839BCFD953AAD2DA2B0E70CAED3B5FF5D68F15A1C4BB0A793C"
253 : "A9D58FC956804C5901DE0AF99F345ED1A8617C687864BAC044B7C3C3E732A2B255"
254 : "EC986AA76EA8CB0E0815B3E0E605650AF7D8058EE7E8EBCDEFFDAB8100D3FC1033"
255 : "11BA3AB232EF06BB74BA9A949EC0C7ED324C19B202F4AB725BBB4080C9",
256 : "C643946CEA8748E12D430C48DB038F9165814389",
257 : "59B7E7BA0033CCE8E6837173420FBB382A784D4154A3C166043F5A68CB92945D16"
258 : "892D4CC5585F2D28C780E75A6C20A379E2B58304C1E5FC0D8C15E4E89C4498C8BC"
259 : "B90FB36ED8DC0489B9D0BC09EC4411FB0BFADF25485EEAB6700BE0ACF5C44A6ED7"
260 : "44A015382FF9B8DA7EAA00DEA135FADC59212DBBFFC1537336FA4B7225",
261 : "02708ab36e3f0bfd67ec3b8bd8829d03b84f56bd",
262 : 50,
263 : "02"
264 : },
265 : {
266 : 1024,
267 : "9C664033DB8B203D826F896D2293C62EF9351D5CFD0F4C0AD7EFDA4DDC7F15987"
268 : "6A3C68CAB2586B44FD1BD4DEF7A17905D88D321DD77C4E1720D848CA21D79F9B3"
269 : "D8F537338E09B44E9F481E8DA3C56569F63146596A050EF8FAEE8ACA32C666450"
270 : "04F675C8806EB4025B0A5ECC39CE89983EA40A183A7CF5208BA958045ABD5",
271 : "AD0D8CBA369AF6CD0D2BAC0B4CFCAF0A1F9BCDF7",
272 : "74D717F7092A2AF725FDD6C2561D1DBE5AEE40203C638BA8B9F49003857873701"
273 : "95A44E515C4E8B344F5CDC7F4A6D38097CD57675E7643AB9700692C69F0A99B0E"
274 : "039FDDDFCA8CEB607BDB4ADF2834DE1690F5823FC8199FB8F6F29E5A583B6786A"
275 : "C14C7E67106C3B30568CBB9383F89287D578159778EB18216799D16D46498",
276 : "6481a12a50384888ee84b61024f7c9c685d6ac96",
277 : 289,
278 : "02"
279 : },
280 : {
281 : 1024,
282 :
283 : "B0DFB602EB8462B1DC8C2214A52B587D3E6842CCF1C38D0F7C7F967ED30CF6828"
284 : "1E2675B3BAB594755FB1634E66B4C23936F0725A358F8DFF3C307E2601FD66D63"
285 : "5B17270450C50BD2BEC29E0E9A471DF1C15B0191517952268A2763D4BD28B8503"
286 : "B3399686272B76B11227F693D7833105EF70C2289C3194CF4527024B272DF",
287 : "EA649C04911FAB5A41440287A517EF752A40354B",
288 : "88C5A4563ECB949763E0B696CD04B21321360F54C0EE7B23E2CEDC30E9E486162"
289 : "01BFB1619E7C54B653D1F890C50E04B29205F5E3E2F93A13B0751AF25491C5194"
290 : "93C09DDF6B9C173B3846DFB0E7A5C870BBFC78419260C90E20315410691C8326C"
291 : "858D7063E7921F3F601158E912C7EE487FF259202BEEB10F6D9E99190F696",
292 : "5bf9d17bc62fbbf3d569c92bd4505586b2e5ef1a",
293 : 626,
294 : "02"
295 : },
296 : {
297 : 1024,
298 : "F783C08D7F9463E48BA87893805C4B34B63C85DF7EBDD9EBEE94DB4AF4E4A415C"
299 : "F0F3793AE55096BA1199598798FA8403B28DED7F7C7AFD54FD535861A0150EF4D"
300 : "5871465B13837CCF46BEB0A22F8D38DC7D6AE0E14A3845FD0C027CFA97791B977"
301 : "CE2808BAD9B43CE69390C0F40016056722D82C0D7B1B27413D026A39D7DAD",
302 : "A40D9EE456AED4C8A653FDB47B6629C0B843FE8F",
303 : "DF876263E21F263AE6DA57409BD517DCEADB9216048F066D6B58867F8E59A5EEE"
304 : "700283A946C1455534618979BE6C227673C1B803910262BD93BC94D5089850614"
305 : "F3E29AB64E8C989A7E3E28FE670FFA3EE21DEEEC1AB0B60E1D8E2AA39663BADD7"
306 : "2C9F957D7F3D4F17D9FDAD050EB373A6DEFD09F5DA752EAFE046836E14B67",
307 : "8a9a57706f69f4f566252cdf6d5cbfdf2020150b",
308 : 397,
309 : "02"
310 : },
311 : {
312 : 1024,
313 : "D40E4F6461E145859CCF60FD57962840BD75FFF12C22F76626F566842252AD068"
314 : "29745F0147056354F6C016CF12762B0E331787925B8128CF5AF81F9B176A51934"
315 : "96D792430FF83C7B79BD595BDA10787B34600787FA552EFE3662F37B99AAD3F3A"
316 : "093732680A01345192A19BECCE6BF5D498E44ED6BED5B0BA72AAD49E8276B",
317 : "D12F1BD0AA78B99247FD9F18EAFEE5C136686EA5",
318 : "468EBD20C99449C1E440E6F8E452C6A6BC7551C555FE5E94996E20CFD4DA3B9CC"
319 : "58499D6CC2374CCF9C392715A537DE10CFCA8A6A37AFBD187CF6B88D26881E5F5"
320 : "7521D9D2C9BBA51E7B87B070BBE73F5C5FE31E752CAF88183516D8503BAAC1159"
321 : "928EF50DEE52D96F396B93FB4138D786464C315401A853E57C9A0F9D25839",
322 : "30b3599944a914a330a3f49d11ec88f555422aef",
323 : 678,
324 : "02"
325 : }
326 : };
327 : gpg_error_t err;
328 : int tno;
329 : gcry_sexp_t key_spec, key, pub_key, sec_key, seed_values;
330 : gcry_sexp_t l1;
331 :
332 11 : for (tno = 0; tno < DIM (tbl); tno++)
333 : {
334 10 : if (verbose)
335 0 : info ("generating FIPS 186-2 test key %d\n", tno);
336 :
337 : {
338 : void *data;
339 : size_t datalen;
340 :
341 10 : data = data_from_hex (tbl[tno].seed, &datalen);
342 10 : err = gcry_sexp_build (&key_spec, NULL,
343 : "(genkey (dsa (nbits %d)(use-fips186-2)"
344 : "(derive-parms(seed %b))))",
345 : tbl[tno].nbits, (int)datalen, data);
346 10 : gcry_free (data);
347 : }
348 10 : if (err)
349 0 : die ("error creating S-expression %d: %s\n", tno, gpg_strerror (err));
350 :
351 10 : err = gcry_pk_genkey (&key, key_spec);
352 10 : gcry_sexp_release (key_spec);
353 10 : if (err)
354 : {
355 0 : fail ("error generating key %d: %s\n", tno, gpg_strerror (err));
356 0 : continue;
357 : }
358 :
359 10 : if (verbose > 1)
360 0 : show_sexp ("generated key:\n", key);
361 :
362 10 : pub_key = gcry_sexp_find_token (key, "public-key", 0);
363 10 : if (!pub_key)
364 0 : fail ("public part missing in key %d\n", tno);
365 :
366 10 : sec_key = gcry_sexp_find_token (key, "private-key", 0);
367 10 : if (!sec_key)
368 0 : fail ("private part missing in key %d\n", tno);
369 :
370 10 : l1 = gcry_sexp_find_token (key, "misc-key-info", 0);
371 10 : if (!l1)
372 0 : fail ("misc_key_info part missing in key %d\n", tno);
373 10 : seed_values = gcry_sexp_find_token (l1, "seed-values", 0);
374 10 : if (!seed_values)
375 0 : fail ("seed-values part missing in key %d\n", tno);
376 10 : gcry_sexp_release (l1);
377 :
378 10 : extract_cmp_mpi (sec_key, "p", tbl[tno].p);
379 10 : extract_cmp_mpi (sec_key, "q", tbl[tno].q);
380 10 : extract_cmp_mpi (sec_key, "g", tbl[tno].g);
381 :
382 10 : extract_cmp_data (seed_values, "seed", tbl[tno].seed);
383 10 : extract_cmp_int (seed_values, "counter", tbl[tno].counter);
384 10 : extract_cmp_mpi (seed_values, "h", tbl[tno].h);
385 :
386 10 : gcry_sexp_release (seed_values);
387 10 : gcry_sexp_release (sec_key);
388 10 : gcry_sexp_release (pub_key);
389 10 : gcry_sexp_release (key);
390 : }
391 1 : }
392 :
393 :
394 : static void
395 1 : check_dsa_gen_186_3 (void)
396 : {
397 : /* FIXME: Needs to be implemented. */
398 1 : if (verbose)
399 0 : info ("generating FIPS 186-3 test keys - skipped\n");
400 1 : }
401 :
402 :
403 : int
404 1 : main (int argc, char **argv)
405 : {
406 1 : if (argc > 1 && !strcmp (argv[1], "--verbose"))
407 0 : verbose = 1;
408 1 : else if (argc > 1 && !strcmp (argv[1], "--debug"))
409 : {
410 0 : verbose = 2;
411 0 : debug = 1;
412 : }
413 :
414 1 : xgcry_control (GCRYCTL_DISABLE_SECMEM, 0);
415 1 : if (!gcry_check_version (GCRYPT_VERSION))
416 0 : die ("version mismatch\n");
417 1 : xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
418 1 : if (debug)
419 0 : xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
420 : /* No valuable keys are create, so we can speed up our RNG. */
421 1 : xgcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
422 :
423 :
424 1 : check_dsa_gen_186_2 ();
425 1 : check_dsa_gen_186_3 ();
426 :
427 :
428 1 : return error_count ? 1 : 0;
429 : }
|