Skip to content

aead: Add in-place API based on Buffer trait #59

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

Merged
merged 1 commit into from
Nov 17, 2019
Merged

aead: Add in-place API based on Buffer trait #59

merged 1 commit into from
Nov 17, 2019

Conversation

tarcieri
Copy link
Member

@tarcieri tarcieri commented Oct 4, 2019

NOTE: This PR includes the commit which adds the detached API from #58. It might make sense to review that first.

This PR adds a Buffer trait for Vec-like types which impl AsRef<[u8]>, AsMut<[u8]>, extend_from_slice(), and truncate(), and optionally impls it for Vec<u8> (when the alloc feature is enabled), along with the option to use heapless::Vec (when the heapless feature is enabled), providing a true #![no_std]-friendly option.

Uses impl Buffer as the argument for encrypt_in_place and decrypt_in_place, with default implementations that assume a postfix authentication tag. This allows these methods to handle ciphertext assembly and parsing, after which they can invoke the _detached APIs.

categories = ["cryptography", "no-std"]

[dependencies]
generic-array = { version = "0.12", default-features = false }
heapless = { version = "0.5", optional = true }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could define our own heapless::Vec-like type which wraps a GenericArray<u8, N> and does the minimum necessary to impl the Buffer trait, which would eliminate this dependency. But heapless seems relatively popular in the embedded space.

@tarcieri
Copy link
Member Author

@newpavlov mind taking a look at this? (and #58)

@newpavlov
Copy link
Member

@tarcieri
Can you resolve the conflicts?

I am a bit hesitant about this PR, looks like it will be a bit weird to work with modes which store tag in prefix. And the Buffer trait looks a bit out of place as well. Do you or someone else plan to use API added by this PR? If yes, I will merge it, but I am inclined to remove/rework it in the next minor version.

Defines a `Buffer` trait for `Vec`-like types which impl `AsRef<[u8]>`,
`AsMut<[u8]>`, `extend_from_slice()`, and `truncate()`, and optionally
impls it for `Vec<u8>` (when the `alloc` feature is enabled), along with
the option to use `heapless::Vec` (when the `heapless` feature is
enabled), providing a true `#![no_std]`-friendly option.

Uses `impl Buffer` as the argument for `encrypt_in_place` and
`decrypt_in_place`, with default implementations that assume a postfix
authentication tag. This allows these methods to handle ciphertext
assembly and parsing, after which they can invoke the `_detached` APIs.
@tarcieri
Copy link
Member Author

tarcieri commented Nov 17, 2019

@newpavlov

Can you resolve the conflicts?

Done

I am a bit hesitant about this PR, looks like it will be a bit weird to work with modes which store tag in prefix.

I think it will work great with the offset parameter proposed in #58, since that can handle shifting the ciphertext/plaintext during encryption/decryption.

In that case, you'd extend the buffer by the tag length with e.g. zeroes for encryption, encrypt with an offset, and return the buffer.

For decryption, copy the tag out to a type, and then do offset in-place decryption, offsetting the plaintext back to the beginning of the buffer, and truncating.

Do you or someone else plan to use API added by this PR?

Not immediately. It's a proposal and I'm happy to both think about it and try some alternative designs.

Perhaps bytes would be helpful here.

@tarcieri
Copy link
Member Author

(Note: the test failure looks unrelated)

@newpavlov
Copy link
Member

Hm, your arguments are reasonable. Let's merge it then and see how this API will play out. I will release v0.1.2 with both PRs right away.

@newpavlov
Copy link
Member

As for CI failure, looks like we will have to disable no-std build for the time being, since aead enables alloc by default and there is no good way to disable default features when building workspace...

@newpavlov newpavlov merged commit 5222a47 into RustCrypto:master Nov 17, 2019
@tarcieri
Copy link
Member Author

@newpavlov awesome! In that case, it will be interesting to shift the current RustCrypto/aead crate implementations to use this, as for AEADs which have a typical postfix tag, the default impl should suffice, and it should allow eliminating a lot of boilerplate in those crates for doing the same buffer management.

@tarcieri tarcieri deleted the aead/in-place-api branch November 17, 2019 23:44
@tarcieri
Copy link
Member Author

tarcieri commented Nov 17, 2019

I guess another random thought I had about these was splitting the alloc/in-place buffered/detached methods into separate traits with blanket impls, but I think having one trait and some running code which uses it might be more helpful to start with.

That said, would love to have a release with these in it to update the AEAD impls to use.

@newpavlov
Copy link
Member

aead v0.1.2 is published!

I think there is still a sizable room for improvements, but it will require a certain amount of deliberation and design work, ideally supported by user experience reports.

CI

Duh, the CI failure should be fixed by simply removing --no-default-features...

@tarcieri
Copy link
Member Author

@newpavlov awesome, thanks a lot!

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

Successfully merging this pull request may close these issues.

2 participants