xref: /aosp_15_r20/external/tink/cc/aead/aes_gcm_parameters.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_parameters.h"
18 
19 #include <set>
20 
21 #include "absl/strings/str_cat.h"
22 #include "tink/util/status.h"
23 #include "tink/util/statusor.h"
24 
25 namespace crypto {
26 namespace tink {
27 
SetKeySizeInBytes(int key_size)28 AesGcmParameters::Builder& AesGcmParameters::Builder::SetKeySizeInBytes(
29     int key_size) {
30   key_size_in_bytes_ = key_size;
31   return *this;
32 }
33 
SetIvSizeInBytes(int iv_size)34 AesGcmParameters::Builder& AesGcmParameters::Builder::SetIvSizeInBytes(
35     int iv_size) {
36   iv_size_in_bytes_ = iv_size;
37   return *this;
38 }
39 
SetTagSizeInBytes(int tag_size)40 AesGcmParameters::Builder& AesGcmParameters::Builder::SetTagSizeInBytes(
41     int tag_size) {
42   tag_size_in_bytes_ = tag_size;
43   return *this;
44 }
45 
SetVariant(Variant variant)46 AesGcmParameters::Builder& AesGcmParameters::Builder::SetVariant(
47     Variant variant) {
48   variant_ = variant;
49   return *this;
50 }
51 
Build()52 util::StatusOr<AesGcmParameters> AesGcmParameters::Builder::Build() {
53   if (key_size_in_bytes_ != 16 && key_size_in_bytes_ != 24 &&
54       key_size_in_bytes_ != 32) {
55     return util::Status(
56         absl::StatusCode::kInvalidArgument,
57         absl::StrCat("Key size should be 16, 24, or 32 bytes, got ",
58                      key_size_in_bytes_, " bytes."));
59   }
60   if (iv_size_in_bytes_ <= 0) {
61     return util::Status(absl::StatusCode::kInvalidArgument,
62                         absl::StrCat("IV size should be positive, got ",
63                                      iv_size_in_bytes_, " bytes."));
64   }
65   if (tag_size_in_bytes_ < 12 || tag_size_in_bytes_ > 16) {
66     return util::Status(
67         absl::StatusCode::kInvalidArgument,
68         absl::StrCat("Tag size should be between 12 and 16 bytes, got ",
69                      tag_size_in_bytes_, " bytes."));
70   }
71   static const std::set<Variant>* supported_variants = new std::set<Variant>(
72       {Variant::kTink, Variant::kCrunchy, Variant::kNoPrefix});
73   if (supported_variants->find(variant_) == supported_variants->end()) {
74     return util::Status(
75         absl::StatusCode::kInvalidArgument,
76         "Cannot create AES-GCM parameters with unknown variant.");
77   }
78   return AesGcmParameters(key_size_in_bytes_, iv_size_in_bytes_,
79                           tag_size_in_bytes_, variant_);
80 }
81 
operator ==(const Parameters & other) const82 bool AesGcmParameters::operator==(const Parameters& other) const {
83   const AesGcmParameters* that = dynamic_cast<const AesGcmParameters*>(&other);
84   if (that == nullptr) {
85     return false;
86   }
87   if (key_size_in_bytes_ != that->key_size_in_bytes_) {
88     return false;
89   }
90   if (iv_size_in_bytes_ != that->iv_size_in_bytes_) {
91     return false;
92   }
93   if (tag_size_in_bytes_ != that->tag_size_in_bytes_) {
94     return false;
95   }
96   if (variant_ != that->variant_) {
97     return false;
98   }
99   return true;
100 }
101 
102 }  // namespace tink
103 }  // namespace crypto
104