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"; |
| 45 |
| 46 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) { |
| 47 if (!pickle) |
| 48 return false; |
| 49 |
| 50 if (!pickle->WriteInt(kSecureHashVersion)) |
| 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) { |
| 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 const char* data = reinterpret_cast<char*>(&ctx_); |
| 80 if (!pickle->ReadBytes(&data_iterator, &data, sizeof(ctx_))) |
| 81 return false; |
| 82 |
| 83 return true; |
| 84 } |
| 85 |
37 } // namespace | 86 } // namespace |
38 | 87 |
39 SecureHash* SecureHash::Create(Algorithm algorithm) { | 88 SecureHash* SecureHash::Create(Algorithm algorithm) { |
40 switch (algorithm) { | 89 switch (algorithm) { |
41 case SHA256: | 90 case SHA256: |
42 return new SecureHashSHA256NSS(); | 91 return new SecureHashSHA256NSS(); |
43 default: | 92 default: |
44 NOTIMPLEMENTED(); | 93 NOTIMPLEMENTED(); |
45 return NULL; | 94 return NULL; |
46 } | 95 } |
47 } | 96 } |
48 | 97 |
49 } // namespace crypto | 98 } // namespace crypto |
OLD | NEW |