Skip to content

Commit 2d53bf0

Browse files
committed
update sec256k1 version
- update hmac and sha2 packages - fix tests
1 parent a8d1d58 commit 2d53bf0

File tree

4 files changed

+62
-35
lines changed

4 files changed

+62
-35
lines changed

Cargo.lock

Lines changed: 38 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crypto/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ byteorder = "1.3.4"
1818
digest = "0.8.1"
1919
failure = "0.1.8"
2020
hex = "0.4.1"
21-
hmac = "0.7.1"
21+
hmac = "0.12.1"
2222
memzero = "0.1.0"
2323
rand = "0.7.3"
2424
ring = "0.16.11"
2525
secp256k1 = { version = "0.28.2", features = ["global-context"] }
2626
serde = { version = "1.0.104", optional = true }
27-
sha2 = "0.8.1"
27+
sha2 = "0.10.8"
2828
tiny-bip39 = "0.7.0"
2929

3030
witnet_protected = { path = "../protected" }

crypto/src/hash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Various hash functions
22
3-
use digest::Digest;
3+
use sha2::{Digest};
44
#[cfg(feature = "serde")]
55
use serde::{Deserialize, Serialize};
66

@@ -31,8 +31,8 @@ impl AsRef<[u8]> for Sha256 {
3131
/// Calculate the SHA256 hash
3232
pub fn calculate_sha256(bytes: &[u8]) -> Sha256 {
3333
let mut hasher = sha2::Sha256::new();
34-
hasher.input(bytes);
34+
hasher.update(bytes);
3535
let mut hash = [0; 32];
36-
hash.copy_from_slice(&hasher.result());
36+
hash.copy_from_slice(sha2::Sha256::digest(&bytes).as_ref());
3737
Sha256(hash)
3838
}

crypto/src/key.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ where
124124
}
125125

126126
let key_bytes = self.key;
127-
let mut mac = Hmac::<sha2::Sha512>::new_varkey(key_bytes)
127+
let mut mac = Hmac::<sha2::Sha512>::new_from_slice(key_bytes)
128128
.map_err(|_| MasterKeyGenError::InvalidKeyLength)?;
129-
mac.input(seed_bytes);
130-
let result = mac.result().code();
129+
mac.update(seed_bytes);
130+
let result = mac.finalize().into_bytes();
131131
let (sk_bytes, chain_code_bytes) = result.split_at(32);
132132

133133
// secret/chain_code computation might panic if length returned by hmac is wrong
@@ -281,26 +281,26 @@ impl ExtendedSK {
281281
for index in path.iter() {
282282
extended_sk = extended_sk.child(index)?
283283
}
284-
285284
Ok(extended_sk)
286285
}
287286

288287
/// Try to get a private child key from parent
289288
pub fn child(&self, index: &KeyPathIndex) -> Result<ExtendedSK, KeyDerivationError> {
290289
let mut hmac512: Hmac<sha2::Sha512> =
291-
Hmac::new_varkey(&self.chain_code).map_err(|_| KeyDerivationError::InvalidKeyLength)?;
290+
Hmac::new_from_slice(&self.chain_code.as_ref()).map_err(|_| KeyDerivationError::InvalidKeyLength)?;
292291
let index_bytes = index.as_ref().to_be_bytes();
293-
294292
if index.is_hardened() {
295-
hmac512.input(&[0]); // BIP-32 padding that makes key 33 bytes long
296-
hmac512.input(&self.secret_key[..]);
293+
hmac512.update(&[0u8]); // BIP-32 padding that makes key 33 bytes long
294+
hmac512.update(&self.secret_key[..]);
297295
} else {
298-
hmac512.input(&PublicKey::from_secret_key_global(&self.secret_key).serialize());
296+
hmac512.update(&PublicKey::from_secret_key_global(&self.secret_key).serialize().as_ref());
299297
}
300298

301-
let (chain_code, secret_key) = get_chain_code_and_secret(&index_bytes, hmac512)?;
299+
hmac512.update(&index_bytes.as_ref());
300+
301+
let (chain_code, mut secret_key) = get_chain_code_and_secret(&hmac512)?;
302302

303-
secret_key
303+
secret_key = self.secret_key
304304
.add_tweak(&Scalar::from(secret_key))
305305
.map_err(KeyDerivationError::Secp256k1Error)?;
306306

@@ -458,11 +458,10 @@ impl From<Vec<u32>> for KeyPath {
458458

459459
#[inline]
460460
fn get_chain_code_and_secret(
461-
seed: &[u8],
462-
mut hmac512: Hmac<sha2::Sha512>,
461+
hmac512: &Hmac<sha2::Sha512>,
463462
) -> Result<(Protected, SecretKey), KeyDerivationError> {
464-
hmac512.input(seed);
465-
let i = hmac512.result().code();
463+
let binding = &hmac512.clone().finalize().into_bytes();
464+
let i = binding.iter().as_slice();
466465
let (il, ir) = i.split_at(32);
467466
let chain_code = Protected::from(ir);
468467
let secret_key = SecretKey::from_slice(il).map_err(KeyDerivationError::Secp256k1Error)?;
@@ -530,11 +529,11 @@ mod tests {
530529

531530
let master_key = MasterKeyGen::new(&seed[..]).generate().unwrap();
532531

533-
let expected_secret_key = [
532+
let expected_secret_key:[u8; 32] = [
534533
79, 67, 227, 208, 107, 229, 51, 169, 104, 61, 121, 142, 8, 143, 75, 74, 235, 179, 67,
535534
213, 108, 252, 255, 16, 32, 162, 57, 21, 195, 162, 115, 128,
536535
];
537-
assert_eq!(expected_secret_key, &master_key.secret_key[..]);
536+
assert_eq!(expected_secret_key, *master_key.secret_key.as_ref());
538537
}
539538

540539
#[test]
@@ -555,7 +554,7 @@ mod tests {
555554
.index(0); // address: 0
556555
let account = extended_sk.derive(&path).unwrap();
557556

558-
let expected_account = [
557+
let expected_account:[u8; 32] = [
559558
137, 174, 230, 121, 4, 190, 53, 238, 47, 181, 52, 226, 109, 68, 153, 170, 112, 150, 84,
560559
84, 26, 177, 194, 157, 76, 80, 136, 25, 6, 79, 247, 43,
561560
];
@@ -575,15 +574,15 @@ mod tests {
575574
let master_key = MasterKeyGen::new(&seed).generate().unwrap();
576575

577576
for (expected, keypath) in slip32_vectors() {
578-
let key = master_key.derive(&keypath).unwrap();
577+
let key = &master_key.derive(&keypath).unwrap();
579578
let xprv = key.to_slip32(&keypath).unwrap();
580579

581580
assert_eq!(expected, xprv);
582581

583582
let (recovered_key, path) = ExtendedSK::from_slip32(&xprv).unwrap();
584583

585584
assert_eq!(keypath, path);
586-
assert_eq!(key, recovered_key);
585+
assert_eq!(key, &recovered_key);
587586
}
588587
}
589588
}

0 commit comments

Comments
 (0)