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