| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/pickle.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 33 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); | 33 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
| 34 } | 34 } |
| 35 | 35 |
| 36 virtual void Finish(void* output, size_t len) { | 36 virtual void Finish(void* output, size_t len) { |
| 37 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( | 37 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( |
| 38 static_cast<unsigned char*>(output), len); | 38 static_cast<unsigned char*>(output), len); |
| 39 SHA256_Final(result.safe_buffer(), &ctx_); | 39 SHA256_Final(result.safe_buffer(), &ctx_); |
| 40 } | 40 } |
| 41 | 41 |
| 42 virtual bool Serialize(Pickle* pickle); | 42 virtual bool Serialize(Pickle* pickle); |
| 43 virtual bool Deserialize(void** data_iterator, Pickle* pickle); | 43 virtual bool Deserialize(PickleIterator* data_iterator); |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 SHA256_CTX ctx_; | 46 SHA256_CTX ctx_; |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) { | 49 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) { |
| 50 if (!pickle) | 50 if (!pickle) |
| 51 return false; | 51 return false; |
| 52 | 52 |
| 53 if (!pickle->WriteInt(kSecureHashVersion) || | 53 if (!pickle->WriteInt(kSecureHashVersion) || |
| 54 !pickle->WriteString(kSHA256Descriptor) || | 54 !pickle->WriteString(kSHA256Descriptor) || |
| 55 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) { | 55 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) { |
| 56 return false; | 56 return false; |
| 57 } | 57 } |
| 58 | 58 |
| 59 return true; | 59 return true; |
| 60 } | 60 } |
| 61 | 61 |
| 62 bool SecureHashSHA256OpenSSL::Deserialize(void** data_iterator, | 62 bool SecureHashSHA256OpenSSL::Deserialize(PickleIterator* data_iterator) { |
| 63 Pickle* pickle) { | 63 if (!data_iterator) |
| 64 if (!pickle) | |
| 65 return false; | 64 return false; |
| 66 | 65 |
| 67 int version; | 66 int version; |
| 68 if (!pickle->ReadInt(data_iterator, &version)) | 67 if (!data_iterator->ReadInt(&version)) |
| 69 return false; | 68 return false; |
| 70 | 69 |
| 71 if (version > kSecureHashVersion) | 70 if (version > kSecureHashVersion) |
| 72 return false; // We don't know how to deal with this. | 71 return false; // We don't know how to deal with this. |
| 73 | 72 |
| 74 std::string type; | 73 std::string type; |
| 75 if (!pickle->ReadString(data_iterator, &type)) | 74 if (!data_iterator->ReadString(&type)) |
| 76 return false; | 75 return false; |
| 77 | 76 |
| 78 if (type != kSHA256Descriptor) | 77 if (type != kSHA256Descriptor) |
| 79 return false; // It's the wrong kind. | 78 return false; // It's the wrong kind. |
| 80 | 79 |
| 81 const char* data = NULL; | 80 const char* data = NULL; |
| 82 if (!pickle->ReadBytes(data_iterator, &data, sizeof(ctx_))) | 81 if (!data_iterator->ReadBytes(&data, sizeof(ctx_))) |
| 83 return false; | 82 return false; |
| 84 | 83 |
| 85 memcpy(&ctx_, data, sizeof(ctx_)); | 84 memcpy(&ctx_, data, sizeof(ctx_)); |
| 86 | 85 |
| 87 return true; | 86 return true; |
| 88 } | 87 } |
| 89 | 88 |
| 90 } // namespace | 89 } // namespace |
| 91 | 90 |
| 92 SecureHash* SecureHash::Create(Algorithm algorithm) { | 91 SecureHash* SecureHash::Create(Algorithm algorithm) { |
| 93 switch (algorithm) { | 92 switch (algorithm) { |
| 94 case SHA256: | 93 case SHA256: |
| 95 return new SecureHashSHA256OpenSSL(); | 94 return new SecureHashSHA256OpenSSL(); |
| 96 default: | 95 default: |
| 97 NOTIMPLEMENTED(); | 96 NOTIMPLEMENTED(); |
| 98 return NULL; | 97 return NULL; |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 | 100 |
| 102 } // namespace crypto | 101 } // namespace crypto |
| OLD | NEW |