Re:Password protect a static HTML page
by tptacek on Saturday February 18, 2023 @02:03PM
(#34849930)
If you call "encrypt" more than once in that code, you'll leak the authentication key. Every invocation of GCM encryption needs a unique nonce. Cryptography nerds will chastise you for using a random nonce (there theoretically isn't enough room in the GCM nonce space to safely encrypt large numbers of message with random nonces), but the alternative (using a counter) is even more hazardous. This problem motivates a lot of people to use other AEADs like XChapoly, which has an extended nonce space that safely admits random nonces. Isn't cryptography fun?
Oh yes! Thank you for the feedback.
I've added a new version where the `iv` and the `salt` is random.
Maybe a followup question: Because you need both the `iv` and `salt` to decrypt the message is it ok in an E2E scenario to send all three: `iv`, `salt` and the encrypted message?
Re:Password protect a static HTML page
by tptacek on Saturday February 18, 2023 @03:09PM
(#34850546)
I didn't look to see what "salt" means in your design, but the idiom for using GCM in message encryption is to send ciphertexts that take the form `nonce || ciphertext`, and to decrypt by reading the nonce off the front of the message.
From what I shallowly researched; GCM's nonce seems limited to 12 bytes by convention only. That nonce reuse is so fatal seems absurd to me.
Would "salting" the key safely tackle the problem?
Put explicitly;
send <- nonce || salt || ciphertext
recv -> decrypt(ciphertext, nonce, pbkdf(pass) || salt)
[edit: apply salt outside of the kdf]
Re:Password protect a static HTML page
by tptacek on Saturday February 18, 2023 @03:58PM
(#34851021)
As I remember it, the balance of the bytes in the AES block are used for the counter. At any rate, the convention is essentially universal.
Sounds above my (current) head.
Here I thought GCM was some modern foolproof/footgunless design.
Re:Password protect a static HTML page
by tptacek on Saturday February 18, 2023 @04:34PM
(#34851334)
It is sort of infamously footgunny.
Re:Password protect a static HTML page
by ThePhysicist on Saturday February 18, 2023 @04:35PM
(#34851340)
It's still considered safe to call AES-GCM with a single key and a randomly generated nonce 2^32 times [1], most practical systems don't come anywhere near this limit. And there's AES-GCM-SIV that solves nonce reuse (mostly), though it's not available in the Web Cryptography API at the moment.
1: https://csrc.nist.gov/csrc/media/Projects/crypto-publication...
Also sorry for the long link. Is there any accepted way to post a shorted URL?
[1]: https://www.typescriptlang.org/play?#code/DYUwLgBAbiDGYHsBOE...