migration_table.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*! The migration table.
  2. This is a table listing pairs of (from_bucket_id, to_bucket_id). A pair
  3. in this table indicates that a user with a Lox credential containing
  4. from_bucket_id (and possibly meeting other conditions as well) is
  5. entitled to exchange their credential for one with to_bucket_id. (Note
  6. that the credentials contain the bucket attributes, which include both
  7. the id and the bucket decrytpion key, but the table just contains the
  8. bucket ids.) */
  9. use curve25519_dalek::ristretto::CompressedRistretto;
  10. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  11. use curve25519_dalek::ristretto::RistrettoPoint;
  12. use curve25519_dalek::scalar::Scalar;
  13. use sha2::Digest;
  14. use sha2::Sha256;
  15. use aes_gcm::aead::{generic_array::GenericArray, Aead, NewAead};
  16. use aes_gcm::Aes128Gcm;
  17. use rand::RngCore;
  18. use std::collections::HashMap;
  19. use super::bridge_table;
  20. use super::cred::Migration;
  21. use super::IssuerPrivKey;
  22. use super::CMZ_B_TABLE;
  23. /// Each (plaintext) entry in the returned migration table is serialized
  24. /// into this many bytes
  25. pub const MIGRATION_BYTES: usize = 96;
  26. /// The size of an encrypted entry in the returned migration table
  27. pub const ENC_MIGRATION_BYTES: usize = MIGRATION_BYTES + 12 + 16;
  28. /// The migration table
  29. #[derive(Default, Debug)]
  30. pub struct MigrationTable {
  31. pub table: HashMap<u32, u32>,
  32. }
  33. /// Create an encrypted Migration credential for returning to the user
  34. /// in the trust promotion protocol.
  35. ///
  36. /// Given the attributes of a Migration credential, produce a serialized
  37. /// version (containing only the to_bucket and the MAC, since the
  38. /// receiver will already know the id and from_bucket), encrypted with
  39. /// H2(id, from_bucket, Qk), for the Qk portion of the MAC on the
  40. /// corresponding Migration Key credential (with fixed Pk, given as a
  41. /// precomputed multiplication table). Return the label H1(id,
  42. /// from_attr_i, Qk_i) and the encrypted Migration credential. H1 and
  43. /// H2 are the first 16 bytes and the second 16 bytes respectively of
  44. /// the SHA256 hash of the input.
  45. pub fn encrypt_cred(
  46. id: &Scalar,
  47. from_bucket: &Scalar,
  48. to_bucket: &Scalar,
  49. Pktable: &RistrettoBasepointTable,
  50. migration_priv: &IssuerPrivKey,
  51. migrationkey_priv: &IssuerPrivKey,
  52. ) -> ([u8; 16], [u8; ENC_MIGRATION_BYTES]) {
  53. let Btable: &RistrettoBasepointTable = &CMZ_B_TABLE;
  54. let mut rng = rand::thread_rng();
  55. // Compute the Migration Key credential MAC Qk
  56. let Qk = &(migrationkey_priv.x[0]
  57. + migrationkey_priv.x[1] * id
  58. + migrationkey_priv.x[2] * from_bucket)
  59. * Pktable;
  60. // Compute a MAC (P, Q) on the Migration credential
  61. let b = Scalar::random(&mut rng);
  62. let P = &b * Btable;
  63. let Q = &(b
  64. * (migration_priv.x[0]
  65. + migration_priv.x[1] * id
  66. + migration_priv.x[2] * from_bucket
  67. + migration_priv.x[3] * to_bucket))
  68. * Btable;
  69. // Serialize (to_bucket, P, Q)
  70. let mut credbytes: [u8; MIGRATION_BYTES] = [0; MIGRATION_BYTES];
  71. credbytes[0..32].copy_from_slice(to_bucket.as_bytes());
  72. credbytes[32..64].copy_from_slice(P.compress().as_bytes());
  73. credbytes[64..].copy_from_slice(Q.compress().as_bytes());
  74. // Pick a random nonce
  75. let mut noncebytes: [u8; 12] = [0; 12];
  76. rng.fill_bytes(&mut noncebytes);
  77. let nonce = GenericArray::from_slice(&noncebytes);
  78. // Compute the hash of (id, from_bucket, Qk)
  79. let mut hasher = Sha256::new();
  80. hasher.update(id.as_bytes());
  81. hasher.update(from_bucket.as_bytes());
  82. hasher.update(Qk.compress().as_bytes());
  83. let fullhash = hasher.finalize();
  84. // Create the encryption key from the 2nd half of the hash
  85. let aeskey = GenericArray::from_slice(&fullhash[16..]);
  86. // Encrypt
  87. let cipher = Aes128Gcm::new(aeskey);
  88. let ciphertext: Vec<u8> = cipher.encrypt(&nonce, credbytes.as_ref()).unwrap();
  89. let mut enccredbytes: [u8; ENC_MIGRATION_BYTES] = [0; ENC_MIGRATION_BYTES];
  90. enccredbytes[..12].copy_from_slice(&noncebytes);
  91. enccredbytes[12..].copy_from_slice(ciphertext.as_slice());
  92. // Use the first half of the above hash as the label
  93. let mut label: [u8; 16] = [0; 16];
  94. label[..].copy_from_slice(&fullhash[..16]);
  95. (label, enccredbytes)
  96. }
  97. /// Create an encrypted Migration credential for returning to the user
  98. /// in the trust promotion protocol, given the ids of the from and to
  99. /// buckets, and using a BridgeTable to get the bucket keys.
  100. ///
  101. /// Otherwise the same as encrypt_cred, above, except it returns an
  102. /// Option in case the passed ids were invalid.
  103. pub fn encrypt_cred_ids(
  104. id: &Scalar,
  105. from_id: u32,
  106. to_id: u32,
  107. bridgetable: &bridge_table::BridgeTable,
  108. Pktable: &RistrettoBasepointTable,
  109. migration_priv: &IssuerPrivKey,
  110. migrationkey_priv: &IssuerPrivKey,
  111. ) -> Option<([u8; 16], [u8; ENC_MIGRATION_BYTES])> {
  112. // Look up the bucket keys and form the attributes (Scalars)
  113. let fromkey = bridgetable.keys.get(from_id as usize)?;
  114. let tokey = bridgetable.keys.get(to_id as usize)?;
  115. Some(encrypt_cred(
  116. id,
  117. &bridge_table::to_scalar(from_id, fromkey),
  118. &bridge_table::to_scalar(to_id, tokey),
  119. Pktable,
  120. migration_priv,
  121. migrationkey_priv,
  122. ))
  123. }
  124. impl MigrationTable {
  125. /// For each entry in the MigrationTable, use encrypt_cred_ids to
  126. /// produce an entry in an output HashMap (from labels to encrypted
  127. /// Migration credentials).
  128. pub fn encrypt_table(
  129. &self,
  130. id: &Scalar,
  131. bridgetable: &bridge_table::BridgeTable,
  132. Pktable: &RistrettoBasepointTable,
  133. migration_priv: &IssuerPrivKey,
  134. migrationkey_priv: &IssuerPrivKey,
  135. ) -> HashMap<[u8; 16], [u8; ENC_MIGRATION_BYTES]> {
  136. self.table
  137. .iter()
  138. .filter_map(|(from_id, to_id)| {
  139. encrypt_cred_ids(
  140. id,
  141. *from_id,
  142. *to_id,
  143. bridgetable,
  144. Pktable,
  145. migration_priv,
  146. migrationkey_priv,
  147. )
  148. })
  149. .collect()
  150. }
  151. }
  152. /// Decrypt an encrypted Migration credential given Qk, the known
  153. /// attributes id and from_bucket for the Migration credential, and a
  154. /// HashMap mapping labels to ciphertexts.
  155. pub fn decrypt_cred(
  156. Qk: &RistrettoPoint,
  157. lox_id: &Scalar,
  158. from_bucket: &Scalar,
  159. enc_migration_table: &HashMap<[u8; 16], [u8; ENC_MIGRATION_BYTES]>,
  160. ) -> Option<Migration> {
  161. // Compute the hash of (id, from_bucket, Qk)
  162. let mut hasher = Sha256::new();
  163. hasher.update(lox_id.as_bytes());
  164. hasher.update(from_bucket.as_bytes());
  165. hasher.update(Qk.compress().as_bytes());
  166. let fullhash = hasher.finalize();
  167. // Use the first half of the above hash as the label
  168. let mut label: [u8; 16] = [0; 16];
  169. label[..].copy_from_slice(&fullhash[..16]);
  170. // Look up the label in the HashMap
  171. let ciphertext = enc_migration_table.get(&label)?;
  172. // Create the decryption key from the 2nd half of the hash
  173. let aeskey = GenericArray::from_slice(&fullhash[16..]);
  174. // Decrypt
  175. let nonce = GenericArray::from_slice(&ciphertext[..12]);
  176. let cipher = Aes128Gcm::new(aeskey);
  177. let plaintext: Vec<u8> = match cipher.decrypt(&nonce, ciphertext[12..].as_ref()) {
  178. Ok(v) => v,
  179. Err(_) => return None,
  180. };
  181. let plaintextbytes = plaintext.as_slice();
  182. let mut to_bucket_bytes: [u8; 32] = [0; 32];
  183. to_bucket_bytes.copy_from_slice(&plaintextbytes[..32]);
  184. let to_bucket = Scalar::from_bytes_mod_order(to_bucket_bytes);
  185. let P = CompressedRistretto::from_slice(&plaintextbytes[32..64]).decompress()?;
  186. let Q = CompressedRistretto::from_slice(&plaintextbytes[64..]).decompress()?;
  187. Some(Migration {
  188. P,
  189. Q,
  190. lox_id: *lox_id,
  191. from_bucket: *from_bucket,
  192. to_bucket,
  193. })
  194. }