diff --git a/Cargo.lock b/Cargo.lock index 16493560c4..50e2204336 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -232,6 +232,7 @@ dependencies = [ "cidr 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)", "codechain-core 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-discovery 0.1.0", "codechain-key 0.1.0", "codechain-keystore 0.1.0", @@ -271,7 +272,7 @@ dependencies = [ name = "codechain-core" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-io 1.9.0", "codechain-json 0.1.0", "codechain-key 0.1.0", @@ -312,10 +313,10 @@ dependencies = [ [[package]] name = "codechain-crypto" version = "0.1.0" +source = "git+https://github.com/CodeChain-io/rust-codechain-crypto.git#2857470de2f5480b7d61ff57fb652f9d9fc5585b" dependencies = [ "primitives 0.4.0 (git+https://github.com/CodeChain-io/rust-codechain-primitives.git)", "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "ring 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -324,7 +325,7 @@ dependencies = [ name = "codechain-discovery" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-key 0.1.0", "codechain-logger 0.1.0", "codechain-network 0.1.0", @@ -367,7 +368,7 @@ name = "codechain-key" version = "0.1.0" dependencies = [ "bech32 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "never-type 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -387,7 +388,7 @@ dependencies = [ name = "codechain-keystore" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-json 0.1.0", "codechain-key 0.1.0", "codechain-types 0.1.0", @@ -427,7 +428,7 @@ dependencies = [ name = "codechain-merkle" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "hashdb 0.1.1", "memorydb 0.1.1", @@ -442,7 +443,7 @@ name = "codechain-network" version = "0.1.0" dependencies = [ "cidr 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-io 1.9.0", "codechain-key 0.1.0", "codechain-logger 0.1.0", @@ -469,7 +470,7 @@ version = "0.1.0" dependencies = [ "cidr 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "codechain-core 0.1.0", - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-json 0.1.0", "codechain-key 0.1.0", "codechain-keystore 0.1.0", @@ -505,7 +506,7 @@ dependencies = [ name = "codechain-state" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-key 0.1.0", "codechain-logger 0.1.0", "codechain-merkle 0.1.0", @@ -529,7 +530,7 @@ dependencies = [ name = "codechain-stratum" version = "1.11.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-json 0.1.0", "codechain-logger 0.1.0", "env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -586,7 +587,7 @@ dependencies = [ name = "codechain-types" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-json 0.1.0", "codechain-key 0.1.0", "primitives 0.4.0 (git+https://github.com/CodeChain-io/rust-codechain-primitives.git)", @@ -601,7 +602,7 @@ dependencies = [ name = "codechain-vm" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "codechain-key 0.1.0", "codechain-types 0.1.0", "primitives 0.4.0 (git+https://github.com/CodeChain-io/rust-codechain-primitives.git)", @@ -1227,7 +1228,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "journaldb" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "hashdb 0.1.1", "kvdb 0.1.0", "kvdb-memorydb 0.1.0", @@ -1501,7 +1502,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "memorydb" version = "0.1.1" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "hashdb 0.1.1", "plain_hasher 0.1.0", @@ -2814,7 +2815,7 @@ dependencies = [ name = "trie-standardmap" version = "0.1.0" dependencies = [ - "codechain-crypto 0.1.0", + "codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)", "primitives 0.4.0 (git+https://github.com/CodeChain-io/rust-codechain-primitives.git)", "rlp 0.2.1", ] @@ -3097,6 +3098,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum clippy 0.0.198 (registry+https://github.com/rust-lang/crates.io-index)" = "da3a62431bbcebe5250a1235e022cc61bcc2f32405d8dc08da4011d223c6a4ba" "checksum clippy_lints 0.0.198 (registry+https://github.com/rust-lang/crates.io-index)" = "9517a4eee5daa6eaf318a5bd7a4db0bcd5d92e8d8f22c3e341e60cf1746c73a4" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum codechain-crypto 0.1.0 (git+https://github.com/CodeChain-io/rust-codechain-crypto.git)" = "" "checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc" "checksum cookie 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d53b80dde876f47f03cda35303e368a79b91c70b0d65ecba5fd5280944a08591" "checksum core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "25b9e03f145fd4f2bf705e07b900cd41fc636598fe5dc452fd0db1441c3f496d" diff --git a/Cargo.toml b/Cargo.toml index 160f01f8f4..ba76bb428f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ exclude = [ app_dirs = "^1.2.1" clap = { version = "2", features = ["yaml"] } codechain-core = { path = "core" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-discovery = { path = "discovery" } codechain-logger = { path = "util/logger" } codechain-key = { path = "key" } @@ -64,7 +65,6 @@ lto = true [workspace] members = [ "core", - "crypto", "discovery", "json", "key", diff --git a/core/Cargo.toml b/core/Cargo.toml index cfcff63a5a..00988ddb10 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["CodeChain Team "] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-io = { path = "../util/io" } codechain-json = { path = "../json" } codechain-key = { path = "../key" } diff --git a/crypto/Cargo.toml b/crypto/Cargo.toml deleted file mode 100644 index d16761d96c..0000000000 --- a/crypto/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "codechain-crypto" -version = "0.1.0" -authors = ["CodeChain Team "] - -[dependencies] -ring = "0.14.6" -quick-error = "1.2" -rust-crypto = "0.2.36" -primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" } - -[dev-dependencies] -rand = "0.6.1" diff --git a/crypto/src/aes.rs b/crypto/src/aes.rs deleted file mode 100644 index ac74b16b7a..0000000000 --- a/crypto/src/aes.rs +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2018-2019 Kodebox, Inc. -// This file is part of CodeChain. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -use error::SymmError; -use primitives::H256; -use rcrypto::aes::KeySize::KeySize256; -use rcrypto::aes::{cbc_decryptor, cbc_encryptor}; -use rcrypto::aessafe::AesSafe128Encryptor; -use rcrypto::blockmodes::{CtrMode, PkcsPadding}; -use rcrypto::buffer::{BufferResult, ReadBuffer, RefReadBuffer, RefWriteBuffer, WriteBuffer}; -pub use rcrypto::symmetriccipher::SymmetricCipherError; -use rcrypto::symmetriccipher::{Decryptor, Encryptor}; - -fn is_underflow(result: BufferResult) -> bool { - match result { - BufferResult::BufferUnderflow => true, - BufferResult::BufferOverflow => false, - } -} - -// AES-256/CBC/Pkcs encryption. -pub fn encrypt(data: &[u8], key: &H256, iv: &u128) -> Result, SymmetricCipherError> { - let mut encryptor = cbc_encryptor(KeySize256, key, &iv.to_be_bytes(), PkcsPadding); - - let mut final_result = Vec::::new(); - let mut read_buffer = RefReadBuffer::new(data); - let mut buffer = [0; 4096]; - let mut write_buffer = RefWriteBuffer::new(&mut buffer); - - - let mut finish = false; - while !finish { - finish = is_underflow(encryptor.encrypt(&mut read_buffer, &mut write_buffer, true)?); - final_result.extend(write_buffer.take_read_buffer().take_remaining().iter().cloned()); - } - - Ok(final_result) -} - -// AES-256/CBC/Pkcs decryption. -pub fn decrypt(encrypted_data: &[u8], key: &H256, iv: &u128) -> Result, SymmetricCipherError> { - let mut decryptor = cbc_decryptor(KeySize256, key, &iv.to_be_bytes(), PkcsPadding); - - let mut final_result = Vec::::new(); - let mut read_buffer = RefReadBuffer::new(encrypted_data); - let mut buffer = [0; 4096]; - let mut write_buffer = RefWriteBuffer::new(&mut buffer); - - let mut finish = false; - while !finish { - finish = is_underflow(decryptor.decrypt(&mut read_buffer, &mut write_buffer, true)?); - final_result.extend(write_buffer.take_read_buffer().take_remaining().iter().cloned()); - } - - Ok(final_result) -} - -/// Encrypt a message (CTR mode). -/// -/// Key (`k`) length and initialisation vector (`iv`) length have to be 16 bytes each. -/// An error is returned if the input lengths are invalid. -pub fn encrypt_128_ctr(k: &[u8], iv: &[u8], plain: &[u8], dest: &mut [u8]) -> Result<(), SymmError> { - let mut encryptor = CtrMode::new(AesSafe128Encryptor::new(k), iv.to_vec()); - encryptor.encrypt(&mut RefReadBuffer::new(plain), &mut RefWriteBuffer::new(dest), true)?; - Ok(()) -} - -/// Decrypt a message (CTR mode). -/// -/// Key (`k`) length and initialisation vector (`iv`) length have to be 16 bytes each. -/// An error is returned if the input lengths are invalid. -pub fn decrypt_128_ctr(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) -> Result<(), SymmError> { - let mut encryptor = CtrMode::new(AesSafe128Encryptor::new(k), iv.to_vec()); - encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut RefWriteBuffer::new(dest), true)?; - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - use rand::rngs::OsRng; - use rand::Rng; - use rand::RngCore; - - #[test] - fn aes256_with_random_key_and_iv() { - let message = "0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\ - 0123456789abcdefghijklmnopqrstubewxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - let mut key = H256([0; 32]); - - // In a real program, the key and iv may be determined - // using some other mechanism. If a password is to be used - // as a key, an algorithm like PBKDF2, Bcrypt, or Scrypt (all - // supported by Rust-Crypto!) would be a good choice to derive - // a password. For the purposes of this example, the key and - // iv are just random values. - let mut rng = OsRng::new().ok().unwrap(); - rng.fill_bytes(&mut key); - let iv = rng.gen(); - - let encrypted_data = encrypt(message.as_bytes(), &key, &iv).ok().unwrap(); - let decrypted_data = decrypt(&encrypted_data[..], &key, &iv).ok().unwrap(); - - assert_eq!(message.as_bytes(), &decrypted_data[..]); - } - - #[test] - fn short_input() { - let input = vec![130, 39, 16]; - - let mut key = H256([0; 32]); - - let mut rng = OsRng::new().unwrap(); - rng.fill_bytes(&mut key); - let iv = rng.gen(); - - let encrypted = encrypt(&input, &key, &iv).unwrap(); - let decrypted = decrypt(&encrypted, &key, &iv).unwrap(); - assert_eq!(input, decrypted); - } -} diff --git a/crypto/src/blake.rs b/crypto/src/blake.rs deleted file mode 100644 index 27324fc455..0000000000 --- a/crypto/src/blake.rs +++ /dev/null @@ -1,186 +0,0 @@ -// Copyright 2018 Kodebox, Inc. -// This file is part of CodeChain. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -use primitives::{H128, H160, H256, H512}; -use rcrypto::blake2b::Blake2b; -use rcrypto::digest::Digest; - -/// BLAKE128 -pub fn blake128>(s: T) -> H128 { - H128::blake(s) -} - -pub fn blake128_with_key>(s: T, key: &[u8]) -> H128 { - H128::blake_with_key(s, key) -} - -/// BLAKE256 -pub fn blake256>(s: T) -> H256 { - H256::blake(s) -} - -pub fn blake256_with_key>(s: T, key: &[u8]) -> H256 { - H256::blake_with_key(s, key) -} - -/// BLAKE512 -pub fn blake512>(s: T) -> H512 { - H512::blake(s) -} - -pub fn blake512_with_key>(s: T, key: &[u8]) -> H512 { - H512::blake_with_key(s, key) -} - -pub trait Blake { - fn blake>(s: T) -> Self; - fn blake_with_key>(s: T, key: &[u8]) -> Self; -} - -macro_rules! implement_blake { - ($self:ident) => { - impl Blake for $self { - fn blake>(s: T) -> Self { - let input = s.as_ref(); - let mut result = Self::default(); - let mut hasher = Blake2b::new(result.len()); - hasher.input(input); - hasher.result(&mut *result); - result - } - fn blake_with_key>(s: T, key: &[u8]) -> Self { - let input = s.as_ref(); - let mut result = Self::default(); - let mut hasher = Blake2b::new_keyed(result.len(), &key); - hasher.input(input); - hasher.result(&mut *result); - result - } - } - }; -} - -implement_blake!(H128); -implement_blake!(H160); -implement_blake!(H256); -implement_blake!(H512); - -/// Get the 256-bits BLAKE2b hash of the empty bytes string. -pub const BLAKE_EMPTY: H256 = H256([ - 0x0e, 0x57, 0x51, 0xc0, 0x26, 0xe5, 0x43, 0xb2, 0xe8, 0xab, 0x2e, 0xb0, 0x60, 0x99, 0xda, 0xa1, 0xd1, 0xe5, 0xdf, - 0x47, 0x77, 0x8f, 0x77, 0x87, 0xfa, 0xab, 0x45, 0xcd, 0xf1, 0x2f, 0xe3, 0xa8, -]); - -/// Get the 256-bits BLAKE2b hash of the RLP encoding of empty data. -pub const BLAKE_NULL_RLP: H256 = H256([ - 0x45, 0xb0, 0xcf, 0xc2, 0x20, 0xce, 0xec, 0x5b, 0x7c, 0x1c, 0x62, 0xc4, 0xd4, 0x19, 0x3d, 0x38, 0xe4, 0xeb, 0xa4, - 0x8e, 0x88, 0x15, 0x72, 0x9c, 0xe7, 0x5f, 0x9c, 0x0a, 0xb0, 0xe4, 0xc1, 0xc0, -]); - -/// Get the 256-bits BLAKE2b hash of the RLP encoding of empty list. -pub const BLAKE_EMPTY_LIST_RLP: H256 = H256([ - 0xda, 0x22, 0x3b, 0x09, 0x96, 0x7c, 0x5b, 0xd2, 0x11, 0x07, 0x43, 0x30, 0x7e, 0x0a, 0xf6, 0xd3, 0x9f, 0x61, 0x72, - 0x0a, 0xa7, 0x21, 0x8a, 0x64, 0x0a, 0x08, 0xee, 0xd1, 0x2d, 0xd5, 0x75, 0xc7, -]); - -#[cfg(test)] -mod tests { - use std::panic::catch_unwind; - - use super::*; - - #[test] - fn _blake128() { - let result = H128::blake(b"hello"); - assert_eq!(H128::from("46fb7408d4f285228f4af516ea25851b"), result); - } - - #[test] - fn _blake256() { - let expected = "324dcf027dd4a30a932c441f365a25e86b173defa4b8e58948253471b81b72cf".into(); - let result = blake256(b"hello"); - assert_eq!(result, expected); - } - - #[test] - fn _blake512() { - let expected = "e4cfa39a3d37be31c59609e807970799caa68a19bfaa15135f165085e01d41a65ba1e1b146aeb6bd0092b49eac214c103ccfa3a365954bbbe52f74a2b3620c94".into(); - let result = blake512(b"hello"); - assert_eq!(result, expected); - } - - #[test] - fn blake_empty() { - let expected = BLAKE_EMPTY; - let result = blake256([0u8; 0]); - assert_eq!(result, expected); - } - - #[test] - fn blake_null_rlp() { - let expected = BLAKE_NULL_RLP; - let result = blake256([0x80]); - assert_eq!(result, expected); - } - - #[test] - fn blake_empty_list_rlp() { - let expected = BLAKE_EMPTY_LIST_RLP; - let result = blake256([0xc0]); - assert_eq!(result, expected); - } - - #[test] - fn maximum_length_of_blake256_key_is_512() { - let _ = blake256_with_key([0u8; 0], &[0; 64]); - let must_not_fail = catch_unwind(|| blake256_with_key([0u8; 0], &[0; 64])); - assert!(must_not_fail.is_ok()); - let must_fail = catch_unwind(|| blake256_with_key([0u8; 0], &[0; 65])); - assert!(must_fail.is_err()); - } - - #[test] - fn maximum_length_of_blake512_key_is_512() { - let _ = blake256_with_key([0u8; 0], &[0; 64]); - let must_not_fail = catch_unwind(|| blake512_with_key([0u8; 0], &[0; 64])); - assert!(must_not_fail.is_ok()); - let must_fail = catch_unwind(|| blake512_with_key([0u8; 0], &[0; 65])); - assert!(must_fail.is_err()); - } - - #[test] - fn blake256_output_changes_when_key_changes() { - let r1 = blake256_with_key([0u8; 0], &[0; 64]); - let r2 = blake256_with_key([0u8; 0], &[1; 64]); - assert_ne!(r1, r2); - } - - #[test] - fn blake_trait_with_h256() { - let input = b"hello world"; - let hash_result = blake256(&input); - let trait_result = H256::blake(&input); - assert_eq!(hash_result, trait_result); - } - - #[test] - fn blake_trait_with_h512() { - let input = b"hello world"; - let hash_result = blake512(&input); - let trait_result = H512::blake(&input); - assert_eq!(hash_result, trait_result); - } -} diff --git a/crypto/src/error.rs b/crypto/src/error.rs deleted file mode 100644 index 6677c1b6da..0000000000 --- a/crypto/src/error.rs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -quick_error! { - #[derive(Debug)] - pub enum Error { - Scrypt(e: ScryptError) { - cause(e) - from() - } - Symm(e: SymmError) { - cause(e) - from() - } - ZeroIterations { - description("Iterations' value should not be zero") - } - } -} - -quick_error! { - #[derive(Debug)] - pub enum ScryptError { - // log(N) < r / 16 - InvalidN { - display("Invalid N argument of the scrypt encryption") - } - // p <= (2^31-1 * 32)/(128 * r) - InvalidP { - display("Invalid p argument of the scrypt encryption") - } - } -} - -#[allow(deprecated)] -mod errors { - use rcrypto; - use ring; - - quick_error! { - #[derive(Debug)] - pub enum SymmError wraps PrivSymmErr { - RustCrypto(e: rcrypto::symmetriccipher::SymmetricCipherError) { - display("symmetric crypto error") - from() - } - Ring(e: ring::error::Unspecified) { - display("symmetric crypto error") - cause(e) - from() - } - Offset(x: usize) { - display("offset {} greater than slice length", x) - } - } - } - - impl From for SymmError { - fn from(e: ring::error::Unspecified) -> SymmError { - SymmError(PrivSymmErr::Ring(e)) - } - } - - impl From for SymmError { - fn from(e: rcrypto::symmetriccipher::SymmetricCipherError) -> SymmError { - SymmError(PrivSymmErr::RustCrypto(e)) - } - } -} -pub use self::errors::SymmError; diff --git a/crypto/src/hash.rs b/crypto/src/hash.rs deleted file mode 100644 index f258d3db5e..0000000000 --- a/crypto/src/hash.rs +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2018 Kodebox, Inc. -// This file is part of CodeChain. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -use primitives::{H160, H256}; -use rcrypto::digest::Digest; -use rcrypto::ripemd160::Ripemd160; -use rcrypto::sha1::Sha1; -use rcrypto::sha2::Sha256; -use rcrypto::sha3::Sha3; - -/// RIPEMD160 -#[inline] -pub fn ripemd160>(s: T) -> H160 { - let input = s.as_ref(); - let mut result = H160::default(); - let mut hasher = Ripemd160::new(); - hasher.input(input); - hasher.result(&mut *result); - result -} - -/// SHA-1 -#[inline] -pub fn sha1>(s: T) -> H160 { - let input = s.as_ref(); - let mut result = H160::default(); - let mut hasher = Sha1::new(); - hasher.input(input); - hasher.result(&mut *result); - result -} - -/// SHA-256 -#[inline] -pub fn sha256>(s: T) -> H256 { - let input = s.as_ref(); - let mut result = H256::default(); - let mut hasher = Sha256::new(); - hasher.input(input); - hasher.result(&mut *result); - result -} - -/// KECCAK256 -#[inline] -pub fn keccak256>(s: T) -> H256 { - let input = s.as_ref(); - let mut result = H256::default(); - let mut hasher = Sha3::keccak256(); - hasher.input(input); - hasher.result(&mut result); - result -} - -#[cfg(test)] -mod tests { - use super::{keccak256, ripemd160, sha1, sha256}; - - #[test] - fn _ripemd160() { - let expected = "108f07b8382412612c048d07d13f814118445acd".into(); - let result = ripemd160(b"hello"); - assert_eq!(result, expected); - } - - #[test] - fn _sha1() { - let expected = "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d".into(); - let result = sha1(b"hello"); - assert_eq!(result, expected); - } - - #[test] - fn _sha256() { - let expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".into(); - let result = sha256(b"hello"); - assert_eq!(result, expected); - } - - #[test] - fn _keccak256() { - let expected = "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8".into(); - let result = keccak256(b"hello"); - assert_eq!(result, expected); - } -} diff --git a/crypto/src/lib.rs b/crypto/src/lib.rs deleted file mode 100644 index cc2be3158c..0000000000 --- a/crypto/src/lib.rs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2018-2019 Kodebox, Inc. -// This file is part of CodeChain. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -extern crate crypto as rcrypto; -extern crate primitives; -#[macro_use] -extern crate quick_error; -#[cfg(test)] -extern crate rand; -extern crate ring; - -pub mod aes; -mod blake; -pub mod error; -mod hash; -mod password; -pub mod pbkdf2; -pub mod scrypt; - -use std::num::NonZeroU32; - -pub use crate::error::Error; - -pub const KEY_LENGTH: usize = 32; -pub const KEY_ITERATIONS: usize = 10240; -pub const KEY_LENGTH_AES: usize = KEY_LENGTH / 2; - -pub use crate::blake::*; -pub use crate::hash::{keccak256, ripemd160, sha1, sha256}; -pub use crate::password::Password; - -// Do not move Password. It will make debugger print the password. -pub fn derive_key_iterations(password: &Password, salt: &[u8; 32], c: NonZeroU32) -> (Vec, Vec) { - let mut derived_key = [0u8; KEY_LENGTH]; - pbkdf2::sha256(c, &pbkdf2::Salt(salt), &pbkdf2::Secret(password.as_bytes()), &mut derived_key); - let derived_right_bits = &derived_key[0..KEY_LENGTH_AES]; - let derived_left_bits = &derived_key[KEY_LENGTH_AES..KEY_LENGTH]; - (derived_right_bits.to_vec(), derived_left_bits.to_vec()) -} - -pub fn derive_mac(derived_left_bits: &[u8], cipher_text: &[u8]) -> Vec { - let mut mac = vec![0u8; KEY_LENGTH_AES + cipher_text.len()]; - mac[0..KEY_LENGTH_AES].copy_from_slice(derived_left_bits); - mac[KEY_LENGTH_AES..cipher_text.len() + KEY_LENGTH_AES].copy_from_slice(cipher_text); - mac -} - -pub fn is_equal(a: &[u8], b: &[u8]) -> bool { - ring::constant_time::verify_slices_are_equal(a, b).is_ok() -} diff --git a/crypto/src/password.rs b/crypto/src/password.rs deleted file mode 100644 index b20d37a63a..0000000000 --- a/crypto/src/password.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2019 Kodebox, Inc. -// This file is part of CodeChain. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -use std::ops::Deref; - -pub struct Password<'a>(pub &'a str); - -impl<'a> Deref for Password<'a> { - type Target = str; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} diff --git a/crypto/src/pbkdf2.rs b/crypto/src/pbkdf2.rs deleted file mode 100644 index 71b1aec277..0000000000 --- a/crypto/src/pbkdf2.rs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -use std::num::NonZeroU32; - -use ring; - -pub struct Salt<'a>(pub &'a [u8]); -pub struct Secret<'a>(pub &'a [u8]); - -pub fn sha256(iter: NonZeroU32, salt: &Salt, sec: &Secret, out: &mut [u8; 32]) { - ring::pbkdf2::derive(&ring::digest::SHA256, iter, salt.0, sec.0, &mut out[..]) -} - -pub fn sha512(iter: NonZeroU32, salt: &Salt, sec: &Secret, out: &mut [u8; 64]) { - ring::pbkdf2::derive(&ring::digest::SHA512, iter, salt.0, sec.0, &mut out[..]) -} diff --git a/crypto/src/scrypt.rs b/crypto/src/scrypt.rs deleted file mode 100644 index 333f8b4c41..0000000000 --- a/crypto/src/scrypt.rs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -use rcrypto::scrypt::{scrypt, ScryptParams}; - -use crate::error::ScryptError; -use crate::{Password, KEY_LENGTH, KEY_LENGTH_AES}; - -// Do not move Password. It will make debugger print the password. -pub fn derive_key(pass: &Password, salt: &[u8; 32], n: u32, p: u32, r: u32) -> Result<(Vec, Vec), ScryptError> { - // sanity checks - let log_n = (32 - n.leading_zeros() - 1) as u8; - if u32::from(log_n) >= r * 16 { - return Err(ScryptError::InvalidN) - } - - if u64::from(p) > ((u64::from(u32::max_value()) - 1) * 32) / (128 * u64::from(r)) { - return Err(ScryptError::InvalidP) - } - - let mut derived_key = vec![0u8; KEY_LENGTH]; - let scrypt_params = ScryptParams::new(log_n, r, p); - scrypt(pass.as_bytes(), salt, &scrypt_params, &mut derived_key); - let derived_right_bits = &derived_key[0..KEY_LENGTH_AES]; - let derived_left_bits = &derived_key[KEY_LENGTH_AES..KEY_LENGTH]; - Ok((derived_right_bits.to_vec(), derived_left_bits.to_vec())) -} diff --git a/discovery/Cargo.toml b/discovery/Cargo.toml index 3c02527478..75152d0445 100644 --- a/discovery/Cargo.toml +++ b/discovery/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["CodeChain Team "] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-key = { path = "../key" } codechain-logger = { path = "../util/logger" } codechain-network = { path = "../network" } diff --git a/key/Cargo.toml b/key/Cargo.toml index a4a5f8d216..9ee276691f 100644 --- a/key/Cargo.toml +++ b/key/Cargo.toml @@ -9,7 +9,7 @@ rustc-hex = "1.0" rustc-serialize = "0.3" lazy_static = "1.2" bech32 = "0.2.2" -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } never-type = "0.1.0" parking_lot = "0.6.0" primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" } diff --git a/keystore/Cargo.toml b/keystore/Cargo.toml index 5ffe2db17d..20aa62ae9a 100644 --- a/keystore/Cargo.toml +++ b/keystore/Cargo.toml @@ -16,7 +16,7 @@ serde_derive = "1.0" rustc-hex = "1.0" time = "0.1.34" parking_lot = "0.6.0" -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } smallvec = "0.4" tempdir = "0.3" diff --git a/network/Cargo.toml b/network/Cargo.toml index 8bcd319e56..9e6ff4308b 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["CodeChain Team "] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-io = { path = "../util/io" } codechain-key = { path = "../key" } codechain-logger = { path = "../util/logger" } diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index b48638f844..181aaddd46 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -8,7 +8,7 @@ authors = ["CodeChain Team "] [dependencies] cidr = "0.0.4" codechain-core = { path = "../core" } -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-json = { path = "../json" } codechain-key = { path = "../key" } codechain-keystore = { path = "../keystore" } diff --git a/state/Cargo.toml b/state/Cargo.toml index 483efd1285..3ba24b3a17 100644 --- a/state/Cargo.toml +++ b/state/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["CodeChain Team "] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-logger = { path = "../util/logger" } codechain-merkle = { path = "../util/merkle" } codechain-key = { path = "../key" } diff --git a/stratum/Cargo.toml b/stratum/Cargo.toml index 93e6feb582..7840f2cabb 100644 --- a/stratum/Cargo.toml +++ b/stratum/Cargo.toml @@ -6,7 +6,7 @@ license = "GPL-3.0" authors = ["Parity Technologies ", "CodeChain Team "] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-logger = { path = "../util/logger" } codechain-json = { path = "../json" } jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.11" } diff --git a/types/Cargo.toml b/types/Cargo.toml index 911c3d0fe0..379558ca39 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["CodeChain Team "] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-json = { path = "../json" } codechain-key = { path = "../key" } primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" } diff --git a/util/journaldb/Cargo.toml b/util/journaldb/Cargo.toml index 563e7756aa..1b4ac31d4f 100644 --- a/util/journaldb/Cargo.toml +++ b/util/journaldb/Cargo.toml @@ -14,5 +14,5 @@ rlp = { path = "../rlp" } util-error = { path = "../error" } [dev-dependencies] -codechain-crypto = { path = "../../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } kvdb-memorydb = { path = "../kvdb-memorydb" } diff --git a/util/memorydb/Cargo.toml b/util/memorydb/Cargo.toml index 457813e6bf..ea800266c2 100644 --- a/util/memorydb/Cargo.toml +++ b/util/memorydb/Cargo.toml @@ -7,7 +7,7 @@ license = "GPL-3.0" [dependencies] elastic-array = "0.10" -codechain-crypto = { version = "0.1.0", path = "../../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } hashdb = { version = "0.1.1", path = "../hashdb" } plain_hasher = { path = "../plain_hasher" } primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" } diff --git a/util/merkle/Cargo.toml b/util/merkle/Cargo.toml index 112658681c..4cf53e157a 100644 --- a/util/merkle/Cargo.toml +++ b/util/merkle/Cargo.toml @@ -7,7 +7,7 @@ authors = ["CodeChain Team "] elastic-array = "0.10" rand = "0.6.1" hashdb = {path = "../hashdb" } -codechain-crypto = { path = "../../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" } rlp = {path = "../rlp" } diff --git a/util/trie-standardmap/Cargo.toml b/util/trie-standardmap/Cargo.toml index c3d2cb0e39..495f7fc6b4 100644 --- a/util/trie-standardmap/Cargo.toml +++ b/util/trie-standardmap/Cargo.toml @@ -5,6 +5,6 @@ authors = ["debris ", "CodeChain Team "] [lib] [dependencies] -codechain-crypto = { path = "../crypto" } +codechain-crypto = { git = "https://github.com/CodeChain-io/rust-codechain-crypto.git", version = "0.1" } codechain-key = { path = "../key" } codechain-types = { path = "../types" } primitives = { git = "https://github.com/CodeChain-io/rust-codechain-primitives.git", version = "0.4" }