-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
166 lines (150 loc) · 5.49 KB
/
common.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
#include "common.h"
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
void print_buf(unsigned char *buf, size_t size) {
char hex[size * 3 + 1];
for(size_t i = 0; i < size; i++) {
sprintf(hex + 3 * i, " %.2x", buf[i]);
}
printf("Hex:%s\n", hex);
}
void generate_nonce(int length, unsigned char *buf) {
int x = RAND_bytes(buf, length);
if (x == -1) {
printf("Failed to create Random Nonce");
exit(1);
}
}
void write_in_n_bytes(uint64_t num, int n, unsigned char *buf) {
for (int i = 0; i < n; i++) {
buf[i] |= num >> 8 * (n - 1 - i);
}
}
unsigned int read_unsigned_int_BE(unsigned char *buf, int byte_length) {
int num = 0;
for (int i = 0; i < byte_length; i++) {
num |= buf[i] << 8 * (byte_length - 1 - i);
}
return num;
}
uint64_t read_unsigned_long_int_BE(unsigned char *buf,
int byte_length) {
uint64_t num_valid = 1ULL;
for (int i = 0; i < byte_length; i++) {
uint64_t num = 1ULL << 8 * (byte_length - 1 - i);
num_valid |= num * buf[i];
}
return num_valid;
}
void var_length_int_to_num(unsigned char *buf, unsigned int buf_length,
unsigned int *num,
unsigned int *var_len_int_buf_size) {
*num = 0;
*var_len_int_buf_size = 0;
for (int i = 0; i < buf_length; i++) {
*num |= (buf[i] & 127) << (7 * i);
if ((buf[i] & 128) == 0) {
*var_len_int_buf_size = i + 1;
break;
}
}
}
void num_to_var_length_int(unsigned int num, unsigned char *var_len_int_buf,
unsigned int *var_len_int_buf_size) {
*var_len_int_buf_size = 1;
while (num > 127) {
var_len_int_buf[*var_len_int_buf_size - 1] = 128 | num & 127;
*var_len_int_buf_size += 1;
num >>= 7;
}
var_len_int_buf[*var_len_int_buf_size - 1] = num;
}
unsigned char *parse_received_message(unsigned char *received_buf,
unsigned int received_buf_length,
unsigned char *message_type,
unsigned int *data_buf_length) {
*message_type = received_buf[0];
unsigned int payload_buf_length;
var_length_int_to_num(received_buf + MESSAGE_TYPE_SIZE, received_buf_length,
data_buf_length, &payload_buf_length);
return received_buf + MESSAGE_TYPE_SIZE +
payload_buf_length;
}
void make_buffer_header(unsigned int data_length, unsigned char MESSAGE_TYPE,
unsigned char *header, unsigned int *header_length) {
unsigned char payload_buf[MAX_PAYLOAD_BUF_SIZE];
unsigned int payload_buf_len;
num_to_var_length_int(data_length, payload_buf, &payload_buf_len);
*header_length = MESSAGE_TYPE_SIZE + payload_buf_len;
header[0] = MESSAGE_TYPE;
memcpy(header + MESSAGE_TYPE_SIZE, payload_buf, payload_buf_len);
}
void concat_buffer_header_and_payload(
unsigned char *header, unsigned int header_length, unsigned char *payload,
unsigned int payload_length, unsigned char *ret, unsigned int *ret_length) {
memcpy(ret, header, header_length);
memcpy(ret + header_length, payload, payload_length);
*ret_length = header_length + payload_length;
}
void make_sender_buf(unsigned char *payload, unsigned int payload_length,
unsigned char MESSAGE_TYPE, unsigned char *sender,
unsigned int *sender_length) {
unsigned char header[MAX_PAYLOAD_BUF_SIZE + 1];
unsigned int header_length;
make_buffer_header(payload_length, MESSAGE_TYPE, header, &header_length);
concat_buffer_header_and_payload(header, header_length, payload,
payload_length, sender, sender_length);
}
void connect_as_client(const char *ip_addr, const char *port_num, int *sock) {
struct sockaddr_in serv_addr;
int str_len;
*sock = socket(PF_INET, SOCK_STREAM, 0);
if (*sock == -1) {
error_handling("socket() error");
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; // IPv4
serv_addr.sin_addr.s_addr =
inet_addr(ip_addr); // the ip_address to connect to
serv_addr.sin_port = htons(atoi(port_num));
if (connect(*sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) ==
-1) {
error_handling("connect() error!");
}
printf("\n\n------------Connected-------------\n");
}
void serialize_handshake(unsigned char *nonce, unsigned char *reply_nonce,
unsigned char *ret) {
if (nonce == NULL && reply_nonce == NULL) {
error_handling("Error: handshake should include at least on nonce.");
}
unsigned char indicator = 0;
if (nonce != NULL) {
indicator += 1;
memcpy(ret + 1, nonce, HS_NONCE_SIZE);
}
if (reply_nonce != NULL) {
indicator += 2;
memcpy(ret + 1 + HS_NONCE_SIZE, reply_nonce, HS_NONCE_SIZE);
}
// TODO: add dhParam options.
ret[0] = indicator;
}
void parse_handshake(unsigned char *buf, HS_nonce_t *ret) {
if ((buf[0] & 1) != 0) {
memcpy(ret->nonce, buf + 1, HS_NONCE_SIZE);
}
if ((buf[0] & 2) != 0) {
memcpy(ret->reply_nonce, buf + 1 + HS_NONCE_SIZE, HS_NONCE_SIZE);
}
if ((buf[0] & 4) != 0) {
memcpy(ret->dhParam, buf + 1 + HS_NONCE_SIZE * 2, HS_NONCE_SIZE);
}
}
int mod(int a, int b) {
int r = a % b;
return r < 0 ? r + b : r;
}