PIRClient.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "PIRClient.hpp"
  18. //Use this to limit the upload speed to UPLOAD_LIMIT bits per second
  19. //#define UPLOAD_LIMIT 100000000UL
  20. PIRClientSimple::PIRClientSimple(boost::asio::io_service& ios, ClientParams params, FixedVars vars):
  21. socket_up(ios),
  22. replyWriter(pirParams, writeListeners, messageListeners),
  23. catalog(messageListeners),
  24. clientParams(params),
  25. fixedVars(vars),
  26. optimum(vars),
  27. no_pipeline_mode(false)
  28. {
  29. replyWriter.setdontWrite(params.dontwrite);
  30. //string lwe_params(crypto_params_const);
  31. //cryptoMethod.getPublicParameters().computeNewParameters(lwe_params);
  32. //pirParams.d = kDimensionNumber;
  33. }
  34. void PIRClientSimple::joinAllThreads()
  35. {
  36. replyExt->replyThread.join();
  37. replyWriter.join();
  38. }
  39. PIRClientSimple::~PIRClientSimple()
  40. {
  41. joinAllThreads();
  42. delete cryptoMethod;
  43. }
  44. void PIRClientSimple::connect()
  45. {
  46. using namespace boost::asio::ip;
  47. try
  48. {
  49. socket_up.connect(tcp::endpoint(address::from_string(clientParams.server_ip), clientParams.port));
  50. // Tell the server we are a client
  51. int is_client = 1;
  52. write(socket_up, boost::asio::buffer(&is_client, sizeof(int)));
  53. }
  54. catch (const std::exception& ex)
  55. {
  56. exitWithErrorMessage(__FUNCTION__, ex.what());
  57. }
  58. }
  59. /**
  60. * Downloads the file catalog from the server.
  61. * Exception :
  62. * - Stop the programme if any network trouble.
  63. **/
  64. void PIRClientSimple::downloadCatalog()
  65. {
  66. boost::uint64_t size = 1;
  67. try
  68. {
  69. read(socket_up, boost::asio::buffer(&size, sizeof(size)));
  70. std::cout<< "PIRClient: Catalog bytesize is "<<size<<std::endl;
  71. char* buf = new char[size + 1]();
  72. if (read(socket_up, boost::asio::buffer(buf, size)) < size )
  73. writeWarningMessage(__FUNCTION__, " Catalog has not been fully received.");
  74. catalog.makeMenu(buf);
  75. if (catalog.getFilesNum() == 0)
  76. {
  77. writeWarningMessage(__FUNCTION__,"Empty catalog recieved ! Is there a db directory with files in the server/ directory ? Aborting ");
  78. exit(1);
  79. }
  80. delete[] buf;
  81. std::cout << "PIRClient: Catalog received with " << catalog.getFilesNum() << " files" << std::endl;
  82. }
  83. catch (const std::exception& ex)
  84. {
  85. exitWithErrorMessage(__FUNCTION__, ex.what());
  86. }
  87. }
  88. void PIRClientSimple::optimize()
  89. {
  90. if (exchange_method == CLIENT_DRIVEN)
  91. {
  92. PIROptimizer optimizer;
  93. if (clientParams.verboseoptim == false) optimizer.silent = true;
  94. std::cout << "PIRClient: Starting optimization ..." << std::endl;
  95. OptimVars op = optimizer.optimizeFromServer(fixedVars, socket_up);
  96. optimum = op;
  97. std::cout << "PIRClient: Optimization returned alpha=" << optimum.alpha << " d=" << optimum.d << " crypto_params=" << optimum.crypto_params << std::endl;
  98. pirParams.d = optimum.d;
  99. pirParams.alpha = optimum.alpha;
  100. optimizer.getDimSize(optimum.getFixedVars().n, optimum.alpha, optimum.d, pirParams.n);
  101. pirParams.crypto_params = std::string(optimum.crypto_params);
  102. }
  103. }
  104. void PIRClientSimple::processPIRParams()
  105. {
  106. if (exchange_method == CLIENT_DRIVEN)
  107. {
  108. sendPirParams();
  109. }
  110. else rcvPirParams();
  111. }
  112. /**
  113. * Sends PIR parameters to the server.
  114. * Exception :
  115. * - Stop the programme if any network trouble.
  116. **/
  117. void PIRClientSimple::sendPirParams()
  118. {
  119. try
  120. {
  121. write(socket_up, boost::asio::buffer(&pirParams.d, sizeof(int)));
  122. write(socket_up, boost::asio::buffer(&pirParams.alpha, sizeof(int)));
  123. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  124. {
  125. write(socket_up, boost::asio::buffer(&pirParams.n[i], sizeof(int)));
  126. }
  127. }
  128. catch (const std::exception& ex)
  129. {
  130. exitWithErrorMessage(__FUNCTION__, ex.what());
  131. }
  132. }
  133. void PIRClientSimple::rcvPirParams()
  134. {
  135. try
  136. {
  137. read(socket_up, boost::asio::buffer(&pirParams.d, sizeof(int)));
  138. read(socket_up, boost::asio::buffer(&pirParams.alpha, sizeof(int)));
  139. for (unsigned int i = 0 ; i < pirParams.d ; i++)
  140. {
  141. read(socket_up, boost::asio::buffer(&pirParams.n[i], sizeof(int)));
  142. }
  143. std::cout << "PIRClient: Received PIR parameters alpha=" << pirParams.alpha << " d=" << pirParams.d << std::endl;
  144. }
  145. catch (const std::exception& ex)
  146. {
  147. exitWithErrorMessage(__FUNCTION__, ex.what());
  148. }
  149. }
  150. void PIRClientSimple::rcvPIRParamsExchangeMethod()
  151. {
  152. try
  153. {
  154. read(socket_up, boost::asio::buffer(&exchange_method, sizeof(short)));
  155. if (exchange_method == CLIENT_DRIVEN)
  156. {
  157. std::cout << "PIRClient: Client-driven mode used" << std::endl;
  158. }
  159. else
  160. {
  161. std::cout << "PIRClient: Server-driven mode used" << std::endl;
  162. }
  163. }
  164. catch (const std::exception& ex)
  165. {
  166. exitWithErrorMessage(__FUNCTION__, ex.what());
  167. }
  168. }
  169. void PIRClientSimple::processCryptoParams() {
  170. bool send_paramsandkey = true;
  171. if (exchange_method == CLIENT_DRIVEN)
  172. {
  173. cryptoMethod = HomomorphicCryptoFactory_internal::getCryptoMethod(optimum.crypto_params);
  174. replyWriter.setCryptoMethod(cryptoMethod);
  175. sendCryptoParams(send_paramsandkey);
  176. }
  177. else
  178. {
  179. rcvCryptoParams();
  180. cryptoMethod = HomomorphicCryptoFactory_internal::getCryptoMethod(pirParams.crypto_params);
  181. replyWriter.setCryptoMethod(cryptoMethod);
  182. sendCryptoParams(!send_paramsandkey);
  183. }
  184. }
  185. /**
  186. * Send cryptographics parameters (e.g : key) to the server.
  187. * Exception :
  188. * - Stop the programme if any network trouble.
  189. **/
  190. void PIRClientSimple::sendCryptoParams(bool paramsandkey)
  191. {
  192. unsigned int size;
  193. char* key;
  194. try
  195. {
  196. if (paramsandkey == true)
  197. {
  198. bool shortversion = true;
  199. string crypto_params = cryptoMethod->getSerializedCryptoParams(!shortversion);
  200. size = crypto_params.length();
  201. write(socket_up, boost::asio::buffer(&size, sizeof(size)));
  202. write(socket_up, boost::asio::buffer(crypto_params));
  203. }
  204. size = ceil((double) cryptoMethod->getPublicParameters().getSerializedModulusBitsize() / GlobalConstant::kBitsPerByte);
  205. write(socket_up, boost::asio::buffer(&size, sizeof(size)));
  206. key = cryptoMethod->getPublicParameters().getByteModulus();
  207. write(socket_up, boost::asio::buffer(key, size));//Send key
  208. delete[] key;
  209. }
  210. catch (const std::exception& ex)
  211. {
  212. exitWithErrorMessage(__FUNCTION__, ex.what());
  213. }
  214. }
  215. void PIRClientSimple::rcvCryptoParams() {
  216. try
  217. {
  218. int crypto_params_size = 0;
  219. read(socket_up, boost::asio::buffer(&crypto_params_size, sizeof(crypto_params_size)));
  220. char crypto_params[crypto_params_size + 1];
  221. int read_size = read(socket_up, boost::asio::buffer(crypto_params,crypto_params_size));
  222. crypto_params[read_size] = '\0';
  223. pirParams.crypto_params = string(crypto_params);
  224. std::cout << "PIRClient: Received crypto parameters " << crypto_params << std::endl;
  225. }
  226. catch (const std::exception& ex)
  227. {
  228. exitWithErrorMessage(__FUNCTION__, ex.what());
  229. }
  230. }
  231. /**
  232. * Launches the choose File action to the recorded view. If autochoice is set,
  233. * the first file is chosen.
  234. **/
  235. void PIRClientSimple::chooseFile()
  236. {
  237. chosenElement = 0;
  238. if(!clientParams.autochoice)
  239. {
  240. CatalogEvent catalogEvent(catalog.getFileList());
  241. menuListeners(catalogEvent); // get user input and set chosenElement.
  242. }
  243. std::ostringstream oss;
  244. oss << "PIRClient: Retrieved file is nbr " + (chosenElement+1);
  245. oss << + ", \"" + catalog.getFileName(chosenElement) + "\"";
  246. MessageEvent messageEvent(oss.str());
  247. messageListeners(messageEvent);
  248. }
  249. /**
  250. * Set the query generator and launch parallely query generation and query upload.
  251. **/
  252. void PIRClientSimple::startProcessQuery()
  253. {
  254. PIRQueryGenerator_internal queryGen(pirParams, *cryptoMethod);
  255. queryGen.setChosenElement(chosenElement);
  256. if(no_pipeline_mode) {
  257. std::cout << "PIRClient: Calling query generation and upload without pipelining" << std::endl;
  258. queryGen.generateQuery();
  259. } else {
  260. queryGen.startGenerateQuery();
  261. }
  262. uploadWorker(queryGen);
  263. }
  264. void sleepForBytes(unsigned int bytes) {
  265. #ifdef UPLOAD_LIMIT
  266. uint64_t seconds=(bytes*8)/UPLOAD_LIMIT;
  267. uint64_t nanoseconds=((((double)bytes*8.)/(double)UPLOAD_LIMIT)-(double)seconds)*1000000000UL;
  268. struct timespec req={0},rem={0};
  269. req.tv_sec=seconds;
  270. req.tv_nsec=nanoseconds;
  271. double st=omp_get_wtime();
  272. nanosleep(&req,&rem);
  273. #endif
  274. }
  275. /**
  276. * Upload query to the server et delete its parts from the shared query queue.
  277. * Exception :
  278. * - Stop the programme if any network trouble.
  279. **/
  280. void PIRClientSimple::uploadWorker(PIRQueryGenerator_internal& queryGen)
  281. {
  282. unsigned int size = 0;
  283. char *tmp;
  284. try
  285. {
  286. for (unsigned int j = 1 ; j <= pirParams.d ; j++)
  287. {
  288. size = cryptoMethod->getPublicParameters().getQuerySizeFromRecLvl(j) / GlobalConstant::kBitsPerByte;
  289. for (unsigned int i = 0 ; i < pirParams.n[j - 1] ; i++)
  290. {
  291. tmp = queryGen.queryBuffer.pop_front();
  292. write(socket_up, boost::asio::buffer(tmp, size));
  293. free(tmp);
  294. #ifdef UPLOAD_LIMIT
  295. sleepForBytes(size);
  296. #endif
  297. }
  298. }
  299. }
  300. catch (const std::exception& ex)
  301. {
  302. exitWithErrorMessage(__FUNCTION__, ex.what());
  303. }
  304. }
  305. /**
  306. * Sets reply extractpr and lauches parallely reply download and reply extraction.
  307. **/
  308. void PIRClientSimple::startProcessResult()
  309. {
  310. replyExt = new PIRReplyExtraction_internal(pirParams,*cryptoMethod);
  311. replyExt->setFileParam(catalog.getFileName(chosenElement), catalog.getFileSize(chosenElement));
  312. if(no_pipeline_mode)
  313. {
  314. std::cout << "PIRClient: Calling reply download, extraction and writting without pipelining" << std::endl;
  315. downloadWorker(*replyExt);
  316. replyExt->extractReply(catalog.getMaxFileSize()*pirParams.alpha, replyWriter.getClearDataQueue());
  317. // Tell reply writer we finished the extraction
  318. replyWriter.writeAggregatedFileSecurely(chosenElement, catalog);
  319. }
  320. else
  321. {
  322. replyExt->startExtractReply(catalog.getMaxFileSize()*pirParams.alpha, replyWriter.getClearDataQueue());
  323. replyWriter.startFileWritting(chosenElement, catalog);
  324. downloadWorker(*replyExt);
  325. }
  326. }
  327. /**
  328. * Download reply from the server and stores it chunks in shared replies queue.
  329. * Exception :
  330. * - Stop the program if any network trouble.
  331. **/
  332. void PIRClientSimple::downloadWorker(PIRReplyExtraction_internal& turlututu)
  333. {
  334. using namespace GlobalConstant;
  335. unsigned int i, ciph_siz = cryptoMethod->getPublicParameters().getCiphBitsizeFromRecLvl(pirParams.d)/kBitsPerByte;
  336. double paquet_nbr = ceil(static_cast<double>(catalog.getMaxFileSize()*pirParams.alpha) / double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(0)/kBitsPerByte));
  337. #ifdef DEBUG
  338. cout << "PIRClient: First layer nbr of ciphertexts is " << paquet_nbr << endl;
  339. #endif
  340. for (unsigned int i = 1 ; i < pirParams.d; i++) paquet_nbr = ceil(paquet_nbr * double(cryptoMethod->getPublicParameters().getCiphBitsizeFromRecLvl(i)/kBitsPerByte) / double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(i) / kBitsPerByte));
  341. char* buf;
  342. #ifdef DEBUG
  343. cout << "PIRClient: getMaxFileSize=" << catalog.getMaxFileSize() << " alpha=" << pirParams.alpha << " getAbsorptionBitsize=" << double(cryptoMethod->getPublicParameters().getAbsorptionBitsize(0))/*kBitsPerByte);*/ << endl;
  344. cout << "PIRClient: Last layer nbr of ciphertexts is " << paquet_nbr << endl;
  345. cout << "PIRClient: File size of the chosen element is " << catalog.getFileSize(chosenElement) << endl;
  346. cout << "PIRClient: Ciphertext bytesize is " << ciph_siz << endl;
  347. #endif
  348. try
  349. {
  350. for (i = 0 ; i < paquet_nbr ; i++)
  351. {
  352. buf = (char*) malloc(ciph_siz * sizeof(char));
  353. read(socket_up,boost::asio::buffer(buf, ciph_siz));
  354. replyExt->repliesBuffer.push(buf);
  355. }
  356. }
  357. catch (const std::exception& ex)
  358. {
  359. #ifdef DEBUG
  360. cerr << "PIRReplyWriter: Number of downloaded chunks: " << i << "/" << paquet_nbr << endl;
  361. #endif
  362. exitWithErrorMessage(__FUNCTION__, ex.what());
  363. return;
  364. }
  365. writeWarningMessage(__FUNCTION__, "done.");
  366. }
  367. /**
  368. * Sets the chosen element.
  369. **/
  370. void PIRClientSimple::setChosenElement(uint64_t choice)
  371. {
  372. chosenElement = choice;
  373. }
  374. //void PIRClientSimple::setPIRParameters(PIRParameters& pirParameters)
  375. //{
  376. // pirParams.alpha = pirParameters.alpha;
  377. // pirParams.d = pirParameters.d;
  378. // memcpy(pirParams.n, pirParameters.n, MAX_REC_LVL);
  379. // pirParams.r;
  380. //}
  381. /**
  382. * Add a new message listener.
  383. * Param :
  384. * - messageListener::slot_function_type subscriber, a message listener to add.
  385. **/
  386. boost::signals2::connection PIRClientSimple::addMessageListener(messageListener::slot_function_type subscriber)
  387. {
  388. return messageListeners.connect(subscriber);
  389. }
  390. /**
  391. * Add a new menu listener.
  392. * Param :
  393. * - menuListener::slot_function_type subscriber, a menu listener to add.
  394. **/
  395. boost::signals2::connection PIRClientSimple::addMenuListener(menuListener::slot_function_type subscriber)
  396. {
  397. return menuListeners.connect(subscriber);
  398. }
  399. /**
  400. * Add a new wite listener
  401. * Param :
  402. * - writeListener::slot_function_type subscriber, a write listener to add.
  403. **/
  404. boost::signals2::connection PIRClientSimple::addWriteListener(writeListener::slot_function_type subscriber)
  405. {
  406. return writeListeners.connect(subscriber);
  407. }
  408. void PIRClientSimple::exitWithErrorMessage(string s1, string s2)
  409. {
  410. writeErrorMessage(s1 ,s2);
  411. socket_up.close();
  412. exit(errno);
  413. }
  414. /**
  415. * Send ERROR message to listeners
  416. * Params :
  417. * - string funcName : usually the function name who throw the error message ;
  418. * - string message : message to show.
  419. **/
  420. void PIRClientSimple::writeErrorMessage(string funcName, string message)
  421. {
  422. MessageEvent event(ERROR, message, funcName);
  423. messageListeners(event);
  424. }
  425. /**
  426. * Send WARNING message to listeners
  427. * Params :
  428. * - string funcName : usually the function name who throw the warning message ;
  429. * - string message : message to show.
  430. **/
  431. void PIRClientSimple::writeWarningMessage(string funcName, string message)
  432. {
  433. MessageEvent event(WARNING, message, funcName);
  434. messageListeners(event);
  435. }
  436. void PIRClientSimple::no_pipeline(bool b)
  437. {
  438. no_pipeline_mode = b;
  439. }