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
191
192
193
194
195
196
197
198
199
200
201
/* Copyright (c) Fortanix, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

use crate::log::error;
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use rustls::crypto::hash::{self, HashAlgorithm};
use std::sync::Mutex;

pub(crate) static SHA256: Hash = Hash(&MBED_SHA_256);
pub(crate) static SHA384: Hash = Hash(&MBED_SHA_384);

pub(crate) struct Hash(&'static Algorithm);

/// A digest algorithm.
#[derive(Clone, Debug, PartialEq)]
pub struct Algorithm {
    pub(crate) hash_algorithm: HashAlgorithm,
    pub(crate) hash_type: mbedtls::hash::Type,
    pub(crate) output_len: usize,
}

/// SHA-256 as specified in [FIPS 180-4].
///
/// [FIPS 180-4]: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
pub static MBED_SHA_256: Algorithm = Algorithm {
    hash_algorithm: HashAlgorithm::SHA256,
    hash_type: mbedtls::hash::Type::Sha256,
    output_len: 256 / 8,
};

/// SHA-384 as specified in [FIPS 180-4].
///
/// [FIPS 180-4]: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
pub static MBED_SHA_384: Algorithm = Algorithm {
    hash_algorithm: HashAlgorithm::SHA384,
    hash_type: mbedtls::hash::Type::Sha384,
    output_len: 384 / 8,
};

/// SHA-512 as specified in [FIPS 180-4].
///
/// [FIPS 180-4]: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
pub static MBED_SHA_512: Algorithm = Algorithm {
    hash_algorithm: HashAlgorithm::SHA512,
    hash_type: mbedtls::hash::Type::Sha512,
    output_len: 512 / 8,
};

impl hash::Hash for Hash {
    fn start(&self) -> Box<dyn hash::Context> {
        Box::new(HashContext(MbedHashContext::new(self.0)))
    }

    fn hash(&self, data: &[u8]) -> hash::Output {
        hash::Output::new(&hash(self.0, data))
    }

    fn algorithm(&self) -> HashAlgorithm {
        self.0.hash_algorithm
    }

    fn output_len(&self) -> usize {
        self.0.output_len
    }
}

struct HashContext(MbedHashContext);

impl hash::Context for HashContext {
    fn fork_finish(&self) -> hash::Output {
        hash::Output::new(&self.0.clone().finalize())
    }

    fn fork(&self) -> Box<dyn hash::Context> {
        Box::new(Self(self.0.clone()))
    }

    fn finish(self: Box<Self>) -> hash::Output {
        hash::Output::new(&self.0.finalize())
    }

    fn update(&mut self, data: &[u8]) {
        self.0.update(data)
    }
}

pub(crate) struct MbedHashContext {
    pub(crate) state: Mutex<mbedtls::hash::Md>,
    pub(crate) hash_algo: &'static Algorithm,
}

impl Clone for MbedHashContext {
    fn clone(&self) -> Self {
        let state = self.state.lock().unwrap();
        Self { state: Mutex::new(state.clone()), hash_algo: self.hash_algo }
    }
}

impl MbedHashContext {
    pub(crate) fn new(hash_algo: &'static Algorithm) -> Self {
        Self {
            hash_algo,
            state: Mutex::new(mbedtls::hash::Md::new(hash_algo.hash_type).expect("input is validated")),
        }
    }

    /// Since the trait does not provider a way to return error, empty vector is returned when getting error from `mbedtls`.
    pub(crate) fn finalize(self) -> Vec<u8> {
        match self.state.into_inner() {
            Ok(ctx) => {
                let mut out = vec![0u8; self.hash_algo.output_len];
                match ctx.finish(&mut out) {
                    Ok(_) => out,
                    Err(_err) => {
                        error!("Failed to finalize hash, mbedtls error: {:?}", _err);
                        vec![]
                    }
                }
            }
            Err(_err) => {
                error!("Failed to get lock, error: {:?}", _err);
                vec![]
            }
        }
    }

    pub(crate) fn update(&mut self, data: &[u8]) {
        match self.state.lock().as_mut() {
            Ok(ctx) => match ctx.update(data) {
                Ok(_) => {}
                Err(_err) => {
                    error!("Failed to update hash, mbedtls error: {:?}", _err);
                }
            },
            Err(_err) => {
                error!("Failed to get lock, error: {:?}", _err);
            }
        }
    }
}

pub(crate) fn hash(hash_algo: &'static Algorithm, data: &[u8]) -> Vec<u8> {
    let mut out = vec![0u8; hash_algo.output_len];
    match mbedtls::hash::Md::hash(hash_algo.hash_type, data, &mut out) {
        Ok(_) => out,
        Err(_err) => {
            error!("Failed to do hash, mbedtls error: {:?}", _err);
            vec![]
        }
    }
}

#[cfg(bench)]
mod benchmarks {

    #[bench]
    fn bench_sha_256_hash(b: &mut test::Bencher) {
        bench_hash(b, &super::SHA256);
    }

    #[bench]
    fn bench_sha_384_hash(b: &mut test::Bencher) {
        bench_hash(b, &super::SHA384);
    }

    #[bench]
    fn bench_sha_256_hash_multi_parts(b: &mut test::Bencher) {
        bench_hash_multi_parts(b, &super::SHA256);
    }

    #[bench]
    fn bench_sha_384_hash_multi_parts(b: &mut test::Bencher) {
        bench_hash_multi_parts(b, &super::SHA384);
    }

    fn bench_hash(b: &mut test::Bencher, hash: &super::Hash) {
        use super::hash::Hash;
        let input = [123u8; 1024 * 16];
        b.iter(|| {
            test::black_box(hash.hash(&input));
        });
    }

    fn bench_hash_multi_parts(b: &mut test::Bencher, hash: &super::Hash) {
        use super::hash::Hash;
        let input = [123u8; 1024 * 16];
        b.iter(|| {
            let mut ctx = hash.start();
            for i in 0..16 {
                test::black_box(ctx.update(&input[i * 1024..(i + 1) * 1024]));
            }
            test::black_box(ctx.finish())
        });
    }
}