1
1
use super :: { IPAD , OPAD , get_der_key} ;
2
- use core:: { fmt, slice } ;
2
+ use core:: fmt;
3
3
use digest:: {
4
- HashMarker , InvalidLength , KeyInit , MacMarker , Output ,
4
+ FixedOutput , HashMarker , InvalidLength , KeyInit , MacMarker , Output , Update ,
5
5
block_buffer:: Eager ,
6
- core_api:: {
7
- AlgorithmName , Block , BlockSizeUser , Buffer , BufferKindUser , CoreWrapper , FixedOutputCore ,
8
- OutputSizeUser , UpdateCore ,
9
- } ,
6
+ core_api:: { AlgorithmName , BlockSizeUser , BufferKindUser , OutputSizeUser } ,
10
7
crypto_common:: { Key , KeySizeUser } ,
11
8
} ;
12
9
13
- /// Generic HMAC instance.
14
- pub type Hmac < D > = CoreWrapper < HmacCore < D > > ;
10
+ ///// Generic HMAC instance.
11
+ // pub type Hmac<D> = CoreWrapper<HmacCore<D>>;
15
12
16
13
/// Trait implemented by eager hashes which expose their block-level core.
17
- pub trait EagerHash {
18
- /// Block-level core type of the hash.
19
- type Core : HashMarker
20
- + UpdateCore
21
- + FixedOutputCore
22
- + BufferKindUser < BufferKind = Eager >
23
- + Default
24
- + Clone ;
14
+ pub trait EagerHash :
15
+ HashMarker + Update + FixedOutput + BufferKindUser < BufferKind = Eager > + Default + Clone
16
+ {
25
17
}
26
18
27
- impl < C > EagerHash for CoreWrapper < C >
28
- where
29
- C : HashMarker
30
- + UpdateCore
31
- + FixedOutputCore
32
- + BufferKindUser < BufferKind = Eager >
33
- + Default
34
- + Clone ,
19
+ impl < D > EagerHash for D where
20
+ D : HashMarker + Update + FixedOutput + BufferKindUser < BufferKind = Eager > + Default + Clone
35
21
{
36
- type Core = C ;
37
22
}
38
23
39
24
/// Generic core HMAC instance, which operates over blocks.
40
- pub struct HmacCore < D : EagerHash > {
41
- digest : D :: Core ,
42
- opad_digest : D :: Core ,
25
+ pub struct Hmac < D : EagerHash > {
26
+ digest : D ,
27
+ opad_digest : D ,
43
28
#[ cfg( feature = "reset" ) ]
44
- ipad_digest : D :: Core ,
29
+ ipad_digest : D ,
45
30
}
46
31
47
- impl < D : EagerHash > Clone for HmacCore < D > {
32
+ impl < D : EagerHash > Clone for Hmac < D > {
48
33
fn clone ( & self ) -> Self {
49
34
Self {
50
35
digest : self . digest . clone ( ) ,
@@ -55,45 +40,45 @@ impl<D: EagerHash> Clone for HmacCore<D> {
55
40
}
56
41
}
57
42
58
- impl < D : EagerHash > MacMarker for HmacCore < D > { }
43
+ impl < D : EagerHash > MacMarker for Hmac < D > { }
59
44
60
- impl < D : EagerHash > BufferKindUser for HmacCore < D > {
45
+ impl < D : EagerHash > BufferKindUser for Hmac < D > {
61
46
type BufferKind = Eager ;
62
47
}
63
48
64
- impl < D : EagerHash > KeySizeUser for HmacCore < D > {
65
- type KeySize = << D as EagerHash > :: Core as BlockSizeUser >:: BlockSize ;
49
+ impl < D : EagerHash > KeySizeUser for Hmac < D > {
50
+ type KeySize = <D as BlockSizeUser >:: BlockSize ;
66
51
}
67
52
68
- impl < D : EagerHash > BlockSizeUser for HmacCore < D > {
69
- type BlockSize = << D as EagerHash > :: Core as BlockSizeUser >:: BlockSize ;
53
+ impl < D : EagerHash > BlockSizeUser for Hmac < D > {
54
+ type BlockSize = <D as BlockSizeUser >:: BlockSize ;
70
55
}
71
56
72
- impl < D : EagerHash > OutputSizeUser for HmacCore < D > {
73
- type OutputSize = << D as EagerHash > :: Core as OutputSizeUser >:: OutputSize ;
57
+ impl < D : EagerHash > OutputSizeUser for Hmac < D > {
58
+ type OutputSize = <D as OutputSizeUser >:: OutputSize ;
74
59
}
75
60
76
- impl < D : EagerHash > KeyInit for HmacCore < D > {
61
+ impl < D : EagerHash > KeyInit for Hmac < D > {
77
62
#[ inline( always) ]
78
63
fn new ( key : & Key < Self > ) -> Self {
79
64
Self :: new_from_slice ( key. as_slice ( ) ) . unwrap ( )
80
65
}
81
66
82
67
#[ inline( always) ]
83
68
fn new_from_slice ( key : & [ u8 ] ) -> Result < Self , InvalidLength > {
84
- let mut buf = get_der_key :: < CoreWrapper < D :: Core > > ( key) ;
69
+ let mut buf = get_der_key :: < D > ( key) ;
85
70
for b in buf. iter_mut ( ) {
86
71
* b ^= IPAD ;
87
72
}
88
- let mut digest = D :: Core :: default ( ) ;
89
- digest. update_blocks ( slice :: from_ref ( & buf) ) ;
73
+ let mut digest = D :: default ( ) ;
74
+ digest. update ( buf. as_slice ( ) ) ;
90
75
91
76
for b in buf. iter_mut ( ) {
92
77
* b ^= IPAD ^ OPAD ;
93
78
}
94
79
95
- let mut opad_digest = D :: Core :: default ( ) ;
96
- opad_digest. update_blocks ( slice :: from_ref ( & buf) ) ;
80
+ let mut opad_digest = D :: default ( ) ;
81
+ opad_digest. update ( buf. as_slice ( ) ) ;
97
82
98
83
Ok ( Self {
99
84
#[ cfg( feature = "reset" ) ]
@@ -104,57 +89,67 @@ impl<D: EagerHash> KeyInit for HmacCore<D> {
104
89
}
105
90
}
106
91
107
- impl < D : EagerHash > UpdateCore for HmacCore < D > {
92
+ impl < D : EagerHash > Update for Hmac < D > {
108
93
#[ inline( always) ]
109
- fn update_blocks ( & mut self , blocks : & [ Block < Self > ] ) {
110
- self . digest . update_blocks ( blocks ) ;
94
+ fn update ( & mut self , data : & [ u8 ] ) {
95
+ self . digest . update ( data ) ;
111
96
}
112
97
}
113
98
114
- impl < D : EagerHash > FixedOutputCore for HmacCore < D > {
99
+ impl < D : EagerHash > FixedOutput for Hmac < D > {
115
100
#[ inline( always) ]
116
- fn finalize_fixed_core ( & mut self , buffer : & mut Buffer < Self > , out : & mut Output < Self > ) {
117
- let mut hash = Output :: < D :: Core > :: default ( ) ;
118
- self . digest . finalize_fixed_core ( buffer , & mut hash ) ;
119
- // finalize_fixed_core should reset the buffer as well, but
120
- // to be extra safe we reset it explicitly again.
121
- buffer . reset ( ) ;
122
- # [ cfg ( not ( feature = "reset" ) ) ]
123
- let h = & mut self . opad_digest ;
124
- # [ cfg ( feature = "reset" ) ]
125
- let mut h = self . opad_digest . clone ( ) ;
126
- buffer . digest_blocks ( & hash, |b| h . update_blocks ( b ) ) ;
127
- h . finalize_fixed_core ( buffer , out) ;
101
+ fn finalize_into ( self , out : & mut Output < Self > ) {
102
+ let mut hash = Output :: < D > :: default ( ) ;
103
+
104
+ let Self {
105
+ digest ,
106
+ mut opad_digest ,
107
+ ..
108
+ } = self ;
109
+ digest . finalize_into ( & mut hash ) ;
110
+
111
+ opad_digest . update ( & hash) ;
112
+ opad_digest . finalize_into ( out) ;
128
113
}
129
114
}
130
115
131
116
#[ cfg( feature = "reset" ) ]
132
117
#[ cfg_attr( docsrs, doc( cfg( feature = "reset" ) ) ) ]
133
- impl < D : EagerHash > digest:: Reset for HmacCore < D > {
118
+ impl < D : EagerHash > digest:: Reset for Hmac < D > {
134
119
#[ inline( always) ]
135
120
fn reset ( & mut self ) {
136
121
self . digest = self . ipad_digest . clone ( ) ;
137
122
}
138
123
}
139
124
140
- impl < D : EagerHash > AlgorithmName for HmacCore < D >
125
+ #[ cfg( feature = "reset" ) ]
126
+ #[ cfg_attr( docsrs, doc( cfg( feature = "reset" ) ) ) ]
127
+ impl < D : EagerHash > digest:: FixedOutputReset for Hmac < D > {
128
+ #[ inline( always) ]
129
+ fn finalize_into_reset ( & mut self , out : & mut Output < Self > ) {
130
+ self . clone ( ) . finalize_into ( out) ;
131
+ <Self as digest:: Reset >:: reset ( self ) ;
132
+ }
133
+ }
134
+
135
+ impl < D : EagerHash > AlgorithmName for Hmac < D >
141
136
where
142
- D :: Core : AlgorithmName ,
137
+ D : AlgorithmName ,
143
138
{
144
139
fn write_alg_name ( f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
145
140
f. write_str ( "Hmac<" ) ?;
146
- <D :: Core as AlgorithmName >:: write_alg_name ( f) ?;
141
+ <D as AlgorithmName >:: write_alg_name ( f) ?;
147
142
f. write_str ( ">" )
148
143
}
149
144
}
150
145
151
- impl < D : EagerHash > fmt:: Debug for HmacCore < D >
146
+ impl < D : EagerHash > fmt:: Debug for Hmac < D >
152
147
where
153
- D :: Core : AlgorithmName ,
148
+ D : AlgorithmName ,
154
149
{
155
150
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
156
- f. write_str ( "HmacCore <" ) ?;
157
- <D :: Core as AlgorithmName >:: write_alg_name ( f) ?;
151
+ f. write_str ( "Hmac <" ) ?;
152
+ <D as AlgorithmName >:: write_alg_name ( f) ?;
158
153
f. write_str ( "> { ... }" )
159
154
}
160
155
}
0 commit comments