I want to store encrypted files on some storage backend that allows me to fetch bytes X through Y of the encrypted file. I can obviously decrypt the entire file locally and send it back to the client.

However, if the file is very large it would be nice to be able to arbitrarily decrypt that byte range on the fly. If possible, I'd like to be able to decrypt the parts by different servers at the same time (so I won't necessarily have the full decryption chain at hand).

Is there any block cipher mode that would allow for something like this, or any possible way at all?

  • ECB or CTR would allow that, but you're trading off security by allowing that... – KristoferA Dec 17 '14 at 6:53
  • @KristoferA Can you explain the security trade off in the case of CTR? – Anthony Kraft Dec 18 '14 at 19:06

If you can require that the ranges are block-aligned, the disk encryption modes (LRW, XEX, XTS, CTR, etc.) seem ideal for your purposes. ECB mode also works with block-aligned ranges, but is, well, ECB mode.

If you need byte-level alignment, the only mode that looks promising is OFB mode: you compute a keystream that includes the range you want to decrypt, then XOR the appropriate section of the keystream with the encrypted section of the file. For a large file, this can be rather inefficient, as you may be computing and discarding gigabytes of keystream before you get to the part you're going to use.


Salsa20, at least as implemented in libSodium, seems ideal for this:

Salsa20 is a stream cipher developed by Daniel J. Bernstein that expands a 256-bit key into 2^64 randomly accessible streams, each containing 2^64 randomly accessible 64-byte (512 bits) blocks.


The crypto_stream_salsa20_xor_ic() function is similar to crypto_stream_salsa20_xor() but adds the ability to set the initial value of the block counter to a non-zero value, ic.

This permits direct access to any block without having to compute the previous ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.