ed25519_compact/
sha512.rs

1//! A small, self-contained SHA512 implementation
2//! (C) Frank Denis, public domain
3
4#![allow(
5    non_snake_case,
6    clippy::cast_lossless,
7    clippy::eq_op,
8    clippy::identity_op,
9    clippy::many_single_char_names,
10    clippy::unreadable_literal
11)]
12
13#[cfg_attr(feature = "opt_size", inline(never))]
14#[cfg_attr(not(feature = "opt_size"), inline(always))]
15fn load_be(base: &[u8], offset: usize) -> u64 {
16    let addr = &base[offset..];
17    (addr[7] as u64)
18        | (addr[6] as u64) << 8
19        | (addr[5] as u64) << 16
20        | (addr[4] as u64) << 24
21        | (addr[3] as u64) << 32
22        | (addr[2] as u64) << 40
23        | (addr[1] as u64) << 48
24        | (addr[0] as u64) << 56
25}
26
27#[cfg_attr(feature = "opt_size", inline(never))]
28#[cfg_attr(not(feature = "opt_size"), inline(always))]
29fn store_be(base: &mut [u8], offset: usize, x: u64) {
30    let addr = &mut base[offset..];
31    addr[7] = x as u8;
32    addr[6] = (x >> 8) as u8;
33    addr[5] = (x >> 16) as u8;
34    addr[4] = (x >> 24) as u8;
35    addr[3] = (x >> 32) as u8;
36    addr[2] = (x >> 40) as u8;
37    addr[1] = (x >> 48) as u8;
38    addr[0] = (x >> 56) as u8;
39}
40
41struct W([u64; 16]);
42
43#[derive(Copy, Clone)]
44struct State([u64; 8]);
45
46impl W {
47    fn new(input: &[u8]) -> Self {
48        let mut w = [0u64; 16];
49        for (i, e) in w.iter_mut().enumerate() {
50            *e = load_be(input, i * 8)
51        }
52        W(w)
53    }
54
55    #[cfg_attr(feature = "opt_size", inline(never))]
56    #[cfg_attr(not(feature = "opt_size"), inline(always))]
57    fn Ch(x: u64, y: u64, z: u64) -> u64 {
58        (x & y) ^ (!x & z)
59    }
60
61    #[cfg_attr(feature = "opt_size", inline(never))]
62    #[cfg_attr(not(feature = "opt_size"), inline(always))]
63    fn Maj(x: u64, y: u64, z: u64) -> u64 {
64        (x & y) ^ (x & z) ^ (y & z)
65    }
66
67    #[cfg_attr(feature = "opt_size", inline(never))]
68    #[cfg_attr(not(feature = "opt_size"), inline(always))]
69    fn Sigma0(x: u64) -> u64 {
70        x.rotate_right(28) ^ x.rotate_right(34) ^ x.rotate_right(39)
71    }
72
73    #[cfg_attr(feature = "opt_size", inline(never))]
74    #[cfg_attr(not(feature = "opt_size"), inline(always))]
75    fn Sigma1(x: u64) -> u64 {
76        x.rotate_right(14) ^ x.rotate_right(18) ^ x.rotate_right(41)
77    }
78
79    #[cfg_attr(feature = "opt_size", inline(never))]
80    #[cfg_attr(not(feature = "opt_size"), inline(always))]
81    fn sigma0(x: u64) -> u64 {
82        x.rotate_right(1) ^ x.rotate_right(8) ^ (x >> 7)
83    }
84
85    #[cfg_attr(feature = "opt_size", inline(never))]
86    #[cfg_attr(not(feature = "opt_size"), inline(always))]
87    fn sigma1(x: u64) -> u64 {
88        x.rotate_right(19) ^ x.rotate_right(61) ^ (x >> 6)
89    }
90
91    #[cfg_attr(feature = "opt_size", inline(never))]
92    #[cfg_attr(not(feature = "opt_size"), inline(always))]
93    fn M(&mut self, a: usize, b: usize, c: usize, d: usize) {
94        let w = &mut self.0;
95        w[a] = w[a]
96            .wrapping_add(Self::sigma1(w[b]))
97            .wrapping_add(w[c])
98            .wrapping_add(Self::sigma0(w[d]));
99    }
100
101    #[cfg_attr(feature = "opt_size", inline(never))]
102    #[cfg_attr(not(feature = "opt_size"), inline(always))]
103    fn expand(&mut self) {
104        self.M(0, (0 + 14) & 15, (0 + 9) & 15, (0 + 1) & 15);
105        self.M(1, (1 + 14) & 15, (1 + 9) & 15, (1 + 1) & 15);
106        self.M(2, (2 + 14) & 15, (2 + 9) & 15, (2 + 1) & 15);
107        self.M(3, (3 + 14) & 15, (3 + 9) & 15, (3 + 1) & 15);
108        self.M(4, (4 + 14) & 15, (4 + 9) & 15, (4 + 1) & 15);
109        self.M(5, (5 + 14) & 15, (5 + 9) & 15, (5 + 1) & 15);
110        self.M(6, (6 + 14) & 15, (6 + 9) & 15, (6 + 1) & 15);
111        self.M(7, (7 + 14) & 15, (7 + 9) & 15, (7 + 1) & 15);
112        self.M(8, (8 + 14) & 15, (8 + 9) & 15, (8 + 1) & 15);
113        self.M(9, (9 + 14) & 15, (9 + 9) & 15, (9 + 1) & 15);
114        self.M(10, (10 + 14) & 15, (10 + 9) & 15, (10 + 1) & 15);
115        self.M(11, (11 + 14) & 15, (11 + 9) & 15, (11 + 1) & 15);
116        self.M(12, (12 + 14) & 15, (12 + 9) & 15, (12 + 1) & 15);
117        self.M(13, (13 + 14) & 15, (13 + 9) & 15, (13 + 1) & 15);
118        self.M(14, (14 + 14) & 15, (14 + 9) & 15, (14 + 1) & 15);
119        self.M(15, (15 + 14) & 15, (15 + 9) & 15, (15 + 1) & 15);
120    }
121
122    #[cfg_attr(feature = "opt_size", inline(never))]
123    #[cfg_attr(not(feature = "opt_size"), inline(always))]
124    fn F(&mut self, state: &mut State, i: usize, k: u64) {
125        let t = &mut state.0;
126        t[(16 - i + 7) & 7] = t[(16 - i + 7) & 7]
127            .wrapping_add(Self::Sigma1(t[(16 - i + 4) & 7]))
128            .wrapping_add(Self::Ch(
129                t[(16 - i + 4) & 7],
130                t[(16 - i + 5) & 7],
131                t[(16 - i + 6) & 7],
132            ))
133            .wrapping_add(k)
134            .wrapping_add(self.0[i]);
135        t[(16 - i + 3) & 7] = t[(16 - i + 3) & 7].wrapping_add(t[(16 - i + 7) & 7]);
136        t[(16 - i + 7) & 7] = t[(16 - i + 7) & 7]
137            .wrapping_add(Self::Sigma0(t[(16 - i + 0) & 7]))
138            .wrapping_add(Self::Maj(
139                t[(16 - i + 0) & 7],
140                t[(16 - i + 1) & 7],
141                t[(16 - i + 2) & 7],
142            ));
143    }
144
145    fn G(&mut self, state: &mut State, s: usize) {
146        const ROUND_CONSTANTS: [u64; 80] = [
147            0x428a2f98d728ae22,
148            0x7137449123ef65cd,
149            0xb5c0fbcfec4d3b2f,
150            0xe9b5dba58189dbbc,
151            0x3956c25bf348b538,
152            0x59f111f1b605d019,
153            0x923f82a4af194f9b,
154            0xab1c5ed5da6d8118,
155            0xd807aa98a3030242,
156            0x12835b0145706fbe,
157            0x243185be4ee4b28c,
158            0x550c7dc3d5ffb4e2,
159            0x72be5d74f27b896f,
160            0x80deb1fe3b1696b1,
161            0x9bdc06a725c71235,
162            0xc19bf174cf692694,
163            0xe49b69c19ef14ad2,
164            0xefbe4786384f25e3,
165            0x0fc19dc68b8cd5b5,
166            0x240ca1cc77ac9c65,
167            0x2de92c6f592b0275,
168            0x4a7484aa6ea6e483,
169            0x5cb0a9dcbd41fbd4,
170            0x76f988da831153b5,
171            0x983e5152ee66dfab,
172            0xa831c66d2db43210,
173            0xb00327c898fb213f,
174            0xbf597fc7beef0ee4,
175            0xc6e00bf33da88fc2,
176            0xd5a79147930aa725,
177            0x06ca6351e003826f,
178            0x142929670a0e6e70,
179            0x27b70a8546d22ffc,
180            0x2e1b21385c26c926,
181            0x4d2c6dfc5ac42aed,
182            0x53380d139d95b3df,
183            0x650a73548baf63de,
184            0x766a0abb3c77b2a8,
185            0x81c2c92e47edaee6,
186            0x92722c851482353b,
187            0xa2bfe8a14cf10364,
188            0xa81a664bbc423001,
189            0xc24b8b70d0f89791,
190            0xc76c51a30654be30,
191            0xd192e819d6ef5218,
192            0xd69906245565a910,
193            0xf40e35855771202a,
194            0x106aa07032bbd1b8,
195            0x19a4c116b8d2d0c8,
196            0x1e376c085141ab53,
197            0x2748774cdf8eeb99,
198            0x34b0bcb5e19b48a8,
199            0x391c0cb3c5c95a63,
200            0x4ed8aa4ae3418acb,
201            0x5b9cca4f7763e373,
202            0x682e6ff3d6b2b8a3,
203            0x748f82ee5defb2fc,
204            0x78a5636f43172f60,
205            0x84c87814a1f0ab72,
206            0x8cc702081a6439ec,
207            0x90befffa23631e28,
208            0xa4506cebde82bde9,
209            0xbef9a3f7b2c67915,
210            0xc67178f2e372532b,
211            0xca273eceea26619c,
212            0xd186b8c721c0c207,
213            0xeada7dd6cde0eb1e,
214            0xf57d4f7fee6ed178,
215            0x06f067aa72176fba,
216            0x0a637dc5a2c898a6,
217            0x113f9804bef90dae,
218            0x1b710b35131c471b,
219            0x28db77f523047d84,
220            0x32caab7b40c72493,
221            0x3c9ebe0a15c9bebc,
222            0x431d67c49c100d4c,
223            0x4cc5d4becb3e42b6,
224            0x597f299cfc657e2a,
225            0x5fcb6fab3ad6faec,
226            0x6c44198c4a475817,
227        ];
228        let rc = &ROUND_CONSTANTS[s * 16..];
229        self.F(state, 0, rc[0]);
230        self.F(state, 1, rc[1]);
231        self.F(state, 2, rc[2]);
232        self.F(state, 3, rc[3]);
233        self.F(state, 4, rc[4]);
234        self.F(state, 5, rc[5]);
235        self.F(state, 6, rc[6]);
236        self.F(state, 7, rc[7]);
237        self.F(state, 8, rc[8]);
238        self.F(state, 9, rc[9]);
239        self.F(state, 10, rc[10]);
240        self.F(state, 11, rc[11]);
241        self.F(state, 12, rc[12]);
242        self.F(state, 13, rc[13]);
243        self.F(state, 14, rc[14]);
244        self.F(state, 15, rc[15]);
245    }
246}
247
248impl State {
249    fn new() -> Self {
250        const IV: [u8; 64] = [
251            0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08, 0xbb, 0x67, 0xae, 0x85, 0x84, 0xca,
252            0xa7, 0x3b, 0x3c, 0x6e, 0xf3, 0x72, 0xfe, 0x94, 0xf8, 0x2b, 0xa5, 0x4f, 0xf5, 0x3a,
253            0x5f, 0x1d, 0x36, 0xf1, 0x51, 0x0e, 0x52, 0x7f, 0xad, 0xe6, 0x82, 0xd1, 0x9b, 0x05,
254            0x68, 0x8c, 0x2b, 0x3e, 0x6c, 0x1f, 0x1f, 0x83, 0xd9, 0xab, 0xfb, 0x41, 0xbd, 0x6b,
255            0x5b, 0xe0, 0xcd, 0x19, 0x13, 0x7e, 0x21, 0x79,
256        ];
257        let mut t = [0u64; 8];
258        for (i, e) in t.iter_mut().enumerate() {
259            *e = load_be(&IV, i * 8)
260        }
261        State(t)
262    }
263
264    #[cfg_attr(feature = "opt_size", inline(never))]
265    #[cfg_attr(not(feature = "opt_size"), inline(always))]
266    fn add(&mut self, x: &State) {
267        let sx = &mut self.0;
268        let ex = &x.0;
269        sx[0] = sx[0].wrapping_add(ex[0]);
270        sx[1] = sx[1].wrapping_add(ex[1]);
271        sx[2] = sx[2].wrapping_add(ex[2]);
272        sx[3] = sx[3].wrapping_add(ex[3]);
273        sx[4] = sx[4].wrapping_add(ex[4]);
274        sx[5] = sx[5].wrapping_add(ex[5]);
275        sx[6] = sx[6].wrapping_add(ex[6]);
276        sx[7] = sx[7].wrapping_add(ex[7]);
277    }
278
279    fn store(&self, out: &mut [u8]) {
280        for (i, &e) in self.0.iter().enumerate() {
281            store_be(out, i * 8, e);
282        }
283    }
284
285    fn blocks(&mut self, mut input: &[u8]) -> usize {
286        let mut t = *self;
287        let mut inlen = input.len();
288        while inlen >= 128 {
289            let mut w = W::new(input);
290            w.G(&mut t, 0);
291            w.expand();
292            w.G(&mut t, 1);
293            w.expand();
294            w.G(&mut t, 2);
295            w.expand();
296            w.G(&mut t, 3);
297            w.expand();
298            w.G(&mut t, 4);
299            t.add(self);
300            self.0 = t.0;
301            input = &input[128..];
302            inlen -= 128;
303        }
304        inlen
305    }
306}
307
308#[derive(Copy, Clone)]
309pub struct Hash {
310    state: State,
311    w: [u8; 128],
312    r: usize,
313    len: usize,
314}
315
316impl Hash {
317    pub fn new() -> Hash {
318        Hash {
319            state: State::new(),
320            r: 0,
321            w: [0u8; 128],
322            len: 0,
323        }
324    }
325
326    /// Absorb content
327    pub fn update<T: AsRef<[u8]>>(&mut self, input: T) {
328        let input = input.as_ref();
329        let mut n = input.len();
330        self.len += n;
331        let av = 128 - self.r;
332        let tc = ::core::cmp::min(n, av);
333        self.w[self.r..self.r + tc].copy_from_slice(&input[0..tc]);
334        self.r += tc;
335        n -= tc;
336        let pos = tc;
337        if self.r == 128 {
338            self.state.blocks(&self.w);
339            self.r = 0;
340        }
341        if self.r == 0 && n > 0 {
342            let rb = self.state.blocks(&input[pos..]);
343            if rb > 0 {
344                self.w[..rb].copy_from_slice(&input[pos + n - rb..]);
345                self.r = rb;
346            }
347        }
348    }
349
350    /// Compute SHA512(absorbed content)
351    pub fn finalize(mut self) -> [u8; 64] {
352        let mut padded = [0u8; 256];
353        padded[..self.r].copy_from_slice(&self.w[..self.r]);
354        padded[self.r] = 0x80;
355        let r = if self.r < 112 { 128 } else { 256 };
356        let bits = self.len * 8;
357        for i in 0..8 {
358            padded[r - 8 + i] = (bits as u64 >> (56 - i * 8)) as u8;
359        }
360        self.state.blocks(&padded[..r]);
361        let mut out = [0u8; 64];
362        self.state.store(&mut out);
363        out
364    }
365
366    /// Compute SHA512(`input`)
367    pub fn hash<T: AsRef<[u8]>>(input: T) -> [u8; 64] {
368        let mut h = Hash::new();
369        h.update(input);
370        h.finalize()
371    }
372}
373
374impl Default for Hash {
375    fn default() -> Self {
376        Self::new()
377    }
378}