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