Skip to content

Commit 7bb4c28

Browse files
committed
Adding key logging
1 parent 9a90c6d commit 7bb4c28

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

nodejs/ece.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ var SHA_256_LENGTH = 32;
1414
var MODE_ENCRYPT = 'encrypt';
1515
var MODE_DECRYPT = 'decrypt';
1616

17+
var keylog;
18+
if (process.env.ECE_KEYLOG === '1') {
19+
keylog = function(m, k) {
20+
console.warn(m + ' [' + k.length + ']: ' + base64.encode(k));
21+
};
22+
} else {
23+
keylog = function() {};
24+
}
25+
1726
function HMAC_hash(key, input) {
1827
var hmac = crypto.createHmac('sha256', key);
1928
hmac.update(input);
@@ -113,9 +122,12 @@ function extractSecretAndContext(params, mode) {
113122
if (!result.secret) {
114123
throw new Error('Unable to determine key');
115124
}
125+
keylog('secret', result.secret);
126+
keylog('context', result.context);
116127
if (params.authSecret) {
117128
result.secret = HKDF(base64.decode(params.authSecret), result.secret,
118-
info('auth', new Buffer(0)), SHA_256_LENGTH);
129+
info('auth', new Buffer(0)), SHA_256_LENGTH);
130+
keylog('authsecret', result.secret);
119131
}
120132
return result;
121133
}
@@ -128,6 +140,8 @@ function deriveKeyAndNonce(params, mode) {
128140
key: HKDF_expand(prk, info('aesgcm128', s.context), KEY_LENGTH),
129141
nonce: HKDF_expand(prk, info('nonce', s.context), NONCE_LENGTH)
130142
};
143+
keylog('key', result.key);
144+
keylog('nonce base', result.nonce);
131145
return result;
132146
}
133147

@@ -149,15 +163,18 @@ function generateNonce(base, counter) {
149163
var x = ((m ^ counter) & 0xffffff) +
150164
((((m / 0x1000000) ^ (counter / 0x1000000)) & 0xffffff) * 0x1000000);
151165
nonce.writeUIntBE(x, nonce.length - 6, 6);
166+
keylog('nonce' + counter, nonce);
152167
return nonce;
153168
}
154169

155170
function decryptRecord(key, counter, buffer, padSize) {
171+
keylog('decrypt', buffer);
156172
var nonce = generateNonce(key.nonce, counter);
157173
var gcm = crypto.createDecipheriv(AES_GCM, key.key, nonce);
158174
gcm.setAuthTag(buffer.slice(buffer.length - TAG_LENGTH));
159175
var data = gcm.update(buffer.slice(0, buffer.length - TAG_LENGTH));
160176
data = Buffer.concat([data, gcm.final()]);
177+
keylog('decrypted', data);
161178
padSize = padSize || PAD_SIZE
162179
var pad = data.readUIntBE(0, padSize);
163180
if (pad + padSize > data.length) {
@@ -206,6 +223,7 @@ function decrypt(buffer, params) {
206223
}
207224

208225
function encryptRecord(key, counter, buffer, pad, padSize) {
226+
keylog('encrypt', buffer);
209227
pad = pad || 0;
210228
var nonce = generateNonce(key.nonce, counter);
211229
var gcm = crypto.createCipheriv(AES_GCM, key.key, nonce);
@@ -220,7 +238,9 @@ function encryptRecord(key, counter, buffer, pad, padSize) {
220238
if (tag.length !== TAG_LENGTH) {
221239
throw new Error('invalid tag generated');
222240
}
223-
return Buffer.concat([epadding, ebuffer, tag]);
241+
var encrypted = Buffer.concat([epadding, ebuffer, tag]);
242+
keylog('encrypted', encrypted);
243+
return encrypted;
224244
}
225245

226246
/**

nodejs/encrypt-dh.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@ if (process.argv.length < 4) {
1010
process.exit(2);
1111
}
1212

13-
var sender = crypto.createECDH('prime256v1');
14-
sender.generateKeys();
15-
ece.saveKey('keyid', sender, "P-256");
16-
17-
var salt = base64.encode(crypto.randomBytes(16));
18-
1913
var params = {
2014
keyid: 'keyid',
21-
dh: process.argv[2],
22-
salt: salt
15+
dh: process.argv[2]
2316
};
2417

2518
if (process.argv.length > 4) {
@@ -29,9 +22,23 @@ if (process.argv.length > 4) {
2922
});
3023
}
3124

25+
var sender = crypto.createECDH('prime256v1');
26+
sender.generateKeys();
27+
if (params.senderPrivate) {
28+
sender.setPrivateKey(base64.decode(params.senderPrivate));
29+
}
30+
if (params.senderPublic) {
31+
sender.setPublicKey(base64.decode(params.senderPublic));
32+
}
33+
ece.saveKey('keyid', sender, "P-256");
34+
35+
if (!params.salt) {
36+
params.salt = base64.encode(crypto.randomBytes(16));
37+
}
38+
39+
3240
console.log("Params: " + JSON.stringify(params, null, 2));
3341
var result = ece.encrypt(base64.decode(process.argv[3]), params);
3442

35-
console.log("Salt: " + salt);
3643
console.log("Public Key: " + base64.encode(sender.getPublicKey()));
3744
console.log("Encrypted Message: " + base64.encode(result));

0 commit comments

Comments
 (0)