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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/** \ingroup ELGAMAL_m
* \file elgamal.c
*
* ElGamal signature scheme, code
*
* This code implements two of the six variants described:
*
* - ElGamal Signature variant 1: (i.e. the standard version)
* - Signing equation:
* - r = g^k mod p and
* - s = inv(k) * (h(m) - x*r) mod (p-1)
* - Verifying equation:
* - check 1 <= r <= (p-1)
* - v1 = g^h(m) mod p
* - v2 = y^r * r^s mod p
* - check v1 == v2
* - Simultaneous multiple exponentiation verification:
* - y^r * r^s * g^(p-1-h(m)) mod p = 1 or (the former is probably faster)
* - y^r * r^s * inv(g)^h(m) mod p = 1
*
* - ElGamal Signature variant 3: signing is simpler, because no inverse has to be calculated
* - Signing equation:
* - r = g^k mod p and
* - s = x*r + k*h(m) mod (p-1)
* - Verifying equation:
* - check 1 <= r <= (p-1)
* - v1 = g^s mod p
* - v2 = y^r * r^h(m) mod p
* - Simultaneous multiple exponentiation verification:
* - y^r * r^h(m) * g^(p-1-s) mod p = 1 (one of the exponents is significantly smaller, i.e. h(m))
*
* For more information on this algorithm, see:
* "Handbook of Applied Cryptography"
* 11.5.2 "The ElGamal signature scheme", p. 454-459
*/
/*
* Copyright (c) 1999, 2000, 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 "elgamal.h"
#include "dldp.h"
#include "mp32.h"
#include "debug.h"
int elgv1sign(const mp32barrett* p, const mp32barrett* n, const mp32number* g, randomGeneratorContext* rgc, const mp32number* hm, const mp32number* x, mp32number* r, mp32number* s)
{
register uint32 size = p->size;
register uint32* temp = (uint32*) malloc((13*size+11) * sizeof(*temp));
if (temp)
{
/* get a random k, invertible modulo (p-1) */
mp32brndinv_w(n, rgc, temp, temp+size, temp+2*size);
/* compute r = g^k mod p */
mp32nfree(r);
mp32nsize(r, size);
mp32bpowmod_w(p, g->size, g->data, size, temp, r->data, temp+2*size);
/* compute x*r mod n */
mp32bmulmod_w(n, x->size, x->data, r->size, r->data, temp, temp+2*size);
/* compute -(x*r) mod n */
mp32neg(size, temp);
(void) mp32add(size, temp, n->modl);
/* compute h(m) - x*r mod n */
mp32baddmod_w(n, hm->size, hm->data, size, temp, temp, temp+2*size);
/* compute s = inv(k)*(h(m) - x*r) mod n */
mp32nfree(s);
mp32nsize(s, size);
mp32bmulmod_w(n, size, temp, size, temp+size, s->data, temp+2*size);
free(temp);
return 0;
}
return -1;
}
int elgv1vrfy(const mp32barrett* p, const mp32barrett* n, const mp32number* g, const mp32number* hm, const mp32number* y, const mp32number* r, const mp32number* s)
{
register uint32 size = p->size;
register uint32* temp;
if (mp32z(r->size, r->data))
return 0;
if (mp32gex(r->size, r->data, size, p->modl))
return 0;
if (mp32z(s->size, s->data))
return 0;
if (mp32gex(s->size, s->data, n->size, n->modl))
return 0;
temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
if (temp)
{
register int rc;
/* compute u1 = y^r mod p */
mp32bpowmod_w(p, y->size, y->data, r->size, r->data, temp, temp+2*size);
/* compute u2 = r^s mod p */
mp32bpowmod_w(p, r->size, r->data, s->size, s->data, temp+size, temp+2*size);
/* compute v2 = u1*u2 mod p */
mp32bmulmod_w(p, size, temp, size, temp+size, temp+size, temp+2*size);
/* compute v1 = g^h(m) mod p */
mp32bpowmod_w(p, g->size, g->data, hm->size, hm->data, temp, temp+2*size);
rc = mp32eq(size, temp, temp+size);
free(temp);
return rc;
}
return 0;
}
int elgv3sign(const mp32barrett* p, const mp32barrett* n, const mp32number* g, randomGeneratorContext* rgc, const mp32number* hm, const mp32number* x, mp32number* r, mp32number* s)
{
register uint32 size = p->size;
register uint32* temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
if (temp)
{
/* get a random k */
mp32brnd_w(p, rgc, temp, temp+2*size);
/* compute r = g^k mod p */
mp32nfree(r);
mp32nsize(r, size);
mp32bpowmod_w(p, g->size, g->data, size, temp, r->data, temp+2*size);
/* compute u1 = x*r mod n */
mp32bmulmod_w(n, x->size, x->data, size, r->data, temp+size, temp+2*size);
/* compute u2 = k*h(m) mod n */
mp32bmulmod_w(n, size, temp, hm->size, hm->data, temp, temp+2*size);
/* compute s = u1+u2 mod n */
mp32nfree(s);
mp32nsize(s, n->size);
mp32baddmod_w(n, size, temp, size, temp+size, s->data, temp+2*size);
free(temp);
return 0;
}
return -1;
}
int elgv3vrfy(const mp32barrett* p, const mp32barrett* n, const mp32number* g, const mp32number* hm, const mp32number* y, const mp32number* r, const mp32number* s)
{
register uint32 size = p->size;
register uint32* temp;
if (mp32z(r->size, r->data))
return 0;
if (mp32gex(r->size, r->data, size, p->modl))
return 0;
if (mp32z(s->size, s->data))
return 0;
if (mp32gex(s->size, s->data, n->size, n->modl))
return 0;
temp = (uint32*) malloc((6*size+2) * sizeof(*temp));
if (temp)
{
register int rc;
/* compute u1 = y^r mod p */
mp32bpowmod_w(p, y->size, y->data, r->size, r->data, temp, temp+2*size);
/* compute u2 = r^h(m) mod p */
mp32bpowmod_w(p, r->size, r->data, hm->size, hm->data, temp+size, temp+2*size);
/* compute v2 = u1*u2 mod p */
mp32bmulmod_w(p, size, temp, size, temp+size, temp+size, temp+2*size);
/* compute v1 = g^s mod p */
mp32bpowmod_w(p, g->size, g->data, s->size, s->data, temp, temp+2*size);
rc = mp32eq(size, temp, temp+size);
free(temp);
return rc;
}
return 0;
}
|