From 4c12a9eed43b6c0a2c3b194a7201ba25e0b8432b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 9 Apr 2019 03:04:54 -0400 Subject: stop using rustc_serialize --- Cargo.lock | 30 +++++++++++++++++++------- Cargo.toml | 3 ++- src/base64.rs | 5 ----- src/lib.rs | 3 +-- src/md4.rs | 5 +---- src/primitives.rs | 4 ++++ src/sha1.rs | 17 +++++++++------ tests/set1.rs | 63 +++++++++++++++++++++++++++++-------------------------- tests/set2.rs | 31 ++++++++++++++------------- tests/set3.rs | 14 ++++++------- tests/util.rs | 9 +++----- 11 files changed, 101 insertions(+), 83 deletions(-) delete mode 100644 src/base64.rs diff --git a/Cargo.lock b/Cargo.lock index 7c7f6da..dadad86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,11 +3,24 @@ name = "autocfg" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "base64" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byteorder" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cc" version = "1.0.35" @@ -44,6 +57,11 @@ name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "1.3.0" @@ -58,10 +76,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "matasano" version = "0.0.1" dependencies = [ + "base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.20 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -204,11 +223,6 @@ name = "redox_syscall" version = "0.1.54" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "rustc-serialize" -version = "0.3.24" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "rustc_version" version = "0.2.3" @@ -266,13 +280,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" +"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)" = "5e5f3fee5eeb60324c2781f1e41286bdee933850fff9b3c672587fed5ec58c83" "checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" "checksum openssl 0.10.20 (registry+https://github.com/rust-lang/crates.io-index)" = "5a0d6b781aac4ac1bd6cafe2a2f0ad8c16ae8e1dd5184822a16c50139f8838d9" @@ -290,7 +307,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252" -"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" diff --git a/Cargo.toml b/Cargo.toml index 9246928..d192c5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,8 @@ name = "set4" path = "tests/set4.rs" [dependencies] -rustc-serialize = "0.3" +base64 = "0.10" +hex = "0.3" openssl = "0.10" rand = "0.6" rand_core = "0.4" diff --git a/src/base64.rs b/src/base64.rs deleted file mode 100644 index 9b01da8..0000000 --- a/src/base64.rs +++ /dev/null @@ -1,5 +0,0 @@ -use rustc_serialize::base64::{ToBase64, STANDARD}; - -pub fn to_base64(bytes: &[u8]) -> String { - return bytes.to_base64(STANDARD); -} diff --git a/src/lib.rs b/src/lib.rs index 51fc134..59dab62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ mod aes; -mod base64; mod crack; mod data; mod http; @@ -14,7 +13,6 @@ pub use aes::decrypt_aes_128_cbc; pub use aes::decrypt_aes_128_ecb; pub use aes::encrypt_aes_128_cbc; pub use aes::encrypt_aes_128_ecb; -pub use base64::to_base64; pub use crack::clone_mersenne_twister_from_output; pub use crack::crack_aes_128_ctr_random_access; pub use crack::crack_cbc_bitflipping; @@ -46,6 +44,7 @@ pub use md4::pad_md4; pub use primitives::fixed_xor; pub use primitives::pad_pkcs7; pub use primitives::repeating_key_xor; +pub use primitives::to_base64; pub use primitives::unpad_pkcs7; pub use random::mt19937_stream_cipher; pub use random::MersenneTwister; diff --git a/src/md4.rs b/src/md4.rs index 6495ee2..2df6971 100644 --- a/src/md4.rs +++ b/src/md4.rs @@ -1,6 +1,3 @@ -#[cfg(test)] -use rustc_serialize::hex::ToHex; - pub fn md4(bytes: &[u8]) -> [u8; 16] { md4_with_state( bytes, @@ -185,7 +182,7 @@ fn test_md4() { ]; for &(input, expected) in tests.iter() { println!("{:?}", input); - let got = &md4(input)[..].to_hex(); + let got = hex::encode(&md4(input)[..]); assert_eq!(got, expected); } } diff --git a/src/primitives.rs b/src/primitives.rs index 6f178cc..a93f83c 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -1,3 +1,7 @@ +pub fn to_base64(bytes: &[u8]) -> String { + return base64::encode(bytes); +} + pub fn fixed_xor(bytes1: &[u8], bytes2: &[u8]) -> Vec { return bytes1 .iter() diff --git a/src/sha1.rs b/src/sha1.rs index 7969b21..6b76a3f 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -1,6 +1,3 @@ -#[cfg(test)] -use rustc_serialize::hex::ToHex; - use crate::primitives::fixed_xor; pub fn sha1(bytes: &[u8]) -> [u8; 20] { @@ -138,7 +135,7 @@ fn test_sha1() { ), ]; for &(input, expected) in tests.iter() { - let got = &sha1(input)[..].to_hex(); + let got = hex::encode(&sha1(input)[..]); assert_eq!(got, expected); } } @@ -146,8 +143,16 @@ fn test_sha1() { #[test] fn test_sha1_hmac() { assert_eq!( - &sha1_hmac(b"", b"")[..].to_hex(), + hex::encode(&sha1_hmac(b"", b"")[..]), "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d" ); - assert_eq!(&sha1_hmac(b"The quick brown fox jumps over the lazy dog", b"key")[..].to_hex(), "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"); + assert_eq!( + hex::encode( + &sha1_hmac( + b"The quick brown fox jumps over the lazy dog", + b"key" + )[..] + ), + "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9" + ); } diff --git a/tests/set1.rs b/tests/set1.rs index f1db7e5..f8d0981 100644 --- a/tests/set1.rs +++ b/tests/set1.rs @@ -1,14 +1,13 @@ -use rustc_serialize::hex::FromHex; - mod util; #[test] fn problem_1() { - let hex = "49276d206b696c6c696e6720796f757220627261\ - 696e206c696b65206120706f69736f6e6f757320\ - 6d757368726f6f6d" - .from_hex() - .unwrap(); + let hex = hex::decode( + "49276d206b696c6c696e6720796f757220627261\ + 696e206c696b65206120706f69736f6e6f757320\ + 6d757368726f6f6d", + ) + .unwrap(); let base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEg\ cG9pc29ub3VzIG11c2hyb29t"; let got = matasano::to_base64(&hex[..]); @@ -17,19 +16,21 @@ fn problem_1() { #[test] fn problem_2() { - let bytes1 = "1c0111001f010100061a024b53535009181c".from_hex().unwrap(); - let bytes2 = "686974207468652062756c6c277320657965".from_hex().unwrap(); - let expected = "746865206b696420646f6e277420706c6179".from_hex().unwrap(); + let bytes1 = hex::decode("1c0111001f010100061a024b53535009181c").unwrap(); + let bytes2 = hex::decode("686974207468652062756c6c277320657965").unwrap(); + let expected = + hex::decode("746865206b696420646f6e277420706c6179").unwrap(); let got = matasano::fixed_xor(&bytes1[..], &bytes2[..]); assert_eq!(got, expected); } #[test] fn problem_3() { - let ciphertext = "1b37373331363f78151b7f2b783431333d783978\ - 28372d363c78373e783a393b3736" - .from_hex() - .unwrap(); + let ciphertext = hex::decode( + "1b37373331363f78151b7f2b783431333d783978\ + 28372d363c78373e783a393b3736", + ) + .unwrap(); let plaintext = b"Cooking MC's like a pound of bacon"; let got = matasano::crack_single_byte_xor(&ciphertext[..]); assert_eq!(got, &plaintext[..]); @@ -48,12 +49,13 @@ fn problem_5() { let plaintext = b"Burning 'em, if you ain't quick and nimble\n\ I go crazy when I hear a cymbal"; let key = b"ICE"; - let ciphertext = "0b3637272a2b2e63622c2e69692a23693a2a3c63\ - 24202d623d63343c2a26226324272765272a282b\ - 2f20430a652e2c652a3124333a653e2b2027630c\ - 692b20283165286326302e27282f" - .from_hex() - .unwrap(); + let ciphertext = hex::decode( + "0b3637272a2b2e63622c2e69692a23693a2a3c63\ + 24202d623d63343c2a26226324272765272a282b\ + 2f20430a652e2c652a3124333a653e2b2027630c\ + 692b20283165286326302e27282f", + ) + .unwrap(); let got = matasano::repeating_key_xor(plaintext, key); assert_eq!(got, ciphertext); } @@ -79,16 +81,17 @@ fn problem_7() { #[test] fn problem_8() { let possibles = util::read_as_hex_lines("data/8.txt"); - let ciphertext = "d880619740a8a19b7840a8a31c810a3d08649af7\ - 0dc06f4fd5d2d69c744cd283e2dd052f6b641dbf\ - 9d11b0348542bb5708649af70dc06f4fd5d2d69c\ - 744cd2839475c9dfdbc1d46597949d9c7e82bf5a\ - 08649af70dc06f4fd5d2d69c744cd28397a93eab\ - 8d6aecd566489154789a6b0308649af70dc06f4f\ - d5d2d69c744cd283d403180c98c8f6db1f2a3f9c\ - 4040deb0ab51b29933f2c123c58386b06fba186a" - .from_hex() - .unwrap(); + let ciphertext = hex::decode( + "d880619740a8a19b7840a8a31c810a3d08649af7\ + 0dc06f4fd5d2d69c744cd283e2dd052f6b641dbf\ + 9d11b0348542bb5708649af70dc06f4fd5d2d69c\ + 744cd2839475c9dfdbc1d46597949d9c7e82bf5a\ + 08649af70dc06f4fd5d2d69c744cd28397a93eab\ + 8d6aecd566489154789a6b0308649af70dc06f4f\ + d5d2d69c744cd283d403180c98c8f6db1f2a3f9c\ + 4040deb0ab51b29933f2c123c58386b06fba186a", + ) + .unwrap(); let got = matasano::find_aes_128_ecb_encrypted_string(&possibles[..]); assert_eq!(got, ciphertext); } diff --git a/tests/set2.rs b/tests/set2.rs index a8de407..04614de 100644 --- a/tests/set2.rs +++ b/tests/set2.rs @@ -2,7 +2,6 @@ use std::borrow::ToOwned; use std::collections::HashMap; use rand::Rng; -use rustc_serialize::base64::FromBase64; mod util; @@ -74,13 +73,14 @@ fn problem_11() { #[test] fn problem_12() { - let padding = b"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWct\ - dG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpU\ - aGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ - dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5v\ - LCBJIGp1c3QgZHJvdmUgYnkK" - .from_base64() - .unwrap(); + let padding = base64::decode( + &b"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWct\ + dG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpU\ + aGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ + dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5v\ + LCBJIGp1c3QgZHJvdmUgYnkK"[..], + ) + .unwrap(); let fixed_padding = |input: &[u8]| -> Vec { return input.iter().chain(padding.iter()).map(|x| *x).collect(); }; @@ -137,13 +137,14 @@ fn problem_13() { #[test] fn problem_14() { - let padding = b"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWct\ - dG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpU\ - aGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ - dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5v\ - LCBJIGp1c3QgZHJvdmUgYnkK" - .from_base64() - .unwrap(); + let padding = base64::decode( + &b"Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWct\ + dG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpU\ + aGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\ + dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5v\ + LCBJIGp1c3QgZHJvdmUgYnkK"[..], + ) + .unwrap(); let front_padding: Vec = rand::thread_rng() .sample_iter(&rand::distributions::Standard) .take(rand::thread_rng().gen_range(1, 100)) diff --git a/tests/set3.rs b/tests/set3.rs index 5d66e88..830d714 100644 --- a/tests/set3.rs +++ b/tests/set3.rs @@ -1,5 +1,4 @@ use rand::{FromEntropy, Rng}; -use rustc_serialize::base64::FromBase64; mod util; @@ -22,7 +21,7 @@ fn problem_17() { static mut CHOSEN_PLAINTEXT_IDX: usize = 0; let encrypter = || { let idx = rand::thread_rng().gen_range(0, strings.len()); - let plaintext = strings[idx].from_base64().unwrap(); + let plaintext = base64::decode(strings[idx]).unwrap(); unsafe { CHOSEN_PLAINTEXT_IDX = idx }; let iv = util::random_aes_128_key(); return ( @@ -45,17 +44,18 @@ fn problem_17() { &validator, ); let idx = unsafe { CHOSEN_PLAINTEXT_IDX.clone() }; - let expected = strings[idx].from_base64().unwrap(); + let expected = base64::decode(strings[idx]).unwrap(); assert_eq!(plaintext, expected); } } #[test] fn problem_18() { - let ciphertext = b"L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syL\ - XzhPweyyMTJULu/6/kXX0KSvoOLSFQ==" - .from_base64() - .unwrap(); + let ciphertext = base64::decode( + &b"L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syL\ + XzhPweyyMTJULu/6/kXX0KSvoOLSFQ=="[..], + ) + .unwrap(); let plaintext = &b"Yo, VIP Let's kick it Ice, Ice, baby Ice, Ice, baby "[..]; let got = matasano::aes_128_ctr(&ciphertext[..], b"YELLOW SUBMARINE", 0); diff --git a/tests/util.rs b/tests/util.rs index a253c2d..0d1e37f 100644 --- a/tests/util.rs +++ b/tests/util.rs @@ -4,14 +4,11 @@ use rand::{Rng, RngCore}; use std::fs::File; use std::io::prelude::*; -use rustc_serialize::base64::FromBase64; -use rustc_serialize::hex::FromHex; - pub fn read_as_hex_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() - .map(|line| line.unwrap().from_hex().unwrap()) + .map(|line| hex::decode(line.unwrap()).unwrap()) .collect(); } @@ -19,7 +16,7 @@ pub fn read_as_base64_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() - .map(|line| line.unwrap().from_base64().unwrap()) + .map(|line| base64::decode(&line.unwrap()).unwrap()) .collect(); } @@ -35,7 +32,7 @@ pub fn read_as_base64(filename: &str) -> Vec { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() - .map(|line| line.unwrap().from_base64().unwrap()) + .map(|line| base64::decode(&line.unwrap()).unwrap()) .collect::>>() .concat(); } -- cgit v1.2.3-54-g00ecf