def test_ctr256_decrypt(self): key = bytes.fromhex(""" 603DEB10 15CA71BE 2B73AEF0 857D7781 1F352C07 3B6108D7 2D9810A3 0914DFF4 """.replace(" ", "").replace("\n", "")) iv = bytes.fromhex(""" F0F1F2F3 F4F5F6F7 F8F9FAFB FCFDFEFF """.replace(" ", "").replace("\n", "")) ciphertext = bytes.fromhex(""" 601EC313 775789A5 B7A7F504 BBF3D228 F443E3CA 4D62B59A CA84E990 CACAF5C5 2B0930DA A23DE94C E87017BA 2D84988D DFC9C58D B67AADA6 13C2DD08 457941A6 """.replace(" ", "").replace("\n", "")) plaintext = bytes.fromhex(""" 6BC1BEE2 2E409F96 E93D7E11 7393172A AE2D8A57 1E03AC9C 9EB76FAC 45AF8E51 30C81C46 A35CE411 E5FBC119 1A0A52EF F69F2445 DF4F9B17 AD2B417B E66C3710 """.replace(" ", "").replace("\n", "")) self.assertEqual( tgcrypto.ctr256_decrypt(ciphertext, key, iv, bytes(1)), plaintext)
def decrypt(self, data): """ Decrypts the given cipher text through AES CTR :param data: the cipher text to be decrypted. :return: the decrypted plain text. """ if tgcrypto: return tgcrypto.ctr256_decrypt(data, *self._aes) return self._aes.decrypt(data)
def ctr256_decrypt(data: bytes, key: bytes, iv: bytearray, state: bytearray = None) -> bytes: return tgcrypto.ctr256_decrypt(data, key, iv, state or bytearray(1))
def test_ctr256_decrypt_invalid_state_value(self): with self.assertRaisesRegex( ValueError, r"State value must be in the range \[0, 15\]"): tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(16), bytes([16]))
def test_ctr256_decrypt_invalid_state_size(self): with self.assertRaisesRegex(ValueError, r"State size must be exactly 1 byte"): tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(16), bytes([1, 2, 3]))
def test_ctr256_decrypt_invalid_iv_size(self): with self.assertRaisesRegex(ValueError, r"IV size must be exactly 16 bytes"): tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(15), bytes(1))
def test_ctr256_decrypt_invalid_key_size(self): with self.assertRaisesRegex(ValueError, r"Key size must be exactly 32 bytes"): tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(31), os.urandom(16), bytes(1))
def test_ctr256_decrypt_empty_data(self): with self.assertRaisesRegex(ValueError, r"Data must not be empty"): tgcrypto.ctr256_decrypt(b"", os.urandom(32), os.urandom(16), bytes(1))
def test_ctr256_decrypt_invalid_args_type(self): with self.assertRaisesRegex(TypeError, self.TYPE_ERROR_PATTERN): tgcrypto.ctr256_decrypt(1, 2, 3, 4)
def test_ctr256_decrypt_invalid_args_count(self): with self.assertRaisesRegex( TypeError, r"function takes exactly \d arguments \(\d given\)"): tgcrypto.ctr256_decrypt(os.urandom(8), os.urandom(32), os.urandom(16))
def decrypt(self, data): return tgcrypto.ctr256_decrypt(data, self.key, self.iv, bytes(1))