Files
nearby/internal/crypto_cros/secure_hash.cc
hai007 05ae5583f8 Move crypto-related symbols from ::crypto:: to ::nearby::crypto:: to deconflict
them with chromium ::crypto symbols of the same names.

PiperOrigin-RevId: 684566882
2024-10-10 13:54:53 -07:00

77 lines
2.1 KiB
C++

// 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 <stddef.h>
#include <memory>
#include "internal/crypto_cros/openssl_util.h"
#include <openssl/mem.h>
#include <openssl/sha.h>
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<const unsigned char*>(input), len);
}
void Finish(void* output, size_t len) override {
ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
static_cast<unsigned char*>(output), len);
SHA256_Final(result.safe_buffer(), &ctx_);
}
std::unique_ptr<SecureHash> Clone() const override {
return std::make_unique<SecureHashSHA256>(*this);
}
size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; }
private:
SHA256_CTX ctx_;
};
} // namespace
std::unique_ptr<SecureHash> SecureHash::Create(Algorithm algorithm) {
switch (algorithm) {
case SHA256:
return std::make_unique<SecureHashSHA256>();
default:
// NOTIMPLEMENTED();
return nullptr;
}
}
} // namespace nearby::crypto