lib.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*! Implementation of a new style of bridge authority for Tor that
  2. allows users to invite other users, while protecting the social graph
  3. from the bridge authority itself.
  4. We use CMZ14 credentials (GGM version, which is more efficient, but
  5. makes a stronger security assumption): "Algebraic MACs and
  6. Keyed-Verification Anonymous Credentials" (Chase, Meiklejohn, and
  7. Zaverucha, CCS 2014)
  8. The notation follows that of the paper "Hyphae: Social Secret Sharing"
  9. (Lovecruft and de Valence, 2017), Section 4. */
  10. // We really want points to be capital letters and scalars to be
  11. // lowercase letters
  12. #![allow(non_snake_case)]
  13. #[macro_use]
  14. extern crate zkp;
  15. pub mod bridge_table;
  16. pub mod cred;
  17. pub mod dup_filter;
  18. use sha2::Sha512;
  19. use rand::rngs::OsRng;
  20. use rand::RngCore;
  21. use std::convert::{TryFrom, TryInto};
  22. use curve25519_dalek::constants as dalek_constants;
  23. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  24. use curve25519_dalek::ristretto::RistrettoPoint;
  25. use curve25519_dalek::scalar::Scalar;
  26. use ed25519_dalek::{Keypair, PublicKey, Signature, SignatureError, Signer, Verifier};
  27. use lazy_static::lazy_static;
  28. lazy_static! {
  29. pub static ref CMZ_A: RistrettoPoint =
  30. RistrettoPoint::hash_from_bytes::<Sha512>(b"CMZ Generator A");
  31. pub static ref CMZ_B: RistrettoPoint = dalek_constants::RISTRETTO_BASEPOINT_POINT;
  32. pub static ref CMZ_A_TABLE: RistrettoBasepointTable = RistrettoBasepointTable::create(&CMZ_A);
  33. pub static ref CMZ_B_TABLE: RistrettoBasepointTable =
  34. dalek_constants::RISTRETTO_BASEPOINT_TABLE;
  35. }
  36. #[derive(Clone, Debug)]
  37. pub struct IssuerPrivKey {
  38. x0tilde: Scalar,
  39. x: Vec<Scalar>,
  40. }
  41. impl IssuerPrivKey {
  42. /// Create an IssuerPrivKey for credentials with the given number of
  43. /// attributes.
  44. pub fn new(n: u16) -> IssuerPrivKey {
  45. let mut rng = rand::thread_rng();
  46. let x0tilde = Scalar::random(&mut rng);
  47. let mut x: Vec<Scalar> = Vec::with_capacity((n + 1) as usize);
  48. // Set x to a vector of n+1 random Scalars
  49. x.resize_with((n + 1) as usize, || Scalar::random(&mut rng));
  50. IssuerPrivKey { x0tilde, x }
  51. }
  52. }
  53. #[derive(Clone, Debug)]
  54. pub struct IssuerPubKey {
  55. X: Vec<RistrettoPoint>,
  56. }
  57. impl IssuerPubKey {
  58. /// Create an IssuerPubKey from the corresponding IssuerPrivKey
  59. pub fn new(privkey: &IssuerPrivKey) -> IssuerPubKey {
  60. let Atable: &RistrettoBasepointTable = &CMZ_A_TABLE;
  61. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  62. let n_plus_one = privkey.x.len();
  63. let mut X: Vec<RistrettoPoint> = Vec::with_capacity(n_plus_one);
  64. // The first element is a special case; it is
  65. // X[0] = x0tilde*A + x[0]*B
  66. X.push(&privkey.x0tilde * Atable + &privkey.x[0] * Btable);
  67. // The other elements (1 through n) are X[i] = x[i]*A
  68. for i in 1..n_plus_one {
  69. X.push(&privkey.x[i] * Atable);
  70. }
  71. IssuerPubKey { X }
  72. }
  73. }
  74. /// The BridgeDb. This will typically be a singleton object. The
  75. /// BridgeDb's role is simply to issue signed "open invitations" to
  76. /// people who are not yet part of the system.
  77. #[derive(Debug)]
  78. pub struct BridgeDb {
  79. /// The keypair for signing open invitations
  80. keypair: Keypair,
  81. /// The public key for verifying open invitations
  82. pub pubkey: PublicKey,
  83. /// The number of open-invitation buckets
  84. num_openinv_buckets: u32,
  85. }
  86. /// An open invitation is a [u8; OPENINV_LENGTH] where the first 32
  87. /// bytes are the serialization of a random Scalar (the invitation id),
  88. /// the next 4 bytes are a little-endian bucket number, and the last
  89. /// SIGNATURE_LENGTH bytes are the signature on the first 36 bytes.
  90. pub const OPENINV_LENGTH: usize = 32 // the length of the random
  91. // invitation id (a Scalar)
  92. + 4 // the length of the u32 for the bucket number
  93. + ed25519_dalek::SIGNATURE_LENGTH; // the length of the signature
  94. impl BridgeDb {
  95. /// Create the BridgeDb.
  96. pub fn new(num_openinv_buckets: u32) -> Self {
  97. let mut csprng = OsRng {};
  98. let keypair = Keypair::generate(&mut csprng);
  99. let pubkey = keypair.public;
  100. Self {
  101. keypair,
  102. pubkey,
  103. num_openinv_buckets,
  104. }
  105. }
  106. /// Produce an open invitation. In this example code, we just
  107. /// choose a random open-invitation bucket.
  108. pub fn invite(&self) -> [u8; OPENINV_LENGTH] {
  109. let mut res: [u8; OPENINV_LENGTH] = [0; OPENINV_LENGTH];
  110. let mut rng = rand::thread_rng();
  111. // Choose a random invitation id (a Scalar) and serialize it
  112. let id = Scalar::random(&mut rng);
  113. res[0..32].copy_from_slice(&id.to_bytes());
  114. // Choose a random bucket number (mod num_openinv_buckets) and
  115. // serialize it
  116. let bucket_num = rng.next_u32() % self.num_openinv_buckets;
  117. res[32..(32 + 4)].copy_from_slice(&bucket_num.to_le_bytes());
  118. // Sign the first 36 bytes and serialize it
  119. let sig = self.keypair.sign(&res[0..(32 + 4)]);
  120. res[(32 + 4)..].copy_from_slice(&sig.to_bytes());
  121. res
  122. }
  123. /// Verify an open invitation. Returns the invitation id and the
  124. /// bucket number if the signature checked out. It is up to the
  125. /// caller to then check that the invitation id has not been used
  126. /// before.
  127. pub fn verify(
  128. invitation: [u8; OPENINV_LENGTH],
  129. pubkey: PublicKey,
  130. ) -> Result<(Scalar, u32), SignatureError> {
  131. // Pull out the signature and verify it
  132. let sig = Signature::try_from(&invitation[(32 + 4)..])?;
  133. pubkey.verify(&invitation[0..(32 + 4)], &sig)?;
  134. // The signature passed. Pull out the bucket number and then
  135. // the invitation id
  136. let bucket = u32::from_le_bytes(invitation[32..(32 + 4)].try_into().unwrap());
  137. match Scalar::from_canonical_bytes(invitation[0..32].try_into().unwrap()) {
  138. // It should never happen that there's a valid signature on
  139. // an invalid serialization of a Scalar, but check anyway.
  140. None => Err(SignatureError::new()),
  141. Some(s) => Ok((s, bucket)),
  142. }
  143. }
  144. }
  145. /// The bridge authority. This will typically be a singleton object.
  146. #[derive(Debug)]
  147. pub struct BridgeAuth {
  148. /// The private key for the main Lox credential
  149. lox_priv: IssuerPrivKey,
  150. /// The public key for the main Lox credential
  151. pub lox_pub: IssuerPubKey,
  152. /// The private key for migration credentials
  153. migration_priv: IssuerPrivKey,
  154. /// The public key for migration credentials
  155. pub migration_pub: IssuerPubKey,
  156. /// The public key of the BridgeDb issuing open invitations
  157. pub bridgedb_pub: PublicKey,
  158. /// Duplicate filter for open invitations
  159. openinv_filter: dup_filter::DupFilter<Scalar>,
  160. /// Duplicate filter for credential ids
  161. id_filter: dup_filter::DupFilter<Scalar>,
  162. /// For testing only: offset of the true time to the simulated time
  163. time_offset: time::Duration,
  164. }
  165. impl BridgeAuth {
  166. pub fn new(bridgedb_pub: PublicKey) -> Self {
  167. let lox_priv = IssuerPrivKey::new(6);
  168. let lox_pub = IssuerPubKey::new(&lox_priv);
  169. let migration_priv = IssuerPrivKey::new(3);
  170. let migration_pub = IssuerPubKey::new(&migration_priv);
  171. Self {
  172. lox_priv,
  173. lox_pub,
  174. migration_priv,
  175. migration_pub,
  176. bridgedb_pub,
  177. openinv_filter: Default::default(),
  178. id_filter: Default::default(),
  179. time_offset: time::Duration::zero(),
  180. }
  181. }
  182. /// For testing only: manually advance the day by 1 day
  183. pub fn advance_day(&mut self) {
  184. self.time_offset += time::Duration::days(1);
  185. }
  186. /// For testing only: manually advance the day by the given number
  187. /// of days
  188. pub fn advance_days(&mut self, days: u16) {
  189. self.time_offset += time::Duration::days(days.into());
  190. }
  191. /// Get today's (real or simulated) date
  192. fn today(&self) -> i64 {
  193. (time::OffsetDateTime::now_utc().date() + self.time_offset).julian_day()
  194. }
  195. }