Skip to content

Commit 58a7856

Browse files
committed
Update hmac and sha2
1 parent 2b59b7e commit 58a7856

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

postgres-protocol/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ base64 = "0.12"
1313
byteorder = "1.0"
1414
bytes = "0.5"
1515
fallible-iterator = "0.2"
16-
hmac = "0.7"
16+
hmac = "0.8"
1717
md5 = "0.7"
1818
memchr = "2.0"
1919
rand = "0.7"
20-
sha2 = "0.8"
20+
sha2 = "0.9"
2121
stringprep = "0.1"

postgres-protocol/src/authentication/sasl.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! SASL-based authentication support.
22
3-
use hmac::{Hmac, Mac};
3+
use hmac::{Hmac, Mac, NewMac};
44
use rand::{self, Rng};
55
use sha2::{Digest, Sha256};
6+
use sha2::digest::FixedOutput;
67
use std::fmt::Write;
78
use std::io;
89
use std::iter;
@@ -33,16 +34,16 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
3334

3435
fn hi(str: &[u8], salt: &[u8], i: u32) -> [u8; 32] {
3536
let mut hmac = Hmac::<Sha256>::new_varkey(str).expect("HMAC is able to accept all key sizes");
36-
hmac.input(salt);
37-
hmac.input(&[0, 0, 0, 1]);
38-
let mut prev = hmac.result().code();
37+
hmac.update(salt);
38+
hmac.update(&[0, 0, 0, 1]);
39+
let mut prev = hmac.finalize().into_bytes();
3940

4041
let mut hi = prev;
4142

4243
for _ in 1..i {
4344
let mut hmac = Hmac::<Sha256>::new_varkey(str).expect("already checked above");
44-
hmac.input(prev.as_slice());
45-
prev = hmac.result().code();
45+
hmac.update(&prev);
46+
prev = hmac.finalize().into_bytes();
4647

4748
for (hi, prev) in hi.iter_mut().zip(prev) {
4849
*hi ^= prev;
@@ -196,12 +197,12 @@ impl ScramSha256 {
196197

197198
let mut hmac = Hmac::<Sha256>::new_varkey(&salted_password)
198199
.expect("HMAC is able to accept all key sizes");
199-
hmac.input(b"Client Key");
200-
let client_key = hmac.result().code();
200+
hmac.update(b"Client Key");
201+
let client_key = hmac.finalize().into_bytes();
201202

202203
let mut hash = Sha256::default();
203-
hash.input(client_key.as_slice());
204-
let stored_key = hash.result();
204+
hash.update(client_key.as_slice());
205+
let stored_key = hash.finalize_fixed();
205206

206207
let mut cbind_input = vec![];
207208
cbind_input.extend(channel_binding.gs2_header().as_bytes());
@@ -215,11 +216,11 @@ impl ScramSha256 {
215216

216217
let mut hmac =
217218
Hmac::<Sha256>::new_varkey(&stored_key).expect("HMAC is able to accept all key sizes");
218-
hmac.input(auth_message.as_bytes());
219-
let client_signature = hmac.result();
219+
hmac.update(auth_message.as_bytes());
220+
let client_signature = hmac.finalize().into_bytes();
220221

221222
let mut client_proof = client_key;
222-
for (proof, signature) in client_proof.iter_mut().zip(client_signature.code()) {
223+
for (proof, signature) in client_proof.iter_mut().zip(client_signature) {
223224
*proof ^= signature;
224225
}
225226

@@ -267,12 +268,12 @@ impl ScramSha256 {
267268

268269
let mut hmac = Hmac::<Sha256>::new_varkey(&salted_password)
269270
.expect("HMAC is able to accept all key sizes");
270-
hmac.input(b"Server Key");
271-
let server_key = hmac.result();
271+
hmac.update(b"Server Key");
272+
let server_key = hmac.finalize().into_bytes();
272273

273-
let mut hmac = Hmac::<Sha256>::new_varkey(&server_key.code())
274+
let mut hmac = Hmac::<Sha256>::new_varkey(&server_key)
274275
.expect("HMAC is able to accept all key sizes");
275-
hmac.input(auth_message.as_bytes());
276+
hmac.update(auth_message.as_bytes());
276277
hmac.verify(&verifier)
277278
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "SCRAM verification error"))
278279
}

0 commit comments

Comments
 (0)