Просмотр исходного кода

bin/dao: use chacha20poly1305 crate in dao binary too

Dastan-glitch 3 лет назад
Родитель
Сommit
452212590c
3 измененных файлов с 23 добавлено и 33 удалено
  1. 1 16
      Cargo.lock
  2. 1 1
      bin/dao/daod/Cargo.toml
  3. 21 16
      bin/dao/daod/src/note.rs

+ 1 - 16
Cargo.lock

@@ -1007,21 +1007,6 @@ dependencies = [
  "typenum",
 ]
 
-[[package]]
-name = "crypto_api"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2f855e87e75a4799e18b8529178adcde6fd4f97c1449ff4821e747ff728bb102"
-
-[[package]]
-name = "crypto_api_chachapoly"
-version = "0.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b63ae1025a6981f91b70bdcf11827189f49b01aaa3720115b330cd325d1c3809"
-dependencies = [
- "crypto_api",
-]
-
 [[package]]
 name = "crypto_box"
 version = "0.8.1"
@@ -1171,7 +1156,7 @@ dependencies = [
  "async-std",
  "async-trait",
  "bs58",
- "crypto_api_chachapoly",
+ "chacha20poly1305",
  "darkfi",
  "darkfi-sdk",
  "darkfi-serial",

+ 1 - 1
bin/dao/daod/Cargo.toml

@@ -34,7 +34,7 @@ pasta_curves = "0.4.0"
 halo2_gadgets = "0.2.0"
 halo2_proofs = "0.2.0"
 rand = "0.8.5"
-crypto_api_chachapoly = "0.5.0"
+chacha20poly1305 = "0.10.1"
 group = "0.12.0"
 
 # Encoding and parsing

+ 21 - 16
bin/dao/daod/src/note.rs

@@ -16,7 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use crypto_api_chachapoly::ChachaPolyIetf;
+use chacha20poly1305::{AeadInPlace, ChaCha20Poly1305, KeyInit};
 use rand::rngs::OsRng;
 
 use darkfi::{
@@ -38,14 +38,14 @@ pub fn encrypt<T: Encodable>(note: &T, public: &PublicKey) -> Result<EncryptedNo
 
     let mut input = Vec::new();
     note.encode(&mut input)?;
+    let input_len = input.len();
 
-    let mut ciphertext = vec![0; input.len() + AEAD_TAG_SIZE];
-    assert_eq!(
-        ChachaPolyIetf::aead_cipher()
-            .seal_to(&mut ciphertext, &input, &[], key.as_ref(), &[0u8; 12])
-            .unwrap(),
-        input.len() + AEAD_TAG_SIZE
-    );
+    let mut ciphertext = vec![0_u8; input_len + AEAD_TAG_SIZE];
+    ciphertext[..input_len].copy_from_slice(&input);
+
+    ChaCha20Poly1305::new(key.as_ref().into())
+        .encrypt_in_place([0u8; 12][..].into(), &[], &mut ciphertext)
+        .unwrap();
 
     Ok(EncryptedNote2 { ciphertext, ephem_public })
 }
@@ -61,15 +61,20 @@ impl EncryptedNote2 {
         let shared_secret = sapling_ka_agree(secret, &self.ephem_public);
         let key = kdf_sapling(&shared_secret, &self.ephem_public);
 
-        let mut plaintext = vec![0; self.ciphertext.len()];
-        assert_eq!(
-            ChachaPolyIetf::aead_cipher()
-                .open_to(&mut plaintext, &self.ciphertext, &[], key.as_ref(), &[0u8; 12])
-                .map_err(|e| Error::NoteDecryptionFailed(e.to_string()))?,
-            self.ciphertext.len() - AEAD_TAG_SIZE
-        );
+        let ciphertext_len = self.ciphertext.len();
+        let mut plaintext = vec![0_u8; ciphertext_len];
+        plaintext.copy_from_slice(&self.ciphertext);
 
-        T::decode(&plaintext[..]).map_err(Error::from)
+        match ChaCha20Poly1305::new(key.as_ref().into()).decrypt_in_place(
+            [0u8; 12][..].into(),
+            &[],
+            &mut plaintext,
+        ) {
+            Ok(()) => {
+                Ok(T::decode(&plaintext[..ciphertext_len - AEAD_TAG_SIZE]).map_err(Error::from)?)
+            }
+            Err(e) => Err(Error::NoteDecryptionFailed(e.to_string())),
+        }
     }
 }