toyserver.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <iostream>
  8. #include <map>
  9. using std::map;
  10. #include "pirserver.h"
  11. // This is a toy "private" (not really private) lookup protocol.
  12. // It is only here to show you how to implement a subclass of PIRServer.
  13. // Do not use this code for anything requiring actual privacy.
  14. //
  15. // The params are a random 32-byte string. Lookups are done by
  16. // "encrypting" the lookup key by XORing it with the param string.
  17. // The reply is XORed with repeated copies of the same string.
  18. class ToyServer : public PIRServer {
  19. private:
  20. string xorkey;
  21. map<string, string> table;
  22. public:
  23. ToyServer();
  24. // Fill the current params into the passed string argument
  25. virtual void get_params(string &params);
  26. // Store the given value at the given key. If the value is the
  27. // empty string, delete the key. If the key has already been
  28. // stored, overwrite the value with this one. The key will be
  29. // exactly 32 bytes long.
  30. virtual void store(const string &key, const string &value);
  31. // Perform a private lookup. The client's private query message is
  32. // lookup_query. If successful, return true and fill
  33. // lookup_response with the private response. If unsuccessful,
  34. // return false.
  35. virtual bool lookup(const string &lookup_query,
  36. string &lookup_response);
  37. };
  38. ToyServer::ToyServer()
  39. {
  40. // Initalize xorkey with a 32-byte random string
  41. char randstr[32];
  42. int rfd = open("/dev/urandom", O_RDONLY);
  43. if (rfd < 0 || read(rfd, randstr, 32) < 32) {
  44. // Can't even read random data?
  45. perror("reading random string");
  46. exit(1);
  47. }
  48. close(rfd);
  49. xorkey.assign(randstr, 32);
  50. }
  51. void
  52. ToyServer::get_params(string &params)
  53. {
  54. params.assign(xorkey);
  55. }
  56. void
  57. ToyServer::store(const string &key, const string &value)
  58. {
  59. if (value.length() > 0) {
  60. table[key] = value;
  61. } else {
  62. table.erase(key);
  63. }
  64. std::cerr << "stored (" << key.length() << ") " << key << "\n";
  65. }
  66. bool
  67. ToyServer::lookup(const string &lookup_query, string &lookup_response)
  68. {
  69. if (lookup_query.length() != 32) {
  70. return false;
  71. }
  72. // Decrypt the query
  73. string plain_query(lookup_query);
  74. for (size_t i=0;i<32;++i) {
  75. plain_query[i] ^= xorkey[i];
  76. }
  77. // Do the lookup
  78. map<string,string>::const_iterator iter = table.find(plain_query);
  79. if (iter != table.end()) {
  80. lookup_response.assign(iter->second);
  81. // Encrypt the result
  82. size_t response_size = lookup_response.length();
  83. for (size_t i=0;i<response_size;++i) {
  84. lookup_response[i] ^= xorkey[i % 32];
  85. }
  86. return true;
  87. }
  88. return false;
  89. }
  90. int main(int argc, char **argv) {
  91. ToyServer server;
  92. server.mainloop();
  93. return 0;
  94. }