Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit a3e39c9

Browse files
Andronik Ordianniklasad1
Andronik Ordian
authored andcommitted
update ring to 0.14 (#10262)
* cargo upgrade hyper-rustls --all * cargo upgrade parity-crypto --all * update Cargo.lock * propagate NonZeroU32 * use NonZeroU32::new_unchecked for crypto::KEY_ITERATIONS * update Cargo.lock * replace unsafe code with lazy_static
1 parent 8ab6d89 commit a3e39c9

29 files changed

+256
-180
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pretty_assertions = "0.1"
8080
ipnetwork = "0.12.6"
8181
tempdir = "0.3"
8282
fake-fetch = { path = "util/fake-fetch" }
83+
lazy_static = "1.2.0"
8384

8485
[target.'cfg(windows)'.dependencies]
8586
winapi = { version = "0.3.4", features = ["winsock2", "winuser", "shellapi"] }

accounts/ethkey/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Parity Technologies <[email protected]>"]
66
[dependencies]
77
byteorder = "1.0"
88
edit-distance = "2.0"
9-
parity-crypto = "0.2"
9+
parity-crypto = "0.3.0"
1010
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1" }
1111
ethereum-types = "0.4"
1212
lazy_static = "1.0"

accounts/ethstore/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ tiny-keccak = "1.4"
1616
time = "0.1.34"
1717
itertools = "0.5"
1818
parking_lot = "0.7"
19-
parity-crypto = "0.2"
19+
parity-crypto = "0.3.0"
2020
ethereum-types = "0.4"
2121
dir = { path = "../../util/dir" }
2222
smallvec = "0.6"
2323
parity-wordlist = "1.0"
2424
tempdir = "0.3"
25+
lazy_static = "1.2.0"
2526

2627
[dev-dependencies]
2728
matches = "0.1"

accounts/ethstore/src/account/crypto.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use std::str;
18+
use std::num::NonZeroU32;
1819
use ethkey::{Password, Secret};
1920
use {json, Error, crypto};
2021
use crypto::Keccak256;
@@ -73,12 +74,12 @@ impl From<Crypto> for String {
7374

7475
impl Crypto {
7576
/// Encrypt account secret
76-
pub fn with_secret(secret: &Secret, password: &Password, iterations: u32) -> Result<Self, crypto::Error> {
77+
pub fn with_secret(secret: &Secret, password: &Password, iterations: NonZeroU32) -> Result<Self, crypto::Error> {
7778
Crypto::with_plain(&*secret, password, iterations)
7879
}
7980

8081
/// Encrypt custom plain data
81-
pub fn with_plain(plain: &[u8], password: &Password, iterations: u32) -> Result<Self, crypto::Error> {
82+
pub fn with_plain(plain: &[u8], password: &Password, iterations: NonZeroU32) -> Result<Self, crypto::Error> {
8283
let salt: [u8; 32] = Random::random();
8384
let iv: [u8; 16] = Random::random();
8485

@@ -159,29 +160,33 @@ impl Crypto {
159160
#[cfg(test)]
160161
mod tests {
161162
use ethkey::{Generator, Random};
162-
use super::{Crypto, Error};
163+
use super::{Crypto, Error, NonZeroU32};
164+
165+
lazy_static! {
166+
static ref ITERATIONS: NonZeroU32 = NonZeroU32::new(10240).expect("10240 > 0; qed");
167+
}
163168

164169
#[test]
165170
fn crypto_with_secret_create() {
166171
let keypair = Random.generate().unwrap();
167172
let passwd = "this is sparta".into();
168-
let crypto = Crypto::with_secret(keypair.secret(), &passwd, 10240).unwrap();
173+
let crypto = Crypto::with_secret(keypair.secret(), &passwd, *ITERATIONS).unwrap();
169174
let secret = crypto.secret(&passwd).unwrap();
170175
assert_eq!(keypair.secret(), &secret);
171176
}
172177

173178
#[test]
174179
fn crypto_with_secret_invalid_password() {
175180
let keypair = Random.generate().unwrap();
176-
let crypto = Crypto::with_secret(keypair.secret(), &"this is sparta".into(), 10240).unwrap();
181+
let crypto = Crypto::with_secret(keypair.secret(), &"this is sparta".into(), *ITERATIONS).unwrap();
177182
assert_matches!(crypto.secret(&"this is sparta!".into()), Err(Error::InvalidPassword))
178183
}
179184

180185
#[test]
181186
fn crypto_with_null_plain_data() {
182187
let original_data = b"";
183188
let passwd = "this is sparta".into();
184-
let crypto = Crypto::with_plain(&original_data[..], &passwd, 10240).unwrap();
189+
let crypto = Crypto::with_plain(&original_data[..], &passwd, *ITERATIONS).unwrap();
185190
let decrypted_data = crypto.decrypt(&passwd).unwrap();
186191
assert_eq!(original_data[..], *decrypted_data);
187192
}
@@ -190,7 +195,7 @@ mod tests {
190195
fn crypto_with_tiny_plain_data() {
191196
let original_data = b"{}";
192197
let passwd = "this is sparta".into();
193-
let crypto = Crypto::with_plain(&original_data[..], &passwd, 10240).unwrap();
198+
let crypto = Crypto::with_plain(&original_data[..], &passwd, *ITERATIONS).unwrap();
194199
let decrypted_data = crypto.decrypt(&passwd).unwrap();
195200
assert_eq!(original_data[..], *decrypted_data);
196201
}
@@ -199,7 +204,7 @@ mod tests {
199204
fn crypto_with_huge_plain_data() {
200205
let original_data: Vec<_> = (1..65536).map(|i| (i % 256) as u8).collect();
201206
let passwd = "this is sparta".into();
202-
let crypto = Crypto::with_plain(&original_data, &passwd, 10240).unwrap();
207+
let crypto = Crypto::with_plain(&original_data, &passwd, *ITERATIONS).unwrap();
203208
let decrypted_data = crypto.decrypt(&passwd).unwrap();
204209
assert_eq!(&original_data, &decrypted_data);
205210
}

accounts/ethstore/src/account/kdf.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use json;
18+
use std::num::NonZeroU32;
1819

1920
#[derive(Debug, PartialEq, Clone)]
2021
pub enum Prf {
@@ -23,7 +24,7 @@ pub enum Prf {
2324

2425
#[derive(Debug, PartialEq, Clone)]
2526
pub struct Pbkdf2 {
26-
pub c: u32,
27+
pub c: NonZeroU32,
2728
pub dklen: u32,
2829
pub prf: Prf,
2930
pub salt: Vec<u8>,

accounts/ethstore/src/account/safe_account.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use {json, Error};
2020
use account::Version;
2121
use crypto;
2222
use super::crypto::Crypto;
23+
use std::num::NonZeroU32;
2324

2425
/// Account representation.
2526
#[derive(Debug, PartialEq, Clone)]
@@ -59,7 +60,7 @@ impl SafeAccount {
5960
keypair: &KeyPair,
6061
id: [u8; 16],
6162
password: &Password,
62-
iterations: u32,
63+
iterations: NonZeroU32,
6364
name: String,
6465
meta: String
6566
) -> Result<Self, crypto::Error> {
@@ -135,7 +136,7 @@ impl SafeAccount {
135136
}
136137

137138
/// Create a new `VaultKeyFile` from the given `self`
138-
pub fn into_vault_file(self, iterations: u32, password: &Password) -> Result<json::VaultKeyFile, Error> {
139+
pub fn into_vault_file(self, iterations: NonZeroU32, password: &Password) -> Result<json::VaultKeyFile, Error> {
139140
let meta_plain = json::VaultKeyMeta {
140141
address: self.address.into(),
141142
name: Some(self.name),
@@ -177,7 +178,7 @@ impl SafeAccount {
177178
}
178179

179180
/// Change account's password.
180-
pub fn change_password(&self, old_password: &Password, new_password: &Password, iterations: u32) -> Result<Self, Error> {
181+
pub fn change_password(&self, old_password: &Password, new_password: &Password, iterations: NonZeroU32) -> Result<Self, Error> {
181182
let secret = self.crypto.secret(old_password)?;
182183
let result = SafeAccount {
183184
id: self.id.clone(),
@@ -200,14 +201,19 @@ impl SafeAccount {
200201
#[cfg(test)]
201202
mod tests {
202203
use ethkey::{Generator, Random, verify_public, Message};
203-
use super::SafeAccount;
204+
use super::{SafeAccount, NonZeroU32};
205+
206+
lazy_static! {
207+
static ref ITERATIONS: NonZeroU32 = NonZeroU32::new(10240).expect("10240 > 0; qed");
208+
}
209+
204210

205211
#[test]
206212
fn sign_and_verify_public() {
207213
let keypair = Random.generate().unwrap();
208214
let password = "hello world".into();
209215
let message = Message::default();
210-
let account = SafeAccount::create(&keypair, [0u8; 16], &password, 10240, "Test".to_owned(), "{}".to_owned());
216+
let account = SafeAccount::create(&keypair, [0u8; 16], &password, *ITERATIONS, "Test".to_owned(), "{}".to_owned());
211217
let signature = account.unwrap().sign(&password, &message).unwrap();
212218
assert!(verify_public(keypair.public(), &signature, &message).unwrap());
213219
}
@@ -217,10 +223,9 @@ mod tests {
217223
let keypair = Random.generate().unwrap();
218224
let first_password = "hello world".into();
219225
let sec_password = "this is sparta".into();
220-
let i = 10240;
221226
let message = Message::default();
222-
let account = SafeAccount::create(&keypair, [0u8; 16], &first_password, i, "Test".to_owned(), "{}".to_owned()).unwrap();
223-
let new_account = account.change_password(&first_password, &sec_password, i).unwrap();
227+
let account = SafeAccount::create(&keypair, [0u8; 16], &first_password, *ITERATIONS, "Test".to_owned(), "{}".to_owned()).unwrap();
228+
let new_account = account.change_password(&first_password, &sec_password, *ITERATIONS).unwrap();
224229
assert!(account.sign(&first_password, &message).is_ok());
225230
assert!(account.sign(&sec_password, &message).is_err());
226231
assert!(new_account.sign(&first_password, &message).is_err());

accounts/ethstore/src/accounts_dir/disk.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,16 @@ mod test {
356356
extern crate tempdir;
357357

358358
use std::{env, fs};
359+
use std::num::NonZeroU32;
359360
use super::{KeyDirectory, RootDiskDirectory, VaultKey};
360361
use account::SafeAccount;
361362
use ethkey::{Random, Generator};
362363
use self::tempdir::TempDir;
363364

365+
lazy_static! {
366+
static ref ITERATIONS: NonZeroU32 = NonZeroU32::new(1024).expect("1024 > 0; qed");
367+
}
368+
364369
#[test]
365370
fn should_create_new_account() {
366371
// given
@@ -371,7 +376,7 @@ mod test {
371376
let directory = RootDiskDirectory::create(dir.clone()).unwrap();
372377

373378
// when
374-
let account = SafeAccount::create(&keypair, [0u8; 16], &password, 1024, "Test".to_owned(), "{}".to_owned());
379+
let account = SafeAccount::create(&keypair, [0u8; 16], &password, *ITERATIONS, "Test".to_owned(), "{}".to_owned());
375380
let res = directory.insert(account.unwrap());
376381

377382
// then
@@ -392,7 +397,7 @@ mod test {
392397
let directory = RootDiskDirectory::create(dir.clone()).unwrap();
393398

394399
// when
395-
let account = SafeAccount::create(&keypair, [0u8; 16], &password, 1024, "Test".to_owned(), "{}".to_owned()).unwrap();
400+
let account = SafeAccount::create(&keypair, [0u8; 16], &password, *ITERATIONS, "Test".to_owned(), "{}".to_owned()).unwrap();
396401
let filename = "test".to_string();
397402
let dedup = true;
398403

@@ -428,15 +433,15 @@ mod test {
428433

429434
// and when
430435
let before_root_items_count = fs::read_dir(&dir).unwrap().count();
431-
let vault = directory.as_vault_provider().unwrap().create(vault_name, VaultKey::new(&password, 1024));
436+
let vault = directory.as_vault_provider().unwrap().create(vault_name, VaultKey::new(&password, *ITERATIONS));
432437

433438
// then
434439
assert!(vault.is_ok());
435440
let after_root_items_count = fs::read_dir(&dir).unwrap().count();
436441
assert!(after_root_items_count > before_root_items_count);
437442

438443
// and when
439-
let vault = directory.as_vault_provider().unwrap().open(vault_name, VaultKey::new(&password, 1024));
444+
let vault = directory.as_vault_provider().unwrap().open(vault_name, VaultKey::new(&password, *ITERATIONS));
440445

441446
// then
442447
assert!(vault.is_ok());
@@ -453,8 +458,9 @@ mod test {
453458
let temp_path = TempDir::new("").unwrap();
454459
let directory = RootDiskDirectory::create(&temp_path).unwrap();
455460
let vault_provider = directory.as_vault_provider().unwrap();
456-
vault_provider.create("vault1", VaultKey::new(&"password1".into(), 1)).unwrap();
457-
vault_provider.create("vault2", VaultKey::new(&"password2".into(), 1)).unwrap();
461+
let iter = NonZeroU32::new(1).expect("1 > 0; qed");
462+
vault_provider.create("vault1", VaultKey::new(&"password1".into(), iter)).unwrap();
463+
vault_provider.create("vault2", VaultKey::new(&"password2".into(), iter)).unwrap();
458464

459465
// then
460466
let vaults = vault_provider.list_vaults().unwrap();
@@ -476,7 +482,7 @@ mod test {
476482

477483
let keypair = Random.generate().unwrap();
478484
let password = "test pass".into();
479-
let account = SafeAccount::create(&keypair, [0u8; 16], &password, 1024, "Test".to_owned(), "{}".to_owned());
485+
let account = SafeAccount::create(&keypair, [0u8; 16], &password, *ITERATIONS, "Test".to_owned(), "{}".to_owned());
480486
directory.insert(account.unwrap()).expect("Account should be inserted ok");
481487

482488
let new_hash = directory.files_hash().expect("New files hash should be calculated ok");

accounts/ethstore/src/accounts_dir/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! Accounts Directory
1818
1919
use ethkey::Password;
20+
use std::num::NonZeroU32;
2021
use std::path::{PathBuf};
2122
use {SafeAccount, Error};
2223

@@ -41,7 +42,7 @@ pub struct VaultKey {
4142
/// Vault password
4243
pub password: Password,
4344
/// Number of iterations to produce a derived key from password
44-
pub iterations: u32,
45+
pub iterations: NonZeroU32,
4546
}
4647

4748
/// Keys directory
@@ -96,7 +97,7 @@ pub use self::vault::VaultDiskDirectory;
9697

9798
impl VaultKey {
9899
/// Create new vault key
99-
pub fn new(password: &Password, iterations: u32) -> Self {
100+
pub fn new(password: &Password, iterations: NonZeroU32) -> Self {
100101
VaultKey {
101102
password: password.clone(),
102103
iterations: iterations,

accounts/ethstore/src/accounts_dir/vault.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,17 @@ mod test {
282282

283283
use std::fs;
284284
use std::io::Write;
285+
use std::num::NonZeroU32;
285286
use std::path::PathBuf;
286287
use super::VaultKey;
287288
use super::{VAULT_FILE_NAME, check_vault_name, make_vault_dir_path, create_vault_file, read_vault_file, VaultDiskDirectory};
288289
use self::tempdir::TempDir;
289290

291+
292+
lazy_static! {
293+
static ref ITERATIONS: NonZeroU32 = NonZeroU32::new(1024).expect("1024 > 0; qed");
294+
}
295+
290296
#[test]
291297
fn check_vault_name_succeeds() {
292298
assert!(check_vault_name("vault"));
@@ -325,7 +331,7 @@ mod test {
325331
fn create_vault_file_succeeds() {
326332
// given
327333
let temp_path = TempDir::new("").unwrap();
328-
let key = VaultKey::new(&"password".into(), 1024);
334+
let key = VaultKey::new(&"password".into(), *ITERATIONS);
329335
let mut vault_dir: PathBuf = temp_path.path().into();
330336
vault_dir.push("vault");
331337
fs::create_dir_all(&vault_dir).unwrap();
@@ -344,7 +350,7 @@ mod test {
344350
fn read_vault_file_succeeds() {
345351
// given
346352
let temp_path = TempDir::new("").unwrap();
347-
let key = VaultKey::new(&"password".into(), 1024);
353+
let key = VaultKey::new(&"password".into(), *ITERATIONS);
348354
let vault_file_contents = r#"{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"758696c8dc6378ab9b25bb42790da2f5"},"ciphertext":"54eb50683717d41caaeb12ea969f2c159daada5907383f26f327606a37dc7168","kdf":"pbkdf2","kdfparams":{"c":1024,"dklen":32,"prf":"hmac-sha256","salt":"3c320fa566a1a7963ac8df68a19548d27c8f40bf92ef87c84594dcd5bbc402b6"},"mac":"9e5c2314c2a0781962db85611417c614bd6756666b6b1e93840f5b6ed895f003"}}"#;
349355
let dir: PathBuf = temp_path.path().into();
350356
let mut vault_file_path: PathBuf = dir.clone();
@@ -365,7 +371,7 @@ mod test {
365371
fn read_vault_file_fails() {
366372
// given
367373
let temp_path = TempDir::new("").unwrap();
368-
let key = VaultKey::new(&"password1".into(), 1024);
374+
let key = VaultKey::new(&"password1".into(), *ITERATIONS);
369375
let dir: PathBuf = temp_path.path().into();
370376
let mut vault_file_path: PathBuf = dir.clone();
371377
vault_file_path.push(VAULT_FILE_NAME);
@@ -394,7 +400,7 @@ mod test {
394400
fn vault_directory_can_be_created() {
395401
// given
396402
let temp_path = TempDir::new("").unwrap();
397-
let key = VaultKey::new(&"password".into(), 1024);
403+
let key = VaultKey::new(&"password".into(), *ITERATIONS);
398404
let dir: PathBuf = temp_path.path().into();
399405

400406
// when
@@ -414,7 +420,7 @@ mod test {
414420
fn vault_directory_cannot_be_created_if_already_exists() {
415421
// given
416422
let temp_path = TempDir::new("").unwrap();
417-
let key = VaultKey::new(&"password".into(), 1024);
423+
let key = VaultKey::new(&"password".into(), *ITERATIONS);
418424
let dir: PathBuf = temp_path.path().into();
419425
let mut vault_dir = dir.clone();
420426
vault_dir.push("vault");
@@ -431,7 +437,7 @@ mod test {
431437
fn vault_directory_cannot_be_opened_if_not_exists() {
432438
// given
433439
let temp_path = TempDir::new("").unwrap();
434-
let key = VaultKey::new(&"password".into(), 1024);
440+
let key = VaultKey::new(&"password".into(), *ITERATIONS);
435441
let dir: PathBuf = temp_path.path().into();
436442

437443
// when

0 commit comments

Comments
 (0)