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 class DigestorNSS : public blink::WebCryptoDigestor { | |
Ryan Sleevi
2014/07/17 00:06:54
document
eroman
2014/07/17 20:37:26
Done: Documented as:
// Multipart input SHA diges
Ryan Sleevi
2014/07/17 22:42:55
Doesn't really help, at least for my novel reading
eroman
2014/07/17 23:33:23
Done. Will followup in issue 394945.
| |
40 public: | |
41 explicit DigestorNSS(blink::WebCryptoAlgorithmId algorithm_id) | |
42 : hash_context_(NULL), algorithm_id_(algorithm_id) {} | |
43 | |
44 virtual ~DigestorNSS() { | |
45 if (!hash_context_) | |
46 return; | |
47 | |
48 HASH_Destroy(hash_context_); | |
49 hash_context_ = NULL; | |
50 } | |
51 | |
52 virtual bool consume(const unsigned char* data, unsigned int size) { | |
53 return ConsumeWithStatus(data, size).IsSuccess(); | |
54 } | |
55 | |
56 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) { | |
57 // Initialize everything if the object hasn't been initialized yet. | |
58 if (!hash_context_) { | |
59 Status error = Init(); | |
60 if (!error.IsSuccess()) | |
61 return error; | |
62 } | |
63 | |
64 HASH_Update(hash_context_, data, size); | |
65 | |
66 return Status::Success(); | |
67 } | |
68 | |
69 virtual bool finish(unsigned char*& result_data, | |
70 unsigned int& result_data_size) { | |
71 Status error = FinishInternal(result_, &result_data_size); | |
72 if (!error.IsSuccess()) | |
73 return false; | |
74 result_data = result_; | |
75 return true; | |
76 } | |
77 | |
78 Status FinishWithVectorAndStatus(std::vector<uint8>* result) { | |
79 if (!hash_context_) | |
80 return Status::ErrorUnexpected(); | |
81 | |
82 unsigned int result_length = HASH_ResultLenContext(hash_context_); | |
83 result->resize(result_length); | |
84 unsigned char* digest = Uint8VectorStart(result); | |
85 unsigned int digest_size; // ignored | |
86 return FinishInternal(digest, &digest_size); | |
87 } | |
88 | |
89 private: | |
90 Status Init() { | |
91 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm_id_); | |
92 | |
93 if (hash_type == HASH_AlgNULL) | |
94 return Status::ErrorUnsupported(); | |
95 | |
96 hash_context_ = HASH_Create(hash_type); | |
97 if (!hash_context_) | |
98 return Status::OperationError(); | |
99 | |
100 HASH_Begin(hash_context_); | |
101 | |
102 return Status::Success(); | |
103 } | |
104 | |
105 Status FinishInternal(unsigned char* result, unsigned int* result_size) { | |
106 if (!hash_context_) { | |
107 Status error = Init(); | |
108 if (!error.IsSuccess()) | |
109 return error; | |
110 } | |
111 | |
112 unsigned int hash_result_length = HASH_ResultLenContext(hash_context_); | |
113 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); | |
114 | |
115 HASH_End(hash_context_, result, result_size, hash_result_length); | |
116 | |
117 if (*result_size != hash_result_length) | |
118 return Status::ErrorUnexpected(); | |
119 return Status::Success(); | |
120 } | |
121 | |
122 HASHContext* hash_context_; | |
123 blink::WebCryptoAlgorithmId algorithm_id_; | |
124 unsigned char result_[HASH_LENGTH_MAX]; | |
125 }; | |
126 | |
127 class ShaImplementation : public AlgorithmImplementation { | |
128 public: | |
129 virtual Status Digest(const blink::WebCryptoAlgorithm& algorithm, | |
130 const CryptoData& data, | |
131 std::vector<uint8>* buffer) const OVERRIDE { | |
132 DigestorNSS digestor(algorithm.id()); | |
133 Status error = digestor.ConsumeWithStatus(data.bytes(), data.byte_length()); | |
134 // http://crbug.com/366427: the spec does not define any other failures for | |
135 // digest, so none of the subsequent errors are spec compliant. | |
136 if (!error.IsSuccess()) | |
137 return error; | |
138 return digestor.FinishWithVectorAndStatus(buffer); | |
139 } | |
140 }; | |
141 | |
142 } // namespace | |
143 | |
144 AlgorithmImplementation* CreatePlatformShaImplementation() { | |
145 return new ShaImplementation(); | |
146 } | |
147 | |
148 scoped_ptr<blink::WebCryptoDigestor> CreatePlatformDigestor( | |
149 blink::WebCryptoAlgorithmId algorithm) { | |
150 return scoped_ptr<blink::WebCryptoDigestor>(new DigestorNSS(algorithm)); | |
151 } | |
152 | |
153 } // namespace webcrypto | |
154 | |
155 } // namespace content | |
OLD | NEW |