Skip to content

Upgrade hmac, sha2 and generic_array #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
build:
working_directory: ~/build
docker:
- image: rust:1.19.0
- image: rust:1.20.0
environment:
RUSTFLAGS: -D warnings
- image: sfackler/rust-postgres-test:3
Expand Down
6 changes: 3 additions & 3 deletions postgres-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ base64 = "0.6"
byteorder = "1.0"
bytes = "0.4"
fallible-iterator = "0.1"
generic-array = "0.8"
hmac = "0.4"
generic-array = "0.9"
hmac = "0.5"
md5 = "0.3"
memchr = "1.0"
rand = "0.3"
sha2 = "0.6"
sha2 = "0.7"
stringprep = "0.1"
49 changes: 25 additions & 24 deletions postgres-protocol/src/authentication/sasl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
}

fn hi(str: &[u8], salt: &[u8], i: u32) -> GenericArray<u8, U32> {
let mut hmac = Hmac::<Sha256>::new(str);
let mut hmac = Hmac::<Sha256>::new(str)
.expect("HMAC is able to accept all key sizes");
hmac.input(salt);
hmac.input(&[0, 0, 0, 1]);
let mut prev = hmac.result();
let mut prev = hmac.result().code();

let mut hi = GenericArray::<u8, U32>::clone_from_slice(prev.code());
let mut hi = GenericArray::<u8, U32>::clone_from_slice(&prev);

for _ in 1..i {
let mut hmac = Hmac::<Sha256>::new(str);
hmac.input(prev.code());
prev = hmac.result();
let mut hmac = Hmac::<Sha256>::new(str).expect("already checked above");
hmac.input(prev.as_slice());
prev = hmac.result().code();

for (hi, prev) in hi.iter_mut().zip(prev.code()) {
*hi ^= *prev;
for (hi, prev) in hi.iter_mut().zip(prev) {
*hi ^= prev;
}
}

Expand Down Expand Up @@ -150,26 +151,28 @@ impl ScramSha256 {

let salted_password = hi(&password, &salt, parsed.iteration_count);

let mut hmac = Hmac::<Sha256>::new(&salted_password);
let mut hmac = Hmac::<Sha256>::new(&salted_password)
.expect("HMAC is able to accept all key sizes");
hmac.input(b"Client Key");
let client_key = hmac.result();
let client_key = hmac.result().code();

let mut hash = Sha256::default();
hash.input(client_key.code());
hash.input(client_key.as_slice());
let stored_key = hash.result();

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

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

let mut hmac = Hmac::<Sha256>::new(&stored_key);
let mut hmac = Hmac::<Sha256>::new(&stored_key)
.expect("HMAC is able to accept all key sizes");
hmac.input(auth_message.as_bytes());
let client_signature = hmac.result();

let mut client_proof = GenericArray::<u8, U32>::clone_from_slice(client_key.code());
let mut client_proof = GenericArray::<u8, U32>::clone_from_slice(&client_key);
for (proof, signature) in client_proof.iter_mut().zip(client_signature.code()) {
*proof ^= *signature;
*proof ^= signature;
}

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

let mut hmac = Hmac::<Sha256>::new(&salted_password);
let mut hmac = Hmac::<Sha256>::new(&salted_password)
.expect("HMAC is able to accept all key sizes");
hmac.input(b"Server Key");
let server_key = hmac.result();

let mut hmac = Hmac::<Sha256>::new(server_key.code());
let mut hmac = Hmac::<Sha256>::new(&server_key.code())
.expect("HMAC is able to accept all key sizes");
hmac.input(auth_message.as_bytes());
if hmac.verify(&verifier) {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"SCRAM verification error",
))
}
hmac.verify(&verifier).map_err(|_| io::Error::new(
io::ErrorKind::InvalidInput,
"SCRAM verification error",
))
}
}

Expand Down