11use super :: { IPAD , OPAD , get_der_key} ;
2- use core:: { fmt, slice } ;
2+ use core:: fmt;
33use digest:: {
4- HashMarker , InvalidLength , KeyInit , MacMarker , Output ,
4+ FixedOutput , HashMarker , InvalidLength , KeyInit , MacMarker , Output , Update ,
55 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 } ,
107 crypto_common:: { Key , KeySizeUser } ,
118} ;
129
13- /// Generic HMAC instance.
14- pub type Hmac < D > = CoreWrapper < HmacCore < D > > ;
10+ ///// Generic HMAC instance.
11+ // pub type Hmac<D> = CoreWrapper<HmacCore<D>>;
1512
1613/// 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+ {
2517}
2618
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
3521{
36- type Core = C ;
3722}
3823
3924/// 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 ,
4328 #[ cfg( feature = "reset" ) ]
44- ipad_digest : D :: Core ,
29+ ipad_digest : D ,
4530}
4631
47- impl < D : EagerHash > Clone for HmacCore < D > {
32+ impl < D : EagerHash > Clone for Hmac < D > {
4833 fn clone ( & self ) -> Self {
4934 Self {
5035 digest : self . digest . clone ( ) ,
@@ -55,45 +40,45 @@ impl<D: EagerHash> Clone for HmacCore<D> {
5540 }
5641}
5742
58- impl < D : EagerHash > MacMarker for HmacCore < D > { }
43+ impl < D : EagerHash > MacMarker for Hmac < D > { }
5944
60- impl < D : EagerHash > BufferKindUser for HmacCore < D > {
45+ impl < D : EagerHash > BufferKindUser for Hmac < D > {
6146 type BufferKind = Eager ;
6247}
6348
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 ;
6651}
6752
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 ;
7055}
7156
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 ;
7459}
7560
76- impl < D : EagerHash > KeyInit for HmacCore < D > {
61+ impl < D : EagerHash > KeyInit for Hmac < D > {
7762 #[ inline( always) ]
7863 fn new ( key : & Key < Self > ) -> Self {
7964 Self :: new_from_slice ( key. as_slice ( ) ) . unwrap ( )
8065 }
8166
8267 #[ inline( always) ]
8368 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) ;
8570 for b in buf. iter_mut ( ) {
8671 * b ^= IPAD ;
8772 }
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 ( ) ) ;
9075
9176 for b in buf. iter_mut ( ) {
9277 * b ^= IPAD ^ OPAD ;
9378 }
9479
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 ( ) ) ;
9782
9883 Ok ( Self {
9984 #[ cfg( feature = "reset" ) ]
@@ -104,57 +89,67 @@ impl<D: EagerHash> KeyInit for HmacCore<D> {
10489 }
10590}
10691
107- impl < D : EagerHash > UpdateCore for HmacCore < D > {
92+ impl < D : EagerHash > Update for Hmac < D > {
10893 #[ 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 ) ;
11196 }
11297}
11398
114- impl < D : EagerHash > FixedOutputCore for HmacCore < D > {
99+ impl < D : EagerHash > FixedOutput for Hmac < D > {
115100 #[ 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) ;
128113 }
129114}
130115
131116#[ cfg( feature = "reset" ) ]
132117#[ 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 > {
134119 #[ inline( always) ]
135120 fn reset ( & mut self ) {
136121 self . digest = self . ipad_digest . clone ( ) ;
137122 }
138123}
139124
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 >
141136where
142- D :: Core : AlgorithmName ,
137+ D : AlgorithmName ,
143138{
144139 fn write_alg_name ( f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
145140 f. write_str ( "Hmac<" ) ?;
146- <D :: Core as AlgorithmName >:: write_alg_name ( f) ?;
141+ <D as AlgorithmName >:: write_alg_name ( f) ?;
147142 f. write_str ( ">" )
148143 }
149144}
150145
151- impl < D : EagerHash > fmt:: Debug for HmacCore < D >
146+ impl < D : EagerHash > fmt:: Debug for Hmac < D >
152147where
153- D :: Core : AlgorithmName ,
148+ D : AlgorithmName ,
154149{
155150 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) ?;
158153 f. write_str ( "> { ... }" )
159154 }
160155}
0 commit comments