From 17c2f937544a33dae4504b3d49a8f2b5704ad138 Mon Sep 17 00:00:00 2001 From: Craig Hills Date: Thu, 27 Apr 2017 08:07:33 -0400 Subject: [PATCH] Remove rustc-serialize usage Now that rustc-serialize has been marked as deprecated, it seems prudent to drop it's usage and replace the functionality with other crates. Within this project, it was only used for base64 and hex encoding. Also, the hex encoding is for testing purposes. Other crates exist for those purposes, making this change quite easy. --- Cargo.toml | 3 ++- src/aes_gcm.rs | 6 +++--- src/blake2b.rs | 21 +++++++++++-------- src/digest.rs | 4 ++-- src/hc128.rs | 30 +++++++++++++++------------ src/lib.rs | 3 ++- src/pbkdf2.rs | 15 +++++++------- src/scrypt.rs | 17 +++++++-------- src/sha3.rs | 12 ++++++++--- src/sosemanuk.rs | 54 ++++++++++++++++++++++++++---------------------- 10 files changed, 91 insertions(+), 74 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ea941a7..7ec7b7d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,5 @@ gcc = "^0.3" libc = "^0.2" time = "^0.1" rand = "^0.3" -rustc-serialize = "^0.3" +base64 = "^0.5" +hex = "^0.2" diff --git a/src/aes_gcm.rs b/src/aes_gcm.rs index e560b2d2..58af6588 100644 --- a/src/aes_gcm.rs +++ b/src/aes_gcm.rs @@ -49,7 +49,6 @@ impl<'a> AesGcm<'a> { end_tag: final_block } } - } impl<'a> AeadEncryptor for AesGcm<'static> { @@ -88,10 +87,10 @@ mod test { use aes::KeySize; use aes_gcm::AesGcm; use aead::{AeadEncryptor, AeadDecryptor}; - use serialize::hex::FromHex; + use hex; use std::iter::repeat; fn hex_to_bytes(raw_hex: &str) -> Vec { - raw_hex.from_hex().ok().unwrap() + hex::FromHex::from_hex(raw_hex).ok().unwrap() } struct TestVector { key: Vec, @@ -213,6 +212,7 @@ mod bench { use aes::KeySize; use aes_gcm::AesGcm; use aead::{AeadEncryptor, AeadDecryptor}; + use hex::FromHex; #[bench] pub fn gsm_10(bh: & mut Bencher) { diff --git a/src/blake2b.rs b/src/blake2b.rs index eb946cc1..cfbcd270 100644 --- a/src/blake2b.rs +++ b/src/blake2b.rs @@ -392,8 +392,11 @@ mod digest_tests { //use cryptoutil::test::test_digest_1million_random; use blake2b::Blake2b; use digest::Digest; - use serialize::hex::FromHex; + use hex; + fn decode_hex(value: &str) -> Vec { + hex::FromHex::from_hex(value).unwrap() + } struct Test { input: Vec, @@ -440,18 +443,18 @@ mod digest_tests { // Examples from wikipedia Test { input: vec![], - output: "786a02f742015903c6c6fd852552d272\ - 912f4740e15847618a86e217f71f5419\ - d25e1031afee585313896444934eb04b\ - 903a685b1448b755d56f701afe9be2ce".from_hex().unwrap(), + output: decode_hex("786a02f742015903c6c6fd852552d272\ + 912f4740e15847618a86e217f71f5419\ + d25e1031afee585313896444934eb04b\ + 903a685b1448b755d56f701afe9be2ce"), key: None }, Test { input: "The quick brown fox jumps over the lazy dog".as_bytes().to_vec(), - output: "a8add4bdddfd93e4877d2746e62817b1\ - 16364a1fa7bc148d95090bc7333b3673\ - f82401cf7aa2e4cb1ecd90296e3f14cb\ - 5413f8ed77be73045b13914cdcd6a918".from_hex().unwrap(), + output: decode_hex("a8add4bdddfd93e4877d2746e62817b1\ + 16364a1fa7bc148d95090bc7333b3673\ + f82401cf7aa2e4cb1ecd90296e3f14cb\ + 5413f8ed77be73045b13914cdcd6a918"), key: None }, // from: https://github.com/BLAKE2/BLAKE2/blob/master/testvectors/blake2b-test.txt diff --git a/src/digest.rs b/src/digest.rs index f31dd9b6..1600d46f 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -72,10 +72,10 @@ pub trait Digest { * String in hexadecimal format. */ fn result_str(&mut self) -> String { - use serialize::hex::ToHex; + use hex::ToHex; let mut buf: Vec = repeat(0).take((self.output_bits()+7)/8).collect(); self.result(&mut buf); - buf[..].to_hex() + (&buf[..]).to_hex() } } diff --git a/src/hc128.rs b/src/hc128.rs index 50627855..5a417536 100644 --- a/src/hc128.rs +++ b/src/hc128.rs @@ -182,18 +182,22 @@ impl Decryptor for Hc128 { mod test { use hc128::Hc128; use symmetriccipher::SynchronousStreamCipher; - use serialize::hex::{FromHex}; + use hex; + + fn decode_hex(value: &str) -> Vec { + hex::FromHex::from_hex(value).unwrap() + } // Vectors from http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/hc-256/hc-128/verified.test-vectors?rev=210&view=markup #[test] fn test_hc128_ecrypt_set_2_vector_0() { - let key = "00000000000000000000000000000000".from_hex().unwrap(); - let nonce = "00000000000000000000000000000000".from_hex().unwrap(); + let key = decode_hex("00000000000000000000000000000000"); + let nonce = decode_hex("00000000000000000000000000000000"); let input = [0u8; 64]; let expected_output_hex = "82001573A003FD3B7FD72FFB0EAF63AAC62F12DEB629DCA72785A66268EC758B1EDB36900560898178E0AD009ABF1F491330DC1C246E3D6CB264F6900271D59C"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -205,12 +209,12 @@ mod test { #[test] fn test_hc128_ecrypt_set_6_vector_1() { - let key = "0558ABFE51A4F74A9DF04396E93C8FE2".from_hex().unwrap(); - let nonce = "167DE44BB21980E74EB51C83EA51B81F".from_hex().unwrap(); + let key = decode_hex("0558ABFE51A4F74A9DF04396E93C8FE2"); + let nonce = decode_hex("167DE44BB21980E74EB51C83EA51B81F"); let input = [0u8; 64]; let expected_output_hex = "4F864BF3C96D0363B1903F0739189138F6ED2BC0AF583FEEA0CEA66BA7E06E63FB28BF8B3CA0031D24ABB511C57DD17BFC2861C32400072CB680DF2E58A5CECC"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -223,12 +227,12 @@ mod test { #[test] fn test_hc128_ecrypt_set_6_vector_2() { - let key = "0A5DB00356A9FC4FA2F5489BEE4194E7".from_hex().unwrap(); - let nonce = "1F86ED54BB2289F057BE258CF35AC128".from_hex().unwrap(); + let key = decode_hex("0A5DB00356A9FC4FA2F5489BEE4194E7"); + let nonce = decode_hex("1F86ED54BB2289F057BE258CF35AC128"); let input = [0u8; 64]; let expected_output_hex = "82168AB0023B79AAF1E6B4D823855E14A7084378036A951B1CFEF35173875ED86CB66AB8410491A08582BE40080C3102193BA567F9E95D096C3CC60927DD7901"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -241,12 +245,12 @@ mod test { #[test] fn test_hc128_ecrypt_set_6_vector_3() { - let key = "0F62B5085BAE0154A7FA4DA0F34699EC".from_hex().unwrap(); - let nonce = "288FF65DC42B92F960C72E95FC63CA31".from_hex().unwrap(); + let key = decode_hex("0F62B5085BAE0154A7FA4DA0F34699EC"); + let nonce = decode_hex("288FF65DC42B92F960C72E95FC63CA31"); let input = [0u8; 64]; let expected_output_hex = "1CD8AEDDFE52E217E835D0B7E84E2922D04B1ADBCA53C4522B1AA604C42856A90AF83E2614BCE65C0AECABDD8975B55700D6A26D52FFF0888DA38F1DE20B77B7"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; diff --git a/src/lib.rs b/src/lib.rs index 59ad3afb..4b817e11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,8 @@ #![cfg_attr(feature = "with-bench", feature(test))] extern crate rand; -extern crate rustc_serialize as serialize; +extern crate hex; +extern crate base64; extern crate time; extern crate libc; diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index f21de48f..89aae91e 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -14,8 +14,7 @@ use std::io; use cryptoutil::copy_memory; use rand::{OsRng, Rng}; -use serialize::base64; -use serialize::base64::{FromBase64, ToBase64}; +use base64; use cryptoutil::{read_u32_be, write_u32_be}; use hmac::Hmac; @@ -145,11 +144,11 @@ pub fn pbkdf2_simple(password: &str, c: u32) -> io::Result { let mut result = "$rpbkdf2$0$".to_string(); let mut tmp = [0u8; 4]; write_u32_be(&mut tmp, c); - result.push_str(&tmp.to_base64(base64::STANDARD)[..]); + result.push_str(&base64::encode_config(&tmp, base64::STANDARD)[..]); result.push('$'); - result.push_str(&salt.to_base64(base64::STANDARD)[..]); + result.push_str(&base64::encode_config(&salt, base64::STANDARD)[..]); result.push('$'); - result.push_str(&dk.to_base64(base64::STANDARD)[..]); + result.push_str(&base64::encode_config(&dk, base64::STANDARD)[..]); result.push('$'); Ok(result) @@ -195,7 +194,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result match pstr.from_base64() { + Some(pstr) => match base64::decode(pstr) { Ok(pvec) => { if pvec.len() != 4 { return Err(ERR_STR); } read_u32_be(&pvec[..]) @@ -207,7 +206,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result match sstr.from_base64() { + Some(sstr) => match base64::decode(sstr) { Ok(salt) => salt, Err(_) => return Err(ERR_STR) }, @@ -216,7 +215,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result match hstr.from_base64() { + Some(hstr) => match base64::decode(hstr) { Ok(hash) => hash, Err(_) => return Err(ERR_STR) }, diff --git a/src/scrypt.rs b/src/scrypt.rs index 44920643..91e384c7 100644 --- a/src/scrypt.rs +++ b/src/scrypt.rs @@ -19,8 +19,7 @@ use std::mem::size_of; use cryptoutil::copy_memory; use rand::{OsRng, Rng}; -use serialize::base64; -use serialize::base64::{FromBase64, ToBase64}; +use base64; use cryptoutil::{read_u32_le, read_u32v_le, write_u32_le}; use hmac::Hmac; @@ -287,19 +286,19 @@ pub fn scrypt_simple(password: &str, params: &ScryptParams) -> io::Result Result match pstr.from_base64() { + Some(pstr) => match base64::decode(pstr) { Ok(x) => x, Err(_) => return Err(ERR_STR) }, @@ -368,7 +367,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result match sstr.from_base64() { + Some(sstr) => match base64::decode(sstr) { Ok(salt) => salt, Err(_) => return Err(ERR_STR) }, @@ -377,7 +376,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result match hstr.from_base64() { + Some(hstr) => match base64::decode(hstr) { Ok(hash) => hash, Err(_) => return Err(ERR_STR) }, diff --git a/src/sha3.rs b/src/sha3.rs index 0359e9ce..9260157c 100644 --- a/src/sha3.rs +++ b/src/sha3.rs @@ -459,17 +459,23 @@ impl Clone for Sha3 { mod tests { use digest::Digest; use sha3::{Sha3, Sha3Mode}; - use serialize::hex::{FromHex, ToHex}; + use hex::{self, ToHex}; struct Test { input: &'static str, output_str: &'static str, } + impl Test { + fn input_bytes(&self) -> Vec { + hex::FromHex::from_hex(self.input).unwrap() + } + } + fn test_hash(sh: &mut D, tests: &[Test]) { // Test that it works when accepting the message all at once for t in tests.iter() { - sh.input(&t.input.from_hex().unwrap()); + sh.input(&t.input_bytes()); let mut out_str = vec![0u8; t.output_str.len() / 2]; @@ -486,7 +492,7 @@ mod tests { let mut left = len; while left > 0 { let take = (left + 1) / 2; - sh.input(&t.input.from_hex().unwrap()[len - left..take + len - left]); + sh.input(&t.input_bytes()[len - left..take + len - left]); left = left - take; } diff --git a/src/sosemanuk.rs b/src/sosemanuk.rs index c3847ebd..54f2776f 100644 --- a/src/sosemanuk.rs +++ b/src/sosemanuk.rs @@ -2331,18 +2331,22 @@ impl Decryptor for Sosemanuk { mod test { use sosemanuk::Sosemanuk; use symmetriccipher::SynchronousStreamCipher; - use serialize::hex::{FromHex}; + use hex; + + fn decode_hex(value: &str) -> Vec { + hex::FromHex::from_hex(value).unwrap() + } // Vectors from http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/sosemanuk/unverified.test-vectors?rev=108&view=markup #[test] fn test_sosemanuk_ecrypt_set_1_vector_0() { - let key = "8000000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap(); - let nonce = "00000000000000000000000000000000".from_hex().unwrap(); + let key = decode_hex("8000000000000000000000000000000000000000000000000000000000000000"); + let nonce = decode_hex("00000000000000000000000000000000"); let input = [0u8; 64]; let expected_output_hex = "1782FABFF497A0E89E16E1BCF22F0FE8AA8C566D293AA35B2425E4F26E31C3E7701C08A0D614AF3D3861A7DFF7D6A38A0EFE84A29FADF68D390A3D15B75C972D"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -2354,12 +2358,12 @@ mod test { #[test] fn test_sosemanuk_ecrypt_set_2_vector_63() { - let key = "3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F".from_hex().unwrap(); - let nonce = "00000000000000000000000000000000".from_hex().unwrap(); + let key = decode_hex("3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F3F"); + let nonce = decode_hex("00000000000000000000000000000000"); let input = [0u8; 64]; let expected_output_hex = "7D755F30A2B747A50D7D28147EDF0B3E3FAB6856A7373C7306C00D1D4076969354D7AB4343C0115E7839502C5C699ED06DB119968AEBFD08D8B968A7161D613F"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -2371,12 +2375,12 @@ mod test { #[test] fn test_sosemanuk_ecrypt_set_2_vector_90() { - let key = "5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A".from_hex().unwrap(); - let nonce = "00000000000000000000000000000000".from_hex().unwrap(); + let key = decode_hex("5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A"); + let nonce = decode_hex("00000000000000000000000000000000"); let input = [0u8; 64]; let expected_output_hex = "F5D7D72686322D1751AFD16A1DD98282D2B9A1EE0C305DF52F86AE1B831E90C22E2DE089CEE656A992736385D9135B823B3611098674BF820986A4342B89ABF7"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -2388,12 +2392,12 @@ mod test { #[test] fn test_sosemanuk_ecrypt_set_3_vector_135() { - let key = "8788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4".from_hex().unwrap(); - let nonce = "00000000000000000000000000000000".from_hex().unwrap(); + let key = decode_hex("8788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4"); + let nonce = decode_hex("00000000000000000000000000000000"); let input = [0u8; 64]; let expected_output_hex = "9D7EE5A10BBB0756D66B8DAA5AE08F41B05C9E7C6B13532EAA81F224282B61C66DEEE5AF6251DB26C49B865C5AD4250AE89787FC86C35409CF2986CF820293AA"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -2405,12 +2409,12 @@ mod test { #[test] fn test_sosemanuk_ecrypt_set_3_vector_207() { - let key = "CFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEE".from_hex().unwrap(); - let nonce = "00000000000000000000000000000000".from_hex().unwrap(); + let key = decode_hex("CFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEE"); + let nonce = decode_hex("00000000000000000000000000000000"); let input = [0u8; 64]; let expected_output_hex = "F028923659C6C0A17065E013368D93EBCF2F4FD892B6E27E104EF0A2605708EA26336AE966D5058BC144F7954FE2FC3C258F00734AA5BEC8281814B746197084"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -2422,12 +2426,12 @@ mod test { #[test] fn test_sosemanuk_ecrypt_set_6_vector_3() { - let key = "0F62B5085BAE0154A7FA4DA0F34699EC3F92E5388BDE3184D72A7DD02376C91C".from_hex().unwrap(); - let nonce = "288FF65DC42B92F960C72E95FC63CA31".from_hex().unwrap(); + let key = decode_hex("0F62B5085BAE0154A7FA4DA0F34699EC3F92E5388BDE3184D72A7DD02376C91C"); + let nonce = decode_hex("288FF65DC42B92F960C72E95FC63CA31"); let input = [0u8; 64]; let expected_output_hex = "1FC4F2E266B21C24FDDB3492D40A3FA6DE32CDF13908511E84420ABDFA1D3B0FEC600F83409C57CBE0394B90CDB1D759243EFD8B8E2AB7BC453A8D8A3515183E"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 64]; @@ -2441,12 +2445,12 @@ mod test { #[test] fn test_sosemanuk_vector128_test1() { - let key = "A7C083FEB7".from_hex().unwrap(); - let nonce = "00112233445566778899AABBCCDDEEFF".from_hex().unwrap(); + let key = decode_hex("A7C083FEB7"); + let nonce = decode_hex("00112233445566778899AABBCCDDEEFF"); let input = [0u8; 160]; let expected_output_hex = "FE81D2162C9A100D04895C454A77515BBE6A431A935CB90E2221EBB7EF502328943539492EFF6310C871054C2889CC728F82E86B1AFFF4334B6127A13A155C75151630BD482EB673FF5DB477FA6C53EBE1A4EC38C23C5400C315455D93A2ACED9598604727FA340D5F2A8BD757B77833F74BD2BC049313C80616B4A06268AE350DB92EEC4FA56C171374A67A80C006D0EAD048CE7B640F17D3D5A62D1F251C21"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 160]; @@ -2458,12 +2462,12 @@ mod test { #[test] fn test_sosemanuk_vector128_test2() { - let key = "00112233445566778899AABBCCDDEEFF".from_hex().unwrap(); - let nonce = "8899AABBCCDDEEFF0011223344556677".from_hex().unwrap(); + let key = decode_hex("00112233445566778899AABBCCDDEEFF"); + let nonce = decode_hex("8899AABBCCDDEEFF0011223344556677"); let input = [0u8; 160]; let expected_output_hex = "FA61DBEB71178131A77C714BD2EABF4E1394207A25698AA1308F2F063A0F760604CF67569BA59A3DFAD7F00145C78D29C5FFE5F964950486424451952C84039D234D9C37EECBBCA1EBFB0DD16EA1194A6AFC1A460E33E33FE8D55C48977079C687810D74FEDDEE1B3986218FB1E1C1765E4DF64D7F6911C19A270C59C74B24461717F86CE3B11808FACD4F2E714168DA44CF6360D54DDA2241BCB79401A4EDCC"; - let expected_output = expected_output_hex.from_hex().unwrap(); + let expected_output = decode_hex(expected_output_hex); let mut output = [0u8; 160];