Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: content/renderer/webcrypto/shared_crypto_unittest.cc

Issue 155623005: Refactor to share more code between OpenSSL and NSS implementations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change header guard Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "content/renderer/webcrypto/webcrypto_impl.h" 5 #include "content/renderer/webcrypto/shared_crypto.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "content/public/common/content_paths.h" 20 #include "content/public/common/content_paths.h"
21 #include "content/public/renderer/content_renderer_client.h" 21 #include "content/public/renderer/content_renderer_client.h"
22 #include "content/renderer/renderer_webkitplatformsupport_impl.h" 22 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
23 #include "content/renderer/webcrypto/crypto_data.h"
23 #include "content/renderer/webcrypto/webcrypto_util.h" 24 #include "content/renderer/webcrypto/webcrypto_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" 26 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
26 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" 27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
27 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 28 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
28 #include "third_party/WebKit/public/platform/WebCryptoKey.h" 29 #include "third_party/WebKit/public/platform/WebCryptoKey.h"
29 #include "third_party/re2/re2/re2.h" 30 #include "third_party/re2/re2/re2.h"
30 31
31 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of 32 // The OpenSSL implementation of WebCrypto is less complete, so don't run all of
32 // the tests: http://crbug.com/267888 33 // the tests: http://crbug.com/267888
33 #if defined(USE_OPENSSL) 34 #if defined(USE_OPENSSL)
34 #define MAYBE(test_name) DISABLED_##test_name 35 #define MAYBE(test_name) DISABLED_##test_name
35 #else 36 #else
36 #define MAYBE(test_name) test_name 37 #define MAYBE(test_name) test_name
37 #endif 38 #endif
38 39
39 // Helper macros to verify the value of a Status. 40 // Helper macros to verify the value of a Status.
40 #define EXPECT_STATUS_ERROR(code) EXPECT_FALSE((code).IsSuccess()) 41 #define EXPECT_STATUS_ERROR(code) EXPECT_FALSE((code).IsSuccess())
41 #define EXPECT_STATUS(expected, code) \ 42 #define EXPECT_STATUS(expected, code) \
42 EXPECT_EQ(expected.ToString(), (code).ToString()) 43 EXPECT_EQ(expected.ToString(), (code).ToString())
43 #define ASSERT_STATUS(expected, code) \ 44 #define ASSERT_STATUS(expected, code) \
44 ASSERT_EQ(expected.ToString(), (code).ToString()) 45 ASSERT_EQ(expected.ToString(), (code).ToString())
45 #define EXPECT_STATUS_SUCCESS(code) EXPECT_STATUS(Status::Success(), code) 46 #define EXPECT_STATUS_SUCCESS(code) EXPECT_STATUS(Status::Success(), code)
46 #define ASSERT_STATUS_SUCCESS(code) ASSERT_STATUS(Status::Success(), code) 47 #define ASSERT_STATUS_SUCCESS(code) ASSERT_STATUS(Status::Success(), code)
47 48
48 namespace content { 49 namespace content {
49 50
50 using webcrypto::Status; 51 namespace webcrypto {
51 52
52 namespace { 53 namespace {
53 54
54 // Returns a slightly modified version of the input vector. 55 // Returns a slightly modified version of the input vector.
55 // 56 //
56 // - For non-empty inputs a single bit is inverted. 57 // - For non-empty inputs a single bit is inverted.
57 // - For empty inputs, a byte is added. 58 // - For empty inputs, a byte is added.
58 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { 59 std::vector<uint8> Corrupted(const std::vector<uint8>& input) {
59 std::vector<uint8> corrupted_data(input); 60 std::vector<uint8> corrupted_data(input);
60 if (corrupted_data.empty()) 61 if (corrupted_data.empty())
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } kDigestNameToId[] = { 192 } kDigestNameToId[] = {
192 {"sha-1", blink::WebCryptoAlgorithmIdSha1}, 193 {"sha-1", blink::WebCryptoAlgorithmIdSha1},
193 {"sha-224", blink::WebCryptoAlgorithmIdSha224}, 194 {"sha-224", blink::WebCryptoAlgorithmIdSha224},
194 {"sha-256", blink::WebCryptoAlgorithmIdSha256}, 195 {"sha-256", blink::WebCryptoAlgorithmIdSha256},
195 {"sha-384", blink::WebCryptoAlgorithmIdSha384}, 196 {"sha-384", blink::WebCryptoAlgorithmIdSha384},
196 {"sha-512", blink::WebCryptoAlgorithmIdSha512}, 197 {"sha-512", blink::WebCryptoAlgorithmIdSha512},
197 }; 198 };
198 199
199 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) { 200 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDigestNameToId); ++i) {
200 if (kDigestNameToId[i].name == algorithm_name) 201 if (kDigestNameToId[i].name == algorithm_name)
201 return webcrypto::CreateAlgorithm(kDigestNameToId[i].id); 202 return CreateAlgorithm(kDigestNameToId[i].id);
202 } 203 }
203 204
204 return blink::WebCryptoAlgorithm::createNull(); 205 return blink::WebCryptoAlgorithm::createNull();
205 } 206 }
206 207
207 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON 208 // Helper for ImportJwkFailures and ImportJwkOctFailures. Restores the JWK JSON
208 // dictionary to a good state 209 // dictionary to a good state
209 void RestoreJwkOctDictionary(base::DictionaryValue* dict) { 210 void RestoreJwkOctDictionary(base::DictionaryValue* dict) {
210 dict->Clear(); 211 dict->Clear();
211 dict->SetString("kty", "oct"); 212 dict->SetString("kty", "oct");
212 dict->SetString("alg", "A128CBC"); 213 dict->SetString("alg", "A128CBC");
213 dict->SetString("use", "enc"); 214 dict->SetString("use", "enc");
214 dict->SetBoolean("extractable", false); 215 dict->SetBoolean("extractable", false);
215 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg=="); 216 dict->SetString("k", "GADWrMRHwQfoNaXU5fZvTg==");
216 } 217 }
217 218
218 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( 219 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
219 const std::vector<uint8>& iv, 220 const std::vector<uint8>& iv,
220 const std::vector<uint8>& additional_data, 221 const std::vector<uint8>& additional_data,
221 unsigned int tag_length_bits) { 222 unsigned int tag_length_bits) {
222 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 223 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
223 blink::WebCryptoAlgorithmIdAesGcm, 224 blink::WebCryptoAlgorithmIdAesGcm,
224 new blink::WebCryptoAesGcmParams( 225 new blink::WebCryptoAesGcmParams(
225 webcrypto::Uint8VectorStart(iv), iv.size(), 226 Uint8VectorStart(iv), iv.size(),
226 true, 227 true,
227 webcrypto::Uint8VectorStart(additional_data), 228 Uint8VectorStart(additional_data),
228 additional_data.size(), 229 additional_data.size(),
229 true, tag_length_bits)); 230 true, tag_length_bits));
230 } 231 }
231 232
232 // Helper for ImportJwkRsaFailures. Restores the JWK JSON 233 // Helper for ImportJwkRsaFailures. Restores the JWK JSON
233 // dictionary to a good state 234 // dictionary to a good state
234 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) { 235 void RestoreJwkRsaDictionary(base::DictionaryValue* dict) {
235 dict->Clear(); 236 dict->Clear();
236 dict->SetString("kty", "RSA"); 237 dict->SetString("kty", "RSA");
237 dict->SetString("alg", "RSA1_5"); 238 dict->SetString("alg", "RSA1_5");
238 dict->SetString("use", "enc"); 239 dict->SetString("use", "enc");
239 dict->SetBoolean("extractable", false); 240 dict->SetBoolean("extractable", false);
240 dict->SetString("n", 241 dict->SetString("n",
241 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" 242 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk"
242 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" 243 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm"
243 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); 244 "e7PUJHYW1PW6ENTP0ibeiNOfFvs");
244 dict->SetString("e", "AQAB"); 245 dict->SetString("e", "AQAB");
245 } 246 }
246 247
247 blink::WebCryptoAlgorithm CreateRsaAlgorithmWithInnerHash( 248 blink::WebCryptoAlgorithm CreateRsaAlgorithmWithInnerHash(
248 blink::WebCryptoAlgorithmId algorithm_id, 249 blink::WebCryptoAlgorithmId algorithm_id,
249 blink::WebCryptoAlgorithmId hash_id) { 250 blink::WebCryptoAlgorithmId hash_id) {
250 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || 251 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
251 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); 252 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
252 DCHECK(webcrypto::IsHashAlgorithm(hash_id)); 253 DCHECK(IsHashAlgorithm(hash_id));
253 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 254 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
254 algorithm_id, 255 algorithm_id,
255 new blink::WebCryptoRsaSsaParams(webcrypto::CreateAlgorithm(hash_id))); 256 new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id)));
256 } 257 }
257 258
258 // Determines if two ArrayBuffers have identical content. 259 // Determines if two ArrayBuffers have identical content.
259 bool ArrayBuffersEqual( 260 bool ArrayBuffersEqual(
260 const blink::WebArrayBuffer& a, 261 const blink::WebArrayBuffer& a,
261 const blink::WebArrayBuffer& b) { 262 const blink::WebArrayBuffer& b) {
262 return a.byteLength() == b.byteLength() && 263 return a.byteLength() == b.byteLength() &&
263 memcmp(a.data(), b.data(), a.byteLength()) == 0; 264 memcmp(a.data(), b.data(), a.byteLength()) == 0;
264 } 265 }
265 266
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72" 329 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72"
329 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca" 330 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca"
330 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d" 331 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d"
331 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71" 332 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71"
332 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd" 333 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd"
333 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027" 334 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027"
334 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319" 335 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319"
335 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" 336 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24"
336 "a79f4d"; 337 "a79f4d";
337 338
338 } // namespace 339 class SharedCryptoTest : public testing::Test {
340 protected:
341 virtual void SetUp() OVERRIDE {
342 Init();
343 }
344 };
339 345
340 class WebCryptoImplTest : public testing::Test { 346 // Wrappers to pass vector<> in place of CryptoData.
341 protected: 347
348 Status ImportKeyInternal(blink::WebCryptoKeyFormat format,
349 const std::vector<uint8>& key_data,
350 const blink::WebCryptoAlgorithm& algorithm,
351 bool extractable,
352 blink::WebCryptoKeyUsageMask usage_mask,
353 blink::WebCryptoKey* key) {
354 return ImportKey(format, CryptoData(key_data), algorithm, extractable,
355 usage_mask, key);
356 }
357
358 Status EncryptInternal(const blink::WebCryptoAlgorithm& algorithm,
359 const blink::WebCryptoKey& key,
360 const std::vector<uint8>& data,
361 blink::WebArrayBuffer* buffer) {
362 return Encrypt(algorithm, key, CryptoData(data), buffer);
363 }
364
365 Status DecryptInternal(const blink::WebCryptoAlgorithm& algorithm,
366 const blink::WebCryptoKey& key,
367 const std::vector<uint8>& data,
368 blink::WebArrayBuffer* buffer) {
369 return Decrypt(algorithm, key, CryptoData(data), buffer);
370 }
371
372
373 // TODO: de-indent.
342 blink::WebCryptoKey ImportSecretKeyFromRaw( 374 blink::WebCryptoKey ImportSecretKeyFromRaw(
343 const std::vector<uint8>& key_raw, 375 const std::vector<uint8>& key_raw,
344 const blink::WebCryptoAlgorithm& algorithm, 376 const blink::WebCryptoAlgorithm& algorithm,
345 blink::WebCryptoKeyUsageMask usage) { 377 blink::WebCryptoKeyUsageMask usage) {
346 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 378 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
347 bool extractable = true; 379 bool extractable = true;
348 EXPECT_STATUS_SUCCESS( 380 EXPECT_STATUS_SUCCESS(
349 crypto_.ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 381 ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
350 webcrypto::Uint8VectorStart(key_raw), 382 key_raw,
351 key_raw.size(), 383 algorithm,
352 algorithm, 384 extractable,
353 extractable, 385 usage,
354 usage, 386 &key));
355 &key));
356 387
357 EXPECT_FALSE(key.isNull()); 388 EXPECT_FALSE(key.isNull());
358 EXPECT_TRUE(key.handle()); 389 EXPECT_TRUE(key.handle());
359 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 390 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
360 EXPECT_EQ(algorithm.id(), key.algorithm().id()); 391 EXPECT_EQ(algorithm.id(), key.algorithm().id());
361 EXPECT_EQ(extractable, key.extractable()); 392 EXPECT_EQ(extractable, key.extractable());
362 EXPECT_EQ(usage, key.usages()); 393 EXPECT_EQ(usage, key.usages());
363 return key; 394 return key;
364 } 395 }
365 396
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 EXPECT_EQ(extractable, extractable); 430 EXPECT_EQ(extractable, extractable);
400 EXPECT_EQ(usage_mask, private_key->usages()); 431 EXPECT_EQ(usage_mask, private_key->usages());
401 } 432 }
402 433
403 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a 434 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a
404 // runtime dependency. Test it by trying to import a key. 435 // runtime dependency. Test it by trying to import a key.
405 bool SupportsAesGcm() { 436 bool SupportsAesGcm() {
406 std::vector<uint8> key_raw(16, 0); 437 std::vector<uint8> key_raw(16, 0);
407 438
408 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 439 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
409 Status status = crypto_.ImportKeyInternal( 440 Status status = ImportKeyInternal(
410 blink::WebCryptoKeyFormatRaw, 441 blink::WebCryptoKeyFormatRaw,
411 webcrypto::Uint8VectorStart(key_raw), 442 key_raw,
412 key_raw.size(), 443 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
413 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
414 true, 444 true,
415 blink::WebCryptoKeyUsageEncrypt, 445 blink::WebCryptoKeyUsageEncrypt,
416 &key); 446 &key);
417 447
418 if (status.IsError()) 448 if (status.IsError())
419 EXPECT_EQ(Status::ErrorUnsupported().ToString(), status.ToString()); 449 EXPECT_EQ(Status::ErrorUnsupported().ToString(), status.ToString());
420 return status.IsSuccess(); 450 return status.IsSuccess();
421
422 } 451 }
423 452
424 Status AesGcmEncrypt(const blink::WebCryptoKey& key, 453 Status AesGcmEncrypt(const blink::WebCryptoKey& key,
425 const std::vector<uint8>& iv, 454 const std::vector<uint8>& iv,
426 const std::vector<uint8>& additional_data, 455 const std::vector<uint8>& additional_data,
427 unsigned int tag_length_bits, 456 unsigned int tag_length_bits,
428 const std::vector<uint8>& plain_text, 457 const std::vector<uint8>& plain_text,
429 std::vector<uint8>* cipher_text, 458 std::vector<uint8>* cipher_text,
430 std::vector<uint8>* authentication_tag) { 459 std::vector<uint8>* authentication_tag) {
431 blink::WebCryptoAlgorithm algorithm = CreateAesGcmAlgorithm( 460 blink::WebCryptoAlgorithm algorithm = CreateAesGcmAlgorithm(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 cipher_text.size() + authentication_tag.size()); 498 cipher_text.size() + authentication_tag.size());
470 cipher_text_with_tag.insert( 499 cipher_text_with_tag.insert(
471 cipher_text_with_tag.end(), cipher_text.begin(), cipher_text.end()); 500 cipher_text_with_tag.end(), cipher_text.begin(), cipher_text.end());
472 cipher_text_with_tag.insert( 501 cipher_text_with_tag.insert(
473 cipher_text_with_tag.end(), authentication_tag.begin(), 502 cipher_text_with_tag.end(), authentication_tag.begin(),
474 authentication_tag.end()); 503 authentication_tag.end());
475 504
476 return DecryptInternal(algorithm, key, cipher_text_with_tag, plain_text); 505 return DecryptInternal(algorithm, key, cipher_text_with_tag, plain_text);
477 } 506 }
478 507
479 // Forwarding methods to gain access to protected methods of 508 // Helpers to pass vector<> in place of CryptoData.
480 // WebCryptoImpl.
481
482 Status DigestInternal( 509 Status DigestInternal(
483 const blink::WebCryptoAlgorithm& algorithm, 510 const blink::WebCryptoAlgorithm& algorithm,
484 const std::vector<uint8>& data, 511 const std::vector<uint8>& data,
485 blink::WebArrayBuffer* buffer) { 512 blink::WebArrayBuffer* buffer) {
486 return crypto_.DigestInternal( 513 return Digest(algorithm, CryptoData(data), buffer);
487 algorithm, webcrypto::Uint8VectorStart(data), data.size(), buffer);
488 }
489
490 Status GenerateKeyInternal(
491 const blink::WebCryptoAlgorithm& algorithm,
492 blink::WebCryptoKey* key) {
493 bool extractable = true;
494 blink::WebCryptoKeyUsageMask usage_mask = 0;
495 return crypto_.GenerateSecretKeyInternal(
496 algorithm, extractable, usage_mask, key);
497 }
498
499 Status GenerateKeyPairInternal(
500 const blink::WebCryptoAlgorithm& algorithm,
501 bool extractable,
502 blink::WebCryptoKeyUsageMask usage_mask,
503 blink::WebCryptoKey* public_key,
504 blink::WebCryptoKey* private_key) {
505 return crypto_.GenerateKeyPairInternal(
506 algorithm, extractable, usage_mask, public_key, private_key);
507 }
508
509 Status ImportKeyInternal(
510 blink::WebCryptoKeyFormat format,
511 const std::vector<uint8>& key_data,
512 const blink::WebCryptoAlgorithm& algorithm,
513 bool extractable,
514 blink::WebCryptoKeyUsageMask usage_mask,
515 blink::WebCryptoKey* key) {
516 return crypto_.ImportKeyInternal(format,
517 webcrypto::Uint8VectorStart(key_data),
518 key_data.size(),
519 algorithm,
520 extractable,
521 usage_mask,
522 key);
523 }
524
525 Status ExportKeyInternal(
526 blink::WebCryptoKeyFormat format,
527 const blink::WebCryptoKey& key,
528 blink::WebArrayBuffer* buffer) {
529 return crypto_.ExportKeyInternal(format, key, buffer);
530 } 514 }
531 515
532 Status SignInternal( 516 Status SignInternal(
533 const blink::WebCryptoAlgorithm& algorithm, 517 const blink::WebCryptoAlgorithm& algorithm,
534 const blink::WebCryptoKey& key, 518 const blink::WebCryptoKey& key,
535 const std::vector<uint8>& data, 519 const std::vector<uint8>& data,
536 blink::WebArrayBuffer* buffer) { 520 blink::WebArrayBuffer* buffer) {
537 return crypto_.SignInternal( 521 return Sign(algorithm, key, CryptoData(data), buffer);
538 algorithm, key, webcrypto::Uint8VectorStart(data), data.size(), buffer);
539 } 522 }
540 523
541 Status VerifySignatureInternal( 524 Status VerifySignatureInternal(
542 const blink::WebCryptoAlgorithm& algorithm, 525 const blink::WebCryptoAlgorithm& algorithm,
543 const blink::WebCryptoKey& key, 526 const blink::WebCryptoKey& key,
544 const unsigned char* signature, 527 const CryptoData& signature,
545 unsigned int signature_size,
546 const std::vector<uint8>& data, 528 const std::vector<uint8>& data,
547 bool* signature_match) { 529 bool* signature_match) {
548 return crypto_.VerifySignatureInternal(algorithm, 530 return VerifySignature(algorithm,
549 key, 531 key,
550 signature, 532 signature,
551 signature_size, 533 CryptoData(data),
552 webcrypto::Uint8VectorStart(data), 534 signature_match);
553 data.size(),
554 signature_match);
555 } 535 }
556 536
537
557 Status VerifySignatureInternal( 538 Status VerifySignatureInternal(
558 const blink::WebCryptoAlgorithm& algorithm, 539 const blink::WebCryptoAlgorithm& algorithm,
559 const blink::WebCryptoKey& key, 540 const blink::WebCryptoKey& key,
560 const std::vector<uint8>& signature, 541 const std::vector<uint8>& signature,
561 const std::vector<uint8>& data, 542 const std::vector<uint8>& data,
562 bool* signature_match) { 543 bool* signature_match) {
563 return crypto_.VerifySignatureInternal( 544 return VerifySignature(
564 algorithm, 545 algorithm,
565 key, 546 key,
566 webcrypto::Uint8VectorStart(signature), 547 CryptoData(signature),
567 signature.size(), 548 CryptoData(data),
568 webcrypto::Uint8VectorStart(data),
569 data.size(),
570 signature_match); 549 signature_match);
571 } 550 }
572 551
573 Status EncryptInternal(
574 const blink::WebCryptoAlgorithm& algorithm,
575 const blink::WebCryptoKey& key,
576 const unsigned char* data,
577 unsigned int data_size,
578 blink::WebArrayBuffer* buffer) {
579 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer);
580 }
581
582 Status EncryptInternal(
583 const blink::WebCryptoAlgorithm& algorithm,
584 const blink::WebCryptoKey& key,
585 const std::vector<uint8>& data,
586 blink::WebArrayBuffer* buffer) {
587 return crypto_.EncryptInternal(
588 algorithm, key, webcrypto::Uint8VectorStart(data), data.size(), buffer);
589 }
590
591 Status DecryptInternal(
592 const blink::WebCryptoAlgorithm& algorithm,
593 const blink::WebCryptoKey& key,
594 const unsigned char* data,
595 unsigned int data_size,
596 blink::WebArrayBuffer* buffer) {
597 return crypto_.DecryptInternal(algorithm, key, data, data_size, buffer);
598 }
599
600 Status DecryptInternal(
601 const blink::WebCryptoAlgorithm& algorithm,
602 const blink::WebCryptoKey& key,
603 const std::vector<uint8>& data,
604 blink::WebArrayBuffer* buffer) {
605 return crypto_.DecryptInternal(
606 algorithm, key, webcrypto::Uint8VectorStart(data), data.size(), buffer);
607 }
608
609 Status ImportKeyJwk( 552 Status ImportKeyJwk(
610 const std::vector<uint8>& key_data, 553 const std::vector<uint8>& key_data,
611 const blink::WebCryptoAlgorithm& algorithm, 554 const blink::WebCryptoAlgorithm& algorithm,
612 bool extractable, 555 bool extractable,
613 blink::WebCryptoKeyUsageMask usage_mask, 556 blink::WebCryptoKeyUsageMask usage_mask,
614 blink::WebCryptoKey* key) { 557 blink::WebCryptoKey* key) {
615 return crypto_.ImportKeyJwk(webcrypto::Uint8VectorStart(key_data), 558 return ImportKeyJwk(CryptoData(key_data),
616 key_data.size(), 559 algorithm,
617 algorithm, 560 extractable,
618 extractable, 561 usage_mask,
619 usage_mask, 562 key);
620 key);
621 } 563 }
622 564
623 private: 565 } // namespace
624 WebCryptoImpl crypto_;
625 };
626 566
627 TEST_F(WebCryptoImplTest, StatusToString) { 567 TEST_F(SharedCryptoTest, StatusToString) {
628 EXPECT_EQ("Success", Status::Success().ToString()); 568 EXPECT_EQ("Success", Status::Success().ToString());
629 EXPECT_EQ("", Status::Error().ToString()); 569 EXPECT_EQ("", Status::Error().ToString());
630 EXPECT_EQ("The requested operation is unsupported", 570 EXPECT_EQ("The requested operation is unsupported",
631 Status::ErrorUnsupported().ToString()); 571 Status::ErrorUnsupported().ToString());
632 EXPECT_EQ("The required JWK property \"kty\" was missing", 572 EXPECT_EQ("The required JWK property \"kty\" was missing",
633 Status::ErrorJwkPropertyMissing("kty").ToString()); 573 Status::ErrorJwkPropertyMissing("kty").ToString());
634 EXPECT_EQ("The JWK property \"kty\" must be a string", 574 EXPECT_EQ("The JWK property \"kty\" must be a string",
635 Status::ErrorJwkPropertyWrongType("kty", "string").ToString()); 575 Status::ErrorJwkPropertyWrongType("kty", "string").ToString());
636 EXPECT_EQ("The JWK property \"n\" could not be base64 decoded", 576 EXPECT_EQ("The JWK property \"n\" could not be base64 decoded",
637 Status::ErrorJwkBase64Decode("n").ToString()); 577 Status::ErrorJwkBase64Decode("n").ToString());
638 } 578 }
639 579
640 TEST_F(WebCryptoImplTest, DigestSampleSets) { 580 TEST_F(SharedCryptoTest, DigestSampleSets) {
641 scoped_ptr<base::ListValue> tests; 581 scoped_ptr<base::ListValue> tests;
642 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); 582 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests));
643 583
644 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 584 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
645 SCOPED_TRACE(test_index); 585 SCOPED_TRACE(test_index);
646 base::DictionaryValue* test; 586 base::DictionaryValue* test;
647 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 587 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
648 588
649 blink::WebCryptoAlgorithm test_algorithm = 589 blink::WebCryptoAlgorithm test_algorithm =
650 GetDigestAlgorithm(test, "algorithm"); 590 GetDigestAlgorithm(test, "algorithm");
651 std::vector<uint8> test_input = GetBytesFromHexString(test, "input"); 591 std::vector<uint8> test_input = GetBytesFromHexString(test, "input");
652 std::vector<uint8> test_output = GetBytesFromHexString(test, "output"); 592 std::vector<uint8> test_output = GetBytesFromHexString(test, "output");
653 593
654 blink::WebArrayBuffer output; 594 blink::WebArrayBuffer output;
655 ASSERT_STATUS_SUCCESS(DigestInternal(test_algorithm, test_input, &output)); 595 ASSERT_STATUS_SUCCESS(DigestInternal(test_algorithm, test_input, &output));
656 ExpectArrayBufferMatches(test_output, output); 596 ExpectArrayBufferMatches(test_output, output);
657 } 597 }
658 } 598 }
659 599
660 TEST_F(WebCryptoImplTest, HMACSampleSets) { 600 TEST_F(SharedCryptoTest, HMACSampleSets) {
661 scoped_ptr<base::ListValue> tests; 601 scoped_ptr<base::ListValue> tests;
662 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); 602 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests));
663 603
664 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 604 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
665 SCOPED_TRACE(test_index); 605 SCOPED_TRACE(test_index);
666 base::DictionaryValue* test; 606 base::DictionaryValue* test;
667 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 607 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
668 608
669 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); 609 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash");
670 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 610 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
671 const std::vector<uint8> test_message = 611 const std::vector<uint8> test_message =
672 GetBytesFromHexString(test, "message"); 612 GetBytesFromHexString(test, "message");
673 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac"); 613 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac");
674 614
675 blink::WebCryptoAlgorithm algorithm = 615 blink::WebCryptoAlgorithm algorithm =
676 webcrypto::CreateHmacAlgorithmByHashId(test_hash.id()); 616 CreateHmacAlgorithmByHashId(test_hash.id());
677 617
678 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 618 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
679 test_key, algorithm, blink::WebCryptoKeyUsageSign); 619 test_key,
620 algorithm,
621 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify);
680 622
681 // Verify exported raw key is identical to the imported data 623 // Verify exported raw key is identical to the imported data
682 blink::WebArrayBuffer raw_key; 624 blink::WebArrayBuffer raw_key;
683 EXPECT_STATUS_SUCCESS( 625 EXPECT_STATUS_SUCCESS(
684 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 626 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
685 ExpectArrayBufferMatches(test_key, raw_key); 627 ExpectArrayBufferMatches(test_key, raw_key);
686 628
687 blink::WebArrayBuffer output; 629 blink::WebArrayBuffer output;
688 630
689 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, test_message, &output)); 631 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, test_message, &output));
690 632
691 ExpectArrayBufferMatches(test_mac, output); 633 ExpectArrayBufferMatches(test_mac, output);
692 634
693 bool signature_match = false; 635 bool signature_match = false;
694 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 636 EXPECT_STATUS_SUCCESS(VerifySignature(
695 algorithm, 637 algorithm,
696 key, 638 key,
697 static_cast<const unsigned char*>(output.data()), 639 CryptoData(output),
698 output.byteLength(), 640 CryptoData(test_message),
699 test_message,
700 &signature_match)); 641 &signature_match));
701 EXPECT_TRUE(signature_match); 642 EXPECT_TRUE(signature_match);
702 643
703 // Ensure truncated signature does not verify by passing one less byte. 644 // Ensure truncated signature does not verify by passing one less byte.
704 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 645 EXPECT_STATUS_SUCCESS(VerifySignature(
705 algorithm, 646 algorithm,
706 key, 647 key,
707 static_cast<const unsigned char*>(output.data()), 648 CryptoData(static_cast<const unsigned char*>(output.data()),
708 output.byteLength() - 1, 649 output.byteLength() - 1),
709 test_message, 650 CryptoData(test_message),
710 &signature_match)); 651 &signature_match));
711 EXPECT_FALSE(signature_match); 652 EXPECT_FALSE(signature_match);
712 653
713 // Ensure truncated signature does not verify by passing no bytes. 654 // Ensure truncated signature does not verify by passing no bytes.
714 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 655 EXPECT_STATUS_SUCCESS(VerifySignature(
715 algorithm, 656 algorithm,
716 key, 657 key,
717 NULL, 658 CryptoData(),
718 0, 659 CryptoData(test_message),
719 test_message,
720 &signature_match)); 660 &signature_match));
721 EXPECT_FALSE(signature_match); 661 EXPECT_FALSE(signature_match);
722 662
723 // Ensure extra long signature does not cause issues and fails. 663 // Ensure extra long signature does not cause issues and fails.
724 const unsigned char kLongSignature[1024] = { 0 }; 664 const unsigned char kLongSignature[1024] = { 0 };
725 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 665 EXPECT_STATUS_SUCCESS(VerifySignature(
726 algorithm, 666 algorithm,
727 key, 667 key,
728 kLongSignature, 668 CryptoData(kLongSignature, sizeof(kLongSignature)),
729 sizeof(kLongSignature), 669 CryptoData(test_message),
730 test_message,
731 &signature_match)); 670 &signature_match));
732 EXPECT_FALSE(signature_match); 671 EXPECT_FALSE(signature_match);
733 } 672 }
734 } 673 }
735 674
736 TEST_F(WebCryptoImplTest, AesCbcFailures) { 675 TEST_F(SharedCryptoTest, AesCbcFailures) {
737 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; 676 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c";
738 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 677 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
739 HexStringToBytes(key_hex), 678 HexStringToBytes(key_hex),
740 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 679 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
741 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 680 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
742 681
743 // Verify exported raw key is identical to the imported data 682 // Verify exported raw key is identical to the imported data
744 blink::WebArrayBuffer raw_key; 683 blink::WebArrayBuffer raw_key;
745 EXPECT_STATUS_SUCCESS( 684 EXPECT_STATUS_SUCCESS(
746 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 685 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
747 ExpectArrayBufferMatchesHex(key_hex, raw_key); 686 ExpectArrayBufferMatchesHex(key_hex, raw_key);
748 687
749 blink::WebArrayBuffer output; 688 blink::WebArrayBuffer output;
750 689
751 // Use an invalid |iv| (fewer than 16 bytes) 690 // Use an invalid |iv| (fewer than 16 bytes)
752 { 691 {
753 std::vector<uint8> input(32); 692 std::vector<uint8> input(32);
754 std::vector<uint8> iv; 693 std::vector<uint8> iv;
755 EXPECT_STATUS(Status::ErrorIncorrectSizeAesCbcIv(), EncryptInternal( 694 EXPECT_STATUS(Status::ErrorIncorrectSizeAesCbcIv(), EncryptInternal(
756 webcrypto::CreateAesCbcAlgorithm(iv), key, input, &output)); 695 webcrypto::CreateAesCbcAlgorithm(iv), key, input, &output));
(...skipping 12 matching lines...) Expand all
769 } 708 }
770 709
771 // Give an input that is too large (would cause integer overflow when 710 // Give an input that is too large (would cause integer overflow when
772 // narrowing to an int). 711 // narrowing to an int).
773 { 712 {
774 std::vector<uint8> iv(16); 713 std::vector<uint8> iv(16);
775 714
776 // Pretend the input is large. Don't pass data pointer as NULL in case that 715 // Pretend the input is large. Don't pass data pointer as NULL in case that
777 // is special cased; the implementation shouldn't actually dereference the 716 // is special cased; the implementation shouldn't actually dereference the
778 // data. 717 // data.
779 const unsigned char* input = &iv[0]; 718 CryptoData input(&iv[0], INT_MAX - 3);
780 unsigned int input_len = INT_MAX - 3;
781 719
782 EXPECT_STATUS(Status::ErrorDataTooLarge(), EncryptInternal( 720 EXPECT_STATUS(Status::ErrorDataTooLarge(), Encrypt(
783 webcrypto::CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); 721 CreateAesCbcAlgorithm(iv), key, input, &output));
784 EXPECT_STATUS(Status::ErrorDataTooLarge(), DecryptInternal( 722 EXPECT_STATUS(Status::ErrorDataTooLarge(), Decrypt(
785 webcrypto::CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); 723 CreateAesCbcAlgorithm(iv), key, input, &output));
786 } 724 }
787 725
788 // Fail importing the key (too few bytes specified) 726 // Fail importing the key (too few bytes specified)
789 { 727 {
790 std::vector<uint8> key_raw(1); 728 std::vector<uint8> key_raw(1);
791 std::vector<uint8> iv(16); 729 std::vector<uint8> iv(16);
792 730
793 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 731 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
794 EXPECT_STATUS( 732 EXPECT_STATUS(
795 Status::Error(), 733 Status::Error(),
796 ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 734 ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
797 key_raw, 735 key_raw,
798 webcrypto::CreateAesCbcAlgorithm(iv), 736 CreateAesCbcAlgorithm(iv),
799 true, 737 true,
800 blink::WebCryptoKeyUsageEncrypt, 738 blink::WebCryptoKeyUsageEncrypt,
801 &key)); 739 &key));
802 } 740 }
803 741
804 // TODO(eroman): Enable for OpenSSL once implemented.
805 #if !defined(USE_OPENSSL)
806 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret 742 // Fail exporting the key in SPKI and PKCS#8 formats (not allowed for secret
807 // keys). 743 // keys).
808 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), 744 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(),
809 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 745 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
810 EXPECT_STATUS(Status::ErrorUnsupported(), 746 EXPECT_STATUS(Status::ErrorUnsupported(),
811 ExportKeyInternal(blink::WebCryptoKeyFormatPkcs8, key, &output)); 747 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &output));
812 #endif
813 } 748 }
814 749
815 TEST_F(WebCryptoImplTest, MAYBE(AesCbcSampleSets)) { 750 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) {
816 scoped_ptr<base::ListValue> tests; 751 scoped_ptr<base::ListValue> tests;
817 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests)); 752 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests));
818 753
819 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 754 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
820 SCOPED_TRACE(test_index); 755 SCOPED_TRACE(test_index);
821 base::DictionaryValue* test; 756 base::DictionaryValue* test;
822 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 757 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
823 758
824 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 759 std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
825 std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); 760 std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv");
826 std::vector<uint8> test_plain_text = 761 std::vector<uint8> test_plain_text =
827 GetBytesFromHexString(test, "plain_text"); 762 GetBytesFromHexString(test, "plain_text");
828 std::vector<uint8> test_cipher_text = 763 std::vector<uint8> test_cipher_text =
829 GetBytesFromHexString(test, "cipher_text"); 764 GetBytesFromHexString(test, "cipher_text");
830 765
831 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 766 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
832 test_key, 767 test_key,
833 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 768 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
834 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 769 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
835 770
836 // Verify exported raw key is identical to the imported data 771 // Verify exported raw key is identical to the imported data
837 blink::WebArrayBuffer raw_key; 772 blink::WebArrayBuffer raw_key;
838 EXPECT_STATUS_SUCCESS(ExportKeyInternal( 773 EXPECT_STATUS_SUCCESS(ExportKey(
839 blink::WebCryptoKeyFormatRaw, key, &raw_key)); 774 blink::WebCryptoKeyFormatRaw, key, &raw_key));
840 ExpectArrayBufferMatches(test_key, raw_key); 775 ExpectArrayBufferMatches(test_key, raw_key);
841 776
842 blink::WebArrayBuffer output; 777 blink::WebArrayBuffer output;
843 778
844 // Test encryption. 779 // Test encryption.
845 EXPECT_STATUS( 780 EXPECT_STATUS(
846 Status::Success(), 781 Status::Success(),
847 EncryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), 782 EncryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv),
848 key, 783 key,
(...skipping 10 matching lines...) Expand all
859 &output)); 794 &output));
860 ExpectArrayBufferMatches(test_plain_text, output); 795 ExpectArrayBufferMatches(test_plain_text, output);
861 796
862 const unsigned int kAesCbcBlockSize = 16; 797 const unsigned int kAesCbcBlockSize = 16;
863 798
864 // Decrypt with a padding error by stripping the last block. This also ends 799 // Decrypt with a padding error by stripping the last block. This also ends
865 // up testing decryption over empty cipher text. 800 // up testing decryption over empty cipher text.
866 if (test_cipher_text.size() >= kAesCbcBlockSize) { 801 if (test_cipher_text.size() >= kAesCbcBlockSize) {
867 EXPECT_STATUS( 802 EXPECT_STATUS(
868 Status::Error(), 803 Status::Error(),
869 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), 804 Decrypt(CreateAesCbcAlgorithm(test_iv),
870 key, 805 key,
871 &test_cipher_text[0], 806 CryptoData(&test_cipher_text[0],
872 test_cipher_text.size() - kAesCbcBlockSize, 807 test_cipher_text.size() - kAesCbcBlockSize),
873 &output)); 808 &output));
874 } 809 }
875 810
876 // Decrypt cipher text which is not a multiple of block size by stripping 811 // Decrypt cipher text which is not a multiple of block size by stripping
877 // a few bytes off the cipher text. 812 // a few bytes off the cipher text.
878 if (test_cipher_text.size() > 3) { 813 if (test_cipher_text.size() > 3) {
879 EXPECT_STATUS( 814 EXPECT_STATUS(
880 Status::Error(), 815 Status::Error(),
881 DecryptInternal(webcrypto::CreateAesCbcAlgorithm(test_iv), 816 Decrypt(CreateAesCbcAlgorithm(test_iv),
882 key, 817 key,
883 &test_cipher_text[0], 818 CryptoData(&test_cipher_text[0],
884 test_cipher_text.size() - 3, 819 test_cipher_text.size() - 3),
885 &output)); 820 &output));
886 } 821 }
887 } 822 }
888 } 823 }
889 824
890 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAes)) { 825 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyAes)) {
891 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each 826 // Check key generation for each of AES-CBC, AES-GCM, and AES-KW, and for each
892 // allowed key length. 827 // allowed key length.
893 std::vector<blink::WebCryptoAlgorithm> algorithm; 828 std::vector<blink::WebCryptoAlgorithm> algorithm;
894 const unsigned short kKeyLength[] = {128, 192, 256}; 829 const unsigned short kKeyLength[] = {128, 192, 256};
895 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) { 830 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) {
896 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i])); 831 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i]));
897 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i])); 832 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i]));
898 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i])); 833 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i]));
899 } 834 }
900 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 835 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
901 std::vector<blink::WebArrayBuffer> keys; 836 std::vector<blink::WebArrayBuffer> keys;
902 blink::WebArrayBuffer key_bytes; 837 blink::WebArrayBuffer key_bytes;
903 for (size_t i = 0; i < algorithm.size(); ++i) { 838 for (size_t i = 0; i < algorithm.size(); ++i) {
904 SCOPED_TRACE(i); 839 SCOPED_TRACE(i);
905 // Generate a small sample of keys. 840 // Generate a small sample of keys.
906 keys.clear(); 841 keys.clear();
907 for (int j = 0; j < 16; ++j) { 842 for (int j = 0; j < 16; ++j) {
908 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm[i], &key)); 843 ASSERT_STATUS_SUCCESS(GenerateSecretKey(
844 algorithm[i], true, 0, &key));
909 EXPECT_TRUE(key.handle()); 845 EXPECT_TRUE(key.handle());
910 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 846 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
911 ASSERT_STATUS_SUCCESS( 847 ASSERT_STATUS_SUCCESS(
912 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &key_bytes)); 848 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_bytes));
913 keys.push_back(key_bytes); 849 keys.push_back(key_bytes);
914 } 850 }
915 // Ensure all entries in the key sample set are unique. This is a simplistic 851 // Ensure all entries in the key sample set are unique. This is a simplistic
916 // estimate of whether the generated keys appear random. 852 // estimate of whether the generated keys appear random.
917 EXPECT_FALSE(CopiesExist(keys)); 853 EXPECT_FALSE(CopiesExist(keys));
918 } 854 }
919 } 855 }
920 856
921 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyAesBadLength)) { 857 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyAesBadLength)) {
922 const unsigned short kKeyLen[] = {0, 127, 257}; 858 const unsigned short kKeyLen[] = {0, 127, 257};
923 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 859 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
924 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) { 860 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLen); ++i) {
925 SCOPED_TRACE(i); 861 SCOPED_TRACE(i);
926 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateKeyInternal( 862 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateSecretKey(
927 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), &key)); 863 CreateAesCbcKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
928 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateKeyInternal( 864 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateSecretKey(
929 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), &key)); 865 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
930 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateKeyInternal( 866 EXPECT_STATUS(Status::ErrorGenerateKeyLength(), GenerateSecretKey(
931 CreateAesKwKeyGenAlgorithm(kKeyLen[i]), &key)); 867 CreateAesKwKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
932 } 868 }
933 } 869 }
934 870
935 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmac)) { 871 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmac)) {
936 // Generate a small sample of HMAC keys. 872 // Generate a small sample of HMAC keys.
937 std::vector<blink::WebArrayBuffer> keys; 873 std::vector<blink::WebArrayBuffer> keys;
938 for (int i = 0; i < 16; ++i) { 874 for (int i = 0; i < 16; ++i) {
939 blink::WebArrayBuffer key_bytes; 875 blink::WebArrayBuffer key_bytes;
940 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 876 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
941 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateHmacKeyGenAlgorithm( 877 blink::WebCryptoAlgorithm algorithm = CreateHmacKeyGenAlgorithm(
942 blink::WebCryptoAlgorithmIdSha1, 64); 878 blink::WebCryptoAlgorithmIdSha1, 64);
943 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm, &key)); 879 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key));
944 EXPECT_FALSE(key.isNull()); 880 EXPECT_FALSE(key.isNull());
945 EXPECT_TRUE(key.handle()); 881 EXPECT_TRUE(key.handle());
946 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 882 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
947 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 883 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
948 884
949 blink::WebArrayBuffer raw_key; 885 blink::WebArrayBuffer raw_key;
950 ASSERT_STATUS_SUCCESS( 886 ASSERT_STATUS_SUCCESS(
951 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 887 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
952 EXPECT_EQ(64U, raw_key.byteLength()); 888 EXPECT_EQ(64U, raw_key.byteLength());
953 keys.push_back(raw_key); 889 keys.push_back(raw_key);
954 } 890 }
955 // Ensure all entries in the key sample set are unique. This is a simplistic 891 // Ensure all entries in the key sample set are unique. This is a simplistic
956 // estimate of whether the generated keys appear random. 892 // estimate of whether the generated keys appear random.
957 EXPECT_FALSE(CopiesExist(keys)); 893 EXPECT_FALSE(CopiesExist(keys));
958 } 894 }
959 895
960 // If the key length is not provided, then the block size is used. 896 // If the key length is not provided, then the block size is used.
961 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyHmacNoLength)) { 897 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) {
962 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 898 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
963 blink::WebCryptoAlgorithm algorithm = 899 blink::WebCryptoAlgorithm algorithm =
964 webcrypto::CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); 900 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0);
965 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm, &key)); 901 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key));
966 EXPECT_TRUE(key.handle()); 902 EXPECT_TRUE(key.handle());
967 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 903 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
968 blink::WebArrayBuffer raw_key; 904 blink::WebArrayBuffer raw_key;
969 ASSERT_STATUS_SUCCESS( 905 ASSERT_STATUS_SUCCESS(
970 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 906 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
971 EXPECT_EQ(64U, raw_key.byteLength()); 907 EXPECT_EQ(64U, raw_key.byteLength());
972 908
973 // The block size for HMAC SHA-512 is larger. 909 // The block size for HMAC SHA-512 is larger.
974 algorithm = webcrypto::CreateHmacKeyGenAlgorithm( 910 algorithm = CreateHmacKeyGenAlgorithm(
975 blink::WebCryptoAlgorithmIdSha512, 0); 911 blink::WebCryptoAlgorithmIdSha512, 0);
976 ASSERT_STATUS_SUCCESS(GenerateKeyInternal(algorithm, &key)); 912 ASSERT_STATUS_SUCCESS(GenerateSecretKey(algorithm, true, 0, &key));
977 ASSERT_STATUS_SUCCESS( 913 ASSERT_STATUS_SUCCESS(
978 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 914 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
979 EXPECT_EQ(128U, raw_key.byteLength()); 915 EXPECT_EQ(128U, raw_key.byteLength());
980 } 916 }
981 917
982 TEST_F(WebCryptoImplTest, MAYBE(ImportSecretKeyNoAlgorithm)) { 918 TEST_F(SharedCryptoTest, MAYBE(ImportSecretKeyNoAlgorithm)) {
983 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 919 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
984 920
985 // This fails because the algorithm is null. 921 // This fails because the algorithm is null.
986 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), ImportKeyInternal( 922 EXPECT_STATUS(Status::ErrorMissingAlgorithmImportRawKey(), ImportKeyInternal(
987 blink::WebCryptoKeyFormatRaw, 923 blink::WebCryptoKeyFormatRaw,
988 HexStringToBytes("00000000000000000000"), 924 HexStringToBytes("00000000000000000000"),
989 blink::WebCryptoAlgorithm::createNull(), 925 blink::WebCryptoAlgorithm::createNull(),
990 true, 926 true,
991 blink::WebCryptoKeyUsageEncrypt, 927 blink::WebCryptoKeyUsageEncrypt,
992 &key)); 928 &key));
993 } 929 }
994 930
995 931 TEST_F(SharedCryptoTest, ImportJwkFailures) {
996 TEST_F(WebCryptoImplTest, ImportJwkFailures) {
997 932
998 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 933 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
999 blink::WebCryptoAlgorithm algorithm = 934 blink::WebCryptoAlgorithm algorithm =
1000 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 935 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
1001 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 936 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1002 937
1003 // Baseline pass: each test below breaks a single item, so we start with a 938 // Baseline pass: each test below breaks a single item, so we start with a
1004 // passing case to make sure each failure is caused by the isolated break. 939 // passing case to make sure each failure is caused by the isolated break.
1005 // Each breaking subtest below resets the dictionary to this passing case when 940 // Each breaking subtest below resets the dictionary to this passing case when
1006 // complete. 941 // complete.
1007 base::DictionaryValue dict; 942 base::DictionaryValue dict;
1008 RestoreJwkOctDictionary(&dict); 943 RestoreJwkOctDictionary(&dict);
1009 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 944 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1010 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 945 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 RestoreJwkOctDictionary(&dict); 1009 RestoreJwkOctDictionary(&dict);
1075 1010
1076 // Fail on invalid extractable (wrong type). 1011 // Fail on invalid extractable (wrong type).
1077 dict.SetInteger("extractable", 0); 1012 dict.SetInteger("extractable", 0);
1078 EXPECT_STATUS( 1013 EXPECT_STATUS(
1079 Status::ErrorJwkPropertyWrongType("extractable", "boolean"), 1014 Status::ErrorJwkPropertyWrongType("extractable", "boolean"),
1080 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1015 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1081 RestoreJwkOctDictionary(&dict); 1016 RestoreJwkOctDictionary(&dict);
1082 } 1017 }
1083 1018
1084 TEST_F(WebCryptoImplTest, ImportJwkOctFailures) { 1019 TEST_F(SharedCryptoTest, ImportJwkOctFailures) {
1085 1020
1086 base::DictionaryValue dict; 1021 base::DictionaryValue dict;
1087 RestoreJwkOctDictionary(&dict); 1022 RestoreJwkOctDictionary(&dict);
1088 blink::WebCryptoAlgorithm algorithm = 1023 blink::WebCryptoAlgorithm algorithm =
1089 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 1024 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
1090 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 1025 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1091 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1026 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1092 1027
1093 // Baseline pass. 1028 // Baseline pass.
1094 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 1029 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1095 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1030 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1096 EXPECT_EQ(algorithm.id(), key.algorithm().id()); 1031 EXPECT_EQ(algorithm.id(), key.algorithm().id());
1097 EXPECT_FALSE(key.extractable()); 1032 EXPECT_FALSE(key.extractable());
1098 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages()); 1033 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages());
1099 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1034 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
(...skipping 28 matching lines...) Expand all
1128 RestoreJwkOctDictionary(&dict); 1063 RestoreJwkOctDictionary(&dict);
1129 1064
1130 // Fail on k actual length (192 bits) inconsistent with the embedded JWK alg 1065 // Fail on k actual length (192 bits) inconsistent with the embedded JWK alg
1131 // value (128) for an AES key. 1066 // value (128) for an AES key.
1132 dict.SetString("k", "dGhpcyAgaXMgIDI0ICBieXRlcyBsb25n"); 1067 dict.SetString("k", "dGhpcyAgaXMgIDI0ICBieXRlcyBsb25n");
1133 EXPECT_STATUS(Status::ErrorJwkIncorrectKeyLength(), ImportKeyJwk( 1068 EXPECT_STATUS(Status::ErrorJwkIncorrectKeyLength(), ImportKeyJwk(
1134 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1069 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1135 RestoreJwkOctDictionary(&dict); 1070 RestoreJwkOctDictionary(&dict);
1136 } 1071 }
1137 1072
1138 TEST_F(WebCryptoImplTest, MAYBE(ImportJwkRsaFailures)) { 1073 TEST_F(SharedCryptoTest, MAYBE(ImportJwkRsaFailures)) {
1139 1074
1140 base::DictionaryValue dict; 1075 base::DictionaryValue dict;
1141 RestoreJwkRsaDictionary(&dict); 1076 RestoreJwkRsaDictionary(&dict);
1142 blink::WebCryptoAlgorithm algorithm = 1077 blink::WebCryptoAlgorithm algorithm =
1143 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1078 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1144 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt; 1079 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageEncrypt;
1145 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1080 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1146 1081
1147 // An RSA public key JWK _must_ have an "n" (modulus) and an "e" (exponent) 1082 // An RSA public key JWK _must_ have an "n" (modulus) and an "e" (exponent)
1148 // entry, while an RSA private key must have those plus at least a "d" 1083 // entry, while an RSA private key must have those plus at least a "d"
1149 // (private exponent) entry. 1084 // (private exponent) entry.
1150 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-18, 1085 // See http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-18,
1151 // section 6.3. 1086 // section 6.3.
1152 1087
1153 // Baseline pass. 1088 // Baseline pass.
(...skipping 30 matching lines...) Expand all
1184 } 1119 }
1185 1120
1186 // Fail if "d" parameter is present, implying the JWK is a private key, which 1121 // Fail if "d" parameter is present, implying the JWK is a private key, which
1187 // is not supported. 1122 // is not supported.
1188 dict.SetString("d", "Qk3f0Dsyt"); 1123 dict.SetString("d", "Qk3f0Dsyt");
1189 EXPECT_STATUS(Status::ErrorJwkRsaPrivateKeyUnsupported(), ImportKeyJwk( 1124 EXPECT_STATUS(Status::ErrorJwkRsaPrivateKeyUnsupported(), ImportKeyJwk(
1190 MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1125 MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1191 RestoreJwkRsaDictionary(&dict); 1126 RestoreJwkRsaDictionary(&dict);
1192 } 1127 }
1193 1128
1194 TEST_F(WebCryptoImplTest, MAYBE(ImportJwkInputConsistency)) { 1129 TEST_F(SharedCryptoTest, MAYBE(ImportJwkInputConsistency)) {
1195 // The Web Crypto spec says that if a JWK value is present, but is 1130 // The Web Crypto spec says that if a JWK value is present, but is
1196 // inconsistent with the input value, the operation must fail. 1131 // inconsistent with the input value, the operation must fail.
1197 1132
1198 // Consistency rules when JWK value is not present: Inputs should be used. 1133 // Consistency rules when JWK value is not present: Inputs should be used.
1199 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1134 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1200 bool extractable = false; 1135 bool extractable = false;
1201 blink::WebCryptoAlgorithm algorithm = 1136 blink::WebCryptoAlgorithm algorithm =
1202 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256); 1137 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256);
1203 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify; 1138 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify;
1204 base::DictionaryValue dict; 1139 base::DictionaryValue dict;
1205 dict.SetString("kty", "oct"); 1140 dict.SetString("kty", "oct");
1206 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); 1141 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg");
1207 std::vector<uint8> json_vec = MakeJsonVector(dict); 1142 std::vector<uint8> json_vec = MakeJsonVector(dict);
1208 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 1143 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1209 json_vec, algorithm, extractable, usage_mask, &key)); 1144 json_vec, algorithm, extractable, usage_mask, &key));
1210 EXPECT_TRUE(key.handle()); 1145 EXPECT_TRUE(key.handle());
1211 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1146 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1212 EXPECT_EQ(extractable, key.extractable()); 1147 EXPECT_EQ(extractable, key.extractable());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1245 EXPECT_TRUE(key.extractable()); 1180 EXPECT_TRUE(key.extractable());
1246 EXPECT_STATUS_SUCCESS( 1181 EXPECT_STATUS_SUCCESS(
1247 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key)); 1182 ImportKeyJwk(MakeJsonVector(dict), algorithm, false, usage_mask, &key));
1248 EXPECT_FALSE(key.extractable()); 1183 EXPECT_FALSE(key.extractable());
1249 dict.SetBoolean("extractable", true); // restore previous value 1184 dict.SetBoolean("extractable", true); // restore previous value
1250 1185
1251 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value 1186 // Fail: Input algorithm (AES-CBC) is inconsistent with JWK value
1252 // (HMAC SHA256). 1187 // (HMAC SHA256).
1253 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk( 1188 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk(
1254 json_vec, 1189 json_vec,
1255 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1190 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1256 extractable, 1191 extractable,
1257 usage_mask, 1192 usage_mask,
1258 &key)); 1193 &key));
1259 1194
1260 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value 1195 // Fail: Input algorithm (HMAC SHA1) is inconsistent with JWK value
1261 // (HMAC SHA256). 1196 // (HMAC SHA256).
1262 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk( 1197 EXPECT_STATUS(Status::ErrorJwkAlgorithmInconsistent(), ImportKeyJwk(
1263 json_vec, 1198 json_vec,
1264 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha1), 1199 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha1),
1265 extractable, 1200 extractable,
1266 usage_mask, 1201 usage_mask,
1267 &key)); 1202 &key));
1268 1203
1269 // Pass: JWK alg valid but input algorithm isNull: use JWK algorithm value. 1204 // Pass: JWK alg valid but input algorithm isNull: use JWK algorithm value.
1270 EXPECT_STATUS_SUCCESS(ImportKeyJwk(json_vec, 1205 EXPECT_STATUS_SUCCESS(ImportKeyJwk(json_vec,
1271 blink::WebCryptoAlgorithm::createNull(), 1206 blink::WebCryptoAlgorithm::createNull(),
1272 extractable, 1207 extractable,
1273 usage_mask, 1208 usage_mask,
1274 &key)); 1209 &key));
1275 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); 1210 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id());
1276 1211
1277 // Pass: JWK alg missing but input algorithm specified: use input value 1212 // Pass: JWK alg missing but input algorithm specified: use input value
1278 dict.Remove("alg", NULL); 1213 dict.Remove("alg", NULL);
1279 EXPECT_STATUS_SUCCESS(ImportKeyJwk( 1214 EXPECT_STATUS_SUCCESS(ImportKeyJwk(
1280 MakeJsonVector(dict), 1215 MakeJsonVector(dict),
1281 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256), 1216 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256),
1282 extractable, 1217 extractable,
1283 usage_mask, 1218 usage_mask,
1284 &key)); 1219 &key));
1285 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id()); 1220 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, algorithm.id());
1286 dict.SetString("alg", "HS256"); 1221 dict.SetString("alg", "HS256");
1287 1222
1288 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value 1223 // Fail: Input usage_mask (encrypt) is not a subset of the JWK value
1289 // (sign|verify) 1224 // (sign|verify)
1290 EXPECT_STATUS(Status::ErrorJwkUsageInconsistent(), ImportKeyJwk( 1225 EXPECT_STATUS(Status::ErrorJwkUsageInconsistent(), ImportKeyJwk(
1291 json_vec, algorithm, extractable, blink::WebCryptoKeyUsageEncrypt, &key)); 1226 json_vec, algorithm, extractable, blink::WebCryptoKeyUsageEncrypt, &key));
1292 1227
1293 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK 1228 // Fail: Input usage_mask (encrypt|sign|verify) is not a subset of the JWK
1294 // value (sign|verify) 1229 // value (sign|verify)
1295 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign | 1230 usage_mask = blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageSign |
1296 blink::WebCryptoKeyUsageVerify; 1231 blink::WebCryptoKeyUsageVerify;
1297 EXPECT_STATUS( 1232 EXPECT_STATUS(
1298 Status::ErrorJwkUsageInconsistent(), 1233 Status::ErrorJwkUsageInconsistent(),
1299 ImportKeyJwk(json_vec, algorithm, extractable, usage_mask, &key)); 1234 ImportKeyJwk(json_vec, algorithm, extractable, usage_mask, &key));
1300 1235
1301 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value, 1236 // TODO(padolph): kty vs alg consistency tests: Depending on the kty value,
1302 // only certain alg values are permitted. For example, when kty = "RSA" alg 1237 // only certain alg values are permitted. For example, when kty = "RSA" alg
1303 // must be of the RSA family, or when kty = "oct" alg must be symmetric 1238 // must be of the RSA family, or when kty = "oct" alg must be symmetric
1304 // algorithm. 1239 // algorithm.
1305 } 1240 }
1306 1241
1307 TEST_F(WebCryptoImplTest, MAYBE(ImportJwkHappy)) { 1242 TEST_F(SharedCryptoTest, MAYBE(ImportJwkHappy)) {
1308 1243
1309 // This test verifies the happy path of JWK import, including the application 1244 // This test verifies the happy path of JWK import, including the application
1310 // of the imported key material. 1245 // of the imported key material.
1311 1246
1312 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1247 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1313 bool extractable = false; 1248 bool extractable = false;
1314 blink::WebCryptoAlgorithm algorithm = 1249 blink::WebCryptoAlgorithm algorithm =
1315 webcrypto::CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256); 1250 CreateHmacAlgorithmByHashId(blink::WebCryptoAlgorithmIdSha256);
1316 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign; 1251 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageSign;
1317 1252
1318 // Import a symmetric key JWK and HMAC-SHA256 sign() 1253 // Import a symmetric key JWK and HMAC-SHA256 sign()
1319 // Uses the first SHA256 test vector from the HMAC sample set above. 1254 // Uses the first SHA256 test vector from the HMAC sample set above.
1320 1255
1321 base::DictionaryValue dict; 1256 base::DictionaryValue dict;
1322 dict.SetString("kty", "oct"); 1257 dict.SetString("kty", "oct");
1323 dict.SetString("alg", "HS256"); 1258 dict.SetString("alg", "HS256");
1324 dict.SetString("use", "sig"); 1259 dict.SetString("use", "sig");
1325 dict.SetBoolean("extractable", false); 1260 dict.SetBoolean("extractable", false);
(...skipping 14 matching lines...) Expand all
1340 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, message_raw, &output)); 1275 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, key, message_raw, &output));
1341 1276
1342 const std::string mac_raw = 1277 const std::string mac_raw =
1343 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; 1278 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b";
1344 1279
1345 ExpectArrayBufferMatchesHex(mac_raw, output); 1280 ExpectArrayBufferMatchesHex(mac_raw, output);
1346 1281
1347 // TODO(padolph): Import an RSA public key JWK and use it 1282 // TODO(padolph): Import an RSA public key JWK and use it
1348 } 1283 }
1349 1284
1350 TEST_F(WebCryptoImplTest, MAYBE(ImportExportSpki)) { 1285 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) {
1351 // Passing case: Import a valid RSA key in SPKI format. 1286 // Passing case: Import a valid RSA key in SPKI format.
1352 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1287 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1353 ASSERT_STATUS_SUCCESS(ImportKeyInternal( 1288 ASSERT_STATUS_SUCCESS(ImportKeyInternal(
1354 blink::WebCryptoKeyFormatSpki, 1289 blink::WebCryptoKeyFormatSpki,
1355 HexStringToBytes(kPublicKeySpkiDerHex), 1290 HexStringToBytes(kPublicKeySpkiDerHex),
1356 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1291 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1357 true, 1292 true,
1358 blink::WebCryptoKeyUsageEncrypt, 1293 blink::WebCryptoKeyUsageEncrypt,
1359 &key)); 1294 &key));
1360 EXPECT_TRUE(key.handle()); 1295 EXPECT_TRUE(key.handle());
1361 EXPECT_EQ(blink::WebCryptoKeyTypePublic, key.type()); 1296 EXPECT_EQ(blink::WebCryptoKeyTypePublic, key.type());
1362 EXPECT_TRUE(key.extractable()); 1297 EXPECT_TRUE(key.extractable());
1363 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages()); 1298 EXPECT_EQ(blink::WebCryptoKeyUsageEncrypt, key.usages());
1364 1299
1365 // Failing case: Empty SPKI data 1300 // Failing case: Empty SPKI data
1366 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal( 1301 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal(
(...skipping 12 matching lines...) Expand all
1379 HexStringToBytes(kPublicKeySpkiDerHex), 1314 HexStringToBytes(kPublicKeySpkiDerHex),
1380 blink::WebCryptoAlgorithm::createNull(), 1315 blink::WebCryptoAlgorithm::createNull(),
1381 true, 1316 true,
1382 blink::WebCryptoKeyUsageEncrypt, 1317 blink::WebCryptoKeyUsageEncrypt,
1383 &key)); 1318 &key));
1384 1319
1385 // Failing case: Bad DER encoding. 1320 // Failing case: Bad DER encoding.
1386 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1321 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1387 blink::WebCryptoKeyFormatSpki, 1322 blink::WebCryptoKeyFormatSpki,
1388 HexStringToBytes("618333c4cb"), 1323 HexStringToBytes("618333c4cb"),
1389 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1324 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1390 true, 1325 true,
1391 blink::WebCryptoKeyUsageEncrypt, 1326 blink::WebCryptoKeyUsageEncrypt,
1392 &key)); 1327 &key));
1393 1328
1394 // Failing case: Import RSA key but provide an inconsistent input algorithm. 1329 // Failing case: Import RSA key but provide an inconsistent input algorithm.
1395 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1330 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1396 blink::WebCryptoKeyFormatSpki, 1331 blink::WebCryptoKeyFormatSpki,
1397 HexStringToBytes(kPublicKeySpkiDerHex), 1332 HexStringToBytes(kPublicKeySpkiDerHex),
1398 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1333 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1399 true, 1334 true,
1400 blink::WebCryptoKeyUsageEncrypt, 1335 blink::WebCryptoKeyUsageEncrypt,
1401 &key)); 1336 &key));
1402 1337
1403 // Passing case: Export a previously imported RSA public key in SPKI format 1338 // Passing case: Export a previously imported RSA public key in SPKI format
1404 // and compare to original data. 1339 // and compare to original data.
1405 blink::WebArrayBuffer output; 1340 blink::WebArrayBuffer output;
1406 ASSERT_STATUS_SUCCESS( 1341 ASSERT_STATUS_SUCCESS(
1407 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 1342 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
1408 ExpectArrayBufferMatchesHex(kPublicKeySpkiDerHex, output); 1343 ExpectArrayBufferMatchesHex(kPublicKeySpkiDerHex, output);
1409 1344
1410 // Failing case: Try to export a previously imported RSA public key in raw 1345 // Failing case: Try to export a previously imported RSA public key in raw
1411 // format (not allowed for a public key). 1346 // format (not allowed for a public key).
1412 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), 1347 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(),
1413 ExportKeyInternal(blink::WebCryptoKeyFormatRaw, key, &output)); 1348 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output));
1414 1349
1415 // Failing case: Try to export a non-extractable key 1350 // Failing case: Try to export a non-extractable key
1416 ASSERT_STATUS_SUCCESS(ImportKeyInternal( 1351 ASSERT_STATUS_SUCCESS(ImportKeyInternal(
1417 blink::WebCryptoKeyFormatSpki, 1352 blink::WebCryptoKeyFormatSpki,
1418 HexStringToBytes(kPublicKeySpkiDerHex), 1353 HexStringToBytes(kPublicKeySpkiDerHex),
1419 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5), 1354 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5),
1420 false, 1355 false,
1421 blink::WebCryptoKeyUsageEncrypt, 1356 blink::WebCryptoKeyUsageEncrypt,
1422 &key)); 1357 &key));
1423 EXPECT_TRUE(key.handle()); 1358 EXPECT_TRUE(key.handle());
1424 EXPECT_FALSE(key.extractable()); 1359 EXPECT_FALSE(key.extractable());
1425 EXPECT_STATUS(Status::ErrorKeyNotExtractable(), 1360 EXPECT_STATUS(Status::ErrorKeyNotExtractable(),
1426 ExportKeyInternal(blink::WebCryptoKeyFormatSpki, key, &output)); 1361 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
1427 } 1362 }
1428 1363
1429 TEST_F(WebCryptoImplTest, MAYBE(ImportPkcs8)) { 1364 TEST_F(SharedCryptoTest, MAYBE(ImportPkcs8)) {
1430 // Passing case: Import a valid RSA key in PKCS#8 format. 1365 // Passing case: Import a valid RSA key in PKCS#8 format.
1431 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1366 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1432 ASSERT_STATUS_SUCCESS(ImportKeyInternal( 1367 ASSERT_STATUS_SUCCESS(ImportKeyInternal(
1433 blink::WebCryptoKeyFormatPkcs8, 1368 blink::WebCryptoKeyFormatPkcs8,
1434 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1369 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1435 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 1370 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
1436 true, 1371 true,
1437 blink::WebCryptoKeyUsageSign, 1372 blink::WebCryptoKeyUsageSign,
1438 &key)); 1373 &key));
1439 EXPECT_TRUE(key.handle()); 1374 EXPECT_TRUE(key.handle());
1440 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, key.type()); 1375 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, key.type());
1441 EXPECT_TRUE(key.extractable()); 1376 EXPECT_TRUE(key.extractable());
1442 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); 1377 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages());
1443 1378
1444 // Failing case: Empty PKCS#8 data 1379 // Failing case: Empty PKCS#8 data
1445 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal( 1380 EXPECT_STATUS(Status::ErrorImportEmptyKeyData(), ImportKeyInternal(
(...skipping 12 matching lines...) Expand all
1458 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1393 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1459 blink::WebCryptoAlgorithm::createNull(), 1394 blink::WebCryptoAlgorithm::createNull(),
1460 true, 1395 true,
1461 blink::WebCryptoKeyUsageSign, 1396 blink::WebCryptoKeyUsageSign,
1462 &key)); 1397 &key));
1463 1398
1464 // Failing case: Bad DER encoding. 1399 // Failing case: Bad DER encoding.
1465 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1400 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1466 blink::WebCryptoKeyFormatPkcs8, 1401 blink::WebCryptoKeyFormatPkcs8,
1467 HexStringToBytes("618333c4cb"), 1402 HexStringToBytes("618333c4cb"),
1468 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 1403 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
1469 true, 1404 true,
1470 blink::WebCryptoKeyUsageSign, 1405 blink::WebCryptoKeyUsageSign,
1471 &key)); 1406 &key));
1472 1407
1473 // Failing case: Import RSA key but provide an inconsistent input algorithm. 1408 // Failing case: Import RSA key but provide an inconsistent input algorithm.
1474 EXPECT_STATUS(Status::Error(), ImportKeyInternal( 1409 EXPECT_STATUS(Status::Error(), ImportKeyInternal(
1475 blink::WebCryptoKeyFormatPkcs8, 1410 blink::WebCryptoKeyFormatPkcs8,
1476 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1411 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1477 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1412 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1478 true, 1413 true,
1479 blink::WebCryptoKeyUsageSign, 1414 blink::WebCryptoKeyUsageSign,
1480 &key)); 1415 &key));
1481 } 1416 }
1482 1417
1483 TEST_F(WebCryptoImplTest, MAYBE(GenerateKeyPairRsa)) { 1418 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) {
1484 // Note: using unrealistic short key lengths here to avoid bogging down tests. 1419 // Note: using unrealistic short key lengths here to avoid bogging down tests.
1485 1420
1486 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation. 1421 // Successful WebCryptoAlgorithmIdRsaEsPkcs1v1_5 key generation.
1487 const unsigned int modulus_length = 256; 1422 const unsigned int modulus_length = 256;
1488 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 1423 const std::vector<uint8> public_exponent = HexStringToBytes("010001");
1489 blink::WebCryptoAlgorithm algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1424 blink::WebCryptoAlgorithm algorithm = CreateRsaKeyGenAlgorithm(
1490 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1425 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1491 modulus_length, 1426 modulus_length,
1492 public_exponent); 1427 public_exponent);
1493 bool extractable = false; 1428 bool extractable = false;
1494 const blink::WebCryptoKeyUsageMask usage_mask = 0; 1429 const blink::WebCryptoKeyUsageMask usage_mask = 0;
1495 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1430 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1496 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1431 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1497 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1432 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1498 algorithm, extractable, usage_mask, &public_key, &private_key)); 1433 algorithm, extractable, usage_mask, &public_key, &private_key));
1499 EXPECT_FALSE(public_key.isNull()); 1434 EXPECT_FALSE(public_key.isNull());
1500 EXPECT_FALSE(private_key.isNull()); 1435 EXPECT_FALSE(private_key.isNull());
1501 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1436 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1502 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1437 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1503 EXPECT_TRUE(public_key.extractable()); 1438 EXPECT_TRUE(public_key.extractable());
1504 EXPECT_EQ(extractable, private_key.extractable()); 1439 EXPECT_EQ(extractable, private_key.extractable());
1505 EXPECT_EQ(usage_mask, public_key.usages()); 1440 EXPECT_EQ(usage_mask, public_key.usages());
1506 EXPECT_EQ(usage_mask, private_key.usages()); 1441 EXPECT_EQ(usage_mask, private_key.usages());
1507 1442
1508 // Fail with bad modulus. 1443 // Fail with bad modulus.
1509 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1444 algorithm = CreateRsaKeyGenAlgorithm(
1510 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent); 1445 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 0, public_exponent);
1511 EXPECT_STATUS(Status::ErrorGenerateRsaZeroModulus(), GenerateKeyPairInternal( 1446 EXPECT_STATUS(Status::ErrorGenerateRsaZeroModulus(), GenerateKeyPair(
1512 algorithm, extractable, usage_mask, &public_key, &private_key)); 1447 algorithm, extractable, usage_mask, &public_key, &private_key));
1513 1448
1514 // Fail with bad exponent: larger than unsigned long. 1449 // Fail with bad exponent: larger than unsigned long.
1515 unsigned int exponent_length = sizeof(unsigned long) + 1; // NOLINT 1450 unsigned int exponent_length = sizeof(unsigned long) + 1; // NOLINT
1516 const std::vector<uint8> long_exponent(exponent_length, 0x01); 1451 const std::vector<uint8> long_exponent(exponent_length, 0x01);
1517 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1452 algorithm = CreateRsaKeyGenAlgorithm(
1518 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1453 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1519 modulus_length, 1454 modulus_length,
1520 long_exponent); 1455 long_exponent);
1521 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(), 1456 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(),
1522 GenerateKeyPairInternal(algorithm, extractable, usage_mask, &public_key, 1457 GenerateKeyPair(algorithm, extractable, usage_mask, &public_key,
1523 &private_key)); 1458 &private_key));
1524 1459
1525 // Fail with bad exponent: empty. 1460 // Fail with bad exponent: empty.
1526 const std::vector<uint8> empty_exponent; 1461 const std::vector<uint8> empty_exponent;
1527 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1462 algorithm = CreateRsaKeyGenAlgorithm(
1528 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1463 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1529 modulus_length, 1464 modulus_length,
1530 empty_exponent); 1465 empty_exponent);
1531 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(), 1466 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(),
1532 GenerateKeyPairInternal(algorithm, extractable, usage_mask, &public_key, 1467 GenerateKeyPair(algorithm, extractable, usage_mask, &public_key,
1533 &private_key)); 1468 &private_key));
1534 1469
1535 // Fail with bad exponent: all zeros. 1470 // Fail with bad exponent: all zeros.
1536 std::vector<uint8> exponent_with_leading_zeros(15, 0x00); 1471 std::vector<uint8> exponent_with_leading_zeros(15, 0x00);
1537 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1472 algorithm = CreateRsaKeyGenAlgorithm(
1538 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1473 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1539 modulus_length, 1474 modulus_length,
1540 exponent_with_leading_zeros); 1475 exponent_with_leading_zeros);
1541 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(), 1476 EXPECT_STATUS(Status::ErrorGenerateKeyPublicExponent(),
1542 GenerateKeyPairInternal(algorithm, extractable, usage_mask, &public_key, 1477 GenerateKeyPair(algorithm, extractable, usage_mask, &public_key,
1543 &private_key)); 1478 &private_key));
1544 1479
1545 // Key generation success using exponent with leading zeros. 1480 // Key generation success using exponent with leading zeros.
1546 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(), 1481 exponent_with_leading_zeros.insert(exponent_with_leading_zeros.end(),
1547 public_exponent.begin(), 1482 public_exponent.begin(),
1548 public_exponent.end()); 1483 public_exponent.end());
1549 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1484 algorithm = CreateRsaKeyGenAlgorithm(
1550 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5, 1485 blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5,
1551 modulus_length, 1486 modulus_length,
1552 exponent_with_leading_zeros); 1487 exponent_with_leading_zeros);
1553 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1488 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1554 algorithm, extractable, usage_mask, &public_key, &private_key)); 1489 algorithm, extractable, usage_mask, &public_key, &private_key));
1555 EXPECT_FALSE(public_key.isNull()); 1490 EXPECT_FALSE(public_key.isNull());
1556 EXPECT_FALSE(private_key.isNull()); 1491 EXPECT_FALSE(private_key.isNull());
1557 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1492 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1558 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1493 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1559 EXPECT_TRUE(public_key.extractable()); 1494 EXPECT_TRUE(public_key.extractable());
1560 EXPECT_EQ(extractable, private_key.extractable()); 1495 EXPECT_EQ(extractable, private_key.extractable());
1561 EXPECT_EQ(usage_mask, public_key.usages()); 1496 EXPECT_EQ(usage_mask, public_key.usages());
1562 EXPECT_EQ(usage_mask, private_key.usages()); 1497 EXPECT_EQ(usage_mask, private_key.usages());
1563 1498
1564 // Successful WebCryptoAlgorithmIdRsaOaep key generation. 1499 // Successful WebCryptoAlgorithmIdRsaOaep key generation.
1565 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1500 algorithm = CreateRsaKeyGenAlgorithm(
1566 blink::WebCryptoAlgorithmIdRsaOaep, modulus_length, public_exponent); 1501 blink::WebCryptoAlgorithmIdRsaOaep, modulus_length, public_exponent);
1567 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1502 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1568 algorithm, extractable, usage_mask, &public_key, &private_key)); 1503 algorithm, extractable, usage_mask, &public_key, &private_key));
1569 EXPECT_FALSE(public_key.isNull()); 1504 EXPECT_FALSE(public_key.isNull());
1570 EXPECT_FALSE(private_key.isNull()); 1505 EXPECT_FALSE(private_key.isNull());
1571 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1506 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1572 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1507 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1573 EXPECT_TRUE(public_key.extractable()); 1508 EXPECT_TRUE(public_key.extractable());
1574 EXPECT_EQ(extractable, private_key.extractable()); 1509 EXPECT_EQ(extractable, private_key.extractable());
1575 EXPECT_EQ(usage_mask, public_key.usages()); 1510 EXPECT_EQ(usage_mask, public_key.usages());
1576 EXPECT_EQ(usage_mask, private_key.usages()); 1511 EXPECT_EQ(usage_mask, private_key.usages());
1577 1512
1578 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation. 1513 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation.
1579 algorithm = webcrypto::CreateRsaKeyGenAlgorithm( 1514 algorithm = CreateRsaKeyGenAlgorithm(
1580 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1515 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1581 modulus_length, 1516 modulus_length,
1582 public_exponent); 1517 public_exponent);
1583 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1518 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1584 algorithm, false, usage_mask, &public_key, &private_key)); 1519 algorithm, false, usage_mask, &public_key, &private_key));
1585 EXPECT_FALSE(public_key.isNull()); 1520 EXPECT_FALSE(public_key.isNull());
1586 EXPECT_FALSE(private_key.isNull()); 1521 EXPECT_FALSE(private_key.isNull());
1587 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type()); 1522 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key.type());
1588 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type()); 1523 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key.type());
1589 // Even though "extractable" was set to false, the public key remains 1524 // Even though "extractable" was set to false, the public key remains
1590 // extractable. 1525 // extractable.
1591 EXPECT_TRUE(public_key.extractable()); 1526 EXPECT_TRUE(public_key.extractable());
1592 EXPECT_FALSE(private_key.extractable()); 1527 EXPECT_FALSE(private_key.extractable());
1593 EXPECT_EQ(usage_mask, public_key.usages()); 1528 EXPECT_EQ(usage_mask, public_key.usages());
1594 EXPECT_EQ(usage_mask, private_key.usages()); 1529 EXPECT_EQ(usage_mask, private_key.usages());
1595 1530
1596 // Exporting a private key as SPKI format doesn't make sense. However this 1531 // Exporting a private key as SPKI format doesn't make sense. However this
1597 // will first fail because the key is not extractable. 1532 // will first fail because the key is not extractable.
1598 blink::WebArrayBuffer output; 1533 blink::WebArrayBuffer output;
1599 EXPECT_STATUS(Status::ErrorKeyNotExtractable(), ExportKeyInternal( 1534 EXPECT_STATUS(Status::ErrorKeyNotExtractable(), ExportKey(
1600 blink::WebCryptoKeyFormatSpki, private_key, &output)); 1535 blink::WebCryptoKeyFormatSpki, private_key, &output));
1601 1536
1602 // Re-generate an extractable private_key and try to export it as SPKI format. 1537 // Re-generate an extractable private_key and try to export it as SPKI format.
1603 // This should fail since spki is for public keys. 1538 // This should fail since spki is for public keys.
1604 EXPECT_STATUS_SUCCESS(GenerateKeyPairInternal( 1539 EXPECT_STATUS_SUCCESS(GenerateKeyPair(
1605 algorithm, true, usage_mask, &public_key, &private_key)); 1540 algorithm, true, usage_mask, &public_key, &private_key));
1606 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), ExportKeyInternal( 1541 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), ExportKey(
1607 blink::WebCryptoKeyFormatSpki, private_key, &output)); 1542 blink::WebCryptoKeyFormatSpki, private_key, &output));
1608 } 1543 }
1609 1544
1610 TEST_F(WebCryptoImplTest, MAYBE(RsaEsRoundTrip)) { 1545 TEST_F(SharedCryptoTest, MAYBE(RsaEsRoundTrip)) {
1611 // Import a key pair. 1546 // Import a key pair.
1612 blink::WebCryptoAlgorithm algorithm = 1547 blink::WebCryptoAlgorithm algorithm =
1613 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1548 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1614 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1549 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1615 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1550 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1616 ImportRsaKeyPair( 1551 ImportRsaKeyPair(
1617 HexStringToBytes(kPublicKeySpkiDerHex), 1552 HexStringToBytes(kPublicKeySpkiDerHex),
1618 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1553 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1619 algorithm, 1554 algorithm,
1620 false, 1555 false,
1621 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 1556 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
1622 &public_key, 1557 &public_key,
1623 &private_key); 1558 &private_key);
1624 1559
1625 // Make a maximum-length data message. RSAES can operate on messages up to 1560 // Make a maximum-length data message. RSAES can operate on messages up to
1626 // length of k - 11 bytes, where k is the octet length of the RSA modulus. 1561 // length of k - 11 bytes, where k is the octet length of the RSA modulus.
1627 const unsigned int kMaxMsgSizeBytes = kModulusLength / 8 - 11; 1562 const unsigned int kMaxMsgSizeBytes = kModulusLength / 8 - 11;
1628 // There are two hex chars for each byte. 1563 // There are two hex chars for each byte.
1629 const unsigned int kMsgHexSize = kMaxMsgSizeBytes * 2; 1564 const unsigned int kMsgHexSize = kMaxMsgSizeBytes * 2;
1630 char max_data_hex[kMsgHexSize+1]; 1565 char max_data_hex[kMsgHexSize+1];
1631 std::fill(&max_data_hex[0], &max_data_hex[0] + kMsgHexSize, 'a'); 1566 std::fill(&max_data_hex[0], &max_data_hex[0] + kMsgHexSize, 'a');
1632 max_data_hex[kMsgHexSize] = '\0'; 1567 max_data_hex[kMsgHexSize] = '\0';
1633 1568
1634 // Verify encrypt / decrypt round trip on a few messages. Note that RSA 1569 // Verify encrypt / decrypt round trip on a few messages. Note that RSA
1635 // encryption does not support empty input. 1570 // encryption does not support empty input.
1636 algorithm = 1571 algorithm =
1637 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1572 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1638 const char* const kTestDataHex[] = { 1573 const char* const kTestDataHex[] = {
1639 "ff", 1574 "ff",
1640 "0102030405060708090a0b0c0d0e0f", 1575 "0102030405060708090a0b0c0d0e0f",
1641 max_data_hex 1576 max_data_hex
1642 }; 1577 };
1643 blink::WebArrayBuffer encrypted_data; 1578 blink::WebArrayBuffer encrypted_data;
1644 blink::WebArrayBuffer decrypted_data; 1579 blink::WebArrayBuffer decrypted_data;
1645 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestDataHex); ++i) { 1580 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestDataHex); ++i) {
1646 SCOPED_TRACE(i); 1581 SCOPED_TRACE(i);
1647 EXPECT_STATUS_SUCCESS(EncryptInternal( 1582 EXPECT_STATUS_SUCCESS(EncryptInternal(
1648 algorithm, 1583 algorithm,
1649 public_key, 1584 public_key,
1650 HexStringToBytes(kTestDataHex[i]), 1585 HexStringToBytes(kTestDataHex[i]),
1651 &encrypted_data)); 1586 &encrypted_data));
1652 EXPECT_EQ(kModulusLength / 8, encrypted_data.byteLength()); 1587 EXPECT_EQ(kModulusLength / 8, encrypted_data.byteLength());
1653 ASSERT_STATUS_SUCCESS(DecryptInternal( 1588 ASSERT_STATUS_SUCCESS(Decrypt(
1654 algorithm, 1589 algorithm,
1655 private_key, 1590 private_key,
1656 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1591 CryptoData(encrypted_data),
1657 encrypted_data.byteLength(),
1658 &decrypted_data)); 1592 &decrypted_data));
1659 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data); 1593 ExpectArrayBufferMatchesHex(kTestDataHex[i], decrypted_data);
1660 } 1594 }
1661 } 1595 }
1662 1596
1663 TEST_F(WebCryptoImplTest, MAYBE(RsaEsKnownAnswer)) { 1597 TEST_F(SharedCryptoTest, MAYBE(RsaEsKnownAnswer)) {
1664 scoped_ptr<base::Value> json; 1598 scoped_ptr<base::Value> json;
1665 ASSERT_TRUE(ReadJsonTestFile("rsa_es.json", &json)); 1599 ASSERT_TRUE(ReadJsonTestFile("rsa_es.json", &json));
1666 base::DictionaryValue* test = NULL; 1600 base::DictionaryValue* test = NULL;
1667 ASSERT_TRUE(json->GetAsDictionary(&test)); 1601 ASSERT_TRUE(json->GetAsDictionary(&test));
1668 1602
1669 // Because the random data in PKCS1.5 padding makes the encryption output non- 1603 // Because the random data in PKCS1.5 padding makes the encryption output non-
1670 // deterministic, we cannot easily do a typical known-answer test for RSA 1604 // deterministic, we cannot easily do a typical known-answer test for RSA
1671 // encryption / decryption. Instead we will take a known-good encrypted 1605 // encryption / decryption. Instead we will take a known-good encrypted
1672 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the 1606 // message, decrypt it, re-encrypt it, then decrypt again, verifying that the
1673 // original known cleartext is the result. 1607 // original known cleartext is the result.
1674 1608
1675 const std::vector<uint8> rsa_spki_der = 1609 const std::vector<uint8> rsa_spki_der =
1676 GetBytesFromHexString(test, "rsa_spki_der"); 1610 GetBytesFromHexString(test, "rsa_spki_der");
1677 1611
1678 const std::vector<uint8> rsa_pkcs8_der = 1612 const std::vector<uint8> rsa_pkcs8_der =
1679 GetBytesFromHexString(test, "rsa_pkcs8_der"); 1613 GetBytesFromHexString(test, "rsa_pkcs8_der");
1680 const std::vector<uint8> ciphertext = 1614 const std::vector<uint8> ciphertext =
1681 GetBytesFromHexString(test, "ciphertext"); 1615 GetBytesFromHexString(test, "ciphertext");
1682 const std::vector<uint8> cleartext = 1616 const std::vector<uint8> cleartext =
1683 GetBytesFromHexString(test, "cleartext"); 1617 GetBytesFromHexString(test, "cleartext");
1684 1618
1685 // Import the key pair. 1619 // Import the key pair.
1686 blink::WebCryptoAlgorithm algorithm = 1620 blink::WebCryptoAlgorithm algorithm =
1687 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1621 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1688 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1622 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1689 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1623 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1690 ImportRsaKeyPair( 1624 ImportRsaKeyPair(
1691 rsa_spki_der, 1625 rsa_spki_der,
1692 rsa_pkcs8_der, 1626 rsa_pkcs8_der,
1693 algorithm, 1627 algorithm,
1694 false, 1628 false,
1695 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 1629 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
1696 &public_key, 1630 &public_key,
1697 &private_key); 1631 &private_key);
1698 1632
1699 // Decrypt the known-good ciphertext with the private key. As a check we must 1633 // Decrypt the known-good ciphertext with the private key. As a check we must
1700 // get the known original cleartext. 1634 // get the known original cleartext.
1701 blink::WebArrayBuffer decrypted_data; 1635 blink::WebArrayBuffer decrypted_data;
1702 ASSERT_STATUS_SUCCESS(DecryptInternal( 1636 ASSERT_STATUS_SUCCESS(DecryptInternal(
1703 algorithm, 1637 algorithm,
1704 private_key, 1638 private_key,
1705 ciphertext, 1639 ciphertext,
1706 &decrypted_data)); 1640 &decrypted_data));
1707 EXPECT_FALSE(decrypted_data.isNull()); 1641 EXPECT_FALSE(decrypted_data.isNull());
1708 ExpectArrayBufferMatches(cleartext, decrypted_data); 1642 ExpectArrayBufferMatches(cleartext, decrypted_data);
1709 1643
1710 // Encrypt this decrypted data with the public key. 1644 // Encrypt this decrypted data with the public key.
1711 blink::WebArrayBuffer encrypted_data; 1645 blink::WebArrayBuffer encrypted_data;
1712 ASSERT_STATUS_SUCCESS(EncryptInternal( 1646 ASSERT_STATUS_SUCCESS(Encrypt(
1713 algorithm, 1647 algorithm,
1714 public_key, 1648 public_key,
1715 reinterpret_cast<const unsigned char*>(decrypted_data.data()), 1649 CryptoData(decrypted_data),
1716 decrypted_data.byteLength(),
1717 &encrypted_data)); 1650 &encrypted_data));
1718 EXPECT_EQ(128u, encrypted_data.byteLength()); 1651 EXPECT_EQ(128u, encrypted_data.byteLength());
1719 1652
1720 // Finally, decrypt the newly encrypted result with the private key, and 1653 // Finally, decrypt the newly encrypted result with the private key, and
1721 // compare to the known original cleartext. 1654 // compare to the known original cleartext.
1722 decrypted_data.reset(); 1655 decrypted_data.reset();
1723 ASSERT_STATUS_SUCCESS(DecryptInternal( 1656 ASSERT_STATUS_SUCCESS(Decrypt(
1724 algorithm, 1657 algorithm,
1725 private_key, 1658 private_key,
1726 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1659 CryptoData(encrypted_data),
1727 encrypted_data.byteLength(),
1728 &decrypted_data)); 1660 &decrypted_data));
1729 EXPECT_FALSE(decrypted_data.isNull()); 1661 EXPECT_FALSE(decrypted_data.isNull());
1730 ExpectArrayBufferMatches(cleartext, decrypted_data); 1662 ExpectArrayBufferMatches(cleartext, decrypted_data);
1731 } 1663 }
1732 1664
1733 TEST_F(WebCryptoImplTest, MAYBE(RsaEsFailures)) { 1665 TEST_F(SharedCryptoTest, MAYBE(RsaEsFailures)) {
1734 // Import a key pair. 1666 // Import a key pair.
1735 blink::WebCryptoAlgorithm algorithm = 1667 blink::WebCryptoAlgorithm algorithm =
1736 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1668 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1737 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1669 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1738 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1670 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1739 ImportRsaKeyPair( 1671 ImportRsaKeyPair(
1740 HexStringToBytes(kPublicKeySpkiDerHex), 1672 HexStringToBytes(kPublicKeySpkiDerHex),
1741 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1673 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1742 algorithm, 1674 algorithm,
1743 false, 1675 false,
1744 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt, 1676 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt,
1745 &public_key, 1677 &public_key,
1746 &private_key); 1678 &private_key);
(...skipping 18 matching lines...) Expand all
1765 public_key, 1697 public_key,
1766 std::vector<uint8>(kMaxMsgSizeBytes + 1, '0'), 1698 std::vector<uint8>(kMaxMsgSizeBytes + 1, '0'),
1767 &encrypted_data)); 1699 &encrypted_data));
1768 1700
1769 // Generate encrypted data. 1701 // Generate encrypted data.
1770 EXPECT_STATUS(Status::Success(), 1702 EXPECT_STATUS(Status::Success(),
1771 EncryptInternal(algorithm, public_key, message_hex, &encrypted_data)); 1703 EncryptInternal(algorithm, public_key, message_hex, &encrypted_data));
1772 1704
1773 // Fail decrypt with a public key. 1705 // Fail decrypt with a public key.
1774 blink::WebArrayBuffer decrypted_data; 1706 blink::WebArrayBuffer decrypted_data;
1775 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), DecryptInternal( 1707 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), Decrypt(
1776 algorithm, 1708 algorithm,
1777 public_key, 1709 public_key,
1778 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1710 CryptoData(encrypted_data),
1779 encrypted_data.byteLength(),
1780 &decrypted_data)); 1711 &decrypted_data));
1781 1712
1782 // Corrupt encrypted data; ensure decrypt fails because padding was disrupted. 1713 // Corrupt encrypted data; ensure decrypt fails because padding was disrupted.
1783 std::vector<uint8> corrupted_data( 1714 std::vector<uint8> corrupted_data(
1784 static_cast<uint8*>(encrypted_data.data()), 1715 static_cast<uint8*>(encrypted_data.data()),
1785 static_cast<uint8*>(encrypted_data.data()) + encrypted_data.byteLength()); 1716 static_cast<uint8*>(encrypted_data.data()) + encrypted_data.byteLength());
1786 corrupted_data[corrupted_data.size() / 2] ^= 0x01; 1717 corrupted_data[corrupted_data.size() / 2] ^= 0x01;
1787 EXPECT_STATUS(Status::Error(), 1718 EXPECT_STATUS(Status::Error(),
1788 DecryptInternal(algorithm, private_key, corrupted_data, &decrypted_data)); 1719 DecryptInternal(algorithm, private_key, corrupted_data, &decrypted_data));
1789 1720
1790 // TODO(padolph): Are there other specific data corruption scenarios to 1721 // TODO(padolph): Are there other specific data corruption scenarios to
1791 // consider? 1722 // consider?
1792 1723
1793 // Do a successful decrypt with good data just for confirmation. 1724 // Do a successful decrypt with good data just for confirmation.
1794 EXPECT_STATUS_SUCCESS(DecryptInternal( 1725 EXPECT_STATUS_SUCCESS(Decrypt(
1795 algorithm, 1726 algorithm,
1796 private_key, 1727 private_key,
1797 reinterpret_cast<const unsigned char*>(encrypted_data.data()), 1728 CryptoData(encrypted_data),
1798 encrypted_data.byteLength(),
1799 &decrypted_data)); 1729 &decrypted_data));
1800 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data); 1730 ExpectArrayBufferMatchesHex(message_hex_str, decrypted_data);
1801 } 1731 }
1802 1732
1803 TEST_F(WebCryptoImplTest, MAYBE(RsaSsaSignVerifyFailures)) { 1733 TEST_F(SharedCryptoTest, MAYBE(RsaSsaSignVerifyFailures)) {
1804 // Import a key pair. 1734 // Import a key pair.
1805 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( 1735 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash(
1806 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1736 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1807 blink::WebCryptoAlgorithmIdSha1); 1737 blink::WebCryptoAlgorithmIdSha1);
1808 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1738 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1809 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1739 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1810 ImportRsaKeyPair( 1740 ImportRsaKeyPair(
1811 HexStringToBytes(kPublicKeySpkiDerHex), 1741 HexStringToBytes(kPublicKeySpkiDerHex),
1812 HexStringToBytes(kPrivateKeyPkcs8DerHex), 1742 HexStringToBytes(kPrivateKeyPkcs8DerHex),
1813 algorithm, 1743 algorithm,
1814 false, 1744 false,
1815 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, 1745 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify,
1816 &public_key, 1746 &public_key,
1817 &private_key); 1747 &private_key);
1818 1748
1819 blink::WebArrayBuffer signature; 1749 blink::WebArrayBuffer signature;
1820 bool signature_match; 1750 bool signature_match;
1821 1751
1822 // Compute a signature. 1752 // Compute a signature.
1823 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); 1753 const std::vector<uint8> data = HexStringToBytes("010203040506070809");
1824 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, private_key, data, &signature)); 1754 ASSERT_STATUS_SUCCESS(SignInternal(algorithm, private_key, data, &signature));
1825 1755
1826 // Ensure truncated signature does not verify by passing one less byte. 1756 // Ensure truncated signature does not verify by passing one less byte.
1827 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1757 EXPECT_STATUS_SUCCESS(VerifySignature(
1828 algorithm, 1758 algorithm,
1829 public_key, 1759 public_key,
1830 static_cast<const unsigned char*>(signature.data()), 1760 CryptoData(reinterpret_cast<const unsigned char*>(signature.data()),
1831 signature.byteLength() - 1, 1761 signature.byteLength() - 1),
1832 data, 1762 CryptoData(data),
1833 &signature_match)); 1763 &signature_match));
1834 EXPECT_FALSE(signature_match); 1764 EXPECT_FALSE(signature_match);
1835 1765
1836 // Ensure truncated signature does not verify by passing no bytes. 1766 // Ensure truncated signature does not verify by passing no bytes.
1837 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1767 EXPECT_STATUS_SUCCESS(VerifySignature(
1838 algorithm, 1768 algorithm,
1839 public_key, 1769 public_key,
1840 NULL, 1770 CryptoData(),
1841 0, 1771 CryptoData(data),
1842 data,
1843 &signature_match)); 1772 &signature_match));
1844 EXPECT_FALSE(signature_match); 1773 EXPECT_FALSE(signature_match);
1845 1774
1846 // Ensure corrupted signature does not verify. 1775 // Ensure corrupted signature does not verify.
1847 std::vector<uint8> corrupt_sig( 1776 std::vector<uint8> corrupt_sig(
1848 static_cast<uint8*>(signature.data()), 1777 static_cast<uint8*>(signature.data()),
1849 static_cast<uint8*>(signature.data()) + signature.byteLength()); 1778 static_cast<uint8*>(signature.data()) + signature.byteLength());
1850 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1; 1779 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1;
1851 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1780 EXPECT_STATUS_SUCCESS(VerifySignatureInternal(
1852 algorithm, 1781 algorithm,
1853 public_key, 1782 public_key,
1854 webcrypto::Uint8VectorStart(corrupt_sig), 1783 CryptoData(corrupt_sig),
1855 corrupt_sig.size(),
1856 data, 1784 data,
1857 &signature_match)); 1785 &signature_match));
1858 EXPECT_FALSE(signature_match); 1786 EXPECT_FALSE(signature_match);
1859 1787
1860 // Ensure signatures that are greater than the modulus size fail. 1788 // Ensure signatures that are greater than the modulus size fail.
1861 const unsigned int long_message_size_bytes = 1024; 1789 const unsigned int long_message_size_bytes = 1024;
1862 DCHECK_GT(long_message_size_bytes, kModulusLength/8); 1790 DCHECK_GT(long_message_size_bytes, kModulusLength/8);
1863 const unsigned char kLongSignature[long_message_size_bytes] = { 0 }; 1791 const unsigned char kLongSignature[long_message_size_bytes] = { 0 };
1864 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1792 EXPECT_STATUS_SUCCESS(VerifySignature(
1865 algorithm, 1793 algorithm,
1866 public_key, 1794 public_key,
1867 kLongSignature, 1795 CryptoData(kLongSignature, sizeof(kLongSignature)),
1868 sizeof(kLongSignature), 1796 CryptoData(data),
1869 data,
1870 &signature_match)); 1797 &signature_match));
1871 EXPECT_FALSE(signature_match); 1798 EXPECT_FALSE(signature_match);
1872 1799
1873 // Ensure that verifying using a private key, rather than a public key, fails. 1800 // Ensure that verifying using a private key, rather than a public key, fails.
1874 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), VerifySignatureInternal( 1801 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), VerifySignature(
1875 algorithm, 1802 algorithm,
1876 private_key, 1803 private_key,
1877 static_cast<const unsigned char*>(signature.data()), 1804 CryptoData(signature),
1878 signature.byteLength(), 1805 CryptoData(data),
1879 data,
1880 &signature_match)); 1806 &signature_match));
1881 1807
1882 // Ensure that signing using a public key, rather than a private key, fails. 1808 // Ensure that signing using a public key, rather than a private key, fails.
1883 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(), 1809 EXPECT_STATUS(Status::ErrorUnexpectedKeyType(),
1884 SignInternal(algorithm, public_key, data, &signature)); 1810 SignInternal(algorithm, public_key, data, &signature));
1885 1811
1886 // Ensure that signing and verifying with an incompatible algorithm fails. 1812 // Ensure that signing and verifying with an incompatible algorithm fails.
1887 algorithm = 1813 algorithm =
1888 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5); 1814 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5);
1889 EXPECT_STATUS(Status::ErrorUnsupported(), 1815 EXPECT_STATUS(Status::ErrorUnexpected(),
1890 SignInternal(algorithm, private_key, data, &signature)); 1816 SignInternal(algorithm, private_key, data, &signature));
1891 EXPECT_STATUS(Status::ErrorUnsupported(), VerifySignatureInternal( 1817 EXPECT_STATUS(Status::ErrorUnexpected(), VerifySignature(
1892 algorithm, 1818 algorithm, public_key, CryptoData(signature), CryptoData(data),
1893 public_key,
1894 static_cast<const unsigned char*>(signature.data()),
1895 signature.byteLength(),
1896 data,
1897 &signature_match)); 1819 &signature_match));
1898 1820
1899 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash 1821 // Some crypto libraries (NSS) can automatically select the RSA SSA inner hash
1900 // based solely on the contents of the input signature data. In the Web Crypto 1822 // based solely on the contents of the input signature data. In the Web Crypto
1901 // implementation, the inner hash should be specified uniquely by the input 1823 // implementation, the inner hash should be specified uniquely by the input
1902 // algorithm parameter. To validate this behavior, call Verify with a computed 1824 // algorithm parameter. To validate this behavior, call Verify with a computed
1903 // signature that used one hash type (SHA-1), but pass in an algorithm with a 1825 // signature that used one hash type (SHA-1), but pass in an algorithm with a
1904 // different inner hash type (SHA-256). If the hash type is determined by the 1826 // different inner hash type (SHA-256). If the hash type is determined by the
1905 // signature itself (undesired), the verify will pass, while if the hash type 1827 // signature itself (undesired), the verify will pass, while if the hash type
1906 // is specified by the input algorithm (desired), the verify will fail. 1828 // is specified by the input algorithm (desired), the verify will fail.
1907 1829
1908 // Compute a signature using SHA-1 as the inner hash. 1830 // Compute a signature using SHA-1 as the inner hash.
1909 EXPECT_STATUS_SUCCESS(SignInternal(CreateRsaAlgorithmWithInnerHash( 1831 EXPECT_STATUS_SUCCESS(SignInternal(CreateRsaAlgorithmWithInnerHash(
1910 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1832 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1911 blink::WebCryptoAlgorithmIdSha1), 1833 blink::WebCryptoAlgorithmIdSha1),
1912 private_key, 1834 private_key,
1913 data, 1835 data,
1914 &signature)); 1836 &signature));
1915 1837
1916 // Now verify using an algorithm whose inner hash is SHA-256, not SHA-1. The 1838 // Now verify using an algorithm whose inner hash is SHA-256, not SHA-1. The
1917 // signature should not verify. 1839 // signature should not verify.
1918 // NOTE: public_key was produced by generateKey, and so its associated 1840 // NOTE: public_key was produced by generateKey, and so its associated
1919 // algorithm has WebCryptoRsaKeyGenParams and not WebCryptoRsaSsaParams. Thus 1841 // algorithm has WebCryptoRsaKeyGenParams and not WebCryptoRsaSsaParams. Thus
1920 // it has no inner hash to conflict with the input algorithm. 1842 // it has no inner hash to conflict with the input algorithm.
1921 bool is_match; 1843 bool is_match;
1922 EXPECT_STATUS_SUCCESS(VerifySignatureInternal( 1844 EXPECT_STATUS_SUCCESS(VerifySignature(
1923 CreateRsaAlgorithmWithInnerHash( 1845 CreateRsaAlgorithmWithInnerHash(
1924 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1846 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1925 blink::WebCryptoAlgorithmIdSha256), 1847 blink::WebCryptoAlgorithmIdSha256),
1926 public_key, 1848 public_key,
1927 static_cast<const unsigned char*>(signature.data()), 1849 CryptoData(signature),
1928 signature.byteLength(), 1850 CryptoData(data),
1929 data,
1930 &is_match)); 1851 &is_match));
1931 EXPECT_FALSE(is_match); 1852 EXPECT_FALSE(is_match);
1932 } 1853 }
1933 1854
1934 TEST_F(WebCryptoImplTest, MAYBE(RsaSignVerifyKnownAnswer)) { 1855 TEST_F(SharedCryptoTest, MAYBE(RsaSignVerifyKnownAnswer)) {
1935 scoped_ptr<base::ListValue> tests; 1856 scoped_ptr<base::ListValue> tests;
1936 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests)); 1857 ASSERT_TRUE(ReadJsonTestFileToList("pkcs1v15_sign.json", &tests));
1937 1858
1938 // Import the key pair. 1859 // Import the key pair.
1939 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash( 1860 blink::WebCryptoAlgorithm algorithm = CreateRsaAlgorithmWithInnerHash(
1940 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 1861 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
1941 blink::WebCryptoAlgorithmIdSha1); 1862 blink::WebCryptoAlgorithmIdSha1);
1942 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1863 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1943 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 1864 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
1944 ImportRsaKeyPair( 1865 ImportRsaKeyPair(
(...skipping 27 matching lines...) Expand all
1972 ASSERT_STATUS_SUCCESS(VerifySignatureInternal( 1893 ASSERT_STATUS_SUCCESS(VerifySignatureInternal(
1973 algorithm, 1894 algorithm,
1974 public_key, 1895 public_key,
1975 test_signature, 1896 test_signature,
1976 test_message, 1897 test_message,
1977 &is_match)); 1898 &is_match));
1978 EXPECT_TRUE(is_match); 1899 EXPECT_TRUE(is_match);
1979 } 1900 }
1980 } 1901 }
1981 1902
1982 TEST_F(WebCryptoImplTest, MAYBE(AesKwKeyImport)) { 1903 TEST_F(SharedCryptoTest, MAYBE(AesKwKeyImport)) {
1983 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1904 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1984 blink::WebCryptoAlgorithm algorithm = 1905 blink::WebCryptoAlgorithm algorithm =
1985 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 1906 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
1986 1907
1987 // Import a 128-bit Key Encryption Key (KEK) 1908 // Import a 128-bit Key Encryption Key (KEK)
1988 std::string key_raw_hex_in = "025a8cf3f08b4f6c5f33bbc76a471939"; 1909 std::string key_raw_hex_in = "025a8cf3f08b4f6c5f33bbc76a471939";
1989 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1910 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
1990 HexStringToBytes(key_raw_hex_in), 1911 HexStringToBytes(key_raw_hex_in),
1991 algorithm, 1912 algorithm,
1992 true, 1913 true,
1993 blink::WebCryptoKeyUsageWrapKey, 1914 blink::WebCryptoKeyUsageWrapKey,
1994 &key)); 1915 &key));
1995 blink::WebArrayBuffer key_raw_out; 1916 blink::WebArrayBuffer key_raw_out;
1996 EXPECT_STATUS_SUCCESS(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, 1917 EXPECT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw,
1997 key, 1918 key,
1998 &key_raw_out)); 1919 &key_raw_out));
1999 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out); 1920 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out);
2000 1921
2001 // Import a 192-bit KEK 1922 // Import a 192-bit KEK
2002 key_raw_hex_in = "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103"; 1923 key_raw_hex_in = "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103";
2003 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1924 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
2004 HexStringToBytes(key_raw_hex_in), 1925 HexStringToBytes(key_raw_hex_in),
2005 algorithm, 1926 algorithm,
2006 true, 1927 true,
2007 blink::WebCryptoKeyUsageWrapKey, 1928 blink::WebCryptoKeyUsageWrapKey,
2008 &key)); 1929 &key));
2009 EXPECT_STATUS_SUCCESS(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, 1930 EXPECT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw,
2010 key, 1931 key,
2011 &key_raw_out)); 1932 &key_raw_out));
2012 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out); 1933 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out);
2013 1934
2014 // Import a 256-bit Key Encryption Key (KEK) 1935 // Import a 256-bit Key Encryption Key (KEK)
2015 key_raw_hex_in = 1936 key_raw_hex_in =
2016 "e11fe66380d90fa9ebefb74e0478e78f95664d0c67ca20ce4a0b5842863ac46f"; 1937 "e11fe66380d90fa9ebefb74e0478e78f95664d0c67ca20ce4a0b5842863ac46f";
2017 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1938 ASSERT_STATUS_SUCCESS(ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
2018 HexStringToBytes(key_raw_hex_in), 1939 HexStringToBytes(key_raw_hex_in),
2019 algorithm, 1940 algorithm,
2020 true, 1941 true,
2021 blink::WebCryptoKeyUsageWrapKey, 1942 blink::WebCryptoKeyUsageWrapKey,
2022 &key)); 1943 &key));
2023 EXPECT_STATUS_SUCCESS(ExportKeyInternal(blink::WebCryptoKeyFormatRaw, 1944 EXPECT_STATUS_SUCCESS(ExportKey(blink::WebCryptoKeyFormatRaw,
2024 key, 1945 key,
2025 &key_raw_out)); 1946 &key_raw_out));
2026 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out); 1947 ExpectArrayBufferMatchesHex(key_raw_hex_in, key_raw_out);
2027 1948
2028 // Fail import of 0 length key 1949 // Fail import of 0 length key
2029 EXPECT_STATUS(Status::Error(), 1950 EXPECT_STATUS(Status::Error(),
2030 ImportKeyInternal(blink::WebCryptoKeyFormatRaw, 1951 ImportKeyInternal(blink::WebCryptoKeyFormatRaw,
2031 HexStringToBytes(""), 1952 HexStringToBytes(""),
2032 algorithm, 1953 algorithm,
2033 true, 1954 true,
2034 blink::WebCryptoKeyUsageWrapKey, 1955 blink::WebCryptoKeyUsageWrapKey,
2035 &key)); 1956 &key));
(...skipping 27 matching lines...) Expand all
2063 algorithm, 1984 algorithm,
2064 true, 1985 true,
2065 blink::WebCryptoKeyUsageWrapKey, 1986 blink::WebCryptoKeyUsageWrapKey,
2066 &key)); 1987 &key));
2067 } 1988 }
2068 1989
2069 // TODO(eroman): 1990 // TODO(eroman):
2070 // * Test decryption when the tag length exceeds input size 1991 // * Test decryption when the tag length exceeds input size
2071 // * Test decryption with empty input 1992 // * Test decryption with empty input
2072 // * Test decryption with tag length of 0. 1993 // * Test decryption with tag length of 0.
2073 TEST_F(WebCryptoImplTest, MAYBE(AesGcmSampleSets)) { 1994 TEST_F(SharedCryptoTest, MAYBE(AesGcmSampleSets)) {
2074 // Some Linux test runners may not have a new enough version of NSS. 1995 // Some Linux test runners may not have a new enough version of NSS.
2075 if (!SupportsAesGcm()) { 1996 if (!SupportsAesGcm()) {
2076 LOG(WARNING) << "AES GCM not supported, skipping tests"; 1997 LOG(WARNING) << "AES GCM not supported, skipping tests";
2077 return; 1998 return;
2078 } 1999 }
2079 2000
2080 scoped_ptr<base::ListValue> tests; 2001 scoped_ptr<base::ListValue> tests;
2081 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests)); 2002 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests));
2082 2003
2083 // Note that WebCrypto appends the authentication tag to the ciphertext. 2004 // Note that WebCrypto appends the authentication tag to the ciphertext.
2084 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 2005 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
2085 SCOPED_TRACE(test_index); 2006 SCOPED_TRACE(test_index);
2086 base::DictionaryValue* test; 2007 base::DictionaryValue* test;
2087 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 2008 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
2088 2009
2089 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 2010 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key");
2090 const std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); 2011 const std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv");
2091 const std::vector<uint8> test_additional_data = 2012 const std::vector<uint8> test_additional_data =
2092 GetBytesFromHexString(test, "additional_data"); 2013 GetBytesFromHexString(test, "additional_data");
2093 const std::vector<uint8> test_plain_text = 2014 const std::vector<uint8> test_plain_text =
2094 GetBytesFromHexString(test, "plain_text"); 2015 GetBytesFromHexString(test, "plain_text");
2095 const std::vector<uint8> test_authentication_tag = 2016 const std::vector<uint8> test_authentication_tag =
2096 GetBytesFromHexString(test, "authentication_tag"); 2017 GetBytesFromHexString(test, "authentication_tag");
2097 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8; 2018 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8;
2098 const std::vector<uint8> test_cipher_text = 2019 const std::vector<uint8> test_cipher_text =
2099 GetBytesFromHexString(test, "cipher_text"); 2020 GetBytesFromHexString(test, "cipher_text");
2100 2021
2101 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 2022 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
2102 test_key, 2023 test_key,
2103 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), 2024 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
2104 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 2025 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
2105 2026
2106 // Verify exported raw key is identical to the imported data 2027 // Verify exported raw key is identical to the imported data
2107 blink::WebArrayBuffer raw_key; 2028 blink::WebArrayBuffer raw_key;
2108 EXPECT_STATUS_SUCCESS(ExportKeyInternal( 2029 EXPECT_STATUS_SUCCESS(ExportKey(
2109 blink::WebCryptoKeyFormatRaw, key, &raw_key)); 2030 blink::WebCryptoKeyFormatRaw, key, &raw_key));
2110 2031
2111 ExpectArrayBufferMatches(test_key, raw_key); 2032 ExpectArrayBufferMatches(test_key, raw_key);
2112 2033
2113 // Test encryption. 2034 // Test encryption.
2114 std::vector<uint8> cipher_text; 2035 std::vector<uint8> cipher_text;
2115 std::vector<uint8> authentication_tag; 2036 std::vector<uint8> authentication_tag;
2116 EXPECT_STATUS_SUCCESS(AesGcmEncrypt(key, test_iv, test_additional_data, 2037 EXPECT_STATUS_SUCCESS(AesGcmEncrypt(key, test_iv, test_additional_data,
2117 test_tag_size_bits, test_plain_text, 2038 test_tag_size_bits, test_plain_text,
2118 &cipher_text, &authentication_tag)); 2039 &cipher_text, &authentication_tag));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2152 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i]; 2073 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i];
2153 if (test_tag_size_bits == wrong_tag_size_bits) 2074 if (test_tag_size_bits == wrong_tag_size_bits)
2154 continue; 2075 continue;
2155 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data, 2076 EXPECT_STATUS_ERROR(AesGcmDecrypt(key, test_iv, test_additional_data,
2156 wrong_tag_size_bits, test_cipher_text, 2077 wrong_tag_size_bits, test_cipher_text,
2157 test_authentication_tag, &plain_text)); 2078 test_authentication_tag, &plain_text));
2158 } 2079 }
2159 } 2080 }
2160 } 2081 }
2161 2082
2083 } // namespace webcrypto
2084
2162 } // namespace content 2085 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/webcrypto/shared_crypto.cc ('k') | content/renderer/webcrypto/webcrypto_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698