rustls/crypto/ring/
hash.rs

1#![allow(clippy::duplicate_mod)]
2
3use alloc::boxed::Box;
4
5use super::ring_like::digest;
6use crate::crypto;
7use crate::msgs::enums::HashAlgorithm;
8
9pub(crate) static SHA256: Hash = Hash(&digest::SHA256, HashAlgorithm::SHA256);
10pub(crate) static SHA384: Hash = Hash(&digest::SHA384, HashAlgorithm::SHA384);
11
12pub(crate) struct Hash(&'static digest::Algorithm, HashAlgorithm);
13
14impl crypto::hash::Hash for Hash {
15    fn start(&self) -> Box<dyn crypto::hash::Context> {
16        Box::new(Context(digest::Context::new(self.0)))
17    }
18
19    fn hash(&self, bytes: &[u8]) -> crypto::hash::Output {
20        let mut ctx = digest::Context::new(self.0);
21        ctx.update(bytes);
22        convert(ctx.finish())
23    }
24
25    fn output_len(&self) -> usize {
26        self.0.output_len()
27    }
28
29    fn algorithm(&self) -> HashAlgorithm {
30        self.1
31    }
32
33    fn fips(&self) -> bool {
34        super::fips()
35    }
36}
37
38struct Context(digest::Context);
39
40impl crypto::hash::Context for Context {
41    fn fork_finish(&self) -> crypto::hash::Output {
42        convert(self.0.clone().finish())
43    }
44
45    fn fork(&self) -> Box<dyn crypto::hash::Context> {
46        Box::new(Self(self.0.clone()))
47    }
48
49    fn finish(self: Box<Self>) -> crypto::hash::Output {
50        convert(self.0.finish())
51    }
52
53    fn update(&mut self, data: &[u8]) {
54        self.0.update(data);
55    }
56}
57
58fn convert(val: digest::Digest) -> crypto::hash::Output {
59    crypto::hash::Output::new(val.as_ref())
60}