Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cipher: parallel in-place encrypt/decrypt with offset #764

Open
tarcieri opened this issue Nov 30, 2019 · 2 comments
Open

cipher: parallel in-place encrypt/decrypt with offset #764

tarcieri opened this issue Nov 30, 2019 · 2 comments
Labels
cipher Block and stream cipher crate

Comments

@tarcieri
Copy link
Member

This is a request for the aes crate specifically, but ideally it would work for any BlockCipher.

AES-SIV is a bit unique in that it prepends the authentication tag, rather than using a postfix tag like practically every other AEAD mode.

Ideally it would be nice to support an offset parameter when encrypting (as a counter mode cipher, that's all AES-SIV cares about, but it seems like supporting a decryption offset would be nice for symmetry if nothing else).

For the AES-SIV case specifically, it's a 1-block offset, that is to say, when encrypting the ciphertext is written with an offset of the block size (16-bytes) forward from the original start of the buffer. I understand there are some other use cases for an offset API which need to be finer grained than that (i.e. byte-level), such as parsing multiple AEAD messages in the same buffer as part of some framed packet protocol, but I'm not the one to ask about those.

Something which makes things a bit trickier is AES-SIV, as a counter mode cipher, uses the AES encryption block function to decrypt as well, and needs a "negative" offset in that case, decrypting a ciphertext which begins 1-block into the buffer, and then writing the resulting plaintext out at the beginning of the buffer.

I suppose this can be modeled by taking an isize as a parameter, checking its sign, and using that to determine if the offset is relative to the input or the output. Not sure that's the greatest API, but it would get the job done.

tarcieri referenced this issue in RustCrypto/AEADs Nov 30, 2019
`aes-siv` and `xsalsa20poly1305` use a prepended authentication tag,
which means during in-place encryption/decryption, they need the message
shifted by the tag size.

It would be nice if this could happen during encryption/decryption:

https://github.com/RustCrypto/block-ciphers/issues/59

...but barring that, using `copy_within` (which performs a `memmove`
behind the scenes) should be the fastest available option.

This commit replaces a previous `for` loop which performed this copy
with `copy_within`.

Note that `copy_within` is MSRV 1.37+, so this bumps the project MSRV
up by a single version (from 1.36).
@newpavlov
Copy link
Member

newpavlov commented Sep 16, 2021

@tarcieri
In cipher v0.4 I plan to add support for buffer-to-buffer mode. Is this issue is still relevant in your opinion after that? Supporting in-place offsets generically could be a bit more efficient (you don't need to allocate additional buffers and there is less cache pressure), but implementation complexity will be quite high and I am not sure how to do it API-wise, especially for byte-level offsets. With the inout crate buffers only support two modes: either input and output buffers are equal to each other or they do not overlap. In theory we could could somehow add support for overlapping buffers, but it would require some kind of caching of in-flight blocks (otherwise we risk overwriting input data with output) and it's not clear how exactly it should be handled.

@newpavlov newpavlov transferred this issue from RustCrypto/block-ciphers Sep 16, 2021
@newpavlov newpavlov changed the title aes: parallel in-place encrypt/decrypt with offset cipher: parallel in-place encrypt/decrypt with offset Sep 16, 2021
@newpavlov newpavlov added the cipher Block and stream cipher crate label Sep 16, 2021
@tarcieri
Copy link
Member Author

I'd say it's no big deal if we don't address this.

There are two AEAD modes in our current portfolio that could benefit: aes-siv and xsalsa20poly1305. Both of these use a prefix MAC tag, and utilize copy_within to shift the location of the ciphertext/plaintext within the buffer.

If it were more than these two I'd be a bit more concerned. Both of them are effectively obsoleted by aes-gcm-siv and chacha20poly1305 so I'm not terribly concerned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cipher Block and stream cipher crate
Projects
None yet
Development

No branches or pull requests

2 participants