-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockencrypt.js
271 lines (226 loc) · 8.22 KB
/
lockencrypt.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
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
var fs = require('fs');
var crypto = require('crypto');
var AWS = require('aws-sdk');
//TODO: move to config.
var key = 'alias/lockservice';
var configOpt =
{
credentialProvider: new AWS.CredentialProviderChain(),
region: 'us-east-1'
};
AWS.config = new AWS.Config(configOpt);
var kms = new AWS.KMS();
var IVSIZE = 16;
var CRYPTO_PAD_SIZE = 16;
var BLOCKSIZE = 16;
var MAXMSGSIZE = (5 * 16) - 1;
var SHA256_LENGTH = 32;
function DeviceEncrypt(userId, lockId) {
this.lockId = lockId;
this.userId = userId;
}
// do not alter this, if the format changes than won't be able to
// retrieve encryption key, the encrypt context is based on this format.
function getKeyFileName(userId, lockId) {
var userclean = userId.replace('|', '-');
console.log(userclean);
var keyName = userclean + '-' + lockId;
return keyName;
}
function getKeyFilePath(keyName) {
return './data/keys/' + keyName;
}
function getCiphers(devEnc, callback) {
var keyFileName=getKeyFileName(devEnc.userId, devEnc.lockId);
var keyFile = getKeyFilePath(keyFileName);
console.log('reading key file:', keyFile);
fs.readFile(keyFile, function(fileerr, buffer) {
console.log('read key file returned');
if(fileerr) {
console.error('failure to read key file:',keyFile);
callback({error: 'missing key file'});
} else {
var params = {
EncryptionContext: {device: keyFileName },
CiphertextBlob: buffer
};
console.log('decrypting key with params: ', params);
kms.decrypt(params, function(decryptErr, data) {
console.log('returned from key decrypt, error: ', decryptErr);
var keydata = data.Plaintext;
if(decryptErr) {
console.error('decyrpt key error: ', decryptErr, ' keyFileName: ', keyFileName );
callback({error: 'Decrpyt key failure'});
}else {
console.log('got encryption key', keydata);
crypto.randomBytes(IVSIZE, function(randerr, iv) {
if(randerr) {
console.eror('error gen IV: ', randerr );
callback({error: 'failed to generate IV'});
} else {
console.log('creating ciphers');
devEnc.keydata = keydata;
devEnc.iv = iv;
devEnc.cipher = crypto.createCipheriv('aes-128-cbc', keydata, iv);
callback(null);
}
});
}
});
}
});
}
function encryptStrData(devEncrypt, strData, callback) {
var encodedStrLen = Buffer.byteLength(strData, 'utf-8');
if(encodedStrLen > MAXMSGSIZE) {
callback({error: 'exceeded max msg size'});
} else {
var bufLen = encodedStrLen + BLOCKSIZE + 1;
var cipher = devEncrypt.cipher;
var data = new Buffer(bufLen);
//using Explicit Initialization Vector, first block will be garbled.
data.fill(0,0,BLOCKSIZE);
data.writeUInt8(encodedStrLen, BLOCKSIZE);
data.write(strData, BLOCKSIZE+1);
console.log('buffer size: ', data.length);
var encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
console.log('encrypt size; ', encrypted.length);
var hash = getSignature(devEncrypt.keydata, encrypted);
var encryptAndSign = Buffer.concat([encrypted, hash]);
callback(null, encryptAndSign);
}
}
function getSignature(key, databuffer) {
var hmac= crypto.createHmac("sha256", key);
hmac.write(databuffer);
hmac.end();
var hash = hmac.read();
return hash;
}
function checkSignature(key, databuffer, signature) {
var hash = getSignature(key, databuffer);
if(hash.length == signature.length) {
for(var i = 0; i < hash.length; i++) {
if(hash[i] != signature[i]) {
console.log('signatures do not match');
console.log('msg:', signature, ' calc:', hash, ' buf len:', databuffer.length);
return false;
}
}
} else {
console.log('signature mismatch, hash len: ', hash.length, ' sig len: ', signature.length);
return false;
}
console.log('signatures match');
return true;
}
function validateMsgSig(key, buffer, callback) {
var signdatalen = buffer.length - SHA256_LENGTH;
var cipher = buffer.slice(0, signdatalen);
var sig = buffer.slice(signdatalen);
if(checkSignature(key, cipher, sig)) {
callback(null, cipher);
} else {
callback({error: "signature validation failed"});
}
}
function decryptBuffer(devEncrypt, ciphertext, callback) {
// using Explicit Initialization Vector, first block will be garbled, don't need to pass iv.
// use random iv.
try {
var decipher = crypto.createDecipheriv('aes-128-cbc', devEncrypt.keydata, devEncrypt.iv);
var decrypted = [decipher.update(ciphertext)];
decrypted.push(decipher.final());
var finaldecrypt = Buffer.concat(decrypted);
var plaintextLen = finaldecrypt.readUInt8(BLOCKSIZE);
var plaintext = finaldecrypt.toString('utf8', BLOCKSIZE+1);
console.log('decrypt size:', plaintextLen, ' decrypt text:', plaintext);
callback(null, plaintext);
} catch(err) {
console.error('exception on decrypt:', err);
callback(err);
}
}
DeviceEncrypt.prototype.encryptAndSign = function(string, callback) {
var self = this;
if(self.cipher === undefined) {
console.log('getting ciphers');
getCiphers(self, function(cipherErr) {
if(cipherErr) {
console.error('err getting cipher', cipherErr);
callback(cipherErr);
}else{
console.log('got cipher, encrypting');
encryptStrData(self,string, callback);
}
});
}else {
encryptStrData(string, callback);
}
};
DeviceEncrypt.prototype.decrypt = function(buffer, callback) {
var self = this;
if(self.keydata === undefined) {
getCiphers(self, function(cipherErr) {
if(cipherErr) {
callback(cipherErr);
}else{
decryptBuffer(self, buffer, callback);
}
});
}else {
decryptBuffer(self, buffer, callback);
}
};
DeviceEncrypt.prototype.validateSig = function (buffer, callback) {
var self = this;
if(self.keydata === undefined) {
getCiphers(self, function(cipherErr) {
if(cipherError) {
callback(cipherErr);
} else{
validateMsgSig(self.keydata, buffer, callback);
}
});
} else {
validateMsgSig(self.keydata, buffer, callback);
}
};
DeviceEncrypt.prototype.generateLockKey = function(callback) {
var self = this;
var keyFileName = getKeyFileName(self.userId, self.lockId);
var keyParams = {
KeyId: key,
EncryptionContext: { device: keyFileName },
KeySpec: 'AES_128'
};
console.log('calling kms.generateDataKey');
kms.generateDataKey(keyParams, function(keyerr, keydata) {
if(keyerr) {
console.log('error generating key:' + keyerr);
console.log('stack: ' + keyerr.stack);
callback(false, null);
} else {
console.info('obtained data key, saving to file:', keydata);
var keyFile = getKeyFilePath(keyFileName);
fs.writeFile(keyFile, keydata.CiphertextBlob, function(fileerr){
if(fileerr) {
console.log('error saving key: ' + fileerr);
console.log('stack: ' + fileerr.stack);
callback(filerr);
} else {
callback(null, keydata.Plaintext);
}
});
}
});
};
DeviceEncrypt.prototype.removeLockKey = function(callback) {
var self = this;
var keyFileName = getKeyFileName(self.userId, self.lockId);
var keyFile = getKeyFilePath(keyFileName);
fs.unlink(keyFile, function(deleteErr) {
if(deleteErr) console.log('key file deletion error: ', deleteErr);
});
};
module.exports = DeviceEncrypt;