OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "webcrypto_impl.h" | 5 #include "webcrypto_impl.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 131 |
132 bool EncryptInternal( | 132 bool EncryptInternal( |
133 const WebKit::WebCryptoAlgorithm& algorithm, | 133 const WebKit::WebCryptoAlgorithm& algorithm, |
134 const WebKit::WebCryptoKey& key, | 134 const WebKit::WebCryptoKey& key, |
135 const unsigned char* data, | 135 const unsigned char* data, |
136 unsigned data_size, | 136 unsigned data_size, |
137 WebKit::WebArrayBuffer* buffer) { | 137 WebKit::WebArrayBuffer* buffer) { |
138 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer); | 138 return crypto_.EncryptInternal(algorithm, key, data, data_size, buffer); |
139 } | 139 } |
140 | 140 |
141 bool DecryptInternal( | |
142 const WebKit::WebCryptoAlgorithm& algorithm, | |
143 const WebKit::WebCryptoKey& key, | |
144 const unsigned char* data, | |
145 unsigned data_size, | |
146 WebKit::WebArrayBuffer* buffer) { | |
147 return crypto_.DecryptInternal(algorithm, key, data, data_size, buffer); | |
148 } | |
149 | |
150 private: | 141 private: |
151 WebCryptoImpl crypto_; | 142 WebCryptoImpl crypto_; |
152 }; | 143 }; |
153 | 144 |
154 TEST_F(WebCryptoImplTest, DigestSampleSets) { | 145 TEST_F(WebCryptoImplTest, DigestSampleSets) { |
155 // The results are stored here in hex format for readability. | 146 // The results are stored here in hex format for readability. |
156 // | 147 // |
157 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced | 148 // TODO(bryaneyler): Eventually, all these sample test sets should be replaced |
158 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 | 149 // with the sets here: http://csrc.nist.gov/groups/STM/cavp/index.html#03 |
159 // | 150 // |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 key, | 359 key, |
369 kLongSignature, | 360 kLongSignature, |
370 sizeof(kLongSignature), | 361 sizeof(kLongSignature), |
371 &message_raw[0], | 362 &message_raw[0], |
372 message_raw.size(), | 363 message_raw.size(), |
373 &signature_match)); | 364 &signature_match)); |
374 EXPECT_FALSE(signature_match); | 365 EXPECT_FALSE(signature_match); |
375 } | 366 } |
376 } | 367 } |
377 | 368 |
378 TEST_F(WebCryptoImplTest, AesCbcFailures) { | 369 TEST_F(WebCryptoImplTest, AesCbcEncryptionFailures) { |
379 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 370 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
380 "2b7e151628aed2a6abf7158809cf4f3c", | 371 "2b7e151628aed2a6abf7158809cf4f3c", |
381 CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc), | 372 CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc), |
382 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt); | 373 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt); |
383 | 374 |
384 WebKit::WebArrayBuffer output; | 375 WebKit::WebArrayBuffer output; |
385 | 376 |
386 // Use an invalid |iv| (fewer than 16 bytes) | 377 // Use an invalid |iv| (fewer than 16 bytes) |
387 { | 378 { |
388 std::vector<uint8> input(32); | 379 std::vector<uint8> plain_text(33); |
389 std::vector<uint8> iv; | 380 std::vector<uint8> iv; |
390 EXPECT_FALSE(EncryptInternal( | 381 EXPECT_FALSE(EncryptInternal(CreateAesCbcAlgorithm(iv), |
391 CreateAesCbcAlgorithm(iv), key, &input[0], input.size(), &output)); | 382 key, |
392 EXPECT_FALSE(DecryptInternal( | 383 &plain_text[0], |
393 CreateAesCbcAlgorithm(iv), key, &input[0], input.size(), &output)); | 384 plain_text.size(), |
| 385 &output)); |
394 } | 386 } |
395 | 387 |
396 // Use an invalid |iv| (more than 16 bytes) | 388 // Use an invalid |iv| (more than 16 bytes) |
397 { | 389 { |
398 std::vector<uint8> input(32); | 390 std::vector<uint8> plain_text(33); |
399 std::vector<uint8> iv(17); | 391 std::vector<uint8> iv(17); |
400 EXPECT_FALSE(EncryptInternal( | 392 EXPECT_FALSE(EncryptInternal(CreateAesCbcAlgorithm(iv), |
401 CreateAesCbcAlgorithm(iv), key, &input[0], input.size(), &output)); | 393 key, |
402 EXPECT_FALSE(DecryptInternal( | 394 &plain_text[0], |
403 CreateAesCbcAlgorithm(iv), key, &input[0], input.size(), &output)); | 395 plain_text.size(), |
| 396 &output)); |
404 } | 397 } |
405 | 398 |
406 // Give an input that is too large (would cause integer overflow when | 399 // Give an input that is too large (would cause integer overflow when |
407 // narrowing to an int). | 400 // narrowing to an int). |
408 { | 401 { |
409 std::vector<uint8> iv(16); | 402 std::vector<uint8> iv(16); |
410 | 403 |
411 // Pretend the input is large. Don't pass data pointer as NULL in case that | 404 // Pretend the input is large. Don't pass data pointer as NULL in case that |
412 // is special cased; the implementation shouldn't actually dereference the | 405 // is special cased; the implementation shouldn't actually dereference the |
413 // data. | 406 // data. |
414 const unsigned char* input = &iv[0]; | 407 const unsigned char* plain_text = &iv[0]; |
415 unsigned input_len = INT_MAX - 3; | 408 unsigned plain_text_len = INT_MAX - 3; |
416 | 409 |
417 EXPECT_FALSE(EncryptInternal( | 410 EXPECT_FALSE(EncryptInternal( |
418 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); | 411 CreateAesCbcAlgorithm(iv), key, plain_text, plain_text_len, &output)); |
419 EXPECT_FALSE(DecryptInternal( | |
420 CreateAesCbcAlgorithm(iv), key, input, input_len, &output)); | |
421 } | 412 } |
422 | 413 |
423 // Fail importing the key (too few bytes specified) | 414 // Fail importing the key (too few bytes specified) |
424 { | 415 { |
425 WebKit::WebCryptoKeyType type; | 416 WebKit::WebCryptoKeyType type; |
426 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | 417 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; |
427 | 418 |
428 std::vector<uint8> key_raw(1); | 419 std::vector<uint8> key_raw(1); |
429 std::vector<uint8> iv(16); | 420 std::vector<uint8> iv(16); |
430 | 421 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( | 514 WebKit::WebCryptoKey key = ImportSecretKeyFromRawHexString( |
524 test.key, | 515 test.key, |
525 CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc), | 516 CreateAlgorithm(WebKit::WebCryptoAlgorithmIdAesCbc), |
526 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt); | 517 WebKit::WebCryptoKeyUsageEncrypt | WebKit::WebCryptoKeyUsageDecrypt); |
527 | 518 |
528 std::vector<uint8> plain_text = HexStringToBytes(test.plain_text); | 519 std::vector<uint8> plain_text = HexStringToBytes(test.plain_text); |
529 std::vector<uint8> iv = HexStringToBytes(test.iv); | 520 std::vector<uint8> iv = HexStringToBytes(test.iv); |
530 | 521 |
531 WebKit::WebArrayBuffer output; | 522 WebKit::WebArrayBuffer output; |
532 | 523 |
533 // Test encryption. | |
534 EXPECT_TRUE(EncryptInternal(CreateAesCbcAlgorithm(iv), | 524 EXPECT_TRUE(EncryptInternal(CreateAesCbcAlgorithm(iv), |
535 key, | 525 key, |
536 &plain_text[0], | 526 &plain_text[0], |
537 plain_text.size(), | 527 plain_text.size(), |
538 &output)); | 528 &output)); |
| 529 |
539 ExpectArrayBufferMatchesHex(test.cipher_text, output); | 530 ExpectArrayBufferMatchesHex(test.cipher_text, output); |
540 | |
541 // Test decryption. | |
542 std::vector<uint8> cipher_text = HexStringToBytes(test.cipher_text); | |
543 EXPECT_TRUE(DecryptInternal(CreateAesCbcAlgorithm(iv), | |
544 key, | |
545 &cipher_text[0], | |
546 cipher_text.size(), | |
547 &output)); | |
548 ExpectArrayBufferMatchesHex(test.plain_text, output); | |
549 | |
550 const unsigned kAesCbcBlockSize = 16; | |
551 | |
552 // Decrypt with a padding error by stripping the last block. This also ends | |
553 // up testing decryption over empty cipher text. | |
554 if (cipher_text.size() >= kAesCbcBlockSize) { | |
555 EXPECT_FALSE(DecryptInternal(CreateAesCbcAlgorithm(iv), | |
556 key, | |
557 &cipher_text[0], | |
558 cipher_text.size() - kAesCbcBlockSize, | |
559 &output)); | |
560 } | |
561 | |
562 // Decrypt cipher text which is not block-aligned, by stripping a few bytes | |
563 // off the cipher text. | |
564 if (cipher_text.size() > 3) { | |
565 EXPECT_FALSE(DecryptInternal(CreateAesCbcAlgorithm(iv), | |
566 key, | |
567 &cipher_text[0], | |
568 cipher_text.size() - 3, | |
569 &output)); | |
570 } | |
571 } | 531 } |
572 } | 532 } |
573 | 533 |
574 } // namespace content | 534 } // namespace content |
OLD | NEW |