[Telemetry] Always uploading browser log if enabled instead of wait for crash to...
[chromium-blink-merge.git] / crypto / secure_hash_openssl.cc
blob1033b8e25eb82993ffc0ebee65f5ce3fce2189e3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/secure_hash.h"
7 #include <openssl/mem.h>
8 #include <openssl/sha.h>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/pickle.h"
13 #include "crypto/openssl_util.h"
15 namespace crypto {
17 namespace {
19 const char kSHA256Descriptor[] = "OpenSSL";
21 class SecureHashSHA256OpenSSL : public SecureHash {
22 public:
23 static const int kSecureHashVersion = 1;
25 SecureHashSHA256OpenSSL() {
26 SHA256_Init(&ctx_);
29 ~SecureHashSHA256OpenSSL() override {
30 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
33 void Update(const void* input, size_t len) override {
34 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
37 void Finish(void* output, size_t len) override {
38 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
39 static_cast<unsigned char*>(output), len);
40 SHA256_Final(result.safe_buffer(), &ctx_);
43 bool Serialize(base::Pickle* pickle) override;
44 bool Deserialize(base::PickleIterator* data_iterator) override;
46 private:
47 SHA256_CTX ctx_;
50 bool SecureHashSHA256OpenSSL::Serialize(base::Pickle* pickle) {
51 if (!pickle)
52 return false;
54 if (!pickle->WriteInt(kSecureHashVersion) ||
55 !pickle->WriteString(kSHA256Descriptor) ||
56 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
57 return false;
60 return true;
63 bool SecureHashSHA256OpenSSL::Deserialize(base::PickleIterator* data_iterator) {
64 if (!data_iterator)
65 return false;
67 int version;
68 if (!data_iterator->ReadInt(&version))
69 return false;
71 if (version > kSecureHashVersion)
72 return false; // We don't know how to deal with this.
74 std::string type;
75 if (!data_iterator->ReadString(&type))
76 return false;
78 if (type != kSHA256Descriptor)
79 return false; // It's the wrong kind.
81 const char* data = NULL;
82 if (!data_iterator->ReadBytes(&data, sizeof(ctx_)))
83 return false;
85 memcpy(&ctx_, data, sizeof(ctx_));
87 return true;
90 } // namespace
92 SecureHash* SecureHash::Create(Algorithm algorithm) {
93 switch (algorithm) {
94 case SHA256:
95 return new SecureHashSHA256OpenSSL();
96 default:
97 NOTIMPLEMENTED();
98 return NULL;
102 } // namespace crypto