1 From 8dbe0dc3eea5c689d4f76b37b93fe216cf1f00d4 Mon Sep 17 00:00:00 2001
2 From: Legrandin <helderijs@gmail.com>
3 Date: Sun, 22 Dec 2013 22:24:46 +0100
4 Subject: [PATCH] Throw exception when IV is used with ECB or CTR
6 The IV parameter is currently ignored when initializing
7 a cipher in ECB or CTR mode.
9 For CTR mode, it is confusing: it takes some time to see
10 that a different parameter is needed (the counter).
12 For ECB mode, it is outright dangerous.
14 This patch forces an exception to be raised.
16 lib/Crypto/SelfTest/Cipher/common.py | 31 +++++++++++++++++++++++--------
17 src/block_template.c | 11 +++++++++++
18 2 files changed, 34 insertions(+), 8 deletions(-)
20 diff --git a/lib/Crypto/SelfTest/Cipher/common.py b/lib/Crypto/SelfTest/Cipher/common.py
21 index 420b6ff..a5f8a88 100644
22 --- a/lib/Crypto/SelfTest/Cipher/common.py
23 +++ b/lib/Crypto/SelfTest/Cipher/common.py
24 @@ -239,16 +239,30 @@ class RoundtripTest(unittest.TestCase):
25 return """%s .decrypt() output of .encrypt() should not be garbled""" % (self.module_name,)
28 - for mode in (self.module.MODE_ECB, self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB, self.module.MODE_OPENPGP):
31 + mode = self.module.MODE_ECB
32 + encryption_cipher = self.module.new(a2b_hex(self.key), mode)
33 + ciphertext = encryption_cipher.encrypt(self.plaintext)
34 + decryption_cipher = self.module.new(a2b_hex(self.key), mode)
35 + decrypted_plaintext = decryption_cipher.decrypt(ciphertext)
36 + self.assertEqual(self.plaintext, decrypted_plaintext)
39 + mode = self.module.MODE_OPENPGP
40 + encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
41 + eiv_ciphertext = encryption_cipher.encrypt(self.plaintext)
42 + eiv = eiv_ciphertext[:self.module.block_size+2]
43 + ciphertext = eiv_ciphertext[self.module.block_size+2:]
44 + decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv)
45 + decrypted_plaintext = decryption_cipher.decrypt(ciphertext)
46 + self.assertEqual(self.plaintext, decrypted_plaintext)
48 + ## All other non-AEAD modes (but CTR)
49 + for mode in (self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB):
50 encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
51 ciphertext = encryption_cipher.encrypt(self.plaintext)
53 - if mode != self.module.MODE_OPENPGP:
54 - decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
56 - eiv = ciphertext[:self.module.block_size+2]
57 - ciphertext = ciphertext[self.module.block_size+2:]
58 - decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv)
59 + decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
60 decrypted_plaintext = decryption_cipher.decrypt(ciphertext)
61 self.assertEqual(self.plaintext, decrypted_plaintext)
63 diff --git a/src/block_template.c b/src/block_template.c
64 index f940e0e..d555ceb 100644
65 --- a/src/block_template.c
66 +++ b/src/block_template.c
67 @@ -170,6 +170,17 @@ ALGnew(PyObject *self, PyObject *args, PyObject *kwdict)
68 "Key cannot be the null string");
71 + if (IVlen != 0 && mode == MODE_ECB)
73 + PyErr_Format(PyExc_ValueError, "ECB mode does not use IV");
76 + if (IVlen != 0 && mode == MODE_CTR)
78 + PyErr_Format(PyExc_ValueError,
79 + "CTR mode needs counter parameter, not IV");
82 if (IVlen != BLOCK_SIZE && mode != MODE_ECB && mode != MODE_CTR)
84 PyErr_Format(PyExc_ValueError,
85 From 58de28a5d32bc10e15766e5a59f41b07397cc6cb Mon Sep 17 00:00:00 2001
86 From: Richard Mitchell <richard.j.mitchell@gmail.com>
87 Date: Mon, 28 Apr 2014 16:58:27 +0100
88 Subject: [PATCH] Fix speedtest run for ECB modes.
91 pct-speedtest.py | 2 ++
92 1 file changed, 2 insertions(+)
94 diff --git a/pct-speedtest.py b/pct-speedtest.py
95 index 4ce18be..c7b893a 100644
96 --- a/pct-speedtest.py
97 +++ b/pct-speedtest.py
98 @@ -121,6 +121,8 @@ class Benchmark:
99 blocks = self.random_blocks(16384, 1000)
101 cipher = module.new(key)
102 + elif mode==module.MODE_ECB:
103 + cipher = module.new(key, module.MODE_ECB)
105 cipher = module.new(key, mode, iv)