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}`.
This commit is contained in:
committed by
Marek Kotewicz
parent
a4c7843a07
commit
e30839e85f
@@ -23,7 +23,6 @@ extern crate futures;
|
||||
|
||||
extern crate ansi_term;
|
||||
extern crate cid;
|
||||
extern crate crypto as rust_crypto;
|
||||
extern crate futures_cpupool;
|
||||
extern crate itertools;
|
||||
extern crate multihash;
|
||||
|
||||
@@ -18,21 +18,15 @@
|
||||
|
||||
use multihash;
|
||||
use cid::{Cid, Codec, Version};
|
||||
use rust_crypto::sha2::Sha256;
|
||||
use rust_crypto::digest::Digest;
|
||||
use crypto::digest;
|
||||
use jsonrpc_core::Error;
|
||||
use v1::types::Bytes;
|
||||
use super::errors;
|
||||
|
||||
/// Compute CIDv0 from protobuf encoded bytes.
|
||||
pub fn cid(content: Bytes) -> Result<String, Error> {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.input(&content.0);
|
||||
let len = hasher.output_bytes();
|
||||
let mut buf = Vec::with_capacity(len);
|
||||
buf.resize(len, 0);
|
||||
hasher.result(&mut buf);
|
||||
let mh = multihash::encode(multihash::Hash::SHA2256, &buf).map_err(errors::encoding)?;
|
||||
let hash = digest::sha256(&content.0);
|
||||
let mh = multihash::encode(multihash::Hash::SHA2256, &*hash).map_err(errors::encoding)?;
|
||||
let cid = Cid::new(Codec::DagProtobuf, Version::V0, &mh);
|
||||
Ok(cid.to_string().into())
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use rand::{Rng, OsRng};
|
||||
use ethkey::{Public, Secret, Random, Generator, math};
|
||||
use ethkey::{self, Public, Secret, Random, Generator, math};
|
||||
use crypto;
|
||||
use bytes::Bytes;
|
||||
use jsonrpc_core::Error;
|
||||
@@ -36,7 +36,7 @@ pub fn generate_document_key(account_public: Public, server_key_public: Public)
|
||||
let (common_point, encrypted_point) = encrypt_secret(document_key.public(), &server_key_public)?;
|
||||
|
||||
// ..and now encrypt document key with account public
|
||||
let encrypted_key = crypto::ecies::encrypt(&account_public, &crypto::DEFAULT_MAC, document_key.public())
|
||||
let encrypted_key = ethkey::crypto::ecies::encrypt(&account_public, &crypto::DEFAULT_MAC, document_key.public())
|
||||
.map_err(errors::encryption)?;
|
||||
|
||||
Ok(EncryptedDocumentKey {
|
||||
@@ -57,7 +57,7 @@ pub fn encrypt_document(key: Bytes, document: Bytes) -> Result<Bytes, Error> {
|
||||
{
|
||||
let (mut encryption_buffer, iv_buffer) = encrypted_document.split_at_mut(document.len());
|
||||
|
||||
crypto::aes::encrypt(&key, &iv, &document, &mut encryption_buffer);
|
||||
crypto::aes::encrypt_128_ctr(&key, &iv, &document, &mut encryption_buffer).map_err(errors::encryption)?;
|
||||
iv_buffer.copy_from_slice(&iv);
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ pub fn decrypt_document(key: Bytes, mut encrypted_document: Bytes) -> Result<Byt
|
||||
// use symmetric decryption to decrypt document
|
||||
let iv = encrypted_document.split_off(encrypted_document_len - INIT_VEC_LEN);
|
||||
let mut document = vec![0; encrypted_document_len - INIT_VEC_LEN];
|
||||
crypto::aes::decrypt(&key, &iv, &encrypted_document, &mut document);
|
||||
crypto::aes::decrypt_128_ctr(&key, &iv, &encrypted_document, &mut document).map_err(errors::encryption)?;
|
||||
|
||||
Ok(document)
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ use std::collections::{BTreeMap, HashSet};
|
||||
|
||||
use version::version_data;
|
||||
|
||||
use crypto::{ecies, DEFAULT_MAC};
|
||||
use ethkey::{Brain, Generator};
|
||||
use crypto::DEFAULT_MAC;
|
||||
use ethkey::{crypto::ecies, Brain, Generator};
|
||||
use ethstore::random_phrase;
|
||||
use sync::LightSyncProvider;
|
||||
use ethcore::account_provider::AccountProvider;
|
||||
|
||||
@@ -22,8 +22,8 @@ use std::collections::{BTreeMap, HashSet};
|
||||
use ethereum_types::Address;
|
||||
use version::version_data;
|
||||
|
||||
use crypto::{DEFAULT_MAC, ecies};
|
||||
use ethkey::{Brain, Generator};
|
||||
use crypto::DEFAULT_MAC;
|
||||
use ethkey::{crypto::ecies, Brain, Generator};
|
||||
use ethstore::random_phrase;
|
||||
use sync::{SyncProvider, ManageNetwork};
|
||||
use ethcore::account_provider::AccountProvider;
|
||||
|
||||
Reference in New Issue
Block a user