ot.rs 4.4 KB

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