2525#![ no_std]
2626#![ doc(
2727 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- html_root_url = "https://docs.rs/ghash/0.4.3"
28+ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
3029) ]
3130#![ warn( missing_docs, rust_2018_idioms) ]
3231
3332pub use polyval:: universal_hash;
3433
3534use polyval:: Polyval ;
36- use universal_hash:: { consts:: U16 , NewUniversalHash , UniversalHash } ;
35+ use universal_hash:: {
36+ consts:: U16 ,
37+ crypto_common:: { BlockSizeUser , KeySizeUser , ParBlocksSizeUser } ,
38+ KeyInit , UhfBackend , UhfClosure , UniversalHash ,
39+ } ;
3740
3841#[ cfg( feature = "zeroize" ) ]
3942use zeroize:: Zeroize ;
@@ -45,7 +48,7 @@ pub type Key = universal_hash::Key<GHash>;
4548pub type Block = universal_hash:: Block < GHash > ;
4649
4750/// GHASH tags (16-bytes)
48- pub type Tag = universal_hash:: Output < GHash > ;
51+ pub type Tag = universal_hash:: Block < GHash > ;
4952
5053/// **GHASH**: universal hash over GF(2^128) used by AES-GCM.
5154///
@@ -54,9 +57,11 @@ pub type Tag = universal_hash::Output<GHash>;
5457#[ derive( Clone ) ]
5558pub struct GHash ( Polyval ) ;
5659
57- impl NewUniversalHash for GHash {
60+ impl KeySizeUser for GHash {
5861 type KeySize = U16 ;
62+ }
5963
64+ impl KeyInit for GHash {
6065 /// Initialize GHASH with the given `H` field element
6166 #[ inline]
6267 fn new ( h : & Key ) -> Self {
@@ -79,29 +84,51 @@ impl NewUniversalHash for GHash {
7984 }
8085}
8186
82- impl UniversalHash for GHash {
83- type BlockSize = U16 ;
87+ struct GHashBackend < ' b , B : UhfBackend > ( & ' b mut B ) ;
8488
85- /// Input a field element `X` to be authenticated
86- #[ inline]
87- fn update ( & mut self , x : & Block ) {
88- let mut x = * x;
89+ impl < ' b , B : UhfBackend > BlockSizeUser for GHashBackend < ' b , B > {
90+ type BlockSize = B :: BlockSize ;
91+ }
92+
93+ impl < ' b , B : UhfBackend > ParBlocksSizeUser for GHashBackend < ' b , B > {
94+ type ParBlocksSize = B :: ParBlocksSize ;
95+ }
96+
97+ impl < ' 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 ( ) ;
89100 x. reverse ( ) ;
90- self . 0 . update ( & x) ;
101+ self . 0 . proc_block ( & x) ;
91102 }
103+ }
92104
93- /// Reset internal state
94- #[ inline]
95- fn reset ( & mut self ) {
96- self . 0 . reset ( ) ;
105+ impl BlockSizeUser for GHash {
106+ type BlockSize = U16 ;
107+ }
108+
109+ impl 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) ) ;
97124 }
98125
99126 /// Get GHASH output
100127 #[ inline]
101128 fn finalize ( self ) -> Tag {
102- let mut output = self . 0 . finalize ( ) . into_bytes ( ) ;
129+ let mut output = self . 0 . finalize ( ) ;
103130 output. reverse ( ) ;
104- Tag :: new ( output)
131+ output
105132 }
106133}
107134
0 commit comments