ot.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Oblivious transfer
  2. use subtle::Choice;
  3. use subtle::ConditionallySelectable;
  4. use aes::Block;
  5. use rand::RngCore;
  6. use sha2::Digest;
  7. use sha2::Sha256;
  8. use sha2::Sha512;
  9. use curve25519_dalek::constants as dalek_constants;
  10. use curve25519_dalek::ristretto::CompressedRistretto;
  11. use curve25519_dalek::ristretto::RistrettoBasepointTable;
  12. use curve25519_dalek::ristretto::RistrettoPoint;
  13. use curve25519_dalek::scalar::Scalar;
  14. use lazy_static::lazy_static;
  15. // Generators of the Ristretto group (the standard B and another one C,
  16. // for which the DL relationship is unknown), and their precomputed
  17. // multiplication tables. Used for the Oblivious Transfer protocol
  18. lazy_static! {
  19. pub static ref OT_B: RistrettoPoint = dalek_constants::RISTRETTO_BASEPOINT_POINT;
  20. pub static ref OT_C: RistrettoPoint =
  21. RistrettoPoint::hash_from_bytes::<Sha512>(b"OT Generator C");
  22. pub static ref OT_B_TABLE: RistrettoBasepointTable = dalek_constants::RISTRETTO_BASEPOINT_TABLE;
  23. pub static ref OT_C_TABLE: RistrettoBasepointTable = RistrettoBasepointTable::create(&OT_C);
  24. }
  25. // 1-out-of-2 Oblivious Transfer (OT)
  26. fn ot12_request(sel: Choice) -> ((Choice, Scalar), [u8; 32]) {
  27. let Btable: &RistrettoBasepointTable = &OT_B_TABLE;
  28. let C: &RistrettoPoint = &OT_C;
  29. let mut rng = rand07::thread_rng();
  30. let x = Scalar::random(&mut rng);
  31. let xB = &x * Btable;
  32. let CmxB = C - xB;
  33. let P = RistrettoPoint::conditional_select(&xB, &CmxB, sel);
  34. ((sel, x), P.compress().to_bytes())
  35. }
  36. fn ot12_serve(query: &[u8; 32], m0: &[u8; 16], m1: &[u8; 16]) -> [u8; 64] {
  37. let Btable: &RistrettoBasepointTable = &OT_B_TABLE;
  38. let Ctable: &RistrettoBasepointTable = &OT_C_TABLE;
  39. let mut rng = rand07::thread_rng();
  40. let y = Scalar::random(&mut rng);
  41. let yB = &y * Btable;
  42. let yC = &y * Ctable;
  43. let P = CompressedRistretto::from_slice(query).decompress().unwrap();
  44. let yP0 = y * P;
  45. let yP1 = yC - yP0;
  46. let mut HyP0 = Sha256::digest(yP0.compress().as_bytes());
  47. for i in 0..16 {
  48. HyP0[i] ^= m0[i];
  49. }
  50. let mut HyP1 = Sha256::digest(yP1.compress().as_bytes());
  51. for i in 0..16 {
  52. HyP1[i] ^= m1[i];
  53. }
  54. let mut ret = [0u8; 64];
  55. ret[0..32].copy_from_slice(yB.compress().as_bytes());
  56. ret[32..48].copy_from_slice(&HyP0[0..16]);
  57. ret[48..64].copy_from_slice(&HyP1[0..16]);
  58. ret
  59. }
  60. fn ot12_receive(state: (Choice, Scalar), response: &[u8; 64]) -> [u8; 16] {
  61. let yB = CompressedRistretto::from_slice(&response[0..32])
  62. .decompress()
  63. .unwrap();
  64. let yP = state.1 * yB;
  65. let mut HyP = Sha256::digest(yP.compress().as_bytes());
  66. for i in 0..16 {
  67. HyP[i] ^= u8::conditional_select(&response[32 + i], &response[48 + i], state.0);
  68. }
  69. HyP[0..16].try_into().unwrap()
  70. }
  71. // Obliviously fetch the key for element q of the database (which has
  72. // 2^r elements total). Each bit of q is used in a 1-out-of-2 OT to get
  73. // one of the keys in each of the r pairs of keys on the server side.
  74. // The resulting r keys are XORed together.
  75. pub fn otkey_init() {
  76. // Resolve the lazy statics
  77. let _B: &RistrettoPoint = &OT_B;
  78. let _Btable: &RistrettoBasepointTable = &OT_B_TABLE;
  79. let _C: &RistrettoPoint = &OT_C;
  80. let _Ctable: &RistrettoBasepointTable = &OT_C_TABLE;
  81. }
  82. pub fn otkey_request(q: usize, r: usize) -> (Vec<(Choice, Scalar)>, Vec<[u8; 32]>) {
  83. let mut state: Vec<(Choice, Scalar)> = Vec::with_capacity(r);
  84. let mut query: Vec<[u8; 32]> = Vec::with_capacity(r);
  85. for i in 0..r {
  86. let bit = ((q >> i) & 1) as u8;
  87. let (si, qi) = ot12_request(bit.into());
  88. state.push(si);
  89. query.push(qi);
  90. }
  91. (state, query)
  92. }
  93. pub fn otkey_serve(query: Vec<[u8; 32]>, keys: &Vec<[u8; 16]>) -> Vec<[u8; 64]> {
  94. let r = query.len();
  95. assert!(keys.len() == 2 * r);
  96. let mut response: Vec<[u8; 64]> = Vec::with_capacity(r);
  97. for i in 0..r {
  98. response.push(ot12_serve(&query[i], &keys[2 * i], &keys[2 * i + 1]));
  99. }
  100. response
  101. }
  102. // XOR a 16-byte slice into a Block (which will be used as an AES key)
  103. pub fn xor16(outar: &mut Block, inar: &[u8; 16]) {
  104. for i in 0..16 {
  105. outar[i] ^= inar[i];
  106. }
  107. }
  108. pub fn otkey_receive(state: Vec<(Choice, Scalar)>, response: &Vec<[u8; 64]>) -> Block {
  109. let r = state.len();
  110. assert!(response.len() == r);
  111. let mut key = Block::from([0u8; 16]);
  112. for i in 0..r {
  113. xor16(&mut key, &ot12_receive(state[i], &response[i]));
  114. }
  115. key
  116. }
  117. // Generate the keys for encrypting the database
  118. pub fn gen_db_enc_keys(r: usize) -> Vec<[u8; 16]> {
  119. let mut keys: Vec<[u8; 16]> = Vec::new();
  120. let mut rng = rand::thread_rng();
  121. for _ in 0..2 * r {
  122. let mut k: [u8; 16] = [0; 16];
  123. rng.fill_bytes(&mut k);
  124. keys.push(k);
  125. }
  126. keys
  127. }