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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 103 } |
104 | 104 |
105 TEST(WebCryptoHmacTest, GenerateKeyIsRandom) { | 105 TEST(WebCryptoHmacTest, GenerateKeyIsRandom) { |
106 // Generate a small sample of HMAC keys. | 106 // Generate a small sample of HMAC keys. |
107 std::vector<std::vector<uint8_t>> keys; | 107 std::vector<std::vector<uint8_t>> keys; |
108 for (int i = 0; i < 16; ++i) { | 108 for (int i = 0; i < 16; ++i) { |
109 std::vector<uint8_t> key_bytes; | 109 std::vector<uint8_t> key_bytes; |
110 blink::WebCryptoKey key; | 110 blink::WebCryptoKey key; |
111 blink::WebCryptoAlgorithm algorithm = | 111 blink::WebCryptoAlgorithm algorithm = |
112 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); | 112 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); |
113 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 113 ASSERT_EQ( |
| 114 Status::Success(), |
| 115 GenerateSecretKey(algorithm, true, blink::WebCryptoKeyUsageSign, &key)); |
114 EXPECT_FALSE(key.isNull()); | 116 EXPECT_FALSE(key.isNull()); |
115 EXPECT_TRUE(key.handle()); | 117 EXPECT_TRUE(key.handle()); |
116 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 118 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
117 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 119 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
118 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 120 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
119 key.algorithm().hmacParams()->hash().id()); | 121 key.algorithm().hmacParams()->hash().id()); |
120 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); | 122 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
121 | 123 |
122 std::vector<uint8_t> raw_key; | 124 std::vector<uint8_t> raw_key; |
123 ASSERT_EQ(Status::Success(), | 125 ASSERT_EQ(Status::Success(), |
124 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 126 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
125 EXPECT_EQ(64U, raw_key.size()); | 127 EXPECT_EQ(64U, raw_key.size()); |
126 keys.push_back(raw_key); | 128 keys.push_back(raw_key); |
127 } | 129 } |
128 // Ensure all entries in the key sample set are unique. This is a simplistic | 130 // Ensure all entries in the key sample set are unique. This is a simplistic |
129 // estimate of whether the generated keys appear random. | 131 // estimate of whether the generated keys appear random. |
130 EXPECT_FALSE(CopiesExist(keys)); | 132 EXPECT_FALSE(CopiesExist(keys)); |
131 } | 133 } |
132 | 134 |
133 // If the key length is not provided, then the block size is used. | 135 // If the key length is not provided, then the block size is used. |
134 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { | 136 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { |
135 blink::WebCryptoKey key; | 137 blink::WebCryptoKey key; |
136 blink::WebCryptoAlgorithm algorithm = | 138 blink::WebCryptoAlgorithm algorithm = |
137 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 139 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
138 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 140 ASSERT_EQ( |
| 141 Status::Success(), |
| 142 GenerateSecretKey(algorithm, true, blink::WebCryptoKeyUsageSign, &key)); |
139 EXPECT_TRUE(key.handle()); | 143 EXPECT_TRUE(key.handle()); |
140 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 144 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
141 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 145 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
142 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 146 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
143 key.algorithm().hmacParams()->hash().id()); | 147 key.algorithm().hmacParams()->hash().id()); |
144 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); | 148 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
145 std::vector<uint8_t> raw_key; | 149 std::vector<uint8_t> raw_key; |
146 ASSERT_EQ(Status::Success(), | 150 ASSERT_EQ(Status::Success(), |
147 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 151 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
148 EXPECT_EQ(64U, raw_key.size()); | 152 EXPECT_EQ(64U, raw_key.size()); |
149 } | 153 } |
150 | 154 |
151 // If the key length is not provided, then the block size is used. | 155 // If the key length is not provided, then the block size is used. |
152 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { | 156 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { |
153 blink::WebCryptoKey key; | 157 blink::WebCryptoKey key; |
154 blink::WebCryptoAlgorithm algorithm = | 158 blink::WebCryptoAlgorithm algorithm = |
155 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); | 159 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
156 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 160 ASSERT_EQ( |
| 161 Status::Success(), |
| 162 GenerateSecretKey(algorithm, true, blink::WebCryptoKeyUsageSign, &key)); |
157 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 163 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
158 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, | 164 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, |
159 key.algorithm().hmacParams()->hash().id()); | 165 key.algorithm().hmacParams()->hash().id()); |
160 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); | 166 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); |
161 std::vector<uint8_t> raw_key; | 167 std::vector<uint8_t> raw_key; |
162 ASSERT_EQ(Status::Success(), | 168 ASSERT_EQ(Status::Success(), |
163 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 169 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
164 EXPECT_EQ(128U, raw_key.size()); | 170 EXPECT_EQ(128U, raw_key.size()); |
165 } | 171 } |
166 | 172 |
| 173 TEST(WebCryptoHmacTest, GenerateKeyEmptyUsage) { |
| 174 blink::WebCryptoKey key; |
| 175 blink::WebCryptoAlgorithm algorithm = |
| 176 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
| 177 ASSERT_EQ(Status::ErrorCreateKeyEmptyUsages(), |
| 178 GenerateSecretKey(algorithm, true, 0, &key)); |
| 179 } |
| 180 |
167 TEST(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { | 181 TEST(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { |
168 blink::WebCryptoKey key; | 182 blink::WebCryptoKey key; |
169 base::DictionaryValue dict; | 183 base::DictionaryValue dict; |
170 dict.SetString("kty", "oct"); | 184 dict.SetString("kty", "oct"); |
171 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); | 185 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); |
172 base::ListValue* key_ops = new base::ListValue; | 186 base::ListValue* key_ops = new base::ListValue; |
173 dict.Set("key_ops", key_ops); // Takes ownership. | 187 dict.Set("key_ops", key_ops); // Takes ownership. |
174 | 188 |
175 key_ops->AppendString("sign"); | 189 key_ops->AppendString("sign"); |
176 | 190 |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(big_data), | 478 ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(big_data), |
465 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), | 479 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), |
466 true, blink::WebCryptoKeyUsageSign, &key)); | 480 true, blink::WebCryptoKeyUsageSign, &key)); |
467 } | 481 } |
468 | 482 |
469 } // namespace | 483 } // namespace |
470 | 484 |
471 } // namespace webcrypto | 485 } // namespace webcrypto |
472 | 486 |
473 } // namespace content | 487 } // namespace content |
OLD | NEW |