xref: /aosp_15_r20/external/tink/cc/subtle/hmac_boringssl.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 Google Inc.
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_SUBTLE_HMAC_BORINGSSL_H_
18 #define TINK_SUBTLE_HMAC_BORINGSSL_H_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "absl/strings/string_view.h"
25 #include "openssl/evp.h"
26 #include "tink/internal/fips_utils.h"
27 #include "tink/mac.h"
28 #include "tink/subtle/common_enums.h"
29 #include "tink/util/secret_data.h"
30 #include "tink/util/status.h"
31 #include "tink/util/statusor.h"
32 
33 namespace crypto {
34 namespace tink {
35 namespace subtle {
36 
37 class HmacBoringSsl : public Mac {
38  public:
39   static crypto::tink::util::StatusOr<std::unique_ptr<Mac>> New(
40       HashType hash_type, uint32_t tag_size, util::SecretData key);
41 
42   // Computes and returns the HMAC for 'data'.
43   crypto::tink::util::StatusOr<std::string> ComputeMac(
44       absl::string_view data) const override;
45 
46   // Verifies if 'mac' is a correct HMAC for 'data'.
47   // Returns Status::OK if 'mac' is correct, and a non-OK-Status otherwise.
48   crypto::tink::util::Status VerifyMac(
49       absl::string_view mac,
50       absl::string_view data) const override;
51 
52   static constexpr crypto::tink::internal::FipsCompatibility kFipsStatus =
53       crypto::tink::internal::FipsCompatibility::kRequiresBoringCrypto;
54 
55  private:
56   // Minimum HMAC key size in bytes.
57   static constexpr size_t kMinKeySize = 16;
58 
HmacBoringSsl(const EVP_MD * md,uint32_t tag_size,util::SecretData key)59   HmacBoringSsl(const EVP_MD* md, uint32_t tag_size, util::SecretData key)
60       : md_(md), tag_size_(tag_size), key_(std::move(key)) {}
61 
62   // HmacBoringSsl is not owner of md (it is owned by BoringSSL).
63   const EVP_MD* const md_;
64   const uint32_t tag_size_;
65   const util::SecretData key_;
66 };
67 
68 }  // namespace subtle
69 }  // namespace tink
70 }  // namespace crypto
71 
72 #endif  // TINK_SUBTLE_HMAC_BORINGSSL_H_
73