• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "fcp/secagg/client/secagg_client_r1_share_keys_input_set_state.h"
18 
19 #include <cstdint>
20 #include <string>
21 #include <utility>
22 
23 #include "fcp/base/monitoring.h"
24 #include "fcp/secagg/client/other_client_state.h"
25 #include "fcp/secagg/client/secagg_client_aborted_state.h"
26 #include "fcp/secagg/client/secagg_client_completed_state.h"
27 #include "fcp/secagg/client/secagg_client_r2_masked_input_coll_input_set_state.h"
28 #include "fcp/secagg/client/secagg_client_state.h"
29 #include "fcp/secagg/client/send_to_server_interface.h"
30 #include "fcp/secagg/client/state_transition_listener_interface.h"
31 #include "fcp/secagg/shared/aes_prng_factory.h"
32 #include "fcp/secagg/shared/ecdh_key_agreement.h"
33 #include "fcp/secagg/shared/input_vector_specification.h"
34 #include "fcp/secagg/shared/secagg_vector.h"
35 #include "fcp/secagg/shared/shamir_secret_sharing.h"
36 
37 namespace fcp {
38 namespace secagg {
SecAggClientR1ShareKeysInputSetState(uint32_t max_neighbors_expected,uint32_t minimum_surviving_neighbors_for_reconstruction,std::unique_ptr<EcdhKeyAgreement> enc_key_agreement,std::unique_ptr<SecAggVectorMap> input_map,std::unique_ptr<std::vector<InputVectorSpecification>> input_vector_specs,std::unique_ptr<SecurePrng> prng,std::unique_ptr<EcdhKeyAgreement> prng_key_agreement,std::unique_ptr<SendToServerInterface> sender,std::unique_ptr<StateTransitionListenerInterface> transition_listener,std::unique_ptr<AesPrngFactory> prng_factory,AsyncAbort * async_abort)39 SecAggClientR1ShareKeysInputSetState::SecAggClientR1ShareKeysInputSetState(
40     uint32_t max_neighbors_expected,
41     uint32_t minimum_surviving_neighbors_for_reconstruction,
42     std::unique_ptr<EcdhKeyAgreement> enc_key_agreement,
43     std::unique_ptr<SecAggVectorMap> input_map,
44     std::unique_ptr<std::vector<InputVectorSpecification> > input_vector_specs,
45     std::unique_ptr<SecurePrng> prng,
46     std::unique_ptr<EcdhKeyAgreement> prng_key_agreement,
47     std::unique_ptr<SendToServerInterface> sender,
48     std::unique_ptr<StateTransitionListenerInterface> transition_listener,
49     std::unique_ptr<AesPrngFactory> prng_factory, AsyncAbort* async_abort)
50     : SecAggClientR1ShareKeysBaseState(
51           std::move(sender), std::move(transition_listener), async_abort),
52       max_neighbors_expected_(max_neighbors_expected),
53       minimum_surviving_neighbors_for_reconstruction_(
54           minimum_surviving_neighbors_for_reconstruction),
55       enc_key_agreement_(std::move(enc_key_agreement)),
56       input_map_(std::move(input_map)),
57       input_vector_specs_(std::move(input_vector_specs)),
58       prng_(std::move(prng)),
59       prng_key_agreement_(std::move(prng_key_agreement)),
60       prng_factory_(std::move(prng_factory)) {}
61 
62 StatusOr<std::unique_ptr<SecAggClientState> >
HandleMessage(const ServerToClientWrapperMessage & message)63 SecAggClientR1ShareKeysInputSetState::HandleMessage(
64     const ServerToClientWrapperMessage& message) {
65   // Handle abort messages or share keys requests only.
66   if (message.has_abort()) {
67     if (message.abort().early_success()) {
68       return {std::make_unique<SecAggClientCompletedState>(
69           std::move(sender_), std::move(transition_listener_))};
70     } else {
71       return {std::make_unique<SecAggClientAbortedState>(
72           "Aborting because of abort message from the server.",
73           std::move(sender_), std::move(transition_listener_))};
74     }
75   } else if (!message.has_share_keys_request()) {
76     // Returns an error indicating that the message is of invalid type.
77     return SecAggClientState::HandleMessage(message);
78   }
79   uint32_t client_id;
80   uint32_t number_of_alive_clients;
81   uint32_t number_of_clients;
82   std::string error_message;
83   auto other_client_enc_keys = std::make_unique<std::vector<AesKey> >();
84   auto other_client_prng_keys = std::make_unique<std::vector<AesKey> >();
85   auto other_client_states = std::make_unique<std::vector<OtherClientState> >();
86   auto own_self_key_share = std::make_unique<ShamirShare>();
87   auto session_id = std::make_unique<SessionId>();
88 
89   uint8_t self_prng_key_buffer[AesKey::kSize];
90   for (uint8_t& i : self_prng_key_buffer) {
91     i = prng_->Rand8();
92   }
93   auto self_prng_key = std::make_unique<AesKey>(self_prng_key_buffer);
94 
95   bool success = HandleShareKeysRequest(
96       message.share_keys_request(), *enc_key_agreement_,
97       max_neighbors_expected_, minimum_surviving_neighbors_for_reconstruction_,
98       *prng_key_agreement_, *self_prng_key, prng_.get(), &client_id,
99       &error_message, &number_of_alive_clients, &number_of_clients,
100       other_client_enc_keys.get(), other_client_prng_keys.get(),
101       other_client_states.get(), &self_prng_key_shares_,
102       &pairwise_prng_key_shares_, session_id.get());
103 
104   if (!success) {
105     return AbortAndNotifyServer(error_message);
106   }
107 
108   if (async_abort_ && async_abort_->Signalled()) {
109     return AbortAndNotifyServer(async_abort_->Message());
110   }
111 
112   if (!EncryptAndSendResponse(*other_client_enc_keys, pairwise_prng_key_shares_,
113                               self_prng_key_shares_, sender_.get())) {
114     return AbortAndNotifyServer(async_abort_->Message());
115   }
116 
117   *own_self_key_share = self_prng_key_shares_[client_id];
118   return {std::make_unique<SecAggClientR2MaskedInputCollInputSetState>(
119       client_id, minimum_surviving_neighbors_for_reconstruction_,
120       number_of_alive_clients, number_of_clients, std::move(input_map_),
121       std::move(input_vector_specs_), std::move(other_client_states),
122       std::move(other_client_enc_keys), std::move(other_client_prng_keys),
123       std::move(own_self_key_share), std::move(self_prng_key),
124       std::move(sender_), std::move(transition_listener_),
125       std::move(session_id), std::move(prng_factory_), async_abort_)};
126 }
127 
StateName() const128 std::string SecAggClientR1ShareKeysInputSetState::StateName() const {
129   return "R1_SHARE_KEYS_INPUT_SET";
130 }
131 
132 }  // namespace secagg
133 }  // namespace fcp
134