Skip to content

Commit 060a712

Browse files
author
teycir
committed
fix(crypto): use zero salt for deterministic key derivation
Previously, a random salt was used, leading to non-deterministic master key derivation. This change ensures that the master key derivation is deterministic by using a zero-filled salt.
1 parent 665ddd1 commit 060a712

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

lib/crypto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ async function deriveMasterKey(keyA: CryptoKey, keyB: CryptoKey): Promise<Crypto
4848
['deriveBits']
4949
);
5050

51-
// Derive 256-bit key using HKDF
52-
const salt = crypto.getRandomValues(new Uint8Array(32));
51+
// Derive 256-bit key using HKDF with zero salt for deterministic derivation
52+
const salt = new Uint8Array(32); // Zero-filled salt
5353
const derivedBits = await crypto.subtle.deriveBits(
5454
{
5555
name: 'HKDF',

tests/unit/crypto-fix.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
import { encryptData, decryptData } from '../../lib/crypto';
3+
4+
describe('Crypto - Encryption/Decryption', () => {
5+
it('should encrypt and decrypt text successfully', async () => {
6+
const originalText = 'This is a secret message';
7+
8+
const encrypted = await encryptData(originalText);
9+
expect(encrypted.keyA).toBeTruthy();
10+
expect(encrypted.keyB).toBeTruthy();
11+
expect(encrypted.iv).toBeTruthy();
12+
expect(encrypted.encryptedBlob).toBeTruthy();
13+
14+
const decrypted = await decryptData(encrypted.encryptedBlob, {
15+
keyA: encrypted.keyA,
16+
keyB: encrypted.keyB,
17+
iv: encrypted.iv,
18+
});
19+
20+
const decryptedText = new TextDecoder().decode(decrypted);
21+
expect(decryptedText).toBe(originalText);
22+
});
23+
24+
it('should fail with wrong keys', async () => {
25+
const originalText = 'Secret';
26+
const encrypted = await encryptData(originalText);
27+
28+
const wrongEncrypted = await encryptData('Different');
29+
30+
await expect(
31+
decryptData(encrypted.encryptedBlob, {
32+
keyA: wrongEncrypted.keyA,
33+
keyB: encrypted.keyB,
34+
iv: encrypted.iv,
35+
})
36+
).rejects.toThrow();
37+
});
38+
});

0 commit comments

Comments
 (0)