Closed hxzhao527 closed 2 years ago
I think adding a block_size()
method makes sense.
Kind of curious what you're trying to do there specifically and if we could add some additional higher level methods to help. Do you specifically need (unauthenticated) AES-CBC encryption?
Just transform golang to rust. 😂
In go, https://golang.org/pkg/crypto/cipher/#Block, provide a method BlockSize()
, it is
convenient.
Eventually they'll just be associated consts (after const generics land), but I agree for now having a method is easier than <Aes128 as BlockCipher>::BlockSize::to_usize()
you mean something like this?
pub trait BlockCipher {
const Size: usize;
fn block_size() -> usize {
Self::Size
}
fn new() -> Self;
}
pub struct Aes {}
impl BlockCipher for Aes {
const Size: usize = 1;
fn new() -> Self {
Self {}
}
}
pub struct Cbc<C: BlockCipher> {
cipher: C,
}
impl<C> Cbc<C>
where
C: BlockCipher,
{
fn encrypt(self, data: &str) {
for _ in 0..C::block_size() {
println!("data: {}", data);
}
}
fn new() -> Self {
Self { cipher: C::new() }
}
}
#[cfg(test)]
mod tests {
use super::*;
type AesCbc = Cbc<Aes>;
#[test]
pub fn test_size() {
println!("block_size: {}", Aes::block_size());
let encryptor = AesCbc::new();
encryptor.encrypt("hello");
}
}
What I'm talking about involves changes to the Rust language. Here's a blog post I wrote on the topic:
https://tonyarcieri.com/rust-in-2019-security-maturity-stability#const-generics-and-cryptography_2
Got it.
====== For others: The Rust RFC Book : 2000-const_generics Tracking issue for const generics (RFC 2000)
describe & question
BlockCipher.encrypt
work in-place, and no allocate. So I need to calculate the buffer size manually.my question :
block_size
method toBlockCipher
?my code
Cargo.toml