-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreciever.c
423 lines (300 loc) · 8.59 KB
/
reciever.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <pbc/pbc.h>
#include <openssl/ssl.h>
#include <openssl/sha.h>
#include <arpa/inet.h>
#define SERVER_PORT 8080
#define SENDER_PORT 8040
#define RECIEVER_PORT 8000
element_t g,g1,u,v,d,h,sks,skr,pks,pkr,c1,c3,s;
char c2[]="";
pairing_t e;
int server_fd, new_socket, valread,sender_fd,reciever_fd;
struct sockaddr_in server_address,sender_address,reciever_address;
int opt = 1;
int server_addrlen = sizeof(server_address),sender_addrlen = sizeof(sender_address),reciever_addrlen = sizeof(reciever_address);
char id_buffer[1024] = {0},secret_buffer[1024] = {0};
char *prev_sender = "nan",*prev_token = "nan";
int serverConnected = -1,senderConnected = -1,recieverConnected = -1;
int sender_socket = 0,server_socket = 0,reciever_socket=0, valread;
char *msg = "sender|connect|nan";
char buffer[4096*8*10] = {0};
void getGlobalSetup()
{
char param[1024];
size_t count = fread(param, 1, 1024, stdin);
if (!count)
{
pbc_die("input error");
}
OpenSSL_add_all_algorithms();
pbc_random_set_file("/dev/urandom");
printf("Global setup running...\n");
printf("Getting global setup and pks...\n");
pairing_init_set_buf(e, param, count);
element_init_G1(g,e);
element_init_G1(g1,e);
element_init_G1(u,e);
element_init_G1(v,e);
element_init_G1(d,e);
element_init_G1(h,e);
element_init_G1(c1, e);
element_init_G1(c3,e);
element_init_Zr(s,e);
element_init_G1(pks,e);
element_init_Zr(sks,e);
FILE *fp_g,*fp_g1,*fp_u,*fp_v,*fp_d,*fp_h,*fp_pks ;
fp_g = fopen("global_setup/g.txt", "rb") ;
fp_g1 = fopen("global_setup/g1.txt", "rb") ;
fp_u = fopen("global_setup/u.txt", "rb") ;
fp_v = fopen("global_setup/v.txt", "rb") ;
fp_d = fopen("global_setup/d.txt", "rb") ;
fp_h = fopen("global_setup/h.txt", "rb") ;
fp_pks = fopen("global_setup/pks.txt", "rb") ;
if(fp_g&&fp_g1&&fp_u&&fp_v&&fp_d&&fp_h&&fp_pks){
fseek(fp_g, 0, SEEK_END);
long g_len = ftell(fp_g);
rewind(fp_g);
fseek(fp_g1, 0, SEEK_END);
long g1_len = ftell(fp_g1);
rewind(fp_g1);
fseek(fp_u, 0, SEEK_END);
long u_len = ftell(fp_u);
rewind(fp_u);
fseek(fp_v, 0, SEEK_END);
long v_len = ftell(fp_v);
rewind(fp_v);
fseek(fp_d, 0, SEEK_END);
long d_len = ftell(fp_d);
rewind(fp_d);
fseek(fp_h, 0, SEEK_END);
long h_len = ftell(fp_h);
rewind(fp_h);
fseek(fp_pks, 0, SEEK_END);
long pks_len = ftell(fp_pks);
rewind(fp_pks);
char *g_bytes = malloc(g_len * sizeof( char));
char *g1_bytes = malloc(g1_len * sizeof( char));
char *u_bytes = malloc(u_len * sizeof( char));
char *v_bytes = malloc(v_len * sizeof( char));
char *d_bytes = malloc(d_len * sizeof( char));
char *h_bytes = malloc(h_len * sizeof( char));
char *pks_bytes = malloc(pks_len * sizeof( char));
fread(g_bytes, g_len, 1, fp_g);
fclose(fp_g);
element_from_bytes_compressed(g, g_bytes);
free(g_bytes);
element_printf("g : %B\n\n", g);
fread(g1_bytes, g1_len, 1, fp_g1);
fclose(fp_g1);
element_from_bytes_compressed(g1, g1_bytes);
free(g1_bytes);
element_printf("g1 : %B\n\n", g1);
fread(u_bytes, u_len, 1, fp_u);
fclose(fp_u);
element_from_bytes_compressed(u, u_bytes);
free(u_bytes);
element_printf("u : %B\n\n", u);
fread(v_bytes, v_len, 1, fp_v);
fclose(fp_v);
element_from_bytes_compressed(v, v_bytes);
free(v_bytes);
element_printf("v : %B\n\n", v);
fread(d_bytes, d_len, 1, fp_d);
fclose(fp_d);
element_from_bytes_compressed(d, d_bytes);
free(d_bytes);
element_printf("d : %B\n\n", d);
fread(h_bytes, h_len, 1, fp_h);
fclose(fp_h);
element_from_bytes_compressed(h, h_bytes);
free(h_bytes);
element_printf("h : %B\n\n", h);
fread(pks_bytes, pks_len, 1, fp_pks);
fclose(fp_pks);
element_from_bytes_compressed(pks, pks_bytes);
free(pks_bytes);
element_printf("pks : %B\n\n", pks);
}
}
void KeyGenR()
{
element_init_G1(pkr,e);
element_init_Zr(skr,e);
element_random(skr);
element_pow_zn(pkr, g, skr);
element_printf("Skr = %B\n",skr);
element_printf("Pkr = %B\n",pkr);
FILE *fp_pkr ;
fp_pkr = fopen("global_setup/pkr.txt", "w") ;
if(fp_pkr){
int pkr_len = element_length_in_bytes_compressed(pks);
char *pkr_bytes = malloc(pkr_len * sizeof( char));
element_to_bytes_compressed(pkr_bytes, pkr);
fputs(pkr_bytes, fp_pkr) ;
fputs("\n", fp_pkr) ;
fclose(fp_pkr) ;
free(pkr_bytes);
printf("PKS written\n");
}
}
int makeSocket(){
printf("SOCKET INITIALIZATION\n");
if ((reciever_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(reciever_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
reciever_address.sin_family = AF_INET;
reciever_address.sin_addr.s_addr = INADDR_ANY;
reciever_address.sin_port = htons( RECIEVER_PORT );
if (bind(reciever_fd, (struct sockaddr *)&reciever_address,
sizeof(reciever_address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(reciever_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
return 1;
}
int connectToServer(){
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Server Socket creation error \n");
return -1;
}
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
printf("\nConnection to server failed \n");
return -1;
}
return 1;
}
int connectToSender(){
if ((sender_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Sender Socket creation error \n");
return -1;
}
sender_address.sin_family = AF_INET;
sender_address.sin_port = htons(SENDER_PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &sender_address.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sender_socket, (struct sockaddr *)&sender_address, sizeof(sender_address)) < 0)
{
printf("\nConnection to sender failed \n");
return -1;
}
return 1;
}
int retryToServer(){
if (connect(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
printf("\nConnection to server failed \n");
return -1;
}
return 1;
}
int retryToSender(){
if (connect(sender_socket, (struct sockaddr *)&sender_address, sizeof(sender_address)) < 0)
{
printf("\nConnection to sender failed \n");
return -1;
}
return 1;
}
void getPEKS(){
FILE *fp_c1,*fp_c2,*fp_c3 ;
//fp_s= fopen("global_setup/s.txt", "rb") ;
fp_c1 = fopen("global_setup/c1.txt", "rb") ;
fp_c2= fopen("global_setup/c2.txt", "rb") ;
fp_c3 = fopen("global_setup/c3.txt", "rb") ;
if(fp_c1&&fp_c2&&fp_c3){
/*fseek(fp_s, 0, SEEK_END);
long s_len = ftell(fp_s);
rewind(fp_s);*/
fseek(fp_c1, 0, SEEK_END);
long c1_len = ftell(fp_c1);
rewind(fp_c1);
fseek(fp_c2, 0, SEEK_END);
long c2_len = ftell(fp_c2);
rewind(fp_c2);
fseek(fp_c3, 0, SEEK_END);
long c3_len = ftell(fp_c3);
rewind(fp_c3);
//char *s_bytes = malloc(s_len * sizeof( char));
char *c1_bytes = malloc(c1_len * sizeof( char));
char *c3_bytes = malloc(c3_len * sizeof( char));
/*fread(s_bytes, s_len, 1, fp_s);
fclose(fp_s);
element_from_bytes_compressed(s,s_bytes);
free(s_bytes);
element_printf("s : %B\n\n", s);*/
fread(c1_bytes, c1_len, 1, fp_c1);
fclose(fp_c1);
element_from_bytes_compressed(c1,c1_bytes);
free(c1_bytes);
element_printf("c1 : %B\n\n", c1);
fread(c2, c2_len, 1, fp_c2);
fclose(fp_c2);
printf("c2 : %s\n\n", c2);
fread(c3_bytes, c3_len, 1, fp_c3);
fclose(fp_c3);
element_from_bytes_compressed( c3,c3_bytes);
free(c3_bytes);
element_printf("c3 : %B\n\n", c3);
}
}
int main(int argc, char const *argv[]){
if(makeSocket()){
printf("Reciever socket made successfuly\n");
}
getGlobalSetup();
KeyGenR();
connectToServer();
msg = "pkr_set";
send(server_socket,msg,strlen(msg),0);
memset(buffer,0,sizeof buffer);
while(1){
if ((new_socket = accept(reciever_fd, (struct sockaddr *)&reciever_address,
(socklen_t*)&reciever_addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}else{
valread = read( new_socket , id_buffer, 1024);
if(strcmp(id_buffer,"peks_ct")==0){
printf("Sender sending CT\n");
getPEKS();
}
}
}
return 0;
}