xref: /aosp_15_r20/external/tink/cc/internal/proto_key_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_KEY_SERIALIZATION_H_
18 #define TINK_INTERNAL_PROTO_KEY_SERIALIZATION_H_
19 
20 #include <string>
21 #include <utility>
22 
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "tink/internal/serialization.h"
26 #include "tink/restricted_data.h"
27 #include "tink/util/statusor.h"
28 #include "proto/tink.pb.h"
29 
30 namespace crypto {
31 namespace tink {
32 namespace internal {
33 
34 // Represents a `Key` object serialized with binary protocol buffer
35 // serialization.
36 class ProtoKeySerialization : public Serialization {
37  public:
38   // Copyable and movable.
39   ProtoKeySerialization(const ProtoKeySerialization& other) = default;
40   ProtoKeySerialization& operator=(const ProtoKeySerialization& other) =
41       default;
42   ProtoKeySerialization(ProtoKeySerialization&& other) = default;
43   ProtoKeySerialization& operator=(ProtoKeySerialization&& other) = default;
44 
45   // Creates a `ProtoKeySerialization` object from individual components.
46   static util::StatusOr<ProtoKeySerialization> Create(
47       absl::string_view type_url, RestrictedData serialized_key,
48       google::crypto::tink::KeyData::KeyMaterialType key_material_type,
49       google::crypto::tink::OutputPrefixType output_prefix_type,
50       absl::optional<int> id_requirement);
51 
52   // Returned value is only valid for the lifetime of this object.
TypeUrl()53   absl::string_view TypeUrl() const { return type_url_; }
54 
55   // Returned value is only valid for the lifetime of this object.
ObjectIdentifier()56   absl::string_view ObjectIdentifier() const override {
57     return object_identifier_;
58   }
59 
60   // Returned value is only valid for the lifetime of this object.
SerializedKeyProto()61   RestrictedData SerializedKeyProto() const { return serialized_key_; }
62 
KeyMaterialType()63   google::crypto::tink::KeyData::KeyMaterialType KeyMaterialType() const {
64     return key_material_type_;
65   }
66 
GetOutputPrefixType()67   google::crypto::tink::OutputPrefixType GetOutputPrefixType() const {
68     return output_prefix_type_;
69   }
70 
IdRequirement()71   absl::optional<int> IdRequirement() const { return id_requirement_; }
72 
73  private:
74   friend class ProtoKeySerializationTest;
75   friend class LegacyProtoKey;
76   friend class LegacyProtoKeyTest;
77 
ProtoKeySerialization(absl::string_view type_url,absl::string_view object_identifier,RestrictedData serialized_key,google::crypto::tink::KeyData::KeyMaterialType key_material_type,google::crypto::tink::OutputPrefixType output_prefix_type,absl::optional<int> id_requirement)78   ProtoKeySerialization(
79       absl::string_view type_url, absl::string_view object_identifier,
80       RestrictedData serialized_key,
81       google::crypto::tink::KeyData::KeyMaterialType key_material_type,
82       google::crypto::tink::OutputPrefixType output_prefix_type,
83       absl::optional<int> id_requirement)
84       : type_url_(type_url),
85         object_identifier_(object_identifier),
86         serialized_key_(std::move(serialized_key)),
87         key_material_type_(key_material_type),
88         output_prefix_type_(output_prefix_type),
89         id_requirement_(id_requirement) {}
90 
91   // Returns `true` if this `ProtoKeySerialization` object is equal to
92   // `other` (with the possibility of false negatives due to lack of
93   // determinism during serialization).  Should only be used temporarily by the
94   // `LegacyKeyParameters` class.
95   bool EqualsWithPotentialFalseNegatives(
96       const ProtoKeySerialization& other) const;
97 
98   std::string type_url_;
99   std::string object_identifier_;
100   RestrictedData serialized_key_;
101   google::crypto::tink::KeyData::KeyMaterialType key_material_type_;
102   google::crypto::tink::OutputPrefixType output_prefix_type_;
103   absl::optional<int> id_requirement_;
104 };
105 
106 }  // namespace internal
107 }  // namespace tink
108 }  // namespace crypto
109 
110 #endif  // TINK_INTERNAL_PROTO_KEY_SERIALIZATION_H_
111