@@ -84,16 +84,16 @@ def _iter_chunks(
8484 """AES-128-CCM (CTR + CBC-MAC) implementation"""
8585 # TODO: pycrypto probably has its own implementation
8686 key_bytes = a32_to_bytes (key )
87- nonce = ((iv [0 ] << 32 ) + iv [1 ]) << 64
88- aes = AES .new (key_bytes , AES .MODE_CTR , counter = Counter .new (128 , initial_value = nonce ))
89-
9087 mac_bytes = EMPTY_IV
91- mac_cypher = AES .new (key_bytes , AES .MODE_CBC , mac_bytes )
9288 iv_bytes = a32_to_bytes ([iv [0 ], iv [1 ], iv [0 ], iv [1 ]])
93- data_in : bytes | None = yield b""
89+ nonce = (( iv [ 0 ] << 32 ) + iv [ 1 ]) << 64
9490
91+ aes = AES .new (key_bytes , AES .MODE_CTR , counter = Counter .new (128 , initial_value = nonce ))
92+ mac_cypher = AES .new (key_bytes , AES .MODE_CBC , EMPTY_IV )
9593 chunk_cypher = AES .new (key_bytes , AES .MODE_CBC , iv_bytes )
9694
95+ data_in : bytes | None = yield b""
96+
9797 while data_in is not None :
9898 if decrypt :
9999 decrypted_data = data_out = aes .decrypt (data_in )
@@ -102,10 +102,10 @@ def _iter_chunks(
102102 data_out = aes .encrypt (data_in )
103103
104104 mem_view = memoryview (decrypted_data )
105- last_16b_index = (len (decrypted_data ) % AES .block_size ) or AES .block_size
106- last_16b = pad_bytes (mem_view [- last_16b_index :])
107- chunk_cypher .encrypt (mem_view [:- last_16b_index ])
108- mac_bytes = mac_cypher .encrypt (chunk_cypher .encrypt (last_16b ))
105+ last_block_index = (len (decrypted_data ) % AES .block_size ) or AES .block_size
106+ last_block = pad_bytes (mem_view [- last_block_index :])
107+ chunk_cypher .encrypt (mem_view [:- last_block_index ])
108+ mac_bytes = mac_cypher .encrypt (chunk_cypher .encrypt (last_block ))
109109 data_in = yield data_out
110110
111111 file_mac = str_to_a32 (mac_bytes )
0 commit comments