-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypted_communication.cpp
More file actions
429 lines (361 loc) · 8.92 KB
/
Copy pathencrypted_communication.cpp
File metadata and controls
429 lines (361 loc) · 8.92 KB
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
424
425
426
427
428
429
#include <Arduino.h>
void setup() {
init(); // Initialize serial
pinMode(13, INPUT); // Determine Arduino 1 and 2
Serial.begin(9600); // User input
Serial3.begin(9600); // Arduino input
}
bool wait_on_serial3( uint8_t nbytes, long timeout ) {
/*
Looks for available bytes until it timesout
Argument:
nbytes - number of required bytes
timeout - value of timeout
*/
unsigned long deadline = millis() + timeout;
while (Serial3.available()<nbytes && (timeout < 0 || millis() < deadline)) {
delay(1);
}
return Serial3.available()>=nbytes;
}
void uint32_to_serial3(uint32_t num) {
/*
Writes a 32 bit to Serial3 in 4 bytes
Argument:
num - the byte to write
*/
Serial3.write((char) (num >> 0));
Serial3.write((char) (num >> 8));
Serial3.write((char) (num >> 16));
Serial3.write((char) (num >> 24));
}
uint32_t uint32_from_serial3() {
/*
Reads a 32 bit from Serial3 to Serial
*/
uint32_t num = 0;
num = num | ((uint32_t) Serial3.read()) << 0;
num = num | ((uint32_t) Serial3.read()) << 8;
num = num | ((uint32_t) Serial3.read()) << 16;
num = num | ((uint32_t) Serial3.read()) << 24;
return num;
}
bool checkPrime(uint32_t n) {
/*
Checks if n is a prime number
Arguments:
n - number to check
Returns:
bool of whether it is a prime (true) or not (false)
*/
if (n == 1 || n == 2) {
return false;
} else {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
uint32_t mulmod(uint32_t byte1, uint32_t byte2, uint32_t m) {
/*
Modular Multiplication
Arguments:
byte1 - muliplier
byte2 - multiplicant
m - Given Modulus
Returns:
result - product of byte1 and byte2
*/
uint32_t result = 0;
for (uint32_t i = 0; i<32 ; i++) {
if (byte1 & 1){
result = (result+byte2)%m;
}
byte2 = (byte2*2)%m;
byte1 = byte1 >> 1; // Halves byte1
}
return result;
}
uint32_t powMod(uint32_t byte, uint32_t Key, uint32_t Mod) {
/*
Modular Power
Arguments:
byte: base
Key: exponent
Mod: Modulus
Returns:
newByte: The result of the power
*/
uint32_t newByte = 1;
while (Key > 0) {
if (Key & 1) {
newByte = mulmod(newByte, byte, Mod); // byte times newByte
}
byte = mulmod(byte, byte, Mod); // Byte squared
Key = Key >> 1; // Halves Key
}
return newByte;
}
enum ServerState { Start, Listen, WaitingForKey, WaitingForAck, DataExchange };
uint32_t negativeMod(int32_t x, uint32_t mod) {
/*
Computes the modulus of a negative input
Arguments:
x - a negative number
mod - the modulus
Returns:
x % mod
*/
uint32_t z;
z = -x/mod +1;
return x + z*mod;
}
uint32_t phiN(uint32_t p, uint32_t q) {
/*
computes phiN
*/
return (p-1)*(q-1);
}
uint32_t randNum(uint32_t bits) {
/*
Generates a random number
Arguments:
bits - the number of bits of the generated number
Returns:
randomByte - the random number
*/
uint32_t randomByte = 0;
for (int i = 0; i < bits-1; i++) {
delay(5);
randomByte |= ((uint32_t)(analogRead(A1) & 1) << i);
}
randomByte = (randomByte | ((uint32_t)1 << (bits-1)));
return randomByte;
}
uint32_t gcd(uint32_t a, uint32_t b) {
/*
Computed Greatest Common Demoninator
Arguments:
a - one number
b - another number
Returns:
the greatest common demoninator of a and b
*/
while (b > 0) {
a = a % b;
uint32_t tmp = a;
a = b;
b = tmp;
}
return a;
}
int32_t extendedEuclidean(uint32_t e, uint32_t n) {
/*
Generates d from given e and n
Arguments:
e - The public key
n - modulus
Returns:
the private key (d)
*/
int32_t r[40];
int32_t q;
int32_t s[40];
int32_t t[40];
int32_t i = 1;
//Setting the intial matrix
r[0] = e;
r[1] = n;
s[0] = 1;
s[1] = 0;
t[0] = 0;
t[1] = 1;
while (r[i] > 0) {
q = r[i-1]/r[i];
r[i+1] = r[i-1] - q * r[i];
s[i+1] = s[i-1] - q * s[i];
t[i+1] = t[i-1] - q * t[i];
i++;
}
return s[i-1];
}
uint32_t generatePrime(uint32_t length) {
/*
Generates a prime
Argument:
length - the number of bits of the prime
Returns:
the prime
*/
bool prime = false;
uint32_t primeByte;
while (prime == false) {
primeByte = randNum(length);
prime = checkPrime(primeByte);
}
return primeByte;
}
void generateKeys(uint32_t& e, int32_t& d, uint32_t& n) {
/*
Generates Keys
Arguments:
e, d, n = placeholders for its public key, private key, and modulus
*/
uint32_t p = 0, q = 0, phi = 0;
(int32_t) d;
e = generatePrime(15);
q = generatePrime(16);
p = generatePrime(15);
n = p * q;
phi = phiN(p,q);
while (gcd(e, phi) != 1) {
Serial.println("e failed!");
Serial.println("Genernating new e...");
e = generatePrime(16);
}
d = extendedEuclidean(e, phi);
if (d < 0 or d >= phi) {
d = negativeMod(d, phi);
}
}
void communicate(uint32_t e, uint32_t m, uint32_t d, uint32_t n) {
/*
Encryptes and sends bytes and Decryptes recieved bytes
Arguments:
e: otherPublicKey
m: otherModulus
d: ownPrivateKey
n: ownModulus
*/
uint32_t en_byte; // Encrypte Byte
uint8_t de_byte; // Dencrypte Byte
while (true) {
if (Serial.available() > 0 ) { // User writes input
uint32_t readByte = Serial.read();
Serial.print(char(readByte)); // Write to monitor
en_byte = powMod(char(readByte), e, m); // Encrypt
uint32_to_serial3(en_byte); // Write encrypt value to the other Arduino
if(readByte == '\r') { // If user inputs return
Serial.write('\n');
en_byte = powMod('\n', e, m);
uint32_to_serial3(en_byte);
}
} else if (Serial3.available() > 3) { // Arduino recieving input from other Arduino
en_byte = uint32_from_serial3(); // Read input from Arduino
de_byte = powMod(en_byte, d, n); // Dencrypt
Serial.write(de_byte); // Write Dencrypt value
}
}
}
void synchronize(){
/*
This is where the client and the server declares their keys and mods
They then hand shake to synchronize
Then goes to next function where they communicate
*/
uint32_t e = 0, n = 0, m = 0, e2 = 0; // e and e2 are public keys, n and m are mods
int32_t d = 0; // Private key
uint32_t timeout = 1000;// should put with initial variables
ServerState state = Listen;
if (digitalRead(13) == HIGH) { // This will be client
state = Start;
Serial.println("");
Serial.println("-------------Client----------------");
Serial.println("Genernating Keys...");
generateKeys(e,d,n); // Generates keys
Serial.println("Keys have been Generated!");
while (true) {
if (state == Start){ // Outputs ckey cmod
Serial.println("Waiting for connection...");
Serial3.write('C'); // Sends connection request
uint32_to_serial3(e); // Key
uint32_to_serial3(n); // Mod
state = WaitingForAck;
}
else if (state == WaitingForAck){
if (wait_on_serial3(9, timeout)){
if (Serial3.read() == 'A'){ // Recives Acknolwdgment from server
e2 = uint32_from_serial3(); // Key
m = uint32_from_serial3(); // Mod
Serial3.write('A'); // Sends Acknowledgement
state = DataExchange;
Serial.println("Recived connection...");
}
}
else {
state = Start; // Timeout
}
}
else if (state = DataExchange){
Serial.println("Chat is open:");
delay(500);// ensure all bytes are written
// Clears Serial3 before chatting
while (Serial3.available() > 0) {
Serial3.read();
}
communicate(e2,m,d,n); // Chat
}
}
} else { // This will be server
state = Listen;
Serial.println("");
Serial.println("------------Server-------------");
Serial.println("Genernating Keys...");
generateKeys(e,d,n); // Generates keys
Serial.println("Keys have been Generated!");
while (true){
if (state == Listen){ // Recive connection request
Serial.println("Listening...");
if (wait_on_serial3(9, 1000)){
if (Serial3.read() == 'C'){ // 'C' and Key and Mod
Serial.println("Recived connection...");
state = WaitingForKey;
}
}
}
else if (state == WaitingForKey){ // Recieves keys
Serial3.write('A'); // Sends Acknowledgement
uint32_to_serial3(e); // Key
uint32_to_serial3(n); // Mod
e2 = uint32_from_serial3(); // Key
m = uint32_from_serial3(); // Mod
state = WaitingForAck;
}
else if (state == WaitingForAck){ // Check for acknowledgement
if (wait_on_serial3(1, 1000)){ // If it recives something
char input = Serial3.read();
if (input == 'C' && Serial3.available() >= 8){ // 'C' and Key and Mod
Serial.println("Recived connection...");
state = WaitingForKey;
}
else if (input == 'A') { // Acknowledgement
Serial.println("Recived acknowledgement...");
state = DataExchange;
}
}
else {
state = Listen; // Timeout
}
}
else if (state == DataExchange){ // Initialize communication
Serial.println("Chat is open:");
delay(500); // ensure all bytes are written
// clears out Serial3 before chatting
while (Serial3.available() > 0) {
Serial3.read();
}
communicate(e2,m,d,n); // Chat
}
}
}
}
int main() {
setup();
synchronize(); // Key generation and handshake
Serial.flush();
Serial3.flush();
return 0;
}