1

I have a very simple question. Since XChaCha20 nonces are 192-bit, there's technically no limitation to the max number of message encrypted, since the chances of two random nonces being the same are very very tiny. Is it okay to use the same key, but difference nonces to encrypt large files in chunks? Basically, I read the large file 1KB at a time, encrypt with the same key but random nonce. If the file is 1GB, then I would have used 1000000 nonces. If I encrypted in chunks of 1MB, then I would've used 1000 nonces. Is doing this secure? (Assume everything is authenticated and I use CSPRNGs for nonces)

Evan Su
  • 449
  • 1
  • 3
  • 15

1 Answers1

4

Yes. Indeed, a "nonce" is a number used once. If you ever reuse a (nonce, key) pair, security breaks catastrophically. For a large chunked file, you can simply use the chunk number as the nonce, as long as you re-key after each file and can keep track of the count.

Libsodium's secretstream API does this internally. If possible, use that, as it's far safer than building your own construction.

SAI Peregrinus
  • 5,836
  • 19
  • 26
  • Very good, yet simple explanation. I understand using a "counter" as the nonce is a good idea, but would just randomly generating nonces be safe enough? The chances of a nonce collision is only 1/2**192, which should be minuscule enough not to worry about. – Evan Su Mar 26 '21 at 02:43
  • 1
    Seconded for using the secretstream API. Building this yourself comes along with a lot of footguns. – Stephen Touset Mar 26 '21 at 04:48
  • 2
    @HACKERALERT, you aren't thinking like an attacker. You're right that collisions aren't a problem, but without other mitigations, random nonces allow a third party to invisibly reorder arbitrary blocks in the stream. "Don't roll your own crypto" applies here. – Stephen Touset Mar 26 '21 at 04:54
  • Thanks for pointing that out. I didn't realize that, although it seems so obvious to me now. – Evan Su Mar 26 '21 at 13:11
  • 1
    Nitpick: there is better wording for this: If you ever reuse a nonce, security breaks catastrophically. to: If ever the (key,nonce) pair reoccurs confidentiality is lost. – kelalaka Mar 26 '21 at 17:28
  • 1
    For anyone interested, some related reading on the complexities of using authenticated encryption for chunked streams: https://eprint.iacr.org/2015/189.pdf – Stephen Touset Mar 26 '21 at 19:41