Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: content/child/webcrypto/nss/sha_nss.cc

Issue 1077273002: html_viewer: Move webcrypto to a place where html_viewer can use it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to ToT Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/child/webcrypto/nss/rsa_ssa_nss.cc ('k') | content/child/webcrypto/nss/sym_key_nss.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/stl_util.h"
9 #include "content/child/webcrypto/algorithm_implementation.h"
10 #include "content/child/webcrypto/crypto_data.h"
11 #include "content/child/webcrypto/nss/util_nss.h"
12 #include "content/child/webcrypto/status.h"
13 #include "content/child/webcrypto/webcrypto_util.h"
14 #include "crypto/nss_util.h"
15 #include "crypto/scoped_nss_types.h"
16
17 namespace content {
18
19 namespace webcrypto {
20
21 namespace {
22
23 HASH_HashType WebCryptoAlgorithmToNSSHashType(
24 blink::WebCryptoAlgorithmId algorithm) {
25 switch (algorithm) {
26 case blink::WebCryptoAlgorithmIdSha1:
27 return HASH_AlgSHA1;
28 case blink::WebCryptoAlgorithmIdSha256:
29 return HASH_AlgSHA256;
30 case blink::WebCryptoAlgorithmIdSha384:
31 return HASH_AlgSHA384;
32 case blink::WebCryptoAlgorithmIdSha512:
33 return HASH_AlgSHA512;
34 default:
35 // Not a digest algorithm.
36 return HASH_AlgNULL;
37 }
38 }
39
40 // Implementation of blink::WebCryptoDigester, an internal Blink detail not
41 // part of WebCrypto, that allows chunks of data to be streamed in before
42 // computing a SHA-* digest (as opposed to ShaImplementation, which computes
43 // digests over complete messages)
44 class DigestorNSS : public blink::WebCryptoDigestor {
45 public:
46 explicit DigestorNSS(blink::WebCryptoAlgorithmId algorithm_id)
47 : hash_context_(NULL), algorithm_id_(algorithm_id) {}
48
49 ~DigestorNSS() override {
50 if (!hash_context_)
51 return;
52
53 HASH_Destroy(hash_context_);
54 hash_context_ = NULL;
55 }
56
57 bool consume(const unsigned char* data, unsigned int size) override {
58 return ConsumeWithStatus(data, size).IsSuccess();
59 }
60
61 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) {
62 // Initialize everything if the object hasn't been initialized yet.
63 if (!hash_context_) {
64 Status error = Init();
65 if (!error.IsSuccess())
66 return error;
67 }
68
69 HASH_Update(hash_context_, data, size);
70
71 return Status::Success();
72 }
73
74 bool finish(unsigned char*& result_data,
75 unsigned int& result_data_size) override {
76 Status error = FinishInternal(result_, &result_data_size);
77 if (!error.IsSuccess())
78 return false;
79 result_data = result_;
80 return true;
81 }
82
83 Status FinishWithVectorAndStatus(std::vector<uint8_t>* result) {
84 if (!hash_context_)
85 return Status::ErrorUnexpected();
86
87 unsigned int result_length = HASH_ResultLenContext(hash_context_);
88 result->resize(result_length);
89 unsigned char* digest = vector_as_array(result);
90 unsigned int digest_size; // ignored
91 return FinishInternal(digest, &digest_size);
92 }
93
94 private:
95 Status Init() {
96 HASH_HashType hash_type = WebCryptoAlgorithmToNSSHashType(algorithm_id_);
97
98 if (hash_type == HASH_AlgNULL)
99 return Status::ErrorUnsupported();
100
101 hash_context_ = HASH_Create(hash_type);
102 if (!hash_context_)
103 return Status::OperationError();
104
105 HASH_Begin(hash_context_);
106
107 return Status::Success();
108 }
109
110 Status FinishInternal(unsigned char* result, unsigned int* result_size) {
111 if (!hash_context_) {
112 Status error = Init();
113 if (!error.IsSuccess())
114 return error;
115 }
116
117 unsigned int hash_result_length = HASH_ResultLenContext(hash_context_);
118 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX));
119
120 HASH_End(hash_context_, result, result_size, hash_result_length);
121
122 if (*result_size != hash_result_length)
123 return Status::ErrorUnexpected();
124 return Status::Success();
125 }
126
127 HASHContext* hash_context_;
128 blink::WebCryptoAlgorithmId algorithm_id_;
129 unsigned char result_[HASH_LENGTH_MAX];
130 };
131
132 class ShaImplementation : public AlgorithmImplementation {
133 public:
134 Status Digest(const blink::WebCryptoAlgorithm& algorithm,
135 const CryptoData& data,
136 std::vector<uint8_t>* buffer) const override {
137 DigestorNSS digestor(algorithm.id());
138 Status error = digestor.ConsumeWithStatus(data.bytes(), data.byte_length());
139 // http://crbug.com/366427: the spec does not define any other failures for
140 // digest, so none of the subsequent errors are spec compliant.
141 if (!error.IsSuccess())
142 return error;
143 return digestor.FinishWithVectorAndStatus(buffer);
144 }
145 };
146
147 } // namespace
148
149 AlgorithmImplementation* CreatePlatformShaImplementation() {
150 return new ShaImplementation();
151 }
152
153 scoped_ptr<blink::WebCryptoDigestor> CreatePlatformDigestor(
154 blink::WebCryptoAlgorithmId algorithm) {
155 return scoped_ptr<blink::WebCryptoDigestor>(new DigestorNSS(algorithm));
156 }
157
158 } // namespace webcrypto
159
160 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/nss/rsa_ssa_nss.cc ('k') | content/child/webcrypto/nss/sym_key_nss.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698