浏览代码

crypto/note: Don't use detached chacha20 API.

Luther Blissett 3 年之前
父节点
当前提交
ef3e12a7bc
共有 1 个文件被更改,包括 7 次插入13 次删除
  1. 7 13
      src/crypto/note.rs

+ 7 - 13
src/crypto/note.rs

@@ -38,14 +38,10 @@ impl Note {
         let mut ciphertext = vec![0_u8; input_len + AEAD_TAG_SIZE];
         ciphertext[..input_len].copy_from_slice(&input);
 
-        let tag = ChaCha20Poly1305::new(key.as_ref().into())
-            .encrypt_in_place_detached([0u8; 12][..].into(), &[], &mut ciphertext[..input_len])
+        ChaCha20Poly1305::new(key.as_ref().into())
+            .encrypt_in_place([0u8; 12][..].into(), &[], &mut ciphertext)
             .unwrap();
 
-        ciphertext[input_len..].copy_from_slice(&tag);
-
-        assert_eq!(input_len + AEAD_TAG_SIZE, ciphertext.len());
-
         Ok(EncryptedNote { ciphertext, ephem_public })
     }
 }
@@ -61,18 +57,16 @@ impl EncryptedNote {
         let shared_secret = sapling_ka_agree(secret, &self.ephem_public);
         let key = kdf_sapling(&shared_secret, &self.ephem_public);
 
-        let output_len = self.ciphertext.len() - AEAD_TAG_SIZE;
-
-        let mut plaintext = vec![0_u8; output_len];
-        plaintext.copy_from_slice(&self.ciphertext[..output_len]);
+        let ciphertext_len = self.ciphertext.len();
+        let mut plaintext = vec![0_u8; ciphertext_len];
+        plaintext.copy_from_slice(&self.ciphertext);
 
-        match ChaCha20Poly1305::new(key.as_ref().into()).decrypt_in_place_detached(
+        match ChaCha20Poly1305::new(key.as_ref().into()).decrypt_in_place(
             [0u8; 12][..].into(),
             &[],
             &mut plaintext,
-            self.ciphertext[output_len..].into(),
         ) {
-            Ok(()) => Ok(Note::decode(&plaintext[..])?),
+            Ok(()) => Ok(Note::decode(&plaintext[..ciphertext_len - AEAD_TAG_SIZE])?),
             Err(e) => Err(Error::NoteDecryptionFailed(e.to_string())),
         }
     }