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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![no_std]
extern crate byteorder;
mod util;
mod ops;
use byteorder::ByteOrder;
pub use ops::Hc256Rng;
#[derive(Clone, Copy)]
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: 0
}
}
pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
let mut pos = 0;
if self.index != 0 && input.len() >= 4 - self.index {
pos += 4 - self.index;
output[..pos].clone_from_slice(&self.buff[self.index..]);
self.index = 0;
}
while pos + 4 <= input.len() {
let end = pos + 4;
util::u32_to_u8(
util::Endian::read_u32(&input[pos..end]) ^ self.inner.gen(),
&mut output[pos..end]
);
pos = end;
}
for b in self.take(input.len() - pos) {
output[pos] = input[pos] ^ b;
pos += 1;
}
}
}
impl Iterator for HC256 {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.index == 0 {
util::u32_to_u8(self.inner.gen(), &mut self.buff);
}
let output = self.buff[self.index];
self.index = (self.index + 1) % self.buff.len();
Some(output)
}
}