Skip to content

Commit

Permalink
Derive pairwise secret from DH output and room secret
Browse files Browse the repository at this point in the history
  • Loading branch information
ayyghost committed Nov 16, 2024
1 parent 0cf0622 commit 362afa8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion js/etc/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Cryptodog.keys = function () { };

return {
roomId,
roomSecret,
roomSecret: new Uint8Array(roomSecret)
};
};
}());
3 changes: 2 additions & 1 deletion js/etc/multiParty.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ Cryptodog.multiParty = function () { };
Cryptodog.multiParty.ecdhWorker.postMessage({
theirName: sender,
theirPublicKey: publicKey,
ourPrivateKey: Cryptodog.me.mpPrivateKey
ourPrivateKey: Cryptodog.me.mpPrivateKey,
roomSecret: Cryptodog.me.roomSecret,
});
buddy.mpPublicKey = publicKey;
buddy.mpFingerprint = Cryptodog.multiParty.genFingerprint(sender);
Expand Down
18 changes: 15 additions & 3 deletions js/workers/ecdh.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
importScripts('../lib/crypto-js.js', '../lib/bigint.mod.js', '../lib/elliptic.js');

onmessage = function (event) {
let sharedSecret = genSharedSecret(event.data.theirPublicKey, event.data.ourPrivateKey);
let sharedSecret = genSharedSecret(event.data.theirPublicKey, event.data.ourPrivateKey, event.data.roomSecret);
postMessage({ theirName: event.data.theirName, secretKey: sharedSecret });
};

// convert a Uint8Array to a WordArray
const toWordArray = function (uint8Arr) {
var wa = [], i;
for (i = 0; i < uint8Arr.length; i++) {
wa[(i / 4) | 0] |= uint8Arr[i] << (24 - 8 * i);
}
return CryptoJS.lib.WordArray.create(wa, uint8Arr.length);
};

// Generate shared secrets
// First 256 bytes are for encryption, last 256 bytes are for HMAC.
// Represented as WordArrays
function genSharedSecret(theirPublicKey, ourPrivateKey) {
function genSharedSecret(theirPublicKey, ourPrivateKey, roomSecret) {
// I need to convert the BigInt to WordArray here. I do it using the Base64 representation.
var sharedSecret = CryptoJS.SHA512(
CryptoJS.enc.Base64.parse(
Expand All @@ -18,9 +27,12 @@ function genSharedSecret(theirPublicKey, ourPrivateKey) {
)
)
);
// concat the room secret to the hashed DH output, and hash that to get the final shared secret
const mixed = sharedSecret.concat(toWordArray(roomSecret));
sharedSecret = CryptoJS.SHA512(mixed);

return {
message: sharedSecret.words.slice(0, 8),
hmac: sharedSecret.words.slice(8, 16)
};
};
};

0 comments on commit 362afa8

Please sign in to comment.