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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/webcrypto/webcrypto_impl_unittest.cc
diff --git a/content/renderer/webcrypto/webcrypto_impl_unittest.cc b/content/renderer/webcrypto/webcrypto_impl_unittest.cc
index 51a162433227338eb4e361e2f3a3a6093814446a..bbf10126f86facb1acdd9e152152e154d788854b 100644
--- a/content/renderer/webcrypto/webcrypto_impl_unittest.cc
+++ b/content/renderer/webcrypto/webcrypto_impl_unittest.cc
@@ -77,6 +77,19 @@ WebKit::WebCryptoAlgorithm CreateAesCbcAlgorithm(
new WebKit::WebCryptoAesKeyGenParams(key_length_bits));
}
+WebKit::WebCryptoAlgorithm CreateRsaAlgorithm(
+ WebKit::WebCryptoAlgorithmId algorithm_id,
+ unsigned modulus_length,
+ const std::vector<uint8>& public_exponent) {
+ DCHECK(algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
+ algorithm_id == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
+ algorithm_id == WebKit::WebCryptoAlgorithmIdRsaOaep);
+ return WebKit::WebCryptoAlgorithm::adoptParamsAndCreate(
+ algorithm_id,
+ new WebKit::WebCryptoRsaKeyGenParams(
+ modulus_length, Start(public_exponent), public_exponent.size()));
+}
+
} // namespace
namespace content {
@@ -124,6 +137,13 @@ class WebCryptoImplTest : public testing::Test {
return crypto_.GenerateKeyInternal(algorithm, handle, type);
}
+ bool GenerateKeyPairInternal(
+ const WebKit::WebCryptoAlgorithm& algorithm,
+ scoped_ptr<WebKit::WebCryptoKeyHandle>* public_key,
+ scoped_ptr<WebKit::WebCryptoKeyHandle>* private_key) {
+ return crypto_.GenerateKeyPairInternal(algorithm, public_key, private_key);
+ }
+
bool ImportKeyInternal(
WebKit::WebCryptoKeyFormat format,
const std::vector<uint8>& key_data,
@@ -656,4 +676,91 @@ TEST_F(WebCryptoImplTest, GenerateKeyHmacNoLength) {
EXPECT_EQ(type, WebKit::WebCryptoKeyTypeSecret);
}
+#if !defined(USE_OPENSSL)
+
+TEST_F(WebCryptoImplTest, GenerateKeyPairRsa) {
+
+ // Note: using unrealistic short key lengths here to avoid bogging down tests.
+
+ // 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.
+ unsigned modulus_length = 256;
+ std::vector<uint8> public_exponent = HexStringToBytes("010001");
+ WebKit::WebCryptoAlgorithm algorithm =
+ CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
+ modulus_length,
+ public_exponent);
+ scoped_ptr<WebKit::WebCryptoKeyHandle> public_key_handle;
+ scoped_ptr<WebKit::WebCryptoKeyHandle> private_key_handle;
+ EXPECT_TRUE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+ EXPECT_TRUE(public_key_handle);
+ EXPECT_TRUE(private_key_handle);
+
+ // bad modulus
eroman 2013/10/28 20:00:47 Please capitalize for consistency throughout.
padolph 2013/10/28 21:08:53 Done.
+ modulus_length = 0;
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
+ modulus_length,
+ public_exponent);
+ EXPECT_FALSE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+ 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.
+
+ // bad exponent, larger than unsigned long
+ unsigned exponent_length = sizeof(unsigned long) + 1;
+ const std::vector<uint8> long_exponent(exponent_length, 0x01);
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
+ modulus_length,
+ long_exponent);
+ EXPECT_FALSE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+
+ // bad exponent, empty
+ const std::vector<uint8> empty_exponent;
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
+ modulus_length,
+ empty_exponent);
+ EXPECT_FALSE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+
+ // bad exponent, all zeros
+ std::vector<uint8> exponent_with_leading_zeros(15, 0x00);
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
+ modulus_length,
+ exponent_with_leading_zeros);
+ EXPECT_FALSE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+
+ // good exponent with leading zeros
+ exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(),
+ public_exponent.begin(),
+ public_exponent.end());
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
+ modulus_length,
+ exponent_with_leading_zeros);
+ EXPECT_TRUE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+ EXPECT_TRUE(public_key_handle);
+ EXPECT_TRUE(private_key_handle);
+
+ // happy WebCryptoAlgorithmIdRsaOaep
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaOaep,
+ modulus_length,
+ public_exponent);
+ EXPECT_TRUE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+ EXPECT_TRUE(public_key_handle);
+ EXPECT_TRUE(private_key_handle);
+
+ // happy WebCryptoAlgorithmIdRsaSsaPkcs1v1_5
+ algorithm = CreateRsaAlgorithm(WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
+ modulus_length,
+ public_exponent);
+ EXPECT_TRUE(GenerateKeyPairInternal(
+ algorithm, &public_key_handle, &private_key_handle));
+ EXPECT_TRUE(public_key_handle);
+ EXPECT_TRUE(private_key_handle);
+}
+
+#endif // #if !defined(USE_OPENSSL)
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698