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

Side by Side Diff: content/child/webcrypto/shared_crypto_unittest.cc

Issue 233733004: [webcrypto] Make operations run on worker threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix a crash (RSA-OAEP keys not supported) Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/child/webcrypto/shared_crypto.h" 5 #include "content/child/webcrypto/shared_crypto.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 ASSERT_EQ(expected.ToString(), (code).ToString()) 45 ASSERT_EQ(expected.ToString(), (code).ToString())
46 #define EXPECT_STATUS_SUCCESS(code) EXPECT_STATUS(Status::Success(), code) 46 #define EXPECT_STATUS_SUCCESS(code) EXPECT_STATUS(Status::Success(), code)
47 #define ASSERT_STATUS_SUCCESS(code) ASSERT_STATUS(Status::Success(), code) 47 #define ASSERT_STATUS_SUCCESS(code) ASSERT_STATUS(Status::Success(), code)
48 48
49 namespace content { 49 namespace content {
50 50
51 namespace webcrypto { 51 namespace webcrypto {
52 52
53 namespace { 53 namespace {
54 54
55 // -----------------------------------------------------------------------------
56 // TODO(eroman): Remove these helpers and convert all of the tests to using the
57 // std::vector<> flavor of functions directly.
Ryan Sleevi 2014/04/18 00:51:26 At the risk of being a PITA, can you rename the fu
eroman 2014/04/18 18:45:56 Agreed. My plan is to refactor the tests to call t
58 // -----------------------------------------------------------------------------
59
60 blink::WebArrayBuffer CreateArrayBuffer(const uint8* data,
61 unsigned int data_size) {
62 blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(data_size, 1);
63 DCHECK(!buffer.isNull());
64 if (data_size) // data_size == 0 might mean the data pointer is invalid
65 memcpy(buffer.data(), data, data_size);
66 return buffer;
67 }
68
69 void AssignWebArrayBuffer(const std::vector<uint8>& in,
70 blink::WebArrayBuffer* out) {
71 *out = CreateArrayBuffer(Uint8VectorStart(in), in.size());
72 }
73
74 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm,
75 const blink::WebCryptoKey& key,
76 const CryptoData& data,
77 blink::WebArrayBuffer* web_buffer) {
78 std::vector<uint8> buffer;
79 Status status = Encrypt(algorithm, key, data, &buffer);
80 AssignWebArrayBuffer(buffer, web_buffer);
81 return status;
82 }
83
84 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm,
85 const blink::WebCryptoKey& key,
86 const CryptoData& data,
87 blink::WebArrayBuffer* web_buffer) {
88 std::vector<uint8> buffer;
89 Status status = Decrypt(algorithm, key, data, &buffer);
90 AssignWebArrayBuffer(buffer, web_buffer);
91 return status;
92 }
93
94 Status Digest(const blink::WebCryptoAlgorithm& algorithm,
95 const CryptoData& data,
96 blink::WebArrayBuffer* web_buffer) {
97 std::vector<uint8> buffer;
98 Status status = Digest(algorithm, data, &buffer);
99 AssignWebArrayBuffer(buffer, web_buffer);
100 return status;
101 }
102
103 Status ExportKey(blink::WebCryptoKeyFormat format,
104 const blink::WebCryptoKey& key,
105 blink::WebArrayBuffer* web_buffer) {
106 std::vector<uint8> buffer;
107 Status status = webcrypto::ExportKey(format, key, &buffer);
108 AssignWebArrayBuffer(buffer, web_buffer);
109 return status;
110 }
111
112 Status Sign(const blink::WebCryptoAlgorithm& algorithm,
113 const blink::WebCryptoKey& key,
114 const CryptoData& data,
115 blink::WebArrayBuffer* web_buffer) {
116 std::vector<uint8> buffer;
117
118 Status status = Sign(algorithm, key, data, &buffer);
119 AssignWebArrayBuffer(buffer, web_buffer);
120 return status;
121 }
122
123 Status WrapKey(blink::WebCryptoKeyFormat format,
124 const blink::WebCryptoKey& wrapping_key,
125 const blink::WebCryptoKey& key_to_wrap,
126 const blink::WebCryptoAlgorithm& wrapping_algorithm,
127 blink::WebArrayBuffer* web_buffer) {
128 std::vector<uint8> buffer;
129
130 Status status = webcrypto::WrapKey(
131 format, wrapping_key, key_to_wrap, wrapping_algorithm, &buffer);
132 AssignWebArrayBuffer(buffer, web_buffer);
133 return status;
134 }
135
136 // -----------------------------------------------------------------------------
137
55 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a 138 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a
56 // runtime dependency. Test it by trying to import a key. 139 // runtime dependency. Test it by trying to import a key.
57 // TODO(padolph): Consider caching the result of the import key test. 140 // TODO(padolph): Consider caching the result of the import key test.
58 bool SupportsAesGcm() { 141 bool SupportsAesGcm() {
59 std::vector<uint8> key_raw(16, 0); 142 std::vector<uint8> key_raw(16, 0);
60 143
61 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 144 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
62 Status status = ImportKey(blink::WebCryptoKeyFormatRaw, 145 Status status = ImportKey(blink::WebCryptoKeyFormatRaw,
63 CryptoData(key_raw), 146 CryptoData(key_raw),
64 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), 147 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
(...skipping 1719 matching lines...) Expand 10 before | Expand all | Expand 10 after
1784 EXPECT_EQ(test.usage, key.usages()); 1867 EXPECT_EQ(test.usage, key.usages());
1785 1868
1786 // Export the key in raw format and compare to the original. 1869 // Export the key in raw format and compare to the original.
1787 blink::WebArrayBuffer key_raw_out; 1870 blink::WebArrayBuffer key_raw_out;
1788 ASSERT_STATUS_SUCCESS( 1871 ASSERT_STATUS_SUCCESS(
1789 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); 1872 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out));
1790 ExpectArrayBufferMatchesHex(test.key_hex, key_raw_out); 1873 ExpectArrayBufferMatchesHex(test.key_hex, key_raw_out);
1791 } 1874 }
1792 } 1875 }
1793 1876
1877 TEST_F(SharedCryptoTest, MAYBE(ExportJwkEmptySymmetricKey)) {
1878 const blink::WebCryptoAlgorithm import_algorithm =
1879 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1);
1880
1881 blink::WebCryptoKeyUsageMask usages = blink::WebCryptoKeyUsageSign;
1882 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1883
1884 // Import a zero-byte HMAC key.
1885 const char key_data_hex[] = "";
1886 key = ImportSecretKeyFromRaw(
1887 HexStringToBytes(key_data_hex), import_algorithm, usages);
1888 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits());
1889
1890 // Export the key in JWK format and validate.
1891 blink::WebArrayBuffer json;
1892 ASSERT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatJwk, key, &json));
1893 EXPECT_TRUE(VerifySecretJwk(json, "HS1", key_data_hex, usages));
1894
1895 // Now try re-importing the JWK key.
1896 key = blink::WebCryptoKey::createNull();
1897 EXPECT_STATUS_SUCCESS(ImportKey(blink::WebCryptoKeyFormatJwk,
1898 CryptoData(json),
1899 import_algorithm,
1900 true,
1901 usages,
1902 &key));
1903
1904 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1905 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits());
1906
1907 blink::WebArrayBuffer exported_key_data;
1908 EXPECT_STATUS_SUCCESS(
1909 ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key_data));
1910
1911 EXPECT_EQ(0u, exported_key_data.byteLength());
1912 }
1913
1794 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) { 1914 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) {
1795 // Passing case: Import a valid RSA key in SPKI format. 1915 // Passing case: Import a valid RSA key in SPKI format.
1796 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1916 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1797 ASSERT_STATUS_SUCCESS( 1917 ASSERT_STATUS_SUCCESS(
1798 ImportKey(blink::WebCryptoKeyFormatSpki, 1918 ImportKey(blink::WebCryptoKeyFormatSpki,
1799 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 1919 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
1800 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1920 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1801 true, 1921 true,
1802 blink::WebCryptoKeyUsageEncrypt, 1922 blink::WebCryptoKeyUsageEncrypt,
1803 &key)); 1923 &key));
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
2030 algorithm, extractable, usage_mask, &public_key, &private_key)); 2150 algorithm, extractable, usage_mask, &public_key, &private_key));
2031 EXPECT_FALSE(public_key.isNull()); 2151 EXPECT_FALSE(public_key.isNull());
2032 EXPECT_FALSE(private_key.isNull()); 2152 EXPECT_FALSE(private_key.isNull());
2033 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 2153 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
2034 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 2154 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
2035 EXPECT_TRUE(public_key.extractable()); 2155 EXPECT_TRUE(public_key.extractable());
2036 EXPECT_EQ(extractable, private_key.extractable()); 2156 EXPECT_EQ(extractable, private_key.extractable());
2037 EXPECT_EQ(usage_mask, public_key.usages()); 2157 EXPECT_EQ(usage_mask, public_key.usages());
2038 EXPECT_EQ(usage_mask, private_key.usages()); 2158 EXPECT_EQ(usage_mask, private_key.usages());
2039 2159
2040 // Successful WebCryptoAlgorithmIdRsaOaep key generation. 2160 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha256)
2041 algorithm = CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaOaep, 2161 algorithm =
2042 blink::WebCryptoAlgorithmIdSha256, 2162 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2043 modulus_length, 2163 blink::WebCryptoAlgorithmIdSha256,
2044 public_exponent); 2164 modulus_length,
2165 public_exponent);
2045 EXPECT_STATUS_SUCCESS(GenerateKeyPair( 2166 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
2046 algorithm, extractable, usage_mask, &public_key, &private_key)); 2167 algorithm, extractable, usage_mask, &public_key, &private_key));
2047 EXPECT_FALSE(public_key.isNull()); 2168 EXPECT_FALSE(public_key.isNull());
2048 EXPECT_FALSE(private_key.isNull()); 2169 EXPECT_FALSE(private_key.isNull());
2049 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 2170 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
2050 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 2171 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
2051 EXPECT_EQ(modulus_length, 2172 EXPECT_EQ(modulus_length,
2052 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); 2173 public_key.algorithm().rsaHashedParams()->modulusLengthBits());
2053 EXPECT_EQ(modulus_length, 2174 EXPECT_EQ(modulus_length,
2054 private_key.algorithm().rsaHashedParams()->modulusLengthBits()); 2175 private_key.algorithm().rsaHashedParams()->modulusLengthBits());
(...skipping 1205 matching lines...) Expand 10 before | Expand all | Expand 10 after
3260 algorithm, 3381 algorithm,
3261 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)), 3382 CreateAesCbcAlgorithm(std::vector<uint8>(0, 16)),
3262 true, 3383 true,
3263 blink::WebCryptoKeyUsageEncrypt, 3384 blink::WebCryptoKeyUsageEncrypt,
3264 &unwrapped_key)); 3385 &unwrapped_key));
3265 } 3386 }
3266 3387
3267 } // namespace webcrypto 3388 } // namespace webcrypto
3268 3389
3269 } // namespace content 3390 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698