@@ -611,3 +611,136 @@ def test_encrypt_ctr_nonce_mismatch(self):
611611 crypttext , mechanism = Mechanism .AES_CTR , mechanism_param = CTRParams (b"1" * 12 )
612612 )
613613 self .assertNotEqual (data , text )
614+
615+ @parameterized .expand (
616+ [
617+ (
618+ "ae6852f8121067cc4bf7a5765577f39e" ,
619+ b"Single block msg" ,
620+ "00000030" ,
621+ "0000000000000000" ,
622+ "e4095d4fb7a7b3792d6175a3261311b8" ,
623+ ),
624+ (
625+ "7e24067817fae0d743d6ce1f32539163" ,
626+ bytes (range (0x20 )),
627+ "006cb6db" ,
628+ "c0543b59da48d90b" ,
629+ "5104a106168a72d9790d41ee8edad388eb2e1efc46da57c8fce630df9141be28" ,
630+ ),
631+ (
632+ "7691be035e5020a8ac6e618529f9a0dc" ,
633+ bytes (range (0x24 )),
634+ "00e0017b" ,
635+ "27777f3f4a1786f0" ,
636+ "c1cf48a89f2ffdd9cf4652e9efdb72d74540a42bde6d7836d59a5ceaaef3105325b2072f" ,
637+ ),
638+ (
639+ "16af5b145fc9f579c175f93e3bfb0eed863d06ccfdb78515" ,
640+ b"Single block msg" ,
641+ "00000048" ,
642+ "36733c147d6d93cb" ,
643+ "4b55384fe259c9c84e7935a003cbe928" ,
644+ ),
645+ (
646+ "7c5cb2401b3dc33c19e7340819e0f69c678c3db8e6f6a91a" ,
647+ bytes (range (0x20 )),
648+ "0096b03b" ,
649+ "020c6eadc2cb500d" ,
650+ "453243fc609b23327edfaafa7131cd9f8490701c5ad4a79cfc1fe0ff42f4fb00" ,
651+ ),
652+ (
653+ "02bf391ee8ecb159b959617b0965279bf59b60a786d3e0fe" ,
654+ bytes (range (0x24 )),
655+ "0007bdfd" ,
656+ "5cbd60278dcc0912" ,
657+ "96893fc55e5c722f540b7dd1ddf7e758d288bc95c69165884536c811662f2188abee0935" ,
658+ ),
659+ (
660+ "776beff2851db06f4c8a0542c8696f6c6a81af1eec96b4d37fc1d689e6c1c104" ,
661+ b"Single block msg" ,
662+ "00000060" ,
663+ "db5672c97aa8f0b2" ,
664+ "145ad01dbf824ec7560863dc71e3e0c0" ,
665+ ),
666+ (
667+ "f6d66d6bd52d59bb0796365879eff886c66dd51a5b6a99744b50590c87a23884" ,
668+ bytes (range (0x20 )),
669+ "00faac24" ,
670+ "c1585ef15a43d875" ,
671+ "f05e231b3894612c49ee000b804eb2a9b8306b508f839d6a5530831d9344af1c" ,
672+ ),
673+ ]
674+ )
675+ # https://github.com/opencryptoki/opencryptoki/issues/881
676+ @FIXME .opencryptoki
677+ @requires (Mechanism .AES_CTR )
678+ def test_aes_ctr_test_vector (self , key , plaintext , nonce , iv , expected_ciphertext ):
679+ """Official test vectors from RFC 3686"""
680+ key = self .session .create_object (
681+ {
682+ pkcs11 .Attribute .CLASS : pkcs11 .ObjectClass .SECRET_KEY ,
683+ pkcs11 .Attribute .KEY_TYPE : pkcs11 .KeyType .AES ,
684+ pkcs11 .Attribute .VALUE : bytes .fromhex (key ),
685+ }
686+ )
687+
688+ params = CTRParams (bytes .fromhex (nonce ) + bytes .fromhex (iv ))
689+ ciphertext = key .encrypt (plaintext , mechanism_param = params , mechanism = Mechanism .AES_CTR )
690+ self .assertEqual (bytes .fromhex (expected_ciphertext ), ciphertext )
691+
692+ @parameterized .expand (
693+ [
694+ (
695+ "00000000000000000000000000000000" ,
696+ "" ,
697+ "" ,
698+ "000000000000000000000000" ,
699+ "" ,
700+ "58e2fccefa7e3061367f1d57a4e7455a" ,
701+ ),
702+ (
703+ "00000000000000000000000000000000" ,
704+ "00000000000000000000000000000000" ,
705+ "" ,
706+ "000000000000000000000000" ,
707+ "0388dace60b6a392f328c2b971b2fe78" ,
708+ "ab6e47d42cec13bdf53a67b21257bddf" ,
709+ ),
710+ (
711+ "feffe9928665731c6d6a8f9467308308" ,
712+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255" ,
713+ "" ,
714+ "cafebabefacedbaddecaf888" ,
715+ "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985" ,
716+ "4d5c2af327cd64a62cf35abd2ba6fab4" ,
717+ ),
718+ (
719+ "feffe9928665731c6d6a8f9467308308" ,
720+ "d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39" ,
721+ "feedfacedeadbeeffeedfacedeadbeefabaddad2" ,
722+ "cafebabefacedbaddecaf888" ,
723+ "42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091" ,
724+ "5bc94fbc3221a5db94fae95ae7121a47" ,
725+ ),
726+ ],
727+ )
728+ @requires (Mechanism .AES_GCM )
729+ def test_aes_gcm_test_vector (
730+ self , key , plaintext , aad , nonce , expected_ciphertext , expected_tag
731+ ):
732+ """Some test vectors from McGrew-Viega"""
733+ key = self .session .create_object (
734+ {
735+ pkcs11 .Attribute .CLASS : pkcs11 .ObjectClass .SECRET_KEY ,
736+ pkcs11 .Attribute .KEY_TYPE : pkcs11 .KeyType .AES ,
737+ pkcs11 .Attribute .VALUE : bytes .fromhex (key ),
738+ }
739+ )
740+
741+ params = GCMParams (nonce = bytes .fromhex (nonce ), aad = bytes .fromhex (aad ))
742+ result = key .encrypt (
743+ bytes .fromhex (plaintext ), mechanism_param = params , mechanism = Mechanism .AES_GCM
744+ )
745+ expected_output = bytes .fromhex (expected_ciphertext ) + bytes .fromhex (expected_tag )
746+ self .assertEqual (expected_output , result )
0 commit comments