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_r0_advertise_keys_input_set_state.h"
18
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "absl/container/node_hash_map.h"
26 #include "fcp/base/monitoring.h"
27 #include "fcp/secagg/client/secagg_client_aborted_state.h"
28 #include "fcp/secagg/client/secagg_client_completed_state.h"
29 #include "fcp/secagg/client/secagg_client_r1_share_keys_input_set_state.h"
30 #include "fcp/secagg/client/secagg_client_state.h"
31 #include "fcp/secagg/client/send_to_server_interface.h"
32 #include "fcp/secagg/client/state_transition_listener_interface.h"
33 #include "fcp/secagg/shared/aes_prng_factory.h"
34 #include "fcp/secagg/shared/ecdh_key_agreement.h"
35 #include "fcp/secagg/shared/input_vector_specification.h"
36 #include "fcp/secagg/shared/secagg_messages.pb.h"
37 #include "fcp/secagg/shared/secagg_vector.h"
38
39 namespace fcp {
40 namespace secagg {
41
42 SecAggClientR0AdvertiseKeysInputSetState::
SecAggClientR0AdvertiseKeysInputSetState(uint32_t max_neighbors_expected,uint32_t minimum_surviving_neighbors_for_reconstruction,std::unique_ptr<SecAggVectorMap> input_map,std::unique_ptr<std::vector<InputVectorSpecification>> input_vector_specs,std::unique_ptr<SecurePrng> prng,std::unique_ptr<SendToServerInterface> sender,std::unique_ptr<StateTransitionListenerInterface> transition_listener,std::unique_ptr<AesPrngFactory> prng_factory,AsyncAbort * async_abort)43 SecAggClientR0AdvertiseKeysInputSetState(
44 uint32_t max_neighbors_expected,
45 uint32_t minimum_surviving_neighbors_for_reconstruction,
46 std::unique_ptr<SecAggVectorMap> input_map,
47 std::unique_ptr<std::vector<InputVectorSpecification> >
48 input_vector_specs,
49 std::unique_ptr<SecurePrng> prng,
50 std::unique_ptr<SendToServerInterface> sender,
51 std::unique_ptr<StateTransitionListenerInterface> transition_listener,
52
53 std::unique_ptr<AesPrngFactory> prng_factory, AsyncAbort* async_abort)
54 : SecAggClientAliveBaseState(std::move(sender),
55 std::move(transition_listener),
56 ClientState::R0_ADVERTISE_KEYS, async_abort),
57 max_neighbors_expected_(max_neighbors_expected),
58 minimum_surviving_neighbors_for_reconstruction_(
59 minimum_surviving_neighbors_for_reconstruction),
60 input_map_(std::move(input_map)),
61 input_vector_specs_(std::move(input_vector_specs)),
62 prng_(std::move(prng)),
63 prng_factory_(std::move(prng_factory)) {}
64
65 SecAggClientR0AdvertiseKeysInputSetState::
66 ~SecAggClientR0AdvertiseKeysInputSetState() = default;
67
68 StatusOr<std::unique_ptr<SecAggClientState> >
Start()69 SecAggClientR0AdvertiseKeysInputSetState::Start() {
70 auto enc_key_agreement = EcdhKeyAgreement::CreateFromRandomKeys().value();
71 auto prng_key_agreement = EcdhKeyAgreement::CreateFromRandomKeys().value();
72
73 ClientToServerWrapperMessage message;
74 PairOfPublicKeys* public_keys =
75 message.mutable_advertise_keys()->mutable_pair_of_public_keys();
76 public_keys->set_enc_pk(enc_key_agreement->PublicKey().AsString());
77 public_keys->set_noise_pk(prng_key_agreement->PublicKey().AsString());
78
79 sender_->Send(&message);
80 return {std::make_unique<SecAggClientR1ShareKeysInputSetState>(
81 max_neighbors_expected_, minimum_surviving_neighbors_for_reconstruction_,
82 std::move(enc_key_agreement), std::move(input_map_),
83 std::move(input_vector_specs_), std::move(prng_),
84 std::move(prng_key_agreement), std::move(sender_),
85 std::move(transition_listener_), std::move(prng_factory_), async_abort_)};
86 }
87
88 StatusOr<std::unique_ptr<SecAggClientState> >
HandleMessage(const ServerToClientWrapperMessage & message)89 SecAggClientR0AdvertiseKeysInputSetState::HandleMessage(
90 const ServerToClientWrapperMessage& message) {
91 // Handle abort messages only.
92 if (message.has_abort()) {
93 if (message.abort().early_success()) {
94 return {std::make_unique<SecAggClientCompletedState>(
95 std::move(sender_), std::move(transition_listener_))};
96 } else {
97 return {std::make_unique<SecAggClientAbortedState>(
98 "Aborting because of abort message from the server.",
99 std::move(sender_), std::move(transition_listener_))};
100 }
101 } else {
102 // Returns an error indicating that the message is of invalid type.
103 return SecAggClientState::HandleMessage(message);
104 }
105 }
106
StateName() const107 std::string SecAggClientR0AdvertiseKeysInputSetState::StateName() const {
108 return "R0_ADVERTISE_KEYS_INPUT_SET";
109 }
110
111 } // namespace secagg
112 } // namespace fcp
113