cc-hash.c (fhash): The FILE name may be null.
[u/mdw/catacomb] / dsa-check.c
1 /* -*-c-*-
2 *
3 * $Id: dsa-check.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Consistency checking for DSA keys
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include "dh.h"
33 #include "dsa.h"
34 #include "dsarand.h"
35 #include "grand.h"
36 #include "keycheck.h"
37 #include "mp.h"
38 #include "mprand.h"
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 /* --- @dsa_checkparam@ --- *
43 *
44 * Arguments: @keycheck *kc@ = keycheck state
45 * @const dsa_param *dp@ = pointer to the parameter set
46 * @const dsa_seed *ds@ = pointer to seed information
47 *
48 * Returns: Zero if all OK, or return status from function.
49 *
50 * Use: Checks a set of DSA parameters for consistency and security.
51 */
52
53 int dsa_checkparam(keycheck *kc, const dsa_param *dp, const dsa_seed *ds)
54 {
55 if (ds) {
56 grand *r = dsarand_create(ds->p, ds->sz);
57 mp *p = MP_NEW, *q = MP_NEW;
58 int rc = 0;
59 unsigned i;
60 unsigned long n;
61
62 r->ops->misc(r, DSARAND_PASSES, 2);
63 q = mprand(q, mp_bits(dp->q), r, 1);
64 if (!mp_eq(q, dp->q) &&
65 keycheck_report(kc, KCSEV_ERR, "q doesn't match seed provided"))
66 rc = -1;
67 else {
68 n = mp_bits(dp->p);
69 r->ops->misc(r, DSARAND_PASSES, 1);
70 for (i = 0; i <= ds->count; i++)
71 p = mprand(p, n, r, 0);
72 q = mp_lsl(q, q, 1);
73 mp_div(0, &q, p, q);
74 p = mp_sub(p, p, q);
75 p->v[0] |= 1;
76 if (!mp_eq(p, dp->p) &&
77 keycheck_report(kc, KCSEV_ERR, "p doesn't match seed provided"))
78 rc = -1;
79 }
80 mp_drop(p);
81 mp_drop(q);
82 r->ops->destroy(r);
83 if (rc)
84 return (rc);
85 }
86 return (dh_checkparam(kc, dp, 0, 0));
87 }
88
89 /*----- That's all, folks -------------------------------------------------*/