OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <sechash.h> | |
6 #include <vector> | |
7 | |
8 #include "content/child/webcrypto/algorithm_implementation.h" | |
9 #include "content/child/webcrypto/crypto_data.h" | |
10 #include "content/child/webcrypto/nss/util_nss.h" | |
11 #include "content/child/webcrypto/status.h" | |
12 #include "content/child/webcrypto/webcrypto_util.h" | |
13 #include "crypto/nss_util.h" | |
14 #include "crypto/scoped_nss_types.h" | |
15 | |
16 namespace content { | |
17 | |
18 namespace webcrypto { | |
19 | |
20 namespace { | |
21 | |
22 HASH_HashType WebCryptoAlgorithmToNSSHashType( | |
23 blink::WebCryptoAlgorithmId algorithm) { | |
24 switch (algorithm) { | |
25 case blink::WebCryptoAlgorithmIdSha1: | |
26 return HASH_AlgSHA1; | |
27 case blink::WebCryptoAlgorithmIdSha256: | |
28 return HASH_AlgSHA256; | |
29 case blink::WebCryptoAlgorithmIdSha384: | |
30 return HASH_AlgSHA384; | |
31 case blink::WebCryptoAlgorithmIdSha512: | |
32 return HASH_AlgSHA512; | |
33 default: | |
34 // Not a digest algorithm. | |
35 return HASH_AlgNULL; | |
36 } | |
37 } | |
38 | |
39 // Implementation of blink::WebCryptoDigester, an internal Blink detail not | |
40 // part of WebCrypto, that allows chunks of data to be streamed in before | |
41 // computing a SHA-* digest (as opposed to ShaImplementation, which computes | |
42 // digests over complete messages) | |
43 class DigestorNSS : public blink::WebCryptoDigestor { | |
44 public: | |
45 explicit DigestorNSS(blink::WebCryptoAlgorithmId algorithm_id) | |
46 : hash_context_(NULL), algorithm_id_(algorithm_id) {} | |
47 | |
48 virtual ~DigestorNSS() { | |
49 if (!hash_context_) | |
50 return; | |
51 | |
52 HASH_Destroy(hash_context_); | |
53 hash_context_ = NULL; | |
54 } | |
55 | |
56 virtual bool consume(const unsigned char* data, unsigned int size) { | |
57 return ConsumeWithStatus(data, size).IsSuccess(); | |
58 } | |
59 | |
60 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) { | |
61 // Initialize everything if the object hasn't been initialized yet. | |
62 if (!hash_context_) { | |
63 Status error = Init(); | |
64 if (!error.IsSuccess()) | |
65 return error; | |
66 } | |
67 | |
68 HASH_Update(hash_context_, data, size); | |
69 | |
70 return Status::Success(); | |
71 } | |
72 | |
73 virtual bool finish(unsigned char*& result_data, | |
74 unsigned int& result_data_size) { | |
75 Status error = FinishInternal(result_, &result_data_size); | |
76 if (!error.IsSuccess()) | |
77 return false; | |
78 result_data = result_; | |
79 return true; | |
80 } | |
81 | |
82 Status FinishWithVectorAndStatus(std::vector<uint8>* result) { | |
83 if (!hash_context_) | |
84 return Status::ErrorUnexpected(); | |
85 | |
86 unsigned int result_length = HASH_ResultLenContext(hash_context_); | |
87 result->resize(result_length); | |
88 unsigned char* digest = Uint8VectorStart(result); | |
89 unsigned int digest_size; // ignored | |
90 return FinishInternal(digest, &digest_size); | |
91 } | |
92 | |
93 private: | |
94 Status Init() { | |
95 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm_id_); | |
96 | |
97 if (hash_type == HASH_AlgNULL) | |
98 return Status::ErrorUnsupported(); | |
99 | |
100 hash_context_ = HASH_Create(hash_type); | |
101 if (!hash_context_) | |
102 return Status::OperationError(); | |
103 | |
104 HASH_Begin(hash_context_); | |
105 | |
106 return Status::Success(); | |
107 } | |
108 | |
109 Status FinishInternal(unsigned char* result, unsigned int* result_size) { | |
110 if (!hash_context_) { | |
111 Status error = Init(); | |
112 if (!error.IsSuccess()) | |
113 return error; | |
114 } | |
115 | |
116 unsigned int hash_result_length = HASH_ResultLenContext(hash_context_); | |
117 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); | |
118 | |
119 HASH_End(hash_context_, result, result_size, hash_result_length); | |
120 | |
121 if (*result_size != hash_result_length) | |
122 return Status::ErrorUnexpected(); | |
123 return Status::Success(); | |
124 } | |
125 | |
126 HASHContext* hash_context_; | |
127 blink::WebCryptoAlgorithmId algorithm_id_; | |
128 unsigned char result_[HASH_LENGTH_MAX]; | |
129 }; | |
130 | |
131 class ShaImplementation : public AlgorithmImplementation { | |
132 public: | |
133 virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm, | |
134 const CryptoData& data, | |
135 std::vector<uint8>* buffer) const OVERRIDE { | |
136 DigestorNSS digestor(algorithm.id()); | |
137 Status error = digestor.ConsumeWithStatus(data.bytes(), data.byte_length()); | |
138 // http://crbug.com/366427: the spec does not define any other failures for | |
139 // digest, so none of the subsequent errors are spec compliant. | |
140 if (!error.IsSuccess()) | |
141 return error; | |
142 return digestor.FinishWithVectorAndStatus(buffer); | |
143 } | |
144 }; | |
145 | |
146 } // namespace | |
147 | |
148 AlgorithmImplementation* CreatePlatformShaImplementation() { | |
149 return new ShaImplementation(); | |
150 } | |
151 | |
152 scoped_ptr<blink::WebCryptoDigestor> CreatePlatformDigestor( | |
153 blink::WebCryptoAlgorithmId algorithm) { | |
154 return scoped_ptr<blink::WebCryptoDigestor>(new DigestorNSS(algorithm)); | |
155 } | |
156 | |
157 } // namespace webcrypto | |
158 | |
159 } // namespace content | |
OLD | NEW |