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

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: Fixed bug. 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
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
17 class SecureHashSHA256OpenSSL : public SecureHash { 18 class SecureHashSHA256OpenSSL : public SecureHash {
18 public: 19 public:
20 static const int kSecureHashVersion = 1;
agl 2011/11/18 19:57:02 all the same comments apply to this file.
ahendrickson 2011/11/18 21:23:09 Done.
21 static const char* kDescriptor;
22
19 SecureHashSHA256OpenSSL() { 23 SecureHashSHA256OpenSSL() {
20 SHA256_Init(&ctx_); 24 SHA256_Init(&ctx_);
21 } 25 }
22 26
23 virtual ~SecureHashSHA256OpenSSL() { 27 virtual ~SecureHashSHA256OpenSSL() {
24 OPENSSL_cleanse(&ctx_, sizeof(ctx_)); 28 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
25 } 29 }
26 30
27 virtual void Update(const void* input, size_t len) { 31 virtual void Update(const void* input, size_t len) {
28 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); 32 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
29 } 33 }
30 34
31 virtual void Finish(void* output, size_t len) { 35 virtual void Finish(void* output, size_t len) {
32 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( 36 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
33 static_cast<unsigned char*>(output), len); 37 static_cast<unsigned char*>(output), len);
34 SHA256_Final(result.safe_buffer(), &ctx_); 38 SHA256_Final(result.safe_buffer(), &ctx_);
35 } 39 }
36 40
41 virtual bool Serialize(Pickle* pickle);
42 virtual bool Deserialize(Pickle* pickle);
43
37 private: 44 private:
38 SHA256_CTX ctx_; 45 SHA256_CTX ctx_;
39 }; 46 };
40 47
48 const char* SecureHashSHA256OpenSSL::kDescriptor = "OpenSSL";
49
50 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) {
51 if (!pickle)
52 return false;
53
54 if (!pickle->WriteInt(kSecureHashVersion))
55 return false;
56 if (!pickle->WriteString(kDescriptor))
57 return false;
58 if (!pickle->WriteBytes(&ctx_, sizeof(ctx_)))
59 return false;
60
61 return true;
62 }
63
64 bool SecureHashSHA256OpenSSL::Deserialize(Pickle* pickle) {
65 if (!pickle)
66 return false;
67
68 void* data_iterator = NULL;
69 int version;
70 if (!pickle->ReadInt(&data_iterator, &version))
71 return false;
72
73 if (version > kSecureHashVersion)
74 return false; // We don't know how to deal with this.
75
76 std::string type;
77 if (!pickle->ReadString(&data_iterator, &type))
78 return false;
79
80 if (type != kDescriptor)
81 return false; // It's the wrong kind.
82
83 char data_buffer[sizeof(ctx_)] = "";
84 const char* data = data_buffer;
85 if (!pickle->ReadBytes(&data_iterator, &data, sizeof(ctx_)))
86 return false;
87
88 memcpy(&ctx_, data, sizeof(ctx_));
89
90 return true;
91 }
92
41 } // namespace 93 } // namespace
42 94
43 SecureHash* SecureHash::Create(Algorithm algorithm) { 95 SecureHash* SecureHash::Create(Algorithm algorithm) {
44 switch (algorithm) { 96 switch (algorithm) {
45 case SHA256: 97 case SHA256:
46 return new SecureHashSHA256OpenSSL(); 98 return new SecureHashSHA256OpenSSL();
47 default: 99 default:
48 NOTIMPLEMENTED(); 100 NOTIMPLEMENTED();
49 return NULL; 101 return NULL;
50 } 102 }
51 } 103 }
52 104
53 } // namespace crypto 105 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698