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