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

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

Issue 404733005: Replace uses of uint8 with uint8_t. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase onto master Created 6 years, 5 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
« no previous file with comments | « content/child/webcrypto/platform_crypto.h ('k') | content/child/webcrypto/structured_clone.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <algorithm> 5 #include <algorithm>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 } 96 }
97 97
98 namespace { 98 namespace {
99 99
100 // ----------------------------------------------------------------------------- 100 // -----------------------------------------------------------------------------
101 101
102 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a 102 // TODO(eroman): For Linux builds using system NSS, AES-GCM support is a
103 // runtime dependency. Test it by trying to import a key. 103 // runtime dependency. Test it by trying to import a key.
104 // TODO(padolph): Consider caching the result of the import key test. 104 // TODO(padolph): Consider caching the result of the import key test.
105 bool SupportsAesGcm() { 105 bool SupportsAesGcm() {
106 std::vector<uint8> key_raw(16, 0); 106 std::vector<uint8_t> key_raw(16, 0);
107 107
108 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 108 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
109 Status status = ImportKey(blink::WebCryptoKeyFormatRaw, 109 Status status = ImportKey(blink::WebCryptoKeyFormatRaw,
110 CryptoData(key_raw), 110 CryptoData(key_raw),
111 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), 111 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
112 true, 112 true,
113 blink::WebCryptoKeyUsageEncrypt, 113 blink::WebCryptoKeyUsageEncrypt,
114 &key); 114 &key);
115 115
116 if (status.IsError()) 116 if (status.IsError())
(...skipping 26 matching lines...) Expand all
143 return false; 143 return false;
144 } 144 }
145 #endif 145 #endif
146 return true; 146 return true;
147 } 147 }
148 148
149 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm( 149 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm(
150 blink::WebCryptoAlgorithmId algorithm_id, 150 blink::WebCryptoAlgorithmId algorithm_id,
151 const blink::WebCryptoAlgorithmId hash_id, 151 const blink::WebCryptoAlgorithmId hash_id,
152 unsigned int modulus_length, 152 unsigned int modulus_length,
153 const std::vector<uint8>& public_exponent) { 153 const std::vector<uint8_t>& public_exponent) {
154 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || 154 DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
155 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); 155 algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep);
156 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); 156 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id));
157 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 157 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
158 algorithm_id, 158 algorithm_id,
159 new blink::WebCryptoRsaHashedKeyGenParams( 159 new blink::WebCryptoRsaHashedKeyGenParams(
160 CreateAlgorithm(hash_id), 160 CreateAlgorithm(hash_id),
161 modulus_length, 161 modulus_length,
162 webcrypto::Uint8VectorStart(public_exponent), 162 webcrypto::Uint8VectorStart(public_exponent),
163 public_exponent.size())); 163 public_exponent.size()));
164 } 164 }
165 165
166 // Creates an RSA-OAEP algorithm 166 // Creates an RSA-OAEP algorithm
167 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm( 167 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
168 const std::vector<uint8>& label) { 168 const std::vector<uint8_t>& label) {
169 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 169 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
170 blink::WebCryptoAlgorithmIdRsaOaep, 170 blink::WebCryptoAlgorithmIdRsaOaep,
171 new blink::WebCryptoRsaOaepParams( 171 new blink::WebCryptoRsaOaepParams(
172 !label.empty(), Uint8VectorStart(label), label.size())); 172 !label.empty(), Uint8VectorStart(label), label.size()));
173 } 173 }
174 174
175 // Creates an AES-CBC algorithm. 175 // Creates an AES-CBC algorithm.
176 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(const std::vector<uint8>& iv) { 176 blink::WebCryptoAlgorithm CreateAesCbcAlgorithm(
177 const std::vector<uint8_t>& iv) {
177 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 178 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
178 blink::WebCryptoAlgorithmIdAesCbc, 179 blink::WebCryptoAlgorithmIdAesCbc,
179 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size())); 180 new blink::WebCryptoAesCbcParams(Uint8VectorStart(iv), iv.size()));
180 } 181 }
181 182
182 // Creates an AES-GCM algorithm. 183 // Creates an AES-GCM algorithm.
183 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm( 184 blink::WebCryptoAlgorithm CreateAesGcmAlgorithm(
184 const std::vector<uint8>& iv, 185 const std::vector<uint8_t>& iv,
185 const std::vector<uint8>& additional_data, 186 const std::vector<uint8_t>& additional_data,
186 unsigned int tag_length_bits) { 187 unsigned int tag_length_bits) {
187 EXPECT_TRUE(SupportsAesGcm()); 188 EXPECT_TRUE(SupportsAesGcm());
188 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 189 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
189 blink::WebCryptoAlgorithmIdAesGcm, 190 blink::WebCryptoAlgorithmIdAesGcm,
190 new blink::WebCryptoAesGcmParams(Uint8VectorStart(iv), 191 new blink::WebCryptoAesGcmParams(Uint8VectorStart(iv),
191 iv.size(), 192 iv.size(),
192 true, 193 true,
193 Uint8VectorStart(additional_data), 194 Uint8VectorStart(additional_data),
194 additional_data.size(), 195 additional_data.size(),
195 true, 196 true,
(...skipping 11 matching lines...) Expand all
207 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( 208 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
208 blink::WebCryptoAlgorithmIdHmac, 209 blink::WebCryptoAlgorithmIdHmac,
209 new blink::WebCryptoHmacKeyGenParams( 210 new blink::WebCryptoHmacKeyGenParams(
210 CreateAlgorithm(hash_id), (key_length_bits != 0), key_length_bits)); 211 CreateAlgorithm(hash_id), (key_length_bits != 0), key_length_bits));
211 } 212 }
212 213
213 // Returns a slightly modified version of the input vector. 214 // Returns a slightly modified version of the input vector.
214 // 215 //
215 // - For non-empty inputs a single bit is inverted. 216 // - For non-empty inputs a single bit is inverted.
216 // - For empty inputs, a byte is added. 217 // - For empty inputs, a byte is added.
217 std::vector<uint8> Corrupted(const std::vector<uint8>& input) { 218 std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input) {
218 std::vector<uint8> corrupted_data(input); 219 std::vector<uint8_t> corrupted_data(input);
219 if (corrupted_data.empty()) 220 if (corrupted_data.empty())
220 corrupted_data.push_back(0); 221 corrupted_data.push_back(0);
221 corrupted_data[corrupted_data.size() / 2] ^= 0x01; 222 corrupted_data[corrupted_data.size() / 2] ^= 0x01;
222 return corrupted_data; 223 return corrupted_data;
223 } 224 }
224 225
225 std::vector<uint8> HexStringToBytes(const std::string& hex) { 226 std::vector<uint8_t> HexStringToBytes(const std::string& hex) {
226 std::vector<uint8> bytes; 227 std::vector<uint8_t> bytes;
227 base::HexStringToBytes(hex, &bytes); 228 base::HexStringToBytes(hex, &bytes);
228 return bytes; 229 return bytes;
229 } 230 }
230 231
231 std::vector<uint8> MakeJsonVector(const std::string& json_string) { 232 std::vector<uint8_t> MakeJsonVector(const std::string& json_string) {
232 return std::vector<uint8>(json_string.begin(), json_string.end()); 233 return std::vector<uint8_t>(json_string.begin(), json_string.end());
233 } 234 }
234 235
235 std::vector<uint8> MakeJsonVector(const base::DictionaryValue& dict) { 236 std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict) {
236 std::string json; 237 std::string json;
237 base::JSONWriter::Write(&dict, &json); 238 base::JSONWriter::Write(&dict, &json);
238 return MakeJsonVector(json); 239 return MakeJsonVector(json);
239 } 240 }
240 241
241 // ---------------------------------------------------------------- 242 // ----------------------------------------------------------------
242 // Helpers for working with JSON data files for test expectations. 243 // Helpers for working with JSON data files for test expectations.
243 // ---------------------------------------------------------------- 244 // ----------------------------------------------------------------
244 245
245 // Reads a file in "src/content/test/data/webcrypto" to a base::Value. 246 // Reads a file in "src/content/test/data/webcrypto" to a base::Value.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 ignore_result(json.release()); 293 ignore_result(json.release());
293 294
294 return ::testing::AssertionSuccess(); 295 return ::testing::AssertionSuccess();
295 } 296 }
296 297
297 // Read a string property from the dictionary with path |property_name| 298 // Read a string property from the dictionary with path |property_name|
298 // (which can include periods for nested dictionaries). Interprets the 299 // (which can include periods for nested dictionaries). Interprets the
299 // string as a hex encoded string and converts it to a bytes list. 300 // string as a hex encoded string and converts it to a bytes list.
300 // 301 //
301 // Returns empty vector on failure. 302 // Returns empty vector on failure.
302 std::vector<uint8> GetBytesFromHexString(base::DictionaryValue* dict, 303 std::vector<uint8_t> GetBytesFromHexString(base::DictionaryValue* dict,
303 const char* property_name) { 304 const char* property_name) {
304 std::string hex_string; 305 std::string hex_string;
305 if (!dict->GetString(property_name, &hex_string)) { 306 if (!dict->GetString(property_name, &hex_string)) {
306 EXPECT_TRUE(false) << "Couldn't get string property: " << property_name; 307 EXPECT_TRUE(false) << "Couldn't get string property: " << property_name;
307 return std::vector<uint8>(); 308 return std::vector<uint8_t>();
308 } 309 }
309 310
310 return HexStringToBytes(hex_string); 311 return HexStringToBytes(hex_string);
311 } 312 }
312 313
313 // Reads a string property with path "property_name" and converts it to a 314 // Reads a string property with path "property_name" and converts it to a
314 // WebCryptoAlgorith. Returns null algorithm on failure. 315 // WebCryptoAlgorith. Returns null algorithm on failure.
315 blink::WebCryptoAlgorithm GetDigestAlgorithm(base::DictionaryValue* dict, 316 blink::WebCryptoAlgorithm GetDigestAlgorithm(base::DictionaryValue* dict,
316 const char* property_name) { 317 const char* property_name) {
317 std::string algorithm_name; 318 std::string algorithm_name;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 "n", 362 "n",
362 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk" 363 "qLOyhK-OtQs4cDSoYPFGxJGfMYdjzWxVmMiuSBGh4KvEx-CwgtaTpef87Wdc9GaFEncsDLxk"
363 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm" 364 "p0LGxjD1M8jMcvYq6DPEC_JYQumEu3i9v5fAEH1VvbZi9cTg-rmEXLUUjvc5LdOq_5OuHmtm"
364 "e7PUJHYW1PW6ENTP0ibeiNOfFvs"); 365 "e7PUJHYW1PW6ENTP0ibeiNOfFvs");
365 dict->SetString("e", "AQAB"); 366 dict->SetString("e", "AQAB");
366 } 367 }
367 368
368 // Returns true if any of the vectors in the input list have identical content. 369 // Returns true if any of the vectors in the input list have identical content.
369 // Dumb O(n^2) implementation but should be fast enough for the input sizes that 370 // Dumb O(n^2) implementation but should be fast enough for the input sizes that
370 // are used. 371 // are used.
371 bool CopiesExist(const std::vector<std::vector<uint8> >& bufs) { 372 bool CopiesExist(const std::vector<std::vector<uint8_t> >& bufs) {
372 for (size_t i = 0; i < bufs.size(); ++i) { 373 for (size_t i = 0; i < bufs.size(); ++i) {
373 for (size_t j = i + 1; j < bufs.size(); ++j) { 374 for (size_t j = i + 1; j < bufs.size(); ++j) {
374 if (CryptoData(bufs[i]) == CryptoData(bufs[j])) 375 if (CryptoData(bufs[i]) == CryptoData(bufs[j]))
375 return true; 376 return true;
376 } 377 }
377 } 378 }
378 return false; 379 return false;
379 } 380 }
380 381
381 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( 382 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm(
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B" 446 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B"
446 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138" 447 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138"
447 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137"; 448 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137";
448 const char* const kPublicKeyExponentHex = "010001"; 449 const char* const kPublicKeyExponentHex = "010001";
449 450
450 // TODO(eroman): Remove unnecessary test fixture. 451 // TODO(eroman): Remove unnecessary test fixture.
451 class SharedCryptoTest : public testing::Test { 452 class SharedCryptoTest : public testing::Test {
452 }; 453 };
453 454
454 blink::WebCryptoKey ImportSecretKeyFromRaw( 455 blink::WebCryptoKey ImportSecretKeyFromRaw(
455 const std::vector<uint8>& key_raw, 456 const std::vector<uint8_t>& key_raw,
456 const blink::WebCryptoAlgorithm& algorithm, 457 const blink::WebCryptoAlgorithm& algorithm,
457 blink::WebCryptoKeyUsageMask usage) { 458 blink::WebCryptoKeyUsageMask usage) {
458 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 459 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
459 bool extractable = true; 460 bool extractable = true;
460 EXPECT_EQ(Status::Success(), 461 EXPECT_EQ(Status::Success(),
461 ImportKey(blink::WebCryptoKeyFormatRaw, 462 ImportKey(blink::WebCryptoKeyFormatRaw,
462 CryptoData(key_raw), 463 CryptoData(key_raw),
463 algorithm, 464 algorithm,
464 extractable, 465 extractable,
465 usage, 466 usage,
466 &key)); 467 &key));
467 468
468 EXPECT_FALSE(key.isNull()); 469 EXPECT_FALSE(key.isNull());
469 EXPECT_TRUE(key.handle()); 470 EXPECT_TRUE(key.handle());
470 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 471 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
471 EXPECT_EQ(algorithm.id(), key.algorithm().id()); 472 EXPECT_EQ(algorithm.id(), key.algorithm().id());
472 EXPECT_EQ(extractable, key.extractable()); 473 EXPECT_EQ(extractable, key.extractable());
473 EXPECT_EQ(usage, key.usages()); 474 EXPECT_EQ(usage, key.usages());
474 return key; 475 return key;
475 } 476 }
476 477
477 void ImportRsaKeyPair(const std::vector<uint8>& spki_der, 478 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der,
478 const std::vector<uint8>& pkcs8_der, 479 const std::vector<uint8_t>& pkcs8_der,
479 const blink::WebCryptoAlgorithm& algorithm, 480 const blink::WebCryptoAlgorithm& algorithm,
480 bool extractable, 481 bool extractable,
481 blink::WebCryptoKeyUsageMask public_key_usage_mask, 482 blink::WebCryptoKeyUsageMask public_key_usage_mask,
482 blink::WebCryptoKeyUsageMask private_key_usage_mask, 483 blink::WebCryptoKeyUsageMask private_key_usage_mask,
483 blink::WebCryptoKey* public_key, 484 blink::WebCryptoKey* public_key,
484 blink::WebCryptoKey* private_key) { 485 blink::WebCryptoKey* private_key) {
485 ASSERT_EQ(Status::Success(), 486 ASSERT_EQ(Status::Success(),
486 ImportKey(blink::WebCryptoKeyFormatSpki, 487 ImportKey(blink::WebCryptoKeyFormatSpki,
487 CryptoData(spki_der), 488 CryptoData(spki_der),
488 algorithm, 489 algorithm,
(...skipping 16 matching lines...) Expand all
505 private_key)); 506 private_key));
506 EXPECT_FALSE(private_key->isNull()); 507 EXPECT_FALSE(private_key->isNull());
507 EXPECT_TRUE(private_key->handle()); 508 EXPECT_TRUE(private_key->handle());
508 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); 509 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type());
509 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); 510 EXPECT_EQ(algorithm.id(), private_key->algorithm().id());
510 EXPECT_EQ(extractable, private_key->extractable()); 511 EXPECT_EQ(extractable, private_key->extractable());
511 EXPECT_EQ(private_key_usage_mask, private_key->usages()); 512 EXPECT_EQ(private_key_usage_mask, private_key->usages());
512 } 513 }
513 514
514 Status AesGcmEncrypt(const blink::WebCryptoKey& key, 515 Status AesGcmEncrypt(const blink::WebCryptoKey& key,
515 const std::vector<uint8>& iv, 516 const std::vector<uint8_t>& iv,
516 const std::vector<uint8>& additional_data, 517 const std::vector<uint8_t>& additional_data,
517 unsigned int tag_length_bits, 518 unsigned int tag_length_bits,
518 const std::vector<uint8>& plain_text, 519 const std::vector<uint8_t>& plain_text,
519 std::vector<uint8>* cipher_text, 520 std::vector<uint8_t>* cipher_text,
520 std::vector<uint8>* authentication_tag) { 521 std::vector<uint8_t>* authentication_tag) {
521 EXPECT_TRUE(SupportsAesGcm()); 522 EXPECT_TRUE(SupportsAesGcm());
522 blink::WebCryptoAlgorithm algorithm = 523 blink::WebCryptoAlgorithm algorithm =
523 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits); 524 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits);
524 525
525 std::vector<uint8> output; 526 std::vector<uint8_t> output;
526 Status status = Encrypt(algorithm, key, CryptoData(plain_text), &output); 527 Status status = Encrypt(algorithm, key, CryptoData(plain_text), &output);
527 if (status.IsError()) 528 if (status.IsError())
528 return status; 529 return status;
529 530
530 if ((tag_length_bits % 8) != 0) { 531 if ((tag_length_bits % 8) != 0) {
531 EXPECT_TRUE(false) << "Encrypt should have failed."; 532 EXPECT_TRUE(false) << "Encrypt should have failed.";
532 return Status::OperationError(); 533 return Status::OperationError();
533 } 534 }
534 535
535 size_t tag_length_bytes = tag_length_bits / 8; 536 size_t tag_length_bytes = tag_length_bits / 8;
536 537
537 if (tag_length_bytes > output.size()) { 538 if (tag_length_bytes > output.size()) {
538 EXPECT_TRUE(false) << "tag length is larger than output"; 539 EXPECT_TRUE(false) << "tag length is larger than output";
539 return Status::OperationError(); 540 return Status::OperationError();
540 } 541 }
541 542
542 // The encryption result is cipher text with authentication tag appended. 543 // The encryption result is cipher text with authentication tag appended.
543 cipher_text->assign(output.begin(), 544 cipher_text->assign(output.begin(),
544 output.begin() + (output.size() - tag_length_bytes)); 545 output.begin() + (output.size() - tag_length_bytes));
545 authentication_tag->assign(output.begin() + cipher_text->size(), 546 authentication_tag->assign(output.begin() + cipher_text->size(),
546 output.end()); 547 output.end());
547 548
548 return Status::Success(); 549 return Status::Success();
549 } 550 }
550 551
551 Status AesGcmDecrypt(const blink::WebCryptoKey& key, 552 Status AesGcmDecrypt(const blink::WebCryptoKey& key,
552 const std::vector<uint8>& iv, 553 const std::vector<uint8_t>& iv,
553 const std::vector<uint8>& additional_data, 554 const std::vector<uint8_t>& additional_data,
554 unsigned int tag_length_bits, 555 unsigned int tag_length_bits,
555 const std::vector<uint8>& cipher_text, 556 const std::vector<uint8_t>& cipher_text,
556 const std::vector<uint8>& authentication_tag, 557 const std::vector<uint8_t>& authentication_tag,
557 std::vector<uint8>* plain_text) { 558 std::vector<uint8_t>* plain_text) {
558 EXPECT_TRUE(SupportsAesGcm()); 559 EXPECT_TRUE(SupportsAesGcm());
559 blink::WebCryptoAlgorithm algorithm = 560 blink::WebCryptoAlgorithm algorithm =
560 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits); 561 CreateAesGcmAlgorithm(iv, additional_data, tag_length_bits);
561 562
562 // Join cipher text and authentication tag. 563 // Join cipher text and authentication tag.
563 std::vector<uint8> cipher_text_with_tag; 564 std::vector<uint8_t> cipher_text_with_tag;
564 cipher_text_with_tag.reserve(cipher_text.size() + authentication_tag.size()); 565 cipher_text_with_tag.reserve(cipher_text.size() + authentication_tag.size());
565 cipher_text_with_tag.insert( 566 cipher_text_with_tag.insert(
566 cipher_text_with_tag.end(), cipher_text.begin(), cipher_text.end()); 567 cipher_text_with_tag.end(), cipher_text.begin(), cipher_text.end());
567 cipher_text_with_tag.insert(cipher_text_with_tag.end(), 568 cipher_text_with_tag.insert(cipher_text_with_tag.end(),
568 authentication_tag.begin(), 569 authentication_tag.begin(),
569 authentication_tag.end()); 570 authentication_tag.end());
570 571
571 return Decrypt(algorithm, key, CryptoData(cipher_text_with_tag), plain_text); 572 return Decrypt(algorithm, key, CryptoData(cipher_text_with_tag), plain_text);
572 } 573 }
573 574
(...skipping 17 matching lines...) Expand all
591 blink::WebCryptoKey* key) { 592 blink::WebCryptoKey* key) {
592 return ImportKeyJwk(CryptoData(MakeJsonVector(dict)), 593 return ImportKeyJwk(CryptoData(MakeJsonVector(dict)),
593 algorithm, 594 algorithm,
594 extractable, 595 extractable,
595 usage_mask, 596 usage_mask,
596 key); 597 key);
597 } 598 }
598 599
599 // Parses a vector of JSON into a dictionary. 600 // Parses a vector of JSON into a dictionary.
600 scoped_ptr<base::DictionaryValue> GetJwkDictionary( 601 scoped_ptr<base::DictionaryValue> GetJwkDictionary(
601 const std::vector<uint8>& json) { 602 const std::vector<uint8_t>& json) {
602 base::StringPiece json_string( 603 base::StringPiece json_string(
603 reinterpret_cast<const char*>(Uint8VectorStart(json)), json.size()); 604 reinterpret_cast<const char*>(Uint8VectorStart(json)), json.size());
604 base::Value* value = base::JSONReader::Read(json_string); 605 base::Value* value = base::JSONReader::Read(json_string);
605 EXPECT_TRUE(value); 606 EXPECT_TRUE(value);
606 base::DictionaryValue* dict_value = NULL; 607 base::DictionaryValue* dict_value = NULL;
607 value->GetAsDictionary(&dict_value); 608 value->GetAsDictionary(&dict_value);
608 return scoped_ptr<base::DictionaryValue>(dict_value); 609 return scoped_ptr<base::DictionaryValue>(dict_value);
609 } 610 }
610 611
611 // Verifies the input dictionary contains the expected values. Exact matches are 612 // Verifies the input dictionary contains the expected values. Exact matches are
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 return ::testing::AssertionFailure() 654 return ::testing::AssertionFailure()
654 << "Expected 'key_ops' mask to be " << use_mask_expected 655 << "Expected 'key_ops' mask to be " << use_mask_expected
655 << " but found " << key_ops_mask << " (" << value_string << ")"; 656 << " but found " << key_ops_mask << " (" << value_string << ")";
656 657
657 return ::testing::AssertionSuccess(); 658 return ::testing::AssertionSuccess();
658 } 659 }
659 660
660 // Verifies that the JSON in the input vector contains the provided 661 // Verifies that the JSON in the input vector contains the provided
661 // expected values. Exact matches are required on the fields examined. 662 // expected values. Exact matches are required on the fields examined.
662 ::testing::AssertionResult VerifySecretJwk( 663 ::testing::AssertionResult VerifySecretJwk(
663 const std::vector<uint8>& json, 664 const std::vector<uint8_t>& json,
664 const std::string& alg_expected, 665 const std::string& alg_expected,
665 const std::string& k_expected_hex, 666 const std::string& k_expected_hex,
666 blink::WebCryptoKeyUsageMask use_mask_expected) { 667 blink::WebCryptoKeyUsageMask use_mask_expected) {
667 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json); 668 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json);
668 if (!dict.get() || dict->empty()) 669 if (!dict.get() || dict->empty())
669 return ::testing::AssertionFailure() << "JSON parsing failed"; 670 return ::testing::AssertionFailure() << "JSON parsing failed";
670 671
671 // ---- k 672 // ---- k
672 std::string value_string; 673 std::string value_string;
673 if (!dict->GetString("k", &value_string)) 674 if (!dict->GetString("k", &value_string))
674 return ::testing::AssertionFailure() << "Missing 'k'"; 675 return ::testing::AssertionFailure() << "Missing 'k'";
675 std::string k_value; 676 std::string k_value;
676 if (!webcrypto::Base64DecodeUrlSafe(value_string, &k_value)) 677 if (!webcrypto::Base64DecodeUrlSafe(value_string, &k_value))
677 return ::testing::AssertionFailure() << "Base64DecodeUrlSafe(k) failed"; 678 return ::testing::AssertionFailure() << "Base64DecodeUrlSafe(k) failed";
678 if (!LowerCaseEqualsASCII(base::HexEncode(k_value.data(), k_value.size()), 679 if (!LowerCaseEqualsASCII(base::HexEncode(k_value.data(), k_value.size()),
679 k_expected_hex.c_str())) { 680 k_expected_hex.c_str())) {
680 return ::testing::AssertionFailure() << "Expected 'k' to be " 681 return ::testing::AssertionFailure() << "Expected 'k' to be "
681 << k_expected_hex 682 << k_expected_hex
682 << " but found something different"; 683 << " but found something different";
683 } 684 }
684 685
685 return VerifyJwk(dict, "oct", alg_expected, use_mask_expected); 686 return VerifyJwk(dict, "oct", alg_expected, use_mask_expected);
686 } 687 }
687 688
688 // Verifies that the JSON in the input vector contains the provided 689 // Verifies that the JSON in the input vector contains the provided
689 // expected values. Exact matches are required on the fields examined. 690 // expected values. Exact matches are required on the fields examined.
690 ::testing::AssertionResult VerifyPublicJwk( 691 ::testing::AssertionResult VerifyPublicJwk(
691 const std::vector<uint8>& json, 692 const std::vector<uint8_t>& json,
692 const std::string& alg_expected, 693 const std::string& alg_expected,
693 const std::string& n_expected_hex, 694 const std::string& n_expected_hex,
694 const std::string& e_expected_hex, 695 const std::string& e_expected_hex,
695 blink::WebCryptoKeyUsageMask use_mask_expected) { 696 blink::WebCryptoKeyUsageMask use_mask_expected) {
696 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json); 697 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json);
697 if (!dict.get() || dict->empty()) 698 if (!dict.get() || dict->empty())
698 return ::testing::AssertionFailure() << "JSON parsing failed"; 699 return ::testing::AssertionFailure() << "JSON parsing failed";
699 700
700 // ---- n 701 // ---- n
701 std::string value_string; 702 std::string value_string;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 scoped_ptr<base::ListValue> tests; 794 scoped_ptr<base::ListValue> tests;
794 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); 795 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests));
795 796
796 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 797 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
797 SCOPED_TRACE(test_index); 798 SCOPED_TRACE(test_index);
798 base::DictionaryValue* test; 799 base::DictionaryValue* test;
799 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 800 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
800 801
801 blink::WebCryptoAlgorithm test_algorithm = 802 blink::WebCryptoAlgorithm test_algorithm =
802 GetDigestAlgorithm(test, "algorithm"); 803 GetDigestAlgorithm(test, "algorithm");
803 std::vector<uint8> test_input = GetBytesFromHexString(test, "input"); 804 std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input");
804 std::vector<uint8> test_output = GetBytesFromHexString(test, "output"); 805 std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output");
805 806
806 std::vector<uint8> output; 807 std::vector<uint8_t> output;
807 ASSERT_EQ(Status::Success(), 808 ASSERT_EQ(Status::Success(),
808 Digest(test_algorithm, CryptoData(test_input), &output)); 809 Digest(test_algorithm, CryptoData(test_input), &output));
809 EXPECT_BYTES_EQ(test_output, output); 810 EXPECT_BYTES_EQ(test_output, output);
810 } 811 }
811 } 812 }
812 813
813 TEST_F(SharedCryptoTest, DigestSampleSetsInChunks) { 814 TEST_F(SharedCryptoTest, DigestSampleSetsInChunks) {
814 scoped_ptr<base::ListValue> tests; 815 scoped_ptr<base::ListValue> tests;
815 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests)); 816 ASSERT_TRUE(ReadJsonTestFileToList("digest.json", &tests));
816 817
817 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 818 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
818 SCOPED_TRACE(test_index); 819 SCOPED_TRACE(test_index);
819 base::DictionaryValue* test; 820 base::DictionaryValue* test;
820 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 821 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
821 822
822 blink::WebCryptoAlgorithm test_algorithm = 823 blink::WebCryptoAlgorithm test_algorithm =
823 GetDigestAlgorithm(test, "algorithm"); 824 GetDigestAlgorithm(test, "algorithm");
824 std::vector<uint8> test_input = GetBytesFromHexString(test, "input"); 825 std::vector<uint8_t> test_input = GetBytesFromHexString(test, "input");
825 std::vector<uint8> test_output = GetBytesFromHexString(test, "output"); 826 std::vector<uint8_t> test_output = GetBytesFromHexString(test, "output");
826 827
827 // Test the chunk version of the digest functions. Test with 129 byte chunks 828 // Test the chunk version of the digest functions. Test with 129 byte chunks
828 // because the SHA-512 chunk size is 128 bytes. 829 // because the SHA-512 chunk size is 128 bytes.
829 unsigned char* output; 830 unsigned char* output;
830 unsigned int output_length; 831 unsigned int output_length;
831 static const size_t kChunkSizeBytes = 129; 832 static const size_t kChunkSizeBytes = 129;
832 size_t length = test_input.size(); 833 size_t length = test_input.size();
833 scoped_ptr<blink::WebCryptoDigestor> digestor( 834 scoped_ptr<blink::WebCryptoDigestor> digestor(
834 CreateDigestor(test_algorithm.id())); 835 CreateDigestor(test_algorithm.id()));
835 std::vector<uint8>::iterator begin = test_input.begin(); 836 std::vector<uint8_t>::iterator begin = test_input.begin();
836 size_t chunk_index = 0; 837 size_t chunk_index = 0;
837 while (begin != test_input.end()) { 838 while (begin != test_input.end()) {
838 size_t chunk_length = std::min(kChunkSizeBytes, length - chunk_index); 839 size_t chunk_length = std::min(kChunkSizeBytes, length - chunk_index);
839 std::vector<uint8> chunk(begin, begin + chunk_length); 840 std::vector<uint8_t> chunk(begin, begin + chunk_length);
840 ASSERT_TRUE(chunk.size() > 0); 841 ASSERT_TRUE(chunk.size() > 0);
841 EXPECT_TRUE(digestor->consume(&chunk.front(), chunk.size())); 842 EXPECT_TRUE(digestor->consume(&chunk.front(), chunk.size()));
842 chunk_index = chunk_index + chunk_length; 843 chunk_index = chunk_index + chunk_length;
843 begin = begin + chunk_length; 844 begin = begin + chunk_length;
844 } 845 }
845 EXPECT_TRUE(digestor->finish(output, output_length)); 846 EXPECT_TRUE(digestor->finish(output, output_length));
846 EXPECT_BYTES_EQ(test_output, CryptoData(output, output_length)); 847 EXPECT_BYTES_EQ(test_output, CryptoData(output, output_length));
847 } 848 }
848 } 849 }
849 850
850 TEST_F(SharedCryptoTest, HMACSampleSets) { 851 TEST_F(SharedCryptoTest, HMACSampleSets) {
851 scoped_ptr<base::ListValue> tests; 852 scoped_ptr<base::ListValue> tests;
852 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests)); 853 ASSERT_TRUE(ReadJsonTestFileToList("hmac.json", &tests));
853 // TODO(padolph): Missing known answer tests for HMAC SHA384, and SHA512. 854 // TODO(padolph): Missing known answer tests for HMAC SHA384, and SHA512.
854 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 855 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
855 SCOPED_TRACE(test_index); 856 SCOPED_TRACE(test_index);
856 base::DictionaryValue* test; 857 base::DictionaryValue* test;
857 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 858 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
858 859
859 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash"); 860 blink::WebCryptoAlgorithm test_hash = GetDigestAlgorithm(test, "hash");
860 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 861 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
861 const std::vector<uint8> test_message = 862 const std::vector<uint8_t> test_message =
862 GetBytesFromHexString(test, "message"); 863 GetBytesFromHexString(test, "message");
863 const std::vector<uint8> test_mac = GetBytesFromHexString(test, "mac"); 864 const std::vector<uint8_t> test_mac = GetBytesFromHexString(test, "mac");
864 865
865 blink::WebCryptoAlgorithm algorithm = 866 blink::WebCryptoAlgorithm algorithm =
866 CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac); 867 CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac);
867 868
868 blink::WebCryptoAlgorithm import_algorithm = 869 blink::WebCryptoAlgorithm import_algorithm =
869 CreateHmacImportAlgorithm(test_hash.id()); 870 CreateHmacImportAlgorithm(test_hash.id());
870 871
871 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 872 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
872 test_key, 873 test_key,
873 import_algorithm, 874 import_algorithm,
874 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify); 875 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify);
875 876
876 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id()); 877 EXPECT_EQ(test_hash.id(), key.algorithm().hmacParams()->hash().id());
877 EXPECT_EQ(test_key.size() * 8, key.algorithm().hmacParams()->lengthBits()); 878 EXPECT_EQ(test_key.size() * 8, key.algorithm().hmacParams()->lengthBits());
878 879
879 // Verify exported raw key is identical to the imported data 880 // Verify exported raw key is identical to the imported data
880 std::vector<uint8> raw_key; 881 std::vector<uint8_t> raw_key;
881 EXPECT_EQ(Status::Success(), 882 EXPECT_EQ(Status::Success(),
882 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 883 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
883 EXPECT_BYTES_EQ(test_key, raw_key); 884 EXPECT_BYTES_EQ(test_key, raw_key);
884 885
885 std::vector<uint8> output; 886 std::vector<uint8_t> output;
886 887
887 ASSERT_EQ(Status::Success(), 888 ASSERT_EQ(Status::Success(),
888 Sign(algorithm, key, CryptoData(test_message), &output)); 889 Sign(algorithm, key, CryptoData(test_message), &output));
889 890
890 EXPECT_BYTES_EQ(test_mac, output); 891 EXPECT_BYTES_EQ(test_mac, output);
891 892
892 bool signature_match = false; 893 bool signature_match = false;
893 EXPECT_EQ(Status::Success(), 894 EXPECT_EQ(Status::Success(),
894 Verify(algorithm, 895 Verify(algorithm,
895 key, 896 key,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 } 930 }
930 931
931 TEST_F(SharedCryptoTest, AesCbcFailures) { 932 TEST_F(SharedCryptoTest, AesCbcFailures) {
932 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c"; 933 const std::string key_hex = "2b7e151628aed2a6abf7158809cf4f3c";
933 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 934 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
934 HexStringToBytes(key_hex), 935 HexStringToBytes(key_hex),
935 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 936 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
936 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 937 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
937 938
938 // Verify exported raw key is identical to the imported data 939 // Verify exported raw key is identical to the imported data
939 std::vector<uint8> raw_key; 940 std::vector<uint8_t> raw_key;
940 EXPECT_EQ(Status::Success(), 941 EXPECT_EQ(Status::Success(),
941 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 942 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
942 EXPECT_BYTES_EQ_HEX(key_hex, raw_key); 943 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
943 944
944 std::vector<uint8> output; 945 std::vector<uint8_t> output;
945 946
946 // Use an invalid |iv| (fewer than 16 bytes) 947 // Use an invalid |iv| (fewer than 16 bytes)
947 { 948 {
948 std::vector<uint8> input(32); 949 std::vector<uint8_t> input(32);
949 std::vector<uint8> iv; 950 std::vector<uint8_t> iv;
950 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(), 951 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(),
951 Encrypt(webcrypto::CreateAesCbcAlgorithm(iv), 952 Encrypt(webcrypto::CreateAesCbcAlgorithm(iv),
952 key, 953 key,
953 CryptoData(input), 954 CryptoData(input),
954 &output)); 955 &output));
955 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(), 956 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(),
956 Decrypt(webcrypto::CreateAesCbcAlgorithm(iv), 957 Decrypt(webcrypto::CreateAesCbcAlgorithm(iv),
957 key, 958 key,
958 CryptoData(input), 959 CryptoData(input),
959 &output)); 960 &output));
960 } 961 }
961 962
962 // Use an invalid |iv| (more than 16 bytes) 963 // Use an invalid |iv| (more than 16 bytes)
963 { 964 {
964 std::vector<uint8> input(32); 965 std::vector<uint8_t> input(32);
965 std::vector<uint8> iv(17); 966 std::vector<uint8_t> iv(17);
966 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(), 967 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(),
967 Encrypt(webcrypto::CreateAesCbcAlgorithm(iv), 968 Encrypt(webcrypto::CreateAesCbcAlgorithm(iv),
968 key, 969 key,
969 CryptoData(input), 970 CryptoData(input),
970 &output)); 971 &output));
971 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(), 972 EXPECT_EQ(Status::ErrorIncorrectSizeAesCbcIv(),
972 Decrypt(webcrypto::CreateAesCbcAlgorithm(iv), 973 Decrypt(webcrypto::CreateAesCbcAlgorithm(iv),
973 key, 974 key,
974 CryptoData(input), 975 CryptoData(input),
975 &output)); 976 &output));
976 } 977 }
977 978
978 // Give an input that is too large (would cause integer overflow when 979 // Give an input that is too large (would cause integer overflow when
979 // narrowing to an int). 980 // narrowing to an int).
980 { 981 {
981 std::vector<uint8> iv(16); 982 std::vector<uint8_t> iv(16);
982 983
983 // Pretend the input is large. Don't pass data pointer as NULL in case that 984 // Pretend the input is large. Don't pass data pointer as NULL in case that
984 // is special cased; the implementation shouldn't actually dereference the 985 // is special cased; the implementation shouldn't actually dereference the
985 // data. 986 // data.
986 CryptoData input(&iv[0], INT_MAX - 3); 987 CryptoData input(&iv[0], INT_MAX - 3);
987 988
988 EXPECT_EQ(Status::ErrorDataTooLarge(), 989 EXPECT_EQ(Status::ErrorDataTooLarge(),
989 Encrypt(CreateAesCbcAlgorithm(iv), key, input, &output)); 990 Encrypt(CreateAesCbcAlgorithm(iv), key, input, &output));
990 EXPECT_EQ(Status::ErrorDataTooLarge(), 991 EXPECT_EQ(Status::ErrorDataTooLarge(),
991 Decrypt(CreateAesCbcAlgorithm(iv), key, input, &output)); 992 Decrypt(CreateAesCbcAlgorithm(iv), key, input, &output));
992 } 993 }
993 994
994 // Fail importing the key (too few bytes specified) 995 // Fail importing the key (too few bytes specified)
995 { 996 {
996 std::vector<uint8> key_raw(1); 997 std::vector<uint8_t> key_raw(1);
997 std::vector<uint8> iv(16); 998 std::vector<uint8_t> iv(16);
998 999
999 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1000 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1000 EXPECT_EQ(Status::ErrorImportAesKeyLength(), 1001 EXPECT_EQ(Status::ErrorImportAesKeyLength(),
1001 ImportKey(blink::WebCryptoKeyFormatRaw, 1002 ImportKey(blink::WebCryptoKeyFormatRaw,
1002 CryptoData(key_raw), 1003 CryptoData(key_raw),
1003 CreateAesCbcAlgorithm(iv), 1004 CreateAesCbcAlgorithm(iv),
1004 true, 1005 true,
1005 blink::WebCryptoKeyUsageEncrypt, 1006 blink::WebCryptoKeyUsageEncrypt,
1006 &key)); 1007 &key));
1007 } 1008 }
(...skipping 19 matching lines...) Expand all
1027 1028
1028 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) { 1029 TEST_F(SharedCryptoTest, MAYBE(AesCbcSampleSets)) {
1029 scoped_ptr<base::ListValue> tests; 1030 scoped_ptr<base::ListValue> tests;
1030 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests)); 1031 ASSERT_TRUE(ReadJsonTestFileToList("aes_cbc.json", &tests));
1031 1032
1032 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 1033 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
1033 SCOPED_TRACE(test_index); 1034 SCOPED_TRACE(test_index);
1034 base::DictionaryValue* test; 1035 base::DictionaryValue* test;
1035 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 1036 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
1036 1037
1037 std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 1038 std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
1038 std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); 1039 std::vector<uint8_t> test_iv = GetBytesFromHexString(test, "iv");
1039 std::vector<uint8> test_plain_text = 1040 std::vector<uint8_t> test_plain_text =
1040 GetBytesFromHexString(test, "plain_text"); 1041 GetBytesFromHexString(test, "plain_text");
1041 std::vector<uint8> test_cipher_text = 1042 std::vector<uint8_t> test_cipher_text =
1042 GetBytesFromHexString(test, "cipher_text"); 1043 GetBytesFromHexString(test, "cipher_text");
1043 1044
1044 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 1045 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
1045 test_key, 1046 test_key,
1046 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 1047 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
1047 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 1048 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
1048 1049
1049 EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits()); 1050 EXPECT_EQ(test_key.size() * 8, key.algorithm().aesParams()->lengthBits());
1050 1051
1051 // Verify exported raw key is identical to the imported data 1052 // Verify exported raw key is identical to the imported data
1052 std::vector<uint8> raw_key; 1053 std::vector<uint8_t> raw_key;
1053 EXPECT_EQ(Status::Success(), 1054 EXPECT_EQ(Status::Success(),
1054 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 1055 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
1055 EXPECT_BYTES_EQ(test_key, raw_key); 1056 EXPECT_BYTES_EQ(test_key, raw_key);
1056 1057
1057 std::vector<uint8> output; 1058 std::vector<uint8_t> output;
1058 1059
1059 // Test encryption. 1060 // Test encryption.
1060 EXPECT_EQ(Status::Success(), 1061 EXPECT_EQ(Status::Success(),
1061 Encrypt(webcrypto::CreateAesCbcAlgorithm(test_iv), 1062 Encrypt(webcrypto::CreateAesCbcAlgorithm(test_iv),
1062 key, 1063 key,
1063 CryptoData(test_plain_text), 1064 CryptoData(test_plain_text),
1064 &output)); 1065 &output));
1065 EXPECT_BYTES_EQ(test_cipher_text, output); 1066 EXPECT_BYTES_EQ(test_cipher_text, output);
1066 1067
1067 // Test decryption. 1068 // Test decryption.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 // allowed key length. 1104 // allowed key length.
1104 std::vector<blink::WebCryptoAlgorithm> algorithm; 1105 std::vector<blink::WebCryptoAlgorithm> algorithm;
1105 const unsigned short kKeyLength[] = {128, 256}; 1106 const unsigned short kKeyLength[] = {128, 256};
1106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) { 1107 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kKeyLength); ++i) {
1107 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i])); 1108 algorithm.push_back(CreateAesCbcKeyGenAlgorithm(kKeyLength[i]));
1108 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i])); 1109 algorithm.push_back(CreateAesKwKeyGenAlgorithm(kKeyLength[i]));
1109 if (SupportsAesGcm()) 1110 if (SupportsAesGcm())
1110 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i])); 1111 algorithm.push_back(CreateAesGcmKeyGenAlgorithm(kKeyLength[i]));
1111 } 1112 }
1112 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1113 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1113 std::vector<std::vector<uint8> > keys; 1114 std::vector<std::vector<uint8_t> > keys;
1114 std::vector<uint8> key_bytes; 1115 std::vector<uint8_t> key_bytes;
1115 for (size_t i = 0; i < algorithm.size(); ++i) { 1116 for (size_t i = 0; i < algorithm.size(); ++i) {
1116 SCOPED_TRACE(i); 1117 SCOPED_TRACE(i);
1117 // Generate a small sample of keys. 1118 // Generate a small sample of keys.
1118 keys.clear(); 1119 keys.clear();
1119 for (int j = 0; j < 16; ++j) { 1120 for (int j = 0; j < 16; ++j) {
1120 ASSERT_EQ(Status::Success(), 1121 ASSERT_EQ(Status::Success(),
1121 GenerateSecretKey(algorithm[i], true, 0, &key)); 1122 GenerateSecretKey(algorithm[i], true, 0, &key));
1122 EXPECT_TRUE(key.handle()); 1123 EXPECT_TRUE(key.handle());
1123 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1124 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1124 ASSERT_EQ(Status::Success(), 1125 ASSERT_EQ(Status::Success(),
(...skipping 22 matching lines...) Expand all
1147 if (SupportsAesGcm()) { 1148 if (SupportsAesGcm()) {
1148 EXPECT_EQ(Status::ErrorGenerateKeyLength(), 1149 EXPECT_EQ(Status::ErrorGenerateKeyLength(),
1149 GenerateSecretKey( 1150 GenerateSecretKey(
1150 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), true, 0, &key)); 1151 CreateAesGcmKeyGenAlgorithm(kKeyLen[i]), true, 0, &key));
1151 } 1152 }
1152 } 1153 }
1153 } 1154 }
1154 1155
1155 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmac)) { 1156 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmac)) {
1156 // Generate a small sample of HMAC keys. 1157 // Generate a small sample of HMAC keys.
1157 std::vector<std::vector<uint8> > keys; 1158 std::vector<std::vector<uint8_t> > keys;
1158 for (int i = 0; i < 16; ++i) { 1159 for (int i = 0; i < 16; ++i) {
1159 std::vector<uint8> key_bytes; 1160 std::vector<uint8_t> key_bytes;
1160 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1161 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1161 blink::WebCryptoAlgorithm algorithm = 1162 blink::WebCryptoAlgorithm algorithm =
1162 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512); 1163 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 512);
1163 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); 1164 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key));
1164 EXPECT_FALSE(key.isNull()); 1165 EXPECT_FALSE(key.isNull());
1165 EXPECT_TRUE(key.handle()); 1166 EXPECT_TRUE(key.handle());
1166 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1167 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1167 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 1168 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
1168 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, 1169 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1,
1169 key.algorithm().hmacParams()->hash().id()); 1170 key.algorithm().hmacParams()->hash().id());
1170 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); 1171 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits());
1171 1172
1172 std::vector<uint8> raw_key; 1173 std::vector<uint8_t> raw_key;
1173 ASSERT_EQ(Status::Success(), 1174 ASSERT_EQ(Status::Success(),
1174 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 1175 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
1175 EXPECT_EQ(64U, raw_key.size()); 1176 EXPECT_EQ(64U, raw_key.size());
1176 keys.push_back(raw_key); 1177 keys.push_back(raw_key);
1177 } 1178 }
1178 // Ensure all entries in the key sample set are unique. This is a simplistic 1179 // Ensure all entries in the key sample set are unique. This is a simplistic
1179 // estimate of whether the generated keys appear random. 1180 // estimate of whether the generated keys appear random.
1180 EXPECT_FALSE(CopiesExist(keys)); 1181 EXPECT_FALSE(CopiesExist(keys));
1181 } 1182 }
1182 1183
1183 // If the key length is not provided, then the block size is used. 1184 // If the key length is not provided, then the block size is used.
1184 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) { 1185 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyHmacNoLength)) {
1185 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1186 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1186 blink::WebCryptoAlgorithm algorithm = 1187 blink::WebCryptoAlgorithm algorithm =
1187 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0); 1188 CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha1, 0);
1188 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); 1189 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key));
1189 EXPECT_TRUE(key.handle()); 1190 EXPECT_TRUE(key.handle());
1190 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1191 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1191 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 1192 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
1192 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, 1193 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1,
1193 key.algorithm().hmacParams()->hash().id()); 1194 key.algorithm().hmacParams()->hash().id());
1194 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits()); 1195 EXPECT_EQ(512u, key.algorithm().hmacParams()->lengthBits());
1195 std::vector<uint8> raw_key; 1196 std::vector<uint8_t> raw_key;
1196 ASSERT_EQ(Status::Success(), 1197 ASSERT_EQ(Status::Success(),
1197 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 1198 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
1198 EXPECT_EQ(64U, raw_key.size()); 1199 EXPECT_EQ(64U, raw_key.size());
1199 1200
1200 // The block size for HMAC SHA-512 is larger. 1201 // The block size for HMAC SHA-512 is larger.
1201 algorithm = CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0); 1202 algorithm = CreateHmacKeyGenAlgorithm(blink::WebCryptoAlgorithmIdSha512, 0);
1202 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key)); 1203 ASSERT_EQ(Status::Success(), GenerateSecretKey(algorithm, true, 0, &key));
1203 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 1204 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
1204 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512, 1205 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha512,
1205 key.algorithm().hmacParams()->hash().id()); 1206 key.algorithm().hmacParams()->hash().id());
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1361 EXPECT_EQ(Status::Success(), 1362 EXPECT_EQ(Status::Success(),
1362 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key)); 1363 ImportKeyJwkFromDict(dict, algorithm, false, usage_mask, &key));
1363 1364
1364 // Fail on empty JSON. 1365 // Fail on empty JSON.
1365 EXPECT_EQ( 1366 EXPECT_EQ(
1366 Status::ErrorImportEmptyKeyData(), 1367 Status::ErrorImportEmptyKeyData(),
1367 ImportKeyJwk( 1368 ImportKeyJwk(
1368 CryptoData(MakeJsonVector("")), algorithm, false, usage_mask, &key)); 1369 CryptoData(MakeJsonVector("")), algorithm, false, usage_mask, &key));
1369 1370
1370 // Fail on invalid JSON. 1371 // Fail on invalid JSON.
1371 const std::vector<uint8> bad_json_vec = MakeJsonVector( 1372 const std::vector<uint8_t> bad_json_vec = MakeJsonVector(
1372 "{" 1373 "{"
1373 "\"kty\" : \"oct\"," 1374 "\"kty\" : \"oct\","
1374 "\"alg\" : \"HS256\"," 1375 "\"alg\" : \"HS256\","
1375 "\"use\" : "); 1376 "\"use\" : ");
1376 EXPECT_EQ(Status::ErrorJwkNotDictionary(), 1377 EXPECT_EQ(Status::ErrorJwkNotDictionary(),
1377 ImportKeyJwk( 1378 ImportKeyJwk(
1378 CryptoData(bad_json_vec), algorithm, false, usage_mask, &key)); 1379 CryptoData(bad_json_vec), algorithm, false, usage_mask, &key));
1379 1380
1380 // Fail on JWK alg present but incorrect (expecting A128CBC). 1381 // Fail on JWK alg present but incorrect (expecting A128CBC).
1381 dict.SetString("alg", "A127CBC"); 1382 dict.SetString("alg", "A127CBC");
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
1591 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 1592 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
1592 ASSERT_EQ(Status::Success(), 1593 ASSERT_EQ(Status::Success(),
1593 ImportKey(blink::WebCryptoKeyFormatSpki, 1594 ImportKey(blink::WebCryptoKeyFormatSpki,
1594 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 1595 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
1595 test.algorithm, 1596 test.algorithm,
1596 true, 1597 true,
1597 test.usage, 1598 test.usage,
1598 &public_key)); 1599 &public_key));
1599 1600
1600 // Export the public key as JWK and verify its contents 1601 // Export the public key as JWK and verify its contents
1601 std::vector<uint8> jwk; 1602 std::vector<uint8_t> jwk;
1602 ASSERT_EQ(Status::Success(), 1603 ASSERT_EQ(Status::Success(),
1603 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk)); 1604 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk));
1604 EXPECT_TRUE(VerifyPublicJwk(jwk, 1605 EXPECT_TRUE(VerifyPublicJwk(jwk,
1605 test.jwk_alg, 1606 test.jwk_alg,
1606 kPublicKeyModulusHex, 1607 kPublicKeyModulusHex,
1607 kPublicKeyExponentHex, 1608 kPublicKeyExponentHex,
1608 test.usage)); 1609 test.usage));
1609 1610
1610 // Import the JWK back in to create a new key 1611 // Import the JWK back in to create a new key
1611 blink::WebCryptoKey public_key2 = blink::WebCryptoKey::createNull(); 1612 blink::WebCryptoKey public_key2 = blink::WebCryptoKey::createNull();
1612 ASSERT_EQ( 1613 ASSERT_EQ(
1613 Status::Success(), 1614 Status::Success(),
1614 ImportKeyJwk( 1615 ImportKeyJwk(
1615 CryptoData(jwk), test.algorithm, true, test.usage, &public_key2)); 1616 CryptoData(jwk), test.algorithm, true, test.usage, &public_key2));
1616 ASSERT_TRUE(public_key2.handle()); 1617 ASSERT_TRUE(public_key2.handle());
1617 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type()); 1618 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key2.type());
1618 EXPECT_TRUE(public_key2.extractable()); 1619 EXPECT_TRUE(public_key2.extractable());
1619 EXPECT_EQ(test.algorithm.id(), public_key2.algorithm().id()); 1620 EXPECT_EQ(test.algorithm.id(), public_key2.algorithm().id());
1620 1621
1621 // Only perform SPKI consistency test for RSA-SSA as its 1622 // Only perform SPKI consistency test for RSA-SSA as its
1622 // export format is the same as kPublicKeySpkiDerHex 1623 // export format is the same as kPublicKeySpkiDerHex
1623 if (test.algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5) { 1624 if (test.algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5) {
1624 // Export the new key as spki and compare to the original. 1625 // Export the new key as spki and compare to the original.
1625 std::vector<uint8> spki; 1626 std::vector<uint8_t> spki;
1626 ASSERT_EQ(Status::Success(), 1627 ASSERT_EQ(Status::Success(),
1627 ExportKey(blink::WebCryptoKeyFormatSpki, public_key2, &spki)); 1628 ExportKey(blink::WebCryptoKeyFormatSpki, public_key2, &spki));
1628 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, CryptoData(spki)); 1629 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, CryptoData(spki));
1629 } 1630 }
1630 } 1631 }
1631 } 1632 }
1632 1633
1633 TEST_F(SharedCryptoTest, MAYBE(ImportJwkRsaFailures)) { 1634 TEST_F(SharedCryptoTest, MAYBE(ImportJwkRsaFailures)) {
1634 base::DictionaryValue dict; 1635 base::DictionaryValue dict;
1635 RestoreJwkRsaDictionary(&dict); 1636 RestoreJwkRsaDictionary(&dict);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1684 1685
1685 // Consistency rules when JWK value is not present: Inputs should be used. 1686 // Consistency rules when JWK value is not present: Inputs should be used.
1686 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1687 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1687 bool extractable = false; 1688 bool extractable = false;
1688 blink::WebCryptoAlgorithm algorithm = 1689 blink::WebCryptoAlgorithm algorithm =
1689 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256); 1690 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha256);
1690 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify; 1691 blink::WebCryptoKeyUsageMask usage_mask = blink::WebCryptoKeyUsageVerify;
1691 base::DictionaryValue dict; 1692 base::DictionaryValue dict;
1692 dict.SetString("kty", "oct"); 1693 dict.SetString("kty", "oct");
1693 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); 1694 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg");
1694 std::vector<uint8> json_vec = MakeJsonVector(dict); 1695 std::vector<uint8_t> json_vec = MakeJsonVector(dict);
1695 EXPECT_EQ( 1696 EXPECT_EQ(
1696 Status::Success(), 1697 Status::Success(),
1697 ImportKeyJwk( 1698 ImportKeyJwk(
1698 CryptoData(json_vec), algorithm, extractable, usage_mask, &key)); 1699 CryptoData(json_vec), algorithm, extractable, usage_mask, &key));
1699 EXPECT_TRUE(key.handle()); 1700 EXPECT_TRUE(key.handle());
1700 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1701 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1701 EXPECT_EQ(extractable, key.extractable()); 1702 EXPECT_EQ(extractable, key.extractable());
1702 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 1703 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
1703 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, 1704 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256,
1704 key.algorithm().hmacParams()->hash().id()); 1705 key.algorithm().hmacParams()->hash().id());
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1831 dict.SetBoolean("ext", false); 1832 dict.SetBoolean("ext", false);
1832 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg"); 1833 dict.SetString("k", "l3nZEgZCeX8XRwJdWyK3rGB8qwjhdY8vOkbIvh4lxTuMao9Y_--hdg");
1833 1834
1834 ASSERT_EQ( 1835 ASSERT_EQ(
1835 Status::Success(), 1836 Status::Success(),
1836 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key)); 1837 ImportKeyJwkFromDict(dict, algorithm, extractable, usage_mask, &key));
1837 1838
1838 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, 1839 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256,
1839 key.algorithm().hmacParams()->hash().id()); 1840 key.algorithm().hmacParams()->hash().id());
1840 1841
1841 const std::vector<uint8> message_raw = HexStringToBytes( 1842 const std::vector<uint8_t> message_raw = HexStringToBytes(
1842 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a" 1843 "b1689c2591eaf3c9e66070f8a77954ffb81749f1b00346f9dfe0b2ee905dcc288baf4a"
1843 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92" 1844 "92de3f4001dd9f44c468c3d07d6c6ee82faceafc97c2fc0fc0601719d2dcd0aa2aec92"
1844 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f" 1845 "d1b0ae933c65eb06a03c9c935c2bad0459810241347ab87e9f11adb30415424c6c7f5f"
1845 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e"); 1846 "22a003b8ab8de54f6ded0e3ab9245fa79568451dfa258e");
1846 1847
1847 std::vector<uint8> output; 1848 std::vector<uint8_t> output;
1848 1849
1849 ASSERT_EQ(Status::Success(), 1850 ASSERT_EQ(Status::Success(),
1850 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), 1851 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac),
1851 key, 1852 key,
1852 CryptoData(message_raw), 1853 CryptoData(message_raw),
1853 &output)); 1854 &output));
1854 1855
1855 const std::string mac_raw = 1856 const std::string mac_raw =
1856 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b"; 1857 "769f00d3e6a6cc1fb426a14a4f76c6462e6149726e0dee0ec0cf97a16605ac8b";
1857 1858
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1928 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt | 1929 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt |
1929 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey, 1930 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey,
1930 "A256CBC"}, 1931 "A256CBC"},
1931 // Zero usage value 1932 // Zero usage value
1932 {key_hex_512, hmac_sha_512_alg, 0, "HS512"}, 1933 {key_hex_512, hmac_sha_512_alg, 0, "HS512"},
1933 }; 1934 };
1934 1935
1935 // Round-trip import/export each key. 1936 // Round-trip import/export each key.
1936 1937
1937 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1938 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1938 std::vector<uint8> json; 1939 std::vector<uint8_t> json;
1939 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests); 1940 for (size_t test_index = 0; test_index < ARRAYSIZE_UNSAFE(kTests);
1940 ++test_index) { 1941 ++test_index) {
1941 SCOPED_TRACE(test_index); 1942 SCOPED_TRACE(test_index);
1942 const TestCase& test = kTests[test_index]; 1943 const TestCase& test = kTests[test_index];
1943 1944
1944 // Skip AES-GCM tests where not supported. 1945 // Skip AES-GCM tests where not supported.
1945 if (test.algorithm.id() == blink::WebCryptoAlgorithmIdAesGcm && 1946 if (test.algorithm.id() == blink::WebCryptoAlgorithmIdAesGcm &&
1946 !SupportsAesGcm()) { 1947 !SupportsAesGcm()) {
1947 continue; 1948 continue;
1948 } 1949 }
(...skipping 11 matching lines...) Expand all
1960 ASSERT_EQ( 1961 ASSERT_EQ(
1961 Status::Success(), 1962 Status::Success(),
1962 ImportKeyJwk(CryptoData(json), test.algorithm, true, test.usage, &key)); 1963 ImportKeyJwk(CryptoData(json), test.algorithm, true, test.usage, &key));
1963 EXPECT_TRUE(key.handle()); 1964 EXPECT_TRUE(key.handle());
1964 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 1965 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
1965 EXPECT_EQ(test.algorithm.id(), key.algorithm().id()); 1966 EXPECT_EQ(test.algorithm.id(), key.algorithm().id());
1966 EXPECT_EQ(true, key.extractable()); 1967 EXPECT_EQ(true, key.extractable());
1967 EXPECT_EQ(test.usage, key.usages()); 1968 EXPECT_EQ(test.usage, key.usages());
1968 1969
1969 // Export the key in raw format and compare to the original. 1970 // Export the key in raw format and compare to the original.
1970 std::vector<uint8> key_raw_out; 1971 std::vector<uint8_t> key_raw_out;
1971 ASSERT_EQ(Status::Success(), 1972 ASSERT_EQ(Status::Success(),
1972 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); 1973 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out));
1973 EXPECT_BYTES_EQ_HEX(test.key_hex, key_raw_out); 1974 EXPECT_BYTES_EQ_HEX(test.key_hex, key_raw_out);
1974 } 1975 }
1975 } 1976 }
1976 1977
1977 TEST_F(SharedCryptoTest, MAYBE(ExportJwkEmptySymmetricKey)) { 1978 TEST_F(SharedCryptoTest, MAYBE(ExportJwkEmptySymmetricKey)) {
1978 const blink::WebCryptoAlgorithm import_algorithm = 1979 const blink::WebCryptoAlgorithm import_algorithm =
1979 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1); 1980 webcrypto::CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1);
1980 1981
1981 blink::WebCryptoKeyUsageMask usages = blink::WebCryptoKeyUsageSign; 1982 blink::WebCryptoKeyUsageMask usages = blink::WebCryptoKeyUsageSign;
1982 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 1983 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
1983 1984
1984 // Import a zero-byte HMAC key. 1985 // Import a zero-byte HMAC key.
1985 const char key_data_hex[] = ""; 1986 const char key_data_hex[] = "";
1986 key = ImportSecretKeyFromRaw( 1987 key = ImportSecretKeyFromRaw(
1987 HexStringToBytes(key_data_hex), import_algorithm, usages); 1988 HexStringToBytes(key_data_hex), import_algorithm, usages);
1988 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits()); 1989 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits());
1989 1990
1990 // Export the key in JWK format and validate. 1991 // Export the key in JWK format and validate.
1991 std::vector<uint8> json; 1992 std::vector<uint8_t> json;
1992 ASSERT_EQ(Status::Success(), 1993 ASSERT_EQ(Status::Success(),
1993 ExportKey(blink::WebCryptoKeyFormatJwk, key, &json)); 1994 ExportKey(blink::WebCryptoKeyFormatJwk, key, &json));
1994 EXPECT_TRUE(VerifySecretJwk(json, "HS1", key_data_hex, usages)); 1995 EXPECT_TRUE(VerifySecretJwk(json, "HS1", key_data_hex, usages));
1995 1996
1996 // Now try re-importing the JWK key. 1997 // Now try re-importing the JWK key.
1997 key = blink::WebCryptoKey::createNull(); 1998 key = blink::WebCryptoKey::createNull();
1998 EXPECT_EQ(Status::Success(), 1999 EXPECT_EQ(Status::Success(),
1999 ImportKey(blink::WebCryptoKeyFormatJwk, 2000 ImportKey(blink::WebCryptoKeyFormatJwk,
2000 CryptoData(json), 2001 CryptoData(json),
2001 import_algorithm, 2002 import_algorithm,
2002 true, 2003 true,
2003 usages, 2004 usages,
2004 &key)); 2005 &key));
2005 2006
2006 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 2007 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
2007 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits()); 2008 EXPECT_EQ(0u, key.algorithm().hmacParams()->lengthBits());
2008 2009
2009 std::vector<uint8> exported_key_data; 2010 std::vector<uint8_t> exported_key_data;
2010 EXPECT_EQ(Status::Success(), 2011 EXPECT_EQ(Status::Success(),
2011 ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key_data)); 2012 ExportKey(blink::WebCryptoKeyFormatRaw, key, &exported_key_data));
2012 2013
2013 EXPECT_EQ(0u, exported_key_data.size()); 2014 EXPECT_EQ(0u, exported_key_data.size());
2014 } 2015 }
2015 2016
2016 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) { 2017 TEST_F(SharedCryptoTest, MAYBE(ImportExportSpki)) {
2017 if (!SupportsRsaKeyImport()) 2018 if (!SupportsRsaKeyImport())
2018 return; 2019 return;
2019 2020
(...skipping 15 matching lines...) Expand all
2035 EXPECT_EQ(kModulusLengthBits, 2036 EXPECT_EQ(kModulusLengthBits,
2036 key.algorithm().rsaHashedParams()->modulusLengthBits()); 2037 key.algorithm().rsaHashedParams()->modulusLengthBits());
2037 EXPECT_BYTES_EQ_HEX( 2038 EXPECT_BYTES_EQ_HEX(
2038 "010001", 2039 "010001",
2039 CryptoData(key.algorithm().rsaHashedParams()->publicExponent())); 2040 CryptoData(key.algorithm().rsaHashedParams()->publicExponent()));
2040 2041
2041 // Failing case: Empty SPKI data 2042 // Failing case: Empty SPKI data
2042 EXPECT_EQ( 2043 EXPECT_EQ(
2043 Status::ErrorImportEmptyKeyData(), 2044 Status::ErrorImportEmptyKeyData(),
2044 ImportKey(blink::WebCryptoKeyFormatSpki, 2045 ImportKey(blink::WebCryptoKeyFormatSpki,
2045 CryptoData(std::vector<uint8>()), 2046 CryptoData(std::vector<uint8_t>()),
2046 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 2047 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
2047 true, 2048 true,
2048 blink::WebCryptoKeyUsageVerify, 2049 blink::WebCryptoKeyUsageVerify,
2049 &key)); 2050 &key));
2050 2051
2051 // Failing case: Bad DER encoding. 2052 // Failing case: Bad DER encoding.
2052 EXPECT_EQ( 2053 EXPECT_EQ(
2053 Status::DataError(), 2054 Status::DataError(),
2054 ImportKey(blink::WebCryptoKeyFormatSpki, 2055 ImportKey(blink::WebCryptoKeyFormatSpki,
2055 CryptoData(HexStringToBytes("618333c4cb")), 2056 CryptoData(HexStringToBytes("618333c4cb")),
2056 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5), 2057 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5),
2057 true, 2058 true,
2058 blink::WebCryptoKeyUsageVerify, 2059 blink::WebCryptoKeyUsageVerify,
2059 &key)); 2060 &key));
2060 2061
2061 // Failing case: Import RSA key but provide an inconsistent input algorithm. 2062 // Failing case: Import RSA key but provide an inconsistent input algorithm.
2062 EXPECT_EQ(Status::ErrorUnsupportedImportKeyFormat(), 2063 EXPECT_EQ(Status::ErrorUnsupportedImportKeyFormat(),
2063 ImportKey(blink::WebCryptoKeyFormatSpki, 2064 ImportKey(blink::WebCryptoKeyFormatSpki,
2064 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)), 2065 CryptoData(HexStringToBytes(kPublicKeySpkiDerHex)),
2065 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 2066 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
2066 true, 2067 true,
2067 blink::WebCryptoKeyUsageEncrypt, 2068 blink::WebCryptoKeyUsageEncrypt,
2068 &key)); 2069 &key));
2069 2070
2070 // Passing case: Export a previously imported RSA public key in SPKI format 2071 // Passing case: Export a previously imported RSA public key in SPKI format
2071 // and compare to original data. 2072 // and compare to original data.
2072 std::vector<uint8> output; 2073 std::vector<uint8_t> output;
2073 ASSERT_EQ(Status::Success(), 2074 ASSERT_EQ(Status::Success(),
2074 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output)); 2075 ExportKey(blink::WebCryptoKeyFormatSpki, key, &output));
2075 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, output); 2076 EXPECT_BYTES_EQ_HEX(kPublicKeySpkiDerHex, output);
2076 2077
2077 // Failing case: Try to export a previously imported RSA public key in raw 2078 // Failing case: Try to export a previously imported RSA public key in raw
2078 // format (not allowed for a public key). 2079 // format (not allowed for a public key).
2079 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(), 2080 EXPECT_EQ(Status::ErrorUnsupportedExportKeyFormat(),
2080 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output)); 2081 ExportKey(blink::WebCryptoKeyFormatRaw, key, &output));
2081 2082
2082 // Failing case: Try to export a non-extractable key 2083 // Failing case: Try to export a non-extractable key
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
2122 EXPECT_TRUE(key.extractable()); 2123 EXPECT_TRUE(key.extractable());
2123 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); 2124 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages());
2124 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1, 2125 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha1,
2125 key.algorithm().rsaHashedParams()->hash().id()); 2126 key.algorithm().rsaHashedParams()->hash().id());
2126 EXPECT_EQ(kModulusLengthBits, 2127 EXPECT_EQ(kModulusLengthBits,
2127 key.algorithm().rsaHashedParams()->modulusLengthBits()); 2128 key.algorithm().rsaHashedParams()->modulusLengthBits());
2128 EXPECT_BYTES_EQ_HEX( 2129 EXPECT_BYTES_EQ_HEX(
2129 "010001", 2130 "010001",
2130 CryptoData(key.algorithm().rsaHashedParams()->publicExponent())); 2131 CryptoData(key.algorithm().rsaHashedParams()->publicExponent()));
2131 2132
2132 std::vector<uint8> exported_key; 2133 std::vector<uint8_t> exported_key;
2133 ASSERT_EQ(Status::Success(), 2134 ASSERT_EQ(Status::Success(),
2134 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_key)); 2135 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_key));
2135 EXPECT_BYTES_EQ_HEX(kPrivateKeyPkcs8DerHex, exported_key); 2136 EXPECT_BYTES_EQ_HEX(kPrivateKeyPkcs8DerHex, exported_key);
2136 2137
2137 // Failing case: Empty PKCS#8 data 2138 // Failing case: Empty PKCS#8 data
2138 EXPECT_EQ(Status::ErrorImportEmptyKeyData(), 2139 EXPECT_EQ(Status::ErrorImportEmptyKeyData(),
2139 ImportKey(blink::WebCryptoKeyFormatPkcs8, 2140 ImportKey(blink::WebCryptoKeyFormatPkcs8,
2140 CryptoData(std::vector<uint8>()), 2141 CryptoData(std::vector<uint8_t>()),
2141 CreateRsaHashedImportAlgorithm( 2142 CreateRsaHashedImportAlgorithm(
2142 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2143 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2143 blink::WebCryptoAlgorithmIdSha1), 2144 blink::WebCryptoAlgorithmIdSha1),
2144 true, 2145 true,
2145 blink::WebCryptoKeyUsageSign, 2146 blink::WebCryptoKeyUsageSign,
2146 &key)); 2147 &key));
2147 2148
2148 // Failing case: Bad DER encoding. 2149 // Failing case: Bad DER encoding.
2149 EXPECT_EQ( 2150 EXPECT_EQ(
2150 Status::DataError(), 2151 Status::DataError(),
(...skipping 29 matching lines...) Expand all
2180 ASSERT_EQ(Status::Success(), 2181 ASSERT_EQ(Status::Success(),
2181 ImportKey(blink::WebCryptoKeyFormatPkcs8, 2182 ImportKey(blink::WebCryptoKeyFormatPkcs8,
2182 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), 2183 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
2183 CreateRsaHashedImportAlgorithm( 2184 CreateRsaHashedImportAlgorithm(
2184 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2185 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2185 blink::WebCryptoAlgorithmIdSha1), 2186 blink::WebCryptoAlgorithmIdSha1),
2186 true, 2187 true,
2187 blink::WebCryptoKeyUsageSign, 2188 blink::WebCryptoKeyUsageSign,
2188 &key)); 2189 &key));
2189 2190
2190 std::vector<uint8> exported_key_jwk; 2191 std::vector<uint8_t> exported_key_jwk;
2191 ASSERT_EQ(Status::Success(), 2192 ASSERT_EQ(Status::Success(),
2192 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_key_jwk)); 2193 ExportKey(blink::WebCryptoKeyFormatJwk, key, &exported_key_jwk));
2193 2194
2194 // All of the optional parameters (p, q, dp, dq, qi) should be present in the 2195 // All of the optional parameters (p, q, dp, dq, qi) should be present in the
2195 // output. 2196 // output.
2196 const char* expected_jwk = 2197 const char* expected_jwk =
2197 "{\"alg\":\"RS1\",\"d\":\"M6UEKpCyfU9UUcqbu9C0R3GhAa-IQ0Cu-YhfKku-" 2198 "{\"alg\":\"RS1\",\"d\":\"M6UEKpCyfU9UUcqbu9C0R3GhAa-IQ0Cu-YhfKku-"
2198 "kuiUpySsPFaMj5eFOtB8AmbIxqPKCSnx6PESMYhEKfxNmuVf7olqEM5wfD7X5zTkRyejlXRQ" 2199 "kuiUpySsPFaMj5eFOtB8AmbIxqPKCSnx6PESMYhEKfxNmuVf7olqEM5wfD7X5zTkRyejlXRQ"
2199 "GlMmgxCcKrrKuig8MbS9L1PD7jfjUs7jT55QO9gMBiKtecbc7og1R8ajsyU\",\"dp\":" 2200 "GlMmgxCcKrrKuig8MbS9L1PD7jfjUs7jT55QO9gMBiKtecbc7og1R8ajsyU\",\"dp\":"
2200 "\"KPoTk4ZVvh-" 2201 "\"KPoTk4ZVvh-"
(...skipping 16 matching lines...) Expand all
2217 ASSERT_EQ(Status::Success(), 2218 ASSERT_EQ(Status::Success(),
2218 ImportKey(blink::WebCryptoKeyFormatJwk, 2219 ImportKey(blink::WebCryptoKeyFormatJwk,
2219 CryptoData(exported_key_jwk), 2220 CryptoData(exported_key_jwk),
2220 CreateRsaHashedImportAlgorithm( 2221 CreateRsaHashedImportAlgorithm(
2221 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2222 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2222 blink::WebCryptoAlgorithmIdSha1), 2223 blink::WebCryptoAlgorithmIdSha1),
2223 true, 2224 true,
2224 blink::WebCryptoKeyUsageSign, 2225 blink::WebCryptoKeyUsageSign,
2225 &key)); 2226 &key));
2226 2227
2227 std::vector<uint8> exported_key_pkcs8; 2228 std::vector<uint8_t> exported_key_pkcs8;
2228 ASSERT_EQ( 2229 ASSERT_EQ(
2229 Status::Success(), 2230 Status::Success(),
2230 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_key_pkcs8)); 2231 ExportKey(blink::WebCryptoKeyFormatPkcs8, key, &exported_key_pkcs8));
2231 2232
2232 ASSERT_EQ(CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), 2233 ASSERT_EQ(CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
2233 CryptoData(exported_key_pkcs8)); 2234 CryptoData(exported_key_pkcs8));
2234 } 2235 }
2235 2236
2236 // Tests importing multiple RSA private keys from JWK, and then exporting to 2237 // Tests importing multiple RSA private keys from JWK, and then exporting to
2237 // PKCS8. 2238 // PKCS8.
(...skipping 19 matching lines...) Expand all
2257 base::DictionaryValue* key_values; 2258 base::DictionaryValue* key_values;
2258 ASSERT_TRUE(key_list->GetDictionary(key_index, &key_values)); 2259 ASSERT_TRUE(key_list->GetDictionary(key_index, &key_values));
2259 2260
2260 // Get the JWK representation of the key. 2261 // Get the JWK representation of the key.
2261 base::DictionaryValue* key_jwk; 2262 base::DictionaryValue* key_jwk;
2262 ASSERT_TRUE(key_values->GetDictionary("jwk", &key_jwk)); 2263 ASSERT_TRUE(key_values->GetDictionary("jwk", &key_jwk));
2263 2264
2264 // Get the PKCS8 representation of the key. 2265 // Get the PKCS8 representation of the key.
2265 std::string pkcs8_hex_string; 2266 std::string pkcs8_hex_string;
2266 ASSERT_TRUE(key_values->GetString("pkcs8", &pkcs8_hex_string)); 2267 ASSERT_TRUE(key_values->GetString("pkcs8", &pkcs8_hex_string));
2267 std::vector<uint8> pkcs8_bytes = HexStringToBytes(pkcs8_hex_string); 2268 std::vector<uint8_t> pkcs8_bytes = HexStringToBytes(pkcs8_hex_string);
2268 2269
2269 // Get the modulus length for the key. 2270 // Get the modulus length for the key.
2270 int modulus_length_bits = 0; 2271 int modulus_length_bits = 0;
2271 ASSERT_TRUE(key_values->GetInteger("modulusLength", &modulus_length_bits)); 2272 ASSERT_TRUE(key_values->GetInteger("modulusLength", &modulus_length_bits));
2272 2273
2273 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2274 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2274 2275
2275 // Import the key from JWK. 2276 // Import the key from JWK.
2276 ASSERT_EQ( 2277 ASSERT_EQ(
2277 Status::Success(), 2278 Status::Success(),
2278 ImportKeyJwkFromDict(*key_jwk, 2279 ImportKeyJwkFromDict(*key_jwk,
2279 CreateRsaHashedImportAlgorithm( 2280 CreateRsaHashedImportAlgorithm(
2280 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2281 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2281 blink::WebCryptoAlgorithmIdSha256), 2282 blink::WebCryptoAlgorithmIdSha256),
2282 true, 2283 true,
2283 blink::WebCryptoKeyUsageSign, 2284 blink::WebCryptoKeyUsageSign,
2284 &private_key)); 2285 &private_key));
2285 2286
2286 live_keys.push_back(private_key); 2287 live_keys.push_back(private_key);
2287 2288
2288 EXPECT_EQ( 2289 EXPECT_EQ(
2289 modulus_length_bits, 2290 modulus_length_bits,
2290 static_cast<int>( 2291 static_cast<int>(
2291 private_key.algorithm().rsaHashedParams()->modulusLengthBits())); 2292 private_key.algorithm().rsaHashedParams()->modulusLengthBits()));
2292 2293
2293 // Export to PKCS8 and verify that it matches expectation. 2294 // Export to PKCS8 and verify that it matches expectation.
2294 std::vector<uint8> exported_key_pkcs8; 2295 std::vector<uint8_t> exported_key_pkcs8;
2295 ASSERT_EQ( 2296 ASSERT_EQ(
2296 Status::Success(), 2297 Status::Success(),
2297 ExportKey( 2298 ExportKey(
2298 blink::WebCryptoKeyFormatPkcs8, private_key, &exported_key_pkcs8)); 2299 blink::WebCryptoKeyFormatPkcs8, private_key, &exported_key_pkcs8));
2299 2300
2300 EXPECT_BYTES_EQ(pkcs8_bytes, exported_key_pkcs8); 2301 EXPECT_BYTES_EQ(pkcs8_bytes, exported_key_pkcs8);
2301 } 2302 }
2302 } 2303 }
2303 2304
2304 // Import an RSA private key using JWK. Next import a JWK containing the same 2305 // Import an RSA private key using JWK. Next import a JWK containing the same
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
2439 blink::WebCryptoKeyUsageSign, 2440 blink::WebCryptoKeyUsageSign,
2440 &key)); 2441 &key));
2441 2442
2442 } 2443 }
2443 2444
2444 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) { 2445 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsa)) {
2445 // Note: using unrealistic short key lengths here to avoid bogging down tests. 2446 // Note: using unrealistic short key lengths here to avoid bogging down tests.
2446 2447
2447 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha256) 2448 // Successful WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 key generation (sha256)
2448 const unsigned int modulus_length = 256; 2449 const unsigned int modulus_length = 256;
2449 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 2450 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
2450 blink::WebCryptoAlgorithm algorithm = 2451 blink::WebCryptoAlgorithm algorithm =
2451 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2452 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2452 blink::WebCryptoAlgorithmIdSha256, 2453 blink::WebCryptoAlgorithmIdSha256,
2453 modulus_length, 2454 modulus_length,
2454 public_exponent); 2455 public_exponent);
2455 bool extractable = true; 2456 bool extractable = true;
2456 const blink::WebCryptoKeyUsageMask usage_mask = 0; 2457 const blink::WebCryptoKeyUsageMask usage_mask = 0;
2457 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 2458 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
2458 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 2459 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
2459 2460
(...skipping 12 matching lines...) Expand all
2472 public_key.algorithm().rsaHashedParams()->hash().id()); 2473 public_key.algorithm().rsaHashedParams()->hash().id());
2473 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, 2474 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256,
2474 private_key.algorithm().rsaHashedParams()->hash().id()); 2475 private_key.algorithm().rsaHashedParams()->hash().id());
2475 EXPECT_TRUE(public_key.extractable()); 2476 EXPECT_TRUE(public_key.extractable());
2476 EXPECT_EQ(extractable, private_key.extractable()); 2477 EXPECT_EQ(extractable, private_key.extractable());
2477 EXPECT_EQ(usage_mask, public_key.usages()); 2478 EXPECT_EQ(usage_mask, public_key.usages());
2478 EXPECT_EQ(usage_mask, private_key.usages()); 2479 EXPECT_EQ(usage_mask, private_key.usages());
2479 2480
2480 // Try exporting the generated key pair, and then re-importing to verify that 2481 // Try exporting the generated key pair, and then re-importing to verify that
2481 // the exported data was valid. 2482 // the exported data was valid.
2482 std::vector<uint8> public_key_spki; 2483 std::vector<uint8_t> public_key_spki;
2483 EXPECT_EQ( 2484 EXPECT_EQ(
2484 Status::Success(), 2485 Status::Success(),
2485 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); 2486 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki));
2486 2487
2487 if (SupportsRsaKeyImport()) { 2488 if (SupportsRsaKeyImport()) {
2488 public_key = blink::WebCryptoKey::createNull(); 2489 public_key = blink::WebCryptoKey::createNull();
2489 EXPECT_EQ(Status::Success(), 2490 EXPECT_EQ(Status::Success(),
2490 ImportKey(blink::WebCryptoKeyFormatSpki, 2491 ImportKey(blink::WebCryptoKeyFormatSpki,
2491 CryptoData(public_key_spki), 2492 CryptoData(public_key_spki),
2492 CreateRsaHashedImportAlgorithm( 2493 CreateRsaHashedImportAlgorithm(
2493 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2494 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2494 blink::WebCryptoAlgorithmIdSha256), 2495 blink::WebCryptoAlgorithmIdSha256),
2495 true, 2496 true,
2496 usage_mask, 2497 usage_mask,
2497 &public_key)); 2498 &public_key));
2498 EXPECT_EQ(modulus_length, 2499 EXPECT_EQ(modulus_length,
2499 public_key.algorithm().rsaHashedParams()->modulusLengthBits()); 2500 public_key.algorithm().rsaHashedParams()->modulusLengthBits());
2500 2501
2501 std::vector<uint8> private_key_pkcs8; 2502 std::vector<uint8_t> private_key_pkcs8;
2502 EXPECT_EQ( 2503 EXPECT_EQ(
2503 Status::Success(), 2504 Status::Success(),
2504 ExportKey( 2505 ExportKey(
2505 blink::WebCryptoKeyFormatPkcs8, private_key, &private_key_pkcs8)); 2506 blink::WebCryptoKeyFormatPkcs8, private_key, &private_key_pkcs8));
2506 private_key = blink::WebCryptoKey::createNull(); 2507 private_key = blink::WebCryptoKey::createNull();
2507 EXPECT_EQ(Status::Success(), 2508 EXPECT_EQ(Status::Success(),
2508 ImportKey(blink::WebCryptoKeyFormatPkcs8, 2509 ImportKey(blink::WebCryptoKeyFormatPkcs8,
2509 CryptoData(private_key_pkcs8), 2510 CryptoData(private_key_pkcs8),
2510 CreateRsaHashedImportAlgorithm( 2511 CreateRsaHashedImportAlgorithm(
2511 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2512 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
(...skipping 10 matching lines...) Expand all
2522 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2523 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2523 blink::WebCryptoAlgorithmIdSha256, 2524 blink::WebCryptoAlgorithmIdSha256,
2524 0, 2525 0,
2525 public_exponent); 2526 public_exponent);
2526 EXPECT_EQ(Status::ErrorGenerateRsaZeroModulus(), 2527 EXPECT_EQ(Status::ErrorGenerateRsaZeroModulus(),
2527 GenerateKeyPair( 2528 GenerateKeyPair(
2528 algorithm, extractable, usage_mask, &public_key, &private_key)); 2529 algorithm, extractable, usage_mask, &public_key, &private_key));
2529 2530
2530 // Fail with bad exponent: larger than unsigned long. 2531 // Fail with bad exponent: larger than unsigned long.
2531 unsigned int exponent_length = sizeof(unsigned long) + 1; // NOLINT 2532 unsigned int exponent_length = sizeof(unsigned long) + 1; // NOLINT
2532 const std::vector<uint8> long_exponent(exponent_length, 0x01); 2533 const std::vector<uint8_t> long_exponent(exponent_length, 0x01);
2533 algorithm = 2534 algorithm =
2534 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2535 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2535 blink::WebCryptoAlgorithmIdSha256, 2536 blink::WebCryptoAlgorithmIdSha256,
2536 modulus_length, 2537 modulus_length,
2537 long_exponent); 2538 long_exponent);
2538 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(), 2539 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(),
2539 GenerateKeyPair( 2540 GenerateKeyPair(
2540 algorithm, extractable, usage_mask, &public_key, &private_key)); 2541 algorithm, extractable, usage_mask, &public_key, &private_key));
2541 2542
2542 // Fail with bad exponent: empty. 2543 // Fail with bad exponent: empty.
2543 const std::vector<uint8> empty_exponent; 2544 const std::vector<uint8_t> empty_exponent;
2544 algorithm = 2545 algorithm =
2545 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2546 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2546 blink::WebCryptoAlgorithmIdSha256, 2547 blink::WebCryptoAlgorithmIdSha256,
2547 modulus_length, 2548 modulus_length,
2548 empty_exponent); 2549 empty_exponent);
2549 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(), 2550 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(),
2550 GenerateKeyPair( 2551 GenerateKeyPair(
2551 algorithm, extractable, usage_mask, &public_key, &private_key)); 2552 algorithm, extractable, usage_mask, &public_key, &private_key));
2552 2553
2553 // Fail with bad exponent: all zeros. 2554 // Fail with bad exponent: all zeros.
2554 std::vector<uint8> exponent_with_leading_zeros(15, 0x00); 2555 std::vector<uint8_t> exponent_with_leading_zeros(15, 0x00);
2555 algorithm = 2556 algorithm =
2556 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2557 CreateRsaHashedKeyGenAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2557 blink::WebCryptoAlgorithmIdSha256, 2558 blink::WebCryptoAlgorithmIdSha256,
2558 modulus_length, 2559 modulus_length,
2559 exponent_with_leading_zeros); 2560 exponent_with_leading_zeros);
2560 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(), 2561 EXPECT_EQ(Status::ErrorGenerateKeyPublicExponent(),
2561 GenerateKeyPair( 2562 GenerateKeyPair(
2562 algorithm, extractable, usage_mask, &public_key, &private_key)); 2563 algorithm, extractable, usage_mask, &public_key, &private_key));
2563 2564
2564 // Key generation success using exponent with leading zeros. 2565 // Key generation success using exponent with leading zeros.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2605 private_key.algorithm().rsaHashedParams()->hash().id()); 2606 private_key.algorithm().rsaHashedParams()->hash().id());
2606 // Even though "extractable" was set to false, the public key remains 2607 // Even though "extractable" was set to false, the public key remains
2607 // extractable. 2608 // extractable.
2608 EXPECT_TRUE(public_key.extractable()); 2609 EXPECT_TRUE(public_key.extractable());
2609 EXPECT_FALSE(private_key.extractable()); 2610 EXPECT_FALSE(private_key.extractable());
2610 EXPECT_EQ(usage_mask, public_key.usages()); 2611 EXPECT_EQ(usage_mask, public_key.usages());
2611 EXPECT_EQ(usage_mask, private_key.usages()); 2612 EXPECT_EQ(usage_mask, private_key.usages());
2612 2613
2613 // Exporting a private key as SPKI format doesn't make sense. However this 2614 // Exporting a private key as SPKI format doesn't make sense. However this
2614 // will first fail because the key is not extractable. 2615 // will first fail because the key is not extractable.
2615 std::vector<uint8> output; 2616 std::vector<uint8_t> output;
2616 EXPECT_EQ(Status::ErrorKeyNotExtractable(), 2617 EXPECT_EQ(Status::ErrorKeyNotExtractable(),
2617 ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output)); 2618 ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output));
2618 2619
2619 // Re-generate an extractable private_key and try to export it as SPKI format. 2620 // Re-generate an extractable private_key and try to export it as SPKI format.
2620 // This should fail since spki is for public keys. 2621 // This should fail since spki is for public keys.
2621 EXPECT_EQ( 2622 EXPECT_EQ(
2622 Status::Success(), 2623 Status::Success(),
2623 GenerateKeyPair(algorithm, true, usage_mask, &public_key, &private_key)); 2624 GenerateKeyPair(algorithm, true, usage_mask, &public_key, &private_key));
2624 EXPECT_EQ(Status::ErrorUnexpectedKeyType(), 2625 EXPECT_EQ(Status::ErrorUnexpectedKeyType(),
2625 ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output)); 2626 ExportKey(blink::WebCryptoKeyFormatSpki, private_key, &output));
2626 } 2627 }
2627 2628
2628 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsaBadModulusLength)) { 2629 TEST_F(SharedCryptoTest, MAYBE(GenerateKeyPairRsaBadModulusLength)) {
2629 const unsigned int kBadModulus[] = { 2630 const unsigned int kBadModulus[] = {
2630 0, 2631 0,
2631 255, // Not a multiple of 8. 2632 255, // Not a multiple of 8.
2632 1023, // Not a multiple of 8. 2633 1023, // Not a multiple of 8.
2633 0xFFFFFFFF, // Cannot fit in a signed int. 2634 0xFFFFFFFF, // Cannot fit in a signed int.
2634 16384 + 8, // 16384 is the maxmimum length that NSS succeeds for. 2635 16384 + 8, // 16384 is the maxmimum length that NSS succeeds for.
2635 }; 2636 };
2636 2637
2637 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 2638 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
2638 2639
2639 for (size_t i = 0; i < arraysize(kBadModulus); ++i) { 2640 for (size_t i = 0; i < arraysize(kBadModulus); ++i) {
2640 const unsigned int modulus_length = kBadModulus[i]; 2641 const unsigned int modulus_length = kBadModulus[i];
2641 blink::WebCryptoAlgorithm algorithm = CreateRsaHashedKeyGenAlgorithm( 2642 blink::WebCryptoAlgorithm algorithm = CreateRsaHashedKeyGenAlgorithm(
2642 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 2643 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
2643 blink::WebCryptoAlgorithmIdSha256, 2644 blink::WebCryptoAlgorithmIdSha256,
2644 modulus_length, 2645 modulus_length,
2645 public_exponent); 2646 public_exponent);
2646 bool extractable = true; 2647 bool extractable = true;
2647 const blink::WebCryptoKeyUsageMask usage_mask = 0; 2648 const blink::WebCryptoKeyUsageMask usage_mask = 0;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2703 import_algorithm, 2704 import_algorithm,
2704 false, 2705 false,
2705 blink::WebCryptoKeyUsageVerify, 2706 blink::WebCryptoKeyUsageVerify,
2706 blink::WebCryptoKeyUsageSign, 2707 blink::WebCryptoKeyUsageSign,
2707 &public_key, 2708 &public_key,
2708 &private_key)); 2709 &private_key));
2709 2710
2710 blink::WebCryptoAlgorithm algorithm = 2711 blink::WebCryptoAlgorithm algorithm =
2711 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); 2712 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5);
2712 2713
2713 std::vector<uint8> signature; 2714 std::vector<uint8_t> signature;
2714 bool signature_match; 2715 bool signature_match;
2715 2716
2716 // Compute a signature. 2717 // Compute a signature.
2717 const std::vector<uint8> data = HexStringToBytes("010203040506070809"); 2718 const std::vector<uint8_t> data = HexStringToBytes("010203040506070809");
2718 ASSERT_EQ(Status::Success(), 2719 ASSERT_EQ(Status::Success(),
2719 Sign(algorithm, private_key, CryptoData(data), &signature)); 2720 Sign(algorithm, private_key, CryptoData(data), &signature));
2720 2721
2721 // Ensure truncated signature does not verify by passing one less byte. 2722 // Ensure truncated signature does not verify by passing one less byte.
2722 EXPECT_EQ( 2723 EXPECT_EQ(
2723 Status::Success(), 2724 Status::Success(),
2724 Verify(algorithm, 2725 Verify(algorithm,
2725 public_key, 2726 public_key,
2726 CryptoData(Uint8VectorStart(signature), signature.size() - 1), 2727 CryptoData(Uint8VectorStart(signature), signature.size() - 1),
2727 CryptoData(data), 2728 CryptoData(data),
2728 &signature_match)); 2729 &signature_match));
2729 EXPECT_FALSE(signature_match); 2730 EXPECT_FALSE(signature_match);
2730 2731
2731 // Ensure truncated signature does not verify by passing no bytes. 2732 // Ensure truncated signature does not verify by passing no bytes.
2732 EXPECT_EQ(Status::Success(), 2733 EXPECT_EQ(Status::Success(),
2733 Verify(algorithm, 2734 Verify(algorithm,
2734 public_key, 2735 public_key,
2735 CryptoData(), 2736 CryptoData(),
2736 CryptoData(data), 2737 CryptoData(data),
2737 &signature_match)); 2738 &signature_match));
2738 EXPECT_FALSE(signature_match); 2739 EXPECT_FALSE(signature_match);
2739 2740
2740 // Ensure corrupted signature does not verify. 2741 // Ensure corrupted signature does not verify.
2741 std::vector<uint8> corrupt_sig = signature; 2742 std::vector<uint8_t> corrupt_sig = signature;
2742 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1; 2743 corrupt_sig[corrupt_sig.size() / 2] ^= 0x1;
2743 EXPECT_EQ(Status::Success(), 2744 EXPECT_EQ(Status::Success(),
2744 Verify(algorithm, 2745 Verify(algorithm,
2745 public_key, 2746 public_key,
2746 CryptoData(corrupt_sig), 2747 CryptoData(corrupt_sig),
2747 CryptoData(data), 2748 CryptoData(data),
2748 &signature_match)); 2749 &signature_match));
2749 EXPECT_FALSE(signature_match); 2750 EXPECT_FALSE(signature_match);
2750 2751
2751 // Ensure signatures that are greater than the modulus size fail. 2752 // Ensure signatures that are greater than the modulus size fail.
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
2839 false, 2840 false,
2840 blink::WebCryptoKeyUsageVerify, 2841 blink::WebCryptoKeyUsageVerify,
2841 blink::WebCryptoKeyUsageSign, 2842 blink::WebCryptoKeyUsageSign,
2842 &public_key, 2843 &public_key,
2843 &private_key)); 2844 &private_key));
2844 2845
2845 blink::WebCryptoAlgorithm algorithm = 2846 blink::WebCryptoAlgorithm algorithm =
2846 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5); 2847 CreateAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5);
2847 2848
2848 // Validate the signatures are computed and verified as expected. 2849 // Validate the signatures are computed and verified as expected.
2849 std::vector<uint8> signature; 2850 std::vector<uint8_t> signature;
2850 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 2851 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
2851 SCOPED_TRACE(test_index); 2852 SCOPED_TRACE(test_index);
2852 2853
2853 base::DictionaryValue* test; 2854 base::DictionaryValue* test;
2854 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 2855 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
2855 2856
2856 std::vector<uint8> test_message = 2857 std::vector<uint8_t> test_message =
2857 GetBytesFromHexString(test, "message_hex"); 2858 GetBytesFromHexString(test, "message_hex");
2858 std::vector<uint8> test_signature = 2859 std::vector<uint8_t> test_signature =
2859 GetBytesFromHexString(test, "signature_hex"); 2860 GetBytesFromHexString(test, "signature_hex");
2860 2861
2861 signature.clear(); 2862 signature.clear();
2862 ASSERT_EQ( 2863 ASSERT_EQ(
2863 Status::Success(), 2864 Status::Success(),
2864 Sign(algorithm, private_key, CryptoData(test_message), &signature)); 2865 Sign(algorithm, private_key, CryptoData(test_message), &signature));
2865 EXPECT_BYTES_EQ(test_signature, signature); 2866 EXPECT_BYTES_EQ(test_signature, signature);
2866 2867
2867 bool is_match = false; 2868 bool is_match = false;
2868 ASSERT_EQ(Status::Success(), 2869 ASSERT_EQ(Status::Success(),
(...skipping 13 matching lines...) Expand all
2882 2883
2883 // Import a 128-bit Key Encryption Key (KEK) 2884 // Import a 128-bit Key Encryption Key (KEK)
2884 std::string key_raw_hex_in = "025a8cf3f08b4f6c5f33bbc76a471939"; 2885 std::string key_raw_hex_in = "025a8cf3f08b4f6c5f33bbc76a471939";
2885 ASSERT_EQ(Status::Success(), 2886 ASSERT_EQ(Status::Success(),
2886 ImportKey(blink::WebCryptoKeyFormatRaw, 2887 ImportKey(blink::WebCryptoKeyFormatRaw,
2887 CryptoData(HexStringToBytes(key_raw_hex_in)), 2888 CryptoData(HexStringToBytes(key_raw_hex_in)),
2888 algorithm, 2889 algorithm,
2889 true, 2890 true,
2890 blink::WebCryptoKeyUsageWrapKey, 2891 blink::WebCryptoKeyUsageWrapKey,
2891 &key)); 2892 &key));
2892 std::vector<uint8> key_raw_out; 2893 std::vector<uint8_t> key_raw_out;
2893 EXPECT_EQ(Status::Success(), 2894 EXPECT_EQ(Status::Success(),
2894 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); 2895 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out));
2895 EXPECT_BYTES_EQ_HEX(key_raw_hex_in, key_raw_out); 2896 EXPECT_BYTES_EQ_HEX(key_raw_hex_in, key_raw_out);
2896 2897
2897 // Import a 192-bit KEK 2898 // Import a 192-bit KEK
2898 key_raw_hex_in = "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103"; 2899 key_raw_hex_in = "c0192c6466b2370decbb62b2cfef4384544ffeb4d2fbc103";
2899 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), 2900 ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
2900 ImportKey(blink::WebCryptoKeyFormatRaw, 2901 ImportKey(blink::WebCryptoKeyFormatRaw,
2901 CryptoData(HexStringToBytes(key_raw_hex_in)), 2902 CryptoData(HexStringToBytes(key_raw_hex_in)),
2902 algorithm, 2903 algorithm,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2958 blink::WebCryptoKeyUsageWrapKey, 2959 blink::WebCryptoKeyUsageWrapKey,
2959 &key)); 2960 &key));
2960 } 2961 }
2961 2962
2962 TEST_F(SharedCryptoTest, MAYBE(UnwrapFailures)) { 2963 TEST_F(SharedCryptoTest, MAYBE(UnwrapFailures)) {
2963 // This test exercises the code path common to all unwrap operations. 2964 // This test exercises the code path common to all unwrap operations.
2964 scoped_ptr<base::ListValue> tests; 2965 scoped_ptr<base::ListValue> tests;
2965 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); 2966 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
2966 base::DictionaryValue* test; 2967 base::DictionaryValue* test;
2967 ASSERT_TRUE(tests->GetDictionary(0, &test)); 2968 ASSERT_TRUE(tests->GetDictionary(0, &test));
2968 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); 2969 const std::vector<uint8_t> test_kek = GetBytesFromHexString(test, "kek");
2969 const std::vector<uint8> test_ciphertext = 2970 const std::vector<uint8_t> test_ciphertext =
2970 GetBytesFromHexString(test, "ciphertext"); 2971 GetBytesFromHexString(test, "ciphertext");
2971 2972
2972 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 2973 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
2973 2974
2974 // Using a wrapping algorithm that does not match the wrapping key algorithm 2975 // Using a wrapping algorithm that does not match the wrapping key algorithm
2975 // should fail. 2976 // should fail.
2976 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( 2977 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
2977 test_kek, 2978 test_kek,
2978 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), 2979 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
2979 blink::WebCryptoKeyUsageUnwrapKey); 2980 blink::WebCryptoKeyUsageUnwrapKey);
(...skipping 10 matching lines...) Expand all
2990 } 2991 }
2991 2992
2992 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapKnownAnswer)) { 2993 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapKnownAnswer)) {
2993 scoped_ptr<base::ListValue> tests; 2994 scoped_ptr<base::ListValue> tests;
2994 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); 2995 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
2995 2996
2996 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 2997 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
2997 SCOPED_TRACE(test_index); 2998 SCOPED_TRACE(test_index);
2998 base::DictionaryValue* test; 2999 base::DictionaryValue* test;
2999 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 3000 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
3000 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); 3001 const std::vector<uint8_t> test_kek = GetBytesFromHexString(test, "kek");
3001 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 3002 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
3002 const std::vector<uint8> test_ciphertext = 3003 const std::vector<uint8_t> test_ciphertext =
3003 GetBytesFromHexString(test, "ciphertext"); 3004 GetBytesFromHexString(test, "ciphertext");
3004 const blink::WebCryptoAlgorithm wrapping_algorithm = 3005 const blink::WebCryptoAlgorithm wrapping_algorithm =
3005 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 3006 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
3006 3007
3007 // Import the wrapping key. 3008 // Import the wrapping key.
3008 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( 3009 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
3009 test_kek, 3010 test_kek,
3010 wrapping_algorithm, 3011 wrapping_algorithm,
3011 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); 3012 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey);
3012 3013
3013 // Import the key to be wrapped. 3014 // Import the key to be wrapped.
3014 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 3015 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
3015 test_key, 3016 test_key,
3016 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), 3017 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1),
3017 blink::WebCryptoKeyUsageSign); 3018 blink::WebCryptoKeyUsageSign);
3018 3019
3019 // Wrap the key and verify the ciphertext result against the known answer. 3020 // Wrap the key and verify the ciphertext result against the known answer.
3020 std::vector<uint8> wrapped_key; 3021 std::vector<uint8_t> wrapped_key;
3021 ASSERT_EQ(Status::Success(), 3022 ASSERT_EQ(Status::Success(),
3022 WrapKey(blink::WebCryptoKeyFormatRaw, 3023 WrapKey(blink::WebCryptoKeyFormatRaw,
3023 key, 3024 key,
3024 wrapping_key, 3025 wrapping_key,
3025 wrapping_algorithm, 3026 wrapping_algorithm,
3026 &wrapped_key)); 3027 &wrapped_key));
3027 EXPECT_BYTES_EQ(test_ciphertext, wrapped_key); 3028 EXPECT_BYTES_EQ(test_ciphertext, wrapped_key);
3028 3029
3029 // Unwrap the known ciphertext to get a new test_key. 3030 // Unwrap the known ciphertext to get a new test_key.
3030 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3031 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3031 ASSERT_EQ( 3032 ASSERT_EQ(
3032 Status::Success(), 3033 Status::Success(),
3033 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3034 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3034 CryptoData(test_ciphertext), 3035 CryptoData(test_ciphertext),
3035 wrapping_key, 3036 wrapping_key,
3036 wrapping_algorithm, 3037 wrapping_algorithm,
3037 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), 3038 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1),
3038 true, 3039 true,
3039 blink::WebCryptoKeyUsageSign, 3040 blink::WebCryptoKeyUsageSign,
3040 &unwrapped_key)); 3041 &unwrapped_key));
3041 EXPECT_FALSE(key.isNull()); 3042 EXPECT_FALSE(key.isNull());
3042 EXPECT_TRUE(key.handle()); 3043 EXPECT_TRUE(key.handle());
3043 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 3044 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
3044 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 3045 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
3045 EXPECT_EQ(true, key.extractable()); 3046 EXPECT_EQ(true, key.extractable());
3046 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages()); 3047 EXPECT_EQ(blink::WebCryptoKeyUsageSign, key.usages());
3047 3048
3048 // Export the new key and compare its raw bytes with the original known key. 3049 // Export the new key and compare its raw bytes with the original known key.
3049 std::vector<uint8> raw_key; 3050 std::vector<uint8_t> raw_key;
3050 EXPECT_EQ(Status::Success(), 3051 EXPECT_EQ(Status::Success(),
3051 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); 3052 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
3052 EXPECT_BYTES_EQ(test_key, raw_key); 3053 EXPECT_BYTES_EQ(test_key, raw_key);
3053 } 3054 }
3054 } 3055 }
3055 3056
3056 // Unwrap a HMAC key using AES-KW, and then try doing a sign/verify with the 3057 // Unwrap a HMAC key using AES-KW, and then try doing a sign/verify with the
3057 // unwrapped key 3058 // unwrapped key
3058 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyUnwrapSignVerifyHmac)) { 3059 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyUnwrapSignVerifyHmac)) {
3059 scoped_ptr<base::ListValue> tests; 3060 scoped_ptr<base::ListValue> tests;
3060 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); 3061 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
3061 3062
3062 base::DictionaryValue* test; 3063 base::DictionaryValue* test;
3063 ASSERT_TRUE(tests->GetDictionary(0, &test)); 3064 ASSERT_TRUE(tests->GetDictionary(0, &test));
3064 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); 3065 const std::vector<uint8_t> test_kek = GetBytesFromHexString(test, "kek");
3065 const std::vector<uint8> test_ciphertext = 3066 const std::vector<uint8_t> test_ciphertext =
3066 GetBytesFromHexString(test, "ciphertext"); 3067 GetBytesFromHexString(test, "ciphertext");
3067 const blink::WebCryptoAlgorithm wrapping_algorithm = 3068 const blink::WebCryptoAlgorithm wrapping_algorithm =
3068 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 3069 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
3069 3070
3070 // Import the wrapping key. 3071 // Import the wrapping key.
3071 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( 3072 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
3072 test_kek, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey); 3073 test_kek, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey);
3073 3074
3074 // Unwrap the known ciphertext. 3075 // Unwrap the known ciphertext.
3075 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 3076 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
3076 ASSERT_EQ( 3077 ASSERT_EQ(
3077 Status::Success(), 3078 Status::Success(),
3078 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3079 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3079 CryptoData(test_ciphertext), 3080 CryptoData(test_ciphertext),
3080 wrapping_key, 3081 wrapping_key,
3081 wrapping_algorithm, 3082 wrapping_algorithm,
3082 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1), 3083 CreateHmacImportAlgorithm(blink::WebCryptoAlgorithmIdSha1),
3083 false, 3084 false,
3084 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, 3085 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify,
3085 &key)); 3086 &key));
3086 3087
3087 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); 3088 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type());
3088 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id()); 3089 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, key.algorithm().id());
3089 EXPECT_FALSE(key.extractable()); 3090 EXPECT_FALSE(key.extractable());
3090 EXPECT_EQ(blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify, 3091 EXPECT_EQ(blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify,
3091 key.usages()); 3092 key.usages());
3092 3093
3093 // Sign an empty message and ensure it is verified. 3094 // Sign an empty message and ensure it is verified.
3094 std::vector<uint8> test_message; 3095 std::vector<uint8_t> test_message;
3095 std::vector<uint8> signature; 3096 std::vector<uint8_t> signature;
3096 3097
3097 ASSERT_EQ(Status::Success(), 3098 ASSERT_EQ(Status::Success(),
3098 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), 3099 Sign(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac),
3099 key, 3100 key,
3100 CryptoData(test_message), 3101 CryptoData(test_message),
3101 &signature)); 3102 &signature));
3102 3103
3103 EXPECT_GT(signature.size(), 0u); 3104 EXPECT_GT(signature.size(), 0u);
3104 3105
3105 bool verify_result; 3106 bool verify_result;
3106 ASSERT_EQ(Status::Success(), 3107 ASSERT_EQ(Status::Success(),
3107 Verify(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac), 3108 Verify(CreateAlgorithm(blink::WebCryptoAlgorithmIdHmac),
3108 key, 3109 key,
3109 CryptoData(signature), 3110 CryptoData(signature),
3110 CryptoData(test_message), 3111 CryptoData(test_message),
3111 &verify_result)); 3112 &verify_result));
3112 } 3113 }
3113 3114
3114 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapErrors)) { 3115 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyWrapUnwrapErrors)) {
3115 scoped_ptr<base::ListValue> tests; 3116 scoped_ptr<base::ListValue> tests;
3116 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); 3117 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
3117 base::DictionaryValue* test; 3118 base::DictionaryValue* test;
3118 // Use 256 bits of data with a 256-bit KEK 3119 // Use 256 bits of data with a 256-bit KEK
3119 ASSERT_TRUE(tests->GetDictionary(3, &test)); 3120 ASSERT_TRUE(tests->GetDictionary(3, &test));
3120 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); 3121 const std::vector<uint8_t> test_kek = GetBytesFromHexString(test, "kek");
3121 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 3122 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
3122 const std::vector<uint8> test_ciphertext = 3123 const std::vector<uint8_t> test_ciphertext =
3123 GetBytesFromHexString(test, "ciphertext"); 3124 GetBytesFromHexString(test, "ciphertext");
3124 const blink::WebCryptoAlgorithm wrapping_algorithm = 3125 const blink::WebCryptoAlgorithm wrapping_algorithm =
3125 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 3126 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
3126 const blink::WebCryptoAlgorithm key_algorithm = 3127 const blink::WebCryptoAlgorithm key_algorithm =
3127 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 3128 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
3128 // Import the wrapping key. 3129 // Import the wrapping key.
3129 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( 3130 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
3130 test_kek, 3131 test_kek,
3131 wrapping_algorithm, 3132 wrapping_algorithm,
3132 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); 3133 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey);
3133 // Import the key to be wrapped. 3134 // Import the key to be wrapped.
3134 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 3135 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
3135 test_key, 3136 test_key,
3136 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 3137 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
3137 blink::WebCryptoKeyUsageEncrypt); 3138 blink::WebCryptoKeyUsageEncrypt);
3138 3139
3139 // Unwrap with wrapped data too small must fail. 3140 // Unwrap with wrapped data too small must fail.
3140 const std::vector<uint8> small_data(test_ciphertext.begin(), 3141 const std::vector<uint8_t> small_data(test_ciphertext.begin(),
3141 test_ciphertext.begin() + 23); 3142 test_ciphertext.begin() + 23);
3142 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3143 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3143 EXPECT_EQ(Status::ErrorDataTooSmall(), 3144 EXPECT_EQ(Status::ErrorDataTooSmall(),
3144 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3145 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3145 CryptoData(small_data), 3146 CryptoData(small_data),
3146 wrapping_key, 3147 wrapping_key,
3147 wrapping_algorithm, 3148 wrapping_algorithm,
3148 key_algorithm, 3149 key_algorithm,
3149 true, 3150 true,
3150 blink::WebCryptoKeyUsageEncrypt, 3151 blink::WebCryptoKeyUsageEncrypt,
3151 &unwrapped_key)); 3152 &unwrapped_key));
3152 3153
3153 // Unwrap with wrapped data size not a multiple of 8 bytes must fail. 3154 // Unwrap with wrapped data size not a multiple of 8 bytes must fail.
3154 const std::vector<uint8> unaligned_data(test_ciphertext.begin(), 3155 const std::vector<uint8_t> unaligned_data(test_ciphertext.begin(),
3155 test_ciphertext.end() - 2); 3156 test_ciphertext.end() - 2);
3156 EXPECT_EQ(Status::ErrorInvalidAesKwDataLength(), 3157 EXPECT_EQ(Status::ErrorInvalidAesKwDataLength(),
3157 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3158 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3158 CryptoData(unaligned_data), 3159 CryptoData(unaligned_data),
3159 wrapping_key, 3160 wrapping_key,
3160 wrapping_algorithm, 3161 wrapping_algorithm,
3161 key_algorithm, 3162 key_algorithm,
3162 true, 3163 true,
3163 blink::WebCryptoKeyUsageEncrypt, 3164 blink::WebCryptoKeyUsageEncrypt,
3164 &unwrapped_key)); 3165 &unwrapped_key));
3165 } 3166 }
3166 3167
3167 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyUnwrapCorruptData)) { 3168 TEST_F(SharedCryptoTest, MAYBE(AesKwRawSymkeyUnwrapCorruptData)) {
3168 scoped_ptr<base::ListValue> tests; 3169 scoped_ptr<base::ListValue> tests;
3169 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests)); 3170 ASSERT_TRUE(ReadJsonTestFileToList("aes_kw.json", &tests));
3170 base::DictionaryValue* test; 3171 base::DictionaryValue* test;
3171 // Use 256 bits of data with a 256-bit KEK 3172 // Use 256 bits of data with a 256-bit KEK
3172 ASSERT_TRUE(tests->GetDictionary(3, &test)); 3173 ASSERT_TRUE(tests->GetDictionary(3, &test));
3173 const std::vector<uint8> test_kek = GetBytesFromHexString(test, "kek"); 3174 const std::vector<uint8_t> test_kek = GetBytesFromHexString(test, "kek");
3174 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 3175 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
3175 const std::vector<uint8> test_ciphertext = 3176 const std::vector<uint8_t> test_ciphertext =
3176 GetBytesFromHexString(test, "ciphertext"); 3177 GetBytesFromHexString(test, "ciphertext");
3177 const blink::WebCryptoAlgorithm wrapping_algorithm = 3178 const blink::WebCryptoAlgorithm wrapping_algorithm =
3178 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 3179 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
3179 3180
3180 // Import the wrapping key. 3181 // Import the wrapping key.
3181 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( 3182 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
3182 test_kek, 3183 test_kek,
3183 wrapping_algorithm, 3184 wrapping_algorithm,
3184 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey); 3185 blink::WebCryptoKeyUsageWrapKey | blink::WebCryptoKeyUsageUnwrapKey);
3185 3186
(...skipping 13 matching lines...) Expand all
3199 } 3200 }
3200 3201
3201 TEST_F(SharedCryptoTest, MAYBE(AesKwJwkSymkeyUnwrapKnownData)) { 3202 TEST_F(SharedCryptoTest, MAYBE(AesKwJwkSymkeyUnwrapKnownData)) {
3202 // The following data lists a known HMAC SHA-256 key, then a JWK 3203 // The following data lists a known HMAC SHA-256 key, then a JWK
3203 // representation of this key which was encrypted ("wrapped") using AES-KW and 3204 // representation of this key which was encrypted ("wrapped") using AES-KW and
3204 // the following wrapping key. 3205 // the following wrapping key.
3205 // For reference, the intermediate clear JWK is 3206 // For reference, the intermediate clear JWK is
3206 // {"alg":"HS256","ext":true,"k":<b64urlKey>,"key_ops":["verify"],"kty":"oct"} 3207 // {"alg":"HS256","ext":true,"k":<b64urlKey>,"key_ops":["verify"],"kty":"oct"}
3207 // (Not shown is space padding to ensure the cleartext meets the size 3208 // (Not shown is space padding to ensure the cleartext meets the size
3208 // requirements of the AES-KW algorithm.) 3209 // requirements of the AES-KW algorithm.)
3209 const std::vector<uint8> key_data = HexStringToBytes( 3210 const std::vector<uint8_t> key_data = HexStringToBytes(
3210 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"); 3211 "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F");
3211 const std::vector<uint8> wrapped_key_data = HexStringToBytes( 3212 const std::vector<uint8_t> wrapped_key_data = HexStringToBytes(
3212 "14E6380B35FDC5B72E1994764B6CB7BFDD64E7832894356AAEE6C3768FC3D0F115E6B0" 3213 "14E6380B35FDC5B72E1994764B6CB7BFDD64E7832894356AAEE6C3768FC3D0F115E6B0"
3213 "6729756225F999AA99FDF81FD6A359F1576D3D23DE6CB69C3937054EB497AC1E8C38D5" 3214 "6729756225F999AA99FDF81FD6A359F1576D3D23DE6CB69C3937054EB497AC1E8C38D5"
3214 "5E01B9783A20C8D930020932CF25926103002213D0FC37279888154FEBCEDF31832158" 3215 "5E01B9783A20C8D930020932CF25926103002213D0FC37279888154FEBCEDF31832158"
3215 "97938C5CFE5B10B4254D0C399F39D0"); 3216 "97938C5CFE5B10B4254D0C399F39D0");
3216 const std::vector<uint8> wrapping_key_data = 3217 const std::vector<uint8_t> wrapping_key_data =
3217 HexStringToBytes("000102030405060708090A0B0C0D0E0F"); 3218 HexStringToBytes("000102030405060708090A0B0C0D0E0F");
3218 const blink::WebCryptoAlgorithm wrapping_algorithm = 3219 const blink::WebCryptoAlgorithm wrapping_algorithm =
3219 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw); 3220 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw);
3220 3221
3221 // Import the wrapping key. 3222 // Import the wrapping key.
3222 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw( 3223 blink::WebCryptoKey wrapping_key = ImportSecretKeyFromRaw(
3223 wrapping_key_data, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey); 3224 wrapping_key_data, wrapping_algorithm, blink::WebCryptoKeyUsageUnwrapKey);
3224 3225
3225 // Unwrap the known wrapped key data to produce a new key 3226 // Unwrap the known wrapped key data to produce a new key
3226 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3227 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
(...skipping 13 matching lines...) Expand all
3240 EXPECT_TRUE(unwrapped_key.handle()); 3241 EXPECT_TRUE(unwrapped_key.handle());
3241 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, unwrapped_key.type()); 3242 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, unwrapped_key.type());
3242 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, unwrapped_key.algorithm().id()); 3243 EXPECT_EQ(blink::WebCryptoAlgorithmIdHmac, unwrapped_key.algorithm().id());
3243 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256, 3244 EXPECT_EQ(blink::WebCryptoAlgorithmIdSha256,
3244 unwrapped_key.algorithm().hmacParams()->hash().id()); 3245 unwrapped_key.algorithm().hmacParams()->hash().id());
3245 EXPECT_EQ(256u, unwrapped_key.algorithm().hmacParams()->lengthBits()); 3246 EXPECT_EQ(256u, unwrapped_key.algorithm().hmacParams()->lengthBits());
3246 EXPECT_EQ(true, unwrapped_key.extractable()); 3247 EXPECT_EQ(true, unwrapped_key.extractable());
3247 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, unwrapped_key.usages()); 3248 EXPECT_EQ(blink::WebCryptoKeyUsageVerify, unwrapped_key.usages());
3248 3249
3249 // Export the new key's raw data and compare to the known original. 3250 // Export the new key's raw data and compare to the known original.
3250 std::vector<uint8> raw_key; 3251 std::vector<uint8_t> raw_key;
3251 EXPECT_EQ(Status::Success(), 3252 EXPECT_EQ(Status::Success(),
3252 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); 3253 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
3253 EXPECT_BYTES_EQ(key_data, raw_key); 3254 EXPECT_BYTES_EQ(key_data, raw_key);
3254 } 3255 }
3255 3256
3256 // TODO(eroman): 3257 // TODO(eroman):
3257 // * Test decryption when the tag length exceeds input size 3258 // * Test decryption when the tag length exceeds input size
3258 // * Test decryption with empty input 3259 // * Test decryption with empty input
3259 // * Test decryption with tag length of 0. 3260 // * Test decryption with tag length of 0.
3260 TEST_F(SharedCryptoTest, AesGcmSampleSets) { 3261 TEST_F(SharedCryptoTest, AesGcmSampleSets) {
3261 // Some Linux test runners may not have a new enough version of NSS. 3262 // Some Linux test runners may not have a new enough version of NSS.
3262 if (!SupportsAesGcm()) { 3263 if (!SupportsAesGcm()) {
3263 LOG(WARNING) << "AES GCM not supported, skipping tests"; 3264 LOG(WARNING) << "AES GCM not supported, skipping tests";
3264 return; 3265 return;
3265 } 3266 }
3266 3267
3267 scoped_ptr<base::ListValue> tests; 3268 scoped_ptr<base::ListValue> tests;
3268 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests)); 3269 ASSERT_TRUE(ReadJsonTestFileToList("aes_gcm.json", &tests));
3269 3270
3270 // Note that WebCrypto appends the authentication tag to the ciphertext. 3271 // Note that WebCrypto appends the authentication tag to the ciphertext.
3271 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 3272 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
3272 SCOPED_TRACE(test_index); 3273 SCOPED_TRACE(test_index);
3273 base::DictionaryValue* test; 3274 base::DictionaryValue* test;
3274 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 3275 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
3275 3276
3276 const std::vector<uint8> test_key = GetBytesFromHexString(test, "key"); 3277 const std::vector<uint8_t> test_key = GetBytesFromHexString(test, "key");
3277 const std::vector<uint8> test_iv = GetBytesFromHexString(test, "iv"); 3278 const std::vector<uint8_t> test_iv = GetBytesFromHexString(test, "iv");
3278 const std::vector<uint8> test_additional_data = 3279 const std::vector<uint8_t> test_additional_data =
3279 GetBytesFromHexString(test, "additional_data"); 3280 GetBytesFromHexString(test, "additional_data");
3280 const std::vector<uint8> test_plain_text = 3281 const std::vector<uint8_t> test_plain_text =
3281 GetBytesFromHexString(test, "plain_text"); 3282 GetBytesFromHexString(test, "plain_text");
3282 const std::vector<uint8> test_authentication_tag = 3283 const std::vector<uint8_t> test_authentication_tag =
3283 GetBytesFromHexString(test, "authentication_tag"); 3284 GetBytesFromHexString(test, "authentication_tag");
3284 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8; 3285 const unsigned int test_tag_size_bits = test_authentication_tag.size() * 8;
3285 const std::vector<uint8> test_cipher_text = 3286 const std::vector<uint8_t> test_cipher_text =
3286 GetBytesFromHexString(test, "cipher_text"); 3287 GetBytesFromHexString(test, "cipher_text");
3287 3288
3288 #if defined(USE_OPENSSL) 3289 #if defined(USE_OPENSSL)
3289 // TODO(eroman): Add support for 256-bit keys. 3290 // TODO(eroman): Add support for 256-bit keys.
3290 if (test_key.size() == 32) { 3291 if (test_key.size() == 32) {
3291 LOG(WARNING) 3292 LOG(WARNING)
3292 << "OpenSSL doesn't support 256-bit keys for AES-GCM; skipping"; 3293 << "OpenSSL doesn't support 256-bit keys for AES-GCM; skipping";
3293 continue; 3294 continue;
3294 } 3295 }
3295 #endif 3296 #endif
3296 3297
3297 blink::WebCryptoKey key = ImportSecretKeyFromRaw( 3298 blink::WebCryptoKey key = ImportSecretKeyFromRaw(
3298 test_key, 3299 test_key,
3299 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), 3300 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm),
3300 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); 3301 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt);
3301 3302
3302 // Verify exported raw key is identical to the imported data 3303 // Verify exported raw key is identical to the imported data
3303 std::vector<uint8> raw_key; 3304 std::vector<uint8_t> raw_key;
3304 EXPECT_EQ(Status::Success(), 3305 EXPECT_EQ(Status::Success(),
3305 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key)); 3306 ExportKey(blink::WebCryptoKeyFormatRaw, key, &raw_key));
3306 3307
3307 EXPECT_BYTES_EQ(test_key, raw_key); 3308 EXPECT_BYTES_EQ(test_key, raw_key);
3308 3309
3309 // Test encryption. 3310 // Test encryption.
3310 std::vector<uint8> cipher_text; 3311 std::vector<uint8_t> cipher_text;
3311 std::vector<uint8> authentication_tag; 3312 std::vector<uint8_t> authentication_tag;
3312 EXPECT_EQ(Status::Success(), 3313 EXPECT_EQ(Status::Success(),
3313 AesGcmEncrypt(key, 3314 AesGcmEncrypt(key,
3314 test_iv, 3315 test_iv,
3315 test_additional_data, 3316 test_additional_data,
3316 test_tag_size_bits, 3317 test_tag_size_bits,
3317 test_plain_text, 3318 test_plain_text,
3318 &cipher_text, 3319 &cipher_text,
3319 &authentication_tag)); 3320 &authentication_tag));
3320 3321
3321 EXPECT_BYTES_EQ(test_cipher_text, cipher_text); 3322 EXPECT_BYTES_EQ(test_cipher_text, cipher_text);
3322 EXPECT_BYTES_EQ(test_authentication_tag, authentication_tag); 3323 EXPECT_BYTES_EQ(test_authentication_tag, authentication_tag);
3323 3324
3324 // Test decryption. 3325 // Test decryption.
3325 std::vector<uint8> plain_text; 3326 std::vector<uint8_t> plain_text;
3326 EXPECT_EQ(Status::Success(), 3327 EXPECT_EQ(Status::Success(),
3327 AesGcmDecrypt(key, 3328 AesGcmDecrypt(key,
3328 test_iv, 3329 test_iv,
3329 test_additional_data, 3330 test_additional_data,
3330 test_tag_size_bits, 3331 test_tag_size_bits,
3331 test_cipher_text, 3332 test_cipher_text,
3332 test_authentication_tag, 3333 test_authentication_tag,
3333 &plain_text)); 3334 &plain_text));
3334 EXPECT_BYTES_EQ(test_plain_text, plain_text); 3335 EXPECT_BYTES_EQ(test_plain_text, plain_text);
3335 3336
(...skipping 25 matching lines...) Expand all
3361 EXPECT_EQ(Status::OperationError(), 3362 EXPECT_EQ(Status::OperationError(),
3362 AesGcmDecrypt(key, 3363 AesGcmDecrypt(key,
3363 test_iv, 3364 test_iv,
3364 test_additional_data, 3365 test_additional_data,
3365 test_tag_size_bits, 3366 test_tag_size_bits,
3366 test_cipher_text, 3367 test_cipher_text,
3367 Corrupted(test_authentication_tag), 3368 Corrupted(test_authentication_tag),
3368 &plain_text)); 3369 &plain_text));
3369 3370
3370 // Try different incorrect tag lengths 3371 // Try different incorrect tag lengths
3371 uint8 kAlternateTagLengths[] = {0, 8, 96, 120, 128, 160, 255}; 3372 uint8_t kAlternateTagLengths[] = {0, 8, 96, 120, 128, 160, 255};
3372 for (size_t tag_i = 0; tag_i < arraysize(kAlternateTagLengths); ++tag_i) { 3373 for (size_t tag_i = 0; tag_i < arraysize(kAlternateTagLengths); ++tag_i) {
3373 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i]; 3374 unsigned int wrong_tag_size_bits = kAlternateTagLengths[tag_i];
3374 if (test_tag_size_bits == wrong_tag_size_bits) 3375 if (test_tag_size_bits == wrong_tag_size_bits)
3375 continue; 3376 continue;
3376 EXPECT_NE(Status::Success(), 3377 EXPECT_NE(Status::Success(),
3377 AesGcmDecrypt(key, 3378 AesGcmDecrypt(key,
3378 test_iv, 3379 test_iv,
3379 test_additional_data, 3380 test_additional_data,
3380 wrong_tag_size_bits, 3381 wrong_tag_size_bits,
3381 test_cipher_text, 3382 test_cipher_text,
3382 test_authentication_tag, 3383 test_authentication_tag,
3383 &plain_text)); 3384 &plain_text));
3384 } 3385 }
3385 } 3386 }
3386 } 3387 }
3387 3388
3388 // AES 192-bit is not allowed: http://crbug.com/381829 3389 // AES 192-bit is not allowed: http://crbug.com/381829
3389 TEST_F(SharedCryptoTest, MAYBE(ImportAesCbc192Raw)) { 3390 TEST_F(SharedCryptoTest, MAYBE(ImportAesCbc192Raw)) {
3390 std::vector<uint8> key_raw(24, 0); 3391 std::vector<uint8_t> key_raw(24, 0);
3391 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 3392 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
3392 Status status = ImportKey(blink::WebCryptoKeyFormatRaw, 3393 Status status = ImportKey(blink::WebCryptoKeyFormatRaw,
3393 CryptoData(key_raw), 3394 CryptoData(key_raw),
3394 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc), 3395 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc),
3395 true, 3396 true,
3396 blink::WebCryptoKeyUsageEncrypt, 3397 blink::WebCryptoKeyUsageEncrypt,
3397 &key); 3398 &key);
3398 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), status); 3399 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), status);
3399 } 3400 }
3400 3401
(...skipping 20 matching lines...) Expand all
3421 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 3422 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
3422 Status status = GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(192), 3423 Status status = GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(192),
3423 true, 3424 true,
3424 blink::WebCryptoKeyUsageEncrypt, 3425 blink::WebCryptoKeyUsageEncrypt,
3425 &key); 3426 &key);
3426 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), status); 3427 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), status);
3427 } 3428 }
3428 3429
3429 // AES 192-bit is not allowed: http://crbug.com/381829 3430 // AES 192-bit is not allowed: http://crbug.com/381829
3430 TEST_F(SharedCryptoTest, MAYBE(UnwrapAesCbc192)) { 3431 TEST_F(SharedCryptoTest, MAYBE(UnwrapAesCbc192)) {
3431 std::vector<uint8> wrapping_key_data(16, 0); 3432 std::vector<uint8_t> wrapping_key_data(16, 0);
3432 std::vector<uint8> wrapped_key = HexStringToBytes( 3433 std::vector<uint8_t> wrapped_key = HexStringToBytes(
3433 "1A07ACAB6C906E50883173C29441DB1DE91D34F45C435B5F99C822867FB3956F"); 3434 "1A07ACAB6C906E50883173C29441DB1DE91D34F45C435B5F99C822867FB3956F");
3434 3435
3435 blink::WebCryptoKey wrapping_key = 3436 blink::WebCryptoKey wrapping_key =
3436 ImportSecretKeyFromRaw(wrapping_key_data, 3437 ImportSecretKeyFromRaw(wrapping_key_data,
3437 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw), 3438 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesKw),
3438 blink::WebCryptoKeyUsageUnwrapKey); 3439 blink::WebCryptoKeyUsageUnwrapKey);
3439 3440
3440 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3441 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3441 ASSERT_EQ(Status::ErrorAes192BitUnsupported(), 3442 ASSERT_EQ(Status::ErrorAes192BitUnsupported(),
3442 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3443 UnwrapKey(blink::WebCryptoKeyFormatRaw,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
3587 ASSERT_EQ(Status::Success(), 3588 ASSERT_EQ(Status::Success(),
3588 ImportKeyJwkFromDict( 3589 ImportKeyJwkFromDict(
3589 *jwk.get(), 3590 *jwk.get(),
3590 CreateRsaHashedImportAlgorithm( 3591 CreateRsaHashedImportAlgorithm(
3591 blink::WebCryptoAlgorithmIdRsaOaep, test_data.hash_alg), 3592 blink::WebCryptoAlgorithmIdRsaOaep, test_data.hash_alg),
3592 true, 3593 true,
3593 blink::WebCryptoKeyUsageEncrypt, 3594 blink::WebCryptoKeyUsageEncrypt,
3594 &public_key)); 3595 &public_key));
3595 3596
3596 // Now export the key as JWK and verify its contents 3597 // Now export the key as JWK and verify its contents
3597 std::vector<uint8> jwk_data; 3598 std::vector<uint8_t> jwk_data;
3598 ASSERT_EQ(Status::Success(), 3599 ASSERT_EQ(Status::Success(),
3599 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk_data)); 3600 ExportKey(blink::WebCryptoKeyFormatJwk, public_key, &jwk_data));
3600 EXPECT_TRUE(VerifyPublicJwk(jwk_data, 3601 EXPECT_TRUE(VerifyPublicJwk(jwk_data,
3601 test_data.expected_jwk_alg, 3602 test_data.expected_jwk_alg,
3602 kPublicKeyModulusHex, 3603 kPublicKeyModulusHex,
3603 kPublicKeyExponentHex, 3604 kPublicKeyExponentHex,
3604 blink::WebCryptoKeyUsageEncrypt)); 3605 blink::WebCryptoKeyUsageEncrypt));
3605 } 3606 }
3606 } 3607 }
3607 3608
3608 TEST_F(SharedCryptoRsaOaepTest, EncryptDecryptKnownAnswerTest) { 3609 TEST_F(SharedCryptoRsaOaepTest, EncryptDecryptKnownAnswerTest) {
3609 if (!SupportsRsaOaep()) { 3610 if (!SupportsRsaOaep()) {
3610 LOG(WARNING) << "RSA-OAEP support not present; skipping."; 3611 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3611 return; 3612 return;
3612 } 3613 }
3613 3614
3614 scoped_ptr<base::ListValue> tests; 3615 scoped_ptr<base::ListValue> tests;
3615 ASSERT_TRUE(ReadJsonTestFileToList("rsa_oaep.json", &tests)); 3616 ASSERT_TRUE(ReadJsonTestFileToList("rsa_oaep.json", &tests));
3616 3617
3617 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) { 3618 for (size_t test_index = 0; test_index < tests->GetSize(); ++test_index) {
3618 SCOPED_TRACE(test_index); 3619 SCOPED_TRACE(test_index);
3619 3620
3620 base::DictionaryValue* test = NULL; 3621 base::DictionaryValue* test = NULL;
3621 ASSERT_TRUE(tests->GetDictionary(test_index, &test)); 3622 ASSERT_TRUE(tests->GetDictionary(test_index, &test));
3622 3623
3623 blink::WebCryptoAlgorithm digest_algorithm = 3624 blink::WebCryptoAlgorithm digest_algorithm =
3624 GetDigestAlgorithm(test, "hash"); 3625 GetDigestAlgorithm(test, "hash");
3625 ASSERT_FALSE(digest_algorithm.isNull()); 3626 ASSERT_FALSE(digest_algorithm.isNull());
3626 std::vector<uint8> public_key_der = 3627 std::vector<uint8_t> public_key_der =
3627 GetBytesFromHexString(test, "public_key"); 3628 GetBytesFromHexString(test, "public_key");
3628 std::vector<uint8> private_key_der = 3629 std::vector<uint8_t> private_key_der =
3629 GetBytesFromHexString(test, "private_key"); 3630 GetBytesFromHexString(test, "private_key");
3630 std::vector<uint8> ciphertext = GetBytesFromHexString(test, "ciphertext"); 3631 std::vector<uint8_t> ciphertext = GetBytesFromHexString(test, "ciphertext");
3631 std::vector<uint8> plaintext = GetBytesFromHexString(test, "plaintext"); 3632 std::vector<uint8_t> plaintext = GetBytesFromHexString(test, "plaintext");
3632 std::vector<uint8> label = GetBytesFromHexString(test, "label"); 3633 std::vector<uint8_t> label = GetBytesFromHexString(test, "label");
3633 3634
3634 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( 3635 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
3635 blink::WebCryptoAlgorithmIdRsaOaep, digest_algorithm.id()); 3636 blink::WebCryptoAlgorithmIdRsaOaep, digest_algorithm.id());
3636 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 3637 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3637 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 3638 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3638 3639
3639 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(public_key_der, 3640 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(public_key_der,
3640 private_key_der, 3641 private_key_der,
3641 import_algorithm, 3642 import_algorithm,
3642 false, 3643 false,
3643 blink::WebCryptoKeyUsageEncrypt, 3644 blink::WebCryptoKeyUsageEncrypt,
3644 blink::WebCryptoKeyUsageDecrypt, 3645 blink::WebCryptoKeyUsageDecrypt,
3645 &public_key, 3646 &public_key,
3646 &private_key)); 3647 &private_key));
3647 3648
3648 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label); 3649 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3649 std::vector<uint8> decrypted_data; 3650 std::vector<uint8_t> decrypted_data;
3650 ASSERT_EQ(Status::Success(), 3651 ASSERT_EQ(Status::Success(),
3651 Decrypt(op_algorithm, 3652 Decrypt(op_algorithm,
3652 private_key, 3653 private_key,
3653 CryptoData(ciphertext), 3654 CryptoData(ciphertext),
3654 &decrypted_data)); 3655 &decrypted_data));
3655 EXPECT_BYTES_EQ(plaintext, decrypted_data); 3656 EXPECT_BYTES_EQ(plaintext, decrypted_data);
3656 std::vector<uint8> encrypted_data; 3657 std::vector<uint8_t> encrypted_data;
3657 ASSERT_EQ( 3658 ASSERT_EQ(
3658 Status::Success(), 3659 Status::Success(),
3659 Encrypt( 3660 Encrypt(
3660 op_algorithm, public_key, CryptoData(plaintext), &encrypted_data)); 3661 op_algorithm, public_key, CryptoData(plaintext), &encrypted_data));
3661 std::vector<uint8> redecrypted_data; 3662 std::vector<uint8_t> redecrypted_data;
3662 ASSERT_EQ(Status::Success(), 3663 ASSERT_EQ(Status::Success(),
3663 Decrypt(op_algorithm, 3664 Decrypt(op_algorithm,
3664 private_key, 3665 private_key,
3665 CryptoData(encrypted_data), 3666 CryptoData(encrypted_data),
3666 &redecrypted_data)); 3667 &redecrypted_data));
3667 EXPECT_BYTES_EQ(plaintext, redecrypted_data); 3668 EXPECT_BYTES_EQ(plaintext, redecrypted_data);
3668 } 3669 }
3669 } 3670 }
3670 3671
3671 TEST_F(SharedCryptoRsaOaepTest, EncryptWithLargeMessageFails) { 3672 TEST_F(SharedCryptoRsaOaepTest, EncryptWithLargeMessageFails) {
(...skipping 19 matching lines...) Expand all
3691 // The maximum size of an encrypted message is: 3692 // The maximum size of an encrypted message is:
3692 // modulus length 3693 // modulus length
3693 // - 1 (leading octet) 3694 // - 1 (leading octet)
3694 // - hash size (maskedSeed) 3695 // - hash size (maskedSeed)
3695 // - hash size (lHash portion of maskedDB) 3696 // - hash size (lHash portion of maskedDB)
3696 // - 1 (at least one octet for the padding string) 3697 // - 1 (at least one octet for the padding string)
3697 size_t kMaxMessageSize = (kModulusLengthBits / 8) - 2 - (2 * kHashSize); 3698 size_t kMaxMessageSize = (kModulusLengthBits / 8) - 2 - (2 * kHashSize);
3698 3699
3699 // The label has no influence on the maximum message size. For simplicity, 3700 // The label has no influence on the maximum message size. For simplicity,
3700 // use the empty string. 3701 // use the empty string.
3701 std::vector<uint8> label; 3702 std::vector<uint8_t> label;
3702 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label); 3703 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3703 3704
3704 // Test that a message just before the boundary succeeds. 3705 // Test that a message just before the boundary succeeds.
3705 std::string large_message; 3706 std::string large_message;
3706 large_message.resize(kMaxMessageSize - 1, 'A'); 3707 large_message.resize(kMaxMessageSize - 1, 'A');
3707 3708
3708 std::vector<uint8> ciphertext; 3709 std::vector<uint8_t> ciphertext;
3709 ASSERT_EQ( 3710 ASSERT_EQ(
3710 Status::Success(), 3711 Status::Success(),
3711 Encrypt( 3712 Encrypt(
3712 op_algorithm, public_key, CryptoData(large_message), &ciphertext)); 3713 op_algorithm, public_key, CryptoData(large_message), &ciphertext));
3713 3714
3714 // Test that a message at the boundary succeeds. 3715 // Test that a message at the boundary succeeds.
3715 large_message.resize(kMaxMessageSize, 'A'); 3716 large_message.resize(kMaxMessageSize, 'A');
3716 ciphertext.clear(); 3717 ciphertext.clear();
3717 3718
3718 ASSERT_EQ( 3719 ASSERT_EQ(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3750 ASSERT_EQ(Status::Success(), 3751 ASSERT_EQ(Status::Success(),
3751 ImportKeyJwkFromDict(*jwk.get(), 3752 ImportKeyJwkFromDict(*jwk.get(),
3752 CreateRsaHashedImportAlgorithm( 3753 CreateRsaHashedImportAlgorithm(
3753 blink::WebCryptoAlgorithmIdRsaOaep, kHash), 3754 blink::WebCryptoAlgorithmIdRsaOaep, kHash),
3754 true, 3755 true,
3755 blink::WebCryptoKeyUsageEncrypt, 3756 blink::WebCryptoKeyUsageEncrypt,
3756 &public_key)); 3757 &public_key));
3757 3758
3758 // The label has no influence on the maximum message size. For simplicity, 3759 // The label has no influence on the maximum message size. For simplicity,
3759 // use the empty string. 3760 // use the empty string.
3760 std::vector<uint8> label; 3761 std::vector<uint8_t> label;
3761 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label); 3762 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3762 3763
3763 std::string small_message("A"); 3764 std::string small_message("A");
3764 std::vector<uint8> ciphertext; 3765 std::vector<uint8_t> ciphertext;
3765 // This is an operation error, as the internal consistency checking of the 3766 // This is an operation error, as the internal consistency checking of the
3766 // algorithm parameters is up to the implementation. 3767 // algorithm parameters is up to the implementation.
3767 ASSERT_EQ( 3768 ASSERT_EQ(
3768 Status::OperationError(), 3769 Status::OperationError(),
3769 Encrypt( 3770 Encrypt(
3770 op_algorithm, public_key, CryptoData(small_message), &ciphertext)); 3771 op_algorithm, public_key, CryptoData(small_message), &ciphertext));
3771 } 3772 }
3772 3773
3773 TEST_F(SharedCryptoRsaOaepTest, DecryptWithLargeMessageFails) { 3774 TEST_F(SharedCryptoRsaOaepTest, DecryptWithLargeMessageFails) {
3774 if (!SupportsRsaOaep()) { 3775 if (!SupportsRsaOaep()) {
3775 LOG(WARNING) << "RSA-OAEP support not present; skipping."; 3776 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3776 return; 3777 return;
3777 } 3778 }
3778 3779
3779 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 3780 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3780 ASSERT_EQ(Status::Success(), 3781 ASSERT_EQ(Status::Success(),
3781 ImportKey(blink::WebCryptoKeyFormatPkcs8, 3782 ImportKey(blink::WebCryptoKeyFormatPkcs8,
3782 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)), 3783 CryptoData(HexStringToBytes(kPrivateKeyPkcs8DerHex)),
3783 CreateRsaHashedImportAlgorithm( 3784 CreateRsaHashedImportAlgorithm(
3784 blink::WebCryptoAlgorithmIdRsaOaep, 3785 blink::WebCryptoAlgorithmIdRsaOaep,
3785 blink::WebCryptoAlgorithmIdSha1), 3786 blink::WebCryptoAlgorithmIdSha1),
3786 true, 3787 true,
3787 blink::WebCryptoKeyUsageDecrypt, 3788 blink::WebCryptoKeyUsageDecrypt,
3788 &private_key)); 3789 &private_key));
3789 3790
3790 // The label has no influence on the maximum message size. For simplicity, 3791 // The label has no influence on the maximum message size. For simplicity,
3791 // use the empty string. 3792 // use the empty string.
3792 std::vector<uint8> label; 3793 std::vector<uint8_t> label;
3793 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label); 3794 blink::WebCryptoAlgorithm op_algorithm = CreateRsaOaepAlgorithm(label);
3794 3795
3795 std::string large_dummy_message(kModulusLengthBits / 8, 'A'); 3796 std::string large_dummy_message(kModulusLengthBits / 8, 'A');
3796 std::vector<uint8> plaintext; 3797 std::vector<uint8_t> plaintext;
3797 3798
3798 ASSERT_EQ(Status::OperationError(), 3799 ASSERT_EQ(Status::OperationError(),
3799 Decrypt(op_algorithm, 3800 Decrypt(op_algorithm,
3800 private_key, 3801 private_key,
3801 CryptoData(large_dummy_message), 3802 CryptoData(large_dummy_message),
3802 &plaintext)); 3803 &plaintext));
3803 } 3804 }
3804 3805
3805 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapRawKey) { 3806 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapRawKey) {
3806 if (!SupportsRsaOaep()) { 3807 if (!SupportsRsaOaep()) {
3807 LOG(WARNING) << "RSA-OAEP support not present; skipping."; 3808 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3808 return; 3809 return;
3809 } 3810 }
3810 3811
3811 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm( 3812 blink::WebCryptoAlgorithm import_algorithm = CreateRsaHashedImportAlgorithm(
3812 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1); 3813 blink::WebCryptoAlgorithmIdRsaOaep, blink::WebCryptoAlgorithmIdSha1);
3813 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 3814 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
3814 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 3815 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
3815 3816
3816 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair( 3817 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3817 HexStringToBytes(kPublicKeySpkiDerHex), 3818 HexStringToBytes(kPublicKeySpkiDerHex),
3818 HexStringToBytes(kPrivateKeyPkcs8DerHex), 3819 HexStringToBytes(kPrivateKeyPkcs8DerHex),
3819 import_algorithm, 3820 import_algorithm,
3820 false, 3821 false,
3821 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, 3822 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey,
3822 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey, 3823 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey,
3823 &public_key, 3824 &public_key,
3824 &private_key)); 3825 &private_key));
3825 3826
3826 std::vector<uint8> label; 3827 std::vector<uint8_t> label;
3827 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label); 3828 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
3828 3829
3829 const std::string key_hex = "000102030405060708090A0B0C0D0E0F"; 3830 const std::string key_hex = "000102030405060708090A0B0C0D0E0F";
3830 const blink::WebCryptoAlgorithm key_algorithm = 3831 const blink::WebCryptoAlgorithm key_algorithm =
3831 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 3832 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
3832 3833
3833 blink::WebCryptoKey key = 3834 blink::WebCryptoKey key =
3834 ImportSecretKeyFromRaw(HexStringToBytes(key_hex), 3835 ImportSecretKeyFromRaw(HexStringToBytes(key_hex),
3835 key_algorithm, 3836 key_algorithm,
3836 blink::WebCryptoKeyUsageEncrypt); 3837 blink::WebCryptoKeyUsageEncrypt);
3837 ASSERT_FALSE(key.isNull()); 3838 ASSERT_FALSE(key.isNull());
3838 3839
3839 std::vector<uint8> wrapped_key; 3840 std::vector<uint8_t> wrapped_key;
3840 ASSERT_EQ(Status::Success(), 3841 ASSERT_EQ(Status::Success(),
3841 WrapKey(blink::WebCryptoKeyFormatRaw, 3842 WrapKey(blink::WebCryptoKeyFormatRaw,
3842 key, 3843 key,
3843 public_key, 3844 public_key,
3844 wrapping_algorithm, 3845 wrapping_algorithm,
3845 &wrapped_key)); 3846 &wrapped_key));
3846 3847
3847 // Verify that |wrapped_key| can be decrypted and yields the key data. 3848 // Verify that |wrapped_key| can be decrypted and yields the key data.
3848 // Because |private_key| supports both decrypt and unwrap, this is valid. 3849 // Because |private_key| supports both decrypt and unwrap, this is valid.
3849 std::vector<uint8> decrypted_key; 3850 std::vector<uint8_t> decrypted_key;
3850 ASSERT_EQ(Status::Success(), 3851 ASSERT_EQ(Status::Success(),
3851 Decrypt(wrapping_algorithm, 3852 Decrypt(wrapping_algorithm,
3852 private_key, 3853 private_key,
3853 CryptoData(wrapped_key), 3854 CryptoData(wrapped_key),
3854 &decrypted_key)); 3855 &decrypted_key));
3855 EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key); 3856 EXPECT_BYTES_EQ_HEX(key_hex, decrypted_key);
3856 3857
3857 // Now attempt to unwrap the key, which should also decrypt the data. 3858 // Now attempt to unwrap the key, which should also decrypt the data.
3858 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3859 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3859 ASSERT_EQ(Status::Success(), 3860 ASSERT_EQ(Status::Success(),
3860 UnwrapKey(blink::WebCryptoKeyFormatRaw, 3861 UnwrapKey(blink::WebCryptoKeyFormatRaw,
3861 CryptoData(wrapped_key), 3862 CryptoData(wrapped_key),
3862 private_key, 3863 private_key,
3863 wrapping_algorithm, 3864 wrapping_algorithm,
3864 key_algorithm, 3865 key_algorithm,
3865 true, 3866 true,
3866 blink::WebCryptoKeyUsageEncrypt, 3867 blink::WebCryptoKeyUsageEncrypt,
3867 &unwrapped_key)); 3868 &unwrapped_key));
3868 ASSERT_FALSE(unwrapped_key.isNull()); 3869 ASSERT_FALSE(unwrapped_key.isNull());
3869 3870
3870 std::vector<uint8> raw_key; 3871 std::vector<uint8_t> raw_key;
3871 ASSERT_EQ(Status::Success(), 3872 ASSERT_EQ(Status::Success(),
3872 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); 3873 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
3873 EXPECT_BYTES_EQ_HEX(key_hex, raw_key); 3874 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
3874 } 3875 }
3875 3876
3876 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapJwkSymKey) { 3877 TEST_F(SharedCryptoRsaOaepTest, WrapUnwrapJwkSymKey) {
3877 if (!SupportsRsaOaep()) { 3878 if (!SupportsRsaOaep()) {
3878 LOG(WARNING) << "RSA-OAEP support not present; skipping."; 3879 LOG(WARNING) << "RSA-OAEP support not present; skipping.";
3879 return; 3880 return;
3880 } 3881 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
3934 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair( 3935 ASSERT_NO_FATAL_FAILURE(ImportRsaKeyPair(
3935 HexStringToBytes(kPublicKey2048SpkiDerHex), 3936 HexStringToBytes(kPublicKey2048SpkiDerHex),
3936 HexStringToBytes(kPrivateKey2048Pkcs8DerHex), 3937 HexStringToBytes(kPrivateKey2048Pkcs8DerHex),
3937 import_algorithm, 3938 import_algorithm,
3938 false, 3939 false,
3939 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey, 3940 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageWrapKey,
3940 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey, 3941 blink::WebCryptoKeyUsageDecrypt | blink::WebCryptoKeyUsageUnwrapKey,
3941 &public_key, 3942 &public_key,
3942 &private_key)); 3943 &private_key));
3943 3944
3944 std::vector<uint8> label; 3945 std::vector<uint8_t> label;
3945 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label); 3946 blink::WebCryptoAlgorithm wrapping_algorithm = CreateRsaOaepAlgorithm(label);
3946 3947
3947 const std::string key_hex = "000102030405060708090a0b0c0d0e0f"; 3948 const std::string key_hex = "000102030405060708090a0b0c0d0e0f";
3948 const blink::WebCryptoAlgorithm key_algorithm = 3949 const blink::WebCryptoAlgorithm key_algorithm =
3949 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 3950 webcrypto::CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
3950 3951
3951 blink::WebCryptoKey key = 3952 blink::WebCryptoKey key =
3952 ImportSecretKeyFromRaw(HexStringToBytes(key_hex), 3953 ImportSecretKeyFromRaw(HexStringToBytes(key_hex),
3953 key_algorithm, 3954 key_algorithm,
3954 blink::WebCryptoKeyUsageEncrypt); 3955 blink::WebCryptoKeyUsageEncrypt);
3955 ASSERT_FALSE(key.isNull()); 3956 ASSERT_FALSE(key.isNull());
3956 3957
3957 std::vector<uint8> wrapped_key; 3958 std::vector<uint8_t> wrapped_key;
3958 ASSERT_EQ(Status::Success(), 3959 ASSERT_EQ(Status::Success(),
3959 WrapKey(blink::WebCryptoKeyFormatJwk, 3960 WrapKey(blink::WebCryptoKeyFormatJwk,
3960 key, 3961 key,
3961 public_key, 3962 public_key,
3962 wrapping_algorithm, 3963 wrapping_algorithm,
3963 &wrapped_key)); 3964 &wrapped_key));
3964 3965
3965 // Verify that |wrapped_key| can be decrypted and yields a valid JWK object. 3966 // Verify that |wrapped_key| can be decrypted and yields a valid JWK object.
3966 // Because |private_key| supports both decrypt and unwrap, this is valid. 3967 // Because |private_key| supports both decrypt and unwrap, this is valid.
3967 std::vector<uint8> decrypted_jwk; 3968 std::vector<uint8_t> decrypted_jwk;
3968 ASSERT_EQ(Status::Success(), 3969 ASSERT_EQ(Status::Success(),
3969 Decrypt(wrapping_algorithm, 3970 Decrypt(wrapping_algorithm,
3970 private_key, 3971 private_key,
3971 CryptoData(wrapped_key), 3972 CryptoData(wrapped_key),
3972 &decrypted_jwk)); 3973 &decrypted_jwk));
3973 EXPECT_TRUE(VerifySecretJwk( 3974 EXPECT_TRUE(VerifySecretJwk(
3974 decrypted_jwk, "A128CBC", key_hex, blink::WebCryptoKeyUsageEncrypt)); 3975 decrypted_jwk, "A128CBC", key_hex, blink::WebCryptoKeyUsageEncrypt));
3975 3976
3976 // Now attempt to unwrap the key, which should also decrypt the data. 3977 // Now attempt to unwrap the key, which should also decrypt the data.
3977 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull(); 3978 blink::WebCryptoKey unwrapped_key = blink::WebCryptoKey::createNull();
3978 ASSERT_EQ(Status::Success(), 3979 ASSERT_EQ(Status::Success(),
3979 UnwrapKey(blink::WebCryptoKeyFormatJwk, 3980 UnwrapKey(blink::WebCryptoKeyFormatJwk,
3980 CryptoData(wrapped_key), 3981 CryptoData(wrapped_key),
3981 private_key, 3982 private_key,
3982 wrapping_algorithm, 3983 wrapping_algorithm,
3983 key_algorithm, 3984 key_algorithm,
3984 true, 3985 true,
3985 blink::WebCryptoKeyUsageEncrypt, 3986 blink::WebCryptoKeyUsageEncrypt,
3986 &unwrapped_key)); 3987 &unwrapped_key));
3987 ASSERT_FALSE(unwrapped_key.isNull()); 3988 ASSERT_FALSE(unwrapped_key.isNull());
3988 3989
3989 std::vector<uint8> raw_key; 3990 std::vector<uint8_t> raw_key;
3990 ASSERT_EQ(Status::Success(), 3991 ASSERT_EQ(Status::Success(),
3991 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key)); 3992 ExportKey(blink::WebCryptoKeyFormatRaw, unwrapped_key, &raw_key));
3992 EXPECT_BYTES_EQ_HEX(key_hex, raw_key); 3993 EXPECT_BYTES_EQ_HEX(key_hex, raw_key);
3993 } 3994 }
3994 3995
3995 // Try importing an RSA-SSA public key with unsupported key usages using SPKI 3996 // Try importing an RSA-SSA public key with unsupported key usages using SPKI
3996 // format. RSA-SSA public keys only support the 'verify' usage. 3997 // format. RSA-SSA public keys only support the 'verify' usage.
3997 TEST_F(SharedCryptoTest, MAYBE(ImportRsaSsaPublicKeyBadUsage_SPKI)) { 3998 TEST_F(SharedCryptoTest, MAYBE(ImportRsaSsaPublicKeyBadUsage_SPKI)) {
3998 const blink::WebCryptoAlgorithm algorithm = 3999 const blink::WebCryptoAlgorithm algorithm =
3999 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 4000 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
4056 const blink::WebCryptoAlgorithm algorithm = 4057 const blink::WebCryptoAlgorithm algorithm =
4057 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc); 4058 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCbc);
4058 4059
4059 blink::WebCryptoKeyUsageMask bad_usages[] = { 4060 blink::WebCryptoKeyUsageMask bad_usages[] = {
4060 blink::WebCryptoKeyUsageSign, 4061 blink::WebCryptoKeyUsageSign,
4061 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageDecrypt, 4062 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageDecrypt,
4062 blink::WebCryptoKeyUsageDeriveBits, 4063 blink::WebCryptoKeyUsageDeriveBits,
4063 blink::WebCryptoKeyUsageUnwrapKey | blink::WebCryptoKeyUsageVerify, 4064 blink::WebCryptoKeyUsageUnwrapKey | blink::WebCryptoKeyUsageVerify,
4064 }; 4065 };
4065 4066
4066 std::vector<uint8> key_bytes(16); 4067 std::vector<uint8_t> key_bytes(16);
4067 4068
4068 for (size_t i = 0; i < arraysize(bad_usages); ++i) { 4069 for (size_t i = 0; i < arraysize(bad_usages); ++i) {
4069 SCOPED_TRACE(i); 4070 SCOPED_TRACE(i);
4070 4071
4071 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 4072 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
4072 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), 4073 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(),
4073 ImportKey(blink::WebCryptoKeyFormatRaw, 4074 ImportKey(blink::WebCryptoKeyFormatRaw,
4074 CryptoData(key_bytes), 4075 CryptoData(key_bytes),
4075 algorithm, 4076 algorithm,
4076 true, 4077 true,
(...skipping 11 matching lines...) Expand all
4088 4089
4089 blink::WebCryptoKeyUsageMask bad_usages[] = { 4090 blink::WebCryptoKeyUsageMask bad_usages[] = {
4090 blink::WebCryptoKeyUsageEncrypt, 4091 blink::WebCryptoKeyUsageEncrypt,
4091 blink::WebCryptoKeyUsageDecrypt, 4092 blink::WebCryptoKeyUsageDecrypt,
4092 blink::WebCryptoKeyUsageSign, 4093 blink::WebCryptoKeyUsageSign,
4093 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageUnwrapKey, 4094 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageUnwrapKey,
4094 blink::WebCryptoKeyUsageDeriveBits, 4095 blink::WebCryptoKeyUsageDeriveBits,
4095 blink::WebCryptoKeyUsageUnwrapKey | blink::WebCryptoKeyUsageVerify, 4096 blink::WebCryptoKeyUsageUnwrapKey | blink::WebCryptoKeyUsageVerify,
4096 }; 4097 };
4097 4098
4098 std::vector<uint8> key_bytes(16); 4099 std::vector<uint8_t> key_bytes(16);
4099 4100
4100 for (size_t i = 0; i < arraysize(bad_usages); ++i) { 4101 for (size_t i = 0; i < arraysize(bad_usages); ++i) {
4101 SCOPED_TRACE(i); 4102 SCOPED_TRACE(i);
4102 4103
4103 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); 4104 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
4104 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), 4105 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(),
4105 ImportKey(blink::WebCryptoKeyFormatRaw, 4106 ImportKey(blink::WebCryptoKeyFormatRaw,
4106 CryptoData(key_bytes), 4107 CryptoData(key_bytes),
4107 algorithm, 4108 algorithm,
4108 true, 4109 true,
(...skipping 14 matching lines...) Expand all
4123 blink::WebCryptoKeyUsageDecrypt, 4124 blink::WebCryptoKeyUsageDecrypt,
4124 blink::WebCryptoKeyUsageWrapKey, 4125 blink::WebCryptoKeyUsageWrapKey,
4125 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageWrapKey, 4126 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageWrapKey,
4126 blink::WebCryptoKeyUsageVerify | blink::WebCryptoKeyUsageDeriveKey, 4127 blink::WebCryptoKeyUsageVerify | blink::WebCryptoKeyUsageDeriveKey,
4127 }; 4128 };
4128 4129
4129 // Import the wrapping key. 4130 // Import the wrapping key.
4130 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull(); 4131 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull();
4131 ASSERT_EQ(Status::Success(), 4132 ASSERT_EQ(Status::Success(),
4132 ImportKey(blink::WebCryptoKeyFormatRaw, 4133 ImportKey(blink::WebCryptoKeyFormatRaw,
4133 CryptoData(std::vector<uint8>(16)), 4134 CryptoData(std::vector<uint8_t>(16)),
4134 unwrap_algorithm, 4135 unwrap_algorithm,
4135 true, 4136 true,
4136 blink::WebCryptoKeyUsageUnwrapKey, 4137 blink::WebCryptoKeyUsageUnwrapKey,
4137 &wrapping_key)); 4138 &wrapping_key));
4138 4139
4139 // The JWK plain text is: 4140 // The JWK plain text is:
4140 // { "kty": "oct","alg": "HS256","k": "GADWrMRHwQfoNaXU5fZvTg=="} 4141 // { "kty": "oct","alg": "HS256","k": "GADWrMRHwQfoNaXU5fZvTg=="}
4141 const char* kWrappedJwk = 4142 const char* kWrappedJwk =
4142 "0AA245F17064FFB2A7A094436A39BEBFC962C627303D1327EA750CE9F917688C2782A943" 4143 "0AA245F17064FFB2A7A094436A39BEBFC962C627303D1327EA750CE9F917688C2782A943"
4143 "7AE7586547AC490E8AE7D5B02D63868D5C3BB57D36C4C8C5BF3962ACEC6F42E767E5706" 4144 "7AE7586547AC490E8AE7D5B02D63868D5C3BB57D36C4C8C5BF3962ACEC6F42E767E5706"
(...skipping 29 matching lines...) Expand all
4173 blink::WebCryptoKeyUsageSign, 4174 blink::WebCryptoKeyUsageSign,
4174 blink::WebCryptoKeyUsageDecrypt, 4175 blink::WebCryptoKeyUsageDecrypt,
4175 blink::WebCryptoKeyUsageWrapKey, 4176 blink::WebCryptoKeyUsageWrapKey,
4176 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageWrapKey, 4177 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageWrapKey,
4177 }; 4178 };
4178 4179
4179 // Import the wrapping key. 4180 // Import the wrapping key.
4180 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull(); 4181 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull();
4181 ASSERT_EQ(Status::Success(), 4182 ASSERT_EQ(Status::Success(),
4182 ImportKey(blink::WebCryptoKeyFormatRaw, 4183 ImportKey(blink::WebCryptoKeyFormatRaw,
4183 CryptoData(std::vector<uint8>(16)), 4184 CryptoData(std::vector<uint8_t>(16)),
4184 unwrap_algorithm, 4185 unwrap_algorithm,
4185 true, 4186 true,
4186 blink::WebCryptoKeyUsageUnwrapKey, 4187 blink::WebCryptoKeyUsageUnwrapKey,
4187 &wrapping_key)); 4188 &wrapping_key));
4188 4189
4189 // The JWK plaintext is: 4190 // The JWK plaintext is:
4190 // { "kty": "RSA","alg": "RS256","n": "...","e": "AQAB"} 4191 // { "kty": "RSA","alg": "RS256","n": "...","e": "AQAB"}
4191 4192
4192 const char* kWrappedJwk = 4193 const char* kWrappedJwk =
4193 "CE8DAEF99E977EE58958B8C4494755C846E883B2ECA575C5366622839AF71AB30875F152" 4194 "CE8DAEF99E977EE58958B8C4494755C846E883B2ECA575C5366622839AF71AB30875F152"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
4239 // Generate an RSA-SSA key pair with invalid usages. RSA-SSA supports: 4240 // Generate an RSA-SSA key pair with invalid usages. RSA-SSA supports:
4240 // 'sign', 'verify' 4241 // 'sign', 'verify'
4241 TEST_F(SharedCryptoTest, MAYBE(GenerateRsaSsaBadUsages)) { 4242 TEST_F(SharedCryptoTest, MAYBE(GenerateRsaSsaBadUsages)) {
4242 blink::WebCryptoKeyUsageMask bad_usages[] = { 4243 blink::WebCryptoKeyUsageMask bad_usages[] = {
4243 blink::WebCryptoKeyUsageDecrypt, 4244 blink::WebCryptoKeyUsageDecrypt,
4244 blink::WebCryptoKeyUsageVerify | blink::WebCryptoKeyUsageDecrypt, 4245 blink::WebCryptoKeyUsageVerify | blink::WebCryptoKeyUsageDecrypt,
4245 blink::WebCryptoKeyUsageWrapKey, 4246 blink::WebCryptoKeyUsageWrapKey,
4246 }; 4247 };
4247 4248
4248 const unsigned int modulus_length = 256; 4249 const unsigned int modulus_length = 256;
4249 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 4250 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
4250 4251
4251 for (size_t i = 0; i < arraysize(bad_usages); ++i) { 4252 for (size_t i = 0; i < arraysize(bad_usages); ++i) {
4252 SCOPED_TRACE(i); 4253 SCOPED_TRACE(i);
4253 4254
4254 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 4255 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
4255 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 4256 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
4256 4257
4257 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(), 4258 ASSERT_EQ(Status::ErrorCreateKeyBadUsages(),
4258 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( 4259 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm(
4259 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 4260 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
4260 blink::WebCryptoAlgorithmIdSha256, 4261 blink::WebCryptoAlgorithmIdSha256,
4261 modulus_length, 4262 modulus_length,
4262 public_exponent), 4263 public_exponent),
4263 true, 4264 true,
4264 bad_usages[i], 4265 bad_usages[i],
4265 &public_key, 4266 &public_key,
4266 &private_key)); 4267 &private_key));
4267 } 4268 }
4268 } 4269 }
4269 4270
4270 // Generate an RSA-SSA key pair. The public and private keys should select the 4271 // Generate an RSA-SSA key pair. The public and private keys should select the
4271 // key usages which are applicable, and not have the exact same usages as was 4272 // key usages which are applicable, and not have the exact same usages as was
4272 // specified to GenerateKey 4273 // specified to GenerateKey
4273 TEST_F(SharedCryptoTest, MAYBE(GenerateRsaSsaKeyPairIntersectUsages)) { 4274 TEST_F(SharedCryptoTest, MAYBE(GenerateRsaSsaKeyPairIntersectUsages)) {
4274 const unsigned int modulus_length = 256; 4275 const unsigned int modulus_length = 256;
4275 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 4276 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
4276 4277
4277 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 4278 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
4278 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 4279 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
4279 4280
4280 ASSERT_EQ(Status::Success(), 4281 ASSERT_EQ(Status::Success(),
4281 GenerateKeyPair( 4282 GenerateKeyPair(
4282 CreateRsaHashedKeyGenAlgorithm( 4283 CreateRsaHashedKeyGenAlgorithm(
4283 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 4284 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
4284 blink::WebCryptoAlgorithmIdSha256, 4285 blink::WebCryptoAlgorithmIdSha256,
4285 modulus_length, 4286 modulus_length,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
4319 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull(); 4320 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull();
4320 ASSERT_EQ(Status::Success(), 4321 ASSERT_EQ(Status::Success(),
4321 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), 4322 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128),
4322 true, 4323 true,
4323 blink::WebCryptoKeyUsageWrapKey | 4324 blink::WebCryptoKeyUsageWrapKey |
4324 blink::WebCryptoKeyUsageUnwrapKey, 4325 blink::WebCryptoKeyUsageUnwrapKey,
4325 &wrapping_key)); 4326 &wrapping_key));
4326 4327
4327 // Generate an RSA key pair to be wrapped. 4328 // Generate an RSA key pair to be wrapped.
4328 const unsigned int modulus_length = 256; 4329 const unsigned int modulus_length = 256;
4329 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); 4330 const std::vector<uint8_t> public_exponent = HexStringToBytes("010001");
4330 4331
4331 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); 4332 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
4332 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); 4333 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
4333 ASSERT_EQ(Status::Success(), 4334 ASSERT_EQ(Status::Success(),
4334 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( 4335 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm(
4335 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 4336 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
4336 blink::WebCryptoAlgorithmIdSha256, 4337 blink::WebCryptoAlgorithmIdSha256,
4337 modulus_length, 4338 modulus_length,
4338 public_exponent), 4339 public_exponent),
4339 true, 4340 true,
4340 0, 4341 0,
4341 &public_key, 4342 &public_key,
4342 &private_key)); 4343 &private_key));
4343 4344
4344 // Export key pair as SPKI + PKCS8 4345 // Export key pair as SPKI + PKCS8
4345 std::vector<uint8> public_key_spki; 4346 std::vector<uint8_t> public_key_spki;
4346 ASSERT_EQ( 4347 ASSERT_EQ(
4347 Status::Success(), 4348 Status::Success(),
4348 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); 4349 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki));
4349 4350
4350 std::vector<uint8> private_key_pkcs8; 4351 std::vector<uint8_t> private_key_pkcs8;
4351 ASSERT_EQ( 4352 ASSERT_EQ(
4352 Status::Success(), 4353 Status::Success(),
4353 ExportKey( 4354 ExportKey(
4354 blink::WebCryptoKeyFormatPkcs8, private_key, &private_key_pkcs8)); 4355 blink::WebCryptoKeyFormatPkcs8, private_key, &private_key_pkcs8));
4355 4356
4356 // Wrap the key pair. 4357 // Wrap the key pair.
4357 blink::WebCryptoAlgorithm wrap_algorithm = 4358 blink::WebCryptoAlgorithm wrap_algorithm =
4358 CreateAesCbcAlgorithm(std::vector<uint8>(16, 0)); 4359 CreateAesCbcAlgorithm(std::vector<uint8_t>(16, 0));
4359 4360
4360 std::vector<uint8> wrapped_public_key; 4361 std::vector<uint8_t> wrapped_public_key;
4361 ASSERT_EQ(Status::Success(), 4362 ASSERT_EQ(Status::Success(),
4362 WrapKey(blink::WebCryptoKeyFormatSpki, 4363 WrapKey(blink::WebCryptoKeyFormatSpki,
4363 public_key, 4364 public_key,
4364 wrapping_key, 4365 wrapping_key,
4365 wrap_algorithm, 4366 wrap_algorithm,
4366 &wrapped_public_key)); 4367 &wrapped_public_key));
4367 4368
4368 std::vector<uint8> wrapped_private_key; 4369 std::vector<uint8_t> wrapped_private_key;
4369 ASSERT_EQ(Status::Success(), 4370 ASSERT_EQ(Status::Success(),
4370 WrapKey(blink::WebCryptoKeyFormatPkcs8, 4371 WrapKey(blink::WebCryptoKeyFormatPkcs8,
4371 private_key, 4372 private_key,
4372 wrapping_key, 4373 wrapping_key,
4373 wrap_algorithm, 4374 wrap_algorithm,
4374 &wrapped_private_key)); 4375 &wrapped_private_key));
4375 4376
4376 // Unwrap the key pair. 4377 // Unwrap the key pair.
4377 blink::WebCryptoAlgorithm rsa_import_algorithm = 4378 blink::WebCryptoAlgorithm rsa_import_algorithm =
4378 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, 4379 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
(...skipping 17 matching lines...) Expand all
4396 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, 4397 UnwrapKey(blink::WebCryptoKeyFormatPkcs8,
4397 CryptoData(wrapped_private_key), 4398 CryptoData(wrapped_private_key),
4398 wrapping_key, 4399 wrapping_key,
4399 wrap_algorithm, 4400 wrap_algorithm,
4400 rsa_import_algorithm, 4401 rsa_import_algorithm,
4401 true, 4402 true,
4402 0, 4403 0,
4403 &unwrapped_private_key)); 4404 &unwrapped_private_key));
4404 4405
4405 // Export unwrapped key pair as SPKI + PKCS8 4406 // Export unwrapped key pair as SPKI + PKCS8
4406 std::vector<uint8> unwrapped_public_key_spki; 4407 std::vector<uint8_t> unwrapped_public_key_spki;
4407 ASSERT_EQ(Status::Success(), 4408 ASSERT_EQ(Status::Success(),
4408 ExportKey(blink::WebCryptoKeyFormatSpki, 4409 ExportKey(blink::WebCryptoKeyFormatSpki,
4409 unwrapped_public_key, 4410 unwrapped_public_key,
4410 &unwrapped_public_key_spki)); 4411 &unwrapped_public_key_spki));
4411 4412
4412 std::vector<uint8> unwrapped_private_key_pkcs8; 4413 std::vector<uint8_t> unwrapped_private_key_pkcs8;
4413 ASSERT_EQ(Status::Success(), 4414 ASSERT_EQ(Status::Success(),
4414 ExportKey(blink::WebCryptoKeyFormatPkcs8, 4415 ExportKey(blink::WebCryptoKeyFormatPkcs8,
4415 unwrapped_private_key, 4416 unwrapped_private_key,
4416 &unwrapped_private_key_pkcs8)); 4417 &unwrapped_private_key_pkcs8));
4417 4418
4418 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); 4419 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki);
4419 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); 4420 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8);
4420 4421
4421 EXPECT_NE(public_key_spki, wrapped_public_key); 4422 EXPECT_NE(public_key_spki, wrapped_public_key);
4422 EXPECT_NE(private_key_pkcs8, wrapped_private_key); 4423 EXPECT_NE(private_key_pkcs8, wrapped_private_key);
4423 } 4424 }
4424 4425
4425 } // namespace webcrypto 4426 } // namespace webcrypto
4426 4427
4427 } // namespace content 4428 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/platform_crypto.h ('k') | content/child/webcrypto/structured_clone.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698