xeddsa.rs 4.3 KB

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