Skip to content

Commit 671b0f4

Browse files
bugadanipatrickfreed
authored andcommitted
RUST-682 Update encryption dependencies
1 parent 01e04b9 commit 671b0f4

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ derivative = "2.1.1"
4040
err-derive = "0.2.3"
4141
futures = "0.3.5"
4242
hex = "0.4.0"
43-
hmac = "~0.7.1"
43+
hmac = "0.10.1"
4444
lazy_static = "1.4.0"
45-
md-5 = "0.8.0"
45+
md-5 = "0.9.1"
4646
os_info = { version = "3.0.1", default-features = false }
4747
percent-encoding = "2.0.0"
4848
rand = { version = "0.7.2", features = ["small_rng"] }
4949
serde_with = "1.3.1"
50-
sha-1 = "0.8.1"
51-
sha2 = "0.8.0"
50+
sha-1 = "0.9.4"
51+
sha2 = "0.9.3"
5252
socket2 = "0.3.12"
5353
stringprep = "0.1.2"
5454
strsim = "0.10.0"
@@ -59,14 +59,14 @@ trust-dns-resolver = "0.19.5"
5959
typed-builder = "0.4.0"
6060
version_check = "0.9.1"
6161
webpki = "0.21.0"
62-
webpki-roots = "0.18.0"
62+
webpki-roots = "0.21.0"
6363

6464
[dependencies.async-std]
6565
version = "1.6.2"
6666
optional = true
6767

6868
[dependencies.pbkdf2]
69-
version = "0.3.0"
69+
version = "0.6.0"
7070
default-features = false
7171

7272
[dependencies.reqwest]

src/client/auth/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod x509;
1212

1313
use std::{borrow::Cow, fmt::Debug, str::FromStr};
1414

15-
use hmac::Mac;
15+
use hmac::{Mac, NewMac};
1616
use rand::Rng;
1717
use serde::Deserialize;
1818
use typed_builder::TypedBuilder;
@@ -490,9 +490,13 @@ pub(crate) fn generate_nonce() -> String {
490490
base64::encode(&result)
491491
}
492492

