xref: /aosp_15_r20/external/tink/cc/mac/hmac_key.h (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 #ifndef TINK_MAC_HMAC_KEY_H_
18 #define TINK_MAC_HMAC_KEY_H_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "absl/types/optional.h"
25 #include "tink/mac/hmac_parameters.h"
26 #include "tink/mac/mac_key.h"
27 #include "tink/partial_key_access_token.h"
28 #include "tink/restricted_data.h"
29 #include "tink/util/statusor.h"
30 
31 namespace crypto {
32 namespace tink {
33 
34 class HmacKey : public MacKey {
35  public:
36   // Copyable and movable.
37   HmacKey(const HmacKey& other) = default;
38   HmacKey& operator=(const HmacKey& other) = default;
39   HmacKey(HmacKey&& other) = default;
40   HmacKey& operator=(HmacKey&& other) = default;
41 
42   // Creates a new HMAC key.  If the parameters specify a variant that uses
43   // a prefix, then the id is used to compute this prefix.
44   static util::StatusOr<HmacKey> Create(const HmacParameters& parameters,
45                                         const RestrictedData& key_bytes,
46                                         absl::optional<int> id_requirement,
47                                         PartialKeyAccessToken token);
48 
49   // Returns the underlying HMAC key bytes.
GetKeyBytes(PartialKeyAccessToken token)50   const RestrictedData& GetKeyBytes(PartialKeyAccessToken token) const {
51     return key_bytes_;
52   }
53 
GetOutputPrefix()54   absl::string_view GetOutputPrefix() const override { return output_prefix_; }
55 
GetParameters()56   const HmacParameters& GetParameters() const override { return parameters_; }
57 
GetIdRequirement()58   absl::optional<int> GetIdRequirement() const override {
59     return id_requirement_;
60   }
61 
62   bool operator==(const Key& other) const override;
63 
64  private:
HmacKey(const HmacParameters & parameters,const RestrictedData & key_bytes,absl::optional<int> id_requirement,std::string output_prefix)65   HmacKey(const HmacParameters& parameters, const RestrictedData& key_bytes,
66           absl::optional<int> id_requirement, std::string output_prefix)
67       : parameters_(parameters),
68         key_bytes_(key_bytes),
69         id_requirement_(id_requirement),
70         output_prefix_(std::move(output_prefix)) {}
71 
72   static util::StatusOr<std::string> ComputeOutputPrefix(
73       const HmacParameters& parameters, absl::optional<int> id_requirement);
74 
75   HmacParameters parameters_;
76   RestrictedData key_bytes_;
77   absl::optional<int> id_requirement_;
78   std::string output_prefix_;
79 };
80 
81 }  // namespace tink
82 }  // namespace crypto
83 
84 #endif  // TINK_MAC_HMAC_KEY_H_
85