-
Notifications
You must be signed in to change notification settings - Fork 1
/
share.c
348 lines (313 loc) · 7.36 KB
/
share.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "secshare.h"
/*Solve a random system of linear equations using the Gauss-Jordan-Function
*
*This version computes inside the field (M,+,*) for M being the set 0<=k<p for p being a prime number and modulus of the field.
*
*Other than gje_intmod.c it uses interdependent coefficients: each row is formed as k ~~ (m x x^2 x^3 ... x^(n-1)), so that
*k=m*1 + a*x + b*x^2 ... z*x^(n-1)
*This can be used to split a secret m among n persons by creating random a,b,c,... and x and giving each person his/her k and x,
*the algorithm below can then compute m,a,b,c... from n sets of k and x.
*
*Other than gje_intmod2.c it uses large integers and can thus handle much larger numbers.
*
*written by Konrad Rosenbaum, 2004
*This code is protected by the GNU GPL v2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <gmp.h>
/*the matrix and result field*/
mpz_t **kmatrix,**hmatrix,*kvec,*hvec;
/*n=size of the matrix*/
static int n,np;
/*p is the prime and modulo*/
static mpz_t p;
static void setbin(mpz_t n,byte*b,int l)
{
char buf[1024];
static const char hex[]="0123456789ABCDEF";
int i;
for(i=0;i<l;i++){
buf[i*2]=hex[b[i]>>4];
buf[i*2+1]=hex[b[i]&0xf];
}
buf[l*2]=0;
mpz_set_str(n,buf,16);
}
void sharevector(int n_)
{
int i,j;
byte hdl[17];
n=n_;
/*intialize vector and p*/
kvec=malloc(n*sizeof(mpz_t));
hvec=malloc(n*sizeof(mpz_t));
for(i=0;i<n;i++){
mpz_init(kvec[i]);
mpz_init(hvec[i]);
if(i==0){
setbin(kvec[0],key,16);
setbin(hvec[0],hashv,16);
printf("Setting key: %s\n",key_prints(0,key));
printf("Setting hash: %s\n",key_prints(0,hashv));
}else{
for(j=0;j<16;j++)
hdl[j]=randbyte();
setbin(kvec[i],hdl,16);
for(j=0;j<16;j++)
hdl[j]=randbyte();
setbin(hvec[i],hdl,16);
}
}
mpz_init(p);
/*generate random prime*/
hdl[0]=1;
for(i=0;i<500;i++){
for(j=1;j<17;j++)
hdl[j]=randbyte();
hdl[16]|=1;
setbin(p,hdl,17);
j=mpz_probab_prime_p(p,100);
if(j)break;
}
if(!j){
fprintf(stderr,"Unable to generate a good prime. Giving up.\n");
exit(1);
}
}
void sharematrix(int n_)
{
int i,j;
n=n_;np=0;
kmatrix=malloc(n*sizeof(mpz_t*));
hmatrix=malloc(n*sizeof(mpz_t*));
for(i=0;i<n;i++){
kmatrix[i]=malloc((n+1)*sizeof(mpz_t));
hmatrix[i]=malloc((n+1)*sizeof(mpz_t));
for(j=0;j<=n;j++)mpz_init(kmatrix[i][j]);
for(j=0;j<=n;j++)mpz_init(hmatrix[i][j]);
}
mpz_init(p);
}
void share(char*fn)
{
int i;
mpz_t x,k,t,e;
byte rnd[16];
char buf[1024];
FILE *f;
f=fopen(fn,"w+");
if(f==NULL){
fprintf(stderr,"Unable to open file %s.\n",fn);
return;
}
/*generate random x*/
mpz_init(x);
for(i=0;i<16;i++)rnd[i]=randbyte();
setbin(x,rnd,16);
/*calculate k*/
mpz_init(k);
mpz_init(t);
mpz_init(e);
for(i=0;i<n;i++){
/*k=k + kvec[i]*x^i*/
mpz_powm(t,x,e,p);
mpz_mul(t,t,kvec[i]);
mpz_add(k,k,t);
mpz_mod(k,k,p);
/*incr e so that it matches the next i*/
mpz_add_ui(e,e,1);
}
fprintf(f,"%s\n",mpz_get_str(buf,16,p));
fprintf(f,"%s\n",mpz_get_str(buf,16,x));
fprintf(f,"%s\n",mpz_get_str(buf,16,k));
mpz_set_ui(e,0);
mpz_set_ui(k,0);
for(i=0;i<n;i++){
/*k=k + hvec[i]*x^i*/
mpz_powm(t,x,e,p);
mpz_mul(t,t,hvec[i]);
mpz_add(k,k,t);
mpz_mod(k,k,p);
/*incr e so that it matches the next i*/
mpz_add_ui(e,e,1);
}
fprintf(f,"%s\n",mpz_get_str(buf,16,k));
fclose(f);
mpz_clear(x);
mpz_clear(k);
mpz_clear(t);
mpz_clear(e);
}
void unshare(char*fn)
{
FILE *f;
char buf[1024];
mpz_t x,kk,kh,e;
int i;
if(np>=n)return;
/*read file, fill values*/
f=fopen(fn,"r");
if(f==NULL){
fprintf(stderr,"Unable to read file %s. Continuing.\n",fn);
return;
}
mpz_init(x);
mpz_init(kk);
mpz_init(kh);
mpz_init(e);
fscanf(f,"%s",buf);
mpz_set_str(p,buf,16);
fscanf(f,"%s",buf);
mpz_set_str(x,buf,16);
fscanf(f,"%s",buf);
mpz_set_str(kk,buf,16);
fscanf(f,"%s",buf);
mpz_set_str(kh,buf,16);
fclose(f);
/*generate matrixes*/
for(i=0;i<n;i++){
mpz_powm(kmatrix[np][i],x,e,p);
mpz_powm(hmatrix[np][i],x,e,p);
mpz_add_ui(e,e,1);
}
mpz_set(kmatrix[np][n],kk);
mpz_set(hmatrix[np][n],kh);
/*next line...*/
np++;
mpz_clear(x);
mpz_clear(kk);
mpz_clear(kh);
mpz_clear(e);
}
/*print the matrix*/
void printm(mpz_t**matrix)
{
int i,j;
char buf[1024];
for(i=0;i<n;i++){
for(j=0;j<=n;j++)
printf(" %s",mpz_get_str(buf,16,matrix[i][j]));
printf("\n");
}
printf("\n");
}
static void solve(mpz_t**matrix)
{
int i,j,k;
mpz_t f,*t,t1;
mpz_init(f);
mpz_init(t1);
/*bring matrix into Row Echelon Form*/
/*printf("Calculating.\n");*/
for(i=0;i<n;i++){/*iterate main diagonal*/
j=1;
while(!mpz_cmp_ui(matrix[i][i],0)){
/*exchange rows if working position is zero*/
if(i+j>=n){
fprintf(stderr,"System is unsolvable!\n");
exit(1);
}
t=matrix[i];
matrix[i]=matrix[i+j];
matrix[i+j]=t;
j++;
}
/*divide row by working position*/
mpz_invert(f,matrix[i][i],p);/*f=1/m[i,i]*/
for(j=0;j<=n;j++){
/*m[i,j]=m[i,j]/m[i,i] mod p*/
mpz_mul(matrix[i][j],matrix[i][j],f);
mpz_mod(matrix[i][j],matrix[i][j],p);
}
/*go through rows beneath*/
for(j=i+1;j<n;j++){
/*store value of current column next row*/
mpz_set(f,matrix[j][i]);
/*multiply with current row*/
/*subtract from processed row, so that current column in that row is forced to zero*/
for(k=0;k<=n;k++){
mpz_mul(t1,matrix[i][k],f);
mpz_sub(matrix[j][k],matrix[j][k],t1);
mpz_mod(matrix[j][k],matrix[j][k],p);
}
}
}
/*printf("Row Echelon form.\n");*/
/*bring matrix into Reduced Row Echelon Form*/
for(i=n-1;i>=0;i--){/*iterate main diagonal*/
j=1;
while(!mpz_cmp_ui(matrix[i][i],0)){
/*exchange rows if working position is zero*/
if(i-j<0){
fprintf(stderr,"System is unsolvable!\n");
exit(1);
}
t=matrix[i];
matrix[i]=matrix[i+j];
matrix[i+j]=t;
j++;
}
/*divide row by working position*/
mpz_invert(f,matrix[i][i],p);/*f=1/m[i,i]*/
for(j=0;j<=n;j++){
/*m[i,j]=m[i,j]/m[i,i] mod p*/
mpz_mul(matrix[i][j],matrix[i][j],f);
mpz_mod(matrix[i][j],matrix[i][j],p);
}
/*go through rows above*/
for(j=i-1;j>=0;j--){
/*store value of current column next row*/
mpz_set(f,matrix[j][i]);
/*multiply with current row*/
/*subtract from processed row, so that current column in that row is forced to zero*/
for(k=0;k<=n;k++){
mpz_mul(t1,matrix[i][k],f);
mpz_sub(matrix[j][k],matrix[j][k],t1);
mpz_mod(matrix[j][k],matrix[j][k],p);
}
}
}
/*printf("Reduced Row Echelon form.\n");*/
}
static int unhex(char c)
{
if(c>='0'&&c<='9')return c-'0';
if(c>='a'&&c<='f')return c-'a'+10;
if(c>='A'&&c<='F')return c-'A'+10;
return 0;
}
static void str2bin(byte*tgt,const char*str,int len)
{
int i;
char s[1024];
if(strlen(str)<(len*2)){
for(i=0;i<len*2;i++)s[i]='0';
strcpy(s+(len*2-strlen(str)),str);
}else strcpy(s,str);
for(i=0;i<len;i++){
tgt[i]=(unhex(s[i*2])<<4) | unhex(s[i*2+1]);
}
}
void sharecalc()
{
char buf[1024];
if(np<n){
fprintf(stderr,"Could not read enough shares, unable to compute.\n");
exit(1);
}
/*calculate*/
/*printf("Key-Matrix:\n");printm(kmatrix);
printf("Hash-Matrix:\n");printm(hmatrix);*/
solve(kmatrix);
solve(hmatrix);
/*output result*/
str2bin(key,mpz_get_str(buf,16,kmatrix[0][n]),16);
str2bin(hashv,mpz_get_str(buf,16,hmatrix[0][n]),16);
/*printf("Using key: %s\n",key_prints(0,key));
printf("Expected Hash: %s\n",key_prints(0,hashv));*/
/*printf("Key-Matrix:\n");printm(kmatrix);
printf("Hash-Matrix:\n");printm(hmatrix);*/
}