Skip to content

Commit 91090ef

Browse files
committed
Upgrade hmac, sha2 and generic_array
1 parent 9d200ea commit 91090ef

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

postgres-protocol/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ base64 = "0.6"
1212
byteorder = "1.0"
1313
bytes = "0.4"
1414
fallible-iterator = "0.1"
15-
generic-array = "0.8"
16-
hmac = "0.4"
15+
generic-array = "0.9"
16+
hmac = "0.5"
1717
md5 = "0.3"
1818
memchr = "1.0"
1919
rand = "0.3"
20-
sha2 = "0.6"
20+
sha2 = "0.7"
2121
stringprep = "0.1"

postgres-protocol/src/authentication/sasl.rs

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

36-
fn hi(str: &[u8], salt: &[u8], i: u32) -> GenericArray<u8, U32> {
37-
let mut hmac = Hmac::<Sha256>::new(str);
36+
fn hi(str: &[u8], salt: &[u8], i: u32) -> io::Result<GenericArray<u8, U32>> {
37+
let mut hmac = Hmac::<Sha256>::new(str)
38+
.map_err(|_| invalid_key_length_error())?;
3839
hmac.input(salt);
3940
hmac.input(&[0, 0, 0, 1]);
40-
let mut prev = hmac.result();
41+
let mut prev = hmac.result().code();
4142

42-
let mut hi = GenericArray::<u8, U32>::clone_from_slice(prev.code());
43+
let mut hi = GenericArray::<u8, U32>::clone_from_slice(&prev);
4344

4445
for _ in 1..i {
45-
let mut hmac = Hmac::<Sha256>::new(str);
46-
hmac.input(prev.code());
47-
prev = hmac.result();
46+
let mut hmac = Hmac::<Sha256>::new(str).expect("already checked above");
47+
hmac.input(prev.as_slice());
48+
prev = hmac.result().code();
4849

49-
for (hi, prev) in hi.iter_mut().zip(prev.code()) {
50-
*hi ^= *prev;
50+
for (hi, prev) in hi.iter_mut().zip(prev) {
51+
*hi ^= prev;
5152
}
5253
}
5354

54-
hi
55+
Ok(hi)
5556
}
5657

5758
enum State {
@@ -148,28 +149,30 @@ impl ScramSha256 {
148149
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
149150
};
150151

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

153-
let mut hmac = Hmac::<Sha256>::new(&salted_password);
154+
let mut hmac = Hmac::<Sha256>::new(&salted_password)
155+
.map_err(|_| invalid_key_length_error())?;
154156
hmac.input(b"Client Key");
155-
let client_key = hmac.result();
157+
let client_key = hmac.result().code();
156158

157159
let mut hash = Sha256::default();
158-
hash.input(client_key.code());
160+
hash.input(client_key.as_slice());
159161
let stored_key = hash.result();
160162

161163
self.message.clear();
162164
write!(&mut self.message, "c=biws,r={}", parsed.nonce).unwrap();
163165

164166
let auth_message = format!("n=,r={},{},{}", client_nonce, message, self.message);
165167

166-
let mut hmac = Hmac::<Sha256>::new(&stored_key);
168+
let mut hmac = Hmac::<Sha256>::new(&stored_key)
169+
.map_err(|_| invalid_key_length_error())?;
167170
hmac.input(auth_message.as_bytes());
168171
let client_signature = hmac.result();
169172

170-
let mut client_proof = GenericArray::<u8, U32>::clone_from_slice(client_key.code());
173+
let mut client_proof = GenericArray::<u8, U32>::clone_from_slice(&client_key);
171174
for (proof, signature) in client_proof.iter_mut().zip(client_signature.code()) {
172-
*proof ^= *signature;
175+
*proof ^= signature;
173176
}
174177

175178
write!(&mut self.message, ",p={}", base64::encode(&*client_proof)).unwrap();
@@ -215,20 +218,18 @@ impl ScramSha256 {
215218
Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidInput, e)),
216219
};
217220

218-
let mut hmac = Hmac::<Sha256>::new(&salted_password);
221+
let mut hmac = Hmac::<Sha256>::new(&salted_password)
222+
.map_err(|_| invalid_key_length_error())?;
219223
hmac.input(b"Server Key");
220224
let server_key = hmac.result();
221225

222-
let mut hmac = Hmac::<Sha256>::new(server_key.code());
226+
let mut hmac = Hmac::<Sha256>::new(&server_key.code())
227+
.map_err(|_| invalid_key_length_error())?;
223228
hmac.input(auth_message.as_bytes());
224-
if hmac.verify(&verifier) {
225-
Ok(())
226-
} else {
227-
Err(io::Error::new(
228-
io::ErrorKind::InvalidInput,
229-
"SCRAM verification error",
230-
))
231-
}
229+
hmac.verify(&verifier).map_err(|_| io::Error::new(
230+
io::ErrorKind::InvalidInput,
231+
"SCRAM verification error",
232+
))
232233
}
233234
}
234235

@@ -398,6 +399,10 @@ enum ServerFinalMessage<'a> {
398399
Verifier(&'a str),
399400
}
400401

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

0 commit comments

Comments
 (0)