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

Fix x3dh sig (#158)

* rename local variable sig to signature to avoid confusion with the function param

* x3dh/src/main: removed prints and renames variable

* faucetd/src/main: debugging

* remove fix tags

* rename vars

* src/xeddsa.rs: fix import and switched to verify_strict
Sombra 3 лет назад
Родитель
Сommit
2aaf606741
2 измененных файлов с 18 добавлено и 15 удалено
  1. 7 4
      script/research/x3dh/src/main.rs
  2. 11 11
      script/research/x3dh/src/xeddsa.rs

+ 7 - 4
script/research/x3dh/src/main.rs

@@ -450,13 +450,15 @@ fn main() {
     // at some interval (e.g. once a week/month). The new signed prekey
     // and prekey signature will replace the previous values.
 
+    
     // Bob's signed prekey `SPK_B`
     let bob_spk_secret = X25519SecretKey::new(OsRng);
-    let bob_spk_public = X25519PublicKey::from(&bob_spk_secret);
+    let bob_public_spk = X25519PublicKey::from(&bob_spk_secret);
 
     // Bob's prekey signature `Sig(IK_b, Encode(SPK_B))`
     let nonce = [0_u8; 64];
-    let bob_spk_sig = bob_ik_secret.xeddsa_sign(&bob_spk_public.to_bytes(), &nonce);
+    let bob_spk_signature = bob_ik_secret.xeddsa_sign(&bob_public_spk.to_bytes(), &nonce);
+   
 
     // A set of Bob's one-time prekeys `(OPK_B1, OPK_B2, OPK_B3, ...)`
     let mut bob_opk_secrets =
@@ -467,8 +469,8 @@ fn main() {
     bob_opk_publics.push_back(X25519PublicKey::from(&bob_opk_secrets[2]));
 
     let bob_keyset = Keyset {
-        signed_prekey: bob_spk_public,
-        prekey_signature: bob_spk_sig,
+        signed_prekey: bob_public_spk,
+        prekey_signature: bob_spk_signature,
         onetime_prekeys: bob_opk_publics.clone(),
     };
 
@@ -485,6 +487,7 @@ fn main() {
 
     // Alice verifies the prekey signature and aborts the protocol if
     // verification fails.
+
     assert!(bob_keyset
         .identity_key
         .xeddsa_verify(&bob_keyset.signed_prekey.to_bytes(), &bob_keyset.prekey_signature));

+ 11 - 11
script/research/x3dh/src/xeddsa.rs

@@ -22,8 +22,8 @@ use curve25519_dalek::{
     constants::ED25519_BASEPOINT_POINT, montgomery::MontgomeryPoint, scalar::Scalar,
 };
 use digest::Digest;
-use ed25519_dalek::{Signature, SigningKey as Ed25519PublicKey};
 use sha2::Sha512;
+use ed25519_dalek::{Signature, VerifyingKey as Ed25519PublicKey};
 use x25519_dalek::{PublicKey as X25519PublicKey, StaticSecret as X25519SecretKey};
 
 pub trait XeddsaSigner {
@@ -52,11 +52,11 @@ impl XeddsaSigner for X25519SecretKey {
         // x25519-dalek private keys are already clamped, so just compute
         // the Ed25519 public key from the Curve25519 private key.
         let scalar_k = Scalar::from_bits(self.to_bytes());
-        let ep = ED25519_BASEPOINT_POINT * scalar_k;
-        let mut ce = ep.compress();
-        let sign = ce.0[31] >> 7;
+        let edward_point = ED25519_BASEPOINT_POINT * scalar_k;
+        let mut compressed_edwards = edward_point.compress();
+        let sign = compressed_edwards.0[31] >> 7;
         // Set the sign bit to zero after adjusting the private key
-        ce.0[31] &= 0x7F; // A.s = 0
+        compressed_edwards.0[31] &= 0x7F; // A.s = 0
 
         // Compute the negative secret key
 
@@ -73,7 +73,7 @@ impl XeddsaSigner for X25519SecretKey {
         // directly, but rather use a seed to derive other data from.
         // To create signatures compatible with Ed25519, a modified
         // version of the signing algorithm is required that does not
-        // depend on a seed.
+        // dedward_pointend on a seed.
         // r = hash1(a || M || Z) (mod q)
         let mut hash_padding = [0xff, 32];
         hash_padding[0] = 0xfe;
@@ -90,7 +90,7 @@ impl XeddsaSigner for X25519SecretKey {
         // h = hash(R || A || M) (mod q)
         hasher = Sha512::new();
         hasher.update(cap_r.as_bytes());
-        hasher.update(ce.as_bytes());
+        hasher.update(compressed_edwards.as_bytes());
         hasher.update(msg);
         let h = Scalar::from_hash(hasher);
 
@@ -110,9 +110,9 @@ impl XeddsaVerifier for X25519PublicKey {
         let pt = MontgomeryPoint(self.to_bytes());
 
         if let Some(edwards) = pt.to_edwards(0) {
-            let pk = Ed25519PublicKey::from_bytes(&edwards.compress().to_bytes());
-            let sig = Signature::from_bytes(sig);
-            return pk.verify(msg, &sig).is_ok()
+            let pk = Ed25519PublicKey::from_bytes(&edwards.compress().to_bytes()).unwrap();
+            let signature = Signature::from_bytes(sig);
+            return pk.verify_strict(msg, &signature).is_ok()
         }
 
         false
@@ -128,7 +128,7 @@ mod tests {
     fn xeddsa_test() {
         let nonce = [0u8; 64];
         let msg = [0u8; 200];
-
+ 
         let xsecret_key = X25519SecretKey::new(&mut OsRng);
         let xpublic_key = X25519PublicKey::from(&xsecret_key);