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 #include "tink/mac/aes_cmac_parameters.h"
18
19 #include <cstdlib>
20 #include <iostream>
21 #include <memory>
22 #include <ostream>
23 #include <set>
24
25 #include "absl/log/log.h"
26 #include "tink/crypto_format.h"
27 #include "tink/internal/util.h"
28 #include "tink/util/status.h"
29 #include "tink/util/statusor.h"
30
31 namespace crypto {
32 namespace tink {
33
Create(int key_size_in_bytes,int cryptographic_tag_size_in_bytes,Variant variant)34 util::StatusOr<AesCmacParameters> AesCmacParameters::Create(
35 int key_size_in_bytes, int cryptographic_tag_size_in_bytes,
36 Variant variant) {
37 if (key_size_in_bytes != 16 && key_size_in_bytes != 32) {
38 return util::Status(
39 absl::StatusCode::kInvalidArgument,
40 absl::StrCat("Key size should be either 16 or 32 bytes, got ",
41 key_size_in_bytes, " bytes."));
42 }
43 if (cryptographic_tag_size_in_bytes < 10) {
44 return util::Status(
45 absl::StatusCode::kInvalidArgument,
46 absl::StrCat("Tag size should be at least 10 bytes, got ",
47 cryptographic_tag_size_in_bytes, " bytes."));
48 }
49 if (cryptographic_tag_size_in_bytes > 16) {
50 return util::Status(
51 absl::StatusCode::kInvalidArgument,
52 absl::StrCat("Tag size should not exceed 16 bytes, got ",
53 cryptographic_tag_size_in_bytes, " bytes."));
54 }
55 static const std::set<Variant>* supported_variants =
56 new std::set<Variant>({Variant::kTink, Variant::kCrunchy,
57 Variant::kLegacy, Variant::kNoPrefix});
58 if (supported_variants->find(variant) == supported_variants->end()) {
59 return util::Status(
60 absl::StatusCode::kInvalidArgument,
61 "Cannot create AES-CMAC parameters with unknown variant.");
62 }
63 return AesCmacParameters(key_size_in_bytes, cryptographic_tag_size_in_bytes,
64 variant);
65 }
66
TotalTagSizeInBytes() const67 int AesCmacParameters::TotalTagSizeInBytes() const {
68 switch (variant_) {
69 case Variant::kTink:
70 case Variant::kCrunchy:
71 case Variant::kLegacy:
72 return CryptographicTagSizeInBytes() + CryptoFormat::kNonRawPrefixSize;
73 case Variant::kNoPrefix:
74 return CryptographicTagSizeInBytes();
75 default:
76 // Parameters objects with unknown variants should never be created.
77 internal::LogFatal("AES-CMAC parameters has an unknown variant.");
78 }
79 }
80
operator ==(const Parameters & other) const81 bool AesCmacParameters::operator==(const Parameters& other) const {
82 const AesCmacParameters* that =
83 dynamic_cast<const AesCmacParameters*>(&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 (cryptographic_tag_size_in_bytes_ !=
91 that->cryptographic_tag_size_in_bytes_) {
92 return false;
93 }
94 if (variant_ != that->variant_) {
95 return false;
96 }
97 return true;
98 }
99
100 } // namespace tink
101 } // namespace crypto
102