| OLD | NEW |
| 1 // Copyright (c) 2012 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/crypto.h> | |
| 8 #include <openssl/sha.h> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/logging.h" | 7 #include "base/logging.h" |
| 12 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 13 #include "crypto/openssl_util.h" | 9 #include "crypto/third_party/nss/chromium-blapi.h" |
| 10 #include "crypto/third_party/nss/chromium-sha256.h" |
| 14 | 11 |
| 15 namespace crypto { | 12 namespace crypto { |
| 16 | 13 |
| 17 namespace { | 14 namespace { |
| 18 | 15 |
| 19 const char kSHA256Descriptor[] = "OpenSSL"; | 16 const char kSHA256Descriptor[] = "NSS"; |
| 20 | 17 |
| 21 class SecureHashSHA256OpenSSL : public SecureHash { | 18 class SecureHashSHA256NSS : public SecureHash { |
| 22 public: | 19 public: |
| 23 static const int kSecureHashVersion = 1; | 20 static const int kSecureHashVersion = 1; |
| 24 | 21 |
| 25 SecureHashSHA256OpenSSL() { | 22 SecureHashSHA256NSS() { |
| 26 SHA256_Init(&ctx_); | 23 SHA256_Begin(&ctx_); |
| 27 } | 24 } |
| 28 | 25 |
| 29 ~SecureHashSHA256OpenSSL() override { | 26 ~SecureHashSHA256NSS() override { memset(&ctx_, 0, sizeof(ctx_)); } |
| 30 OPENSSL_cleanse(&ctx_, sizeof(ctx_)); | |
| 31 } | |
| 32 | 27 |
| 28 // SecureHash implementation: |
| 33 void Update(const void* input, size_t len) override { | 29 void Update(const void* input, size_t len) override { |
| 34 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); | 30 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
| 35 } | 31 } |
| 36 | 32 |
| 37 void Finish(void* output, size_t len) override { | 33 void Finish(void* output, size_t len) override { |
| 38 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( | 34 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL, |
| 39 static_cast<unsigned char*>(output), len); | 35 static_cast<unsigned int>(len)); |
| 40 SHA256_Final(result.safe_buffer(), &ctx_); | |
| 41 } | 36 } |
| 42 | 37 |
| 43 bool Serialize(Pickle* pickle) override; | 38 bool Serialize(Pickle* pickle) override; |
| 44 bool Deserialize(PickleIterator* data_iterator) override; | 39 bool Deserialize(PickleIterator* data_iterator) override; |
| 45 | 40 |
| 46 private: | 41 private: |
| 47 SHA256_CTX ctx_; | 42 SHA256Context ctx_; |
| 48 }; | 43 }; |
| 49 | 44 |
| 50 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) { | 45 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) { |
| 51 if (!pickle) | 46 if (!pickle) |
| 52 return false; | 47 return false; |
| 53 | 48 |
| 54 if (!pickle->WriteInt(kSecureHashVersion) || | 49 if (!pickle->WriteInt(kSecureHashVersion) || |
| 55 !pickle->WriteString(kSHA256Descriptor) || | 50 !pickle->WriteString(kSHA256Descriptor) || |
| 56 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) { | 51 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) { |
| 57 return false; | 52 return false; |
| 58 } | 53 } |
| 59 | 54 |
| 60 return true; | 55 return true; |
| 61 } | 56 } |
| 62 | 57 |
| 63 bool SecureHashSHA256OpenSSL::Deserialize(PickleIterator* data_iterator) { | 58 bool SecureHashSHA256NSS::Deserialize(PickleIterator* data_iterator) { |
| 64 if (!data_iterator) | |
| 65 return false; | |
| 66 | |
| 67 int version; | 59 int version; |
| 68 if (!data_iterator->ReadInt(&version)) | 60 if (!data_iterator->ReadInt(&version)) |
| 69 return false; | 61 return false; |
| 70 | 62 |
| 71 if (version > kSecureHashVersion) | 63 if (version > kSecureHashVersion) |
| 72 return false; // We don't know how to deal with this. | 64 return false; // We don't know how to deal with this. |
| 73 | 65 |
| 74 std::string type; | 66 std::string type; |
| 75 if (!data_iterator->ReadString(&type)) | 67 if (!data_iterator->ReadString(&type)) |
| 76 return false; | 68 return false; |
| 77 | 69 |
| 78 if (type != kSHA256Descriptor) | 70 if (type != kSHA256Descriptor) |
| 79 return false; // It's the wrong kind. | 71 return false; // It's the wrong kind. |
| 80 | 72 |
| 81 const char* data = NULL; | 73 const char* data = NULL; |
| 82 if (!data_iterator->ReadBytes(&data, sizeof(ctx_))) | 74 if (!data_iterator->ReadBytes(&data, sizeof(ctx_))) |
| 83 return false; | 75 return false; |
| 84 | 76 |
| 85 memcpy(&ctx_, data, sizeof(ctx_)); | 77 memcpy(&ctx_, data, sizeof(ctx_)); |
| 86 | 78 |
| 87 return true; | 79 return true; |
| 88 } | 80 } |
| 89 | 81 |
| 90 } // namespace | 82 } // namespace |
| 91 | 83 |
| 92 SecureHash* SecureHash::Create(Algorithm algorithm) { | 84 SecureHash* SecureHash::Create(Algorithm algorithm) { |
| 93 switch (algorithm) { | 85 switch (algorithm) { |
| 94 case SHA256: | 86 case SHA256: |
| 95 return new SecureHashSHA256OpenSSL(); | 87 return new SecureHashSHA256NSS(); |
| 96 default: | 88 default: |
| 97 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
| 98 return NULL; | 90 return NULL; |
| 99 } | 91 } |
| 100 } | 92 } |
| 101 | 93 |
| 102 } // namespace crypto | 94 } // namespace crypto |
| OLD | NEW |