toyserver.cc 2.8 KB

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