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
|
#include <lib9.h>
#include <kernel.h>
#include <isa.h>
#include "interp.h"
#include "../libinterp/runt.h"
#include "mp.h"
#include "libsec.h"
#include "keys.h"
static char* pkattr[] = { "p", "alpha", "key", nil };
static char* skattr[] = { "p", "alpha", "key", "!secret", nil };
static char* sigattr[] = { "r", "s", nil };
static void*
eg_str2sk(char *str, char **strp)
{
EGpriv *eg;
char *p;
eg = egprivalloc();
eg->pub.p = base64tobig(str, &p);
eg->pub.alpha = base64tobig(p, &p);
eg->pub.key = base64tobig(p, &p);
eg->secret = base64tobig(p, &p);
if(strp)
*strp = p;
return eg;
}
static void*
eg_str2pk(char *str, char **strp)
{
EGpub *eg;
char *p;
eg = egpuballoc();
eg->p = base64tobig(str, &p);
eg->alpha = base64tobig(p, &p);
eg->key = base64tobig(p, &p);
if(strp)
*strp = p;
return eg;
}
static void*
eg_str2sig(char *str, char **strp)
{
EGsig *eg;
char *p;
eg = egsigalloc();
eg->r = base64tobig(str, &p);
eg->s = base64tobig(p, &p);
if(strp)
*strp = p;
return eg;
}
static int
eg_sk2str(void *veg, char *buf, int len)
{
EGpriv *eg;
char *cp, *ep;
eg = (EGpriv*)veg;
ep = buf + len - 1;
cp = buf;
cp += snprint(cp, ep - cp, "%U\n", eg->pub.p);
cp += snprint(cp, ep - cp, "%U\n", eg->pub.alpha);
cp += snprint(cp, ep - cp, "%U\n", eg->pub.key);
cp += snprint(cp, ep - cp, "%U\n", eg->secret);
*cp = 0;
return cp - buf;
}
static int
eg_pk2str(void *veg, char *buf, int len)
{
EGpub *eg;
char *cp, *ep;
eg = (EGpub*)veg;
ep = buf + len - 1;
cp = buf;
cp += snprint(cp, ep - cp, "%U\n", eg->p);
cp += snprint(cp, ep - cp, "%U\n", eg->alpha);
cp += snprint(cp, ep - cp, "%U\n", eg->key);
*cp = 0;
return cp - buf;
}
static int
eg_sig2str(void *veg, char *buf, int len)
{
EGsig *eg;
char *cp, *ep;
eg = veg;
ep = buf + len - 1;
cp = buf;
cp += snprint(cp, ep - cp, "%U\n", eg->r);
cp += snprint(cp, ep - cp, "%U\n", eg->s);
*cp = 0;
return cp - buf;
}
static void*
eg_sk2pk(void *vs)
{
return egprivtopub((EGpriv*)vs);
}
/* generate an el gamal secret key with new params */
static void*
eg_gen(int len)
{
return eggen(len, 0);
}
/* generate an el gamal secret key with same params as a public key */
static void*
eg_genfrompk(void *vpub)
{
EGpub *pub;
EGpriv *priv;
int nlen;
pub = vpub;
priv = egprivalloc();
priv->pub.p = mpcopy(pub->p);
priv->pub.alpha = mpcopy(pub->alpha);
nlen = mpsignif(pub->p);
pub = &priv->pub;
pub->key = mpnew(0);
priv->secret = mpnew(0);
mprand(nlen-1, genrandom, priv->secret);
mpexp(pub->alpha, priv->secret, pub->p, pub->key);
return priv;
}
static void*
eg_sign(BigInt mp, void *key)
{
return egsign((EGpriv*)key, mp);
}
static int
eg_verify(BigInt mp, void *sig, void *key)
{
return egverify((EGpub*)key, (EGsig*)sig, mp) == 0;
}
static void
eg_freepub(void *a)
{
egpubfree((EGpub*)a);
}
static void
eg_freepriv(void *a)
{
egprivfree((EGpriv*)a);
}
static void
eg_freesig(void *a)
{
egsigfree((EGsig*)a);
}
SigAlgVec*
elgamalinit(void)
{
SigAlgVec *vec;
vec = malloc(sizeof(SigAlgVec));
if(vec == nil)
return nil;
vec->name = "elgamal";
vec->pkattr = pkattr;
vec->skattr = skattr;
vec->sigattr = sigattr;
vec->str2sk = eg_str2sk;
vec->str2pk = eg_str2pk;
vec->str2sig = eg_str2sig;
vec->sk2str = eg_sk2str;
vec->pk2str = eg_pk2str;
vec->sig2str = eg_sig2str;
vec->sk2pk = eg_sk2pk;
vec->gensk = eg_gen;
vec->genskfrompk = eg_genfrompk;
vec->sign = eg_sign;
vec->verify = eg_verify;
vec->skfree = eg_freepriv;
vec->pkfree = eg_freepub;
vec->sigfree = eg_freesig;
return vec;
}
|