1#![no_std]
26#![doc(
27 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
28 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
29)]
30#![warn(missing_docs, rust_2018_idioms)]
31
32pub use polyval::universal_hash;
33
34use polyval::Polyval;
35use universal_hash::{
36 consts::U16,
37 crypto_common::{BlockSizeUser, KeySizeUser, ParBlocksSizeUser},
38 KeyInit, UhfBackend, UhfClosure, UniversalHash,
39};
40
41#[cfg(feature = "zeroize")]
42use zeroize::Zeroize;
43
44pub type Key = universal_hash::Key<GHash>;
46
47pub type Block = universal_hash::Block<GHash>;
49
50pub type Tag = universal_hash::Block<GHash>;
52
53#[derive(Clone)]
58pub struct GHash(Polyval);
59
60impl KeySizeUser for GHash {
61 type KeySize = U16;
62}
63
64impl KeyInit for GHash {
65 #[inline]
67 fn new(h: &Key) -> Self {
68 let mut h = *h;
69 h.reverse();
70
71 #[allow(unused_mut)]
72 let mut h_polyval = polyval::mulx(&h);
73
74 #[cfg(feature = "zeroize")]
75 h.zeroize();
76
77 #[allow(clippy::let_and_return)]
78 let result = GHash(Polyval::new(&h_polyval));
79
80 #[cfg(feature = "zeroize")]
81 h_polyval.zeroize();
82
83 result
84 }
85}
86
87struct GHashBackend<'b, B: UhfBackend>(&'b mut B);
88
89impl<'b, B: UhfBackend> BlockSizeUser for GHashBackend<'b, B> {
90 type BlockSize = B::BlockSize;
91}
92
93impl<'b, B: UhfBackend> ParBlocksSizeUser for GHashBackend<'b, B> {
94 type ParBlocksSize = B::ParBlocksSize;
95}
96
97impl<'b, B: UhfBackend> UhfBackend for GHashBackend<'b, B> {
98 fn proc_block(&mut self, x: &universal_hash::Block<B>) {
99 let mut x = x.clone();
100 x.reverse();
101 self.0.proc_block(&x);
102 }
103}
104
105impl BlockSizeUser for GHash {
106 type BlockSize = U16;
107}
108
109impl UniversalHash for GHash {
110 fn update_with_backend(&mut self, f: impl UhfClosure<BlockSize = Self::BlockSize>) {
111 struct GHashClosure<C: UhfClosure>(C);
112
113 impl<C: UhfClosure> BlockSizeUser for GHashClosure<C> {
114 type BlockSize = C::BlockSize;
115 }
116
117 impl<C: UhfClosure> UhfClosure for GHashClosure<C> {
118 fn call<B: UhfBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
119 self.0.call(&mut GHashBackend(backend));
120 }
121 }
122
123 self.0.update_with_backend(GHashClosure(f));
124 }
125
126 #[inline]
128 fn finalize(self) -> Tag {
129 let mut output = self.0.finalize();
130 output.reverse();
131 output
132 }
133}
134
135opaque_debug::implement!(GHash);