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

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

Powered by Google App Engine
This is Rietveld 408576698