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

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

Issue 749183004: WebCrypto: Implement crypto.subtle.deriveKey (chromium-side). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ecdh
Patch Set: rebase Created 6 years 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/webcrypto_impl.h ('k') | no next file » | 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/webcrypto_impl.h" 5 #include "content/child/webcrypto/webcrypto_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 base_key(base_key), 343 base_key(base_key),
344 length_bits(length_bits) {} 344 length_bits(length_bits) {}
345 345
346 const blink::WebCryptoAlgorithm algorithm; 346 const blink::WebCryptoAlgorithm algorithm;
347 const blink::WebCryptoKey base_key; 347 const blink::WebCryptoKey base_key;
348 const unsigned int length_bits; 348 const unsigned int length_bits;
349 349
350 std::vector<uint8_t> derived_bytes; 350 std::vector<uint8_t> derived_bytes;
351 }; 351 };
352 352
353 struct DeriveKeyState : public BaseState {
354 DeriveKeyState(const blink::WebCryptoAlgorithm& algorithm,
355 const blink::WebCryptoKey& base_key,
356 const blink::WebCryptoAlgorithm& import_algorithm,
357 const blink::WebCryptoAlgorithm& key_length_algorithm,
358 bool extractable,
359 blink::WebCryptoKeyUsageMask usages,
360 const blink::WebCryptoResult& result)
361 : BaseState(result),
362 algorithm(algorithm),
363 base_key(base_key),
364 import_algorithm(import_algorithm),
365 key_length_algorithm(key_length_algorithm),
366 extractable(extractable),
367 usages(usages) {}
368
369 const blink::WebCryptoAlgorithm algorithm;
370 const blink::WebCryptoKey base_key;
371 const blink::WebCryptoAlgorithm import_algorithm;
372 const blink::WebCryptoAlgorithm key_length_algorithm;
373 bool extractable;
374 blink::WebCryptoKeyUsageMask usages;
375
376 blink::WebCryptoKey derived_key;
377 };
378
353 // -------------------------------------------------------------------- 379 // --------------------------------------------------------------------
354 // Wrapper functions 380 // Wrapper functions
355 // -------------------------------------------------------------------- 381 // --------------------------------------------------------------------
356 // 382 //
357 // * The methods named Do*() run on the crypto thread. 383 // * The methods named Do*() run on the crypto thread.
358 // * The methods named Do*Reply() run on the target Blink thread 384 // * The methods named Do*Reply() run on the target Blink thread
359 385
360 void DoEncryptReply(scoped_ptr<EncryptState> state) { 386 void DoEncryptReply(scoped_ptr<EncryptState> state) {
361 CompleteWithBufferOrError(state->status, state->buffer, &state->result); 387 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
362 } 388 }
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 DeriveBitsState* state = passed_state.get(); 571 DeriveBitsState* state = passed_state.get();
546 if (state->cancelled()) 572 if (state->cancelled())
547 return; 573 return;
548 state->status = 574 state->status =
549 webcrypto::DeriveBits(state->algorithm, state->base_key, 575 webcrypto::DeriveBits(state->algorithm, state->base_key,
550 state->length_bits, &state->derived_bytes); 576 state->length_bits, &state->derived_bytes);
551 state->origin_thread->PostTask( 577 state->origin_thread->PostTask(
552 FROM_HERE, base::Bind(DoDeriveBitsReply, Passed(&passed_state))); 578 FROM_HERE, base::Bind(DoDeriveBitsReply, Passed(&passed_state)));
553 } 579 }
554 580
581 void DoDeriveKeyReply(scoped_ptr<DeriveKeyState> state) {
582 CompleteWithKeyOrError(state->status, state->derived_key, &state->result);
583 }
584
585 void DoDeriveKey(scoped_ptr<DeriveKeyState> passed_state) {
586 DeriveKeyState* state = passed_state.get();
587 if (state->cancelled())
588 return;
589 state->status = webcrypto::DeriveKey(
590 state->algorithm, state->base_key, state->import_algorithm,
591 state->key_length_algorithm, state->extractable, state->usages,
592 &state->derived_key);
593 state->origin_thread->PostTask(
594 FROM_HERE, base::Bind(DoDeriveKeyReply, Passed(&passed_state)));
595 }
596
555 } // namespace 597 } // namespace
556 598
557 WebCryptoImpl::WebCryptoImpl() { 599 WebCryptoImpl::WebCryptoImpl() {
558 } 600 }
559 601
560 WebCryptoImpl::~WebCryptoImpl() { 602 WebCryptoImpl::~WebCryptoImpl() {
561 } 603 }
562 604
563 void WebCryptoImpl::encrypt(const blink::WebCryptoAlgorithm& algorithm, 605 void WebCryptoImpl::encrypt(const blink::WebCryptoAlgorithm& algorithm,
564 const blink::WebCryptoKey& key, 606 const blink::WebCryptoKey& key,
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 unsigned int length_bits, 750 unsigned int length_bits,
709 blink::WebCryptoResult result) { 751 blink::WebCryptoResult result) {
710 scoped_ptr<DeriveBitsState> state( 752 scoped_ptr<DeriveBitsState> state(
711 new DeriveBitsState(algorithm, base_key, length_bits, result)); 753 new DeriveBitsState(algorithm, base_key, length_bits, result));
712 if (!CryptoThreadPool::PostTask(FROM_HERE, 754 if (!CryptoThreadPool::PostTask(FROM_HERE,
713 base::Bind(DoDeriveBits, Passed(&state)))) { 755 base::Bind(DoDeriveBits, Passed(&state)))) {
714 CompleteWithThreadPoolError(&result); 756 CompleteWithThreadPoolError(&result);
715 } 757 }
716 } 758 }
717 759
760 void WebCryptoImpl::deriveKey(
761 const blink::WebCryptoAlgorithm& algorithm,
762 const blink::WebCryptoKey& base_key,
763 const blink::WebCryptoAlgorithm& import_algorithm,
764 const blink::WebCryptoAlgorithm& key_length_algorithm,
765 bool extractable,
766 blink::WebCryptoKeyUsageMask usages,
767 blink::WebCryptoResult result) {
768 scoped_ptr<DeriveKeyState> state(
769 new DeriveKeyState(algorithm, base_key, import_algorithm,
770 key_length_algorithm, extractable, usages, result));
771 if (!CryptoThreadPool::PostTask(FROM_HERE,
772 base::Bind(DoDeriveKey, Passed(&state)))) {
773 CompleteWithThreadPoolError(&result);
774 }
775 }
776
718 blink::WebCryptoDigestor* WebCryptoImpl::createDigestor( 777 blink::WebCryptoDigestor* WebCryptoImpl::createDigestor(
719 blink::WebCryptoAlgorithmId algorithm_id) { 778 blink::WebCryptoAlgorithmId algorithm_id) {
720 return webcrypto::CreateDigestor(algorithm_id).release(); 779 return webcrypto::CreateDigestor(algorithm_id).release();
721 } 780 }
722 781
723 bool WebCryptoImpl::deserializeKeyForClone( 782 bool WebCryptoImpl::deserializeKeyForClone(
724 const blink::WebCryptoKeyAlgorithm& algorithm, 783 const blink::WebCryptoKeyAlgorithm& algorithm,
725 blink::WebCryptoKeyType type, 784 blink::WebCryptoKeyType type,
726 bool extractable, 785 bool extractable,
727 blink::WebCryptoKeyUsageMask usages, 786 blink::WebCryptoKeyUsageMask usages,
728 const unsigned char* key_data, 787 const unsigned char* key_data,
729 unsigned key_data_size, 788 unsigned key_data_size,
730 blink::WebCryptoKey& key) { 789 blink::WebCryptoKey& key) {
731 return webcrypto::DeserializeKeyForClone( 790 return webcrypto::DeserializeKeyForClone(
732 algorithm, type, extractable, usages, 791 algorithm, type, extractable, usages,
733 webcrypto::CryptoData(key_data, key_data_size), &key); 792 webcrypto::CryptoData(key_data, key_data_size), &key);
734 } 793 }
735 794
736 bool WebCryptoImpl::serializeKeyForClone( 795 bool WebCryptoImpl::serializeKeyForClone(
737 const blink::WebCryptoKey& key, 796 const blink::WebCryptoKey& key,
738 blink::WebVector<unsigned char>& key_data) { 797 blink::WebVector<unsigned char>& key_data) {
739 return webcrypto::SerializeKeyForClone(key, &key_data); 798 return webcrypto::SerializeKeyForClone(key, &key_data);
740 } 799 }
741 800
742 } // namespace content 801 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/webcrypto_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698