Skip to content

Commit 42dbd90

Browse files
fix: using deriveBits instead of encrypt to generate the storag url
1 parent 8797d72 commit 42dbd90

1 file changed

Lines changed: 32 additions & 7 deletions

File tree

src/utils/safeDoc.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ export {
2020
} from "@/utils/codecs";
2121

2222
const PAGE_SALT = codecs.base64.decode("Ljeijq98ZyaFa5NV");
23-
const PAGE_KEY = "4sQNniS/0141scm8";
2423
const DERIVE = {
2524
name: "PBKDF2",
2625
salt: PAGE_SALT,
27-
iterations: 100000,
26+
iterations: Math.pow(2, 16),
2827
hash: "SHA-256",
2928
} as const satisfies Pbkdf2Params;
3029
const ENCRYPT = {
@@ -89,11 +88,37 @@ async function decryptDocument(
8988
return codecs.json.decode(new Uint8Array(base));
9089
}
9190

92-
const publicKeys = autoLru(10, async (privateKeyBase64) =>
93-
codecs.crockfordBase32.encode(
94-
await encrypt(PAGE_KEY, codecs.base64.decode(privateKeyBase64)),
95-
),
96-
);
91+
const publicKeys = autoLru(10, async (privateKeyBase64) => {
92+
const start = Date.now()
93+
try {
94+
return codecs.crockfordBase32.encode(
95+
new Uint8Array(
96+
await crypto.subtle.deriveBits(
97+
{
98+
...DERIVE,
99+
//
100+
// Passwords are set to provide 96 bits of entropy.
101+
// For 128 bit to be safe for brute force attacks we need 2^(120-96) or 2^32 iterations.
102+
//
103+
// See section A.2.2 of NIST SP 800-132 - https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
104+
//
105+
// That said, 2^32 operations exceed the 2^32-1 limit of PBKDF2 and for it to be practically usable
106+
// we need to limit ourselves to 2^22 operations for a brute force complexity equal to 118bit of entropy.
107+
//
108+
// With 118bit considering https://bruteforce.bitsnbites.eu/ the computation time should still be practically
109+
// unreachable.
110+
//
111+
iterations: Math.pow(2, 22),
112+
},
113+
await derivationKeys.get(privateKeyBase64),
114+
128,
115+
),
116+
),
117+
)
118+
} finally {
119+
console.log(`public key generation took ${Date.now()-start}ms`)
120+
}
121+
});
97122

98123
export function getPossibleDocKey(input: string): string | undefined {
99124
for (const generator of passwordGenerators) {

0 commit comments

Comments
 (0)