|
5 | 5 | from parameterized import parameterized |
6 | 6 |
|
7 | 7 | import pkcs11 |
8 | | -from pkcs11 import Mechanism |
| 8 | +from pkcs11 import ArgumentsBad, CTRParams, GCMParams, Mechanism, PKCS11Error |
9 | 9 |
|
10 | 10 | from . import FIXME, TestCase, requires |
11 | 11 |
|
@@ -462,3 +462,152 @@ def test_encrypt_with_key_derived_using_cbc_encrypt( |
462 | 462 | text = self.key.decrypt(crypttext, mechanism_param=iv) |
463 | 463 |
|
464 | 464 | self.assertEqual(text, data) |
| 465 | + |
| 466 | + @requires(Mechanism.AES_GCM) |
| 467 | + def test_encrypt_gcm(self): |
| 468 | + data = b"INPUT DATA" |
| 469 | + nonce = b"0" * 12 |
| 470 | + |
| 471 | + crypttext = self.key.encrypt( |
| 472 | + data, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce) |
| 473 | + ) |
| 474 | + self.assertIsInstance(crypttext, bytes) |
| 475 | + self.assertNotEqual(data, crypttext) |
| 476 | + text = self.key.decrypt( |
| 477 | + crypttext, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce) |
| 478 | + ) |
| 479 | + self.assertEqual(data, text) |
| 480 | + |
| 481 | + def test_gcm_nonce_size_limit(self): |
| 482 | + def _inst(): |
| 483 | + return GCMParams(nonce=b"0" * 13) |
| 484 | + |
| 485 | + self.assertRaises(ArgumentsBad, _inst) |
| 486 | + |
| 487 | + @requires(Mechanism.AES_GCM) |
| 488 | + def test_encrypt_gcm_with_aad(self): |
| 489 | + data = b"INPUT DATA" |
| 490 | + nonce = b"0" * 12 |
| 491 | + |
| 492 | + crypttext = self.key.encrypt( |
| 493 | + data, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce, b"foo") |
| 494 | + ) |
| 495 | + self.assertIsInstance(crypttext, bytes) |
| 496 | + self.assertNotEqual(data, crypttext) |
| 497 | + text = self.key.decrypt( |
| 498 | + crypttext, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce, b"foo") |
| 499 | + ) |
| 500 | + self.assertEqual(data, text) |
| 501 | + |
| 502 | + @requires(Mechanism.AES_GCM) |
| 503 | + def test_encrypt_gcm_with_mismatching_nonces(self): |
| 504 | + data = b"INPUT DATA" |
| 505 | + nonce1 = b"0" * 12 |
| 506 | + nonce2 = b"1" * 12 |
| 507 | + |
| 508 | + crypttext = self.key.encrypt( |
| 509 | + data, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce1, b"foo") |
| 510 | + ) |
| 511 | + self.assertIsInstance(crypttext, bytes) |
| 512 | + self.assertNotEqual(data, crypttext) |
| 513 | + # This should be EncryptedDataInvalid, but in practice not all tokens support this |
| 514 | + with self.assertRaises(PKCS11Error): |
| 515 | + self.key.decrypt( |
| 516 | + crypttext, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce2, b"foo") |
| 517 | + ) |
| 518 | + |
| 519 | + @requires(Mechanism.AES_GCM) |
| 520 | + def test_encrypt_gcm_with_mismatching_aad(self): |
| 521 | + data = b"INPUT DATA" |
| 522 | + nonce = b"0" * 12 |
| 523 | + |
| 524 | + crypttext = self.key.encrypt( |
| 525 | + data, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce, b"foo") |
| 526 | + ) |
| 527 | + self.assertIsInstance(crypttext, bytes) |
| 528 | + self.assertNotEqual(data, crypttext) |
| 529 | + with self.assertRaises(PKCS11Error): |
| 530 | + self.key.decrypt( |
| 531 | + crypttext, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce, b"bar") |
| 532 | + ) |
| 533 | + |
| 534 | + @requires(Mechanism.AES_GCM) |
| 535 | + def test_encrypt_gcm_with_custom_tag_length(self): |
| 536 | + data = b"INPUT DATA" |
| 537 | + nonce = b"0" * 12 |
| 538 | + |
| 539 | + crypttext = self.key.encrypt( |
| 540 | + data, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce, b"foo", 120) |
| 541 | + ) |
| 542 | + self.assertIsInstance(crypttext, bytes) |
| 543 | + self.assertNotEqual(data, crypttext) |
| 544 | + text = self.key.decrypt( |
| 545 | + crypttext, mechanism=Mechanism.AES_GCM, mechanism_param=GCMParams(nonce, b"foo", 120) |
| 546 | + ) |
| 547 | + self.assertEqual(data, text) |
| 548 | + |
| 549 | + # This should be EncryptedDataInvalid, but in practice not all tokens support this |
| 550 | + with self.assertRaises(PKCS11Error): |
| 551 | + text = self.key.decrypt( |
| 552 | + crypttext, |
| 553 | + mechanism=Mechanism.AES_GCM, |
| 554 | + mechanism_param=GCMParams(nonce, b"foo", 128), |
| 555 | + ) |
| 556 | + |
| 557 | + @parameterized.expand( |
| 558 | + [ |
| 559 | + (b""), |
| 560 | + (b"0" * 12), |
| 561 | + (b"0" * 15), |
| 562 | + ] |
| 563 | + ) |
| 564 | + @requires(Mechanism.AES_CTR) |
| 565 | + @FIXME.opencryptoki # opencryptoki incorrectly forces AES-CTR input to be padded |
| 566 | + def test_encrypt_ctr(self, nonce): |
| 567 | + data = b"INPUT DATA SEVERAL BLOCKS LONG SO THE COUNTER GOES UP A FEW TIMES" * 20 |
| 568 | + |
| 569 | + crypttext = self.key.encrypt( |
| 570 | + data, mechanism=Mechanism.AES_CTR, mechanism_param=CTRParams(nonce) |
| 571 | + ) |
| 572 | + self.assertIsInstance(crypttext, bytes) |
| 573 | + self.assertNotEqual(data, crypttext) |
| 574 | + text = self.key.decrypt( |
| 575 | + crypttext, mechanism=Mechanism.AES_CTR, mechanism_param=CTRParams(nonce) |
| 576 | + ) |
| 577 | + self.assertEqual(data, text) |
| 578 | + |
| 579 | + @requires(Mechanism.AES_CTR) |
| 580 | + def test_encrypt_ctr_exactly_padded(self): |
| 581 | + # let's still verify the "restricted" AES-CTR supported by opencryptoki |
| 582 | + data = b"PADDED INPUT DATA TO MAKE OPENCRYPTOKI HAPPY" * 16 |
| 583 | + nonce = b"0" * 15 |
| 584 | + |
| 585 | + crypttext = self.key.encrypt( |
| 586 | + data, mechanism=Mechanism.AES_CTR, mechanism_param=CTRParams(nonce) |
| 587 | + ) |
| 588 | + self.assertIsInstance(crypttext, bytes) |
| 589 | + self.assertNotEqual(data, crypttext) |
| 590 | + text = self.key.decrypt( |
| 591 | + crypttext, mechanism=Mechanism.AES_CTR, mechanism_param=CTRParams(nonce) |
| 592 | + ) |
| 593 | + self.assertEqual(data, text) |
| 594 | + |
| 595 | + def test_ctr_nonce_size_limit(self): |
| 596 | + def _inst(): |
| 597 | + return CTRParams(nonce=b"0" * 16) |
| 598 | + |
| 599 | + self.assertRaises(ArgumentsBad, _inst) |
| 600 | + |
| 601 | + @requires(Mechanism.AES_CTR) |
| 602 | + @FIXME.opencryptoki # opencryptoki incorrectly forces AES-CTR input to be padded |
| 603 | + def test_encrypt_ctr_nonce_mismatch(self): |
| 604 | + data = b"INPUT DATA SEVERAL BLOCKS LONG SO THE COUNTER GOES UP A FEW TIMES" * 20 |
| 605 | + crypttext = self.key.encrypt( |
| 606 | + data, mechanism=Mechanism.AES_CTR, mechanism_param=CTRParams(b"0" * 12) |
| 607 | + ) |
| 608 | + self.assertIsInstance(crypttext, bytes) |
| 609 | + self.assertNotEqual(data, crypttext) |
| 610 | + text = self.key.decrypt( |
| 611 | + crypttext, mechanism=Mechanism.AES_CTR, mechanism_param=CTRParams(b"1" * 12) |
| 612 | + ) |
| 613 | + self.assertNotEqual(data, text) |
0 commit comments