PIRReplyWriter.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "PIRReplyWriter.hpp"
  18. const std::string PIRReplyWriter::kDefaultFolder("reception");
  19. PIRReplyWriter::PIRReplyWriter(PIRParameters& param, boost::signals2::signal<void (WriteEvent&)> &writeListeners_, boost::signals2::signal<void (MessageEvent&)> &messageListeners_) :
  20. filePath(kDefaultFolder),
  21. pirParams(param),
  22. clearChunks("clear_chunks"),
  23. writeListeners(writeListeners_),
  24. messageListeners(messageListeners_),
  25. dontWrite(false)
  26. {
  27. ;//Marco's boost signals fix ! :-D
  28. }
  29. void PIRReplyWriter::setdontWrite(bool newvalue) { dontWrite = newvalue; }
  30. void PIRReplyWriter::writeAggregatedFileSecurely(uint64_t chosenElement, DESC catalog)
  31. {
  32. uint64_t firstElement = chosenElement - chosenElement % pirParams.alpha;
  33. uint64_t lastElement = firstElement + pirParams.alpha - 1;
  34. uint64_t bytestoskip = 0;
  35. char *tmp;
  36. uint64_t chunkSize = cryptoMethod->getPublicParameters().getAbsorptionBitsize()
  37. / GlobalConstant::kBitsPerByte;
  38. WriteEvent event(catalog.getMaxFileSize(), 0);
  39. if (pirParams.alpha > 1) std::cout << "PIRReplyWriter: Dealing with " << pirParams.alpha << " aggregated files" << std::endl;
  40. if (chosenElement != firstElement) std::cout << "PIRReplyWriter: Skipping aggregated files before chosen element ..." << std::endl;
  41. // To avoid timing attacks write down all the elements that were aggregated
  42. for (uint64_t i = firstElement; i <= lastElement; i++)
  43. {
  44. #ifdef MORE_SIDE_CHANNEL_RESISTANCE
  45. writeFileSecurely(i, catalog, bytestoskip, event);
  46. #else
  47. if (i == chosenElement && dontWrite == false)
  48. {
  49. writeFileSecurely(i, catalog, bytestoskip, event);
  50. }
  51. else
  52. {
  53. bytestoskip += catalog.getMaxFileSize();
  54. }
  55. #endif
  56. }
  57. if (chosenElement != lastElement) std::cout << "PIRReplyWriter: Skipping aggregated files after chosen element ..." << std::endl;
  58. // Empty clearchunk queue and say we finished
  59. std::cout << "PIRReplyWriter: Emptying queue ..." << std::endl;
  60. while (bytestoskip > chunkSize)
  61. {
  62. tmp = clearChunks.pop_front();
  63. free(tmp);
  64. bytestoskip -= chunkSize;
  65. }
  66. // ... And remove all of them but the element we are interested in
  67. if (truncate(std::string(filePath + "/" + catalog.getFileName(chosenElement)).c_str(),
  68. catalog.getFileSize(chosenElement)))
  69. {
  70. std::cout << "PIRReplyWriter: Unable to truncate retrieved file" << std::endl;
  71. }
  72. #ifdef MORE_SIDE_CHANNEL_RESISTANCE
  73. for (uint64_t i = firstElement; i <= lastElement; i++)
  74. {
  75. if ( i != chosenElement )
  76. {
  77. std::remove(std::string(filePath + "/" + catalog.getFileName(i)).c_str());
  78. }
  79. }
  80. #endif
  81. std::cout << "PIRReplyWriter: FINISHED (This should end the client)" << std::endl << std::endl;
  82. }
  83. void PIRReplyWriter::writeFileSecurely(uint64_t element, DESC catalog, uint64_t &bytestoskip, WriteEvent &event)
  84. {
  85. std::string completeFilename(filePath + "/" + catalog.getFileName(element));
  86. std::ofstream file(completeFilename.c_str(), ios::out | ios::binary | ios::trunc);
  87. if (file.good())
  88. {
  89. char *tmp;
  90. uint64_t chunkSize = cryptoMethod->getPublicParameters().getAbsorptionBitsize() / GlobalConstant::kBitsPerByte;
  91. uint64_t leftChars = catalog.getMaxFileSize();
  92. #ifdef DEBUG
  93. cout << "PIRReplyWriter: Size of the requested file is " << leftChars << endl;
  94. cout << "PIRReplyWriter: chunkSize is " << chunkSize << endl;
  95. #endif
  96. while (bytestoskip > chunkSize)
  97. {
  98. tmp = clearChunks.pop_front();
  99. free(tmp);
  100. bytestoskip -= chunkSize;
  101. event.addtoWrittenSize(chunkSize);
  102. }
  103. writeListeners(event);
  104. while (leftChars != 0)
  105. {
  106. uint64_t writtenchars = 0;
  107. uint64_t current_bytesskipped = 0;
  108. tmp = clearChunks.front();
  109. if (bytestoskip != 0)
  110. {
  111. file.write(tmp+bytestoskip , min(leftChars, chunkSize - bytestoskip));
  112. writtenchars = min(leftChars, chunkSize - bytestoskip);
  113. bytestoskip = chunkSize - (writtenchars+bytestoskip);
  114. if (bytestoskip < 0) std::cout << "PIRReplyWriter: Skipping a negative amount of bytes THIS SHOULD NOT HAPPEN" << std::endl;
  115. }
  116. else
  117. {
  118. file.write(tmp , min(leftChars, chunkSize));
  119. writtenchars = min(leftChars, chunkSize);
  120. if (writtenchars < chunkSize ) bytestoskip = writtenchars;
  121. }
  122. if (file.fail())
  123. {
  124. MessageEvent mEvent(WARNING,"FAIL DURING FILE WRITTING");
  125. messageListeners(mEvent);
  126. file.close();
  127. exit(1);
  128. }
  129. file.flush();
  130. // Shall we reuse this clear chunk ?
  131. if ( bytestoskip == 0 )
  132. {
  133. clearChunks.pop_front();
  134. free(tmp);
  135. }
  136. event.addtoWrittenSize(writtenchars);
  137. writeListeners(event);
  138. leftChars -= writtenchars;
  139. }
  140. file.close();
  141. }
  142. else
  143. {
  144. MessageEvent event(ERROR, "PIRReplyWriter: ERROR WHEN WRITING FILE ! Maybe " + completeFilename + " is write protected?", __FUNCTION__);
  145. messageListeners(event);
  146. }
  147. }
  148. void PIRReplyWriter::startFileWritting(uint64_t chosenElement, DESC catalog)
  149. {
  150. writeThread = boost::thread(&PIRReplyWriter::writeAggregatedFileSecurely, this, chosenElement, catalog);
  151. }
  152. shared_queue<char*>* PIRReplyWriter::getClearDataQueue()
  153. {
  154. return &clearChunks;
  155. }
  156. void PIRReplyWriter::setCryptoMethod(HomomorphicCrypto* crypto_method)
  157. {
  158. cryptoMethod = crypto_method;
  159. }
  160. void PIRReplyWriter::join()
  161. {
  162. if(writeThread.joinable()) writeThread.join();
  163. }
  164. PIRReplyWriter::~PIRReplyWriter()
  165. {
  166. join();
  167. }