xref: /aosp_15_r20/external/tink/cc/mac/hmac_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/mac/hmac_parameters.h"
18 
19 #include <cstdlib>
20 #include <iostream>
21 #include <map>
22 #include <memory>
23 #include <ostream>
24 #include <set>
25 
26 #include "absl/log/log.h"
27 #include "tink/crypto_format.h"
28 #include "tink/internal/util.h"
29 #include "tink/util/status.h"
30 #include "tink/util/statusor.h"
31 
32 namespace crypto {
33 namespace tink {
34 namespace {
35 
ValidateTagSizeBytes(int cryptographic_tag_size_in_bytes,HmacParameters::HashType hash_type)36 util::Status ValidateTagSizeBytes(int cryptographic_tag_size_in_bytes,
37                                   HmacParameters::HashType hash_type) {
38   if (cryptographic_tag_size_in_bytes < 10) {
39     return util::Status(
40         absl::StatusCode::kInvalidArgument,
41         absl::StrCat("Tag size should be at least 10 bytes, got ",
42                      cryptographic_tag_size_in_bytes, " bytes."));
43   }
44   std::map<HmacParameters::HashType, uint32_t> max_tag_size = {
45       {HmacParameters::HashType::kSha1, 20},
46       {HmacParameters::HashType::kSha224, 28},
47       {HmacParameters::HashType::kSha256, 32},
48       {HmacParameters::HashType::kSha384, 48},
49       {HmacParameters::HashType::kSha512, 64}};
50   if (max_tag_size.find(hash_type) == max_tag_size.end()) {
51     return util::Status(
52         absl::StatusCode::kInvalidArgument,
53         absl::StrCat("Cannot create HMAC parameters with given hash type. ",
54                      hash_type, " not supported."));
55   }
56   if (cryptographic_tag_size_in_bytes > max_tag_size[hash_type]) {
57     return util::Status(
58         absl::StatusCode::kInvalidArgument,
59         absl::StrCat("Tag size is too big for given ", hash_type, " , got ",
60                      cryptographic_tag_size_in_bytes, " bytes."));
61   }
62   return util::OkStatus();
63 }
64 
65 }  // namespace
66 
Create(int key_size_in_bytes,int cryptographic_tag_size_in_bytes,HashType hash_type,Variant variant)67 util::StatusOr<HmacParameters> HmacParameters::Create(
68     int key_size_in_bytes, int cryptographic_tag_size_in_bytes,
69     HashType hash_type, Variant variant) {
70   if (key_size_in_bytes < 16) {
71     return util::Status(absl::StatusCode::kInvalidArgument,
72                         absl::StrCat("Key size must be at least 16 bytes, got ",
73                                      key_size_in_bytes, " bytes."));
74   }
75   util::Status status =
76       ValidateTagSizeBytes(cryptographic_tag_size_in_bytes, hash_type);
77   if (!status.ok()) return status;
78   static const std::set<Variant>* supported_variants =
79       new std::set<Variant>({Variant::kTink, Variant::kCrunchy,
80                              Variant::kLegacy, Variant::kNoPrefix});
81   if (supported_variants->find(variant) == supported_variants->end()) {
82     return util::Status(absl::StatusCode::kInvalidArgument,
83                         "Cannot create HMAC parameters with unknown variant.");
84   }
85   return HmacParameters(key_size_in_bytes, cryptographic_tag_size_in_bytes,
86                         hash_type, variant);
87 }
88 
TotalTagSizeInBytes() const89 int HmacParameters::TotalTagSizeInBytes() const {
90   switch (variant_) {
91     case Variant::kTink:
92     case Variant::kCrunchy:
93     case Variant::kLegacy:
94       return CryptographicTagSizeInBytes() + CryptoFormat::kNonRawPrefixSize;
95     case Variant::kNoPrefix:
96       return CryptographicTagSizeInBytes();
97     default:
98       // Parameters objects with unknown variants should never be created.
99       internal::LogFatal("HMAC parameters has an unknown variant.");
100   }
101 }
102 
operator ==(const Parameters & other) const103 bool HmacParameters::operator==(const Parameters& other) const {
104   const HmacParameters* that = dynamic_cast<const HmacParameters*>(&other);
105   if (that == nullptr) {
106     return false;
107   }
108   if (key_size_in_bytes_ != that->key_size_in_bytes_) {
109     return false;
110   }
111   if (cryptographic_tag_size_in_bytes_ !=
112       that->cryptographic_tag_size_in_bytes_) {
113     return false;
114   }
115   if (hash_type_ != that->hash_type_) {
116     return false;
117   }
118   if (variant_ != that->variant_) {
119     return false;
120   }
121   return true;
122 }
123 
124 }  // namespace tink
125 }  // namespace crypto
126