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]
extern crate byteorder;
mod util;
mod ops;
pub use ops::Hc256Rng;
pub struct HC256 {
inner: Hc256Rng,
buff: [u8; 4],
index: usize
}
impl HC256 {
pub fn new(key: &[u8], iv: &[u8]) -> HC256 {
let mut ukey = [0; 8];
let mut uiv = [0; 8];
util::u8_to_u32(key, &mut ukey);
util::u8_to_u32(iv, &mut uiv);
HC256 {
inner: Hc256Rng::init(&ukey, &uiv),
buff: [0; 4],
index: 4
}
}
pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
for (i, k) in self.take(input.len()).enumerate() {
output[i] = input[i] ^ k;
}
}
}
impl Iterator for HC256 {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.buff.len() {
util::u32_to_u8(self.inner.gen(), &mut self.buff);
self.index = 0;
self.next()
} else {
let output = self.buff[self.index];
self.index += 1;
Some(output)
}
}
}
impl Clone for HC256 {
fn clone(&self) -> HC256 {
let mut hc256 = HC256 {
inner: self.inner.clone(),
buff: [0; 4],
index: self.index
};
hc256.buff.clone_from_slice(&self.buff);
hc256
}
}