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

Side by Side Diff: content/child/webcrypto/platform_crypto_openssl.cc

Issue 200763005: Support for the new WebCrypto digest by chunks API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Get rid of C++11 function call Created 6 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 | Annotate | Revision Log
« no previous file with comments | « content/child/webcrypto/platform_crypto_nss.cc ('k') | content/child/webcrypto/shared_crypto.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "content/child/webcrypto/platform_crypto.h" 5 #include "content/child/webcrypto/platform_crypto.h"
6 6
7 #include <vector> 7 #include <vector>
8 #include <openssl/aes.h> 8 #include <openssl/aes.h>
9 #include <openssl/evp.h> 9 #include <openssl/evp.h>
10 #include <openssl/hmac.h> 10 #include <openssl/hmac.h>
11 #include <openssl/rand.h> 11 #include <openssl/rand.h>
12 #include <openssl/sha.h> 12 #include <openssl/sha.h>
13 13
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
15 #include "content/child/webcrypto/crypto_data.h" 16 #include "content/child/webcrypto/crypto_data.h"
16 #include "content/child/webcrypto/status.h" 17 #include "content/child/webcrypto/status.h"
17 #include "content/child/webcrypto/webcrypto_util.h" 18 #include "content/child/webcrypto/webcrypto_util.h"
18 #include "crypto/openssl_util.h" 19 #include "crypto/openssl_util.h"
19 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" 20 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
22 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" 23 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
23 24
24 namespace content { 25 namespace content {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 static_cast<unsigned int>(final_output_chunk_len); 145 static_cast<unsigned int>(final_output_chunk_len);
145 DCHECK_LE(final_output_len, output_max_len); 146 DCHECK_LE(final_output_len, output_max_len);
146 147
147 ShrinkBuffer(buffer, final_output_len); 148 ShrinkBuffer(buffer, final_output_len);
148 149
149 return Status::Success(); 150 return Status::Success();
150 } 151 }
151 152
152 } // namespace 153 } // namespace
153 154
155 class DigestorOpenSSL : public blink::WebCryptoDigestor {
156 public:
157 explicit DigestorOpenSSL(blink::WebCryptoAlgorithmId algorithm_id)
158 : initialized_(false),
159 digest_context_(EVP_MD_CTX_create()),
160 algorithm_id_(algorithm_id) {}
161
162 virtual bool consume(const unsigned char* data, unsigned int size) {
163 return ConsumeWithStatus(data, size).IsSuccess();
164 }
165
166 Status ConsumeWithStatus(const unsigned char* data, unsigned int size) {
167 crypto::OpenSSLErrStackTracer(FROM_HERE);
168 Status error = Init();
169 if (!error.IsSuccess())
170 return error;
171
172 if (!EVP_DigestUpdate(digest_context_.get(), data, size))
173 return Status::Error();
174
175 return Status::Success();
176 }
177
178 virtual bool finish(unsigned char*& result_data,
179 unsigned int& result_data_size) {
180 Status error = FinishInternal(result_, &result_data_size);
181 if (!error.IsSuccess())
182 return false;
183 result_data = result_;
184 return true;
185 }
186
187 Status FinishWithWebArrayAndStatus(blink::WebArrayBuffer* result) {
188 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
189 *result = blink::WebArrayBuffer::create(hash_expected_size, 1);
190 unsigned char* const hash_buffer =
191 static_cast<unsigned char* const>(result->data());
192 unsigned int hash_buffer_size; // ignored
193 Status error = FinishInternal(hash_buffer, &hash_buffer_size);
194 if (!error.IsSuccess())
195 result->reset();
196 return error;
197 }
198
199 private:
200 Status Init() {
201 if (initialized_)
202 return Status::Success();
203
204 const EVP_MD* digest_algorithm = GetDigest(algorithm_id_);
205 if (!digest_algorithm)
206 return Status::ErrorUnexpected();
207
208 if (!digest_context_.get())
209 return Status::Error();
210
211 if (!EVP_DigestInit_ex(digest_context_.get(), digest_algorithm, NULL))
212 return Status::Error();
213
214 initialized_ = true;
215 return Status::Success();
216 }
217
218 Status FinishInternal(unsigned char* result, unsigned int* result_size) {
219 crypto::OpenSSLErrStackTracer(FROM_HERE);
220 Status error = Init();
221 if (!error.IsSuccess())
222 return error;
223
224 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get());
225 if (hash_expected_size <= 0)
226 return Status::ErrorUnexpected();
227 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE);
228
229 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) ||
230 static_cast<int>(*result_size) != hash_expected_size)
231 return Status::Error();
232
233 return Status::Success();
234 }
235
236 bool initialized_;
237 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context_;
238 blink::WebCryptoAlgorithmId algorithm_id_;
239 unsigned char result_[EVP_MAX_MD_SIZE];
240 };
241
154 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) { 242 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) {
155 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size()); 243 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size());
156 return Status::Success(); 244 return Status::Success();
157 } 245 }
158 246
159 void Init() { crypto::EnsureOpenSSLInit(); } 247 void Init() { crypto::EnsureOpenSSLInit(); }
160 248
161 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode, 249 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode,
162 SymKey* key, 250 SymKey* key,
163 const CryptoData& data, 251 const CryptoData& data,
164 const CryptoData& iv, 252 const CryptoData& iv,
165 blink::WebArrayBuffer* buffer) { 253 blink::WebArrayBuffer* buffer) {
166 // TODO(eroman): inline the function here. 254 // TODO(eroman): inline the function here.
167 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer); 255 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer);
168 } 256 }
169 257
170 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, 258 Status DigestSha(blink::WebCryptoAlgorithmId algorithm,
171 const CryptoData& data, 259 const CryptoData& data,
172 blink::WebArrayBuffer* buffer) { 260 blink::WebArrayBuffer* buffer) {
173 crypto::OpenSSLErrStackTracer(FROM_HERE); 261 DigestorOpenSSL digestor(algorithm);
262 Status error = digestor.ConsumeWithStatus(data.bytes(), data.byte_length());
263 if (!error.IsSuccess())
264 return error;
265 return digestor.FinishWithWebArrayAndStatus(buffer);
266 }
174 267
175 const EVP_MD* digest_algorithm = GetDigest(algorithm); 268 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor(
176 if (!digest_algorithm) 269 blink::WebCryptoAlgorithmId algorithm_id) {
177 return Status::ErrorUnexpected(); 270 return scoped_ptr<blink::WebCryptoDigestor>(
178 271 new DigestorOpenSSL(algorithm_id));
179 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context(
180 EVP_MD_CTX_create());
181 if (!digest_context.get())
182 return Status::Error();
183
184 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) ||
185 !EVP_DigestUpdate(
186 digest_context.get(), data.bytes(), data.byte_length())) {
187 return Status::Error();
188 }
189
190 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get());
191 if (hash_expected_size <= 0)
192 return Status::ErrorUnexpected();
193 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE);
194
195 *buffer = blink::WebArrayBuffer::create(hash_expected_size, 1);
196 unsigned char* const hash_buffer =
197 reinterpret_cast<unsigned char* const>(buffer->data());
198
199 unsigned int hash_size = 0;
200 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) ||
201 static_cast<int>(hash_size) != hash_expected_size) {
202 buffer->reset();
203 return Status::Error();
204 }
205
206 return Status::Success();
207 } 272 }
208 273
209 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, 274 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm,
210 bool extractable, 275 bool extractable,
211 blink::WebCryptoKeyUsageMask usage_mask, 276 blink::WebCryptoKeyUsageMask usage_mask,
212 unsigned keylen_bytes, 277 unsigned keylen_bytes,
213 blink::WebCryptoKey* key) { 278 blink::WebCryptoKey* key) {
214 // TODO(eroman): Is this right? 279 // TODO(eroman): Is this right?
215 if (keylen_bytes == 0) 280 if (keylen_bytes == 0)
216 return Status::ErrorGenerateKeyLength(); 281 return Status::ErrorGenerateKeyLength();
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 blink::WebCryptoKey* key) { 505 blink::WebCryptoKey* key) {
441 // TODO(eroman): http://crbug.com/267888 506 // TODO(eroman): http://crbug.com/267888
442 return Status::ErrorUnsupported(); 507 return Status::ErrorUnsupported();
443 } 508 }
444 509
445 } // namespace platform 510 } // namespace platform
446 511
447 } // namespace webcrypto 512 } // namespace webcrypto
448 513
449 } // namespace content 514 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/platform_crypto_nss.cc ('k') | content/child/webcrypto/shared_crypto.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698