blob: 80286df604a953846527db3e618307a5d163e9cd [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c322aa92011-01-27 11:21:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/secure_hash.h"
[email protected]c322aa92011-01-27 11:21:076
avidd373b8b2015-12-21 21:34:437#include <stddef.h>
[email protected]c322aa92011-01-27 11:21:078
[email protected]c322aa92011-01-27 11:21:079#include "base/logging.h"
rsleeviffe5a132016-06-28 01:51:5210#include "base/memory/ptr_util.h"
[email protected]02c28d222011-11-19 16:54:1311#include "base/pickle.h"
[email protected]4b559b4d2011-04-14 17:37:1412#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:3313#include "third_party/boringssl/src/include/openssl/mem.h"
14#include "third_party/boringssl/src/include/openssl/sha.h"
[email protected]c322aa92011-01-27 11:21:0715
[email protected]4b559b4d2011-04-14 17:37:1416namespace crypto {
[email protected]c322aa92011-01-27 11:21:0717
18namespace {
19
svaldez22de42fe2016-04-21 19:42:2220class SecureHashSHA256 : public SecureHash {
[email protected]c322aa92011-01-27 11:21:0721 public:
svaldez22de42fe2016-04-21 19:42:2222 SecureHashSHA256() {
[email protected]c322aa92011-01-27 11:21:0723 SHA256_Init(&ctx_);
24 }
25
svaldez22de42fe2016-04-21 19:42:2226 SecureHashSHA256(const SecureHashSHA256& other) {
asanka4350f6a2016-03-15 02:40:5727 memcpy(&ctx_, &other.ctx_, sizeof(ctx_));
28 }
29
svaldez22de42fe2016-04-21 19:42:2230 ~SecureHashSHA256() override {
[email protected]c322aa92011-01-27 11:21:0731 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
32 }
33
eromana3a197c92015-01-02 03:49:4534 void Update(const void* input, size_t len) override {
[email protected]c322aa92011-01-27 11:21:0735 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
36 }
37
eromana3a197c92015-01-02 03:49:4538 void Finish(void* output, size_t len) override {
[email protected]c322aa92011-01-27 11:21:0739 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
40 static_cast<unsigned char*>(output), len);
41 SHA256_Final(result.safe_buffer(), &ctx_);
42 }
43
rsleeviffe5a132016-06-28 01:51:5244 std::unique_ptr<SecureHash> Clone() const override {
45 return base::MakeUnique<SecureHashSHA256>(*this);
asanka4350f6a2016-03-15 02:40:5746 }
47
48 size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; }
[email protected]02c28d222011-11-19 16:54:1349
[email protected]c322aa92011-01-27 11:21:0750 private:
51 SHA256_CTX ctx_;
52};
53
54} // namespace
55
rsleeviffe5a132016-06-28 01:51:5256std::unique_ptr<SecureHash> SecureHash::Create(Algorithm algorithm) {
[email protected]c322aa92011-01-27 11:21:0757 switch (algorithm) {
58 case SHA256:
rsleeviffe5a132016-06-28 01:51:5259 return base::MakeUnique<SecureHashSHA256>();
[email protected]c322aa92011-01-27 11:21:0760 default:
61 NOTIMPLEMENTED();
rsleeviffe5a132016-06-28 01:51:5262 return nullptr;
[email protected]c322aa92011-01-27 11:21:0763 }
64}
65
[email protected]4b559b4d2011-04-14 17:37:1466} // namespace crypto