From 661f2a1f6ed02b5b2f21e340845361e70610ff3f Mon Sep 17 00:00:00 2001 From: Legrandin Date: Sun, 4 Aug 2013 22:46:06 +0200 Subject: Made blockalgo.py more PEP-8 compliant (pre-AEAD) [dlitz@dlitz.net: Original commit was:] commit ca460a79aecdbf6e5973e99f8bdbf3888b6d34d2 Author: Legrandin Date: Sun Aug 4 22:46:06 2013 +0200 Made blockalgo.py more PEP-8 compliant The second parameter of the _GHASH constructor is now the length of the block (block_size) and not the full module. [dlitz@dlitz.net: Included only style-related changes that apply cleanly to the master branch (pre-AEAD)] [dlitz@dlitz.net: Omitted functional changes that were made in the author's original commit.] [dlitz@dlitz.net: Omitted some changes that broke exception messages onto multiple lines.] [dlitz@dlitz.net: Omitted some changes that broke arithmetic expressions onto multiple lines.] --- lib/Crypto/Cipher/blockalgo.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/Crypto/Cipher/blockalgo.py b/lib/Crypto/Cipher/blockalgo.py index 89410f5..bdd1dec 100644 --- a/lib/Crypto/Cipher/blockalgo.py +++ b/lib/Crypto/Cipher/blockalgo.py @@ -42,8 +42,8 @@ MODE_ECB = 1 #: (*IV*) is required. #: #: The *IV* is a data block to be transmitted to the receiver. -#: The *IV* can be made public, but it must be authenticated by the receiver and -#: it should be picked randomly. +#: The *IV* can be made public, but it must be authenticated by the receiver +#: and it should be picked randomly. #: #: See `NIST SP800-38A`_ , Section 6.2 . #: @@ -73,7 +73,8 @@ MODE_PGP = 4 #: *Output FeedBack (OFB)*. This mode is very similar to CBC, but it #: transforms the underlying block cipher into a stream cipher. -#: The keystream is the iterated block encryption of an Initialization Vector (*IV*). +#: The keystream is the iterated block encryption of an +#: Initialization Vector (*IV*). #: #: The *IV* is a data block to be transmitted to the receiver. #: The *IV* can be made public, but it should be picked randomly. @@ -110,21 +111,23 @@ MODE_OFB = 5 #: .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf MODE_CTR = 6 -#: OpenPGP. This mode is a variant of CFB, and it is only used in PGP and OpenPGP_ applications. -#: An Initialization Vector (*IV*) is required. -#: -#: Unlike CFB, the IV is not transmitted to the receiver. Instead, the *encrypted* IV is. -#: The IV is a random data block. Two of its bytes are duplicated to act as a checksum -#: for the correctness of the key. The encrypted IV is therefore 2 bytes longer than -#: the clean IV. +#: OpenPGP. This mode is a variant of CFB, and it is only used in PGP and +#: OpenPGP_ applications. An Initialization Vector (*IV*) is required. +#: +#: Unlike CFB, the IV is not transmitted to the receiver. +#: Instead, the *encrypted* IV is. +#: The IV is a random data block. Two of its bytes are duplicated to act +#: as a checksum for the correctness of the key. The encrypted IV is +#: therefore 2 bytes longer than the clean IV. #: #: .. _OpenPGP: http://tools.ietf.org/html/rfc4880 MODE_OPENPGP = 7 def _getParameter(name, index, args, kwargs, default=None): """Find a parameter in tuple and dictionary arguments a function receives""" + param = kwargs.get(name) - if len(args)>index: + if len(args) > index: if param: raise ValueError("Parameter '%s' is specified twice" % name) param = args[index] @@ -225,14 +228,15 @@ class BlockAlgo: if self.mode == MODE_OPENPGP: padding_length = (self.block_size - len(plaintext) % self.block_size) % self.block_size - if padding_length>0: - # CFB mode requires ciphertext to have length multiple of block size, + if padding_length > 0: + # CFB mode requires ciphertext to have length multiple + # of block size, # but PGP mode allows the last block to be shorter if self._done_last_block: raise ValueError("Only the last chunk is allowed to have length not multiple of %d bytes", self.block_size) self._done_last_block = True - padded = plaintext + b('\x00')*padding_length + padded = plaintext + b('\x00') * padding_length res = self._cipher.encrypt(padded)[:len(plaintext)] else: res = self._cipher.encrypt(plaintext) @@ -280,14 +284,15 @@ class BlockAlgo: """ if self.mode == MODE_OPENPGP: padding_length = (self.block_size - len(ciphertext) % self.block_size) % self.block_size - if padding_length>0: - # CFB mode requires ciphertext to have length multiple of block size, + if padding_length > 0: + # CFB mode requires ciphertext to have length multiple + # of block size, # but PGP mode allows the last block to be shorter if self._done_last_block: raise ValueError("Only the last chunk is allowed to have length not multiple of %d bytes", self.block_size) self._done_last_block = True - padded = ciphertext + b('\x00')*padding_length + padded = ciphertext + b('\x00') * padding_length res = self._cipher.decrypt(padded)[:len(ciphertext)] else: res = self._cipher.decrypt(ciphertext) -- cgit v1.2.1