xref: /aosp_15_r20/external/tink/cc/internal/proto_parameters_serialization.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_INTERNAL_PROTO_PARAMETERS_SERIALIZATION_H_
18 #define TINK_INTERNAL_PROTO_PARAMETERS_SERIALIZATION_H_
19 
20 #include <string>
21 
22 #include "absl/strings/string_view.h"
23 #include "tink/internal/serialization.h"
24 #include "tink/util/statusor.h"
25 #include "proto/tink.pb.h"
26 
27 namespace crypto {
28 namespace tink {
29 namespace internal {
30 
31 // Represents a `Parameters` object serialized with binary protocol buffer
32 // serialization.
33 class ProtoParametersSerialization : public Serialization {
34  public:
35   // Copyable and movable.
36   ProtoParametersSerialization(const ProtoParametersSerialization& other) =
37       default;
38   ProtoParametersSerialization& operator=(
39       const ProtoParametersSerialization& other) = default;
40   ProtoParametersSerialization(ProtoParametersSerialization&& other) = default;
41   ProtoParametersSerialization& operator=(
42       ProtoParametersSerialization&& other) = default;
43 
44   // Creates a `ProtoParametersSerialization` object from individual components.
45   static util::StatusOr<ProtoParametersSerialization> Create(
46       absl::string_view type_url,
47       google::crypto::tink::OutputPrefixType output_prefix_type,
48       absl::string_view serialized_proto);
49 
50   // Creates a `ProtoParametersSerialization` object from a key template.
51   static util::StatusOr<ProtoParametersSerialization> Create(
52       google::crypto::tink::KeyTemplate key_template);
53 
GetKeyTemplate()54   const google::crypto::tink::KeyTemplate& GetKeyTemplate() const {
55     return key_template_;
56   }
57 
ObjectIdentifier()58   absl::string_view ObjectIdentifier() const override {
59     return object_identifier_;
60   }
61 
62  private:
63   // The following friend classes require access to
64   // `EqualsWithPotentialFalseNegatives()`.
65   friend class ProtoParametersSerializationTest;
66   friend class LegacyProtoParameters;
67   friend class LegacyProtoParametersTest;
68 
ProtoParametersSerialization(google::crypto::tink::KeyTemplate key_template)69   explicit ProtoParametersSerialization(
70       google::crypto::tink::KeyTemplate key_template)
71       : key_template_(key_template),
72         object_identifier_(key_template.type_url()) {}
73 
74   // Returns `true` if this `ProtoParametersSerialization` object is equal to
75   // `other` (with the possibility of false negatives due to lack of
76   // determinism during serialization).  Should only be used temporarily by the
77   // `LegacyProtoParameters` class.
78   bool EqualsWithPotentialFalseNegatives(
79       const ProtoParametersSerialization& other) const;
80 
81   google::crypto::tink::KeyTemplate key_template_;
82   std::string object_identifier_;
83 };
84 
85 }  // namespace internal
86 }  // namespace tink
87 }  // namespace crypto
88 
89 #endif  // TINK_INTERNAL_PROTO_PARAMETERS_SERIALIZATION_H_
90