@@ -3,7 +3,7 @@ use ring::{hmac, signature};
3
3
use crate :: algorithms:: Algorithm ;
4
4
use crate :: decoding:: { DecodingKey , DecodingKeyKind } ;
5
5
use crate :: encoding:: EncodingKey ;
6
- use crate :: errors:: { Error , Result } ;
6
+ use crate :: errors:: Result ;
7
7
use crate :: serialization:: { b64_decode, b64_encode} ;
8
8
9
9
pub ( crate ) mod ecdsa;
@@ -20,17 +20,6 @@ fn alg_to_hmac(alg: Algorithm) -> hmac::Algorithm {
20
20
}
21
21
}
22
22
23
- /// Returns `Ok(())` if `a == b` and `Err(error::Unspecified)` otherwise.
24
- pub fn verify_slices_are_equal ( a : & [ u8 ] , b : & [ u8 ] ) -> Result < ( ) > {
25
- if b. len ( ) != a. len ( ) {
26
- return Err ( Error :: from ( ring:: error:: Unspecified ) ) ;
27
- }
28
- match openssl:: memcmp:: eq ( a, b) {
29
- true => Ok ( ( ) ) ,
30
- _ => Err ( Error :: from ( ring:: error:: Unspecified ) ) ,
31
- }
32
- }
33
-
34
23
/// The actual HS signing + encoding
35
24
/// Could be in its own file to match RSA/EC but it's 2 lines...
36
25
pub ( crate ) fn sign_hmac ( alg : hmac:: Algorithm , key : & [ u8 ] , message : & [ u8 ] ) -> String {
@@ -94,8 +83,10 @@ pub fn verify(
94
83
match algorithm {
95
84
Algorithm :: HS256 | Algorithm :: HS384 | Algorithm :: HS512 => {
96
85
// we just re-sign the message with the key and compare if they are equal
97
- let signed = sign ( message, & EncodingKey :: from_secret ( key. as_bytes ( ) ) , algorithm) ?;
98
- Ok ( verify_slices_are_equal ( signature. as_ref ( ) , signed. as_ref ( ) ) . is_ok ( ) )
86
+ let encoding_key = & EncodingKey :: from_secret ( key. as_bytes ( ) ) ;
87
+ let key = & hmac:: Key :: new ( alg_to_hmac ( algorithm) , encoding_key. inner ( ) ) ;
88
+ let digest = hmac:: sign ( key, message) ;
89
+ Ok ( hmac:: verify ( key, message, digest. as_ref ( ) ) . is_ok ( ) )
99
90
}
100
91
Algorithm :: ES256 | Algorithm :: ES384 => verify_ring (
101
92
ecdsa:: alg_to_ec_verification ( algorithm) ,
0 commit comments