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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use crate::arith::{Word32, rotr};
use crate::consts::*;
use crate::hash_state;
use crate::hash_state::HashState;
use crate::traits::*;
pub struct Sha256 {
h: [Word32; 8],
message_len: u64,
block_len: usize,
current_block: [u8; SHA256_BLOCK_SIZE],
}
impl Sha256 {
pub fn new() -> Self {
Default::default()
}
fn process_block(&mut self) {
if self.block_len != SHA256_BLOCK_SIZE {
panic!("block is not filled");
}
let mut w = [Word32(0); 64];
for t in 0..16 {
w[t] = self.get_word32_in_block(t)
}
for t in 16..64 {
w[t] = Self::lsigma1(w[t - 2]) + w[t - 7] + Self::lsigma0(w[t - 15]) + w[t - 16];
}
let mut a = self.h[0];
let mut b = self.h[1];
let mut c = self.h[2];
let mut d = self.h[3];
let mut e = self.h[4];
let mut f = self.h[5];
let mut g = self.h[6];
let mut h = self.h[7];
for t in 0..64 {
let t1 = h + Self::sigma1(e) + Self::ch(e, f, g) + SHA256_K[t] + w[t];
let t2 = Self::sigma0(a) + Self::maj(a, b, c);
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
self.h[0] = a + self.h[0];
self.h[1] = b + self.h[1];
self.h[2] = c + self.h[2];
self.h[3] = d + self.h[3];
self.h[4] = e + self.h[4];
self.h[5] = f + self.h[5];
self.h[6] = g + self.h[6];
self.h[7] = h + self.h[7];
self.current_block = [0u8; SHA256_BLOCK_SIZE];
self.block_len = 0;
}
fn get_word32_in_block(&self, i: usize) -> Word32 {
let m: u32 = ((self.current_block[i * 4] as u32) << 24)
+ ((self.current_block[i * 4 + 1] as u32) << 16)
+ ((self.current_block[i * 4 + 2] as u32) << 8)
+ (self.current_block[i * 4 + 3] as u32);
Word32(m)
}
}
impl Sha256 {
fn sigma0(x: Word32) -> Word32 {
rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22)
}
fn sigma1(x: Word32) -> Word32 {
rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25)
}
fn lsigma0(x: Word32) -> Word32 {
rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3)
}
fn lsigma1(x: Word32) -> Word32 {
rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10)
}
fn ch(x: Word32, y: Word32, z: Word32) -> Word32 {
(x & y) ^ (!x & z)
}
fn maj(x: Word32, y: Word32, z: Word32) -> Word32 {
(x & y) ^ (x & z) ^ (y & z)
}
}
impl StreamHasher for Sha256 {
type Output = [u8; 32];
const BLOCK_SIZE: usize = SHA256_BLOCK_SIZE;
fn update(&mut self, buf: &[u8]) -> usize {
let len = buf.len();
if len == 0 {
return 0;
}
let writable_len = Self::BLOCK_SIZE - self.block_len;
let writable_area = &mut self.current_block[self.block_len..];
if len >= writable_len {
writable_area.clone_from_slice(&buf[0..writable_len]);
self.block_len += writable_len;
self.message_len += writable_len as u64;
self.process_block();
self.update(&buf[writable_len..]);
} else {
let write_area = &mut self.current_block[self.block_len..self.block_len + len];
write_area.clone_from_slice(&buf[..]);
self.block_len += len;
self.message_len += len as u64;
}
len
}
fn finish(mut self) -> Self::Output {
self.current_block[self.block_len] = 0x80;
if self.block_len + 1 + 8 > Self::BLOCK_SIZE {
self.block_len = Self::BLOCK_SIZE;
self.process_block();
}
let writable_area = &mut self.current_block[Self::BLOCK_SIZE - 8..Self::BLOCK_SIZE];
let len_bits = self.message_len * 8;
writable_area.clone_from_slice(&len_bits.to_be_bytes());
self.block_len = Self::BLOCK_SIZE;
self.process_block();
let mut final_hash: Self::Output = Default::default();
for i in 0..8 {
let word_area = &mut final_hash[i * 4..i * 4 + 4];
word_area.clone_from_slice(&self.h[i].0.to_be_bytes());
}
return final_hash;
}
}
impl Resumable for Sha256 {
fn pause(self) -> HashState {
let h: [u32; 8] = [
self.h[0].0,
self.h[1].0,
self.h[2].0,
self.h[3].0,
self.h[4].0,
self.h[5].0,
self.h[6].0,
self.h[7].0,
];
HashState::Sha256(hash_state::Sha256HashState {
h,
message_len: self.message_len,
block_len: self.block_len,
current_block: self.current_block,
})
}
fn resume(hash_state: HashState) -> Result<Self, hash_state::Error> {
match hash_state {
HashState::Sha256(hs) => Ok(Self {
h: arr32![hs.h[0], hs.h[1], hs.h[2], hs.h[3], hs.h[4], hs.h[5], hs.h[6], hs.h[7]],
message_len: hs.message_len,
block_len: hs.block_len,
current_block: hs.current_block,
}),
_ => Err(hash_state::Error::HashTypeNotMatch),
}
}
}
impl Default for Sha256 {
fn default() -> Self {
Self {
h: SHA256_H,
current_block: [0u8; SHA256_BLOCK_SIZE],
block_len: 0usize,
message_len: 0u64,
}
}
}