Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(787)

Side by Side Diff: crypto/secure_hash_openssl.cc

Issue 8588057: Added serialization to SecureHash. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added iterator to Deserialize, applied comments from agl. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/secure_hash_default.cc ('k') | crypto/secure_hash_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "crypto/secure_hash.h" 5 #include "crypto/secure_hash.h"
6 6
7 #include <openssl/ssl.h> 7 #include <openssl/ssl.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/pickle.h"
11 #include "crypto/openssl_util.h" 12 #include "crypto/openssl_util.h"
12 13
13 namespace crypto { 14 namespace crypto {
14 15
15 namespace { 16 namespace {
16 17
18 const char kSHA256Descriptor[] = "OpenSSL";
19
17 class SecureHashSHA256OpenSSL : public SecureHash { 20 class SecureHashSHA256OpenSSL : public SecureHash {
18 public: 21 public:
22 static const int kSecureHashVersion = 1;
23
19 SecureHashSHA256OpenSSL() { 24 SecureHashSHA256OpenSSL() {
20 SHA256_Init(&ctx_); 25 SHA256_Init(&ctx_);
21 } 26 }
22 27
23 virtual ~SecureHashSHA256OpenSSL() { 28 virtual ~SecureHashSHA256OpenSSL() {
24 OPENSSL_cleanse(&ctx_, sizeof(ctx_)); 29 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
25 } 30 }
26 31
27 virtual void Update(const void* input, size_t len) { 32 virtual void Update(const void* input, size_t len) {
28 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); 33 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
29 } 34 }
30 35
31 virtual void Finish(void* output, size_t len) { 36 virtual void Finish(void* output, size_t len) {
32 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( 37 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
33 static_cast<unsigned char*>(output), len); 38 static_cast<unsigned char*>(output), len);
34 SHA256_Final(result.safe_buffer(), &ctx_); 39 SHA256_Final(result.safe_buffer(), &ctx_);
35 } 40 }
36 41
42 virtual bool Serialize(Pickle* pickle);
43 virtual bool Deserialize(void** data_iterator, Pickle* pickle);
44
37 private: 45 private:
38 SHA256_CTX ctx_; 46 SHA256_CTX ctx_;
39 }; 47 };
40 48
49 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) {
50 if (!pickle)
51 return false;
52
53 if (!pickle->WriteInt(kSecureHashVersion) ||
54 !pickle->WriteString(kSHA256Descriptor) ||
55 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
56 return false;
57 }
58
59 return true;
60 }
61
62 bool SecureHashSHA256OpenSSL::Deserialize(void** data_iterator,
63 Pickle* pickle) {
64 if (!pickle)
65 return false;
66
67 int version;
68 if (!pickle->ReadInt(data_iterator, &version))
69 return false;
70
71 if (version > kSecureHashVersion)
72 return false; // We don't know how to deal with this.
73
74 std::string type;
75 if (!pickle->ReadString(data_iterator, &type))
76 return false;
77
78 if (type != kSHA256Descriptor)
79 return false; // It's the wrong kind.
80
81 const char* data = NULL;
82 if (!pickle->ReadBytes(data_iterator, &data, sizeof(ctx_)))
83 return false;
84
85 memcpy(&ctx_, data, sizeof(ctx_));
86
87 return true;
88 }
89
41 } // namespace 90 } // namespace
42 91
43 SecureHash* SecureHash::Create(Algorithm algorithm) { 92 SecureHash* SecureHash::Create(Algorithm algorithm) {
44 switch (algorithm) { 93 switch (algorithm) {
45 case SHA256: 94 case SHA256:
46 return new SecureHashSHA256OpenSSL(); 95 return new SecureHashSHA256OpenSSL();
47 default: 96 default:
48 NOTIMPLEMENTED(); 97 NOTIMPLEMENTED();
49 return NULL; 98 return NULL;
50 } 99 }
51 } 100 }
52 101
53 } // namespace crypto 102 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/secure_hash_default.cc ('k') | crypto/secure_hash_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698