xref: /aosp_15_r20/external/tink/cc/subtle/stateful_cmac_boringssl.cc (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 #include "tink/subtle/stateful_cmac_boringssl.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/status/status.h"
25 #include "openssl/evp.h"
26 #include "tink/internal/aes_util.h"
27 #include "tink/internal/ssl_unique_ptr.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 subtle {
35 
New(uint32_t tag_size,const util::SecretData & key_value)36 util::StatusOr<std::unique_ptr<StatefulMac>> StatefulCmacBoringSsl::New(
37     uint32_t tag_size, const util::SecretData& key_value) {
38   util::StatusOr<const EVP_CIPHER*> cipher =
39       internal::GetAesCbcCipherForKeySize(key_value.size());
40   if (!cipher.ok()) {
41     return cipher.status();
42   }
43   if (tag_size > kMaxTagSize) {
44     return util::Status(absl::StatusCode::kInvalidArgument, "invalid tag size");
45   }
46 
47   // Create and initialize the CMAC context
48   internal::SslUniquePtr<CMAC_CTX> ctx(CMAC_CTX_new());
49 
50   // Initialize the CMAC
51   if (!CMAC_Init(ctx.get(), key_value.data(), key_value.size(), *cipher,
52                  nullptr /* engine */)) {
53     return util::Status(absl::StatusCode::kFailedPrecondition,
54                         "CMAC initialization failed");
55   }
56 
57   return {
58       absl::WrapUnique(new StatefulCmacBoringSsl(tag_size, std::move(ctx)))};
59 }
60 
Update(absl::string_view data)61 util::Status StatefulCmacBoringSsl::Update(absl::string_view data) {
62   // BoringSSL expects a non-null pointer for data,
63   // regardless of whether the size is 0.
64   data = internal::EnsureStringNonNull(data);
65 
66   if (!CMAC_Update(cmac_context_.get(),
67                    reinterpret_cast<const uint8_t*>(data.data()),
68                    data.size())) {
69     return util::Status(absl::StatusCode::kInternal,
70                         "Inputs to CMAC Update invalid");
71   }
72   return util::OkStatus();
73 }
74 
Finalize()75 util::StatusOr<std::string> StatefulCmacBoringSsl::Finalize() {
76   uint8_t buf[EVP_MAX_MD_SIZE];
77   size_t out_len;
78 
79   if (!CMAC_Final(cmac_context_.get(), buf, &out_len)) {
80     return util::Status(absl::StatusCode::kInternal,
81                         "CMAC finalization failed");
82   }
83   return std::string(reinterpret_cast<char*>(buf), tag_size_);
84 }
85 
StatefulCmacBoringSslFactory(uint32_t tag_size,const util::SecretData & key_value)86 StatefulCmacBoringSslFactory::StatefulCmacBoringSslFactory(
87     uint32_t tag_size, const util::SecretData& key_value)
88     : tag_size_(tag_size), key_value_(key_value) {}
89 
90 util::StatusOr<std::unique_ptr<StatefulMac>>
Create() const91 StatefulCmacBoringSslFactory::Create() const {
92   return StatefulCmacBoringSsl::New(tag_size_, key_value_);
93 }
94 
95 }  // namespace subtle
96 }  // namespace tink
97 }  // namespace crypto
98