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 = blink::WebCryptoKey::createNull(); | 121 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
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(Status::Success(), GenerateKey(algorithm, true, 0, NULL, &key)); |
125 EXPECT_FALSE(key.isNull()); | 125 EXPECT_FALSE(key.isNull()); |
126 EXPECT_TRUE(key.handle()); | 126 EXPECT_TRUE(key.handle()); |
127 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 127 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
128 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 128 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
129 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 129 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
130 key.algorithm().hmacParams()->hash().id()); | 130 key.algorithm().hmacParams()->hash().id()); |
131 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); | 131 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
132 | 132 |
133 std::vector<uint8_t> raw_key; | 133 std::vector<uint8_t> raw_key; |
134 ASSERT_EQ(Status::Success(), | 134 ASSERT_EQ(Status::Success(), |
135 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 135 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
136 EXPECT_EQ(64U, raw_key.size()); | 136 EXPECT_EQ(64U, raw_key.size()); |
137 keys.push_back(raw_key); | 137 keys.push_back(raw_key); |
138 } | 138 } |
139 // 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 |
140 // estimate of whether the generated keys appear random. | 140 // estimate of whether the generated keys appear random. |
141 EXPECT_FALSE(CopiesExist(keys)); | 141 EXPECT_FALSE(CopiesExist(keys)); |
142 } | 142 } |
143 | 143 |
144 // 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. |
145 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { | 145 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha1) { |
146 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 146 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
147 blink::WebCryptoAlgorithm algorithm = | 147 blink::WebCryptoAlgorithm algorithm = |
148 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); | 148 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); |
149 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 149 ASSERT_EQ(Status::Success(), GenerateKey(algorithm, true, 0, NULL, &key)); |
150 EXPECT_TRUE(key.handle()); | 150 EXPECT_TRUE(key.handle()); |
151 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | 151 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); |
152 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 152 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
153 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, | 153 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, |
154 key.algorithm().hmacParams()->hash().id()); | 154 key.algorithm().hmacParams()->hash().id()); |
155 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); | 155 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); |
156 std::vector<uint8_t> raw_key; | 156 std::vector<uint8_t> raw_key; |
157 ASSERT_EQ(Status::Success(), | 157 ASSERT_EQ(Status::Success(), |
158 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 158 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
159 EXPECT_EQ(64U, raw_key.size()); | 159 EXPECT_EQ(64U, raw_key.size()); |
160 } | 160 } |
161 | 161 |
162 // 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. |
163 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { | 163 TEST(WebCryptoHmacTest, GenerateKeyNoLengthSha512) { |
164 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 164 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
165 blink::WebCryptoAlgorithm algorithm = | 165 blink::WebCryptoAlgorithm algorithm = |
166 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); | 166 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); |
167 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); | 167 ASSERT_EQ(Status::Success(), GenerateKey(algorithm, true, 0, NULL, &key)); |
168 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); | 168 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); |
169 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, | 169 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, |
170 key.algorithm().hmacParams()->hash().id()); | 170 key.algorithm().hmacParams()->hash().id()); |
171 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); | 171 EXPECT_EQ(1024u, key.algorithm().hmacParams()->lengthBits()); |
172 std::vector<uint8_t> raw_key; | 172 std::vector<uint8_t> raw_key; |
173 ASSERT_EQ(Status::Success(), | 173 ASSERT_EQ(Status::Success(), |
174 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); | 174 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); |
175 EXPECT_EQ(128U, raw_key.size()); | 175 EXPECT_EQ(128U, raw_key.size()); |
176 } | 176 } |
177 | 177 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 true, | 536 true, |
537 blink::WebCryptoKeyUsageSign, | 537 blink::WebCryptoKeyUsageSign, |
538 &key)); | 538 &key)); |
539 } | 539 } |
540 | 540 |
541 } // namespace | 541 } // namespace |
542 | 542 |
543 } // namespace webcrypto | 543 } // namespace webcrypto |
544 | 544 |
545 } // namespace content | 545 } // namespace content |
OLD | NEW |