1 | #![allow (clippy::duplicate_mod)] |
2 | |
3 | use alloc::boxed::Box; |
4 | |
5 | use super::ring_like::digest; |
6 | use crate::crypto; |
7 | use crate::msgs::enums::HashAlgorithm; |
8 | |
9 | pub(crate) static SHA256: Hash = Hash(&digest::SHA256, HashAlgorithm::SHA256); |
10 | pub(crate) static SHA384: Hash = Hash(&digest::SHA384, HashAlgorithm::SHA384); |
11 | |
12 | pub(crate) struct Hash(&'static digest::Algorithm, HashAlgorithm); |
13 | |
14 | impl 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: Context = digest::Context::new(self.0); |
21 | ctx.update(data:bytes); |
22 | convert(val: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 | |
38 | struct Context(digest::Context); |
39 | |
40 | impl 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 | |
58 | fn convert(val: digest::Digest) -> crypto::hash::Output { |
59 | crypto::hash::Output::new(bytes:val.as_ref()) |
60 | } |
61 | |