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

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

Issue 68303009: [webcrypto] Add RSASSA-PKCS1-v1_5 sign and verify for NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 7 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
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 "content/renderer/webcrypto/webcrypto_impl.h" 5 #include "content/renderer/webcrypto/webcrypto_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 dict->SetString("alg", "RSA1_5"); 70 dict->SetString("alg", "RSA1_5");
71 dict->SetString("use", "enc"); 71 dict->SetString("use", "enc");
72 dict->SetBoolean("extractable", false); 72 dict->SetBoolean("extractable", false);
73 dict->SetString("n", 73 dict->SetString("n",
74 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" 74 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk"
75 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" 75 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm"
76 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); 76 "e7PUJHYW1PW6ENTP0ibeiNOfFvs");
77 dict->SetString("e", "AQAB"); 77 dict->SetString("e", "AQAB");
78 } 78 }
79 79
80 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm(
81 blink::WebCryptoAlgorithmId algorithm_id,
82 unsigned modulus_length,
83 const std::vector<uint8>& public_exponent) {
84 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
85 algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
86 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
87 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
88 algorithm_id,
89 new blink::WebCryptoRsaKeyGenParams(
90 modulus_length,
91 webcrypto::Uint8VectorStart(public_exponent),
92 public_exponent.size()));
93 }
94
95 #endif // #if !defined(USE_OPENSSL) 80 #endif // #if !defined(USE_OPENSSL)
96 81
82 // TODO(padolph): Move to webcrypto_util
83 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) {
84 return alg_id == blink::WebCryptoAlgorithmIdSha1 ||
85 alg_id == blink::WebCryptoAlgorithmIdSha224 ||
86 alg_id == blink::WebCryptoAlgorithmIdSha256 ||
87 alg_id == blink::WebCryptoAlgorithmIdSha384 ||
88 alg_id == blink::WebCryptoAlgorithmIdSha512;
89 }
90
91 blink::WebCryptoAlgorithm CreateRsaAlgorithmWithInnerHash(
92 blink::WebCryptoAlgorithmId algorithm_id,
93 blink::WebCryptoAlgorithmId hash_id) {
94 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
95 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
96 DCHECK(IsHashAlgorithm(hash_id));
97 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
98 algorithm_id,
99 new blink::WebCryptoRsaSsaParams(webcrypto::CreateAlgorithm(hash_id)));
100 }
101
97 } // namespace 102 } // namespace
98 103
99 class WebCryptoImplTest : public testing::Test { 104 class WebCryptoImplTest : public testing::Test {
100 protected: 105 protected:
101 blink::WebCryptoKey ImportSecretKeyFromRawHexString( 106 blink::WebCryptoKey ImportSecretKeyFromRawHexString(
102 const std::string& key_hex, 107 const std::string& key_hex,
103 const blink::WebCryptoAlgorithm& algorithm, 108 const blink::WebCryptoAlgorithm& algorithm,
104 blink::WebCryptoKeyUsageMask usage) { 109 blink::WebCryptoKeyUsageMask usage) {
105 std::vector<uint8> key_raw = HexStringToBytes(key_hex); 110 std::vector<uint8> key_raw = HexStringToBytes(key_hex);
106 111
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 bool* signature_match) { 195 bool* signature_match) {
191 return crypto_.VerifySignatureInternal(algorithm, 196 return crypto_.VerifySignatureInternal(algorithm,
192 key, 197 key,
193 signature, 198 signature,
194 signature_size, 199 signature_size,
195 webcrypto::Uint8VectorStart(data), 200 webcrypto::Uint8VectorStart(data),
196 data.size(), 201 data.size(),
197 signature_match); 202 signature_match);
198 } 203 }
199 204
205 bool VerifySignatureInternal(
206 const blink::WebCryptoAlgorithm& algorithm,
207 const blink::WebCryptoKey& key,
208 const std::vector<uint8>& signature,
209 const std::vector<uint8>& data,
210 bool* signature_match) {
211 return crypto_.VerifySignatureInternal(
212 algorithm,
213 key,
214 webcrypto::Uint8VectorStart(signature),
215 signature.size(),
216 webcrypto::Uint8VectorStart(data),
217 data.size(),
218 signature_match);
219 }
220
200 bool EncryptInternal( 221 bool EncryptInternal(
201 const blink::WebCryptoAlgorithm& algorithm, 222 const blink::WebCryptoAlgorithm& algorithm,
202 const blink::WebCryptoKey& key, 223 const blink::WebCryptoKey& key,
203 const unsigned char* data, 224 const unsigned char* data,
204 unsigned data_size, 225 unsigned data_size,
205 blink::WebArrayBuffer* buffer) { 226 blink::WebArrayBuffer* buffer) {
206 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer); 227 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer);
207 } 228 }
208 229
209 bool EncryptInternal( 230 bool EncryptInternal(
(...skipping 1028 matching lines...) Expand 10 before | Expand all | Expand 10 after
1238 // Fail with bad modulus. 1259 // Fail with bad modulus.
1239 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1260 algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1240 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); 1261 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent);
1241 EXPECT_FALSE(GenerateKeyPairInternal( 1262 EXPECT_FALSE(GenerateKeyPairInternal(
1242 algorithm, extractable, usage_mask, &public_key, &private_key)); 1263 algorithm, extractable, usage_mask, &public_key, &private_key));
1243 1264
1244 // Fail with bad exponent: larger than unsigned long. 1265 // Fail with bad exponent: larger than unsigned long.
1245 unsigned exponent_length = sizeof(unsigned long) + 1; // NOLINT 1266 unsigned exponent_length = sizeof(unsigned long) + 1; // NOLINT
1246 const std::vector<uint8> long_exponent(exponent_length, 0x01); 1267 const std::vector<uint8> long_exponent(exponent_length, 0x01);
1247 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1268 algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1248 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, modulus_length, long_exponent); 1269 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1270 modulus_length,
1271 long_exponent);
1249 EXPECT_FALSE(GenerateKeyPairInternal( 1272 EXPECT_FALSE(GenerateKeyPairInternal(
1250 algorithm, extractable, usage_mask, &public_key, &private_key)); 1273 algorithm, extractable, usage_mask, &public_key, &private_key));
1251 1274
1252 // Fail with bad exponent: empty. 1275 // Fail with bad exponent: empty.
1253 const std::vector<uint8> empty_exponent; 1276 const std::vector<uint8> empty_exponent;
1254 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1277 algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1255 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1278 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1256 modulus_length, 1279 modulus_length,
1257 empty_exponent); 1280 empty_exponent);
1258 EXPECT_FALSE(GenerateKeyPairInternal( 1281 EXPECT_FALSE(GenerateKeyPairInternal(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 blink::WebArrayBuffer output; 1345 blink::WebArrayBuffer output;
1323 EXPECT_FALSE( 1346 EXPECT_FALSE(
1324 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, private_key, &output)); 1347 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, private_key, &output));
1325 } 1348 }
1326 1349
1327 TEST_F(WebCryptoImplTest, RsaEsRoundTrip) { 1350 TEST_F(WebCryptoImplTest, RsaEsRoundTrip) {
1328 // Note: using unrealistic short key length here to avoid bogging down tests. 1351 // Note: using unrealistic short key length here to avoid bogging down tests.
1329 1352
1330 // Create a key pair. 1353 // Create a key pair.
1331 const unsigned kModulusLength = 256; 1354 const unsigned kModulusLength = 256;
1332 blink::WebCryptoAlgorithm algorithm = 1355 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1333 CreateRsaKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1356 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1334 kModulusLength, 1357 kModulusLength,
1335 HexStringToBytes("010001")); 1358 HexStringToBytes("010001"));
1336 const blink::WebCryptoKeyUsageMask usage_mask = 1359 const blink::WebCryptoKeyUsageMask usage_mask =
1337 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt; 1360 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt;
1338 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1361 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1339 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1362 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1340 EXPECT_TRUE(GenerateKeyPairInternal( 1363 EXPECT_TRUE(GenerateKeyPairInternal(
1341 algorithm, false, usage_mask, &public_key, &private_key)); 1364 algorithm, false, usage_mask, &public_key, &private_key));
1342 EXPECT_FALSE(public_key.isNull()); 1365 EXPECT_FALSE(public_key.isNull());
1343 EXPECT_FALSE(private_key.isNull()); 1366 EXPECT_FALSE(private_key.isNull());
1344 1367
1345 // Make a maximum-length data message. RSAES can operate on messages up to 1368 // Make a maximum-length data message. RSAES can operate on messages up to
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1504 &decrypted_data)); 1527 &decrypted_data));
1505 EXPECT_FALSE(decrypted_data.isNull()); 1528 EXPECT_FALSE(decrypted_data.isNull());
1506 ExpectArrayBufferMatchesHex(cleartext_hex, decrypted_data); 1529 ExpectArrayBufferMatchesHex(cleartext_hex, decrypted_data);
1507 } 1530 }
1508 1531
1509 TEST_F(WebCryptoImplTest, RsaEsFailures) { 1532 TEST_F(WebCryptoImplTest, RsaEsFailures) {
1510 // Note: using unrealistic short key length here to avoid bogging down tests. 1533 // Note: using unrealistic short key length here to avoid bogging down tests.
1511 1534
1512 // Create a key pair. 1535 // Create a key pair.
1513 const unsigned kModulusLength = 256; 1536 const unsigned kModulusLength = 256;
1514 blink::WebCryptoAlgorithm algorithm = 1537 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1515 CreateRsaKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1538 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1516 kModulusLength, 1539 kModulusLength,
1517 HexStringToBytes("010001")); 1540 HexStringToBytes("010001"));
1518 const blink::WebCryptoKeyUsageMask usage_mask = 1541 const blink::WebCryptoKeyUsageMask usage_mask =
1519 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt; 1542 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt;
1520 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1543 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1521 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1544 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1522 EXPECT_TRUE(GenerateKeyPairInternal( 1545 EXPECT_TRUE(GenerateKeyPairInternal(
1523 algorithm, false, usage_mask, &public_key, &private_key)); 1546 algorithm, false, usage_mask, &public_key, &private_key));
1524 EXPECT_FALSE(public_key.isNull()); 1547 EXPECT_FALSE(public_key.isNull());
1525 EXPECT_FALSE(private_key.isNull()); 1548 EXPECT_FALSE(private_key.isNull());
1526 1549
1527 // Fail encrypt with a private key. 1550 // Fail encrypt with a private key.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1572 // Do a successful decrypt with good data just for confirmation. 1595 // Do a successful decrypt with good data just for confirmation.
1573 EXPECT_TRUE(DecryptInternal( 1596 EXPECT_TRUE(DecryptInternal(
1574 algorithm, 1597 algorithm,
1575 private_key, 1598 private_key,
1576 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1599 reinterpret_cast<const unsigned char*>(encrypted_data.data()),
1577 encrypted_data.byteLength(), 1600 encrypted_data.byteLength(),
1578 &decrypted_data)); 1601 &decrypted_data));
1579 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data); 1602 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data);
1580 } 1603 }
1581 1604
1605 TEST_F(WebCryptoImplTest, RsaSsaSignVerify) {
1606
1607 // Generate an RSA key pair.
1608 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm(
1609 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1610 1024,
1611 HexStringToBytes("010001"));
1612 const blink::WebCryptoKeyUsageMask usage_mask =
1613 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify;
1614 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1615 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1616 EXPECT_TRUE(GenerateKeyPairInternal(
1617 algorithm, true, usage_mask, &public_key, &private_key));
1618 EXPECT_FALSE(public_key.isNull());
1619 EXPECT_FALSE(private_key.isNull());
1620
1621 algorithm = CreateRsaAlgorithmWithInnerHash(
1622 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1623 blink::WebCryptoAlgorithmIdSha1);
1624 blink::WebArrayBuffer signature;
1625 bool signature_match = false;
1626
1627 const char* kTestData[] = {"", "00", "010203040506070809"};
1628 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestData); ++i) {
1629
1630 SCOPED_TRACE(i);
1631
1632 // Sign data with the private key.
1633 const std::vector<uint8> data = HexStringToBytes(kTestData[i]);
1634 ASSERT_TRUE(SignInternal(algorithm, private_key, data, &signature));
1635
1636 // Verify the signature with the public key.
1637 signature_match = false;
1638 EXPECT_TRUE(VerifySignatureInternal(
1639 algorithm,
1640 public_key,
1641 static_cast<const unsigned char*>(signature.data()),
1642 signature.byteLength(),
1643 data,
1644 &signature_match));
1645 EXPECT_TRUE(signature_match);
1646 }
1647
1648 const std::vector<uint8> data = HexStringToBytes("010203040506070809");
1649 ASSERT_TRUE(SignInternal(algorithm, private_key, data, &signature));
1650
1651 // Ensure truncated signature does not verify by passing one less byte.
1652 signature_match = false;
1653 EXPECT_TRUE(VerifySignatureInternal(
1654 algorithm,
1655 public_key,
1656 static_cast<const unsigned char*>(signature.data()),
1657 signature.byteLength() - 1,
1658 data,
1659 &signature_match));
1660 EXPECT_FALSE(signature_match);
1661
1662 // Ensure corrupted signature does not verify.
1663 std::vector<uint8> corrupt_sig(
1664 static_cast<uint8*>(signature.data()),
1665 static_cast<uint8*>(signature.data()) + signature.byteLength());
1666 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1;
1667 signature_match = false;
1668 EXPECT_TRUE(VerifySignatureInternal(
1669 algorithm,
1670 public_key,
1671 webcrypto::Uint8VectorStart(corrupt_sig),
1672 corrupt_sig.size(),
1673 data,
1674 &signature_match));
1675 EXPECT_FALSE(signature_match);
1676
1677 // Ensure extra long signature does not cause issues and fails.
1678 const unsigned char kLongSignature[1024] = { 0 };
1679 EXPECT_TRUE(VerifySignatureInternal(
1680 algorithm,
1681 public_key,
1682 kLongSignature,
1683 sizeof(kLongSignature),
1684 data,
1685 &signature_match));
1686 EXPECT_FALSE(signature_match);
1687
1688 // Ensure can't verify using a private key.
1689 EXPECT_FALSE(VerifySignatureInternal(
1690 algorithm,
1691 private_key,
1692 static_cast<const unsigned char*>(signature.data()),
1693 signature.byteLength(),
1694 data,
1695 &signature_match));
1696
1697 // Ensure can't sign using a public key.
1698 EXPECT_FALSE(SignInternal(algorithm, public_key, data, &signature));
1699
1700 // TODO(padolph): Not sure this kind of test is required here, it might be
1701 // more appropriate on the Blink side.
1702 // Fail sign with malformed algorithm (no inner hash)
1703 algorithm =
1704 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5);
1705 EXPECT_FALSE(SignInternal(algorithm, private_key, data, &signature));
1706
1707 // TODO(padolph): Not sure this kind of test is required here, it might be
1708 // more appropriate on the Blink side.
1709 // Fail sign and verify with incompatible algorithm
1710 algorithm =
1711 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1712 EXPECT_FALSE(SignInternal(algorithm, private_key, data, &signature));
1713 EXPECT_FALSE(VerifySignatureInternal(
1714 algorithm,
1715 public_key,
1716 static_cast<const unsigned char*>(signature.data()),
1717 signature.byteLength(),
1718 data,
1719 &signature_match));
1720 }
1721
1722 TEST_F(WebCryptoImplTest, RsaSignVerifyKnownAnswer) {
1723
1724 // Use the NIST test vectors from Example 1 of
1725 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt
1726 // These vectors are known answers for RSA PKCS#1 v1.5 Signature with a SHA-1
1727 // digest, using a predefined key pair.
1728
1729 // The following key pair is comprised of the SPKI (public key) and PKCS#8
1730 // (private key) representations of the key pair provided in Example 1 of the
1731 // NIST link above.
1732 const std::string public_key_spki_der_hex =
1733 "30819f300d06092a864886f70d010101050003818d0030818902818100a5"
1734 "6e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad9"
1735 "91d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfc"
1736 "e0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e"
1737 "6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cf"
1738 "fb2249bd9a21370203010001";
1739 const std::string private_key_pkcs8_der_hex =
1740 "30820275020100300d06092a864886f70d01010105000482025f3082025b"
1741 "02010002818100a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52"
1742 "a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab"
1743 "7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921c"
1744 "b23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef"
1745 "22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d"
1746 "4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c"
1747 "568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee"
1748 "896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31"
1749 "b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b3"
1750 "25024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8629"
1751 "6b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b"
1752 "3b6dcd3eda8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde12"
1753 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72"
1754 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca"
1755 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d"
1756 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71"
1757 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd"
1758 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027"
1759 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319"
1760 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24"
1761 "a79f4d";
1762
1763 // The following data are the input messages and corresponding computed RSA
1764 // PKCS#1 v1.5 signatures from the NIST link above.
1765 struct TestCase {
1766 const std::string message_hex;
1767 const std::string signature_hex;
1768 };
1769
1770 const TestCase kTests[] = {
1771 // PKCS#1 v1.5 Signature Example 1.1
1772 {"cdc87da223d786df3b45e0bbbc721326"
1773 "d1ee2af806cc315475cc6f0d9c66e1b6"
1774 "2371d45ce2392e1ac92844c310102f15"
1775 "6a0d8d52c1f4c40ba3aa65095786cb76"
1776 "9757a6563ba958fed0bcc984e8b517a3"
1777 "d5f515b23b8a41e74aa867693f90dfb0"
1778 "61a6e86dfaaee64472c00e5f20945729"
1779 "cbebe77f06ce78e08f4098fba41f9d61"
1780 "93c0317e8b60d4b6084acb42d29e3808"
1781 "a3bc372d85e331170fcbf7cc72d0b71c"
1782 "296648b3a4d10f416295d0807aa625ca"
1783 "b2744fd9ea8fd223c42537029828bd16"
1784 "be02546f130fd2e33b936d2676e08aed"
1785 "1b73318b750a0167d0",
1786 "6bc3a06656842930a247e30d5864b4d8"
1787 "19236ba7c68965862ad7dbc4e24af28e"
1788 "86bb531f03358be5fb74777c6086f850"
1789 "caef893f0d6fcc2d0c91ec013693b4ea"
1790 "00b80cd49aac4ecb5f8911afe539ada4"
1791 "a8f3823d1d13e472d1490547c659c761"
1792 "7f3d24087ddb6f2b72096167fc097cab"
1793 "18e9a458fcb634cdce8ee35894c484d7"},
1794 // PKCS#1 v1.5 Signature Example 1.2
1795 {"851384cdfe819c22ed6c4ccb30daeb5c"
1796 "f059bc8e1166b7e3530c4c233e2b5f8f"
1797 "71a1cca582d43ecc72b1bca16dfc7013"
1798 "226b9e",
1799 "84fd2ce734ec1da828d0f15bf49a8707"
1800 "c15d05948136de537a3db421384167c8"
1801 "6fae022587ee9e137daee75473826293"
1802 "2d271c744c6d3a189ad4311bdb020492"
1803 "e322fbddc40406ea860d4e8ea2a4084a"
1804 "a98b9622a446756fdb740ddb3d91db76"
1805 "70e211661bbf8709b11c08a70771422d"
1806 "1a12def29f0688a192aebd89e0f896f8"},
1807 // PKCS#1 v1.5 Signature Example1.3
1808 {"a4b159941761c40c6a82f2b80d1b94f5"
1809 "aa2654fd17e12d588864679b54cd04ef"
1810 "8bd03012be8dc37f4b83af7963faff0d"
1811 "fa225477437c48017ff2be8191cf3955"
1812 "fc07356eab3f322f7f620e21d254e5db"
1813 "4324279fe067e0910e2e81ca2cab31c7"
1814 "45e67a54058eb50d993cdb9ed0b4d029"
1815 "c06d21a94ca661c3ce27fae1d6cb20f4"
1816 "564d66ce4767583d0e5f060215b59017"
1817 "be85ea848939127bd8c9c4d47b51056c"
1818 "031cf336f17c9980f3b8f5b9b6878e8b"
1819 "797aa43b882684333e17893fe9caa6aa"
1820 "299f7ed1a18ee2c54864b7b2b99b7261"
1821 "8fb02574d139ef50f019c9eef4169713"
1822 "38e7d470",
1823 "0b1f2e5180e5c7b4b5e672929f664c48"
1824 "96e50c35134b6de4d5a934252a3a245f"
1825 "f48340920e1034b7d5a5b524eb0e1cf1"
1826 "2befef49b27b732d2c19e1c43217d6e1"
1827 "417381111a1d36de6375cf455b3c9812"
1828 "639dbc27600c751994fb61799ecf7da6"
1829 "bcf51540afd0174db4033188556675b1"
1830 "d763360af46feeca5b60f882829ee7b2"},
1831 // PKCS#1 v1.5 Signature Example 1.4
1832 {"bc656747fa9eafb3f0",
1833 "45607ad611cf5747a41ac94d0ffec878"
1834 "bdaf63f6b57a4b088bf36e34e109f840"
1835 "f24b742ada16102dabf951cbc44f8982"
1836 "e94ed4cd09448d20ec0efa73545f80b6"
1837 "5406bed6194a61c340b4ad1568cbb758"
1838 "51049f11af1734964076e02029aee200"
1839 "e40e80be0f4361f69841c4f92a4450a2"
1840 "286d43289b405554c54d25c6ecb584f4"},
1841 // PKCS#1 v1.5 Signature Example 1.5
1842 {"b45581547e5427770c768e8b82b75564"
1843 "e0ea4e9c32594d6bff706544de0a8776"
1844 "c7a80b4576550eee1b2acabc7e8b7d3e"
1845 "f7bb5b03e462c11047eadd00629ae575"
1846 "480ac1470fe046f13a2bf5af17921dc4"
1847 "b0aa8b02bee6334911651d7f8525d10f"
1848 "32b51d33be520d3ddf5a709955a3dfe7"
1849 "8283b9e0ab54046d150c177f037fdccc"
1850 "5be4ea5f68b5e5a38c9d7edcccc4975f"
1851 "455a6909b4",
1852 "54be9d90877515f450279c15b5f61ad6"
1853 "f15ecc95f18cbed82b65b1667a575809"
1854 "587994668044f3bc2ae7f884501f64f0"
1855 "b43f588cfa205a6ab704328c2d4ab92a"
1856 "7ae13440614d3e085f401da9ad28e210"
1857 "5e4a0edb681a6424df047388ce051ee9"
1858 "df7bc2163fe347520ad51ccd51806438"
1859 "3e741acad3cbdc2cb5a7c68e868464c2"},
1860 // PKCS#1 v1.5 Signature Example 1.6
1861 {"10aae9a0ab0b595d0841207b700d48d7"
1862 "5faedde3b775cd6b4cc88ae06e4694ec"
1863 "74ba18f8520d4f5ea69cbbe7cc2beba4"
1864 "3efdc10215ac4eb32dc302a1f53dc6c4"
1865 "352267e7936cfebf7c8d67035784a390"
1866 "9fa859c7b7b59b8e39c5c2349f1886b7"
1867 "05a30267d402f7486ab4f58cad5d69ad"
1868 "b17ab8cd0ce1caf5025af4ae24b1fb87"
1869 "94c6070cc09a51e2f9911311e3877d00"
1870 "44c71c57a993395008806b723ac38373"
1871 "d395481818528c1e7053739282053529"
1872 "510e935cd0fa77b8fa53cc2d474bd4fb"
1873 "3cc5c672d6ffdc90a00f9848712c4bcf"
1874 "e46c60573659b11e6457e861f0f604b6"
1875 "138d144f8ce4e2da73",
1876 "0e6ff63a856b9cbd5dbe423183122047"
1877 "dd39d6f76d1b2310e546fe9ee73b33ef"
1878 "a7c78f9474455c9e5b88cb383aafc369"
1879 "8668e7b7a59a9cbb5b0897b6c5afb7f8"
1880 "bac4b924e98d760a15fc43d2814ab2d5"
1881 "187f79bed9915a93397ebc22a7677506"
1882 "a02e076d3ffdc0441dbd4db00453dc28"
1883 "d830e0573f77b817b505c38b4a4bb5d0"},
1884 // PKCS#1 v1.5 Signature Example 1.7
1885 {"efb5da1b4d1e6d9a5dff92d0184da7e3"
1886 "1f877d1281ddda625664869e8379e67a"
1887 "d3b75eae74a580e9827abd6eb7a002cb"
1888 "5411f5266797768fb8e95ae40e3e8b34"
1889 "66f5ab15d69553952939ec23e61d5849"
1890 "7fac76aa1c0bb5a3cb4a54383587c7bb"
1891 "78d13eefda205443e6ce4365802df55c"
1892 "64713497984e7ca96722b3edf84d56",
1893 "8385d58533a995f72df262b70f40b391"
1894 "ddf515f464b9d2cc2d66398fc05689d8"
1895 "11632946d62eabdca7a31fcf6cd6c981"
1896 "d28bbc29083e4a6d5b2b378ca4e540f0"
1897 "60b96d53ad2693f82178b94e2e2f86b9"
1898 "accfa02025107e062ab7080175684501"
1899 "028f676461d81c008fe4750671649970"
1900 "878fc175cf98e96b2ecbf6874d77dacb"},
1901 // PKCS#1 v1.5 Signature Example 1.8
1902 {"53bb58ce42f1984940552657233b1496"
1903 "9af365c0a561a4132af18af39432280e"
1904 "3e437082434b19231837184f02cf2b2e"
1905 "726bebf74d7ae3256d8b72f3eafdb134"
1906 "d33de06f2991d299d59f5468d43b9958"
1907 "d6a968f5969edbbc6e7185cbc716c7c9"
1908 "45dafa9cc71ddfaaa01094a452ddf5e2"
1909 "407320400bf05ea9729cafbf0600e788"
1910 "07ef9462e3fde32ed7d981a56f4751ef"
1911 "64fb4549910ecc911d728053b3994300"
1912 "4740e6f5821fe8d75c0617bf2c6b24bb"
1913 "fc34013fc95f0dedf5ba297f504fb833"
1914 "da2a436d1d8ff1cc5193e2a64389fced"
1915 "918e7feb6716330f66801db9497549cf"
1916 "1d3bd97cf1bc6255",
1917 "8e1f3d26ec7c6bbb8c54c5d25f312058"
1918 "7803af6d3c2b99a37ced6a3657d4ae54"
1919 "266f63fffde660c866d65d0ab0589e1d"
1920 "12d9ce6054b05c8668ae127171ccaae7"
1921 "f1cd409677f52157b6123ab227f27a00"
1922 "966d1439b42a32169d1070394026fc8b"
1923 "c93545b1ac252d0f7da751c02e33a478"
1924 "31fbd71514c2bbbd3adb6740c0fd68ad"},
1925 // PKCS#1 v1.5 Signature Example 1.9
1926 {"27cadc698450945f204ec3cf8c6cbd8c"
1927 "eb4cc0cbe312274fa96b04deac855160"
1928 "c0e04e4ac5d38210c27c",
1929 "7b63f9223356f35f6117f68c8f822003"
1930 "4fc2384ab5dc6904141f139314d6ee89"
1931 "f54ec6ffd18c413a23c5931c7fbb13c5"
1932 "55ccfd590e0eaa853c8c94d2520cd425"
1933 "0d9a05a193b65dc749b82478af0156ee"
1934 "1de55ddad33ec1f0099cad6c891a3617"
1935 "c7393d05fbfbbb00528a001df0b204eb"
1936 "df1a341090dea89f870a877458427f7b"},
1937 // PKCS#1 v1.5 Signature Example 1.10
1938 {"716407e901b9ef92d761b013fd13eb7a"
1939 "d72aed",
1940 "2a22dbe3774d5b297201b55a0f17f42d"
1941 "ce63b7845cb325cfe951d0badb5c5a14"
1942 "472143d896c86cc339f83671164215ab"
1943 "c97862f2151654e75a3b357c37311b3d"
1944 "7268cab540202e23bee52736f2cd86cc"
1945 "e0c7dbde95e1c600a47395dc5eb0a472"
1946 "153fbc4fb21b643e0c04ae14dd37e97e"
1947 "617a7567c89652219781001ba6f83298"},
1948 // PKCS#1 v1.5 Signature Example 1.11
1949 {"46c24e4103001629c712dd4ce8d747ee"
1950 "595d6c744ccc4f71347d9b8abf49d1b8"
1951 "fb2ef91b95dc899d4c0e3d2997e638f4"
1952 "cf3f68e0498de5aabd13f0dfe02ff26b"
1953 "a4379104e78ffa95ffbd15067ef8cbd7"
1954 "eb7860fecc71abe13d5c720a66851f2d"
1955 "efd4e795054d7bec024bb422a46a7368"
1956 "b56d95b47aebafbeadd612812593a70d"
1957 "b9f96d451ee15edb299308d777f4bb68"
1958 "ed3377c32156b41b7a9c92a14c8b8114"
1959 "4399c56a5a432f4f770aa97da8415d0b"
1960 "da2e813206031e70620031c881d616bf"
1961 "fd5f03bf147c1e73766c26246208",
1962 "12235b0b406126d9d260d447e923a110"
1963 "51fb243079f446fd73a70181d53634d7"
1964 "a0968e4ee27777eda63f6e4a3a91ad59"
1965 "85998a4848da59ce697b24bb332fa2ad"
1966 "9ce462ca4affdc21dab908e8ce15af6e"
1967 "b9105b1abcf39142aa17b34c4c092386"
1968 "a7abbfe028afdbebc14f2ce26fbee5ed"
1969 "eca11502d39a6b7403154843d98a62a7"},
1970 // PKCS#1 v1.5 Signature Example 1.12
1971 {"bc99a932aa16d622bfff79c50b4c4235"
1972 "8673261129e28d6a918ff1b0f1c4f46a"
1973 "d8afa98b0ca0f56f967975b0a29be882"
1974 "e93b6cd3fc33e1faef72e52b2ae0a3f1"
1975 "2024506e25690e902e78298214555653"
1976 "2284cf505789738f4da31fa1333d3af8"
1977 "62b2ba6b6ce7ab4cce6aba",
1978 "872ec5ad4f1846256f17e9936ac50e43"
1979 "e9963ea8c1e76f15879b7874d77d122a"
1980 "609dc8c561145b94bf4ffdffdeb17e6e"
1981 "76ffc6c10c0747f5e37a9f434f5609e7"
1982 "9da5250215a457afdf12c6507cc1551f"
1983 "54a28010595826a2c9b97fa0aa851cc6"
1984 "8b705d7a06d720ba027e4a1c0b019500"
1985 "fb63b78071684dcfa9772700b982dc66"},
1986 // PKCS#1 v1.5 Signature Example 1.13
1987 {"731e172ac063992c5b11ba170dfb23bb"
1988 "000d47ba195329cf278061037381514c"
1989 "146064c5285db130dd5bae98b7722259"
1990 "50eab05d3ea996f6fffb9a8c8622913f"
1991 "279914c89ada4f3dd77666a868bfcbff"
1992 "2b95b7daf453d4e2c9d75beee7f8e709"
1993 "05e4066a4f73aecc67f956aa5a3292b8"
1994 "488c917d317cfdc86253e690381e15ab",
1995 "76204eacc1d63ec1d6ad5bd0692e1a2f"
1996 "686df6e64ca945c77a824de212efa6d9"
1997 "782d81b4591403ff4020620298c07ebd"
1998 "3a8a61c5bf4dad62cbfc4ae6a03937be"
1999 "4b49a216d570fc6e81872937876e27bd"
2000 "19cf601effc30ddca573c9d56cd4569b"
2001 "db4851c450c42cb21e738cdd61027b8b"
2002 "e5e9b410fc46aa3f29e4be9e64451346"},
2003 // PKCS#1 v1.5 Signature Example 1.14
2004 {"0211382683a74d8d2a2cb6a06550563b"
2005 "e1c26ca62821e4ff163b720464fc3a28"
2006 "d91bedddc62749a5538eaf41fbe0c82a"
2007 "77e06ad99383c9e985ffb8a93fd4d7c5"
2008 "8db51ad91ba461d69a8fd7ddabe24967"
2009 "57a0c49122c1a79a85cc0553e8214d03"
2010 "6dfe0185efa0d05860c612fa0882c82d"
2011 "246e5830a67355dff18a2c36b732f988"
2012 "cfedc562264c6254b40fcabb97b76094"
2013 "7568dcd6a17cda6ee8855bddbab93702"
2014 "471aa0cfb1bed2e13118eba1175b73c9"
2015 "6253c108d0b2aba05ab8e17e84392e20"
2016 "085f47404d8365527dc3fb8f2bb48a50"
2017 "038e71361ccf973407",
2018 "525500918331f1042eae0c5c2054aa7f"
2019 "92deb26991b5796634f229daf9b49eb2"
2020 "054d87319f3cfa9b466bd075ef6699ae"
2021 "a4bd4a195a1c52968b5e2b75e092d846"
2022 "ea1b5cc27905a8e1d5e5de0edfdb2139"
2023 "1ebb951864ebd9f0b0ec35b654287136"
2024 "0a317b7ef13ae06af684e38e21b1e19b"
2025 "c7298e5d6fe0013a164bfa25d3e7313d"},
2026 // PKCS#1 v1.5 Signature Example 1.15
2027 {"fc6b700d22583388ab2f8dafcaf1a056"
2028 "20698020da4bae44dafbd0877b501250"
2029 "6dc3181d5c66bf023f348b41fd9f9479"
2030 "5ab96452a4219f2d39d72af359cf1956"
2031 "51c7",
2032 "4452a6cc2626b01e95ab306df0d0cc74"
2033 "84fbab3c22e9703283567f66eadc248d"
2034 "bda58fce7dd0c70cce3f150fca4b369d"
2035 "ff3b6237e2b16281ab55b53fb13089c8"
2036 "5cd265056b3d62a88bfc2135b16791f7"
2037 "fbcab9fd2dc33becb617be419d2c0461"
2038 "42a4d47b338314552edd4b6fe9ce1104"
2039 "ecec4a9958d7331e930fc09bf08a6e64"},
2040 // PKCS#1 v1.5 Signature Example 1.16
2041 {"13ba086d709cfa5fedaa557a89181a61"
2042 "40f2300ed6d7c3febb6cf68abebcbc67"
2043 "8f2bca3dc2330295eec45bb1c4075f3a"
2044 "da987eae88b39c51606cb80429e649d9"
2045 "8acc8441b1f8897db86c5a4ce0abf28b"
2046 "1b81dca3667697b850696b74a5ebd85d"
2047 "ec56c90f8abe513efa857853720be319"
2048 "607921bca947522cd8fac8cace5b827c"
2049 "3e5a129e7ee57f6b84932f14141ac427"
2050 "4e8cbb46e6912b0d3e2177d499d1840c"
2051 "d47d4d7ae0b4cdc4d3",
2052 "1f3b5a87db72a2c97bb3eff2a65a3012"
2053 "68eacd89f42abc1098c1f2de77b0832a"
2054 "65d7815feb35070063f221bb3453bd43"
2055 "4386c9a3fde18e3ca1687fb649e86c51"
2056 "d658619dde5debb86fe15491ff77ab74"
2057 "8373f1be508880d66ea81e870e91cdf1"
2058 "704875c17f0b10103188bc64eef5a355"
2059 "1b414c733670215b1a22702562581ab1"},
2060 // PKCS#1 v1.5 Signature Example 1.17
2061 {"eb1e5935",
2062 "370cb9839ae6074f84b2acd6e6f6b792"
2063 "1b4b523463757f6446716140c4e6c0e7"
2064 "5bec6ad0197ebfa86bf46d094f5f6cd3"
2065 "6dca3a5cc73c8bbb70e2c7c9ab5d964e"
2066 "c8e3dfde481b4a1beffd01b4ad15b31a"
2067 "e7aebb9b70344a9411083165fdf9c375"
2068 "4bbb8b94dd34bd4813dfada1f6937de4"
2069 "267d5597ca09a31e83d7f1a79dd19b5e"},
2070 // PKCS#1 v1.5 Signature Example 1.18
2071 {"6346b153e889c8228209630071c8a577"
2072 "83f368760b8eb908cfc2b276",
2073 "2479c975c5b1ae4c4e940f473a9045b8"
2074 "bf5b0bfca78ec29a38dfbedc8a749b7a"
2075 "2692f7c52d5bc7c831c7232372a00fed"
2076 "3b6b49e760ec99e074ff2eead5134e83"
2077 "05725dfa39212b84bd4b8d80bc8bc17a"
2078 "512823a3beb18fc08e45ed19c26c8177"
2079 "07d67fb05832ef1f12a33e90cd93b8a7"
2080 "80319e2963ca25a2af7b09ad8f595c21"},
2081 // PKCS#1 v1.5 Signature Example 1.19
2082 {"64702db9f825a0f3abc361974659f5e9"
2083 "d30c3aa4f56feac69050c72905e77fe0"
2084 "c22f88a378c21fcf45fe8a5c71730209"
2085 "3929",
2086 "152f3451c858d69594e6567dfb31291c"
2087 "1ee7860b9d15ebd5a5edd276ac3e6f7a"
2088 "8d1480e42b3381d2be023acf7ebbdb28"
2089 "de3d2163ae44259c6df98c335d045b61"
2090 "dac9dba9dbbb4e6ab4a083cd76b580cb"
2091 "e472206a1a9fd60680ceea1a570a29b0"
2092 "881c775eaef5525d6d2f344c28837d0a"
2093 "ca422bbb0f1aba8f6861ae18bd73fe44"},
2094 // PKCS#1 v1.5 Signature Example 1.20
2095 {"941921de4a1c9c1618d6f3ca3c179f6e"
2096 "29bae6ddf9a6a564f929e3ce82cf3265"
2097 "d7837d5e692be8dcc9e86c",
2098 "7076c287fc6fff2b20537435e5a3107c"
2099 "e4da10716186d01539413e609d27d1da"
2100 "6fd952c61f4bab91c045fa4f8683ecc4"
2101 "f8dde74227f773cff3d96db84718c494"
2102 "4b06affeba94b725f1b07d3928b2490a"
2103 "85c2f1abf492a9177a7cd2ea0c966875"
2104 "6f825bbec900fa8ac3824e114387ef57"
2105 "3780ca334882387b94e5aad7a27a28dc"}};
2106
2107 // Import the public key.
2108 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash(
2109 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2110 blink::WebCryptoAlgorithmIdSha1);
2111 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2112 ASSERT_TRUE(ImportKeyInternal(
2113 blink::WebCryptoKeyFormatSpki,
2114 HexStringToBytes(public_key_spki_der_hex),
2115 algorithm,
2116 true,
2117 blink::WebCryptoKeyUsageVerify,
2118 &public_key));
2119 EXPECT_FALSE(public_key.isNull());
2120 EXPECT_TRUE(public_key.handle());
2121
2122 // Import the private key.
2123 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2124 ASSERT_TRUE(ImportKeyInternal(
2125 blink::WebCryptoKeyFormatPkcs8,
2126 HexStringToBytes(private_key_pkcs8_der_hex),
2127 algorithm,
2128 true,
2129 blink::WebCryptoKeyUsageSign,
2130 &private_key));
2131 EXPECT_FALSE(private_key.isNull());
2132 EXPECT_TRUE(private_key.handle());
2133
2134 // Validate the signatures are computed and verified as expected.
2135 blink::WebArrayBuffer signature;
2136 for (size_t idx = 0; idx < ARRAYSIZE_UNSAFE(kTests); ++idx) {
2137
2138 SCOPED_TRACE(idx);
2139 const TestCase& test = kTests[idx];
2140 const std::vector<uint8> message = HexStringToBytes(test.message_hex);
2141
2142 signature.reset();
2143 ASSERT_TRUE(SignInternal(algorithm, private_key, message, &signature));
2144 ExpectArrayBufferMatchesHex(test.signature_hex, signature);
2145
2146 bool is_match = false;
2147 ASSERT_TRUE(VerifySignatureInternal(
2148 algorithm,
2149 public_key,
2150 HexStringToBytes(test.signature_hex),
2151 message,
2152 &is_match));
2153 EXPECT_TRUE(is_match);
2154 }
2155 }
2156
1582 #endif // #if !defined(USE_OPENSSL) 2157 #endif // #if !defined(USE_OPENSSL)
1583 2158
1584 } // namespace content 2159 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698