// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "internal/crypto_cros/secure_hash.h" #include #include #include "internal/crypto_cros/openssl_util.h" #include #include namespace nearby::crypto { namespace { class SecureHashSHA256 : public SecureHash { public: SecureHashSHA256() { // Ensure that CPU features detection is performed before using // BoringSSL. This will enable hw accelerated implementations. EnsureOpenSSLInit(); SHA256_Init(&ctx_); } SecureHashSHA256(const SecureHashSHA256& other) { memcpy(&ctx_, &other.ctx_, sizeof(ctx_)); } ~SecureHashSHA256() override { OPENSSL_cleanse(&ctx_, sizeof(ctx_)); } void Update(const void* input, size_t len) override { SHA256_Update(&ctx_, static_cast(input), len); } void Finish(void* output, size_t len) override { ScopedOpenSSLSafeSizeBuffer result( static_cast(output), len); SHA256_Final(result.safe_buffer(), &ctx_); } std::unique_ptr Clone() const override { return std::make_unique(*this); } size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; } private: SHA256_CTX ctx_; }; } // namespace std::unique_ptr SecureHash::Create(Algorithm algorithm) { switch (algorithm) { case SHA256: return std::make_unique(); default: // NOTIMPLEMENTED(); return nullptr; } } } // namespace nearby::crypto