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

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: fixes for eroman Created 7 years, 1 month 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size())); 70 new WebKit::WebCryptoAesCbcParams(Start(iv), iv.size()));
71 } 71 }
72 72
73 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm( 73 WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(
74 unsigned short key_length_bits) { 74 unsigned short key_length_bits) {
75 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( 75 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
76 WebKit::WebCryptoAlgorithmIdAesCbc, 76 WebKit::WebCryptoAlgorithmIdAesCbc,
77 new WebKit::WebCryptoAesKeyGenParams(key_length_bits)); 77 new WebKit::WebCryptoAesKeyGenParams(key_length_bits));
78 } 78 }
79 79
80 WebKit::WebCryptoAlgorithm CreateRsaAlgorithm(
81 WebKit::WebCryptoAlgorithmId algorithm_id,
82 unsigned modulus_length,
83 const std::vector<uint8>& public_exponent) {
84 DCHECK(algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
85 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
86 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaOaep);
87 return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
88 algorithm_id,
89 new WebKit::WebCryptoRsaKeyGenParams(
90 modulus_length, Start(public_exponent), public_exponent.size()));
91 }
92
80 } // namespace 93 } // namespace
81 94
82 namespace content { 95 namespace content {
83 96
84 class WebCryptoImplTest : public testing::Test { 97 class WebCryptoImplTest : public testing::Test {
85 protected: 98 protected:
86 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString( 99 WebKit::WebCryptoKey ImportSecretKeyFromRawHexString(
87 const std::string& key_hex, 100 const std::string& key_hex,
88 const WebKit::WebCryptoAlgorithm& algorithm, 101 const WebKit::WebCryptoAlgorithm& algorithm,
89 WebKit::WebCryptoKeyUsageMask usage) { 102 WebKit::WebCryptoKeyUsageMask usage) {
(...skipping 27 matching lines...) Expand all
117 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer); 130 return crypto_.DigestInternal(algorithm, Start(data), data.size(), buffer);
118 } 131 }
119 132
120 bool GenerateKeyInternal( 133 bool GenerateKeyInternal(
121 const WebKit::WebCryptoAlgorithm& algorithm, 134 const WebKit::WebCryptoAlgorithm& algorithm,
122 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, 135 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle,
123 WebKit::WebCryptoKeyType* type) { 136 WebKit::WebCryptoKeyType* type) {
124 return crypto_.GenerateKeyInternal(algorithm, handle, type); 137 return crypto_.GenerateKeyInternal(algorithm, handle, type);
125 } 138 }
126 139
140 bool GenerateKeyPairInternal(
141 const WebKit::WebCryptoAlgorithm& algorithm,
142 scoped_ptr<WebKit::WebCryptoKeyHandle>* public_key,
143 scoped_ptr<WebKit::WebCryptoKeyHandle>* private_key) {
144 return crypto_.GenerateKeyPairInternal(algorithm, public_key, private_key);
145 }
146
127 bool ImportKeyInternal( 147 bool ImportKeyInternal(
128 WebKit::WebCryptoKeyFormat format, 148 WebKit::WebCryptoKeyFormat format,
129 const std::vector<uint8>& key_data, 149 const std::vector<uint8>& key_data,
130 const WebKit::WebCryptoAlgorithm& algorithm, 150 const WebKit::WebCryptoAlgorithm& algorithm,
131 WebKit::WebCryptoKeyUsageMask usage_mask, 151 WebKit::WebCryptoKeyUsageMask usage_mask,
132 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, 152 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle,
133 WebKit::WebCryptoKeyType* type) { 153 WebKit::WebCryptoKeyType* type) {
134 return crypto_.ImportKeyInternal(format, 154 return crypto_.ImportKeyInternal(format,
135 Start(key_data), 155 Start(key_data),
136 key_data.size(), 156 key_data.size(),
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) { 669 TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) {
650 scoped_ptr<WebKit::WebCryptoKeyHandle> result; 670 scoped_ptr<WebKit::WebCryptoKeyHandle> result;
651 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic; 671 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypePublic;
652 WebKit::WebCryptoAlgorithm algorithm = 672 WebKit::WebCryptoAlgorithm algorithm =
653 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 0); 673 CreateHmacKeyAlgorithm(WebKit::WebCryptoAlgorithmIdSha1, 0);
654 ASSERT_TRUE(GenerateKeyInternal(algorithm, &result, &type)); 674 ASSERT_TRUE(GenerateKeyInternal(algorithm, &result, &type));
655 EXPECT_TRUE(result); 675 EXPECT_TRUE(result);
656 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret); 676 EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret);
657 } 677 }
658 678
679 #if !defined(USE_OPENSSL)
680
681 TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) {
682
683 // Note: using unrealistic short key lengths here to avoid bogging down tests.
684
685 // happy WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key gen
eroman 2013/10/28 20:00:47 Could you capitalize the comment and end with a pe
padolph 2013/10/28 21:08:53 Done.
686 unsigned modulus_length = 256;
687 std::vector<uint8> public_exponent = HexStringToBytes("010001");
688 WebKit::WebCryptoAlgorithm algorithm =
689 CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
690 modulus_length,
691 public_exponent);
692 scoped_ptr<WebKit::WebCryptoKeyHandle> public_key_handle;
693 scoped_ptr<WebKit::WebCryptoKeyHandle> private_key_handle;
694 EXPECT_TRUE(GenerateKeyPairInternal(
695 algorithm, &public_key_handle, &private_key_handle));
696 EXPECT_TRUE(public_key_handle);
697 EXPECT_TRUE(private_key_handle);
698
699 // bad modulus
eroman 2013/10/28 20:00:47 Please capitalize for consistency throughout.
padolph 2013/10/28 21:08:53 Done.
700 modulus_length = 0;
701 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
702 modulus_length,
703 public_exponent);
704 EXPECT_FALSE(GenerateKeyPairInternal(
705 algorithm, &public_key_handle, &private_key_handle));
706 modulus_length = 256; // restore modulus_length for next test
eroman 2013/10/28 20:00:47 [optional] I think it would be clearer to inline t
padolph 2013/10/28 21:08:53 Done.
707
708 // bad exponent, larger than unsigned long
709 unsigned exponent_length = sizeof(unsigned long) + 1;
710 const std::vector<uint8> long_exponent(exponent_length, 0x01);
711 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
712 modulus_length,
713 long_exponent);
714 EXPECT_FALSE(GenerateKeyPairInternal(
715 algorithm, &public_key_handle, &private_key_handle));
716
717 // bad exponent, empty
718 const std::vector<uint8> empty_exponent;
719 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
720 modulus_length,
721 empty_exponent);
722 EXPECT_FALSE(GenerateKeyPairInternal(
723 algorithm, &public_key_handle, &private_key_handle));
724
725 // bad exponent, all zeros
726 std::vector<uint8> exponent_with_leading_zeros(15, 0x00);
727 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
728 modulus_length,
729 exponent_with_leading_zeros);
730 EXPECT_FALSE(GenerateKeyPairInternal(
731 algorithm, &public_key_handle, &private_key_handle));
732
733 // good exponent with leading zeros
734 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(),
735 public_exponent.begin(),
736 public_exponent.end());
737 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
738 modulus_length,
739 exponent_with_leading_zeros);
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
745 // happy WebCryptoAlgorithmIdRsaOaep
746 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaOaep,
747 modulus_length,
748 public_exponent);
749 EXPECT_TRUE(GenerateKeyPairInternal(
750 algorithm, &public_key_handle, &private_key_handle));
751 EXPECT_TRUE(public_key_handle);
752 EXPECT_TRUE(private_key_handle);
753
754 // happy WebCryptoAlgorithmIdRsaSsaPkcs1v1_5
755 algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
756 modulus_length,
757 public_exponent);
758 EXPECT_TRUE(GenerateKeyPairInternal(
759 algorithm, &public_key_handle, &private_key_handle));
760 EXPECT_TRUE(public_key_handle);
761 EXPECT_TRUE(private_key_handle);
762 }
763
764 #endif // #if !defined(USE_OPENSSL)
765
659 } // namespace content 766 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698