crypto/
symmetriccipher.rs1use buffer::{BufferResult, RefReadBuffer, RefWriteBuffer};
8use cryptoutil::symm_enc_or_dec;
9
10pub trait BlockEncryptor {
11 fn block_size(&self) -> usize;
12 fn encrypt_block(&self, input: &[u8], output: &mut [u8]);
13}
14
15pub trait BlockEncryptorX8 {
16 fn block_size(&self) -> usize;
17 fn encrypt_block_x8(&self, input: &[u8], output: &mut [u8]);
18}
19
20pub trait BlockDecryptor {
21 fn block_size(&self) -> usize;
22 fn decrypt_block(&self, input: &[u8], output: &mut [u8]);
23}
24
25pub trait BlockDecryptorX8 {
26 fn block_size(&self) -> usize;
27 fn decrypt_block_x8(&self, input: &[u8], output: &mut [u8]);
28}
29
30#[derive(Debug, Clone, Copy)]
31pub enum SymmetricCipherError {
32 InvalidLength,
33 InvalidPadding
34}
35
36pub trait Encryptor {
37 fn encrypt(&mut self, input: &mut RefReadBuffer, output: &mut RefWriteBuffer, eof: bool)
38 -> Result<BufferResult, SymmetricCipherError>;
39}
40
41pub trait Decryptor {
42 fn decrypt(&mut self, input: &mut RefReadBuffer, output: &mut RefWriteBuffer, eof: bool)
43 -> Result<BufferResult, SymmetricCipherError>;
44}
45
46pub trait SynchronousStreamCipher {
47 fn process(&mut self, input: &[u8], output: &mut [u8]);
48}
49
50impl SynchronousStreamCipher for Box<SynchronousStreamCipher + 'static> {
52 fn process(&mut self, input: &[u8], output: &mut [u8]) {
53 let me = &mut **self;
54 me.process(input, output);
55 }
56}
57
58impl Encryptor for Box<SynchronousStreamCipher + 'static> {
59 fn encrypt(&mut self, input: &mut RefReadBuffer, output: &mut RefWriteBuffer, _: bool)
60 -> Result<BufferResult, SymmetricCipherError> {
61 symm_enc_or_dec(self, input, output)
62 }
63}
64
65impl Decryptor for Box<SynchronousStreamCipher + 'static> {
66 fn decrypt(&mut self, input: &mut RefReadBuffer, output: &mut RefWriteBuffer, _: bool)
67 -> Result<BufferResult, SymmetricCipherError> {
68 symm_enc_or_dec(self, input, output)
69 }
70}