From fb6462aa07afd8c83528fd7145d52309844fd01f Mon Sep 17 00:00:00 2001 From: Muhamad Azamy Date: Thu, 21 Sep 2023 22:41:59 +0200 Subject: [PATCH] use blake2 --- Cargo.lock | 42 ++++++------------------------------------ Cargo.toml | 4 +--- src/store/bs.rs | 22 +++++++++------------- 3 files changed, 16 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5112b0a..592871a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,18 +109,6 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" -[[package]] -name = "arrayref" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" - -[[package]] -name = "arrayvec" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" - [[package]] name = "assert_cmd" version = "2.0.11" @@ -227,14 +215,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "blake2s_simd" -version = "1.0.2" +name = "blake2" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94230421e395b9920d23df13ea5d77a20e1725331f90fbbf6df6040b33f756ae" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" dependencies = [ - "arrayref", - "arrayvec", - "constant_time_eq", + "digest", ] [[package]] @@ -386,12 +372,6 @@ dependencies = [ "tokio-util", ] -[[package]] -name = "constant_time_eq" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" - [[package]] name = "core-foundation" version = "0.9.3" @@ -503,6 +483,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -1543,7 +1524,7 @@ dependencies = [ "async-recursion", "async-trait", "bb8-redis", - "blake2s_simd", + "blake2", "bytes", "capnp", "capnpc", @@ -1562,7 +1543,6 @@ dependencies = [ "reqwest", "serde", "serde_json", - "sha2", "simple_logger", "snap", "sqlx", @@ -1723,16 +1703,6 @@ dependencies = [ "cfg-if", "cpufeatures", "digest", - "sha2-asm", -] - -[[package]] -name = "sha2-asm" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27ba7066011e3fb30d808b51affff34f0a66d3a03a58edd787c6e420e40e44e" -dependencies = [ - "cc", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9f42b9a..3bfd14c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,9 +50,7 @@ async-trait = "0.1.53" url = "2.3.1" serde = {version = "1", features = ["derive"] } serde_json = "1.0" -sha2 = {version = "0.10", features = ["asm"] } -# blake2 = {version = "0.10"} -blake2s_simd = "1.0" +blake2 = {version = "0.10"} aes-gcm = "0.10" hex = "0.4" lazy_static = "1.4" diff --git a/src/store/bs.rs b/src/store/bs.rs index 2e5b644..2b6f06e 100644 --- a/src/store/bs.rs +++ b/src/store/bs.rs @@ -1,12 +1,11 @@ use super::{Error, Result, Store}; use crate::fungi::meta::Block; use aes_gcm::{ - aead::{generic_array::GenericArray, Aead, KeyInit}, + aead::{Aead, KeyInit}, Aes256Gcm, Nonce, }; -//use blake2::{Blake2s256, Digest}; - -//type Hasher = Blake2s256; +use blake2::{Blake2s256, Digest}; +type Hasher = Blake2s256; /// The block store builds on top of a store and adds encryption and compression #[derive(Clone, Debug)] @@ -50,19 +49,16 @@ where pub async fn set(&self, blob: &[u8]) -> Result { // we first calculate the hash of the plain-text data - let key = blake2s_simd::blake2s(blob); - - let enc_key = GenericArray::from_slice(key.as_array()); - //let key = Hasher::digest(blob); + let key = Hasher::digest(blob); let mut encoder = snap::raw::Encoder::new(); // data is then compressed let compressed = encoder.compress_vec(blob)?; // we then encrypt it using the hash of the plain-text as a key - let cipher = Aes256Gcm::new(enc_key); + let cipher = Aes256Gcm::new(&key); // the nonce is still driven from the key, a nonce is 12 bytes for aes // it's done like this so a store can still dedup the data - let nonce = Nonce::from_slice(&key.as_bytes()[..12]); + let nonce = Nonce::from_slice(&key[..12]); // we encrypt the data let encrypted = cipher @@ -70,11 +66,11 @@ where .map_err(|_| Error::EncryptionError)?; // we hash it again, and use that as the store key - let id = blake2s_simd::blake2s(&encrypted); + let id = Hasher::digest(&encrypted); let block = Block { - id: *id.as_array(), - key: *key.as_array(), + id: id.into(), + key: key.into(), }; self.store.set(&block.id, &encrypted).await?;