Chromium Code Reviews| 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 "webcrypto_impl.h" | 5 #include "webcrypto_impl.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | |
| 11 #include "content/public/renderer/content_renderer_client.h" | 12 #include "content/public/renderer/content_renderer_client.h" |
| 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 13 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 13 #include "content/renderer/webcrypto_impl.h" | 14 #include "content/renderer/webcrypto_impl.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 16 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 20 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { | 22 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { |
| 21 WebKit::WebCryptoAlgorithmIdSha1, | 23 WebKit::WebCryptoAlgorithmIdSha1, |
| 22 WebKit::WebCryptoAlgorithmIdSha224, | 24 WebKit::WebCryptoAlgorithmIdSha224, |
| 23 WebKit::WebCryptoAlgorithmIdSha256, | 25 WebKit::WebCryptoAlgorithmIdSha256, |
| 24 WebKit::WebCryptoAlgorithmIdSha384, | 26 WebKit::WebCryptoAlgorithmIdSha384, |
| 25 WebKit::WebCryptoAlgorithmIdSha512 | 27 WebKit::WebCryptoAlgorithmIdSha512 |
| 26 }; | 28 }; |
| 27 | 29 |
| 28 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { | 30 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 33 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
| 32 // The results are stored here in hex format for readability. | 34 // The results are stored here in hex format for readability. |
| 33 // | 35 // |
| 34 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | 36 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced |
| 35 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | 37 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 |
| 36 struct { | 38 struct { |
| 37 const char* input; | 39 const char* input; |
| 38 size_t input_length; | 40 unsigned input_length; |
| 39 const char* hex_result[arraysize(kAlgorithmIds)]; | 41 const char* hex_result[arraysize(kAlgorithmIds)]; |
| 40 } input_set[] = { | 42 } input_set[] = { |
| 41 { | 43 { |
| 42 "", 0, | 44 "", 0, |
| 43 { | 45 { |
| 44 // echo -n "" | sha1sum | 46 // echo -n "" | sha1sum |
| 45 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | 47 "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
| 46 // echo -n "" | sha224sum | 48 // echo -n "" | sha224sum |
| 47 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | 49 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", |
| 48 // echo -n "" | sha256sum | 50 // echo -n "" | sha256sum |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | 87 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" |
| 86 "22780d7e1b95bfeaa86a678e4552", | 88 "22780d7e1b95bfeaa86a678e4552", |
| 87 // echo -n -e "\000\001\002\003\004\005" | sha512sum | 89 // echo -n -e "\000\001\002\003\004\005" | sha512sum |
| 88 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | 90 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" |
| 89 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | 91 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", |
| 90 }, | 92 }, |
| 91 }, | 93 }, |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { | 96 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { |
| 97 SCOPED_TRACE(base::StringPrintf("id_index: %zu", id_index)); | |
|
Ryan Sleevi
2013/09/12 23:25:20
BUG: Don't use %zu. Use PRIuS
http://google-style
Bryan Eyler
2013/09/13 20:37:36
Done.
| |
| 98 | |
| 95 WebKit::WebCryptoAlgorithm algorithm( | 99 WebKit::WebCryptoAlgorithm algorithm( |
| 96 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 100 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 97 kAlgorithmIds[id_index], NULL)); | 101 kAlgorithmIds[id_index], NULL)); |
| 98 | 102 |
| 99 for (size_t set_index = 0; | 103 for (size_t set_index = 0; |
| 100 set_index < ARRAYSIZE_UNSAFE(input_set); | 104 set_index < ARRAYSIZE_UNSAFE(input_set); |
| 101 set_index++) { | 105 set_index++) { |
| 106 SCOPED_TRACE(base::StringPrintf("set_index: %zu", set_index)); | |
| 107 | |
| 102 WebKit::WebArrayBuffer array_buffer; | 108 WebKit::WebArrayBuffer array_buffer; |
| 103 | 109 |
| 104 WebCryptoImpl crypto; | 110 WebCryptoImpl crypto; |
| 105 crypto.DigestInternal( | 111 EXPECT_TRUE( |
| 106 algorithm, | 112 crypto.DigestInternal( |
| 107 reinterpret_cast<const unsigned char*>(input_set[set_index].input), | 113 algorithm, |
| 108 input_set[set_index].input_length, | 114 reinterpret_cast<const unsigned char*>( |
| 109 &array_buffer); | 115 input_set[set_index].input), |
| 116 input_set[set_index].input_length, | |
| 117 &array_buffer)); | |
| 110 | 118 |
| 111 // Ignore case, it's checking the hex value. | 119 // Ignore case, it's checking the hex value. |
| 112 EXPECT_STRCASEEQ( | 120 EXPECT_STRCASEEQ( |
| 113 input_set[set_index].hex_result[id_index], | 121 input_set[set_index].hex_result[id_index], |
| 114 base::HexEncode( | 122 base::HexEncode( |
| 115 array_buffer.data(), array_buffer.byteLength()).c_str()); | 123 array_buffer.data(), array_buffer.byteLength()).c_str()); |
| 116 } | 124 } |
| 117 } | 125 } |
| 118 } | 126 } |
| 119 | 127 |
| 128 TEST_F(WebCryptoImplTest, HMACSampleSets) { | |
| 129 struct { | |
| 130 WebKit::WebCryptoAlgorithmId algorithm; | |
| 131 const char* key; | |
| 132 const char* message; | |
| 133 const char* mac; | |
| 134 } input_set[] = { | |
| 135 // Empty sets. Result generated via OpenSSL commandline tool. These | |
| 136 // particular results are also posted on the Wikipedia page examples: | |
| 137 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code | |
| 138 { | |
| 139 WebKit::WebCryptoAlgorithmIdSha1, | |
| 140 "", | |
| 141 "", | |
| 142 // openssl dgst -sha1 -hmac "" < /dev/null | |
| 143 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", | |
| 144 }, | |
| 145 { | |
| 146 WebKit::WebCryptoAlgorithmIdSha256, | |
| 147 "", | |
| 148 "", | |
| 149 // openssl dgst -sha256 -hmac "" < /dev/null | |
| 150 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", | |
| 151 }, | |
| 152 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 | |
| 153 // Download: | |
| 154 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip | |
| 155 // L=20 set 45 | |
| 156 { | |
| 157 WebKit::WebCryptoAlgorithmIdSha1, | |
| 158 // key | |
| 159 "59785928d72516e31272", | |
| 160 // message | |
| 161 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" | |
| 162 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" | |
| 163 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" | |
| 164 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", | |
| 165 // mac | |
| 166 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", | |
| 167 }, | |
| 168 // L=20 set 299 | |
| 169 { | |
| 170 WebKit::WebCryptoAlgorithmIdSha1, | |
| 171 // key | |
| 172 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" | |
| 173 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" | |
| 174 "f5866aa62930d75b58f6", | |
| 175 // message | |
| 176 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" | |
| 177 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" | |
| 178 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" | |
| 179 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", | |
| 180 // mac | |
| 181 "4ac41ab89f625c60125ed65ffa958c6b490ea670", | |
| 182 }, | |
| 183 // L=32, set 30 | |
| 184 { | |
| 185 WebKit::WebCryptoAlgorithmIdSha256, | |
| 186 // key | |
| 187 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" | |
| 188 "58ffefa176", | |
| 189 // message | |
| 190 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | |
| 191 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | |
| 192 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | |
| 193 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", | |
| 194 // mac | |
| 195 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", | |
| 196 }, | |
| 197 // L=32, set 224 | |
| 198 { | |
| 199 WebKit::WebCryptoAlgorithmIdSha256, | |
| 200 // key | |
| 201 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" | |
| 202 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | |
| 203 "3371289b", | |
| 204 // message | |
| 205 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | |
| 206 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | |
| 207 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | |
| 208 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | |
| 209 // mac | |
| 210 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | |
| 211 }, | |
| 212 }; | |
| 213 | |
| 214 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(input_set); index++) { | |
| 215 SCOPED_TRACE(base::StringPrintf("index: %zu", index)); | |
| 216 | |
| 217 WebKit::WebCryptoAlgorithm hash_algorithm( | |
| 218 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 219 input_set[index].algorithm, NULL)); | |
| 220 | |
| 221 scoped_ptr<WebKit::WebCryptoHmacParams> hmac_params( | |
| 222 new WebKit::WebCryptoHmacParams(hash_algorithm)); | |
| 223 | |
| 224 WebKit::WebCryptoAlgorithm hmac_algorithm( | |
| 225 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 226 WebKit::WebCryptoAlgorithmIdHmac, hmac_params.release())); | |
| 227 | |
| 228 WebKit::WebCryptoKeyType type; | |
| 229 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
| 230 | |
| 231 std::vector<uint8> key_raw; | |
| 232 base::HexStringToBytes(input_set[index].key, &key_raw); | |
| 233 | |
| 234 WebCryptoImpl crypto; | |
| 235 | |
| 236 EXPECT_TRUE( | |
| 237 crypto.ImportKeyInternal( | |
| 238 WebKit::WebCryptoKeyFormatRaw, | |
| 239 key_raw.data(), | |
| 240 key_raw.size(), | |
| 241 hmac_algorithm, | |
| 242 WebKit::WebCryptoKeyUsageSign, | |
| 243 &handle, | |
| 244 &type)); | |
| 245 | |
| 246 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, type); | |
| 247 ASSERT_TRUE(handle.get()); | |
| 248 | |
| 249 WebKit::WebCryptoKey crypto_key = | |
| 250 WebKit::WebCryptoKey::create( | |
| 251 handle.release(), | |
| 252 type, | |
| 253 false, | |
| 254 hmac_algorithm, | |
| 255 WebKit::WebCryptoKeyUsageSign); | |
| 256 | |
| 257 std::vector<uint8> message_raw; | |
| 258 base::HexStringToBytes(input_set[index].message, &message_raw); | |
| 259 | |
| 260 WebKit::WebArrayBuffer array_buffer; | |
| 261 | |
| 262 EXPECT_TRUE( | |
| 263 crypto.SignInternal( | |
| 264 hmac_algorithm, | |
| 265 crypto_key, | |
| 266 message_raw.data(), | |
| 267 message_raw.size(), | |
| 268 &array_buffer)); | |
| 269 | |
| 270 // Ignore case, it's checking the hex value. | |
| 271 EXPECT_STRCASEEQ( | |
| 272 input_set[index].mac, | |
| 273 base::HexEncode( | |
| 274 array_buffer.data(), array_buffer.byteLength()).c_str()); | |
| 275 } | |
| 276 } | |
| 277 | |
| 120 } // namespace content | 278 } // namespace content |
| OLD | NEW |