crypto/
symmetriccipher.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use 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
50// TODO - Its a bit unclear to me why this is necessary
51impl 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}