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 "content/public/renderer/content_renderer_client.h" | 11 #include "content/public/renderer/content_renderer_client.h" |
| 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | 12 #include "content/renderer/renderer_webkitplatformsupport_impl.h" |
| 13 #include "content/renderer/webcrypto_impl.h" | 13 #include "content/renderer/webcrypto_impl.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 18 | 18 |
| 19 using namespace WebKit; | |
| 20 | |
| 19 namespace content { | 21 namespace content { |
| 20 | 22 |
| 21 const WebKit::WebCryptoAlgorithmId kAlgorithmIds[] = { | 23 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { |
| 22 WebKit::WebCryptoAlgorithmIdSha1, | 24 public: |
| 23 WebKit::WebCryptoAlgorithmIdSha224, | 25 WebCryptoKey ImportSecretKeyFromRawHexString( |
| 24 WebKit::WebCryptoAlgorithmIdSha256, | 26 const std::string& key_hex, |
| 25 WebKit::WebCryptoAlgorithmIdSha384, | 27 const WebCryptoAlgorithm& algorithm, |
| 26 WebKit::WebCryptoAlgorithmIdSha512 | 28 WebCryptoKeyUsageMask usage) { |
| 27 }; | 29 WebCryptoKeyType type; |
| 30 scoped_ptr<WebCryptoKeyHandle> handle; | |
| 28 | 31 |
| 29 class WebCryptoImplTest : public testing::Test, public WebCryptoImpl { | 32 std::vector<uint8> key_raw = HexStringToBytes(key_hex); |
| 33 | |
| 34 EXPECT_TRUE(ImportKeyInternal(WebCryptoKeyFormatRaw, | |
| 35 key_raw.data(), | |
| 36 key_raw.size(), | |
| 37 algorithm, | |
| 38 usage, | |
| 39 &handle, | |
| 40 &type)); | |
| 41 | |
| 42 EXPECT_EQ(WebCryptoKeyTypeSecret, type); | |
| 43 EXPECT_TRUE(handle.get()); | |
|
Bryan Eyler
2013/09/23 20:04:51
Does the WebCryptoKey interface handle a null valu
eroman
2013/09/23 20:11:23
I had to make this an EXPECT_ rather than ASSERT_,
| |
| 44 | |
| 45 WebCryptoKey crypto_key = WebCryptoKey::create(handle.release(), | |
| 46 type, | |
| 47 /*extracatable=*/false, | |
| 48 algorithm, | |
| 49 WebCryptoKeyUsageSign); | |
| 50 | |
| 51 return crypto_key; | |
| 52 } | |
| 53 | |
| 54 std::vector<uint8> HexStringToBytes(const std::string& hex) { | |
| 55 std::vector<uint8> bytes; | |
| 56 base::HexStringToBytes(hex, &bytes); | |
| 57 return bytes; | |
| 58 } | |
| 59 | |
| 60 void ExpectArrayBufferMatchesHex(const std::string& expected_hex, | |
| 61 const WebArrayBuffer& array_buffer) { | |
| 62 EXPECT_STRCASEEQ( | |
| 63 expected_hex.c_str(), | |
| 64 base::HexEncode( | |
| 65 array_buffer.data(), array_buffer.byteLength()).c_str()); | |
| 66 } | |
| 67 | |
| 68 WebCryptoAlgorithm CreateAlgorithm(WebCryptoAlgorithmId id) { | |
| 69 return WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | |
| 70 } | |
| 71 | |
| 72 WebCryptoAlgorithm CreateHmacAlgorithm( | |
| 73 WebCryptoAlgorithmId hashId) { | |
| 74 return WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 75 WebCryptoAlgorithmIdHmac, | |
| 76 new WebCryptoHmacParams(CreateAlgorithm(hashId))); | |
| 77 } | |
| 30 }; | 78 }; |
| 31 | 79 |
| 32 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 80 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
| 33 // The results are stored here in hex format for readability. | 81 // The results are stored here in hex format for readability. |
| 34 // | 82 // |
| 35 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | 83 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced |
| 36 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | 84 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 |
| 37 struct { | 85 // |
| 38 const char* input; | 86 // Results were generated using the command sha{1,224,256,384,512}sum. |
| 39 unsigned input_length; | 87 struct TestCase { |
| 40 const char* hex_result[arraysize(kAlgorithmIds)]; | 88 WebCryptoAlgorithmId algorithm; |
| 41 } input_set[] = { | 89 const std::string hex_input; |
| 42 { | 90 const char* hex_result; |
| 43 "", 0, | 91 }; |
| 44 { | 92 |
| 45 // echo -n "" | sha1sum | 93 const TestCase kTests[] = { |
| 46 "da39a3ee5e6b4b0d3255bfef95601890afd80709", | 94 { WebCryptoAlgorithmIdSha1, "", |
| 47 // echo -n "" | sha224sum | 95 "da39a3ee5e6b4b0d3255bfef95601890afd80709" |
| 48 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
| 49 // echo -n "" | sha256sum | |
| 50 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", | |
| 51 // echo -n "" | sha384sum | |
| 52 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" | |
| 53 "debfe76f65fbd51ad2f14898b95b", | |
| 54 // echo -n "" | sha512sum | |
| 55 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" | |
| 56 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", | |
| 57 }, | |
| 58 }, | 96 }, |
| 59 { | 97 { WebCryptoAlgorithmIdSha224, "", |
| 60 "\000", 1, | 98 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" |
| 61 { | |
| 62 // echo -n -e "\000" | sha1sum | |
| 63 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", | |
| 64 // echo -n -e "\000" | sha224sum | |
| 65 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", | |
| 66 // echo -n -e "\000" | sha256sum | |
| 67 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
| 68 // echo -n -e "\000" | sha384sum | |
| 69 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
| 70 "ec2b413465966817a9c208a11717", | |
| 71 // echo -n -e "\000" | sha512sum | |
| 72 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
| 73 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
| 74 }, | |
| 75 }, | 99 }, |
| 76 { | 100 { WebCryptoAlgorithmIdSha256, "", |
| 77 "\000\001\002\003\004\005", 6, | 101 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 78 { | 102 }, |
| 79 // echo -n -e "\000\001\002\003\004\005" | sha1sum | 103 { WebCryptoAlgorithmIdSha384, "", |
| 80 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | 104 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274e" |
| 81 // echo -n -e "\000\001\002\003\004\005" | sha224sum | 105 "debfe76f65fbd51ad2f14898b95b" |
| 82 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | 106 }, |
| 83 // echo -n -e "\000\001\002\003\004\005" | sha256sum | 107 { WebCryptoAlgorithmIdSha512, "", |
| 84 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | 108 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0" |
| 85 // echo -n -e "\000\001\002\003\004\005" | sha384sum | 109 "d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", |
| 86 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | 110 }, |
| 87 "22780d7e1b95bfeaa86a678e4552", | 111 { WebCryptoAlgorithmIdSha1, "00", |
| 88 // echo -n -e "\000\001\002\003\004\005" | sha512sum | 112 "5ba93c9db0cff93f52b521d7420e43f6eda2784f", |
| 89 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | 113 }, |
| 90 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | 114 { WebCryptoAlgorithmIdSha224, "00", |
| 91 }, | 115 "fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073", |
| 116 }, | |
| 117 { WebCryptoAlgorithmIdSha256, "00", | |
| 118 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | |
| 119 }, | |
| 120 { WebCryptoAlgorithmIdSha384, "00", | |
| 121 "bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933" | |
| 122 "ec2b413465966817a9c208a11717", | |
| 123 }, | |
| 124 { WebCryptoAlgorithmIdSha512, "00", | |
| 125 "b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a" | |
| 126 "802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee", | |
| 127 }, | |
| 128 { WebCryptoAlgorithmIdSha1, "000102030405", | |
| 129 "868460d98d09d8bbb93d7b6cdd15cc7fbec676b9", | |
| 130 }, | |
| 131 { WebCryptoAlgorithmIdSha224, "000102030405", | |
| 132 "7d92e7f1cad1818ed1d13ab41f04ebabfe1fef6bb4cbeebac34c29bc", | |
| 133 }, | |
| 134 { WebCryptoAlgorithmIdSha256, "000102030405", | |
| 135 "17e88db187afd62c16e5debf3e6527cd006bc012bc90b51a810cd80c2d511f43", | |
| 136 }, | |
| 137 { WebCryptoAlgorithmIdSha384, "000102030405", | |
| 138 "79f4738706fce9650ac60266675c3cd07298b09923850d525604d040e6e448adc7dc" | |
| 139 "22780d7e1b95bfeaa86a678e4552", | |
| 140 }, | |
| 141 { WebCryptoAlgorithmIdSha512, "000102030405", | |
| 142 "2f3831bccc94cf061bcfa5f8c23c1429d26e3bc6b76edad93d9025cb91c903af6cf9" | |
| 143 "c935dc37193c04c2c66e7d9de17c358284418218afea2160147aaa912f4c", | |
| 92 }, | 144 }, |
| 93 }; | 145 }; |
| 94 | 146 |
| 95 for (size_t id_index = 0; id_index < arraysize(kAlgorithmIds); id_index++) { | 147 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); |
| 96 SCOPED_TRACE(id_index); | 148 test_index++) { |
| 149 SCOPED_TRACE(test_index); | |
| 150 const TestCase& test = kTests[test_index]; | |
| 97 | 151 |
| 98 WebKit::WebCryptoAlgorithm algorithm( | 152 WebCryptoAlgorithm algorithm = CreateAlgorithm(test.algorithm); |
| 99 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | 153 std::vector<uint8> input = HexStringToBytes(test.hex_input); |
| 100 kAlgorithmIds[id_index], NULL)); | |
| 101 | 154 |
| 102 for (size_t set_index = 0; | 155 WebArrayBuffer output; |
| 103 set_index < ARRAYSIZE_UNSAFE(input_set); | 156 EXPECT_TRUE(DigestInternal(algorithm, input.data(), input.size(), &output)); |
| 104 set_index++) { | 157 ExpectArrayBufferMatchesHex(test.hex_result, output); |
| 105 SCOPED_TRACE(set_index); | |
| 106 | |
| 107 WebKit::WebArrayBuffer array_buffer; | |
| 108 | |
| 109 WebCryptoImpl crypto; | |
| 110 EXPECT_TRUE( | |
| 111 crypto.DigestInternal( | |
| 112 algorithm, | |
| 113 reinterpret_cast<const unsigned char*>( | |
| 114 input_set[set_index].input), | |
| 115 input_set[set_index].input_length, | |
| 116 &array_buffer)); | |
| 117 | |
| 118 // Ignore case, it's checking the hex value. | |
| 119 EXPECT_STRCASEEQ( | |
| 120 input_set[set_index].hex_result[id_index], | |
| 121 base::HexEncode( | |
| 122 array_buffer.data(), array_buffer.byteLength()).c_str()); | |
| 123 } | |
| 124 } | 158 } |
| 125 } | 159 } |
| 126 | 160 |
| 127 TEST_F(WebCryptoImplTest, HMACSampleSets) { | 161 TEST_F(WebCryptoImplTest, HMACSampleSets) { |
| 128 struct { | 162 struct TestCase { |
| 129 WebKit::WebCryptoAlgorithmId algorithm; | 163 WebCryptoAlgorithmId algorithm; |
| 130 const char* key; | 164 const char* key; |
| 131 const char* message; | 165 const char* message; |
| 132 const char* mac; | 166 const char* mac; |
| 133 } input_set[] = { | 167 }; |
| 168 | |
| 169 const TestCase kTests[] = { | |
| 134 // Empty sets. Result generated via OpenSSL commandline tool. These | 170 // Empty sets. Result generated via OpenSSL commandline tool. These |
| 135 // particular results are also posted on the Wikipedia page examples: | 171 // particular results are also posted on the Wikipedia page examples: |
| 136 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code | 172 // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code |
| 137 { | 173 { |
| 138 WebKit::WebCryptoAlgorithmIdSha1, | 174 WebCryptoAlgorithmIdSha1, |
| 139 "", | 175 "", |
| 140 "", | 176 "", |
| 141 // openssl dgst -sha1 -hmac "" < /dev/null | 177 // openssl dgst -sha1 -hmac "" < /dev/null |
| 142 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", | 178 "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", |
| 143 }, | 179 }, |
| 144 { | 180 { |
| 145 WebKit::WebCryptoAlgorithmIdSha256, | 181 WebCryptoAlgorithmIdSha256, |
| 146 "", | 182 "", |
| 147 "", | 183 "", |
| 148 // openssl dgst -sha256 -hmac "" < /dev/null | 184 // openssl dgst -sha256 -hmac "" < /dev/null |
| 149 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", | 185 "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", |
| 150 }, | 186 }, |
| 151 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 | 187 // For this data, see http://csrc.nist.gov/groups/STM/cavp/index.html#07 |
| 152 // Download: | 188 // Download: |
| 153 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip | 189 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/hmactestvectors.zip |
| 154 // L=20 set 45 | 190 // L=20 set 45 |
| 155 { | 191 { |
| 156 WebKit::WebCryptoAlgorithmIdSha1, | 192 WebCryptoAlgorithmIdSha1, |
| 157 // key | 193 // key |
| 158 "59785928d72516e31272", | 194 "59785928d72516e31272", |
| 159 // message | 195 // message |
| 160 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" | 196 "a3ce8899df1022e8d2d539b47bf0e309c66f84095e21438ec355bf119ce5fdcb4e73a6" |
| 161 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" | 197 "19cdf36f25b369d8c38ff419997f0c59830108223606e31223483fd39edeaa4d3f0d21" |
| 162 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" | 198 "198862d239c9fd26074130ff6c86493f5227ab895c8f244bd42c7afce5d147a20a5907" |
| 163 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", | 199 "98c68e708e964902d124dadecdbda9dbd0051ed710e9bf", |
| 164 // mac | 200 // mac |
| 165 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", | 201 "3c8162589aafaee024fc9a5ca50dd2336fe3eb28", |
| 166 }, | 202 }, |
| 167 // L=20 set 299 | 203 // L=20 set 299 |
| 168 { | 204 { |
| 169 WebKit::WebCryptoAlgorithmIdSha1, | 205 WebCryptoAlgorithmIdSha1, |
| 170 // key | 206 // key |
| 171 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" | 207 "ceb9aedf8d6efcf0ae52bea0fa99a9e26ae81bacea0cff4d5eecf201e3bca3c3577480" |
| 172 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" | 208 "621b818fd717ba99d6ff958ea3d59b2527b019c343bb199e648090225867d994607962" |
| 173 "f5866aa62930d75b58f6", | 209 "f5866aa62930d75b58f6", |
| 174 // message | 210 // message |
| 175 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" | 211 "99958aa459604657c7bf6e4cdfcc8785f0abf06ffe636b5b64ecd931bd8a4563055924" |
| 176 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" | 212 "21fc28dbcccb8a82acea2be8e54161d7a78e0399a6067ebaca3f2510274dc9f92f2c8a" |
| 177 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" | 213 "e4265eec13d7d42e9f8612d7bc258f913ecb5a3a5c610339b49fb90e9037b02d684fc6" |
| 178 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", | 214 "0da835657cb24eab352750c8b463b1a8494660d36c3ab2", |
| 179 // mac | 215 // mac |
| 180 "4ac41ab89f625c60125ed65ffa958c6b490ea670", | 216 "4ac41ab89f625c60125ed65ffa958c6b490ea670", |
| 181 }, | 217 }, |
| 182 // L=32, set 30 | 218 // L=32, set 30 |
| 183 { | 219 { |
| 184 WebKit::WebCryptoAlgorithmIdSha256, | 220 WebCryptoAlgorithmIdSha256, |
| 185 // key | 221 // key |
| 186 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" | 222 "9779d9120642797f1747025d5b22b7ac607cab08e1758f2f3a46c8be1e25c53b8c6a8f" |
| 187 "58ffefa176", | 223 "58ffefa176", |
| 188 // message | 224 // message |
| 189 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" | 225 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" |
| 190 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" | 226 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" |
| 191 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" | 227 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" |
| 192 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", | 228 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e", |
| 193 // mac | 229 // mac |
| 194 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", | 230 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b", |
| 195 }, | 231 }, |
| 196 // L=32, set 224 | 232 // L=32, set 224 |
| 197 { | 233 { |
| 198 WebKit::WebCryptoAlgorithmIdSha256, | 234 WebCryptoAlgorithmIdSha256, |
| 199 // key | 235 // key |
| 200 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" | 236 "4b7ab133efe99e02fc89a28409ee187d579e774f4cba6fc223e13504e3511bef8d4f63" |
| 201 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" | 237 "8b9aca55d4a43b8fbd64cf9d74dcc8c9e8d52034898c70264ea911a3fd70813fa73b08" |
| 202 "3371289b", | 238 "3371289b", |
| 203 // message | 239 // message |
| 204 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" | 240 "138efc832c64513d11b9873c6fd4d8a65dbf367092a826ddd587d141b401580b798c69" |
| 205 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" | 241 "025ad510cff05fcfbceb6cf0bb03201aaa32e423d5200925bddfadd418d8e30e18050e" |
| 206 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" | 242 "b4f0618eb9959d9f78c1157d4b3e02cd5961f138afd57459939917d9144c95d8e6a94c" |
| 207 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", | 243 "8f6d4eef3418c17b1ef0b46c2a7188305d9811dccb3d99", |
| 208 // mac | 244 // mac |
| 209 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", | 245 "4f1ee7cb36c58803a8721d4ac8c4cf8cae5d8832392eed2a96dc59694252801b", |
| 210 }, | 246 }, |
| 211 }; | 247 }; |
| 212 | 248 |
| 213 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(input_set); index++) { | 249 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); |
| 214 SCOPED_TRACE(index); | 250 test_index++) { |
| 251 SCOPED_TRACE(test_index); | |
| 252 const TestCase& test = kTests[test_index]; | |
| 215 | 253 |
| 216 WebKit::WebCryptoAlgorithm hash_algorithm( | 254 WebCryptoAlgorithm algorithm = CreateHmacAlgorithm(test.algorithm); |
| 217 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 218 input_set[index].algorithm, NULL)); | |
| 219 | 255 |
| 220 scoped_ptr<WebKit::WebCryptoHmacParams> hmac_params( | 256 WebCryptoKey key = ImportSecretKeyFromRawHexString( |
| 221 new WebKit::WebCryptoHmacParams(hash_algorithm)); | 257 test.key, algorithm, WebCryptoKeyUsageSign); |
| 222 | 258 |
| 223 WebKit::WebCryptoAlgorithm hmac_algorithm( | 259 std::vector<uint8> message_raw = HexStringToBytes(test.message); |
| 224 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( | |
| 225 WebKit::WebCryptoAlgorithmIdHmac, hmac_params.release())); | |
| 226 | 260 |
| 227 WebKit::WebCryptoKeyType type; | 261 WebArrayBuffer output; |
| 228 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
| 229 | 262 |
| 230 std::vector<uint8> key_raw; | 263 EXPECT_TRUE(SignInternal( |
| 231 base::HexStringToBytes(input_set[index].key, &key_raw); | 264 algorithm, key, message_raw.data(), message_raw.size(), &output)); |
| 232 | 265 |
| 233 WebCryptoImpl crypto; | 266 ExpectArrayBufferMatchesHex(test.mac, output); |
| 234 | |
| 235 EXPECT_TRUE( | |
| 236 crypto.ImportKeyInternal( | |
| 237 WebKit::WebCryptoKeyFormatRaw, | |
| 238 key_raw.data(), | |
| 239 key_raw.size(), | |
| 240 hmac_algorithm, | |
| 241 WebKit::WebCryptoKeyUsageSign, | |
| 242 &handle, | |
| 243 &type)); | |
| 244 | |
| 245 EXPECT_EQ(WebKit::WebCryptoKeyTypeSecret, type); | |
| 246 ASSERT_TRUE(handle.get()); | |
| 247 | |
| 248 WebKit::WebCryptoKey crypto_key = | |
| 249 WebKit::WebCryptoKey::create( | |
| 250 handle.release(), | |
| 251 type, | |
| 252 false, | |
| 253 hmac_algorithm, | |
| 254 WebKit::WebCryptoKeyUsageSign); | |
| 255 | |
| 256 std::vector<uint8> message_raw; | |
| 257 base::HexStringToBytes(input_set[index].message, &message_raw); | |
| 258 | |
| 259 WebKit::WebArrayBuffer array_buffer; | |
| 260 | |
| 261 EXPECT_TRUE( | |
| 262 crypto.SignInternal( | |
| 263 hmac_algorithm, | |
| 264 crypto_key, | |
| 265 message_raw.data(), | |
| 266 message_raw.size(), | |
| 267 &array_buffer)); | |
| 268 | |
| 269 // Ignore case, it's checking the hex value. | |
| 270 EXPECT_STRCASEEQ( | |
| 271 input_set[index].mac, | |
| 272 base::HexEncode( | |
| 273 array_buffer.data(), array_buffer.byteLength()).c_str()); | |
| 274 } | 267 } |
| 275 } | 268 } |
| 276 | 269 |
| 277 } // namespace content | 270 } // namespace content |
| OLD | NEW |