OLD | NEW |
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 1686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1697 key_raw_hex_in = | 1697 key_raw_hex_in = |
1698 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; | 1698 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; |
1699 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, | 1699 EXPECT_FALSE(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
1700 HexStringToBytes(key_raw_hex_in), | 1700 HexStringToBytes(key_raw_hex_in), |
1701 algorithm, | 1701 algorithm, |
1702 true, | 1702 true, |
1703 blink::WebCryptoKeyUsageWrapKey, | 1703 blink::WebCryptoKeyUsageWrapKey, |
1704 &key)); | 1704 &key)); |
1705 } | 1705 } |
1706 | 1706 |
| 1707 TEST_F(WebCryptoImplTest, MAYBE(AesKwEncryptDecryptKnownAnswer)) { |
| 1708 |
| 1709 // The following tests use test vectors from |
| 1710 // http://www.ietf.org/rfc/rfc3394.txt |
| 1711 |
| 1712 struct TestCase { |
| 1713 const char* kek_hex; |
| 1714 const char* data_hex; |
| 1715 const char* ciphertext_hex; |
| 1716 }; |
| 1717 |
| 1718 const TestCase kTests[] = { |
| 1719 // 4.1 Wrap 128 bits of Key Data with a 128-bit KEK |
| 1720 { |
| 1721 "000102030405060708090A0B0C0D0E0F", |
| 1722 "00112233445566778899AABBCCDDEEFF", |
| 1723 "1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5" |
| 1724 }, |
| 1725 // 4.2 Wrap 128 bits of Key Data with a 192-bit KEK |
| 1726 { |
| 1727 "000102030405060708090A0B0C0D0E0F1011121314151617", |
| 1728 "00112233445566778899AABBCCDDEEFF", |
| 1729 "96778B25AE6CA435F92B5B97C050AED2468AB8A17AD84E5D" |
| 1730 }, |
| 1731 // 4.3 Wrap 128 bits of Key Data with a 256-bit KEK |
| 1732 { |
| 1733 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", |
| 1734 "00112233445566778899AABBCCDDEEFF", |
| 1735 "64E8C3F9CE0F5BA263E9777905818A2A93C8191E7D6E8AE7" |
| 1736 }, |
| 1737 // 4.4 Wrap 192 bits of Key Data with a 192-bit KEK |
| 1738 { |
| 1739 "000102030405060708090A0B0C0D0E0F1011121314151617", |
| 1740 "00112233445566778899AABBCCDDEEFF0001020304050607", |
| 1741 "031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2" |
| 1742 }, |
| 1743 // 4.5 Wrap 192 bits of Key Data with a 256-bit KEK |
| 1744 { |
| 1745 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", |
| 1746 "00112233445566778899AABBCCDDEEFF0001020304050607", |
| 1747 "A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1" |
| 1748 |
| 1749 }, |
| 1750 // 4.6 Wrap 256 bits of Key Data with a 256-bit KEK |
| 1751 { |
| 1752 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", |
| 1753 "00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F", |
| 1754 "28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B" |
| 1755 "7A02DD21" |
| 1756 }, |
| 1757 }; |
| 1758 |
| 1759 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(kTests); index++) { |
| 1760 |
| 1761 SCOPED_TRACE(index); |
| 1762 const TestCase& test = kTests[index]; |
| 1763 const blink::WebCryptoAlgorithm algorithm = |
| 1764 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); |
| 1765 |
| 1766 // Import the key. |
| 1767 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
| 1768 test.kek_hex, |
| 1769 algorithm, |
| 1770 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); |
| 1771 |
| 1772 // Verify the exported raw key is identical to the imported data. |
| 1773 blink::WebArrayBuffer raw_key; |
| 1774 EXPECT_TRUE(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 1775 ExpectArrayBufferMatchesHex(test.kek_hex, raw_key); |
| 1776 |
| 1777 // Encrypt the data and verify the result against the known answer. |
| 1778 blink::WebArrayBuffer output; |
| 1779 EXPECT_TRUE(EncryptInternal(algorithm, |
| 1780 key, |
| 1781 HexStringToBytes(test.data_hex), |
| 1782 &output)); |
| 1783 ExpectArrayBufferMatchesHex(test.ciphertext_hex, output); |
| 1784 |
| 1785 // Decrypt the ciphertext and verify the result against the known input. |
| 1786 EXPECT_TRUE(DecryptInternal(algorithm, |
| 1787 key, |
| 1788 HexStringToBytes(test.ciphertext_hex), |
| 1789 &output)); |
| 1790 ExpectArrayBufferMatchesHex(test.data_hex, output); |
| 1791 } |
| 1792 } |
| 1793 |
| 1794 TEST_F(WebCryptoImplTest, MAYBE(AesKwEncryptDecryptFailures)) { |
| 1795 const blink::WebCryptoAlgorithm algorithm = |
| 1796 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); |
| 1797 blink::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
| 1798 "000102030405060708090A0B0C0D0E0F", |
| 1799 algorithm, |
| 1800 blink::WebCryptoKeyUsageWrapKey); |
| 1801 |
| 1802 // For encrypt, the input data size must be at least 16 bytes. Expect failure |
| 1803 // with a data size of 8 bytes, and success with 16. |
| 1804 blink::WebArrayBuffer output; |
| 1805 EXPECT_FALSE(EncryptInternal( |
| 1806 algorithm, |
| 1807 key, |
| 1808 HexStringToBytes("11f9ec1b249b2629"), |
| 1809 &output)); |
| 1810 EXPECT_TRUE(EncryptInternal( |
| 1811 algorithm, |
| 1812 key, |
| 1813 HexStringToBytes("2139128461ed6d341dff4db94f60094f"), |
| 1814 &output)); |
| 1815 |
| 1816 // For encrypt, the input data size must be a multiple of 8 bytes. Expect |
| 1817 // failure with a data size of 17 bytes. |
| 1818 EXPECT_FALSE(EncryptInternal( |
| 1819 algorithm, |
| 1820 key, |
| 1821 HexStringToBytes("0248cb45ca808c8aacfad2b2c092c15745"), |
| 1822 &output)); |
| 1823 } |
| 1824 |
1707 } // namespace content | 1825 } // namespace content |
OLD | NEW |