diff --git a/Cargo.lock b/Cargo.lock index 527809f..950ae45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,6 +96,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + [[package]] name = "aes" version = "0.8.4" @@ -107,6 +117,20 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "ahash" version = "0.8.8" @@ -1640,7 +1664,7 @@ dependencies = [ "bech32 0.10.0-beta", "bitcoin-internals", "bitcoin_hashes 0.13.0", - "hex-conservative", + "hex-conservative 0.1.1", "hex_lit", "secp256k1 0.28.2", ] @@ -1651,6 +1675,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9425c3bf7089c983facbae04de54513cce73b41c7f9ff8c845b54e7bc64ebbfb" +[[package]] +name = "bitcoin-io" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "340e09e8399c7bd8912f495af6aa58bea0c9214773417ffaa8f6460f93aaee56" + [[package]] name = "bitcoin_hashes" version = "0.11.0" @@ -1664,7 +1694,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" dependencies = [ "bitcoin-internals", - "hex-conservative", + "hex-conservative 0.1.1", +] + +[[package]] +name = "bitcoin_hashes" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16" +dependencies = [ + "bitcoin-io", + "hex-conservative 0.2.1", ] [[package]] @@ -2410,6 +2450,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] @@ -3490,6 +3531,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gilrs" version = "0.10.4" @@ -3835,6 +3886,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2" +[[package]] +name = "hex-conservative" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd" +dependencies = [ + "arrayvec", +] + [[package]] name = "hex-literal" version = "0.4.1" @@ -5044,6 +5104,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "open-fastrlp" version = "0.1.4" @@ -5420,6 +5486,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -7095,9 +7173,11 @@ dependencies = [ name = "tool" version = "0.2.2" dependencies = [ + "aes-gcm", "anyhow", "bip32", "bip39", + "bitcoin_hashes 0.14.0", "bs58", "bytes", "clap 4.5.1", @@ -7110,10 +7190,11 @@ dependencies = [ "num-traits", "rand 0.8.5", "regex", - "rust-crypto", "secp256k1 0.27.0", "serde", "serde_json", + "sha2", + "sha3", "tokio", "tracing", "tracing-subscriber 0.3.18", @@ -7384,6 +7465,16 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common", + "subtle", +] + [[package]] name = "untrusted" version = "0.7.1" diff --git a/tool/Cargo.toml b/tool/Cargo.toml index fb4e541..63dfb65 100644 --- a/tool/Cargo.toml +++ b/tool/Cargo.toml @@ -18,7 +18,6 @@ tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } clap = { version = "4.3", features = ["derive"] } anyhow = "1.0" -rust-crypto = "0.2.36" secp256k1 = { version = "0.27.0", default-features = false, features = [ "global-context", "rand-std", @@ -39,3 +38,8 @@ csv = "1.3.0" regex = "1.10.3" num-traits = "0.2.18" num-bigint = "0.4.4" +sha3 = "0.10.8" +sha2 = "0.10.8" +aes-gcm = "0.10.3" +bitcoin_hashes = "0.14.0" + diff --git a/tool/src/btc.rs b/tool/src/btc.rs index 96c651d..479e3c6 100644 --- a/tool/src/btc.rs +++ b/tool/src/btc.rs @@ -2,10 +2,10 @@ use crate::bip32::{derive_private_by_path, derive_public_by_path, mnemonic_to_x_ use crate::eth::get_public_key; use anyhow::Result; use bip32::{Prefix, PublicKey as Bip32PubKey}; +use bitcoin_hashes::{ripemd160, Hash}; use bs58::{decode, encode}; -use crypto::digest::Digest; -use crypto::{ripemd160, sha2::Sha256}; use secp256k1::{PublicKey, Secp256k1, SecretKey}; +use sha2::{Digest, Sha256}; use std::str::FromStr; use tracing::info; @@ -21,20 +21,18 @@ const OP_CHECKSIG: u8 = 0xac; // 先实现最基础的P2PKH和P2WPKH,其他的多签和script的类型暂不考虑,后续考虑优化,这里也不过多依赖第三方库 fn pub_key_to_address(public_key: PublicKey) -> String { // sha256 - let mut sha256 = Box::new(Sha256::new()); + let mut sha256 = Sha256::new(); // 压缩型的地址 - sha256.input(&public_key.serialize()[..]); - let mut out_1 = vec![0u8; sha256.output_bytes()]; - sha256.result(&mut out_1); + sha256.update(&public_key.serialize()[..]); + let out_1 = sha256.finalize().to_vec(); // ripemd160 - let mut hash = Box::new(ripemd160::Ripemd160::new()); - hash.input(&out_1); - let mut out = vec![0u8; hash.output_bytes()]; - hash.result(&mut out); + let mut hash = ripemd160::Hash::hash(&out_1); + let out_bytes: &[u8] = hash.as_ref(); + let mut out: Vec = out_bytes.to_vec(); // P2PKH let p2pkh = [ - &[OP_DUP, OP_HASH160, out.len() as u8][..], - &out[..], + &[OP_DUP, OP_HASH160, 20][..], + &out, &[OP_EQUALVERIFY, OP_CHECKSIG], ] .concat(); @@ -84,14 +82,12 @@ pub fn get_tx_hash(raw_tx: String) -> Result<()> { } fn double_sha256(input: &[u8]) -> Vec { - let mut sha256 = Box::new(Sha256::new()); - sha256.input(&input[..]); - let mut out = vec![0u8; sha256.output_bytes()]; - sha256.result(&mut out); - let mut sha256 = Box::new(Sha256::new()); - sha256.input(&out); - let mut result = vec![0u8; sha256.output_bytes()]; - sha256.result(&mut result); + let mut sha256 = Sha256::new(); + sha256.update(&input[..]); + let out = sha256.finalize().to_vec(); + let mut sha256 = Sha256::new(); + sha256.update(&out); + let result = sha256.finalize().to_vec(); result } diff --git a/tool/src/encrypt_decrypt.rs b/tool/src/encrypt_decrypt.rs index 374677c..e3a6d8e 100644 --- a/tool/src/encrypt_decrypt.rs +++ b/tool/src/encrypt_decrypt.rs @@ -1,11 +1,10 @@ -use crypto::aead::{AeadDecryptor, AeadEncryptor}; -use crypto::{ - aes::KeySize::KeySize256, - aes_gcm::AesGcm, - digest::Digest, - sha3::{Sha3, Sha3Mode}, +use aes_gcm::aead::{AeadMut, Buffer}; +use aes_gcm::{ + aead::{Aead, AeadCore, OsRng}, + Aes256Gcm, AesGcm, Key, KeyInit, Nonce, }; use rand::prelude::*; +use sha3::{Digest, Keccak256}; use std::iter::repeat; use std::{fmt::Write, string::String}; use tracing::info; @@ -18,10 +17,9 @@ use crate::util::{hex_string_2_array, u8_array_convert_string}; /// fn generate_key(password: String) -> Vec { - let mut sh = Box::new(Sha3::new(Sha3Mode::Keccak256)); - sh.input(&password.as_bytes()); - let mut out = vec![0u8; sh.output_bytes()]; - sh.result(&mut out); + let mut sh = Keccak256::new(); + sh.update(&password.as_bytes()); + let out = sh.finalize().to_vec(); out } @@ -29,20 +27,26 @@ pub fn encrypt(plaintext: String, password: String) -> anyhow::Result<()> { info!("plaintext: {plaintext}"); let key = generate_key(password); let mut rng = rand::thread_rng(); - let mut iv = [0u8; 12]; - rng.fill_bytes(&mut iv); let mut aad = [0u8; 16]; rng.fill_bytes(&mut aad); - let mut aes_gcm = AesGcm::new(KeySize256, &key[..], &iv, &aad); + let aes_key = Key::::from_slice(key.as_slice()); + let mut aes_gcm = Aes256Gcm::new(&aes_key); let plaintext_array = plaintext.as_bytes(); - let mut out: Vec = repeat(0) - .take((plaintext_array.len() as u64).try_into().unwrap()) - .collect(); - let mut out_tag: Vec = repeat(0).take(16).collect(); - aes_gcm.encrypt(&plaintext_array, &mut out[..], &mut out_tag); - let out_str: String = u8_array_convert_string(&out); - let iv_str = u8_array_convert_string(&iv); - let tag_str = u8_array_convert_string(&out_tag); + let nonce = Aes256Gcm::generate_nonce(OsRng); + let mut out = aes_gcm + .encrypt( + &nonce, + aes_gcm::aead::Payload { + msg: plaintext_array, + aad: aad.as_slice(), + }, + ) + .unwrap(); + println!("out: {:?}", out); + let index = out.len() - 16; + let out_str: String = u8_array_convert_string(&out[..index]); + let iv_str = u8_array_convert_string(&nonce.as_slice()); + let tag_str = u8_array_convert_string(&out[index..]); let aad_str = u8_array_convert_string(&aad); info!( "cipher: {:?}\n{:?}, \niv: {:?}\n tag:{:?}, aav: {:?}", @@ -60,17 +64,27 @@ pub fn decrypt( ) -> anyhow::Result<()> { info!("cipher: {cipher}"); let key = generate_key(password); - let cipher_arr = hex_string_2_array(&cipher); - let iv = hex_string_2_array(&iv); + let mut cipher_arr = hex_string_2_array(&cipher); + let mut nonce_arr = hex_string_2_array(&iv); let tag = hex_string_2_array(&tag); let aad = hex_string_2_array(&aad); - let mut aes_gcm = AesGcm::new(KeySize256, &key[..], &iv, &aad); - let mut out: Vec = repeat(0).take(cipher_arr.len()).collect(); - let result = aes_gcm.decrypt(&cipher_arr, &mut out, &tag); - let plaintext = String::from_utf8(out.clone()).unwrap(); + let aes_key = Key::::from_slice(key.as_slice()); + let mut aes_gcm = Aes256Gcm::new(&aes_key); + cipher_arr.extend_from_slice(&tag); + let result = aes_gcm + .decrypt( + &Nonce::from_slice(nonce_arr.as_slice()), + aes_gcm::aead::Payload { + msg: cipher_arr.as_slice(), + aad: &aad, + }, + ) + .unwrap(); + //let result = aes_gcm.decrypt(&cipher_arr, &mut out, &tag); + let plaintext = String::from_utf8(result.clone()).unwrap(); println!( - "decrypt success: {:?}, result: {:?}, plaintext: {:?}", - result, out, plaintext + "decrypt success, result: {:?}, plaintext: {:?}", + result, plaintext ); Ok(()) } diff --git a/tool/src/eth.rs b/tool/src/eth.rs index 26305d8..fca9ae0 100644 --- a/tool/src/eth.rs +++ b/tool/src/eth.rs @@ -6,8 +6,6 @@ use bip32::secp256k1::elliptic_curve::weierstrass::add; use bip32::{Prefix, PublicKey as Bip32PubKey}; use bytes::Buf; use clap::builder::Str; -use crypto::digest::Digest; -use crypto::sha3::Sha3; use ethers::abi::{parse_abi_str, AbiEncode}; use ethers::types::transaction::eip2718::TypedTransaction; use ethers::utils::hex::ToHex; @@ -23,6 +21,7 @@ use num_traits::Num; use regex::Regex; use secp256k1::{PublicKey, Secp256k1, SecretKey}; use serde_json::json; +use sha3::Digest; use std::collections::HashMap; use std::f64; use std::io::{Cursor, Read}; @@ -275,10 +274,9 @@ pub fn get_public_key(private_key: &str) -> PublicKey { } fn pub_key_to_address(public_key: PublicKey) -> String { - let mut hash = Box::new(Sha3::keccak256()); - hash.input(&public_key.serialize_uncompressed()[1..]); - let mut out = vec![0u8; hash.output_bytes()]; - hash.result(&mut out); + let mut hasher = sha3::Keccak256::new(); + hasher.update(&public_key.serialize_uncompressed()[1..]); + let out = hasher.finalize().to_vec(); // 取后20个bytes作为地址 let addr = u8_array_convert_string(&out[12..]); let mut address = "0x".to_string();