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

Side by Side Diff: content/renderer/webcrypto/webcrypto_impl_unittest.cc

Issue 34583010: [webcrypto] Add RSA key generation using NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re-upload after 500 server failure Created 7 years, 2 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "webcrypto_impl.h" 5 #include "webcrypto_impl.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 return &data[0]; 52 return &data[0];
53 } 53 }
54 54
55 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( 55 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(
56 const std::vector<uint8>& iv) { 56 const std::vector<uint8>& iv) {
57 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( 57 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
58 WebKit::WebCryptoAlgorithmIdAesCbc, 58 WebKit::WebCryptoAlgorithmIdAesCbc,
59 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); 59 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size()));
60 } 60 }
61 61
62 WebKit::WebCryptoAlgorithm CreateRsaAlgorithm(
63 WebKit::WebCryptoAlgorithmId algorithm_id,
64 unsigned modulus_length,
65 const std::vector<unsigned char> public_exponent) {
eroman 2013/10/23 20:02:46 make this a const reference.
padolph 2013/10/23 23:21:47 Oops. Thanks for catching that.
66 DCHECK(algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
67 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
68 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaOaep);
69 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
70 algorithm_id,
71 new WebKit::WebCryptoRsaKeyGenParams(
72 modulus_length, Start(public_exponent), public_exponent.size()));
73 }
74
62 } // namespace 75 } // namespace
63 76
64 namespace content { 77 namespace content {
65 78
66 class WebCryptoImplTest : public testing::Test { 79 class WebCryptoImplTest : public testing::Test {
67 protected: 80 protected:
68 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString( 81 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString(
69 const std::string& key_hex, 82 const std::string& key_hex,
70 const WebKit::WebCryptoAlgorithm& algorithm, 83 const WebKit::WebCryptoAlgorithm& algorithm,
71 WebKit::WebCryptoKeyUsageMask usage) { 84 WebKit::WebCryptoKeyUsageMask usage) {
(...skipping 27 matching lines...) Expand all
99 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer); 112 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer);
100 } 113 }
101 114
102 bool GenerateKeyInternal( 115 bool GenerateKeyInternal(
103 const WebKit::WebCryptoAlgorithm& algorithm, 116 const WebKit::WebCryptoAlgorithm& algorithm,
104 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, 117 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle,
105 WebKit::WebCryptoKeyType* type) { 118 WebKit::WebCryptoKeyType* type) {
106 return crypto_.GenerateKeyInternal(algorithm, handle, type); 119 return crypto_.GenerateKeyInternal(algorithm, handle, type);
107 } 120 }
108 121
122 bool GenerateKeyPairInternal(
123 const WebKit::WebCryptoAlgorithm& algorithm,
124 scoped_ptr<WebKit::WebCryptoKeyHandle>* public_key,
125 scoped_ptr<WebKit::WebCryptoKeyHandle>* private_key) {
126 return crypto_.GenerateKeyPairInternal(algorithm, public_key, private_key);
127 }
128
109 bool ImportKeyInternal( 129 bool ImportKeyInternal(
110 WebKit::WebCryptoKeyFormat format, 130 WebKit::WebCryptoKeyFormat format,
111 const std::vector<uint8>& key_data, 131 const std::vector<uint8>& key_data,
112 const WebKit::WebCryptoAlgorithm& algorithm, 132 const WebKit::WebCryptoAlgorithm& algorithm,
113 WebKit::WebCryptoKeyUsageMask usage_mask, 133 WebKit::WebCryptoKeyUsageMask usage_mask,
114 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, 134 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle,
115 WebKit::WebCryptoKeyType* type) { 135 WebKit::WebCryptoKeyType* type) {
116 return crypto_.ImportKeyInternal(format, 136 return crypto_.ImportKeyInternal(format,
117 Start(key_data), 137 Start(key_data),
118 key_data.size(), 138 key_data.size(),
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 WebKit::WebCryptoAlgorithmIdHmac, params.release())); 682 WebKit::WebCryptoAlgorithmIdHmac, params.release()));
663 683
664 scoped_ptr<WebKit::WebCryptoKeyHandle> result; 684 scoped_ptr<WebKit::WebCryptoKeyHandle> result;
665 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic; 685 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic;
666 686
667 ASSERT_TRUE(GenerateKeyInternal(algorithm, &result, &type)); 687 ASSERT_TRUE(GenerateKeyInternal(algorithm, &result, &type));
668 EXPECT_TRUE(bool(result)); 688 EXPECT_TRUE(bool(result));
669 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret); 689 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret);
670 } 690 }
671 691
672 #endif //#if !defined(USE_OPENSSL) 692 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) {
693 // Note: using unrealistic short key lengths here to avoid bogging down tests
694 // happy WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key gen
695 unsigned modulus_length = 256;
696 const unsigned char f4[3] = {0x01, 0x00, 0x01};
697 std::vector<unsigned char> public_exponent(&f4[0], &f4[0] + sizeof(f4));
698 WebKit::WebCryptoAlgorithm algorithm =
699 CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
700 modulus_length,
701 public_exponent);
702 scoped_ptr<WebKit::WebCryptoKeyHandle> public_key_handle;
703 scoped_ptr<WebKit::WebCryptoKeyHandle> private_key_handle;
704 EXPECT_TRUE(GenerateKeyPairInternal(
705 algorithm, &public_key_handle, &private_key_handle));
706 EXPECT_TRUE(public_key_handle);
707 EXPECT_TRUE(private_key_handle);
708
709 // bad modulus
710 modulus_length = 0;
711 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
712 modulus_length,
713 public_exponent);
714 EXPECT_FALSE(GenerateKeyPairInternal(
715 algorithm, &public_key_handle, &private_key_handle));
716
717 // bad exponent
718 modulus_length = 256;
719 public_exponent.clear();
720 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
721 modulus_length,
722 public_exponent);
723 EXPECT_FALSE(GenerateKeyPairInternal(
724 algorithm, &public_key_handle, &private_key_handle));
725
726 // happy WebCryptoAlgorithmIdRsaOaep
727 public_exponent.assign(&f4[0], &f4[0] + sizeof(f4));
728 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaOaep,
729 modulus_length,
730 public_exponent);
731 EXPECT_TRUE(GenerateKeyPairInternal(
732 algorithm, &public_key_handle, &private_key_handle));
733 EXPECT_TRUE(public_key_handle);
734 EXPECT_TRUE(private_key_handle);
735
736 // happy WebCryptoAlgorithmIdRsaSsaPkcs1v1_5
737 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
738 modulus_length,
739 public_exponent);
740 EXPECT_TRUE(GenerateKeyPairInternal(
741 algorithm, &public_key_handle, &private_key_handle));
742 EXPECT_TRUE(public_key_handle);
743 EXPECT_TRUE(private_key_handle);
744 }
eroman 2013/10/23 20:02:46 Can you add tests where: * exponenent is larger t
padolph 2013/10/23 23:21:47 Done.
745
746 #endif // #if !defined(USE_OPENSSL)
673 747
674 } // namespace content 748 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698