add time to associated data

This commit is contained in:
Sam Hadow 2025-03-18 11:05:16 +01:00
parent 4b7d9f363d
commit fbabcf36d6

View File

@ -2,7 +2,7 @@ import { genKeys, sharedKey } from "./ecdh.js";
import { keccakAEAD } from "./aead.js";
import { keccakKDF } from "./kdf.js";
import { render_room, render_rooms_wrapper } from "./rooms.js";
import { fromHexString, toHexString, generateRandomUint8Array } from "./arrayutils.js";
import { fromHexString, toHexString, generateRandomUint8Array, concatUint8Arrays } from "./arrayutils.js";
const socket = io();
let secret = null;
@ -44,7 +44,7 @@ socket.on('chat message', (msg, room, tag_received, iv, nonce, pubkey_received)
render_room(room, pubkey_received);
messages = document.getElementById(`messages-${room}`);
}
const associated_data = fromHexString(Array.from((document.getElementById('pubkey')).classList).find(className => className.startsWith('key-')).replace('key-', ''));
const associated_data = concatUint8Arrays(fromHexString(Array.from((document.getElementById('pubkey')).classList).find(className => className.startsWith('key-')).replace('key-', '')), getRoundedUTCTime());
const pubkey = Array.from(messages.classList).find(className => className.startsWith('key-')).replace('key-', '');
let {plaintext, tag} = decrypt_message(msg, pubkey, fromHexString(iv), fromHexString(nonce), associated_data);
if (tag === tag_received && pubkey == pubkey_received) {
@ -113,7 +113,7 @@ function encrypt_message(message, user_pubkey) {
let encoded_msg = (new TextEncoder()).encode(message);
let iv = generateRandomUint8Array();
let nonce = generateRandomUint8Array();
let associated_data = fromHexString(user_pubkey);
let associated_data = concatUint8Arrays(fromHexString(user_pubkey), getRoundedUTCTime());
let {cipher, tag} = keccakAEAD.encrypt(encryption_key, encoded_msg, iv, associated_data, nonce);
return {cipher: toHexString(cipher),
tag: toHexString(tag),
@ -161,3 +161,21 @@ export async function reconnectSocket() {
console.log("Socket reconnected.");
}, 100);
}
function getRoundedUTCTime() {
const roundedTime = Math.round(Date.now() / 60000) * 60000;
const date = new Date(roundedTime);
const year = date.getUTCFullYear();
const month = date.getUTCMonth();
const day = date.getUTCDate();
const yearHigh = Math.floor(year / 256);
const yearLow = year % 256;
const hours = date.getUTCHours();
const minutes = date.getUTCMinutes();
return new Uint8Array([yearHigh, yearLow, month, day, hours, minutes]);
}