Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove rustc-serialize usage #415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions src/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl<'a> AesGcm<'a> {
end_tag: final_block
}
}

}

impl<'a> AeadEncryptor for AesGcm<'static> {
Expand Down Expand Up @@ -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<u8> {
raw_hex.from_hex().ok().unwrap()
hex::FromHex::from_hex(raw_hex).ok().unwrap()
}
struct TestVector {
key: Vec<u8>,
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 12 additions & 9 deletions src/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
hex::FromHex::from_hex(value).unwrap()
}

struct Test {
input: Vec<u8>,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = repeat(0).take((self.output_bits()+7)/8).collect();
self.result(&mut buf);
buf[..].to_hex()
(&buf[..]).to_hex()
}
}
30 changes: 17 additions & 13 deletions src/hc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
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];

Expand All @@ -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];

Expand All @@ -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];

Expand All @@ -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];

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 7 additions & 8 deletions src/pbkdf2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,11 +144,11 @@ pub fn pbkdf2_simple(password: &str, c: u32) -> io::Result<String> {
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)
Expand Down Expand Up @@ -195,7 +194,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Parse the iteration count
let c = match iter.next() {
Some(pstr) => match pstr.from_base64() {
Some(pstr) => match base64::decode(pstr) {
Ok(pvec) => {
if pvec.len() != 4 { return Err(ERR_STR); }
read_u32_be(&pvec[..])
Expand All @@ -207,7 +206,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Salt
let salt = match iter.next() {
Some(sstr) => match sstr.from_base64() {
Some(sstr) => match base64::decode(sstr) {
Ok(salt) => salt,
Err(_) => return Err(ERR_STR)
},
Expand All @@ -216,7 +215,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Hashed value
let hash = match iter.next() {
Some(hstr) => match hstr.from_base64() {
Some(hstr) => match base64::decode(hstr) {
Ok(hash) => hash,
Err(_) => return Err(ERR_STR)
},
Expand Down
17 changes: 8 additions & 9 deletions src/scrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -287,19 +286,19 @@ pub fn scrypt_simple(password: &str, params: &ScryptParams) -> io::Result<String
tmp[0] = params.log_n;
tmp[1] = params.r as u8;
tmp[2] = params.p as u8;
result.push_str(&*tmp.to_base64(base64::STANDARD));
result.push_str(&*base64::encode_config(&tmp, base64::STANDARD));
} else {
result.push_str("1$");
let mut tmp = [0u8; 9];
tmp[0] = params.log_n;
write_u32_le(&mut tmp[1..5], params.r);
write_u32_le(&mut tmp[5..9], params.p);
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)
Expand Down Expand Up @@ -339,7 +338,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result<bool, &'static
// Parse the parameters - the size of them depends on the if we are using the compact or
// expanded format
let pvec = match iter.next() {
Some(pstr) => match pstr.from_base64() {
Some(pstr) => match base64::decode(pstr) {
Ok(x) => x,
Err(_) => return Err(ERR_STR)
},
Expand Down Expand Up @@ -368,7 +367,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Salt
let salt = match iter.next() {
Some(sstr) => match sstr.from_base64() {
Some(sstr) => match base64::decode(sstr) {
Ok(salt) => salt,
Err(_) => return Err(ERR_STR)
},
Expand All @@ -377,7 +376,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Hashed value
let hash = match iter.next() {
Some(hstr) => match hstr.from_base64() {
Some(hstr) => match base64::decode(hstr) {
Ok(hash) => hash,
Err(_) => return Err(ERR_STR)
},
Expand Down
12 changes: 9 additions & 3 deletions src/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
hex::FromHex::from_hex(self.input).unwrap()
}
}

fn test_hash<D: Digest>(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];

Expand All @@ -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;
}

Expand Down
Loading