Chromium Code Reviews| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/pickle.h" | |
| 8 #include "crypto/third_party/nss/chromium-blapi.h" | 9 #include "crypto/third_party/nss/chromium-blapi.h" |
| 9 #include "crypto/third_party/nss/chromium-sha256.h" | 10 #include "crypto/third_party/nss/chromium-sha256.h" |
| 10 | 11 |
| 11 namespace crypto { | 12 namespace crypto { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 class SecureHashSHA256NSS : public SecureHash { | 16 class SecureHashSHA256NSS : public SecureHash { |
| 16 public: | 17 public: |
| 18 static const int kSecureHashVersion = 1; | |
| 19 static const char* kDescriptor; | |
| 20 | |
| 17 SecureHashSHA256NSS() { | 21 SecureHashSHA256NSS() { |
| 18 SHA256_Begin(&ctx_); | 22 SHA256_Begin(&ctx_); |
| 19 } | 23 } |
| 20 | 24 |
| 21 virtual ~SecureHashSHA256NSS() { | 25 virtual ~SecureHashSHA256NSS() { |
| 22 } | 26 } |
| 23 | 27 |
| 24 virtual void Update(const void* input, size_t len) { | 28 virtual void Update(const void* input, size_t len) { |
| 25 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); | 29 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
| 26 } | 30 } |
| 27 | 31 |
| 28 virtual void Finish(void* output, size_t len) { | 32 virtual void Finish(void* output, size_t len) { |
| 29 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL, | 33 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL, |
| 30 static_cast<unsigned int>(len)); | 34 static_cast<unsigned int>(len)); |
| 31 } | 35 } |
| 32 | 36 |
| 37 virtual bool Serialize(Pickle* pickle); | |
| 38 virtual bool Deserialize(Pickle* pickle); | |
| 39 | |
| 33 private: | 40 private: |
| 34 SHA256Context ctx_; | 41 SHA256Context ctx_; |
| 35 }; | 42 }; |
| 36 | 43 |
| 44 const char* SecureHashSHA256NSS::kDescriptor = "NSS"; | |
|
agl
2011/11/18 19:57:02
Move it out of the class and do:
static const cha
ahendrickson
2011/11/18 21:23:09
Done.
| |
| 45 | |
| 46 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) { | |
| 47 if (!pickle) | |
| 48 return false; | |
| 49 | |
| 50 if (!pickle->WriteInt(kSecureHashVersion)) | |
|
agl
2011/11/18 19:57:02
if (!pickle->WriteInt(kSecureHashVersion) ||
!
ahendrickson
2011/11/18 21:23:09
Done.
| |
| 51 return false; | |
| 52 if (!pickle->WriteString(kDescriptor)) | |
| 53 return false; | |
| 54 if (!pickle->WriteBytes(&ctx_, sizeof(ctx_))) | |
| 55 return false; | |
| 56 | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 bool SecureHashSHA256NSS::Deserialize(Pickle* pickle) { | |
|
agl
2011/11/18 19:57:02
this interface (specifically the lack of a void* i
ahendrickson
2011/11/18 21:23:09
Fixed.
| |
| 61 if (!pickle) | |
| 62 return false; | |
| 63 | |
| 64 void* data_iterator = NULL; | |
| 65 int version; | |
| 66 if (!pickle->ReadInt(&data_iterator, &version)) | |
| 67 return false; | |
| 68 | |
| 69 if (version > kSecureHashVersion) | |
| 70 return false; // We don't know how to deal with this. | |
| 71 | |
| 72 std::string type; | |
| 73 if (!pickle->ReadString(&data_iterator, &type)) | |
| 74 return false; | |
| 75 | |
| 76 if (type != kDescriptor) | |
| 77 return false; // It's the wrong kind. | |
| 78 | |
| 79 char data_buffer[sizeof(ctx_)] = ""; | |
|
agl
2011/11/18 19:57:02
Pickle doesn't need a buffer to write it. It sets
ahendrickson
2011/11/18 21:23:09
Done.
| |
| 80 const char* data = data_buffer; | |
| 81 if (!pickle->ReadBytes(&data_iterator, &data, sizeof(ctx_))) | |
| 82 return false; | |
| 83 | |
| 84 memcpy(&ctx_, data, sizeof(ctx_)); | |
| 85 | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 37 } // namespace | 89 } // namespace |
| 38 | 90 |
| 39 SecureHash* SecureHash::Create(Algorithm algorithm) { | 91 SecureHash* SecureHash::Create(Algorithm algorithm) { |
| 40 switch (algorithm) { | 92 switch (algorithm) { |
| 41 case SHA256: | 93 case SHA256: |
| 42 return new SecureHashSHA256NSS(); | 94 return new SecureHashSHA256NSS(); |
| 43 default: | 95 default: |
| 44 NOTIMPLEMENTED(); | 96 NOTIMPLEMENTED(); |
| 45 return NULL; | 97 return NULL; |
| 46 } | 98 } |
| 47 } | 99 } |
| 48 | 100 |
| 49 } // namespace crypto | 101 } // namespace crypto |
| OLD | NEW |