OLD | NEW |
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; |
| 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 const char* data = reinterpret_cast<char*>(&ctx_); |
| 84 if (!pickle->ReadBytes(&data_iterator, &data, sizeof(ctx_))) |
| 85 return false; |
| 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 |
OLD | NEW |