xref: /aosp_15_r20/external/tink/cc/aead/aes_gcm_key.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2023 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 #include "tink/aead/aes_gcm_key.h"
18 
19 #include <string>
20 
21 #include "absl/strings/escaping.h"
22 #include "absl/types/optional.h"
23 #include "tink/aead/aes_gcm_parameters.h"
24 #include "tink/partial_key_access_token.h"
25 #include "tink/restricted_data.h"
26 #include "tink/subtle/subtle_util.h"
27 #include "tink/util/status.h"
28 #include "tink/util/statusor.h"
29 
30 namespace crypto {
31 namespace tink {
32 namespace {
33 
ComputeOutputPrefix(const AesGcmParameters & parameters,absl::optional<int> id_requirement)34 util::StatusOr<std::string> ComputeOutputPrefix(
35     const AesGcmParameters& parameters, absl::optional<int> id_requirement) {
36   switch (parameters.GetVariant()) {
37     case AesGcmParameters::Variant::kNoPrefix:
38       return std::string("");  // Empty prefix.
39     case AesGcmParameters::Variant::kCrunchy:
40       if (!id_requirement.has_value()) {
41         return util::Status(
42             absl::StatusCode::kInvalidArgument,
43             "id requirement must have value with kCrunchy or kLegacy");
44       }
45       return absl::StrCat(absl::HexStringToBytes("00"),
46                           subtle::BigEndian32(*id_requirement));
47     case AesGcmParameters::Variant::kTink:
48       if (!id_requirement.has_value()) {
49         return util::Status(absl::StatusCode::kInvalidArgument,
50                             "id requirement must have value with kTink");
51       }
52       return absl::StrCat(absl::HexStringToBytes("01"),
53                           subtle::BigEndian32(*id_requirement));
54     default:
55       return util::Status(
56           absl::StatusCode::kInvalidArgument,
57           absl::StrCat("Invalid variant: ", parameters.GetVariant()));
58   }
59 }
60 
61 }  // namespace
62 
Create(const AesGcmParameters & parameters,const RestrictedData & key_bytes,absl::optional<int> id_requirement,PartialKeyAccessToken token)63 util::StatusOr<AesGcmKey> AesGcmKey::Create(const AesGcmParameters& parameters,
64                                             const RestrictedData& key_bytes,
65                                             absl::optional<int> id_requirement,
66                                             PartialKeyAccessToken token) {
67   if (parameters.KeySizeInBytes() != key_bytes.size()) {
68     return util::Status(absl::StatusCode::kInvalidArgument,
69                         "Key size does not match AES-GCM parameters");
70   }
71   if (parameters.HasIdRequirement() && !id_requirement.has_value()) {
72     return util::Status(
73         absl::StatusCode::kInvalidArgument,
74         "Cannot create key without ID requirement with parameters with ID "
75         "requirement");
76   }
77   if (!parameters.HasIdRequirement() && id_requirement.has_value()) {
78     return util::Status(
79         absl::StatusCode::kInvalidArgument,
80         "Cannot create key with ID requirement with parameters without ID "
81         "requirement");
82   }
83   util::StatusOr<std::string> output_prefix =
84       ComputeOutputPrefix(parameters, id_requirement);
85   if (!output_prefix.ok()) {
86     return output_prefix.status();
87   }
88   return AesGcmKey(parameters, key_bytes, id_requirement,
89                    *std::move(output_prefix));
90 }
91 
operator ==(const Key & other) const92 bool AesGcmKey::operator==(const Key& other) const {
93   const AesGcmKey* that = dynamic_cast<const AesGcmKey*>(&other);
94   if (that == nullptr) {
95     return false;
96   }
97   if (GetParameters() != that->GetParameters()) {
98     return false;
99   }
100   if (id_requirement_ != that->id_requirement_) {
101     return false;
102   }
103   return key_bytes_ == that->key_bytes_;
104 }
105 
106 }  // namespace tink
107 }  // namespace crypto
108