-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDecrypt.ino
258 lines (219 loc) · 5.51 KB
/
Decrypt.ino
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
#include <ChaChaPoly.h>
// Adapted from https://platformio.org/lib/show/1168/Crypto
// Max sizes in bytes
#define MAX_PLAINTEXT_LEN 300
#define MAX_AUTHDATA_LEN 32
#define MAX_IV_LEN 16
#define MAX_TAG_LEN 16
// Pager-specific pre-shared key
const uint8_t KEY[32] = {0x12, 0xC0, 0x00, 0xEF, 0x88, 0x06, 0x8E, 0x07,
0x77, 0x11, 0x8C, 0x20, 0xDE, 0xEB, 0x27, 0x02,
0xF2, 0x2A, 0x06, 0x04, 0x2E, 0x35, 0x34, 0xDB,
0xCD, 0x9C, 0xE1, 0xEA, 0xC9, 0x17, 0x5D, 0xE9};
// Struct for encryption info
struct MsgVector {
uint8_t ciphertext[MAX_PLAINTEXT_LEN];
uint8_t authdata[MAX_AUTHDATA_LEN];
uint8_t iv[MAX_IV_LEN];
uint8_t tag[MAX_TAG_LEN];
size_t datasize;
size_t authsize;
size_t ivsize;
size_t tagsize;
};
// ChaCha20-Poly1305 cipher
ChaChaPoly chachapoly;
// Incoming encrypted msg
MsgVector msgVector;
// Generic buffer
byte buffer[MAX_PLAINTEXT_LEN];
bool decrypt(ChaChaPoly *cipher, MsgVector *msg) {
/*
* Decrypts a ChaChaPoly message.
*
* Args:
* cipher: ChaChaPoly cipher
* msg: MsgVector of encrypted msg and associated info
* Returns:
* Was decryption successful?
*/
size_t posn;
memset(buffer, 0x00, sizeof(buffer));
// Initialize cipher
cipher->clear();
if (!cipher->setKey(KEY, 32)) {
Serial.println("Failed to set key");
return false;
}
if (!cipher->setIV(msg->iv, msg->ivsize)) {
Serial.println("Failed to set IV");
return false;
}
// Decrypt data
cipher->addAuthData(msg->authdata, msg->authsize);
cipher->decrypt(buffer, msg->ciphertext, msg->datasize);
// Print decrypted msg
if (cipher->checkTag(msg->tag, msg->tagsize)) {
for (posn = 0; posn < msg->datasize; posn++) {
Serial.print((char)buffer[posn]);
}
Serial.println();
} else {
Serial.println("Failed to authenticate tag");
return false;
}
return true;
}
bool readMsgVectorInput(uint8_t *buf, size_t *bufSize, int len) {
/*
* Reads input for a msgVector field.
*
* Args:
* buf: Input buffer to write into
* bufSize: Address of input buffer size to write into
* len: Max number of bytes to read
* Returns:
* Was reading input successful?
*/
while (!Serial.available()); // block
int inputSize = readSerialHexUntil('\n', buf, len);
if (inputSize < 0) {
Serial.println("Invalid input\n");
return false;
}
*bufSize = inputSize;
printBuffer(buf, inputSize, 0);
return true;
}
int readSerialHexUntil(char term, uint8_t *buf, int len) {
/*
* Reads a hex string into buffer from Serial.
*
* Args:
* delim: Terminating character of hex string
* buf: Buffer to read into
* len: Max number of bytes to read
* Returns:
* Number of bytes read, or -1 if invalid input
*/
int i;
for (i = 0; i < len; i++) {
int b, h;
unsigned long hex;
// 1st hex digit of byte
while (!Serial.available()); // block
b = Serial.read();
if (b < 0 || b == (int)term) { break; }
// Invalid 1st digit; destroy rest of input
if ((h = htoi(b)) < 0) {
while (b != (int)term) {
b = Serial.read();
}
i = -1;
break;
}
hex = h * 16L;
// 2nd hex digit of byte
while (!Serial.available()); // block
b = Serial.read();
// Invalid byte or invalid 2nd digit; destroy rest of input
if (b < 0 || b == (int)term || (h = htoi(b)) < 0) {
while (b >= 0 && b != (int)term) {
b = Serial.read();
}
i = -1;
break;
}
hex += h;
buf[i] = hex;
}
destroySerialBuffer();
return i;
}
void printBuffer(uint8_t *buf, int len, char delim) {
/*
* Prints buffer as hex.
*
* Args:
* buf: Buffer to print
* len: Number of bytes to print
* delim: Delimiter between bytes
*/
for (int i = 0; i < len; i++) {
if (buf[i] < 0x10) {
Serial.print('0');
}
Serial.print(buf[i], HEX);
if (delim && i < len - 1) {
Serial.print(delim);
}
}
Serial.println();
}
void destroySerialBuffer() {
/*
* Destroys rest of input buffer.
*/
while (Serial.available()) {
Serial.read();
}
}
int htoi(int ch) {
/*
* For a hex digit, converts ASCII representation to (decimal) int.
* e.g., htoi('A') == 11
*
* Args:
* ch: Hex digit as ASCII
* Returns:
* Hex digit as int, or -1 if invalid hex
*/
if (ch >= '0' && ch <= '9') {
return ch - '0';
}
if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
}
if (ch >= 'a' && ch <= 'f') {
return ch - 'a' + 10;
}
return -1;
}
void setup() {
/*
* Sets up sketch.
*/
Serial.begin(9600);
Serial.println("Starting pager...\n");
}
void loop() {
/*
* Loops sketch.
*/
Serial.println(">>> Incoming encrypted message...");
// Input: encrypted message
Serial.print("Enter msg: ");
if (!readMsgVectorInput(msgVector.ciphertext, &msgVector.datasize, MAX_PLAINTEXT_LEN)) {
return;
}
// Input: additional data
Serial.print("Enter AD: ");
if (!readMsgVectorInput(msgVector.authdata, &msgVector.authsize, MAX_AUTHDATA_LEN)) {
return;
}
// Input: initialization vector
Serial.print("Enter IV: ");
if (!readMsgVectorInput(msgVector.iv, &msgVector.ivsize, MAX_IV_LEN)) {
return;
}
// Input: tag
Serial.print("Enter tag: ");
if (!readMsgVectorInput(msgVector.tag, &msgVector.tagsize, MAX_TAG_LEN)) {
return;
}
// Decrypt message
Serial.println("<<< Decrypting message...");
decrypt(&chachapoly, &msgVector);
Serial.println();
destroySerialBuffer();
}