aboutsummaryrefslogtreecommitdiff
path: root/fcp/secagg/client/secagg_client_r0_advertise_keys_input_not_set_state.cc
blob: bdd33d11e2f71c8e30b69e006d3864d620cb3f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "fcp/secagg/client/secagg_client_r0_advertise_keys_input_not_set_state.h"

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/container/node_hash_map.h"
#include "fcp/base/monitoring.h"
#include "fcp/secagg/client/secagg_client_aborted_state.h"
#include "fcp/secagg/client/secagg_client_completed_state.h"
#include "fcp/secagg/client/secagg_client_r0_advertise_keys_input_set_state.h"
#include "fcp/secagg/client/secagg_client_r1_share_keys_input_not_set_state.h"
#include "fcp/secagg/client/secagg_client_state.h"
#include "fcp/secagg/client/send_to_server_interface.h"
#include "fcp/secagg/client/state_transition_listener_interface.h"
#include "fcp/secagg/shared/aes_prng_factory.h"
#include "fcp/secagg/shared/ecdh_key_agreement.h"
#include "fcp/secagg/shared/input_vector_specification.h"
#include "fcp/secagg/shared/secagg_messages.pb.h"
#include "fcp/secagg/shared/secagg_vector.h"

namespace fcp {
namespace secagg {

SecAggClientR0AdvertiseKeysInputNotSetState::
    SecAggClientR0AdvertiseKeysInputNotSetState(
        uint32_t max_neighbors_expected,
        uint32_t minimum_surviving_neighbors_for_reconstruction,
        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)
    : SecAggClientAliveBaseState(std::move(sender),
                                 std::move(transition_listener),
                                 ClientState::R0_ADVERTISE_KEYS, async_abort),
      max_neighbors_expected_(max_neighbors_expected),
      minimum_surviving_neighbors_for_reconstruction_(
          minimum_surviving_neighbors_for_reconstruction),
      input_vector_specs_(std::move(input_vector_specs)),
      prng_(std::move(prng)),
      prng_factory_(std::move(prng_factory)) {}

SecAggClientR0AdvertiseKeysInputNotSetState::
    ~SecAggClientR0AdvertiseKeysInputNotSetState() = default;

StatusOr<std::unique_ptr<SecAggClientState> >
SecAggClientR0AdvertiseKeysInputNotSetState::Start() {
  auto enc_key_agreement = EcdhKeyAgreement::CreateFromRandomKeys().value();
  auto prng_key_agreement = EcdhKeyAgreement::CreateFromRandomKeys().value();

  ClientToServerWrapperMessage message;
  PairOfPublicKeys* public_keys =
      message.mutable_advertise_keys()->mutable_pair_of_public_keys();
  public_keys->set_enc_pk(enc_key_agreement->PublicKey().AsString());
  public_keys->set_noise_pk(prng_key_agreement->PublicKey().AsString());

  sender_->Send(&message);
  return {std::make_unique<SecAggClientR1ShareKeysInputNotSetState>(
      max_neighbors_expected_, minimum_surviving_neighbors_for_reconstruction_,
      std::move(enc_key_agreement), std::move(input_vector_specs_),
      std::move(prng_), std::move(prng_key_agreement), std::move(sender_),
      std::move(transition_listener_), std::move(prng_factory_), async_abort_)};
}

StatusOr<std::unique_ptr<SecAggClientState> >
SecAggClientR0AdvertiseKeysInputNotSetState::HandleMessage(
    const ServerToClientWrapperMessage& message) {
  // Handle abort messages only.
  if (message.has_abort()) {
    if (message.abort().early_success()) {
      return {std::make_unique<SecAggClientCompletedState>(
          std::move(sender_), std::move(transition_listener_))};
    } else {
      return {std::make_unique<SecAggClientAbortedState>(
          "Aborting because of abort message from the server.",
          std::move(sender_), std::move(transition_listener_))};
    }
  } else {
    // Returns an error indicating that the message is of invalid type.
    return SecAggClientState::HandleMessage(message);
  }
}

StatusOr<std::unique_ptr<SecAggClientState> >
SecAggClientR0AdvertiseKeysInputNotSetState::SetInput(
    std::unique_ptr<SecAggVectorMap> input_map) {
  if (!ValidateInput(*input_map, *input_vector_specs_)) {
    return FCP_STATUS(INVALID_ARGUMENT)
           << "The input to SetInput does not match the "
              "InputVectorSpecification.";
  }

  return {std::make_unique<SecAggClientR0AdvertiseKeysInputSetState>(
      max_neighbors_expected_, minimum_surviving_neighbors_for_reconstruction_,
      std::move(input_map), std::move(input_vector_specs_), std::move(prng_),
      std::move(sender_), std::move(transition_listener_),
      std::move(prng_factory_), async_abort_)};
}

std::string SecAggClientR0AdvertiseKeysInputNotSetState::StateName() const {
  return "R0_ADVERTISE_KEYS_INPUT_NOT_SET";
}

}  // namespace secagg
}  // namespace fcp