Skip to content

Commit 5296a91

Browse files
bmc-msftdemoray
andauthored
move to hmac instead of ring for hmac calculation (#662)
Co-authored-by: Brian Caswell <bcaswell@microsoft.com>
1 parent 58fcc4e commit 5296a91

File tree

13 files changed

+40
-45
lines changed

13 files changed

+40
-45
lines changed

sdk/data_cosmos/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ edition = "2018"
1717
[dependencies]
1818
async-trait = "0.1"
1919
azure_core = { path = "../core", version = "0.1" }
20-
ring = "0.16"
2120
base64 = "0.13"
2221
chrono = "0.4"
2322
http = "0.2"
@@ -29,6 +28,9 @@ url = "2.2"
2928
uuid = { version = "0.8", features = ["v4"] }
3029
thiserror = "1.0"
3130
bytes = "1.0"
31+
hmac = "0.12"
32+
sha2 = "0.10"
33+
3234

3335
[dev-dependencies]
3436
env_logger = "0.9"

sdk/data_cosmos/src/authorization_policy.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use crate::resources::permission::AuthorizationToken;
33
use crate::resources::ResourceType;
44
use crate::TimeNonce;
55
use azure_core::{Context, Policy, PolicyResult, Request};
6+
use hmac::{Hmac, Mac};
67
use http::header::AUTHORIZATION;
78
use http::HeaderValue;
8-
use ring::hmac;
9+
use sha2::Sha256;
910
use std::borrow::Cow;
1011
use std::sync::Arc;
1112
use url::form_urlencoded;
@@ -228,10 +229,11 @@ fn string_to_sign(
228229
/// encoded and returned to the caller. Possibile optimization: profile if the HMAC struct
229230
/// initialization is expensive and, if so, cache it somehow to avoid recreating it at every
230231
/// request.
231-
fn encode_str_to_sign(str_to_sign: &str, key: &[u8]) -> String {
232-
let key = hmac::Key::new(ring::hmac::HMAC_SHA256, key);
233-
let sig = hmac::sign(&key, str_to_sign.as_bytes());
234-
base64::encode(sig.as_ref())
232+
fn encode_str_to_sign(data: &str, key: &[u8]) -> String {
233+
let mut hmac = Hmac::<Sha256>::new_from_slice(key).unwrap();
234+
hmac.update(data.as_bytes());
235+
let signature = hmac.finalize().into_bytes();
236+
base64::encode(&signature)
235237
}
236238

237239
#[cfg(test)]

sdk/messaging_servicebus/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ edition = "2018"
1616

1717
[dependencies]
1818
azure_core = { path = "../core", version = "0.1" }
19-
ring = "0.16"
2019
base64 = "0.13"
2120
chrono = "0.4"
2221
log = "0.4"
2322
url = "2.2"
23+
hmac = "0.12"
24+
sha2 = "0.10"
2425

2526
[dev-dependencies]
2627
futures = "0.3"

sdk/storage/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ edition = "2018"
1515
[dependencies]
1616
async-trait = "0.1"
1717
azure_core = { path = "../core", version = "0.1", default-features=false }
18-
ring = "0.16"
1918
base64 = "0.13"
2019
chrono = "0.4"
2120
http = "0.2"
@@ -31,6 +30,8 @@ bytes = "1.0"
3130
RustyXML = "0.3"
3231
thiserror = "1.0"
3332
once_cell = "1.7"
33+
hmac = "0.12"
34+
sha2 = "0.10"
3435

3536
[dev-dependencies]
3637
tokio = { version = "1.0", features = ["macros"] }

sdk/storage/src/core/clients/storage_account_client.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::headers::CONTENT_MD5;
22
use crate::{
33
core::{ConnectionString, No},
4+
hmac::sign,
45
shared_access_signature::account_sas::{
56
AccountSharedAccessSignatureBuilder, ClientAccountSharedAccessSignature,
67
},
@@ -13,7 +14,6 @@ use http::{
1314
method::Method,
1415
request::{Builder, Request},
1516
};
16-
use ring::hmac;
1717
use std::sync::Arc;
1818
use url::Url;
1919

@@ -450,22 +450,12 @@ fn generate_authorization(
450450
// debug!("\nstr_to_sign == {:?}\n", str_to_sign);
451451
// debug!("str_to_sign == {}", str_to_sign);
452452

453-
let auth = encode_str_to_sign(&str_to_sign, key);
453+
let auth = sign(&str_to_sign, key).unwrap();
454454
// debug!("auth == {:?}", auth);
455455

456456
format!("SharedKey {}:{}", account, auth)
457457
}
458458

459-
fn encode_str_to_sign(str_to_sign: &str, hmac_key: &str) -> String {
460-
let key = hmac::Key::new(ring::hmac::HMAC_SHA256, &base64::decode(hmac_key).unwrap());
461-
let sig = hmac::sign(&key, str_to_sign.as_bytes());
462-
463-
// let res = hmac.result();
464-
// debug!("{:?}", res.code());
465-
466-
base64::encode(sig.as_ref())
467-
}
468-
469459
fn add_if_exists<K: AsHeaderName>(h: &HeaderMap, key: K) -> &str {
470460
match h.get(key) {
471461
Some(ce) => ce.to_str().unwrap(),

sdk/storage/src/core/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub enum Error {
7373
HeadersNotFound(Vec<String>),
7474
#[error("error writing the header value: {0}")]
7575
InvalidHeaderValue(#[from] azure_core::HttpHeaderError),
76+
#[error("error generating hmac: {0}")]
77+
Hmac(#[from] hmac::digest::InvalidLength),
7678
}
7779

7880
impl From<azure_core::error::Error> for Error {

sdk/storage/src/core/hmac.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::Result;
2+
use base64::encode;
3+
use hmac::{Hmac, Mac};
4+
use sha2::Sha256;
5+
6+
pub fn sign(data: &str, key: &str) -> Result<String> {
7+
let mut hmac = Hmac::<Sha256>::new_from_slice(&base64::decode(key)?)?;
8+
hmac.update(data.as_bytes());
9+
let signature = hmac.finalize().into_bytes();
10+
Ok(encode(&signature))
11+
}

sdk/storage/src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod connection_string_builder;
44
mod copy_id;
55
mod copy_progress;
66
mod errors;
7+
pub mod hmac;
78
mod into_azure_path;
89
mod macros;
910
pub mod prelude;

sdk/storage/src/core/shared_access_signature/account_sas.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::core::{
2-
shared_access_signature::{format_date, format_form, sign, SasProtocol, SasToken},
2+
hmac::sign,
3+
shared_access_signature::{format_date, format_form, SasProtocol, SasToken},
34
No, ToAssign,
45
};
56
use chrono::{DateTime, Utc};
@@ -170,7 +171,7 @@ impl AccountSharedAccessSignature {
170171
self.signed_version,
171172
);
172173

173-
sign(&self.key, &string_to_sign)
174+
sign(&string_to_sign, &self.key).unwrap()
174175
}
175176
_ => {
176177
// TODO: support other version tags?

sdk/storage/src/core/shared_access_signature/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use chrono::{DateTime, Utc};
2-
use ring::hmac;
32
use std::fmt;
43
use url::form_urlencoded;
54

@@ -10,12 +9,6 @@ pub trait SasToken {
109
fn token(&self) -> String;
1110
}
1211

13-
pub(crate) fn sign(key: &str, data: &str) -> String {
14-
let key = hmac::Key::new(ring::hmac::HMAC_SHA256, &base64::decode(key).unwrap());
15-
let sig_bytes = hmac::sign(&key, data.as_bytes());
16-
base64::encode(&sig_bytes)
17-
}
18-
1912
pub(crate) fn format_date(d: DateTime<Utc>) -> String {
2013
d.format("%Y-%m-%dT%H:%M:%SZ").to_string()
2114
}

0 commit comments

Comments
 (0)