@@ -513,6 +513,10 @@ def test_decrypt_data_too_short(self):
513513 with pytest .raises (InvalidTag ):
514514 aesgcm .decrypt (b"0" * 12 , b"0" , None )
515515
516+ with pytest .raises (InvalidTag ):
517+ buf = bytearray (16 )
518+ aesgcm .decrypt_into (b"0" * 12 , b"0" , None , buf )
519+
516520 def test_vectors (self , backend , subtests ):
517521 vectors = _load_gcm_vectors ()
518522 for vector in vectors :
@@ -573,6 +577,9 @@ def test_invalid_nonce_length(self, length, backend):
573577 aesgcm .encrypt_into (b"\x00 " * length , b"hi" , None , buf )
574578 with pytest .raises (ValueError ):
575579 aesgcm .decrypt (b"\x00 " * length , b"hi" , None )
580+ with pytest .raises (ValueError ):
581+ buf = bytearray (16 )
582+ aesgcm .decrypt_into (b"\x00 " * length , b"hi" , None , buf )
576583
577584 def test_bad_key (self , backend ):
578585 with pytest .raises (TypeError ):
@@ -650,6 +657,44 @@ def test_encrypt_into_buffer_incorrect_size(self, ptlen, buflen, backend):
650657 with pytest .raises (ValueError , match = "buffer must be" ):
651658 aesgcm .encrypt_into (nonce , pt , None , buf )
652659
660+ def test_decrypt_into (self , backend ):
661+ key = AESGCM .generate_key (128 )
662+ aesgcm = AESGCM (key )
663+ nonce = os .urandom (12 )
664+ pt = b"decrypt me"
665+ ad = b"additional"
666+ ct = aesgcm .encrypt (nonce , pt , ad )
667+ buf = bytearray (len (pt ))
668+ n = aesgcm .decrypt_into (nonce , ct , ad , buf )
669+ assert n == len (pt )
670+ assert buf == pt
671+
672+ @pytest .mark .parametrize (
673+ ("ctlen" , "buflen" ), [(26 , 9 ), (26 , 11 ), (31 , 14 ), (36 , 21 )]
674+ )
675+ def test_decrypt_into_buffer_incorrect_size (self , ctlen , buflen , backend ):
676+ key = AESGCM .generate_key (128 )
677+ aesgcm = AESGCM (key )
678+ nonce = os .urandom (12 )
679+ ct = b"x" * ctlen
680+ buf = bytearray (buflen )
681+ with pytest .raises (ValueError , match = "buffer must be" ):
682+ aesgcm .decrypt_into (nonce , ct , None , buf )
683+
684+ def test_decrypt_into_invalid_tag (self , backend ):
685+ key = AESGCM .generate_key (128 )
686+ aesgcm = AESGCM (key )
687+ nonce = os .urandom (12 )
688+ pt = b"some data"
689+ ad = b"additional"
690+ ct = aesgcm .encrypt (nonce , pt , ad )
691+ # Corrupt the ciphertext
692+ corrupted_ct = bytearray (ct )
693+ corrupted_ct [0 ] ^= 1
694+ buf = bytearray (len (pt ))
695+ with pytest .raises (InvalidTag ):
696+ aesgcm .decrypt_into (nonce , bytes (corrupted_ct ), ad , buf )
697+
653698
654699@pytest .mark .skipif (
655700 _aead_supported (AESOCB3 ),
0 commit comments