| 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 "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "content/child/webcrypto/algorithm_dispatch.h" | 7 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 8 #include "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
| 9 #include "content/child/webcrypto/status.h" | 9 #include "content/child/webcrypto/status.h" |
| 10 #include "content/child/webcrypto/test/test_helpers.h" | 10 #include "content/child/webcrypto/test/test_helpers.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST(WebCryptoHmacTest, GenerateKeyIsRandom) { | 116 TEST(WebCryptoHmacTest, GenerateKeyIsRandom) { |
| 117 // Generate a small sample of HMAC keys. | 117 // Generate a small sample of HMAC keys. |
| 118 std::vector<std::vector<uint8_t> > keys; | 118 std::vector<std::vector<uint8_t> > keys; |
| 119 for (int i = 0; i < 16; ++i) { | 119 for (int i = 0; i < 16; ++i) { |
| 120 std::vector<uint8_t> key_bytes; | 120 std::vector<uint8_t> key_bytes; |
| 121 blink::WebCryptoKey key; | 121 blink::WebCryptoKey key; |
| 122 blink::WebCryptoAlgorithm algorithm = | 122 blink::WebCryptoAlgorithm algorithm = |
| 123 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); | 123 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); |
| 124 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 124 ASSERT_EQ( |
| 125 Status::Success(), |
| 126 GenerateSecretKey(algorithm, true, blink::WebCryptoKeyUsageSign, &key)); |
| 125 EXPECT_FALSE(key.isNull()); | 127 EXPECT_FALSE(key.isNull()); |
| 126 EXPECT_TRUE(key.handle()); | 128 EXPECT_TRUE(key.handle()); |
| 127 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 129 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 128 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 130 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 129 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 131 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 130 key.algorithm().hmacParams()->hash().id()); | 132 key.algorithm().hmacParams()->hash().id()); |
| 131 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); | 133 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
| 132 | 134 |
| 133 std::vector<uint8_t> raw_key; | 135 std::vector<uint8_t> raw_key; |
| 134 ASSERT_EQ(Status::Success(), | 136 ASSERT_EQ(Status::Success(), |
| 135 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 137 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 136 EXPECT_EQ(64U, raw_key.size()); | 138 EXPECT_EQ(64U, raw_key.size()); |
| 137 keys.push_back(raw_key); | 139 keys.push_back(raw_key); |
| 138 } | 140 } |
| 139 // Ensure all entries in the key sample set are unique. This is a simplistic | 141 // Ensure all entries in the key sample set are unique. This is a simplistic |
| 140 // estimate of whether the generated keys appear random. | 142 // estimate of whether the generated keys appear random. |
| 141 EXPECT_FALSE(CopiesExist(keys)); | 143 EXPECT_FALSE(CopiesExist(keys)); |
| 142 } | 144 } |
| 143 | 145 |
| 144 // If the key length is not provided, then the block size is used. | 146 // If the key length is not provided, then the block size is used. |
| 145 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { | 147 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { |
| 146 blink::WebCryptoKey key; | 148 blink::WebCryptoKey key; |
| 147 blink::WebCryptoAlgorithm algorithm = | 149 blink::WebCryptoAlgorithm algorithm = |
| 148 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 150 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
| 149 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 151 ASSERT_EQ( |
| 152 Status::Success(), |
| 153 GenerateSecretKey(algorithm, true, blink::WebCryptoKeyUsageSign, &key)); |
| 150 EXPECT_TRUE(key.handle()); | 154 EXPECT_TRUE(key.handle()); |
| 151 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 155 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
| 152 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 156 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 153 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 157 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
| 154 key.algorithm().hmacParams()->hash().id()); | 158 key.algorithm().hmacParams()->hash().id()); |
| 155 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); | 159 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
| 156 std::vector<uint8_t> raw_key; | 160 std::vector<uint8_t> raw_key; |
| 157 ASSERT_EQ(Status::Success(), | 161 ASSERT_EQ(Status::Success(), |
| 158 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 162 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 159 EXPECT_EQ(64U, raw_key.size()); | 163 EXPECT_EQ(64U, raw_key.size()); |
| 160 } | 164 } |
| 161 | 165 |
| 162 // If the key length is not provided, then the block size is used. | 166 // If the key length is not provided, then the block size is used. |
| 163 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { | 167 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { |
| 164 blink::WebCryptoKey key; | 168 blink::WebCryptoKey key; |
| 165 blink::WebCryptoAlgorithm algorithm = | 169 blink::WebCryptoAlgorithm algorithm = |
| 166 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); | 170 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
| 167 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 171 ASSERT_EQ( |
| 172 Status::Success(), |
| 173 GenerateSecretKey(algorithm, true, blink::WebCryptoKeyUsageSign, &key)); |
| 168 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 174 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
| 169 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, | 175 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, |
| 170 key.algorithm().hmacParams()->hash().id()); | 176 key.algorithm().hmacParams()->hash().id()); |
| 171 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); | 177 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); |
| 172 std::vector<uint8_t> raw_key; | 178 std::vector<uint8_t> raw_key; |
| 173 ASSERT_EQ(Status::Success(), | 179 ASSERT_EQ(Status::Success(), |
| 174 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 180 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
| 175 EXPECT_EQ(128U, raw_key.size()); | 181 EXPECT_EQ(128U, raw_key.size()); |
| 176 } | 182 } |
| 177 | 183 |
| 184 TEST(WebCryptoHmacTest, GenerateKeyEmptyUsage) { |
| 185 blink::WebCryptoKey key; |
| 186 blink::WebCryptoAlgorithm algorithm = |
| 187 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
| 188 ASSERT_EQ( |
| 189 Status::ErrorCreateKeyBadUsages(), |
| 190 GenerateSecretKey(algorithm, true, 0, &key)); |
| 191 } |
| 192 |
| 178 TEST(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { | 193 TEST(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { |
| 179 blink::WebCryptoKey key; | 194 blink::WebCryptoKey key; |
| 180 base::DictionaryValue dict; | 195 base::DictionaryValue dict; |
| 181 dict.SetString("kty", "oct"); | 196 dict.SetString("kty", "oct"); |
| 182 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); | 197 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); |
| 183 base::ListValue* key_ops = new base::ListValue; | 198 base::ListValue* key_ops = new base::ListValue; |
| 184 dict.Set("key_ops", key_ops); // Takes ownership. | 199 dict.Set("key_ops", key_ops); // Takes ownership. |
| 185 | 200 |
| 186 key_ops->AppendString("sign"); | 201 key_ops->AppendString("sign"); |
| 187 | 202 |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 true, | 550 true, |
| 536 blink::WebCryptoKeyUsageSign, | 551 blink::WebCryptoKeyUsageSign, |
| 537 &key)); | 552 &key)); |
| 538 } | 553 } |
| 539 | 554 |
| 540 } // namespace | 555 } // namespace |
| 541 | 556 |
| 542 } // namespace webcrypto | 557 } // namespace webcrypto |
| 543 | 558 |
| 544 } // namespace content | 559 } // namespace content |
| OLD | NEW |