493-
fn mac<M: Mac>(key: &[u8], input: &[u8], auth_mechanism: &str) -> Result<impl AsRef<[u8]>> {
493+
fn mac<M: Mac + NewMac>(
494+
key: &[u8],
495+
input: &[u8],
496+
auth_mechanism: &str,
497+
) -> Result<impl AsRef<[u8]>> {
494498
let mut mac =
495499
M::new_varkey(key).map_err(|_| Error::unknown_authentication_error(auth_mechanism))?;
496-
mac.input(input);
497-
Ok(mac.result().code())
500+
mac.update(input);
501+
Ok(mac.finalize().into_bytes())
498502
}

src/client/auth/scram.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use std::{
77
sync::RwLock,
88
};
99

10-
use hmac::{Hmac, Mac};
10+
use hmac::{digest::Digest, Hmac, Mac, NewMac};
1111
use lazy_static::lazy_static;
1212
use md5::Md5;
13-
use sha1::{Digest, Sha1};
13+
use sha1::Sha1;
1414
use sha2::Sha256;
1515

1616
use crate::{
@@ -42,7 +42,7 @@ const USERNAME_KEY: char = 'n';
4242
const NO_CHANNEL_BINDING: char = 'n';
4343

4444
/// The minimum number of iterations of the hash function that we will accept from the server.
45-
const MIN_ITERATION_COUNT: usize = 4096;
45+
const MIN_ITERATION_COUNT: u32 = 4096;
4646

4747
lazy_static! {
4848
/// Cache of pre-computed salted passwords.
@@ -55,7 +55,7 @@ lazy_static! {
5555
struct CacheEntry {
5656
password: String,
5757
salt: Vec<u8>,
58-
i: usize,
58+
i: u32,
5959
mechanism: ScramVersion,
6060
}
6161

@@ -298,7 +298,7 @@ impl ScramVersion {
298298
}
299299

300300
/// The "h_i" function as defined in the SCRAM RFC.
301-
fn h_i(&self, str: &str, salt: &[u8], iterations: usize) -> Vec<u8> {
301+
fn h_i(&self, str: &str, salt: &[u8], iterations: u32) -> Vec<u8> {
302302
match self {
303303
ScramVersion::Sha1 => h_i::<Hmac<Sha1>>(str, salt, iterations, 160 / 8),
304304
ScramVersion::Sha256 => h_i::<Hmac<Sha256>>(str, salt, iterations, 256 / 8),
@@ -311,14 +311,14 @@ impl ScramVersion {
311311
&self,
312312
username: &str,
313313
password: &str,
314-
i: usize,
314+
i: u32,
315315
salt: &[u8],
316316
) -> Result<Vec<u8>> {
317317
let normalized_password = match self {
318318
ScramVersion::Sha1 => {
319319
let mut md5 = Md5::new();
320-
md5.input(format!("{}:mongo:{}", username, password));
321-
Cow::Owned(hex::encode(md5.result()))
320+
md5.update(format!("{}:mongo:{}", username, password));
321+
Cow::Owned(hex::encode(md5.finalize()))
322322
}
323323
ScramVersion::Sha256 => match stringprep::saslprep(password) {
324324
Ok(p) => p,
@@ -353,9 +353,9 @@ fn xor(lhs: &[u8], rhs: &[u8]) -> Vec<u8> {
353353
.collect()
354354
}
355355

356-
fn mac_verify<M: Mac>(key: &[u8], input: &[u8], signature: &[u8]) -> Result<()> {
356+
fn mac_verify<M: Mac + NewMac>(key: &[u8], input: &[u8], signature: &[u8]) -> Result<()> {
357357
let mut mac = M::new_varkey(key).map_err(|_| Error::unknown_authentication_error("SCRAM"))?;
358-
mac.input(input);
358+
mac.update(input);
359359
match mac.verify(signature) {
360360
Ok(_) => Ok(()),
361361
Err(_) => Err(Error::authentication_error(
@@ -367,11 +367,16 @@ fn mac_verify<M: Mac>(key: &[u8], input: &[u8], signature: &[u8]) -> Result<()>
367367

368368
fn hash<D: Digest>(val: &[u8]) -> Vec<u8> {
369369
let mut hash = D::new();
370-
hash.input(val);
371-
hash.result().to_vec()
370+
hash.update(val);
371+
hash.finalize().to_vec()
372372
}
373373

374-
fn h_i<M: Mac + Sync>(str: &str, salt: &[u8], iterations: usize, output_size: usize) -> Vec<u8> {
374+
fn h_i<M: Mac + NewMac + Sync>(
375+
str: &str,
376+
salt: &[u8],
377+
iterations: u32,
378+
output_size: usize,
379+
) -> Vec<u8> {
375380
let mut buf = vec![0u8; output_size];
376381
pbkdf2::pbkdf2::<M>(str.as_bytes(), salt, iterations, buf.as_mut_slice());
377382
buf
@@ -469,7 +474,7 @@ struct ServerFirst {
469474
message: String,
470475
nonce: String,
471476
salt: Vec<u8>,
472-
i: usize,
477+
i: u32,
473478
}
474479

475480
impl ServerFirst {
@@ -494,7 +499,7 @@ impl ServerFirst {
494499
let salt = base64::decode(parse_kvp(parts[1], SALT_KEY)?.as_str())
495500
.map_err(|_| Error::invalid_authentication_response("SCRAM"))?;
496501

497-
let i: usize = match parse_kvp(parts[2], ITERATION_COUNT_KEY)?.parse() {
502+
let i: u32 = match parse_kvp(parts[2], ITERATION_COUNT_KEY)?.parse() {
498503
Ok(num) => num,
499504
Err(_) => {
500505
return Err(Error::authentication_error(
@@ -530,7 +535,7 @@ impl ServerFirst {
530535
self.salt.as_slice()
531536
}
532537

533-
fn i(&self) -> usize {
538+
fn i(&self) -> u32 {
534539
self.i
535540
}
536541

0 commit comments

Comments
 (0)