networkServer.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef __PRSONA_NETWORK_SERVER_HPP
  2. #define __PRSONA_NETWORK_SERVER_HPP
  3. #include <string>
  4. #include <sstream>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include "server.hpp"
  8. #include "CivetServer.h"
  9. #define MG_WEBSOCKET_OPCODE_DATACOMPLETE 0xb
  10. #define UPDATE_LOCK_URI "/lock"
  11. #define UPDATE_UNLOCK_URI "/unlock"
  12. #define GIVE_NEW_VOTE_URI "/ws?b"
  13. #define ACCEPT_EPOCH_UPDATES_URI "/ws?w"
  14. #define GIVE_NEW_USER_URI "/ws?x"
  15. #define PRSONA_PORT 8080
  16. #define USE_SSL 0
  17. enum RequestType {
  18. PRSONA_ADD_CLIENT = 'a',
  19. PRSONA_RECEIVE_VOTE,
  20. PRSONA_GET_BGN_PUBKEY,
  21. PRSONA_GET_NUM_CLIENTS,
  22. PRSONA_GET_NUM_SERVERS,
  23. PRSONA_GET_VOTES_BY,
  24. PRSONA_GET_ALL_VOTES,
  25. PRSONA_GET_USER_TALLY,
  26. PRSONA_GET_SERVER_TALLY,
  27. PRSONA_GET_PSEUDONYMS,
  28. PRSONA_GET_VOTE_ROW_COMMITMENT,
  29. PRSONA_GET_VOTE_MATRIX_COMMITMENT,
  30. PRSONA_GET_USER_TALLY_COMMITMENT,
  31. PRSONA_GET_SERVER_TALLY_COMMITMENT,
  32. PRSONA_GET_PSEUDONYMS_COMMITMENT,
  33. PRSONA_GET_BGN_DETAILS,
  34. PRSONA_ADD_CURR_SEED_TO_GENERATOR,
  35. PRSONA_SET_FRESH_GENERATOR,
  36. PRSONA_ADD_RAND_SEED_TO_GENERATOR,
  37. PRSONA_SET_EG_BLIND_GENERATOR,
  38. PRSONA_EPOCH_BUILD_UP,
  39. PRSONA_EPOCH_BREAK_DOWN,
  40. PRSONA_EPOCH_UPDATE,
  41. PRSONA_NEW_USER_UPDATE,
  42. PRSONA_GET_PARTIAL_DECRYPTION,
  43. PRSONA_RECEIVE_PARTIAL_DECRYPTION,
  44. PRSONA_GET_FRESH_GENERATOR = '-',
  45. PRSONA_GET_EG_BLIND_GENERATOR ='_',
  46. PRSONA_RECEIVE_FRESH_GENERATOR = 'a',
  47. PRSONA_RECEIVE_VOTE_TALLY,
  48. PRSONA_RECEIVE_NEW_USER_DATA,
  49. PRSONA_VERIFY_REPUTATION_PROOF
  50. };
  51. struct synchronization_tool {
  52. std::mutex mtx;
  53. std::condition_variable cv;
  54. size_t val, val2;
  55. };
  56. static int synchro_websocket_data_handler(
  57. struct mg_connection *conn,
  58. int bits,
  59. char *data,
  60. size_t data_len,
  61. void *user_data);
  62. static void synchro_websocket_close_handler(
  63. const struct mg_connection *conn,
  64. void *user_data);
  65. static int empty_websocket_data_handler(
  66. struct mg_connection *conn,
  67. int bits,
  68. char *data,
  69. size_t data_len,
  70. void *user_data)
  71. { return false; }
  72. static void empty_websocket_close_handler(
  73. const struct mg_connection *conn,
  74. void *user_data)
  75. { /* */ }
  76. void obtain_update_locks(
  77. std::unique_lock<std::mutex> &lck,
  78. const std::vector<std::string>& serverIPs,
  79. const std::string& selfIP,
  80. struct synchronization_tool *synch);
  81. void release_update_locks(
  82. std::unique_lock<std::mutex> &lck,
  83. const std::vector<std::string>& serverIPs,
  84. const std::string& selfIP,
  85. struct synchronization_tool *synch);
  86. std::string make_epoch_initiator_string(
  87. std::vector<Proof> generatorProof,
  88. Twistpoint nextGenerator);
  89. std::string make_epoch_update_string(
  90. std::vector<std::vector<Proof>> pi,
  91. std::vector<std::vector<Twistpoint>> permutationCommits,
  92. std::vector<std::vector<Twistpoint>> freshPseudonymCommits,
  93. std::vector<std::vector<Twistpoint>> freshPseudonymSeedCommits,
  94. std::vector<std::vector<CurveBipoint>> serverTallyCommits,
  95. std::vector<std::vector<std::vector<TwistBipoint>>> partwayVoteMatrixCommits,
  96. std::vector<std::vector<std::vector<TwistBipoint>>> finalVoteMatrixCommits,
  97. std::vector<std::vector<Twistpoint>> userTallyMaskCommits,
  98. std::vector<std::vector<Twistpoint>> userTallyMessageCommits,
  99. std::vector<std::vector<Twistpoint>> userTallySeedCommits,
  100. Twistpoint nextGenerator,
  101. bool doUserTallies);
  102. class PrsonaServerWebSocketHandler : public CivetWebSocketHandler {
  103. public:
  104. // CONSTRUCTORS
  105. PrsonaServerWebSocketHandler(
  106. const PrsonaServer *prsonaServer,
  107. const std::mutex *updateMtx,
  108. const std::vector<std::string> serverIPs,
  109. const std::string selfIP);
  110. virtual bool handleConnection(
  111. CivetServer *server,
  112. const struct mg_connection *conn);
  113. virtual void handleReadyState(
  114. CivetServer *server,
  115. struct mg_connection *conn);
  116. virtual bool handleData(
  117. CivetServer *server,
  118. struct mg_connection *conn,
  119. int bits,
  120. char *data,
  121. size_t data_len);
  122. virtual void handleClose(
  123. CivetServer *server,
  124. const struct mg_connection *conn);
  125. private:
  126. const PrsonaServer *prsonaServer;
  127. const std::mutex *updateMtx;
  128. const size_t *epochNum;
  129. const std::vector<std::string> serverIPs;
  130. const std::string selfIP;
  131. struct synchronization_tool updateSynch, distributeSynch;
  132. // BASIC PUBLIC SYSTEM INFO GETTERS
  133. void get_bgn_public_key(struct mg_connection *c) const;
  134. void get_num_clients(struct mg_connection *c) const;
  135. void get_num_servers(struct mg_connection *c) const;
  136. // ENCRYPTED DATA GETTERS
  137. void get_current_votes_by(struct mg_connection *c) const;
  138. void get_all_current_votes(struct mg_connection *c) const;
  139. void get_current_user_encrypted_tally(struct mg_connection *c) const;
  140. void get_current_server_encrypted_tally(struct mg_connection *c) const;
  141. void get_current_pseudonyms(struct mg_connection *c) const;
  142. // PROOF COMMITMENT GETTERS
  143. void get_vote_row_commitment(struct mg_connection *c) const;
  144. void get_vote_matrix_commitment(struct mg_connection *c) const;
  145. void get_user_tally_commitment(struct mg_connection *c) const;
  146. void get_server_tally_commitment(struct mg_connection *c) const;
  147. void get_pseudonyms_commitment(struct mg_connection *c) const;
  148. // CLIENT INTERACTIONS
  149. void add_new_client(struct mg_connection *c);
  150. void receive_vote(struct mg_connection *c);
  151. // CONSTRUCTOR HELPERS
  152. void get_bgn_details(struct mg_connection *c) const;
  153. void initialize_fresh_generator(struct mg_connection *c);
  154. void add_rand_seed_to_generator(struct mg_connection *c) const;
  155. void set_EG_blind_generator(struct mg_connection *c);
  156. // EPOCH ROUNDS
  157. void build_up_midway_pseudonyms(struct mg_connection *c);
  158. void break_down_midway_pseudonyms(struct mg_connection *c);
  159. void accept_epoch_updates(struct mg_connection *c);
  160. // DATA MAINTENANCE
  161. void import_new_user_update(struct mg_connection *c);
  162. };
  163. #endif