xeddsa.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Taken from https://docs.rs/ockam_vault/latest/src/ockam_vault/xeddsa.rs.html
  19. //! XEdDSA according to <https://signal.org/docs/specifications/xeddsa/#xeddsa>
  20. use curve25519_dalek::{
  21. constants::ED25519_BASEPOINT_TABLE,
  22. montgomery::MontgomeryPoint,
  23. scalar::{clamp_integer, Scalar},
  24. };
  25. use digest::Digest;
  26. use ed25519_dalek::{Signature, VerifyingKey as Ed25519PublicKey};
  27. use sha2::Sha512;
  28. use x25519_dalek::{PublicKey as X25519PublicKey, StaticSecret as X25519SecretKey};
  29. pub trait XeddsaSigner {
  30. fn xeddsa_sign(&self, msg: &[u8], nonce: &[u8; 64]) -> [u8; 64];
  31. }
  32. pub trait XeddsaVerifier {
  33. fn xeddsa_verify(&self, msg: &[u8], nonce: &[u8; 64]) -> bool;
  34. }
  35. impl XeddsaSigner for X25519SecretKey {
  36. fn xeddsa_sign(&self, msg: &[u8], nonce: &[u8; 64]) -> [u8; 64] {
  37. //
  38. // PREPARATION OF THE KEY MATERIAL
  39. //
  40. // This algorithm to sign data using a Curve25519 keypair has to
  41. // tackle two issues. The first issue is that the conversion of
  42. // a Curve25519 public key to an Ed25519 public key is not unique
  43. // when only having access to the u coordinate of the Curve25519
  44. // public key, which is the case with the serialization format
  45. // commonly used. In fact the conversion is unique by the sign of
  46. // the Ed25519 public key x coordinate. This signing algorithm
  47. // "solves" the problem by modifying the private key so that the
  48. // sign of the resulting Ed25519 public key is always zero.
  49. // x25519-dalek private keys are already clamped, so just compute
  50. // the Ed25519 public key from the Curve25519 private key.
  51. let scalar_k = Scalar::from_bytes_mod_order(clamp_integer(self.to_bytes()));
  52. let edward_point = ED25519_BASEPOINT_TABLE * &scalar_k;
  53. let mut compressed_edwards = edward_point.compress();
  54. let sign = compressed_edwards.0[31] >> 7;
  55. // Set the sign bit to zero after adjusting the private key
  56. compressed_edwards.0[31] &= 0x7F; // A.s = 0
  57. // Compute the negative secret key
  58. // If the sign bit of the calculated Ed25519 public key is zero,
  59. // the private key doesn't have to be touched. If the sign bit
  60. // is one, the private key has to be inverted prior to using it.
  61. let k = if sign == 1 { -scalar_k } else { scalar_k };
  62. //
  63. // SIGNING
  64. //
  65. // The second problem this algorithm has to tackle is that
  66. // Ed25519 signature algorithms don't use the private scalar
  67. // directly, but rather use a seed to derive other data from.
  68. // To create signatures compatible with Ed25519, a modified
  69. // version of the signing algorithm is required that does not
  70. // depend on a seed.
  71. // r = hash1(a || M || Z) (mod q)
  72. let mut hash_padding = [0xff, 32];
  73. hash_padding[0] = 0xfe;
  74. let mut hasher = Sha512::new();
  75. hasher.update(hash_padding);
  76. hasher.update(k.as_bytes());
  77. hasher.update(msg);
  78. hasher.update(nonce.as_ref());
  79. let r = Scalar::from_hash(hasher);
  80. // R = rB
  81. let cap_r = (ED25519_BASEPOINT_TABLE * &r).compress();
  82. // h = hash(R || A || M) (mod q)
  83. hasher = Sha512::new();
  84. hasher.update(cap_r.as_bytes());
  85. hasher.update(compressed_edwards.as_bytes());
  86. hasher.update(msg);
  87. let h = Scalar::from_hash(hasher);
  88. // s = r + ha (mod q)
  89. let s = r + h * k;
  90. // return R || s
  91. let mut sig = [0u8; 64];
  92. sig[..32].copy_from_slice(cap_r.as_bytes());
  93. sig[32..].copy_from_slice(s.as_bytes());
  94. sig
  95. }
  96. }
  97. impl XeddsaVerifier for X25519PublicKey {
  98. fn xeddsa_verify(&self, msg: &[u8], sig: &[u8; 64]) -> bool {
  99. let pt = MontgomeryPoint(self.to_bytes());
  100. if let Some(edwards) = pt.to_edwards(0) {
  101. let pk = Ed25519PublicKey::from_bytes(&edwards.compress().to_bytes()).unwrap();
  102. let signature = Signature::from_bytes(sig);
  103. return pk.verify_strict(msg, &signature).is_ok()
  104. }
  105. false
  106. }
  107. }
  108. #[cfg(test)]
  109. mod tests {
  110. use super::*;
  111. use rand::rngs::OsRng;
  112. #[test]
  113. fn xeddsa_test() {
  114. let nonce = [0u8; 64];
  115. let msg = [0u8; 200];
  116. let xsecret_key = X25519SecretKey::new(&mut OsRng);
  117. let xpublic_key = X25519PublicKey::from(&xsecret_key);
  118. let sig = xsecret_key.xeddsa_sign(&msg, &nonce);
  119. assert!(xpublic_key.xeddsa_verify(&msg, &sig));
  120. }
  121. }