1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "crypto/secure_hash.h"
6
7 #include <stddef.h>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/notreached.h"
11 #include "base/pickle.h"
12 #include "crypto/openssl_util.h"
13 #include "third_party/boringssl/src/include/openssl/mem.h"
14 #include "third_party/boringssl/src/include/openssl/sha.h"
15
16 namespace crypto {
17
18 namespace {
19
20 class SecureHashSHA256 : public SecureHash {
21 public:
SecureHashSHA256()22 SecureHashSHA256() {
23 // Ensure that CPU features detection is performed before using
24 // BoringSSL. This will enable hw accelerated implementations.
25 EnsureOpenSSLInit();
26 SHA256_Init(&ctx_);
27 }
28
SecureHashSHA256(const SecureHashSHA256 & other)29 SecureHashSHA256(const SecureHashSHA256& other) {
30 memcpy(&ctx_, &other.ctx_, sizeof(ctx_));
31 }
32
~SecureHashSHA256()33 ~SecureHashSHA256() override {
34 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
35 }
36
Update(const void * input,size_t len)37 void Update(const void* input, size_t len) override {
38 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
39 }
40
Finish(void * output,size_t len)41 void Finish(void* output, size_t len) override {
42 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
43 static_cast<unsigned char*>(output), len);
44 SHA256_Final(result.safe_buffer(), &ctx_);
45 }
46
Clone() const47 std::unique_ptr<SecureHash> Clone() const override {
48 return std::make_unique<SecureHashSHA256>(*this);
49 }
50
GetHashLength() const51 size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; }
52
53 private:
54 SHA256_CTX ctx_;
55 };
56
57 class SecureHashSHA512 : public SecureHash {
58 public:
SecureHashSHA512()59 SecureHashSHA512() {
60 // Ensure that CPU features detection is performed before using
61 // BoringSSL. This will enable hw accelerated implementations.
62 EnsureOpenSSLInit();
63 SHA512_Init(&ctx_);
64 }
65
SecureHashSHA512(const SecureHashSHA512 & other)66 SecureHashSHA512(const SecureHashSHA512& other) {
67 memcpy(&ctx_, &other.ctx_, sizeof(ctx_));
68 }
69
~SecureHashSHA512()70 ~SecureHashSHA512() override { OPENSSL_cleanse(&ctx_, sizeof(ctx_)); }
71
Update(const void * input,size_t len)72 void Update(const void* input, size_t len) override {
73 SHA512_Update(&ctx_, static_cast<const unsigned char*>(input), len);
74 }
75
Finish(void * output,size_t len)76 void Finish(void* output, size_t len) override {
77 ScopedOpenSSLSafeSizeBuffer<SHA512_DIGEST_LENGTH> result(
78 static_cast<unsigned char*>(output), len);
79 SHA512_Final(result.safe_buffer(), &ctx_);
80 }
81
Clone() const82 std::unique_ptr<SecureHash> Clone() const override {
83 return std::make_unique<SecureHashSHA512>(*this);
84 }
85
GetHashLength() const86 size_t GetHashLength() const override { return SHA512_DIGEST_LENGTH; }
87
88 private:
89 SHA512_CTX ctx_;
90 };
91
92 } // namespace
93
Create(Algorithm algorithm)94 std::unique_ptr<SecureHash> SecureHash::Create(Algorithm algorithm) {
95 switch (algorithm) {
96 case SHA256:
97 return std::make_unique<SecureHashSHA256>();
98 case SHA512:
99 return std::make_unique<SecureHashSHA512>();
100 default:
101 NOTIMPLEMENTED();
102 return nullptr;
103 }
104 }
105
106 } // namespace crypto
107