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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#![crate_type = "lib"]
#![crate_name = "keccak_rust"]
mod keccak_f;
use keccak_f::*;
mod sponge;
use sponge::*;
pub type BitsWidth = usize;
pub type Rate = usize;
pub type Capacity = usize;
pub type Security = (Rate, Capacity);
pub type Byte = u8;
pub type State = [[u64; 5]; 5];
pub type BytesVec = Vec<Byte>;
pub type BytesArr = [Byte];
pub type PermutationsNum = usize;
static RATES: [usize; 4] = [1152, 1088, 832, 576];
static CAPACITIES: [usize; 4] = [448, 512, 768, 1024];
static PERMUTATIONS_NUM: [usize; 7] = [12, 14, 16, 18, 20, 22, 24];
static ROUND_CONSTANTS: [u64; 24] = [
0x0000000000000001,
0x0000000000008082,
0x800000000000808A,
0x8000000080008000,
0x000000000000808B,
0x0000000080000001,
0x8000000080008081,
0x8000000000008009,
0x000000000000008A,
0x0000000000000088,
0x0000000080008009,
0x000000008000000A,
0x000000008000808B,
0x800000000000008B,
0x8000000000008089,
0x8000000000008003,
0x8000000000008002,
0x8000000000000080,
0x000000000000800A,
0x800000008000000A,
0x8000000080008081,
0x8000000000008080,
0x0000000080000001,
0x8000000080008008,
];
static ROTATION_CONSTANTS: State = [
[0, 36, 3, 41, 18],
[1, 44, 10, 45, 2],
[62, 6, 43, 15, 61],
[28, 55, 25, 21, 56],
[27, 20, 39, 8, 14],
];
pub enum StateBitsWidth {
F25,
F50,
F100,
F200,
F400,
F800,
F1600,
Custom(BitsWidth),
}
pub enum SecurityLevel {
SHA224,
SHA256,
SHA384,
SHA512,
Custom(Security),
}
pub struct Keccak {
state: State,
sponge: Sponge,
}
impl Keccak {
pub fn new(security: SecurityLevel, width: StateBitsWidth) -> Keccak {
let security_level = match security {
SecurityLevel::SHA224 => (RATES[0], CAPACITIES[0]),
SecurityLevel::SHA256 => (RATES[1], CAPACITIES[1]),
SecurityLevel::SHA384 => (RATES[2], CAPACITIES[2]),
SecurityLevel::SHA512 => (RATES[3], CAPACITIES[3]),
SecurityLevel::Custom(security) => (security.0, security.1),
};
Keccak {
state: [[0; 5]; 5],
sponge: Sponge::new(security_level.0 / 8, security_level.1 / 8, width),
}
}
pub fn append(&mut self, input: &BytesArr) {
let padding_total = self.sponge.rate - (input.len() % self.sponge.rate);
let mut padding: Vec<Byte>;
if padding_total == 1 {
padding = vec![0x81];
} else {
padding = vec![];
padding.push(0x01);
for _ in 0..(padding_total - 2) {
padding.push(0x00);
}
padding.push(0x80);
}
let padded_input: &BytesArr = &[input, &padding].concat();
self.sponge.absorb(&mut self.state, padded_input);
}
pub fn hash(&mut self) -> BytesVec {
self.sponge.squeeze(&mut self.state)
}
}