-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_serial.cpp
More file actions
173 lines (145 loc) · 3.69 KB
/
secure_serial.cpp
File metadata and controls
173 lines (145 loc) · 3.69 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
#include "uECC.h"
#include "secure_serial.h"
static int random_uECC(uint8_t *dest, unsigned size) {
while (size) {
uint8_t val = 0;
for (unsigned i = 0; i < 8; ++i) {
int init = analogRead(0);
int count = 0;
while (analogRead(0) == init) ++count;
if (!count) val = (val << 1) | (init & 0x01);
else val = (val << 1) | (count & 0x01);
}
*dest = val;
++dest;
--size;
}
return 1;
}
void print_array(uint8_t* arr, int len) {
for (int i=0; i<len; ++i) {
Serial.print(arr[i]);
Serial.print(",");
}
Serial.println();
}
void secure_serial::begin(uint8_t rx, uint8_t tx, bool first_device) {
LOG_START
serial = new SoftwareSerial(rx, tx);
curve = uECC_secp256r1();
first = first_device;
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
serial->begin(115200);
while (!(*serial));
uECC_set_rng(&random_uECC);
uECC_make_key(public_key, private_key, curve);
print_array(public_key, key_size * 2);
print_array(private_key, key_size);
LOG_END
}
void secure_serial::recv_to_serial() {
LOG_START
while (serial->available()) {
Serial.write((char) serial->read());
}
LOG_END
}
void secure_serial::send(const char* msg) {
LOG_START
serial->print(msg);
LOG_END
}
void secure_serial::recv_line(char* dst, int maxlen) {
int l=0;
char ch = '\0';
while (ch != '\n' && l < maxlen) {
ch = serial->read();
dst[l++] = ch;
}
}
void secure_serial::send_from_serial() {
LOG_START
while (Serial.available())
serial->print((char) Serial.read());
recv_to_serial();
LOG_END
}
int secure_serial::available() {
return serial->available();
}
void secure_serial::recv_pub_key() {
LOG_START
send(CMDRECP);
WAIT(CMDRECA);
uint8_t recv_pub_key[key_size * 2];
uint8_t sz = serial->readBytes(recv_pub_key, key_size * 2);
#ifdef DEBUG
Serial.print("[DEBUG] recv:");
Serial.println(sz);
#endif
uECC_shared_secret(recv_pub_key, private_key, shared_secret, curve);
LOG_END
}
void secure_serial::send_hello() {
LOG_START
send(CMDHELLO1);
LOG_END
}
void secure_serial::send_hello2() {
LOG_START
send(CMDHELLO2);
LOG_END
}
void secure_serial::send_pub_key() {
LOG_START
send(CMDRECA);
for (uint8_t i=0; i<key_size; ++i)
serial->write(public_key[i]);
if (!first) send(CMDRECP);
LOG_END
}
void secure_serial::initial_sequence() {
LOG_START
if (first) {
send_hello();
recv_pub_key();
WAIT(CMDRECP);
send_pub_key();
} else {
WAIT(CMDHELLO1);
send_hello2();
WAIT(CMDRECP);
send_pub_key();
recv_pub_key();
}
LOG_END
}
void secure_serial::send_msg(uint8_t* message, int len) {
LOG_START
send(CMDMSG);
serial->print(len);
for (int i = 0; i < len; i++) {
serial->print((char)(message[i] ^ shared_secret[i%key_size]));
}
LOG_END
}
void secure_serial::recv_msg(uint8_t* message, int len) {
LOG_START
WAIT(CMDMSG);
int r = serial->read();
serial->readBytes(message, r);
for (int i=0; i<r; ++i) {
message[i] ^= shared_secret[i % key_size];
}
LOG_END
}
void secure_serial::set_public_key(const char *key) {
memcpy(public_key, key, key_size * 2);
}
void secure_serial::set_private_key(const char *key) {
memcpy(private_key, key, key_size * 2);
}
void secure_serial::set_shared_secret(const char *secret) {
memcpy(shared_secret, secret, key_size);
}