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

Consider supporting buffer-to-buffer cipher operations #31

Closed
newpavlov opened this issue Nov 18, 2018 · 2 comments · Fixed by #849
Closed

Consider supporting buffer-to-buffer cipher operations #31

newpavlov opened this issue Nov 18, 2018 · 2 comments · Fixed by #849
Labels
cipher Block and stream cipher crate enhancement

Comments

@newpavlov
Copy link
Member

For some use-cases (e.g. in TLS implementations) you need to decrypt/encrypt data from read-only source and write result into provided buffer.

To prevent code duplication I think the best solution will be to implement algorithms over enum like this:

enum CryptoBuf<'a> {
    InBuf(&'a mut [u8]),
    BufToBuf { in_buf: &'a [u8], out_buf: &'a mut [u8] },
}

trait Cipher: Sized {
    /// Users are heavily discouraged from using this method
    fn _encrypt(self, data: CryptoBuf);
    fn encrypt(self, buf: &mut [u8]) {
        self._encrypt(CryptoBuf::InBuf(buf));
    }
    fn encrypt_b2b(self, in_buf: &mut [u8], out_buf: &mut [u8]) -> Result<(), Error> {
        if check_lengths(in_buf, out_buf) { Err(Error)? }
        self._encrypt(CryptoBuf::BufToBuf { in_buf, out_buf });
        Ok(())
    }
}

Any thoughts?

@tarcieri
Copy link
Member

Isn't the general idea more simply expressed as:

in_buf: Option<&'a [u8]>

...with the idea being that if in_buf is None, then out_buf is also the input buffer?

@newpavlov
Copy link
Member Author

Hm, you are right, but I think using a separate enum will be more ergonomic (for algorithm implementations), e.g. we will be able to offload to it a lot of common functionality, like: reading, writing, indexing, slicing, ensuring that buffers have equal length, etc.

I think it also could be worth to think about how we could integrate Read and Write here.

@newpavlov newpavlov mentioned this issue Aug 20, 2019
13 tasks
@tarcieri tarcieri added the cipher Block and stream cipher crate label Feb 4, 2021
dns2utf8 pushed a commit to dns2utf8/traits that referenced this issue Jan 24, 2023
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 enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants