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