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