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_CMAC_BORINGSSL_H_ 18 #define TINK_SUBTLE_STATEFUL_CMAC_BORINGSSL_H_ 19 20 #include <memory> 21 #include <string> 22 #include <utility> 23 24 #include "openssl/cmac.h" 25 #include "openssl/evp.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 CMAC implementation of Stateful Mac interface. 38 class StatefulCmacBoringSsl : public subtle::StatefulMac { 39 public: 40 // Key must be 16 or 32 bytes, all other sizes will be rejected. 41 static util::StatusOr<std::unique_ptr<StatefulMac>> New( 42 uint32_t tag_size, const util::SecretData& key_value); 43 util::Status Update(absl::string_view data) override; 44 util::StatusOr<std::string> Finalize() override; 45 46 private: 47 static constexpr size_t kSmallKeySize = 16; 48 static constexpr size_t kBigKeySize = 32; 49 static constexpr size_t kMaxTagSize = 16; 50 StatefulCmacBoringSsl(uint32_t tag_size,internal::SslUniquePtr<CMAC_CTX> ctx)51 StatefulCmacBoringSsl(uint32_t tag_size, internal::SslUniquePtr<CMAC_CTX> ctx) 52 : cmac_context_(std::move(ctx)), tag_size_(tag_size) {} 53 54 const internal::SslUniquePtr<CMAC_CTX> cmac_context_; 55 const uint32_t tag_size_; 56 }; 57 58 class StatefulCmacBoringSslFactory : public subtle::StatefulMacFactory { 59 public: 60 StatefulCmacBoringSslFactory(uint32_t tag_size, 61 const util::SecretData& key_value); 62 util::StatusOr<std::unique_ptr<StatefulMac>> Create() const override; 63 64 private: 65 const uint32_t tag_size_; 66 const util::SecretData key_value_; 67 }; 68 69 } // namespace subtle 70 } // namespace tink 71 } // namespace crypto 72 73 #endif // TINK_SUBTLE_STATEFUL_CMAC_BORINGSSL_H_ 74