Răsfoiți Sursa

x3dh: Use local serialization for MessageHeader.

Luther Blissett 3 ani în urmă
părinte
comite
57cea9928d
2 a modificat fișierele cu 20 adăugiri și 6 ștergeri
  1. 0 1
      script/research/x3dh/Cargo.toml
  2. 20 5
      script/research/x3dh/src/main.rs

+ 0 - 1
script/research/x3dh/Cargo.toml

@@ -14,4 +14,3 @@ aes-gcm-siv = "0.11.1"
 curve25519-dalek = "3.2.1"
 ed25519-dalek = "1.0.1"
 x25519-dalek = "1.2.0"
-darkfi-serial = {path = "../../../src/serial", features = ["derive", "x25519-dalek"]}

+ 20 - 5
script/research/x3dh/src/main.rs

@@ -4,7 +4,6 @@ use std::collections::{HashMap, VecDeque};
 
 use aes_gcm_siv::{AeadInPlace, Aes256GcmSiv, KeyInit};
 use anyhow::Result;
-use darkfi_serial::{serialize, SerialDecodable, SerialEncodable};
 use digest::Update;
 use rand::rngs::OsRng;
 use sha2::Sha256;
@@ -81,7 +80,7 @@ struct InitialMessage {
     pub ciphertext: Vec<u8>,
 }
 
-#[derive(Copy, Clone, SerialEncodable, SerialDecodable)]
+#[derive(Copy, Clone)]
 struct MessageHeader {
     /// Ratchet public key
     dh: X25519PublicKey,
@@ -97,6 +96,22 @@ impl MessageHeader {
     pub fn new(dh: X25519PublicKey, pn: u64, n: u64) -> Self {
         Self { dh, pn, n }
     }
+
+    pub fn to_bytes(&self) -> [u8; 48] {
+        let mut ret = [0u8; 48];
+        ret[..32].copy_from_slice(&self.dh.to_bytes());
+        ret[32..40].copy_from_slice(&self.pn.to_le_bytes());
+        ret[40..].copy_from_slice(&self.pn.to_le_bytes());
+        ret
+    }
+
+    pub fn from_bytes(arr: [u8; 48]) -> Self {
+        let pk_bytes: [u8; 32] = arr[..32].try_into().unwrap();
+        let dh = X25519PublicKey::from(pk_bytes);
+        let pn = u64::from_le_bytes(arr[32..40].try_into().unwrap());
+        let n = u64::from_le_bytes(arr[40..].try_into().unwrap());
+        Self { dh, pn, n }
+    }
 }
 
 #[derive(Clone)]
@@ -172,7 +187,7 @@ impl DoubleRatchetSessionState {
 
         let mut associated_data = Vec::with_capacity(ad.len());
         associated_data.extend_from_slice(ad);
-        associated_data.extend_from_slice(&serialize(&header));
+        associated_data.extend_from_slice(&header.to_bytes());
 
         let mut ciphertext = vec![0u8; plaintext.len() + AEAD_TAG_SIZE];
         ciphertext[..plaintext.len()].copy_from_slice(plaintext);
@@ -231,7 +246,7 @@ impl DoubleRatchetSessionState {
         let mut plaintext = vec![0u8; ciphertext.len()];
         plaintext.copy_from_slice(ciphertext);
 
-        let header_bytes = serialize(&header);
+        let header_bytes = header.to_bytes();
         let mut associated_data = Vec::with_capacity(ad.len() + header_bytes.len());
         associated_data.extend_from_slice(ad);
         associated_data.extend_from_slice(&header_bytes);
@@ -255,7 +270,7 @@ impl DoubleRatchetSessionState {
             let mut plaintext = vec![0u8; ciphertext.len()];
             plaintext.copy_from_slice(ciphertext);
 
-            let header_bytes = serialize(&header);
+            let header_bytes = header.to_bytes();
             let mut associated_data = Vec::with_capacity(ad.len() + header_bytes.len());
             associated_data.extend_from_slice(ad);
             associated_data.extend_from_slice(&header_bytes);