Skip to main content

BlockCrypt

Trait BlockCrypt 

Source
pub trait BlockCrypt:
    Send
    + Sync
    + Debug {
    // Required methods
    fn encrypt(&self, dst: &mut [u8], src: &[u8]);
    fn decrypt(&self, dst: &mut [u8], src: &[u8]);

    // Provided methods
    fn header_size(&self) -> usize { ... }
    fn overhead(&self) -> usize { ... }
    fn nonce_size(&self) -> usize { ... }
    fn seal(
        &self,
        dst: &mut [u8],
        nonce: &[u8],
        plaintext: &[u8],
    ) -> Result<usize, String> { ... }
    fn open(
        &self,
        dst: &mut [u8],
        nonce: &[u8],
        ciphertext: &[u8],
    ) -> Result<usize, String> { ... }
    fn is_aead(&self) -> bool { ... }
}
Expand description

BlockCrypt trait 定义加密/解密方法

参考 kcp-go 的 BlockCrypt 接口

Required Methods§

Source

fn encrypt(&self, dst: &mut [u8], src: &[u8])

加密整个块,从 src 到 dst dst 和 src 可以指向同一块内存

注意:对于 AEAD 模式,应该使用 seal 方法而不是 encrypt

Source

fn decrypt(&self, dst: &mut [u8], src: &[u8])

解密整个块,从 src 到 dst dst 和 src 可以指向同一块内存

注意:对于 AEAD 模式,应该使用 open 方法而不是 decrypt

Provided Methods§

Source

fn header_size(&self) -> usize

获取加密头部大小

Source

fn overhead(&self) -> usize

获取加密开销(用于 AEAD 模式,CFB 模式返回 0)

Source

fn nonce_size(&self) -> usize

获取 nonce 大小

CFB 模式使用固定的 16 字节 nonce AEAD 模式(如 AES-GCM)使用 12 字节 nonce

Source

fn seal( &self, dst: &mut [u8], nonce: &[u8], plaintext: &[u8], ) -> Result<usize, String>

AEAD 模式的 Seal 方法

对于 CFB 模式,此方法会 panic(应该使用 encrypt) 对于 AEAD 模式,使用此方法进行加密

§参数
  • dst - 输出缓冲区,必须至少有 len(plaintext) + overhead() 的空间
  • nonce - nonce 值
  • plaintext - 要加密的明文
§返回

返回写入的字节数(包含认证标签)

Source

fn open( &self, dst: &mut [u8], nonce: &[u8], ciphertext: &[u8], ) -> Result<usize, String>

AEAD 模式的 Open 方法

对于 CFB 模式,此方法会 panic(应该使用 decrypt) 对于 AEAD 模式,使用此方法进行解密和认证

§参数
  • dst - 输出缓冲区
  • nonce - nonce 值
  • ciphertext - 要解密的密文(包含认证标签)
§返回

返回写入的字节数(明文长度),如果认证失败则返回错误

Source

fn is_aead(&self) -> bool

判断是否是 AEAD 模式

通过检查 overhead() > 0 来判断

Implementors§