client.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. use rand::RngCore;
  2. use std::collections::VecDeque;
  3. use std::mem;
  4. use std::os::raw::c_uchar;
  5. use std::sync::mpsc::*;
  6. use std::thread::*;
  7. use aes::Block;
  8. use rayon::prelude::*;
  9. use subtle::Choice;
  10. use curve25519_dalek::scalar::Scalar;
  11. use crate::dbentry_decrypt;
  12. use crate::ot::*;
  13. use crate::params;
  14. use crate::to_vecdata;
  15. use crate::DbEntry;
  16. use crate::PreProcSingleMsg;
  17. use crate::PreProcSingleRespMsg;
  18. use crate::VecData;
  19. enum Command {
  20. PreProc(usize),
  21. PreProcFinish(Vec<PreProcSingleRespMsg>),
  22. Query(usize),
  23. QueryFinish(Vec<u8>),
  24. }
  25. enum Response {
  26. PubParams(Vec<u8>),
  27. PreProcMsg(Vec<u8>),
  28. PreProcDone,
  29. QueryMsg(Vec<u8>),
  30. QueryDone(DbEntry),
  31. }
  32. // The internal client state for a single outstanding preprocess query
  33. struct PreProcOutSingleState {
  34. rand_idx: usize,
  35. ot_state: Vec<(Choice, Scalar)>,
  36. }
  37. // The internal client state for a single preprocess ready to be used
  38. struct PreProcSingleState {
  39. rand_idx: usize,
  40. ot_key: Block,
  41. }
  42. pub struct Client {
  43. incoming_cmd: SyncSender<Command>,
  44. outgoing_resp: Receiver<Response>,
  45. }
  46. impl Client {
  47. pub fn new(r: usize) -> (Self, Vec<u8>) {
  48. let (incoming_cmd, incoming_cmd_recv) = sync_channel(0);
  49. let (outgoing_resp_send, outgoing_resp) = sync_channel(0);
  50. spawn(move || {
  51. let spiral_params = params::get_spiral_params(r);
  52. let mut clientrng = rand::thread_rng();
  53. let mut rng = rand::thread_rng();
  54. let mut spiral_client = spiral_rs::client::Client::init(&spiral_params, &mut clientrng);
  55. let num_records = 1 << r;
  56. let num_records_mask = num_records - 1;
  57. let spiral_blocking_factor = spiral_params.db_item_size / mem::size_of::<DbEntry>();
  58. // The first communication is the pub_params
  59. let pub_params = spiral_client.generate_keys().serialize();
  60. outgoing_resp_send
  61. .send(Response::PubParams(pub_params))
  62. .unwrap();
  63. // State for outstanding preprocessing queries
  64. let mut preproc_out_state: Vec<PreProcOutSingleState> = Vec::new();
  65. // State for preprocessing queries ready to be used
  66. let mut preproc_state: VecDeque<PreProcSingleState> = VecDeque::new();
  67. // State for outstanding active queries
  68. let mut query_state: VecDeque<PreProcSingleState> = VecDeque::new();
  69. // Wait for commands
  70. loop {
  71. match incoming_cmd_recv.recv() {
  72. Err(_) => break,
  73. Ok(Command::PreProc(num_preproc)) => {
  74. // Ensure we don't already have outstanding
  75. // preprocessing state
  76. assert!(preproc_out_state.is_empty());
  77. let mut preproc_msg: Vec<PreProcSingleMsg> = Vec::new();
  78. for _ in 0..num_preproc {
  79. let rand_idx = (rng.next_u64() as usize) & num_records_mask;
  80. let rand_pir_idx = rand_idx / spiral_blocking_factor;
  81. let spc_query = spiral_client.generate_query(rand_pir_idx).serialize();
  82. let (ot_state, ot_query) = otkey_request(rand_idx, r);
  83. preproc_out_state.push(PreProcOutSingleState { rand_idx, ot_state });
  84. preproc_msg.push(PreProcSingleMsg {
  85. ot_query,
  86. spc_query,
  87. });
  88. }
  89. let ret: Vec<u8> = bincode::serialize(&preproc_msg).unwrap();
  90. outgoing_resp_send.send(Response::PreProcMsg(ret)).unwrap();
  91. }
  92. Ok(Command::PreProcFinish(srvresp)) => {
  93. let num_preproc = srvresp.len();
  94. assert!(preproc_out_state.len() == num_preproc);
  95. let mut newstate: VecDeque<PreProcSingleState> = preproc_out_state
  96. .into_par_iter()
  97. .zip(srvresp)
  98. .map(|(c, s)| {
  99. let ot_key = otkey_receive(c.ot_state, &s.ot_resp);
  100. PreProcSingleState {
  101. rand_idx: c.rand_idx,
  102. ot_key,
  103. }
  104. })
  105. .collect();
  106. preproc_state.append(&mut newstate);
  107. preproc_out_state = Vec::new();
  108. outgoing_resp_send.send(Response::PreProcDone).unwrap();
  109. }
  110. Ok(Command::Query(idx)) => {
  111. // panic if there are no preproc states
  112. // available
  113. let nextstate = preproc_state.pop_front().unwrap();
  114. let offset = (num_records + idx - nextstate.rand_idx) & num_records_mask;
  115. let mut querymsg: Vec<u8> = Vec::new();
  116. querymsg.extend(offset.to_le_bytes());
  117. query_state.push_back(nextstate);
  118. outgoing_resp_send
  119. .send(Response::QueryMsg(querymsg))
  120. .unwrap();
  121. }
  122. Ok(Command::QueryFinish(msg)) => {
  123. // panic if there is no outstanding state
  124. let nextstate = query_state.pop_front().unwrap();
  125. let encdbblock = spiral_client.decode_response(msg.as_slice());
  126. // Extract the one encrypted DbEntry we were
  127. // looking for (and the only one we are able to
  128. // decrypt)
  129. let entry_in_block = nextstate.rand_idx % spiral_blocking_factor;
  130. let loc_in_block = entry_in_block * mem::size_of::<DbEntry>();
  131. let loc_in_block_end = (entry_in_block + 1) * mem::size_of::<DbEntry>();
  132. let encdbentry = DbEntry::from_le_bytes(
  133. encdbblock[loc_in_block..loc_in_block_end]
  134. .try_into()
  135. .unwrap(),
  136. );
  137. let decdbentry =
  138. dbentry_decrypt(&nextstate.ot_key, nextstate.rand_idx, encdbentry);
  139. outgoing_resp_send
  140. .send(Response::QueryDone(decdbentry))
  141. .unwrap();
  142. }
  143. // When adding new messages, the following line is
  144. // useful during development
  145. // _ => panic!("Received something unexpected in client loop"),
  146. }
  147. }
  148. });
  149. let pub_params = match outgoing_resp.recv() {
  150. Ok(Response::PubParams(x)) => x,
  151. _ => panic!("Received something unexpected in client new"),
  152. };
  153. (
  154. Client {
  155. incoming_cmd,
  156. outgoing_resp,
  157. },
  158. pub_params,
  159. )
  160. }
  161. pub fn preproc(&self, num_preproc: usize) -> Vec<u8> {
  162. self.incoming_cmd
  163. .send(Command::PreProc(num_preproc))
  164. .unwrap();
  165. match self.outgoing_resp.recv() {
  166. Ok(Response::PreProcMsg(x)) => x,
  167. _ => panic!("Received something unexpected in preproc"),
  168. }
  169. }
  170. pub fn preproc_finish(&self, msg: &[u8]) {
  171. self.incoming_cmd
  172. .send(Command::PreProcFinish(bincode::deserialize(msg).unwrap()))
  173. .unwrap();
  174. match self.outgoing_resp.recv() {
  175. Ok(Response::PreProcDone) => (),
  176. _ => panic!("Received something unexpected in preproc_finish"),
  177. }
  178. }
  179. pub fn query(&self, idx: usize) -> Vec<u8> {
  180. self.incoming_cmd.send(Command::Query(idx)).unwrap();
  181. match self.outgoing_resp.recv() {
  182. Ok(Response::QueryMsg(x)) => x,
  183. _ => panic!("Received something unexpected in preproc"),
  184. }
  185. }
  186. pub fn query_finish(&self, msg: &[u8]) -> DbEntry {
  187. self.incoming_cmd
  188. .send(Command::QueryFinish(msg.to_vec()))
  189. .unwrap();
  190. match self.outgoing_resp.recv() {
  191. Ok(Response::QueryDone(entry)) => entry,
  192. _ => panic!("Received something unexpected in preproc_finish"),
  193. }
  194. }
  195. }
  196. #[repr(C)]
  197. pub struct ClientNewRet {
  198. client: *mut Client,
  199. pub_params: VecData,
  200. }
  201. #[no_mangle]
  202. pub extern "C" fn spir_client_new(r: u8) -> ClientNewRet {
  203. let (client, pub_params) = Client::new(r as usize);
  204. ClientNewRet {
  205. client: Box::into_raw(Box::new(client)),
  206. pub_params: to_vecdata(pub_params),
  207. }
  208. }
  209. #[no_mangle]
  210. pub extern "C" fn spir_client_free(client: *mut Client) {
  211. if client.is_null() {
  212. return;
  213. }
  214. unsafe {
  215. Box::from_raw(client);
  216. }
  217. }
  218. #[no_mangle]
  219. pub extern "C" fn spir_client_preproc(clientptr: *mut Client, num_preproc: u32) -> VecData {
  220. let client = unsafe {
  221. assert!(!clientptr.is_null());
  222. &mut *clientptr
  223. };
  224. let retvec = client.preproc(num_preproc as usize);
  225. to_vecdata(retvec)
  226. }
  227. #[no_mangle]
  228. pub extern "C" fn spir_client_preproc_finish(
  229. clientptr: *mut Client,
  230. msgdata: *const c_uchar,
  231. msglen: usize,
  232. ) {
  233. let client = unsafe {
  234. assert!(!clientptr.is_null());
  235. &mut *clientptr
  236. };
  237. let msg_slice = unsafe {
  238. assert!(!msgdata.is_null());
  239. std::slice::from_raw_parts(msgdata, msglen)
  240. };
  241. client.preproc_finish(msg_slice);
  242. }
  243. #[no_mangle]
  244. pub extern "C" fn spir_client_query(clientptr: *mut Client, idx: usize) -> VecData {
  245. let client = unsafe {
  246. assert!(!clientptr.is_null());
  247. &mut *clientptr
  248. };
  249. let retvec = client.query(idx);
  250. to_vecdata(retvec)
  251. }
  252. #[no_mangle]
  253. pub extern "C" fn spir_client_query_finish(
  254. clientptr: *mut Client,
  255. msgdata: *const c_uchar,
  256. msglen: usize,
  257. ) -> DbEntry {
  258. let client = unsafe {
  259. assert!(!clientptr.is_null());
  260. &mut *clientptr
  261. };
  262. let msg_slice = unsafe {
  263. assert!(!msgdata.is_null());
  264. std::slice::from_raw_parts(msgdata, msglen)
  265. };
  266. client.query_finish(msg_slice)
  267. }