[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/secure_hash.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 8 | |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 9 | #include "base/logging.h" |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 11 | #include "base/pickle.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 12 | #include "crypto/openssl_util.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame] | 13 | #include "third_party/boringssl/src/include/openssl/mem.h" |
14 | #include "third_party/boringssl/src/include/openssl/sha.h" | ||||
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 15 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 16 | namespace crypto { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 17 | |
18 | namespace { | ||||
19 | |||||
svaldez | 22de42fe | 2016-04-21 19:42:22 | [diff] [blame] | 20 | class SecureHashSHA256 : public SecureHash { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 21 | public: |
svaldez | 22de42fe | 2016-04-21 19:42:22 | [diff] [blame] | 22 | SecureHashSHA256() { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 23 | SHA256_Init(&ctx_); |
24 | } | ||||
25 | |||||
svaldez | 22de42fe | 2016-04-21 19:42:22 | [diff] [blame] | 26 | SecureHashSHA256(const SecureHashSHA256& other) { |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 27 | memcpy(&ctx_, &other.ctx_, sizeof(ctx_)); |
28 | } | ||||
29 | |||||
svaldez | 22de42fe | 2016-04-21 19:42:22 | [diff] [blame] | 30 | ~SecureHashSHA256() override { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 31 | OPENSSL_cleanse(&ctx_, sizeof(ctx_)); |
32 | } | ||||
33 | |||||
eroman | a3a197c9 | 2015-01-02 03:49:45 | [diff] [blame] | 34 | void Update(const void* input, size_t len) override { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 35 | SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
36 | } | ||||
37 | |||||
eroman | a3a197c9 | 2015-01-02 03:49:45 | [diff] [blame] | 38 | void Finish(void* output, size_t len) override { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 39 | ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( |
40 | static_cast<unsigned char*>(output), len); | ||||
41 | SHA256_Final(result.safe_buffer(), &ctx_); | ||||
42 | } | ||||
43 | |||||
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 44 | std::unique_ptr<SecureHash> Clone() const override { |
45 | return base::MakeUnique<SecureHashSHA256>(*this); | ||||
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 46 | } |
47 | |||||
48 | size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; } | ||||
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 49 | |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 50 | private: |
51 | SHA256_CTX ctx_; | ||||
52 | }; | ||||
53 | |||||
54 | } // namespace | ||||
55 | |||||
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 56 | std::unique_ptr<SecureHash> SecureHash::Create(Algorithm algorithm) { |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 57 | switch (algorithm) { |
58 | case SHA256: | ||||
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 59 | return base::MakeUnique<SecureHashSHA256>(); |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 60 | default: |
61 | NOTIMPLEMENTED(); | ||||
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 62 | return nullptr; |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 63 | } |
64 | } | ||||
65 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 66 | } // namespace crypto |