25
25
#![ no_std]
26
26
#![ doc(
27
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
- html_root_url = "https://docs.rs/ghash/0.4.3"
28
+ html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
30
29
) ]
31
30
#![ warn( missing_docs, rust_2018_idioms) ]
32
31
33
32
pub use polyval:: universal_hash;
34
33
35
34
use 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
+ } ;
37
40
38
41
#[ cfg( feature = "zeroize" ) ]
39
42
use zeroize:: Zeroize ;
@@ -45,7 +48,7 @@ pub type Key = universal_hash::Key<GHash>;
45
48
pub type Block = universal_hash:: Block < GHash > ;
46
49
47
50
/// GHASH tags (16-bytes)
48
- pub type Tag = universal_hash:: Output < GHash > ;
51
+ pub type Tag = universal_hash:: Block < GHash > ;
49
52
50
53
/// **GHASH**: universal hash over GF(2^128) used by AES-GCM.
51
54
///
@@ -54,9 +57,11 @@ pub type Tag = universal_hash::Output<GHash>;
54
57
#[ derive( Clone ) ]
55
58
pub struct GHash ( Polyval ) ;
56
59
57
- impl NewUniversalHash for GHash {
60
+ impl KeySizeUser for GHash {
58
61
type KeySize = U16 ;
62
+ }
59
63
64
+ impl KeyInit for GHash {
60
65
/// Initialize GHASH with the given `H` field element
61
66
#[ inline]
62
67
fn new ( h : & Key ) -> Self {
@@ -79,29 +84,51 @@ impl NewUniversalHash for GHash {
79
84
}
80
85
}
81
86
82
- impl UniversalHash for GHash {
83
- type BlockSize = U16 ;
87
+ struct GHashBackend < ' b , B : UhfBackend > ( & ' b mut B ) ;
84
88
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 ( ) ;
89
100
x. reverse ( ) ;
90
- self . 0 . update ( & x) ;
101
+ self . 0 . proc_block ( & x) ;
91
102
}
103
+ }
92
104
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) ) ;
97
124
}
98
125
99
126
/// Get GHASH output
100
127
#[ inline]
101
128
fn finalize ( self ) -> Tag {
102
- let mut output = self . 0 . finalize ( ) . into_bytes ( ) ;
129
+ let mut output = self . 0 . finalize ( ) ;
103
130
output. reverse ( ) ;
104
- Tag :: new ( output)
131
+ output
105
132
}
106
133
}
107
134
0 commit comments