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

Commit e30839e

Browse files
twittnerdebris
authored andcommitted
Consolidate crypto functionality in ethcore-crypto. (#8432)
* Consolidate crypto functionality in `ethcore-crypto`. - Move `ecdh`/`ecies` modules to `ethkey`. - Refactor `ethcore-crypto` to use file per module. - Replace `subtle` with `ethcore_crypto::is_equal`. - Add `aes_gcm` module to `ethcore-crypto`. * Rename `aes::{encrypt,decrypt,decrypt_cbc}` ... ... to `aes::{encrypt_128_ctr,decrypt_128_ctr,decrypt_128_cbc}`.
1 parent a4c7843 commit e30839e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1003
-542
lines changed

Cargo.lock

+11-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethcore/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fetch = { path = "../util/fetch" }
2020
hashdb = { path = "../util/hashdb" }
2121
memorydb = { path = "../util/memorydb" }
2222
patricia-trie = { path = "../util/patricia_trie" }
23+
ethcore-crypto = { path = "crypto" }
2324
error-chain = { version = "0.11", default-features = false }
2425
ethcore-io = { path = "../util/io" }
2526
ethcore-logger = { path = "../logger" }
@@ -56,7 +57,6 @@ util-error = { path = "../util/error" }
5657
snappy = { git = "https://github.com/paritytech/rust-snappy" }
5758
stop-guard = { path = "../util/stop-guard" }
5859
macros = { path = "../util/macros" }
59-
rust-crypto = "0.2.34"
6060
rustc-hex = "1.0"
6161
stats = { path = "../util/stats" }
6262
trace-time = { path = "../util/trace-time" }

ethcore/benches/evm.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern crate test;
2020
extern crate ethcore_util as util;
2121
extern crate rand;
2222
extern crate bn;
23-
extern crate crypto;
23+
extern crate ethcore_crypto;
2424
extern crate ethkey;
2525
extern crate rustc_hex;
2626
extern crate ethcore_bigint;
@@ -61,16 +61,13 @@ fn bn_128_mul(b: &mut Bencher) {
6161

6262
#[bench]
6363
fn sha256(b: &mut Bencher) {
64-
use crypto::sha2::Sha256;
65-
use crypto::digest::Digest;
64+
use ethcore_crypto::digest::sha256;
6665

6766
let mut input: [u8; 256] = [0; 256];
6867
let mut out = [0; 32];
6968

7069
b.iter(|| {
71-
let mut sha = Sha256::new();
72-
sha.input(&input);
73-
sha.result(&mut input[0..32]);
70+
sha256(&input);
7471
});
7572
}
7673

ethcore/crypto/Cargo.toml

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ version = "0.1.0"
44
authors = ["Parity Technologies <admin@parity.io>"]
55

66
[dependencies]
7+
ethereum-types = "0.3"
8+
quick-error = "1.2"
9+
ring = "0.12"
710
rust-crypto = "0.2.36"
811
tiny-keccak = "1.3"
9-
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1", optional = true }
10-
ethkey = { path = "../../ethkey", optional = true }
11-
ethereum-types = "0.3"
12-
subtle = "0.5"
1312

14-
[features]
15-
default = ["secp256k1"]
16-
secp256k1 = ["eth-secp256k1", "ethkey"]

ethcore/crypto/src/aes.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity.
3+
4+
// Parity is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use error::SymmError;
18+
use rcrypto::blockmodes::{CtrMode, CbcDecryptor, PkcsPadding};
19+
use rcrypto::aessafe::{AesSafe128Encryptor, AesSafe128Decryptor};
20+
use rcrypto::symmetriccipher::{Encryptor, Decryptor};
21+
use rcrypto::buffer::{RefReadBuffer, RefWriteBuffer, WriteBuffer};
22+
23+
/// Encrypt a message (CTR mode).
24+
///
25+
/// Key (`k`) length and initialisation vector (`iv`) length have to be 16 bytes each.
26+
/// An error is returned if the input lengths are invalid.
27+
pub fn encrypt_128_ctr(k: &[u8], iv: &[u8], plain: &[u8], dest: &mut [u8]) -> Result<(), SymmError> {
28+
let mut encryptor = CtrMode::new(AesSafe128Encryptor::new(k), iv.to_vec());
29+
encryptor.encrypt(&mut RefReadBuffer::new(plain), &mut RefWriteBuffer::new(dest), true)?;
30+
Ok(())
31+
}
32+
33+
/// Decrypt a message (CTR mode).
34+
///
35+
/// Key (`k`) length and initialisation vector (`iv`) length have to be 16 bytes each.
36+
/// An error is returned if the input lengths are invalid.
37+
pub fn decrypt_128_ctr(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) -> Result<(), SymmError> {
38+
let mut encryptor = CtrMode::new(AesSafe128Encryptor::new(k), iv.to_vec());
39+
encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut RefWriteBuffer::new(dest), true)?;
40+
Ok(())
41+
}
42+
43+
/// Decrypt a message (CBC mode).
44+
///
45+
/// Key (`k`) length and initialisation vector (`iv`) length have to be 16 bytes each.
46+
/// An error is returned if the input lengths are invalid.
47+
pub fn decrypt_128_cbc(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) -> Result<usize, SymmError> {
48+
let mut encryptor = CbcDecryptor::new(AesSafe128Decryptor::new(k), PkcsPadding, iv.to_vec());
49+
let len = dest.len();
50+
let mut buffer = RefWriteBuffer::new(dest);
51+
encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut buffer, true)?;
52+
Ok(len - buffer.remaining())
53+
}
54+

0 commit comments

Comments
 (0)