1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/** \ingroup DSA_m
* \file dsa.c
*
* Digital Signature Algorithm signature scheme, code.
*
* DSA Signature:
* - Signing equation:
* - r = (g^k mod p) mod q and
* - s = (inv(k) * (h(m) + x*r)) mod q
* - Verifying equation:
* - check 0 < r < q and 0 < s < q
* - w = inv(s) mod q
* - u1 = (h(m)*w) mod q
* - u2 = (r*w) mod q
* - v = ((g^u1 * y^u2) mod p) mod q
* - check v == r
*
* For more information on this algorithm, see:
* NIST FIPS 186-1
*/
/*
* Copyright (c) 2001 Virtual Unlimited B.V.
*
* Author: Bob Deblier <bob@virtualunlimited.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*/
#include "system.h"
#include "dsa.h"
#include "dldp.h"
#include "mp32.h"
#include "debug.h"
/*@unchecked@*/
static int _debug = 0;
int dsasign(const mp32barrett* p, const mp32barrett* q, const mp32number* g, randomGeneratorContext* rgc, const mp32number* hm, const mp32number* x, mp32number* r, mp32number* s)
{
register uint32 psize = p->size;
register uint32 qsize = q->size;
register uint32* ptemp;
register uint32* qtemp;
register uint32* pwksp;
register uint32* qwksp;
register int rc = -1; /* assume failure */
ptemp = (uint32*) malloc((5*psize+2) * sizeof(*ptemp));
if (ptemp == NULL)
return rc;
qtemp = (uint32*) malloc((14*qsize+11) * sizeof(*qtemp));
if (qtemp == NULL) {
free(ptemp);
return rc;
}
pwksp = ptemp+psize;
qwksp = qtemp+3*qsize;
/* allocate r */
mp32nfree(r);
mp32nsize(r, qsize);
/* get a random k, invertible modulo q */
mp32brndinv_w(q, rgc, qtemp, qtemp+qsize, qwksp);
/* FIPS 186 test vectors
qtemp[0] = 0x358dad57;
qtemp[1] = 0x1462710f;
qtemp[2] = 0x50e254cf;
qtemp[3] = 0x1a376b2b;
qtemp[4] = 0xdeaadfbf;
mp32binv_w(q, qsize, qtemp, qtemp+qsize, qwksp);
*/
/* g^k mod p */
mp32bpowmod_w(p, g->size, g->data, qsize, qtemp, ptemp, pwksp);
/* (g^k mod p) mod q - simple modulo */
mp32nmod(qtemp+2*qsize, psize, ptemp, qsize, q->modl, pwksp);
mp32copy(qsize, r->data, qtemp+psize+qsize);
/* allocate s */
mp32nfree(s);
mp32nsize(s, qsize);
/* x*r mod q */
mp32bmulmod_w(q, x->size, x->data, r->size, r->data, qtemp, qwksp);
/* add h(m) mod q */
mp32baddmod_w(q, qsize, qtemp, hm->size, hm->data, qtemp+2*qsize, qwksp);
/* multiply inv(k) mod q */
mp32bmulmod_w(q, qsize, qtemp+qsize, qsize, qtemp+2*qsize, s->data, qwksp);
rc = 0;
free(qtemp);
free(ptemp);
return rc;
}
int dsavrfy(const mp32barrett* p, const mp32barrett* q, const mp32number* g, const mp32number* hm, const mp32number* y, const mp32number* r, const mp32number* s)
{
register uint32 psize = p->size;
register uint32 qsize = q->size;
register uint32* ptemp;
register uint32* qtemp;
register uint32* pwksp;
register uint32* qwksp;
register int rc = 0; /* XXX shouldn't this be -1 ?*/
if (mp32z(r->size, r->data))
return rc;
if (mp32gex(r->size, r->data, qsize, q->modl))
return rc;
if (mp32z(s->size, s->data))
return rc;
if (mp32gex(s->size, s->data, qsize, q->modl))
return rc;
ptemp = (uint32*) malloc((6*psize+2) * sizeof(*ptemp));
if (ptemp == NULL)
return rc;
qtemp = (uint32*) malloc((13*qsize+11) * sizeof(*qtemp));
if (qtemp == NULL) {
free(ptemp);
return rc;
}
pwksp = ptemp+2*psize;
qwksp = qtemp+2*qsize;
/* compute w = inv(s) mod q */
if (mp32binv_w(q, s->size, s->data, qtemp, qwksp))
{
/* compute u1 = h(m)*w mod q */
mp32bmulmod_w(q, hm->size, hm->data, qsize, qtemp, qtemp+qsize, qwksp);
/* compute u2 = r*w mod q */
mp32bmulmod_w(q, r->size, r->data, qsize, qtemp, qtemp, qwksp);
/* compute g^u1 mod p */
mp32bpowmod_w(p, g->size, g->data, qsize, qtemp+qsize, ptemp, pwksp);
/* compute y^u2 mod p */
mp32bpowmod_w(p, y->size, y->data, qsize, qtemp, ptemp+psize, pwksp);
/* multiply mod p */
mp32bmulmod_w(p, psize, ptemp, psize, ptemp+psize, ptemp, pwksp);
/* modulo q */
mp32nmod(ptemp+psize, psize, ptemp, qsize, q->modl, pwksp);
rc = mp32eqx(r->size, r->data, psize, ptemp+psize);
}
free(qtemp);
free(ptemp);
return rc;
}
|