PIRReplyWriter.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. tmp = clearChunks.front();
  108. if (bytestoskip != 0)
  109. {
  110. file.write(tmp+bytestoskip , min(leftChars, chunkSize - bytestoskip));
  111. writtenchars = min(leftChars, chunkSize - bytestoskip);
  112. bytestoskip = chunkSize - (writtenchars+bytestoskip);
  113. if (bytestoskip < 0) std::cout << "PIRReplyWriter: Skipping a negative amount of bytes THIS SHOULD NOT HAPPEN" << std::endl;
  114. }
  115. else
  116. {
  117. file.write(tmp , min(leftChars, chunkSize));
  118. writtenchars = min(leftChars, chunkSize);
  119. if (writtenchars < chunkSize ) bytestoskip = writtenchars;
  120. }
  121. if (file.fail())
  122. {
  123. MessageEvent mEvent(WARNING,"FAIL DURING FILE WRITTING");
  124. messageListeners(mEvent);
  125. file.close();
  126. exit(1);
  127. }
  128. file.flush();
  129. // Shall we reuse this clear chunk ?
  130. if ( bytestoskip == 0 )
  131. {
  132. clearChunks.pop_front();
  133. free(tmp);
  134. }
  135. event.addtoWrittenSize(writtenchars);
  136. writeListeners(event);
  137. leftChars -= writtenchars;
  138. }
  139. file.close();
  140. }
  141. else
  142. {
  143. MessageEvent event(ERROR, "PIRReplyWriter: ERROR WHEN WRITING FILE ! Maybe " + completeFilename + " is write protected?", __FUNCTION__);
  144. messageListeners(event);
  145. }
  146. }
  147. void PIRReplyWriter::startFileWritting(uint64_t chosenElement, DESC catalog)
  148. {
  149. writeThread = boost::thread(&PIRReplyWriter::writeAggregatedFileSecurely, this, chosenElement, catalog);
  150. }
  151. shared_queue<char*>* PIRReplyWriter::getClearDataQueue()
  152. {
  153. return &clearChunks;
  154. }
  155. void PIRReplyWriter::setCryptoMethod(HomomorphicCrypto* crypto_method)
  156. {
  157. cryptoMethod = crypto_method;
  158. }
  159. void PIRReplyWriter::join()
  160. {
  161. if(writeThread.joinable()) writeThread.join();
  162. }
  163. PIRReplyWriter::~PIRReplyWriter()
  164. {
  165. join();
  166. }