init ratchets

This commit is contained in:
2025-03-09 17:43:20 +01:00
parent 35ff9cb240
commit 744e1fae29
3 changed files with 55 additions and 17 deletions

View File

@@ -5,6 +5,31 @@ import { keccakKDF } from "./kdf.js";
const socket = io();
let secret = null;
let sharedsecret = {};
let dh_ratchets = {};
let sending_ratchets = {};
let receiving_ratchets = {};
function init_ratchets(order, user_pubkey) {
let dh_ratchet = new keccakKDF()
let key_1 = dh_ratchet.init(sharedsecret[user_pubkey], new Uint8Array(0));
let key_2 = dh_ratchet.next(new Uint8Array(0));
dh_ratchets[user_pubkey] = dh_ratchet;
let sending_ratchet = new keccakKDF();
let receiving_ratchet = new keccakKDF();
switch (order) {
case 0:
sending_ratchet.init(key_1, new Uint8Array(0));
receiving_ratchet.init(key_2, new Uint8Array(0));
break;
case 1:
sending_ratchet.init(key_2, new Uint8Array(0));
receiving_ratchet.init(key_1, new Uint8Array(0));
break;
}
sending_ratchets[user_pubkey] = sending_ratchet;
receiving_ratchets[user_pubkey] = receiving_ratchet;
}
socket.on('chat message', (msg, room) => {
console.log(`received: ${msg}`);
@@ -30,10 +55,12 @@ socket.on('key exchange', (user_pubkey, pubkey, part) => {
sharedsecret[user_pubkey] = sharedKey(secret, fromHexString(pubkey));
socket.emit('key exchange', user_pubkey, toHexString(keys.pubkey), 2);
console.log(`shared secret: ${toHexString(sharedsecret[user_pubkey])}`);
init_ratchets(0, user_pubkey)
break;
case 2:
sharedsecret[user_pubkey] = sharedKey(secret, fromHexString(pubkey));
console.log(`shared secret: ${toHexString(sharedsecret[user_pubkey])}`);
init_ratchets(1, user_pubkey)
break;
}
});