diff options
Diffstat (limited to 'client-web/source/protocol')
-rw-r--r-- | client-web/source/protocol/crypto.ts | 10 | ||||
-rw-r--r-- | client-web/source/protocol/mod.ts | 1 |
2 files changed, 7 insertions, 4 deletions
diff --git a/client-web/source/protocol/crypto.ts b/client-web/source/protocol/crypto.ts index 742f22c..79b7e1d 100644 --- a/client-web/source/protocol/crypto.ts +++ b/client-web/source/protocol/crypto.ts @@ -2,6 +2,8 @@ import { log } from "../logger.ts"; //! I am not a crypto expert at all! Please read carefully and report any issues to me. +const IV_LENGTH = 12 + export async function crypto_seeded_key(seed: string): Promise<CryptoKey> { log("crypto", "importing seed…") const seed_key = await window.crypto.subtle.importKey( @@ -11,7 +13,7 @@ export async function crypto_seeded_key(seed: string): Promise<CryptoKey> { false, ["deriveKey"] ) - //? TODO is it possible to use a unique seed per session here? + //? TODO is it possible to use a unique seed per session here? // const salt = window.crypto.getRandomValues(new Uint8Array(16)); const salt = base64_to_buf("thisisagoodsaltAAAAAAA==") // valid "unique" 16-byte base-64 string log("crypto", "deriving key…") @@ -40,7 +42,7 @@ export async function crypt_hash(input: string): Promise<string> { } export async function crypto_encrypt(key: CryptoKey, data: string): Promise<string> { - const iv = window.crypto.getRandomValues(new Uint8Array(12)); + const iv = window.crypto.getRandomValues(new Uint8Array(IV_LENGTH)); const ciphertext = new Uint8Array(await window.crypto.subtle.encrypt( { name: "AES-GCM", iv }, key, @@ -55,8 +57,8 @@ export async function crypto_encrypt(key: CryptoKey, data: string): Promise<stri export async function crypt_decrypt(key: CryptoKey, data: string): Promise<string> { const buf = base64_to_buf(data); - const iv = buf.slice(0, 12); - const ciphertext = buf.slice(12); + const iv = buf.slice(0, IV_LENGTH); + const ciphertext = buf.slice(IV_LENGTH); const decryptedContent = await window.crypto.subtle.decrypt( { name: "AES-GCM", iv }, key, diff --git a/client-web/source/protocol/mod.ts b/client-web/source/protocol/mod.ts index f976f23..03afa8e 100644 --- a/client-web/source/protocol/mod.ts +++ b/client-web/source/protocol/mod.ts @@ -19,6 +19,7 @@ export class SignalingConnection { const ws_url = new URL(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${window.location.host}/signaling/${encodeURIComponent(this.signaling_id)}`) this.websocket = new WebSocket(ws_url) this.websocket.onerror = () => this.on_error() + this.websocket.onclose = () => this.on_close() this.websocket.onmessage = e => { if (typeof e.data == "string") this.on_message(e.data) } |