-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
75 lines (59 loc) · 1.6 KB
/
server.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
#include "scheme.h"
/*
server receives
1. request for public key
2. encrypted messages (ciphertexts)
server sends
1. public key
runs
1. setup() -> PK, SK
2. decrypt(SK, CT) -> M
*/
int main(int argc, char *argv[]) {
int sockfd, cli_socket, binding, reception, M;
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sockfd < 0) {
fprintf(stderr, "ERROR opening socket\n");
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);
serv_addr.sin_addr.s_addr = INADDR_ANY;
binding = bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
if(binding < 0) {
fprintf(stderr, "ERROR on binding\n");
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
cli_socket = accept(sockfd, (struct sockaddr*) &cli_addr, &clilen);
if (cli_socket < 0) {
fprintf(stderr, "ERROR on accept\n");
}
//generate keys
keys PS = Setup();
write(cli_socket, PS.PK.A, n * m * sizeof(int));
write(cli_socket, PS.PK.b, m * sizeof(int));
CT_tuple CT;
CT.CT1 = malloc(n * sizeof(int));
while(1) {
//receive CT
reception = recv(cli_socket, CT.CT1, n * sizeof(int), MSG_WAITALL);
if(reception < 0) {
fprintf(stderr, "ERROR receiving Ciphertext 1\n");
}
reception = recv(cli_socket, &CT.CT2, sizeof(int), MSG_WAITALL);
if(reception < 0) {
fprintf(stderr, "ERROR receiving Ciphertext 2\n");
}
//shutdown if given CT2 -1
if(CT.CT2 == -1) {
break;
}
//decrypt CT and print message
M = Dec(PS.SK, CT);
printf("%d\n", M);
}
close(sockfd);
return 0;
}