Fix CVE-2013-7459: https://github.com/dlitz/pycrypto/issues/176 https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-7459 Copied from Debian: https://anonscm.debian.org/cgit/collab-maint/python-crypto.git/commit/?id=0de2243837ed369a086f15c50cca2be85bdfab9d Debian adapts this upstream commit: https://github.com/dlitz/pycrypto/commit/8dbe0dc3eea5c689d4f76b37b93fe216cf1f00d4 From 8dbe0dc3eea5c689d4f76b37b93fe216cf1f00d4 Mon Sep 17 00:00:00 2001 From: Legrandin Date: Sun, 22 Dec 2013 22:24:46 +0100 Subject: [PATCH] Throw exception when IV is used with ECB or CTR The IV parameter is currently ignored when initializing a cipher in ECB or CTR mode. For CTR mode, it is confusing: it takes some time to see that a different parameter is needed (the counter). For ECB mode, it is outright dangerous. This patch forces an exception to be raised. --- lib/Crypto/SelfTest/Cipher/common.py | 31 +++++++++++++++++++++++-------- src/block_template.c | 11 +++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) --- a/lib/Crypto/SelfTest/Cipher/common.py +++ b/lib/Crypto/SelfTest/Cipher/common.py @@ -239,19 +239,34 @@ class RoundtripTest(unittest.TestCase): return """%s .decrypt() output of .encrypt() should not be garbled""" % (self.module_name,) def runTest(self): - for mode in (self.module.MODE_ECB, self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB, self.module.MODE_OPENPGP): + + ## ECB mode + mode = self.module.MODE_ECB + encryption_cipher = self.module.new(a2b_hex(self.key), mode) + ciphertext = encryption_cipher.encrypt(self.plaintext) + decryption_cipher = self.module.new(a2b_hex(self.key), mode) + decrypted_plaintext = decryption_cipher.decrypt(ciphertext) + self.assertEqual(self.plaintext, decrypted_plaintext) + + ## OPENPGP mode + mode = self.module.MODE_OPENPGP + encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) + eiv_ciphertext = encryption_cipher.encrypt(self.plaintext) + eiv = eiv_ciphertext[:self.module.block_size+2] + ciphertext = eiv_ciphertext[self.module.block_size+2:] + decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv) + decrypted_plaintext = decryption_cipher.decrypt(ciphertext) + self.assertEqual(self.plaintext, decrypted_plaintext) + + ## All other non-AEAD modes (but CTR) + for mode in (self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB): encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) ciphertext = encryption_cipher.encrypt(self.plaintext) - - if mode != self.module.MODE_OPENPGP: - decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) - else: - eiv = ciphertext[:self.module.block_size+2] - ciphertext = ciphertext[self.module.block_size+2:] - decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv) + decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv) decrypted_plaintext = decryption_cipher.decrypt(ciphertext) self.assertEqual(self.plaintext, decrypted_plaintext) + class PGPTest(unittest.TestCase): def __init__(self, module, params): unittest.TestCase.__init__(self) --- a/src/block_template.c +++ b/src/block_template.c @@ -170,6 +170,17 @@ ALGnew(PyObject *self, PyObject *args, P "Key cannot be the null string"); return NULL; } + if (IVlen != 0 && mode == MODE_ECB) + { + PyErr_Format(PyExc_ValueError, "ECB mode does not use IV"); + return NULL; + } + if (IVlen != 0 && mode == MODE_CTR) + { + PyErr_Format(PyExc_ValueError, + "CTR mode needs counter parameter, not IV"); + return NULL; + } if (IVlen != BLOCK_SIZE && mode != MODE_ECB && mode != MODE_CTR) { PyErr_Format(PyExc_ValueError, class='msg-avail'>...* gnu/packages/admin.scm (wpa-supplicant-minimal, mingetty, di), * gnu/packages/audio.scm (fil-plugins), * gnu/packages/base.scm (make-ld-wrapper, make-glibc-locales), * gnu/packages/bioinformatics.scm (r-samtools), * gnu/packages/chez.scm (chez-mit), * gnu/packages/commencement.scm (make-gcc-toolchain), * gnu/packages/compression.scm (fastjar), * gnu/packages/cran.scm (r-maps, r-rcpp), * gnu/packages/databases.scm (sparql-query), * gnu/packages/dunst.scm (dunst), * gnu/packages/ftp.scm (ncftp), * gnu/packages/gl.scm (freeglut), * gnu/packages/haskell-check.scm (ghc-tasty-golden), * gnu/packages/haskell.scm (ghc-case-insensitive, ghc-text, ghc-haskell-src, ghc-syb, ghc-deepseq-generics, ghc-network-uri, ghc-rerebase, ghc-zlib), * gnu/packages/image.scm (jbig2dec), * gnu/packages/kde-frameworks.scm (kinit, karchive), * gnu/packages/linux.scm (wireless-tools, perf, module-init-tools, kbd), * gnu/packages/lirc.scm (lirc), * gnu/packages/lisp.scm (uglify-js), * gnu/packages/mate.scm (mate-netbook), * gnu/packages/microcom.scm (microcom), * gnu/packages/music.scm (bristol), * gnu/packages/networking.scm (perl-geo-ip), * gnu/packages/patchutils.scm (patches), * gnu/packages/perl-check.scm (perl-test-more-utf8), * gnu/packages/perl.scm (perl-log-report-optional, perl-file-which, perl-io-tty, perl-log-any, perl-digest-sha1, perl-class-load, perl-regexp-common, perl-module-pluggable, perl-class-modifier), * gnu/packages/python-xyz.scm (python-backports-abc, python-natsort), * gnu/packages/samba.scm (iniparser), * gnu/packages/search.scm (mlocate), * gnu/packages/spice.scm (spice), * gnu/packages/statistics.scm (r-dt, r-lubridate, r-estimability, r-commonmark, r-digest, r-viridislite, r-stringr), * gnu/packages/tex.scm (texlive-latex-changebar), * gnu/packages/version-control.scm (subversion), * gnu/packages/w3m.scm (w3m), * gnu/packages/web.scm (perl-http-parser, perl-plack-middleware-reverseproxy), * gnu/packages/xorg.scm (xkeyboard-config, mkfontdir, xcursor-theme, mkfontscale, xinit, font-alias)[description]: Use @code instead of quotes. Efraim Flashner