forked from tiiuae/arid2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.cpp
executable file
·236 lines (199 loc) · 8.76 KB
/
verify.cpp
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
// How to use it
// g++ !(setup|join|sign|open).cpp ./sha1/*.cpp ./base58/*.cpp -o verify -l pbc -l gmp -ltins
// sudo ./verify WIFI_INTERFACE < param/a.param
#include <iostream>
#include <ctime>
#include <chrono>
#include <tins/tins.h>
#include "auxiliary.h"
#include "arid_pdu.h"
#include "colors.h"
using namespace Tins;
using namespace std::chrono;
using namespace std;
int main(int argc, char *argv[])
{
// std::ofstream outfile;
// outfile.open("VERIFY_TEST.txt", std::ios_base::app); // append instead of overwrite
// Pairing
pairing_t pairing;
/* Setup Pairing Parameters */
char param[1024];
size_t count = fread(param, 1, 1024, stdin);
if (!count)
pbc_die("input error");
pairing_init_set_buf(pairing, param, count);
// Public key
element_t gt;
element_t g;
element_t X;
element_t Y;
element_t h;
element_t y1;
element_t y2;
element_t y3;
// Init Public Key gpk
element_init_G1(g, pairing);
element_init_GT(gt, pairing);
element_init_G1(X, pairing);
element_init_G1(Y, pairing);
element_init_GT(h, pairing);
element_init_GT(y1, pairing);
element_init_GT(y2, pairing);
element_init_GT(y3, pairing);
// Read Public Key from file
std::ifstream inFile;
inFile.open("public_key.txt"); // open the public key
std::stringstream strStream;
strStream << inFile.rdbuf(); // read the file
inFile.close(); // close the file
string gpk = strStream.str(); // str holds the content of the file
Auxiliary::SetPublicKey(gpk, g, gt, X, Y, h, y1, y2, y3);
// elements
element_t T1r, T2r, T3r, T4r;
element_t T5r, T6r, T7r;
element_t c_Hr;
element_t Hr;
element_t Spr;
element_t Smr;
element_t Svr;
// init
element_init_GT(T1r, pairing);
element_init_GT(T2r, pairing);
element_init_GT(T3r, pairing);
element_init_GT(T4r, pairing);
element_init_G1(T5r, pairing);
element_init_G1(T6r, pairing);
element_init_G1(T7r, pairing);
element_init_Zr(Spr, pairing);
element_init_Zr(Smr, pairing);
element_init_Zr(Svr, pairing);
element_init_Zr(Hr, pairing);
element_init_Zr(c_Hr, pairing);
// something to measure
// auto end = high_resolution_clock::now();
// duration<double> diff = end - start; // this is in ticks
// milliseconds d = duration_cast<milliseconds>(diff); // ticks to time
// outfile << diff.count() << "\n";
// std::cout << diff.count() << "s\n";
// Networking Part
Allocators::register_allocator<Dot1Q, AridPDU>(0xa21d);
SnifferConfiguration config; // Comment the config part if you want to test in local
config.set_direction(PCAP_D_IN);
config.set_immediate_mode(true);
// config.set_rfmon(true); // SIOCGIWPRIV: Argument list too long
Sniffer sniffer(argv[1], config);
for (;;)
{
try
{
Packet pkt(sniffer.next_packet());
const AridPDU &arid = pkt.pdu()->rfind_pdu<AridPDU>();
if (arid.pdu_type() == PDU::USER_DEFINED_PDU)
{
vector<uint8_t> buffer = arid.get_buffer();
// Message
const char recv_payload[41] = {
buffer[0], buffer[1], buffer[2], buffer[3], // STATIC ID
buffer[4], buffer[5], buffer[6], buffer[7], // Drone Latitude
buffer[8], buffer[9], buffer[10], buffer[11], // Drone Longitude
buffer[12], buffer[13], buffer[14], buffer[15], // Drone Altitude
buffer[16], buffer[17], buffer[18], buffer[19], // Drone Speed
buffer[20], buffer[21], buffer[22], buffer[23], // Drone COG
buffer[24], buffer[25], buffer[26], buffer[27], // UAS Latitude
buffer[28], buffer[29], buffer[30], buffer[31], // UAS Longitude
buffer[32], buffer[33], buffer[34], buffer[35], // UAS Altitude
buffer[36], buffer[37], buffer[38], buffer[39], // Timestamp
buffer[40] // Emergency Code
};
uint16_t len_sign = buffer[41] * 256 + buffer[42]; // Signature Len
string sign(buffer.begin() + (sizeof(recv_payload) + sizeof(len_sign)), buffer.end()); // Signature
// Only for Test
// for (int32_t i = (sizeof(recv_payload)+sizeof(len_sign)); i < len_sign; ++i)
// {
// printf("%c", buffer[i]);
// }
// compare variables
bool cmp_value_1 = 0;
bool cmp_value_2 = 0;
// auto start = high_resolution_clock::now();
Auxiliary::SignatureFromString(sign, c_Hr, Spr, Smr, Svr, T1r, T2r, T3r, T4r, T5r, T6r, T7r);
Auxiliary::Hash_T1_T2_T3(Hr, T1r, T2r, T3r);
// compute R1'
element_t tmp_1;
element_t tmp_2;
element_t tmp_3;
element_t R1_;
element_init_GT(R1_, pairing);
element_init_GT(tmp_1, pairing);
element_init_GT(tmp_2, pairing);
element_init_GT(tmp_3, pairing);
element_pairing(tmp_1, g, T7r); // e(g, T7)
element_pow_zn(tmp_2, tmp_1, Spr); // e(g, T7)^rp
element_pairing(tmp_1, X, T6r); // e(X, T6)
element_pow_zn(tmp_3, tmp_1, Smr); // e(X, T6)^rm
element_div(R1_, tmp_2, tmp_3); // e(g, T7)^rp / e(X, T6)^rm
element_pairing(tmp_3, X, T5r); // e(X, T5)
element_pow_zn(tmp_3, tmp_3, c_Hr); // e(X, T5)^H
element_div(R1_, R1_, tmp_3); // e(g, T7)^rp / e(X, T6)^rm / e(X, T5)^H ==> e(g, T7)^rp / [e(X, T6)^rm * e(X, T5)^H]
// compute R2'
element_t R2_;
element_init_GT(R2_, pairing);
element_pow_zn(R2_, gt, Svr); // R2 = gt^v
element_pow_zn(tmp_1, T1r, c_Hr); // T1^H
element_div(R2_, R2_, tmp_1); // R2 = gt^v/T1^H
// compute R3'
element_t R3_;
element_init_GT(R3_, pairing);
element_pow_zn(tmp_1, h, Svr); // h^v
element_pow_zn(tmp_2, T2r, c_Hr); // T2^H
element_sub(R3_, tmp_1, tmp_2); // R3 = h^v- T2^H
// compute R4'
element_t R4_;
element_init_GT(R4_, pairing);
element_pow_zn(tmp_1, y1, Svr); // y1^v
element_pow_zn(tmp_2, gt, Smr); // gt^m
element_mul(tmp_3, tmp_1, tmp_2); // y^v * gt^m
element_pow_zn(tmp_1, T3r, c_Hr); // T3^H
element_sub(R4_, tmp_3, tmp_1); // R4 = (y1^v * gt^m) - T3^H
// compute R5'
element_t R5_, tmp_pow, tmp_div;
element_init_GT(R5_, pairing);
element_init_Zr(tmp_pow, pairing);
element_init_GT(tmp_div, pairing);
element_pow_zn(R5_, y2, Svr); // R5 = y2^v
element_pow_zn(tmp_div, y3, Hr); // y3^H
element_pow_zn(tmp_div, tmp_div, Svr); // y3^(H*v)
element_mul(R5_, R5_, tmp_div); // R5 = y2^v * y3^(H*v)
element_pow_zn(tmp_div, T4r, c_Hr); // T4^H
element_div(R5_, R5_, tmp_div); // [y2^v * y3^(H*v)]/T4^H
// check c_H == c_H'
element_t check_c_H;
element_init_Zr(check_c_H, pairing);
Auxiliary::Hash_C(check_c_H, R1_, R2_, R3_, R4_, R5_, g, gt, X, Y, h, y1, y2, y3, (char *)recv_payload, sizeof(recv_payload));
// Auxiliary::Hash_C(check_c_H, R1_, R2_, R3_, R4_, R5_, g, gt, X, Y, h, y1, y2, y3, (char *)mes.c_str(), mes.length());
// check e(T 5 , Y ) == e(g, T 6 )
element_t check_1;
element_init_GT(check_1, pairing);
element_t check_2;
element_init_GT(check_2, pairing);
element_pairing(check_1, T5r, Y);
element_pairing(check_2, g, T6r);
// cmp_value_1
cmp_value_1 = element_cmp(check_c_H, c_Hr); // 0==success
// cmp_value_2
cmp_value_2 = element_cmp(check_1, check_2); // 0==success
if (cmp_value_1 == 0 && cmp_value_2 == 0)
cout << GREEN << "Valid Signature" << RESET << endl;
else
cout << RED << "Signature Not Valid" << RESET << endl;
}
}
catch (malformed_packet &)
{
}
catch (pdu_not_found &)
{
}
}
}