-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.js
89 lines (72 loc) · 2.98 KB
/
serial.js
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
const {SerialPort, PacketLengthParser} = require("serialport");
const SerialPortMock = require("serialport");
const crypto = require("crypto");
function joinBuffers(buffers, delimiter = ' ') {
let d = Buffer.from(delimiter);
return buffers.reduce((prev, b) => Buffer.concat([prev, d, b]));
}
//hc12 module config
const hc12 = {
interface: '/dev/serial0',
baudRate: 9600
};
const token = "aaaaa";
const original_iv = [0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00]
const aes_ctx = {
algorithm: 'aes-256-cbc',
key: crypto.createHash('sha256').update(token).digest(),
iv: Buffer.from(original_iv),
sessionKey: null
};
console.log(aes_ctx);
port = new SerialPort({
path: hc12.interface, baudRate: hc12.baudRate, autoOpen: false
});
const parser = port.pipe(new PacketLengthParser({
delimiter: 0xaa,
packetOverhead: 3,
lengthBytes: 2,
lengthOffset: 1,
maxLen: 1024
}));
globals = {SerialPort, SerialPortMock, port};
init = () => {
port.on('error', console.log);
port.open();
parser.on('data', data => {
let plaintext = data.slice(3, data.length);
console.log("plaintext:", plaintext);
if(aes_ctx.sessionKey == null) {
console.log("initializing Session:");
let decipher = crypto.createDecipheriv(aes_ctx.algorithm, aes_ctx.key, aes_ctx.iv);
decipher.setAutoPadding(false);
let sessionIv = decipher.update(plaintext);
sessionIv += decipher.final();
console.log("sessionIv:", sessionIv);
//generate Sessionkey
b = Buffer.concat([aes_ctx.key, Buffer.from(sessionIv)]);
aes_ctx.sessionKey = crypto.createHash('sha256').update(b).digest();
console.log("SessionKey:", aes_ctx.sessionKey);
//send OK Flag
let cipher = crypto.createCipheriv(aes_ctx.algorithm, aes_ctx.sessionKey, aes_ctx.iv);
let cipherText = cipher.update("OK");
cipherText = Buffer.from(cipherText + cipher.final());
console.log("encryptedMessage:", cipherText);
port.write(cipherText);
} else {
let decipher = crypto.createDecipheriv(aes_ctx.algorithm, aes_ctx.sessionKey, aes_ctx.iv);
decipher.setAutoPadding(false);
let decryptedMessage = decipher.update(plaintext);
decryptedMessage += decipher.final();
console.log("decryptedMessage:", decryptedMessage);
}
});
}
//47 66 74 53 67 51 51 64 59 4c 46 44 48 6c 47 00
//77 56 44 63 57 61 61 54 69 7c 76 74 78 5c 77 30
//ed 96 8e 84 0d 10 d2 d3 13 a8 70 bc 13 1a 4e 2c 31 1d 7a d0 9b df 32 b3 41 81 47 22 1f 51 a6 e2 6c 68 74 4d 75 43 36 52 41 36 77 48 41 32 33 00
//ed 96 8e 84 0d 10 d2 d3 13 a8 70 bc 13 1a 4e 2c 31 1d 7a d0 9b df 32 b3 41 81 47 22 1f 51 a6 e2 6c 68 74 4d 75 43 36 52 41 36 77 48 41 32 33 00
init();