xeddsa.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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_POINT, montgomery::MontgomeryPoint, scalar::Scalar,
  22. };
  23. use digest::Digest;
  24. use ed25519_dalek::{PublicKey as Ed25519PublicKey, Signature, Verifier};
  25. use sha2::Sha512;
  26. use x25519_dalek::{PublicKey as X25519PublicKey, StaticSecret as X25519SecretKey};
  27. pub trait XeddsaSigner {
  28. fn xeddsa_sign(&self, msg: &[u8], nonce: &[u8; 64]) -> [u8; 64];
  29. }
  30. pub trait XeddsaVerifier {
  31. fn xeddsa_verify(&self, msg: &[u8], nonce: &[u8; 64]) -> bool;
  32. }
  33. impl XeddsaSigner for X25519SecretKey {
  34. fn xeddsa_sign(&self, msg: &[u8], nonce: &[u8; 64]) -> [u8; 64] {
  35. //
  36. // PREPARATION OF THE KEY MATERIAL
  37. //
  38. // This algorithm to sign data using a Curve25519 keypair has to
  39. // tackle two issues. The first issue is that the conversion of
  40. // a Curve25519 public key to an Ed25519 public key is not unique
  41. // when only having access to the u coordinate of the Curve25519
  42. // public key, which is the case with the serialization format
  43. // commonly used. In fact the conversion is unique by the sign of
  44. // the Ed25519 public key x coordinate. This signing algorithm
  45. // "solves" the problem by modifying the private key so that the
  46. // sign of the resulting Ed25519 public key is always zero.
  47. // x25519-dalek private keys are already clamped, so just compute
  48. // the Ed25519 public key from the Curve25519 private key.
  49. let scalar_k = Scalar::from_bits(self.to_bytes());
  50. let ep = ED25519_BASEPOINT_POINT * scalar_k;
  51. let mut ce = ep.compress();
  52. let sign = ce.0[31] >> 7;
  53. // Set the sign bit to zero after adjusting the private key
  54. ce.0[31] &= 0x7F; // A.s = 0
  55. // Compute the negative secret key
  56. // If the sign bit of the calculated Ed25519 public key is zero,
  57. // the private key doesn't have to be touched. If the sign bit
  58. // is one, the private key has to be inverted prior to using it.
  59. let k = if sign == 1 { -scalar_k } else { scalar_k };
  60. //
  61. // SIGNING
  62. //
  63. // The second problem this algorithm has to tackle is that
  64. // Ed25519 signature algorithms don't use the private scalar
  65. // directly, but rather use a seed to derive other data from.
  66. // To create signatures compatible with Ed25519, a modified
  67. // version of the signing algorithm is required that does not
  68. // depend on a seed.
  69. // r = hash1(a || M || Z) (mod q)
  70. let mut hash_padding = [0xff, 32];
  71. hash_padding[0] = 0xfe;
  72. let mut hasher = Sha512::new();
  73. hasher.update(hash_padding);
  74. hasher.update(k.as_bytes());
  75. hasher.update(msg);
  76. hasher.update(nonce.as_ref());
  77. let r = Scalar::from_hash(hasher);
  78. // R = rB
  79. let cap_r = (ED25519_BASEPOINT_POINT * r).compress();
  80. // h = hash(R || A || M) (mod q)
  81. hasher = Sha512::new();
  82. hasher.update(cap_r.as_bytes());
  83. hasher.update(ce.as_bytes());
  84. hasher.update(msg);
  85. let h = Scalar::from_hash(hasher);
  86. // s = r + ha (mod q)
  87. let s = r + h * k;
  88. // return R || s
  89. let mut sig = [0u8; 64];
  90. sig[..32].copy_from_slice(cap_r.as_bytes());
  91. sig[32..].copy_from_slice(s.as_bytes());
  92. sig
  93. }
  94. }
  95. impl XeddsaVerifier for X25519PublicKey {
  96. fn xeddsa_verify(&self, msg: &[u8], sig: &[u8; 64]) -> bool {
  97. let pt = MontgomeryPoint(self.to_bytes());
  98. if let Some(edwards) = pt.to_edwards(0) {
  99. if let Ok(pk) = Ed25519PublicKey::from_bytes(&edwards.compress().to_bytes()) {
  100. let sig = Signature::from_bytes(sig).unwrap();
  101. return pk.verify(msg, &sig).is_ok()
  102. }
  103. }
  104. false
  105. }
  106. }
  107. #[cfg(test)]
  108. mod tests {
  109. use super::*;
  110. #[test]
  111. fn xeddsa_test() {
  112. let nonce = [0u8; 64];
  113. let msg = [0u8; 200];
  114. let mut privkey = [0u8; 32];
  115. privkey[8] = 189;
  116. let xsecret_key = X25519SecretKey::from(privkey);
  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. }