diff --git a/.vscode/settings.json b/.vscode/settings.json index e81e605..0de10f5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,7 +4,6 @@ "rust-analyzer.cargo.features": ["pure"], "rust-analyzer.cargo.noDefaultFeatures": true, "rust-analyzer.procMacro.enable": true, - "rust-analyzer.cargo.loadOutDirsFromCheck": true, "spellright.ignoreFiles": [ "~/.cargo/", "~/.rustup/", diff --git a/.vscode/spellright.dict b/.vscode/spellright.dict index 6be03f8..bc69730 100644 --- a/.vscode/spellright.dict +++ b/.vscode/spellright.dict @@ -26,3 +26,5 @@ typenum rs ecc rt +js +getrandom diff --git a/Cargo.toml b/Cargo.toml index 80e9d7b..5a200e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.2.1" # docs authors = ["Weiliang Li "] description = "Elliptic Curve Integrated Encryption Scheme for secp256k1 in Rust" -edition = "2018" +edition = "2021" keywords = [ "secp256k1", "crypto", @@ -21,8 +21,8 @@ repository = "https://github.com/ecies/rs" [dependencies] hkdf = "0.11.0" -secp256k1 = { package = "libsecp256k1", version = "0.6" } -sha2 = "0.9.2" +libsecp256k1 = "0.7.0" +sha2 = "0.9.8" # openssl aes openssl = {version = "0.10.32", optional = true} @@ -32,10 +32,11 @@ aes-gcm = {version = "0.9.0", optional = true} typenum = {version = "1.12.0", optional = true} [target.'cfg(target_arch = "wasm32")'.dependencies] -rand = {version = "0.7.3", features = ["wasm-bindgen"]} +getrandom = {version = "0.2.3", features = ["js"]} +rand = {version = "0.8.4", features = ["getrandom"]} [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -rand = {version = "0.7.3"} +rand = {version = "0.8.4"} [features] default = ["openssl"] @@ -46,12 +47,12 @@ criterion = "0.3.3" hex = "0.4.2" [target.'cfg(target_arch = "wasm32")'.dev-dependencies] -wasm-bindgen-test = "0.3.19" +wasm-bindgen-test = "0.3.28" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] -futures-util = "0.3.8" -reqwest = "0.11.0" -tokio = {version = "1.0.1", features = ["rt-multi-thread"]} +futures-util = "0.3.17" +reqwest = "0.11.6" +tokio = {version = "1.13.0", features = ["rt-multi-thread"]} [[bench]] harness = false diff --git a/src/lib.rs b/src/lib.rs index 57be6d9..ec99031 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,7 +50,7 @@ //! //! It's also possible to build to the `wasm32-unknown-unknown` target with the pure Rust backend. Check out [this repo](https://github.com/ecies/rs-wasm) for more details. -pub use secp256k1::{util::FULL_PUBLIC_KEY_SIZE, Error as SecpError, PublicKey, SecretKey}; +pub use libsecp256k1::{util::FULL_PUBLIC_KEY_SIZE, Error as SecpError, PublicKey, SecretKey}; /// Constant variables pub mod consts; diff --git a/src/openssl_aes.rs b/src/openssl_aes.rs index 16fb0d4..e40b0e1 100644 --- a/src/openssl_aes.rs +++ b/src/openssl_aes.rs @@ -36,5 +36,5 @@ pub fn aes_decrypt(key: &[u8], encrypted_msg: &[u8]) -> Option> { let tag = &encrypted_msg[AES_IV_LENGTH..AES_IV_PLUS_TAG_LENGTH]; let encrypted = &encrypted_msg[AES_IV_PLUS_TAG_LENGTH..]; - decrypt_aead(cipher, key, Some(&iv), &EMPTY_BYTES, encrypted, tag).ok() + decrypt_aead(cipher, key, Some(iv), &EMPTY_BYTES, encrypted, tag).ok() } diff --git a/src/utils.rs b/src/utils.rs index 17d9604..20e53e5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,6 @@ use hkdf::Hkdf; +use libsecp256k1::{util::FULL_PUBLIC_KEY_SIZE, Error as SecpError, PublicKey, SecretKey}; use rand::thread_rng; -use secp256k1::{util::FULL_PUBLIC_KEY_SIZE, Error as SecpError, PublicKey, SecretKey}; use sha2::Sha256; use crate::consts::EMPTY_BYTES; @@ -15,16 +15,16 @@ pub use crate::openssl_aes::{aes_decrypt, aes_encrypt}; /// Generate a `(SecretKey, PublicKey)` pair pub fn generate_keypair() -> (SecretKey, PublicKey) { let sk = SecretKey::random(&mut thread_rng()); - (sk.clone(), PublicKey::from_secret_key(&sk)) + (sk, PublicKey::from_secret_key(&sk)) } /// Calculate a shared AES key of our secret key and peer's public key by hkdf pub fn encapsulate(sk: &SecretKey, peer_pk: &PublicKey) -> Result { - let mut shared_point = peer_pk.clone(); - shared_point.tweak_mul_assign(&sk)?; + let mut shared_point = *peer_pk; + shared_point.tweak_mul_assign(sk)?; let mut master = Vec::with_capacity(FULL_PUBLIC_KEY_SIZE * 2); - master.extend(PublicKey::from_secret_key(&sk).serialize().iter()); + master.extend(PublicKey::from_secret_key(sk).serialize().iter()); master.extend(shared_point.serialize().iter()); hkdf_sha256(master.as_slice()) @@ -32,8 +32,8 @@ pub fn encapsulate(sk: &SecretKey, peer_pk: &PublicKey) -> Result Result { - let mut shared_point = pk.clone(); - shared_point.tweak_mul_assign(&peer_sk)?; + let mut shared_point = *pk; + shared_point.tweak_mul_assign(peer_sk)?; let mut master = Vec::with_capacity(FULL_PUBLIC_KEY_SIZE * 2); master.extend(pk.serialize().iter()); @@ -55,8 +55,8 @@ fn hkdf_sha256(master: &[u8]) -> Result { pub(crate) mod tests { use hex::decode; + use libsecp256k1::Error; use rand::{thread_rng, Rng}; - use secp256k1::Error; use super::*; use crate::consts::{AES_IV_LENGTH, EMPTY_BYTES};