2
2
//! According to that organization, only hmac is safely implemented at the
3
3
//! moment.
4
4
5
- use crypto_mac:: Mac ;
6
- use digest:: generic_array:: ArrayLength ;
7
- use digest:: { BlockInput , FixedOutput , Reset , Update } ;
5
+ use digest:: core_api:: { CoreProxy , FixedOutputCore } ;
6
+ use digest:: {
7
+ block_buffer:: Eager ,
8
+ consts:: U256 ,
9
+ core_api:: { BlockSizeUser , BufferKindUser } ,
10
+ generic_array:: typenum:: { IsLess , Le , NonZero } ,
11
+ HashMarker , Mac ,
12
+ } ;
8
13
use hmac:: Hmac ;
9
14
10
15
use crate :: algorithm:: { AlgorithmType , SigningAlgorithm , VerifyingAlgorithm } ;
@@ -34,16 +39,22 @@ type_level_algorithm_type!(sha2::Sha512, AlgorithmType::Hs512);
34
39
35
40
impl < D > SigningAlgorithm for Hmac < D >
36
41
where
37
- D : Update + BlockInput + FixedOutput + Reset + Default + Clone + TypeLevelAlgorithmType ,
38
- D :: BlockSize : ArrayLength < u8 > ,
39
- D :: OutputSize : ArrayLength < u8 > ,
42
+ D : CoreProxy + TypeLevelAlgorithmType ,
43
+ D :: Core : HashMarker
44
+ + BufferKindUser < BufferKind = Eager >
45
+ + FixedOutputCore
46
+ + digest:: Reset
47
+ + Default
48
+ + Clone ,
49
+ <D :: Core as BlockSizeUser >:: BlockSize : IsLess < U256 > ,
50
+ Le < <D :: Core as BlockSizeUser >:: BlockSize , U256 > : NonZero ,
40
51
{
41
52
fn algorithm_type ( & self ) -> AlgorithmType {
42
53
D :: algorithm_type ( )
43
54
}
44
55
45
56
fn sign ( & self , header : & str , claims : & str ) -> Result < String , Error > {
46
- let hmac = get_hmac_with_data ( & self , header, claims) ;
57
+ let hmac = get_hmac_with_data ( self , header, claims) ;
47
58
let mac_result = hmac. finalize ( ) ;
48
59
let code = mac_result. into_bytes ( ) ;
49
60
Ok ( base64:: encode_config ( & code, base64:: URL_SAFE_NO_PAD ) )
@@ -52,26 +63,38 @@ where
52
63
53
64
impl < D > VerifyingAlgorithm for Hmac < D >
54
65
where
55
- D : Update + BlockInput + FixedOutput + Reset + Default + Clone + TypeLevelAlgorithmType ,
56
- D :: BlockSize : ArrayLength < u8 > ,
57
- D :: OutputSize : ArrayLength < u8 > ,
66
+ D : CoreProxy + TypeLevelAlgorithmType ,
67
+ D :: Core : HashMarker
68
+ + BufferKindUser < BufferKind = Eager >
69
+ + FixedOutputCore
70
+ + digest:: Reset
71
+ + Default
72
+ + Clone ,
73
+ <D :: Core as BlockSizeUser >:: BlockSize : IsLess < U256 > ,
74
+ Le < <D :: Core as BlockSizeUser >:: BlockSize , U256 > : NonZero ,
58
75
{
59
76
fn algorithm_type ( & self ) -> AlgorithmType {
60
77
D :: algorithm_type ( )
61
78
}
62
79
63
80
fn verify_bytes ( & self , header : & str , claims : & str , signature : & [ u8 ] ) -> Result < bool , Error > {
64
81
let hmac = get_hmac_with_data ( self , header, claims) ;
65
- hmac. verify ( & signature) ?;
82
+ hmac. verify_slice ( signature) ?;
66
83
Ok ( true )
67
84
}
68
85
}
69
86
70
87
fn get_hmac_with_data < D > ( hmac : & Hmac < D > , header : & str , claims : & str ) -> Hmac < D >
71
88
where
72
- D : Update + BlockInput + FixedOutput + Reset + Default + Clone + TypeLevelAlgorithmType ,
73
- D :: BlockSize : ArrayLength < u8 > ,
74
- D :: OutputSize : ArrayLength < u8 > ,
89
+ D : CoreProxy ,
90
+ D :: Core : HashMarker
91
+ + BufferKindUser < BufferKind = Eager >
92
+ + FixedOutputCore
93
+ + digest:: Reset
94
+ + Default
95
+ + Clone ,
96
+ <D :: Core as BlockSizeUser >:: BlockSize : IsLess < U256 > ,
97
+ Le < <D :: Core as BlockSizeUser >:: BlockSize , U256 > : NonZero ,
75
98
{
76
99
let mut hmac = hmac. clone ( ) ;
77
100
hmac. reset ( ) ;
85
108
mod tests {
86
109
use crate :: algorithm:: { SigningAlgorithm , VerifyingAlgorithm } ;
87
110
use crate :: error:: Error ;
88
- use crypto_mac :: NewMac ;
111
+ use digest :: Mac ;
89
112
use hmac:: Hmac ;
90
113
use sha2:: Sha256 ;
91
114
@@ -96,7 +119,7 @@ mod tests {
96
119
let expected_signature = "TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ" ;
97
120
98
121
let signer: Hmac < Sha256 > = Hmac :: new_from_slice ( b"secret" ) ?;
99
- let computed_signature = SigningAlgorithm :: sign ( & signer, & header, & claims) ?;
122
+ let computed_signature = SigningAlgorithm :: sign ( & signer, header, claims) ?;
100
123
101
124
assert_eq ! ( computed_signature, expected_signature) ;
102
125
Ok ( ( ) )
@@ -110,7 +133,7 @@ mod tests {
110
133
111
134
let verifier: Hmac < Sha256 > = Hmac :: new_from_slice ( b"secret" ) ?;
112
135
assert ! ( VerifyingAlgorithm :: verify(
113
- & verifier, & header, & claims, & signature
136
+ & verifier, header, claims, signature
114
137
) ?) ;
115
138
Ok ( ( ) )
116
139
}
0 commit comments