fission-codes / keystore-idb

In-browser key management with IndexedDB and the Web Crypto API
Apache License 2.0
57 stars 8 forks source link

Streaming encryption/decryption #16

Open dholms opened 4 years ago

dholms commented 4 years ago

Problem

WebCrypto does not support streaming encryption/decryption. This means that if we want to display a large file in the browser, we need to read the entire file into memory, decrypt it, and the stream it to the renderer. https://github.com/w3c/webcrypto/issues/73

Solution

We're using AES-CTR, so after calculating the nonce, the counter is predictable for each block.

W'll need to read one block at a time, encrypt, write to outStream & increment the counter

expede commented 4 years ago

@dholms how much effort would you expect this to take?

@bmann can you add a few words on why this got prioritized so high?

bmann commented 4 years ago

I didn't move it, so it just stayed wherever it is. I've Icebox'd it for now.

expede commented 4 years ago

@bmann cool cool thanks :)

dholms commented 4 years ago

I'd probably budget a day for this. Like with a lot of crypto stuff, it's sort of hard to tell. Theoretically it shouldn't be too hard, but if you run into any issues, it's very hard to debug because you're just dealing with nonsensical bytes.

But yeah icebox for now :+1: . This doesn't matter much until we start streaming large private files (private videos for instance)

icidasset commented 3 years ago

I might try to tackle this one soon-ish, so we can add full Fission support to Diffuse 🙏 We'll need this to stream any media via the standard HTML elements.

@dholms I don't think I'll be able to do in one day. So any pointers are welcome 😄

dholms commented 3 years ago

:+1:

Unfortunately it might be a bit of a pain to do...

Basically:

The other option is to just leave the library the way it is, fully read info into memory, encrypt/decrypt it, and then just write that to a stream. This is pretty memory intensive & won't work well for large files. But if the goal is just a streaming API and you're working with smaller files (I think songs would be fine :thinking:) then this would work as a stopgap

Let me know if this makes sense & if you want any clarity on anything or if a call might help!

expede commented 3 years ago

Posted in Discord, but relevant here: https://security.stackexchange.com/questions/65085/using-aes-as-a-stream-cipher

TL;DR they agree with @dholms on the AES-CTR idea, and give similar tradeoffs.

Importantly:

Within the limits expressed above, CTR mode is as good as such things can get.