Skip to content

Commit bcc0996

Browse files
committed
Hmac::new() is infalliable
1 parent 50e17ac commit bcc0996

File tree

1 file changed

+8
-12
lines changed
  • postgres-protocol/src/authentication

1 file changed

+8
-12
lines changed

postgres-protocol/src/authentication/sasl.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
3333
}
3434
}
3535

36-
fn hi(str: &[u8], salt: &[u8], i: u32) -> io::Result<GenericArray<u8, U32>> {
36+
fn hi(str: &[u8], salt: &[u8], i: u32) -> GenericArray<u8, U32> {
3737
let mut hmac = Hmac::<Sha256>::new(str)
38-
.map_err(|_| invalid_key_length_error())?;
38+
.expect("HMAC is able to accept all key sizes");
3939
hmac.input(salt);
4040
hmac.input(&[0, 0, 0, 1]);
4141
let mut prev = hmac.result().code();
@@ -52,7 +52,7 @@ fn hi(str: &[u8], salt: &[u8], i: u32) -> io::Result<GenericArray<u8, U32>> {
5252
}
5353
}
5454

55-
Ok(hi)
55+
hi
5656
}
5757

5858
enum State {
@@ -149,10 +149,10 @@ impl ScramSha256 {
149149
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
150150
};
151151

152-
let salted_password = hi(&password, &salt, parsed.iteration_count)?;
152+
let salted_password = hi(&password, &salt, parsed.iteration_count);
153153

154154
let mut hmac = Hmac::<Sha256>::new(&salted_password)
155-
.map_err(|_| invalid_key_length_error())?;
155+
.expect("HMAC is able to accept all key sizes");
156156
hmac.input(b"Client Key");
157157
let client_key = hmac.result().code();
158158

@@ -166,7 +166,7 @@ impl ScramSha256 {
166166
let auth_message = format!("n=,r={},{},{}", client_nonce, message, self.message);
167167

168168
let mut hmac = Hmac::<Sha256>::new(&stored_key)
169-
.map_err(|_| invalid_key_length_error())?;
169+
.expect("HMAC is able to accept all key sizes");
170170
hmac.input(auth_message.as_bytes());
171171
let client_signature = hmac.result();
172172

@@ -219,12 +219,12 @@ impl ScramSha256 {
219219
};
220220

221221
let mut hmac = Hmac::<Sha256>::new(&salted_password)
222-
.map_err(|_| invalid_key_length_error())?;
222+
.expect("HMAC is able to accept all key sizes");
223223
hmac.input(b"Server Key");
224224
let server_key = hmac.result();
225225

226226
let mut hmac = Hmac::<Sha256>::new(&server_key.code())
227-
.map_err(|_| invalid_key_length_error())?;
227+
.expect("HMAC is able to accept all key sizes");
228228
hmac.input(auth_message.as_bytes());
229229
hmac.verify(&verifier).map_err(|_| io::Error::new(
230230
io::ErrorKind::InvalidInput,
@@ -399,10 +399,6 @@ enum ServerFinalMessage<'a> {
399399
Verifier(&'a str),
400400
}
401401

402-
fn invalid_key_length_error() -> io::Error {
403-
io::Error::new(io::ErrorKind::InvalidInput, "invalid key length")
404-
}
405-
406402
#[cfg(test)]
407403
mod test {
408404
use super::*;

0 commit comments

Comments
 (0)