toyclient.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "pirclient.h"
  2. // This is a toy "private" (not really private) lookup protocol.
  3. // It is only here to show you how to implement a subclass of PIRClient.
  4. // Do not use this code for anything requiring actual privacy.
  5. //
  6. // The params are a random 32-byte string. Lookups are done by
  7. // "encrypting" the lookup key by XORing it with the param string.
  8. // The reply is XORed with repeated copies of the same string.
  9. class ToyClient : public PIRClient {
  10. public:
  11. ToyClient();
  12. // Create a PIR query. The plainquery must be exactly 32 bytes
  13. // long.
  14. virtual void create(const string &plainquery, const string &params,
  15. void *&queryid, string &pirquery);
  16. // Extract the plaintext response from a PIR response. Returns
  17. // true if successful, false if unsuccessful.
  18. virtual bool extract(void *&queryid, const string &pirresponse,
  19. string &plainresponse);
  20. };
  21. ToyClient::ToyClient()
  22. {
  23. }
  24. // Put anything you'll need to decrypt the response in here
  25. struct Decryptstate {
  26. string decryptkey;
  27. };
  28. void
  29. ToyClient::create(const string &plainquery, const string &params,
  30. void *&queryid, string &pirquery)
  31. {
  32. if (plainquery.length() == 32 && params.length() == 32) {
  33. pirquery.assign(plainquery);
  34. for (size_t i=0;i<32;++i) {
  35. pirquery[i] ^= params[i];
  36. }
  37. Decryptstate *ds = new Decryptstate();
  38. ds->decryptkey.assign(params);
  39. queryid = ds;
  40. }
  41. }
  42. bool
  43. ToyClient::extract(void *&queryid, const string &pirresponse,
  44. string &plainresponse)
  45. {
  46. Decryptstate *ds = (Decryptstate *)queryid;
  47. size_t response_size = pirresponse.length();
  48. plainresponse.assign(pirresponse);
  49. for(size_t i=0;i<response_size;++i) {
  50. plainresponse[i] ^= ds->decryptkey[i % 32];
  51. }
  52. delete ds;
  53. queryid = NULL;
  54. return true;
  55. }
  56. int main(int argc, char **argv) {
  57. ToyClient client;
  58. client.mainloop();
  59. return 0;
  60. }