@@ -321,6 +321,14 @@ def test_invalid_nonce_length(self, backend):
321321 with pytest .raises (ValueError ):
322322 aesccm .encrypt (nonce [:6 ], pt , None )
323323
324+ with pytest .raises (ValueError ):
325+ buf = bytearray (16 )
326+ aesccm .decrypt_into (nonce , b"x" * 20 , None , buf )
327+
328+ with pytest .raises (ValueError ):
329+ buf = bytearray (16 )
330+ aesccm .decrypt_into (nonce [:6 ], b"x" * 20 , None , buf )
331+
324332 def test_vectors (self , subtests , backend ):
325333 vectors = _load_all_params (
326334 os .path .join ("ciphers" , "AES" , "CCM" ),
@@ -423,6 +431,10 @@ def test_decrypt_data_too_short(self, backend):
423431 with pytest .raises (InvalidTag ):
424432 aesccm .decrypt (b"0" * 12 , b"0" , None )
425433
434+ with pytest .raises (InvalidTag ):
435+ buf = bytearray (16 )
436+ aesccm .decrypt_into (b"0" * 12 , b"0" , None , buf )
437+
426438 def test_buffer_protocol (self , backend ):
427439 key = AESCCM .generate_key (128 )
428440 aesccm = AESCCM (key )
@@ -472,6 +484,53 @@ def test_encrypt_into_buffer_incorrect_size(self, ptlen, buflen, backend):
472484 with pytest .raises (ValueError , match = "buffer must be" ):
473485 aesccm .encrypt_into (nonce , pt , None , buf )
474486
487+ def test_decrypt_into (self , backend ):
488+ key = AESCCM .generate_key (128 )
489+ aesccm = AESCCM (key )
490+ nonce = os .urandom (12 )
491+ pt = b"decrypt me"
492+ ad = b"additional"
493+ ct = aesccm .encrypt (nonce , pt , ad )
494+ buf = bytearray (len (pt ))
495+ n = aesccm .decrypt_into (nonce , ct , ad , buf )
496+ assert n == len (pt )
497+ assert buf == pt
498+
499+ @pytest .mark .parametrize (
500+ ("ctlen" , "buflen" ), [(26 , 9 ), (26 , 11 ), (31 , 14 ), (36 , 21 )]
501+ )
502+ def test_decrypt_into_buffer_incorrect_size (self , ctlen , buflen , backend ):
503+ key = AESCCM .generate_key (128 )
504+ aesccm = AESCCM (key )
505+ nonce = os .urandom (12 )
506+ ct = b"x" * ctlen
507+ buf = bytearray (buflen )
508+ with pytest .raises (ValueError , match = "buffer must be" ):
509+ aesccm .decrypt_into (nonce , ct , None , buf )
510+
511+ def test_decrypt_into_invalid_tag (self , backend ):
512+ key = AESCCM .generate_key (128 )
513+ aesccm = AESCCM (key )
514+ nonce = os .urandom (12 )
515+ pt = b"some data"
516+ ad = b"additional"
517+ ct = aesccm .encrypt (nonce , pt , ad )
518+ # Corrupt the ciphertext
519+ corrupted_ct = bytearray (ct )
520+ corrupted_ct [0 ] ^= 1
521+ buf = bytearray (len (pt ))
522+ with pytest .raises (InvalidTag ):
523+ aesccm .decrypt_into (nonce , bytes (corrupted_ct ), ad , buf )
524+
525+ def test_decrypt_into_nonce_too_long (self , backend ):
526+ key = AESCCM .generate_key (128 )
527+ aesccm = AESCCM (key )
528+ pt = b"encrypt me" * 6600
529+ nonce = os .urandom (13 )
530+ buf = bytearray (len (pt ))
531+ with pytest .raises (ValueError , match = "Data too long for nonce" ):
532+ aesccm .decrypt_into (nonce , pt , None , buf )
533+
475534
476535def _load_gcm_vectors ():
477536 vectors = _load_all_params (
0 commit comments