From d8671b9d0943fd6e42365a04880724c2ff864a46 Mon Sep 17 00:00:00 2001 From: Helder Eijs Date: Fri, 3 Dec 2021 22:12:42 +0100 Subject: [PATCH] GH#582 Fix incorrect CBC decryption when in/out use the same buffer --- lib/Crypto/SelfTest/Cipher/common.py | 4 +--- lib/Crypto/SelfTest/Cipher/test_CBC.py | 28 +++++++++++++------------- src/raw_cbc.c | 3 ++- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/Crypto/SelfTest/Cipher/common.py b/lib/Crypto/SelfTest/Cipher/common.py index 4ba2c33a9..c5bc755ab 100644 --- a/lib/Crypto/SelfTest/Cipher/common.py +++ b/lib/Crypto/SelfTest/Cipher/common.py @@ -499,9 +499,7 @@ def make_stream_tests(module, module_name, test_data): ByteArrayTest(module, params), ] - import sys - if sys.version[:3] != '2.6': - tests.append(MemoryviewTest(module, params)) + tests.append(MemoryviewTest(module, params)) extra_tests_added = True # Add the test to the test suite diff --git a/lib/Crypto/SelfTest/Cipher/test_CBC.py b/lib/Crypto/SelfTest/Cipher/test_CBC.py index 13c32838c..573808fd1 100644 --- a/lib/Crypto/SelfTest/Cipher/test_CBC.py +++ b/lib/Crypto/SelfTest/Cipher/test_CBC.py @@ -168,7 +168,7 @@ def test_data_must_be_bytes(self): self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') def test_bytearray(self): - data = b"1" * 16 + data = b"1" * 128 data_ba = bytearray(data) # Encrypt @@ -201,7 +201,7 @@ def test_bytearray(self): self.assertEqual(ref3, ref4) def test_memoryview(self): - data = b"1" * 16 + data = b"1" * 128 data_mv = memoryview(bytearray(data)) # Encrypt @@ -232,19 +232,19 @@ def test_memoryview(self): ref4 = cipher4.decrypt(data_mv) self.assertEqual(ref3, ref4) - + def test_output_param(self): - pt = b'5' * 16 + pt = b'5' * 128 cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) ct = cipher.encrypt(pt) - output = bytearray(16) + output = bytearray(128) cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) res = cipher.encrypt(pt, output=output) self.assertEqual(ct, output) self.assertEqual(res, None) - + cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) res = cipher.decrypt(ct, output=output) self.assertEqual(pt, output) @@ -253,7 +253,7 @@ def test_output_param(self): def test_output_param_same_buffer(self): - pt = b'5' * 16 + pt = b'5' * 128 cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) ct = cipher.encrypt(pt) @@ -262,7 +262,7 @@ def test_output_param_same_buffer(self): res = cipher.encrypt(pt_ba, output=pt_ba) self.assertEqual(ct, pt_ba) self.assertEqual(res, None) - + ct_ba = bytearray(ct) cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) res = cipher.decrypt(ct_ba, output=ct_ba) @@ -271,29 +271,29 @@ def test_output_param_same_buffer(self): def test_output_param_memoryview(self): - - pt = b'5' * 16 + + pt = b'5' * 128 cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) ct = cipher.encrypt(pt) - output = memoryview(bytearray(16)) + output = memoryview(bytearray(128)) cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) cipher.encrypt(pt, output=output) self.assertEqual(ct, output) - + cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) cipher.decrypt(ct, output=output) self.assertEqual(pt, output) def test_output_param_neg(self): - pt = b'5' * 16 + pt = b'5' * 128 cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) ct = cipher.encrypt(pt) cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) - + cipher = AES.new(b'4'*16, self.aes_mode, iv=self.iv_128) self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) diff --git a/src/raw_cbc.c b/src/raw_cbc.c index a8a6b6dc8..36e5247db 100644 --- a/src/raw_cbc.c +++ b/src/raw_cbc.c @@ -137,9 +137,10 @@ EXPORT_SYM int CBC_decrypt(CbcModeState *cbcState, return result; for (i=0; i