You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.3 KiB

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