1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#![no_std]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]
#![deny(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]
pub use cipher;
use cipher::{
generic_array::GenericArray, AsyncStreamCipher, BlockCipher, BlockEncrypt, FromBlockCipher,
};
pub struct Cfb8<C: BlockCipher + BlockEncrypt> {
cipher: C,
iv: GenericArray<u8, C::BlockSize>,
}
impl<C: BlockCipher + BlockEncrypt> FromBlockCipher for Cfb8<C> {
type BlockCipher = C;
type NonceSize = C::BlockSize;
fn from_block_cipher(cipher: C, iv: &GenericArray<u8, Self::NonceSize>) -> Self {
Self {
cipher,
iv: iv.clone(),
}
}
}
impl<C: BlockCipher + BlockEncrypt> AsyncStreamCipher for Cfb8<C> {
fn encrypt(&mut self, data: &mut [u8]) {
let mut iv = self.iv.clone();
let n = iv.len();
for b in data.iter_mut() {
let iv_copy = iv.clone();
self.cipher.encrypt_block(&mut iv);
*b ^= iv[0];
iv[..n - 1].clone_from_slice(&iv_copy[1..]);
iv[n - 1] = *b;
}
self.iv = iv;
}
fn decrypt(&mut self, data: &mut [u8]) {
let mut iv = self.iv.clone();
let n = iv.len();
for b in data.iter_mut() {
let iv_copy = iv.clone();
self.cipher.encrypt_block(&mut iv);
let t = *b;
*b ^= iv[0];
iv[..n - 1].clone_from_slice(&iv_copy[1..]);
iv[n - 1] = t;
}
self.iv = iv;
}
}