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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
9 #include "crypto/third_party/nss/chromium-blapi.h" | 9 #include "crypto/third_party/nss/chromium-blapi.h" |
10 #include "crypto/third_party/nss/chromium-sha256.h" | 10 #include "crypto/third_party/nss/chromium-sha256.h" |
11 | 11 |
12 namespace crypto { | 12 namespace crypto { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 const char kSHA256Descriptor[] = "NSS"; | 16 const char kSHA256Descriptor[] = "NSS"; |
17 | 17 |
18 class SecureHashSHA256NSS : public SecureHash { | 18 class SecureHashSHA256NSS : public SecureHash { |
19 public: | 19 public: |
20 static const int kSecureHashVersion = 1; | 20 static const int kSecureHashVersion = 1; |
21 | 21 |
22 SecureHashSHA256NSS() { | 22 SecureHashSHA256NSS() { |
23 SHA256_Begin(&ctx_); | 23 SHA256_Begin(&ctx_); |
24 } | 24 } |
25 | 25 |
26 virtual ~SecureHashSHA256NSS() { | 26 ~SecureHashSHA256NSS() override { memset(&ctx_, 0, sizeof(ctx_)); } |
27 memset(&ctx_, 0, sizeof(ctx_)); | |
28 } | |
29 | 27 |
30 // SecureHash implementation: | 28 // SecureHash implementation: |
31 virtual void Update(const void* input, size_t len) override { | 29 void Update(const void* input, size_t len) override { |
32 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); | 30 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
33 } | 31 } |
34 | 32 |
35 virtual void Finish(void* output, size_t len) override { | 33 void Finish(void* output, size_t len) override { |
36 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL, | 34 SHA256_End(&ctx_, static_cast<unsigned char*>(output), NULL, |
37 static_cast<unsigned int>(len)); | 35 static_cast<unsigned int>(len)); |
38 } | 36 } |
39 | 37 |
40 virtual bool Serialize(Pickle* pickle) override; | 38 bool Serialize(Pickle* pickle) override; |
41 virtual bool Deserialize(PickleIterator* data_iterator) override; | 39 bool Deserialize(PickleIterator* data_iterator) override; |
42 | 40 |
43 private: | 41 private: |
44 SHA256Context ctx_; | 42 SHA256Context ctx_; |
45 }; | 43 }; |
46 | 44 |
47 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) { | 45 bool SecureHashSHA256NSS::Serialize(Pickle* pickle) { |
48 if (!pickle) | 46 if (!pickle) |
49 return false; | 47 return false; |
50 | 48 |
51 if (!pickle->WriteInt(kSecureHashVersion) || | 49 if (!pickle->WriteInt(kSecureHashVersion) || |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 switch (algorithm) { | 85 switch (algorithm) { |
88 case SHA256: | 86 case SHA256: |
89 return new SecureHashSHA256NSS(); | 87 return new SecureHashSHA256NSS(); |
90 default: | 88 default: |
91 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
92 return NULL; | 90 return NULL; |
93 } | 91 } |
94 } | 92 } |
95 | 93 |
96 } // namespace crypto | 94 } // namespace crypto |
OLD | NEW |