xref: /aosp_15_r20/external/tink/cc/subtle/stateful_hmac_boringssl.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2020 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_SUBTLE_STATEFUL_HMAC_BORINGSSL_H_
18 #define TINK_SUBTLE_STATEFUL_HMAC_BORINGSSL_H_
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "openssl/evp.h"
25 #include "openssl/hmac.h"
26 #include "tink/internal/ssl_unique_ptr.h"
27 #include "tink/subtle/common_enums.h"
28 #include "tink/subtle/mac/stateful_mac.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 // A BoringSSL HMAC implementation of Stateful Mac interface.
38 class StatefulHmacBoringSsl : public subtle::StatefulMac {
39  public:
40   static util::StatusOr<std::unique_ptr<StatefulMac>> New(
41       HashType hash_type, uint32_t tag_size, const util::SecretData& key_value);
42   util::Status Update(absl::string_view data) override;
43   util::StatusOr<std::string> Finalize() override;
44 
45  private:
46   // Minimum HMAC key size in bytes.
47   static constexpr size_t kMinKeySize = 16;
48 
StatefulHmacBoringSsl(uint32_t tag_size,internal::SslUniquePtr<HMAC_CTX> ctx)49   StatefulHmacBoringSsl(uint32_t tag_size, internal::SslUniquePtr<HMAC_CTX> ctx)
50       : hmac_context_(std::move(ctx)), tag_size_(tag_size) {}
51 
52   const internal::SslUniquePtr<HMAC_CTX> hmac_context_;
53   const uint32_t tag_size_;
54 };
55 
56 class StatefulHmacBoringSslFactory : public subtle::StatefulMacFactory {
57  public:
58   StatefulHmacBoringSslFactory(HashType hash_type, uint32_t tag_size,
59                                const util::SecretData& key_value);
60   util::StatusOr<std::unique_ptr<StatefulMac>> Create() const override;
61 
62  private:
63   const HashType hash_type_;
64   const uint32_t tag_size_;
65   const util::SecretData key_value_;
66 };
67 
68 }  // namespace subtle
69 }  // namespace tink
70 }  // namespace crypto
71 
72 #endif  // TINK_SUBTLE_STATEFUL_HMAC_BORINGSSL_H_
73