Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 691eec2

Browse files
authored
Merge pull request sfackler#313 from FauxFaux/update-sha2-0.7
Upgrade hmac, sha2 and generic_array
2 parents 9d200ea + bcc0996 commit 691eec2

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
build:
2828
working_directory: ~/build
2929
docker:
30-
- image: rust:1.19.0
30+
- image: rust:1.20.0
3131
environment:
3232
RUSTFLAGS: -D warnings
3333
- image: sfackler/rust-postgres-test:3

postgres-protocol/Cargo.toml

Lines changed: 3 additions & 3 deletions
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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
3434
}
3535

3636
fn hi(str: &[u8], salt: &[u8], i: u32) -> GenericArray<u8, U32> {
37-
let mut hmac = Hmac::<Sha256>::new(str);
37+
let mut hmac = Hmac::<Sha256>::new(str)
38+
.expect("HMAC is able to accept all key sizes");
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

@@ -150,26 +151,28 @@ impl ScramSha256 {
150151

151152
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+
.expect("HMAC is able to accept all key sizes");
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+
.expect("HMAC is able to accept all key sizes");
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+
.expect("HMAC is able to accept all key sizes");
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+
.expect("HMAC is able to accept all key sizes");
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

0 commit comments

Comments
 (0)