3333pub use polyval:: universal_hash;
3434
3535use polyval:: Polyval ;
36- use universal_hash:: { consts:: U16 , NewUniversalHash , UniversalHash } ;
36+ use universal_hash:: {
37+ consts:: U16 ,
38+ crypto_common:: { BlockSizeUser , KeySizeUser , ParBlocksSizeUser } ,
39+ KeyInit , UhfBackend , UhfClosure , UniversalHash ,
40+ } ;
3741
3842#[ cfg( feature = "zeroize" ) ]
3943use zeroize:: Zeroize ;
@@ -45,7 +49,7 @@ pub type Key = universal_hash::Key<GHash>;
4549pub type Block = universal_hash:: Block < GHash > ;
4650
4751/// GHASH tags (16-bytes)
48- pub type Tag = universal_hash:: Output < GHash > ;
52+ pub type Tag = universal_hash:: Block < GHash > ;
4953
5054/// **GHASH**: universal hash over GF(2^128) used by AES-GCM.
5155///
@@ -54,9 +58,11 @@ pub type Tag = universal_hash::Output<GHash>;
5458#[ derive( Clone ) ]
5559pub struct GHash ( Polyval ) ;
5660
57- impl NewUniversalHash for GHash {
61+ impl KeySizeUser for GHash {
5862 type KeySize = U16 ;
63+ }
5964
65+ impl KeyInit for GHash {
6066 /// Initialize GHASH with the given `H` field element
6167 #[ inline]
6268 fn new ( h : & Key ) -> Self {
@@ -79,29 +85,51 @@ impl NewUniversalHash for GHash {
7985 }
8086}
8187
82- impl UniversalHash for GHash {
83- type BlockSize = U16 ;
88+ struct GHashBackend < ' b , B : UhfBackend > ( & ' b mut B ) ;
8489
85- /// Input a field element `X` to be authenticated
86- #[ inline]
87- fn update ( & mut self , x : & Block ) {
88- let mut x = * x;
90+ impl < ' b , B : UhfBackend > BlockSizeUser for GHashBackend < ' b , B > {
91+ type BlockSize = B :: BlockSize ;
92+ }
93+
94+ impl < ' b , B : UhfBackend > ParBlocksSizeUser for GHashBackend < ' b , B > {
95+ type ParBlocksSize = B :: ParBlocksSize ;
96+ }
97+
98+ impl < ' b , B : UhfBackend > UhfBackend for GHashBackend < ' b , B > {
99+ fn proc_block ( & mut self , x : & universal_hash:: Block < B > ) {
100+ let mut x = x. clone ( ) ;
89101 x. reverse ( ) ;
90- self . 0 . update ( & x) ;
102+ self . 0 . proc_block ( & x) ;
91103 }
104+ }
92105
93- /// Reset internal state
94- #[ inline]
95- fn reset ( & mut self ) {
96- self . 0 . reset ( ) ;
106+ impl BlockSizeUser for GHash {
107+ type BlockSize = U16 ;
108+ }
109+
110+ impl UniversalHash for GHash {
111+ fn update_with_backend ( & mut self , f : impl UhfClosure < BlockSize = Self :: BlockSize > ) {
112+ struct GHashClosure < C : UhfClosure > ( C ) ;
113+
114+ impl < C : UhfClosure > BlockSizeUser for GHashClosure < C > {
115+ type BlockSize = C :: BlockSize ;
116+ }
117+
118+ impl < C : UhfClosure > UhfClosure for GHashClosure < C > {
119+ fn call < B : UhfBackend < BlockSize = Self :: BlockSize > > ( self , backend : & mut B ) {
120+ self . 0 . call ( & mut GHashBackend ( backend) ) ;
121+ }
122+ }
123+
124+ self . 0 . update_with_backend ( GHashClosure ( f) ) ;
97125 }
98126
99127 /// Get GHASH output
100128 #[ inline]
101129 fn finalize ( self ) -> Tag {
102- let mut output = self . 0 . finalize ( ) . into_bytes ( ) ;
130+ let mut output = self . 0 . finalize ( ) ;
103131 output. reverse ( ) ;
104- Tag :: new ( output)
132+ output
105133 }
106134}
107135
0 commit comments