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

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

Issue 118623002: [webcrypto] Add raw symmetric key AES-KW wrap/unwrap for NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor comment change: added a TODO Created 6 years, 9 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
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/renderer/webcrypto/shared_crypto.h" 5 #include "content/renderer/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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 return bytes; 138 return bytes;
139 } 139 }
140 140
141 void ExpectArrayBufferMatches(const std::vector<uint8>& expected, 141 void ExpectArrayBufferMatches(const std::vector<uint8>& expected,
142 const blink::WebArrayBuffer& actual) { 142 const blink::WebArrayBuffer& actual) {
143 EXPECT_EQ( 143 EXPECT_EQ(
144 base::HexEncode(webcrypto::Uint8VectorStart(expected), expected.size()), 144 base::HexEncode(webcrypto::Uint8VectorStart(expected), expected.size()),
145 base::HexEncode(actual.data(), actual.byteLength())); 145 base::HexEncode(actual.data(), actual.byteLength()));
146 } 146 }
147 147
148 void ExpectArrayBufferNotMatch(const std::vector<uint8>& expected,
eroman 2014/03/04 03:16:26 For a future change, the right fix is to change th
padolph 2014/03/04 17:20:07 Done.
149 const blink::WebArrayBuffer& actual) {
150 EXPECT_NE(
151 base::HexEncode(webcrypto::Uint8VectorStart(expected), expected.size()),
152 base::HexEncode(actual.data(), actual.byteLength()));
153 }
154
148 void ExpectCryptoDataMatchesHex(const std::string& expected_hex, 155 void ExpectCryptoDataMatchesHex(const std::string& expected_hex,
149 const CryptoData& actual) { 156 const CryptoData& actual) {
150 EXPECT_STRCASEEQ( 157 EXPECT_STRCASEEQ(
151 expected_hex.c_str(), 158 expected_hex.c_str(),
152 base::HexEncode(actual.bytes(), actual.byte_length()).c_str()); 159 base::HexEncode(actual.bytes(), actual.byte_length()).c_str());
153 } 160 }
154 161
155 void ExpectArrayBufferMatchesHex(const std::string& expected_hex, 162 void ExpectArrayBufferMatchesHex(const std::string& expected_hex,
156 const blink::WebArrayBuffer& array_buffer) { 163 const blink::WebArrayBuffer& array_buffer) {
157 return ExpectCryptoDataMatchesHex(expected_hex, CryptoData(array_buffer)); 164 return ExpectCryptoDataMatchesHex(expected_hex, CryptoData(array_buffer));
(...skipping 1855 matching lines...) Expand 10 before | Expand all | Expand 10 after
2013 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a"; 2020 "72d4e475ff34215416c9ad9c8281247a4d730c5f275ac23f376e73e3bce8d7d5a";
2014 EXPECT_STATUS(Status::Error(), 2021 EXPECT_STATUS(Status::Error(),
2015 ImportKey(blink::WebCryptoKeyFormatRaw, 2022 ImportKey(blink::WebCryptoKeyFormatRaw,
2016 CryptoData(HexStringToBytes(key_raw_hex_in)), 2023 CryptoData(HexStringToBytes(key_raw_hex_in)),
2017 algorithm, 2024 algorithm,
2018 true, 2025 true,
2019 blink::WebCryptoKeyUsageWrapKey, 2026 blink::WebCryptoKeyUsageWrapKey,
2020 &key)); 2027 &key));
2021 } 2028 }
2022 2029
2030 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapKnownAnswer)) {
2031 scoped_ptr<base::ListValue> tests;
2032 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
2033
2034 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
2035 SCOPED_TRACE(test_index);
2036 base::DictionaryValue* test;
2037 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
2038 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek");
2039 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
2040 const std::vector<uint8> test_ciphertext =
2041 GetBytesFromHexString(test, "ciphertext");
2042 const blink::WebCryptoAlgorithm wrapping_algorithm =
2043 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
2044
2045 // Import the wrapping key.
2046 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
2047 test_kek,
2048 wrapping_algorithm,
2049 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey);
2050
2051 // Import the key to be wrapped.
2052 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
2053 test_key,
2054 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2055 blink::WebCryptoKeyUsageEncrypt);
2056
2057 // Wrap the key and verify the ciphertext result against the known answer.
2058 blink::WebArrayBuffer wrapped_key;
2059 ASSERT_STATUS_SUCCESS(WrapKey(blink::WebCryptoKeyFormatRaw,
2060 wrapping_key,
2061 key,
2062 wrapping_algorithm,
2063 &wrapped_key));
2064 ExpectArrayBufferMatches(test_ciphertext, wrapped_key);
2065
2066 // Unwrap the known ciphertext to get a new test_key.
2067 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
2068 ASSERT_STATUS_SUCCESS(
2069 UnwrapKey(blink::WebCryptoKeyFormatRaw,
2070 CryptoData(test_ciphertext),
2071 wrapping_key,
2072 wrapping_algorithm,
2073 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2074 true,
2075 blink::WebCryptoKeyUsageEncrypt,
2076 &unwrapped_key));
2077 EXPECT_FALSE(key.isNull());
2078 EXPECT_TRUE(key.handle());
2079 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
2080 EXPECT_EQ(
2081 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc).id(),
2082 key.algorithm().id());
2083 EXPECT_EQ(true, key.extractable());
2084 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages());
2085
2086 // Export the new key and compare its raw bytes with the original known key.
2087 blink::WebArrayBuffer raw_key;
2088 EXPECT_STATUS_SUCCESS(
2089 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
2090 ExpectArrayBufferMatches(test_key, raw_key);
2091 }
2092 }
2093
2094 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapErrors)) {
2095 scoped_ptr<base::ListValue> tests;
2096 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
2097 base::DictionaryValue* test;
2098 // Use 256 bits of data with a 256-bit KEK
2099 ASSERT_TRUE(tests->GetDictionary(5, &test));
2100 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek");
2101 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
2102 const std::vector<uint8> test_ciphertext =
2103 GetBytesFromHexString(test, "ciphertext");
2104 const blink::WebCryptoAlgorithm wrapping_algorithm =
2105 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
2106 const blink::WebCryptoAlgorithm key_algorithm =
2107 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
2108 // Import the wrapping key.
2109 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
2110 test_kek,
2111 wrapping_algorithm,
2112 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey);
2113 // Import the key to be wrapped.
2114 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
2115 test_key,
2116 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2117 blink::WebCryptoKeyUsageEncrypt);
2118
2119 // Unwrap with null algorithm must fail.
2120 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
2121 EXPECT_STATUS(Status::ErrorMissingAlgorithmUnwrapRawKey(),
2122 UnwrapKey(blink::WebCryptoKeyFormatRaw,
2123 CryptoData(test_ciphertext),
2124 wrapping_key,
2125 wrapping_algorithm,
2126 blink::WebCryptoAlgorithm::createNull(),
2127 true,
2128 blink::WebCryptoKeyUsageEncrypt,
2129 &unwrapped_key));
2130
2131 // Unwrap with wrapped data too small must fail.
2132 const std::vector<uint8> small_data(test_ciphertext.begin(),
2133 test_ciphertext.begin() + 23);
2134 EXPECT_STATUS(Status::ErrorDataTooSmall(),
2135 UnwrapKey(blink::WebCryptoKeyFormatRaw,
2136 CryptoData(small_data),
2137 wrapping_key,
2138 wrapping_algorithm,
2139 key_algorithm,
2140 true,
2141 blink::WebCryptoKeyUsageEncrypt,
2142 &unwrapped_key));
2143
2144 // Unwrap with wrapped data size not a multiple of 8 bytes must fail.
2145 const std::vector<uint8> unaligned_data(test_ciphertext.begin(),
2146 test_ciphertext.end() - 2);
2147 EXPECT_STATUS(Status::ErrorInvalidAesKwDataLength(),
2148 UnwrapKey(blink::WebCryptoKeyFormatRaw,
2149 CryptoData(unaligned_data),
2150 wrapping_key,
2151 wrapping_algorithm,
2152 key_algorithm,
2153 true,
2154 blink::WebCryptoKeyUsageEncrypt,
2155 &unwrapped_key));
2156 }
2157
2158 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyUnwrapCorruptData)) {
2159 scoped_ptr<base::ListValue> tests;
2160 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
2161 base::DictionaryValue* test;
2162 // Use 256 bits of data with a 256-bit KEK
2163 ASSERT_TRUE(tests->GetDictionary(5, &test));
2164 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek");
2165 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
2166 const std::vector<uint8> test_ciphertext =
2167 GetBytesFromHexString(test, "ciphertext");
2168 const blink::WebCryptoAlgorithm wrapping_algorithm =
2169 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
2170
2171 // Import the wrapping key.
2172 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
2173 test_kek,
2174 wrapping_algorithm,
2175 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey);
2176
2177 // Unwrap a corrupted version of the known ciphertext. Unwrap should pass but
2178 // will produce a key that is different than the known original.
2179 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
2180 ASSERT_STATUS_SUCCESS(
2181 UnwrapKey(blink::WebCryptoKeyFormatRaw,
2182 CryptoData(Corrupted(test_ciphertext)),
2183 wrapping_key,
2184 wrapping_algorithm,
2185 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2186 true,
2187 blink::WebCryptoKeyUsageEncrypt,
2188 &unwrapped_key));
2189
2190 // Export the new key and compare its raw bytes with the original known key.
2191 // The comparison should fail.
2192 blink::WebArrayBuffer raw_key;
2193 EXPECT_STATUS_SUCCESS(
2194 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
2195 ExpectArrayBufferNotMatch(test_key, raw_key);
2196 }
2197
2023 // TODO(eroman): 2198 // TODO(eroman):
2024 // * Test decryption when the tag length exceeds input size 2199 // * Test decryption when the tag length exceeds input size
2025 // * Test decryption with empty input 2200 // * Test decryption with empty input
2026 // * Test decryption with tag length of 0. 2201 // * Test decryption with tag length of 0.
2027 TEST_F(SharedCryptoTest, MAYBE(AesGcmSampleSets)) { 2202 TEST_F(SharedCryptoTest, MAYBE(AesGcmSampleSets)) {
2028 // Some Linux test runners may not have a new enough version of NSS. 2203 // Some Linux test runners may not have a new enough version of NSS.
2029 if (!SupportsAesGcm()) { 2204 if (!SupportsAesGcm()) {
2030 LOG(WARNING) << "AES GCM not supported, skipping tests"; 2205 LOG(WARNING) << "AES GCM not supported, skipping tests";
2031 return; 2206 return;
2032 } 2207 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
2136 test_cipher_text, 2311 test_cipher_text,
2137 test_authentication_tag, 2312 test_authentication_tag,
2138 &plain_text)); 2313 &plain_text));
2139 } 2314 }
2140 } 2315 }
2141 } 2316 }
2142 2317
2143 } // namespace webcrypto 2318 } // namespace webcrypto
2144 2319
2145 } // namespace content 2320 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698