Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/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 2048 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2059 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; | 2059 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; |
| 2060 EXPECT_STATUS(Status::Error(), | 2060 EXPECT_STATUS(Status::Error(), |
| 2061 ImportKeyInternal(blink::WebCryptoKeyFormatRaw, | 2061 ImportKeyInternal(blink::WebCryptoKeyFormatRaw, |
| 2062 HexStringToBytes(key_raw_hex_in), | 2062 HexStringToBytes(key_raw_hex_in), |
| 2063 algorithm, | 2063 algorithm, |
| 2064 true, | 2064 true, |
| 2065 blink::WebCryptoKeyUsageWrapKey, | 2065 blink::WebCryptoKeyUsageWrapKey, |
| 2066 &key)); | 2066 &key)); |
| 2067 } | 2067 } |
| 2068 | 2068 |
| 2069 TEST_F(WebCryptoImplTest, MAYBE(AesKwEncryptDecryptKnownAnswer)) { | |
| 2070 | |
| 2071 // The following tests use test vectors from | |
| 2072 // http://www.ietf.org/rfc/rfc3394.txt | |
| 2073 | |
| 2074 struct TestCase { | |
| 2075 const char* kek_hex; | |
| 2076 const char* data_hex; | |
| 2077 const char* ciphertext_hex; | |
| 2078 }; | |
| 2079 | |
| 2080 //TODO(padolph): Move data out of this file. | |
| 2081 const TestCase kTests[] = { | |
| 2082 // 4.1 Wrap 128 bits of Key Data with a 128-bit KEK | |
| 2083 { | |
| 2084 "000102030405060708090A0B0C0D0E0F", | |
|
eroman
2014/02/26 23:40:22
With recent refactors I have been moving the test
| |
| 2085 "00112233445566778899AABBCCDDEEFF", | |
| 2086 "1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5" | |
| 2087 }, | |
| 2088 // 4.2 Wrap 128 bits of Key Data with a 192-bit KEK | |
| 2089 { | |
| 2090 "000102030405060708090A0B0C0D0E0F1011121314151617", | |
| 2091 "00112233445566778899AABBCCDDEEFF", | |
| 2092 "96778B25AE6CA435F92B5B97C050AED2468AB8A17AD84E5D" | |
| 2093 }, | |
| 2094 // 4.3 Wrap 128 bits of Key Data with a 256-bit KEK | |
| 2095 { | |
| 2096 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", | |
| 2097 "00112233445566778899AABBCCDDEEFF", | |
| 2098 "64E8C3F9CE0F5BA263E9777905818A2A93C8191E7D6E8AE7" | |
| 2099 }, | |
| 2100 // 4.4 Wrap 192 bits of Key Data with a 192-bit KEK | |
| 2101 { | |
| 2102 "000102030405060708090A0B0C0D0E0F1011121314151617", | |
| 2103 "00112233445566778899AABBCCDDEEFF0001020304050607", | |
| 2104 "031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2" | |
| 2105 }, | |
| 2106 // 4.5 Wrap 192 bits of Key Data with a 256-bit KEK | |
| 2107 { | |
| 2108 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", | |
| 2109 "00112233445566778899AABBCCDDEEFF0001020304050607", | |
| 2110 "A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1" | |
| 2111 | |
| 2112 }, | |
| 2113 // 4.6 Wrap 256 bits of Key Data with a 256-bit KEK | |
| 2114 { | |
| 2115 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F", | |
| 2116 "00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F", | |
| 2117 "28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B" | |
| 2118 "7A02DD21" | |
| 2119 }, | |
| 2120 }; | |
| 2121 | |
| 2122 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(kTests); index++) { | |
|
eroman
2014/02/26 23:40:22
please rename test_index to match other places in
padolph
2014/02/28 23:28:40
Done.
| |
| 2123 | |
| 2124 SCOPED_TRACE(index); | |
| 2125 const TestCase& test = kTests[index]; | |
| 2126 const blink::WebCryptoAlgorithm algorithm = | |
| 2127 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 2128 | |
| 2129 // Import the key. | |
| 2130 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | |
| 2131 HexStringToBytes(test.kek_hex), | |
| 2132 algorithm, | |
| 2133 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); | |
| 2134 | |
| 2135 // Verify the exported raw key is identical to the imported data. | |
| 2136 blink::WebArrayBuffer raw_key; | |
| 2137 EXPECT_STATUS_SUCCESS( | |
| 2138 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | |
| 2139 ExpectArrayBufferMatchesHex(test.kek_hex, raw_key); | |
| 2140 | |
| 2141 // Encrypt the data and verify the result against the known answer. | |
| 2142 blink::WebArrayBuffer output; | |
| 2143 EXPECT_STATUS_SUCCESS(EncryptInternal(algorithm, | |
| 2144 key, | |
| 2145 HexStringToBytes(test.data_hex), | |
| 2146 &output)); | |
| 2147 ExpectArrayBufferMatchesHex(test.ciphertext_hex, output); | |
| 2148 | |
| 2149 // Decrypt the ciphertext and verify the result against the known input. | |
| 2150 EXPECT_STATUS_SUCCESS(DecryptInternal(algorithm, | |
| 2151 key, | |
| 2152 HexStringToBytes(test.ciphertext_hex), | |
| 2153 &output)); | |
| 2154 ExpectArrayBufferMatchesHex(test.data_hex, output); | |
| 2155 } | |
| 2156 } | |
| 2157 | |
| 2158 TEST_F(WebCryptoImplTest, MAYBE(AesKwEncryptDecryptFailures)) { | |
| 2159 const blink::WebCryptoAlgorithm algorithm = | |
| 2160 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 2161 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | |
| 2162 HexStringToBytes("000102030405060708090A0B0C0D0E0F"), | |
| 2163 algorithm, | |
| 2164 blink::WebCryptoKeyUsageWrapKey); | |
| 2165 | |
| 2166 // For encrypt, the input data size must be at least 16 bytes. Expect failure | |
| 2167 // with a data size of 8 bytes, and success with 16. | |
| 2168 blink::WebArrayBuffer output; | |
| 2169 // TODO(padolph): Check for specific error? | |
| 2170 EXPECT_STATUS_ERROR(EncryptInternal( | |
| 2171 algorithm, | |
| 2172 key, | |
| 2173 HexStringToBytes("11f9ec1b249b2629"), | |
| 2174 &output)); | |
| 2175 EXPECT_STATUS_SUCCESS(EncryptInternal( | |
| 2176 algorithm, | |
| 2177 key, | |
| 2178 HexStringToBytes("2139128461ed6d341dff4db94f60094f"), | |
| 2179 &output)); | |
| 2180 | |
| 2181 // For encrypt, the input data size must be a multiple of 8 bytes. Expect | |
| 2182 // failure with a data size of 17 bytes. | |
| 2183 // TODO(padolph): Check for specific error? | |
| 2184 EXPECT_STATUS_ERROR(EncryptInternal( | |
| 2185 algorithm, | |
| 2186 key, | |
| 2187 HexStringToBytes("0248cb45ca808c8aacfad2b2c092c15745"), | |
| 2188 &output)); | |
| 2189 } | |
| 2190 | |
| 2191 TEST_F(WebCryptoImplTest, MAYBE(AesKwEncryptLargeData)) { | |
| 2192 const std::string large_data_hex = | |
| 2193 "308204be020100300d06092a864886f70d0101010500048204a8308204a4" | |
| 2194 "0201000282010100b3097277c6f9c16f7110441ccc00d8ec364d8b3eb1aa" | |
| 2195 "c3bca32a8877fe348a901a34c3b87917ba00e682caa7677e9e6b2fe2815c" | |
| 2196 "fb5ffd764b85a00ec6e2265693b2197fdcd832308b2b0ecd5069c8871161" | |
| 2197 "40f420c4050ba97157e93756ac1b8052b5ec6f2a846ab569afd6169465e1" | |
| 2198 "641bd1bc3542e4c56b4c925d89e8db6584f17fe10ac32d99d1858e08cf6e" | |
| 2199 "6c7ff000cff2147ce03941251a521f972da975d9a777ac4b8be34aad468c" | |
| 2200 "33422d7848fa0c24314d3f8872f1bbd86ea0d6a641351d8bbaeb075a080f" | |
| 2201 "6ee6749b39b02db5ee27d593bfe67c460435bb183ebb801ebd16d06f9f4a" | |
| 2202 "5043d44ba57e19dd993a50ef06a6783eba6cf58c55359449020301000102" | |
| 2203 "82010100931f9d481e6398f89a21b2c7334133011132b1cd68349abac61e" | |
| 2204 "aa9c687206b79a4167e08ce9d978e9f3ead29c32a9be0d5085dbc1da44af" | |
| 2205 "d429cfb8b4e89c76a2d24530146c38fa3932bdec2c3b7184c4dc7582e7f6" | |
| 2206 "0ceb636777c5772b2b2d424b35a2404be11acb4f3926a5d176d33befbc6f" | |
| 2207 "7c192763afe3f1971a4017213a165a13492f908ce62842fd3a0470dc4323" | |
| 2208 "b043466314e68d552843c18e7b17a3cd7bbabb511bb0df749690fd22a839" | |
| 2209 "32549abd601a2b003d5b0aa7da79f0903964a441d8104f1a4dda10fea67d" | |
| 2210 "0de925fe035345a2c6dd96af2ffd8ed7c5d31fac53d10ffc5d23126bcddf" | |
| 2211 "9c077e04a139329ff208f343365255241faf6c8102818100e3895040106b" | |
| 2212 "dd8d2e9db21051105690c92232db1d2b352558f1c7c7d2f13b6afe19c7b6" | |
| 2213 "05a111e03efac73f676ad86a03740de7dc5dff314cb600b7838b148343fb" | |
| 2214 "0f3a5dc682656b733fa8d747fe830932695ab390c65c21d57d5efd962215" | |
| 2215 "5713ffd4c1902ecc99475d4d3a7fe90744fdd226d8028752710677273362" | |
| 2216 "0f1102818100c96ef928fe52099d4e4fc2bb645cd8790830d19ef1171956" | |
| 2217 "48ab636b9b5c42549af7b737b099fc9604c3b3397abbd884f58e5f8a3e12" | |
| 2218 "8806ae29c81c64702de848995772bc96699c45d8ecf0ad4169c27d1bda51" | |
| 2219 "fbe70ce03f1a6c5bb731ab807b4f07d30ce98f1da73f7804c3ab48e007a1" | |
| 2220 "308024fa1db0d3bd30d207efa1b902818100a9749237a4033134fc0aa59a" | |
| 2221 "514501b342981d97e1d953f344928c5edd529b15cbb8176c10352cc2fd24" | |
| 2222 "774f590dd1aee2738407b1aeaf675fe20c169ff8ec85f612fbfc53ea8b22" | |
| 2223 "4d2bbfb556df5f44e78c8bb9e91161292c697abd4bce8c03a89e546176e6" | |
| 2224 "9273fd939080fb98574bfaadaddd0ff292256bd78cd5bd06c28102818053" | |
| 2225 "e328569f1b512fb6b656d5ada550ed8eb0ae0bb041bb66889affab87a6f3" | |
| 2226 "64c2a8d91f93277dde881b6c1f4af2c1e8154f76905eeb5ec4d1714b0a2f" | |
| 2227 "f5dbd879ab8a9498df571a22a8857c71dae50d7f06c374132114e6aac0f9" | |
| 2228 "5174c875b0eb296d8bc23bde432e2cab71e87f03b970d3fb1bd2ca6ae502" | |
| 2229 "392f04b9135dd902818045876edc196281cf5b1a33b677c0c521a3683fd2" | |
| 2230 "4ff7ef6a4f7934bdc9a91adf9b345b01a23e3c928b44a2f6549a77790d6a" | |
| 2231 "aaa6180530da566a4ade6c9625873bbb3261b203e1e5d2dd9e1fd17a4fa4" | |
| 2232 "01182dffc3b15d4af1661264a4380e1c48693377e668c4d18d1f3e9c5bd2" | |
| 2233 "513fa455369d54fd93d3f358ae8a5eae"; | |
| 2234 | |
| 2235 const blink::WebCryptoAlgorithm algorithm = | |
| 2236 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); | |
| 2237 | |
| 2238 // Import a wrapping key. | |
| 2239 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | |
| 2240 HexStringToBytes("000102030405060708090A0B0C0D0E0F"), | |
| 2241 algorithm, | |
| 2242 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); | |
| 2243 | |
| 2244 // Encrypt the large data. | |
| 2245 blink::WebArrayBuffer ciphertext; | |
| 2246 ASSERT_STATUS_SUCCESS(EncryptInternal(algorithm, | |
| 2247 key, | |
| 2248 HexStringToBytes(large_data_hex), | |
| 2249 &ciphertext)); | |
| 2250 | |
| 2251 // Decrypt the resulting ciphertext and verify against the input. | |
| 2252 blink::WebArrayBuffer plaintext; | |
| 2253 ASSERT_STATUS_SUCCESS(DecryptInternal( | |
| 2254 algorithm, | |
| 2255 key, | |
| 2256 reinterpret_cast<const unsigned char*>(ciphertext.data()), | |
| 2257 ciphertext.byteLength(), | |
| 2258 &plaintext)); | |
| 2259 ExpectArrayBufferMatchesHex(large_data_hex, plaintext); | |
| 2260 } | |
| 2261 | |
| 2069 // TODO(eroman): | 2262 // TODO(eroman): |
| 2070 // * Test decryption when the tag length exceeds input size | 2263 // * Test decryption when the tag length exceeds input size |
| 2071 // * Test decryption with empty input | 2264 // * Test decryption with empty input |
| 2072 // * Test decryption with tag length of 0. | 2265 // * Test decryption with tag length of 0. |
| 2073 TEST_F(WebCryptoImplTest, MAYBE(AesGcmSampleSets)) { | 2266 TEST_F(WebCryptoImplTest, MAYBE(AesGcmSampleSets)) { |
| 2074 // Some Linux test runners may not have a new enough version of NSS. | 2267 // Some Linux test runners may not have a new enough version of NSS. |
| 2075 if (!SupportsAesGcm()) { | 2268 if (!SupportsAesGcm()) { |
| 2076 LOG(WARNING) << "AES GCM not supported, skipping tests"; | 2269 LOG(WARNING) << "AES GCM not supported, skipping tests"; |
| 2077 return; | 2270 return; |
| 2078 } | 2271 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2153 if (test_tag_size_bits == wrong_tag_size_bits) | 2346 if (test_tag_size_bits == wrong_tag_size_bits) |
| 2154 continue; | 2347 continue; |
| 2155 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data, | 2348 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data, |
| 2156 wrong_tag_size_bits, test_cipher_text, | 2349 wrong_tag_size_bits, test_cipher_text, |
| 2157 test_authentication_tag, &plain_text)); | 2350 test_authentication_tag, &plain_text)); |
| 2158 } | 2351 } |
| 2159 } | 2352 } |
| 2160 } | 2353 } |
| 2161 | 2354 |
| 2162 } // namespace content | 2355 } // namespace content |
| OLD | NEW |