From a816ab8ea9fa537be2346aa25c3b0a836eec644e Mon Sep 17 00:00:00 2001 From: tiftof Date: Tue, 7 Oct 2008 17:56:40 +0000 Subject: [PATCH] mode constants are only in blockcipher "from blockcipher import *" instead of define constant in every wrapper XTS check was already in blockcipher class git-svn-id: svn+ssh://devel.yobi.be/home/svn/CryptoPlus/trunk@60 49921240-e1b6-4d8d-af3b-5ae6c3d9e7c1 --- src/Cipher/ARC2.py | 21 ++++++--------------- src/Cipher/Blowfish.py | 21 ++++++--------------- src/Cipher/CAST.py | 21 ++++++--------------- src/Cipher/DES.py | 19 ++++++------------- src/Cipher/DES3.py | 20 ++++++-------------- src/Cipher/IDEA.py | 23 ++++++----------------- src/Cipher/RC5.py | 20 ++++++-------------- src/Cipher/blockcipher.py | 1 + src/Cipher/python_AES.py | 26 ++++++-------------------- src/Cipher/python_Blowfish.py | 32 ++++++++++++-------------------- src/Cipher/python_DES.py | 19 ++++++------------- src/Cipher/python_DES3.py | 19 ++++++------------- src/Cipher/python_PRESENT.py | 21 ++++++--------------- src/Cipher/python_Rijndael.py | 26 ++++++-------------------- src/Cipher/python_Serpent.py | 20 ++++++-------------- src/Cipher/python_Twofish.py | 24 ++++++------------------ 16 files changed, 97 insertions(+), 236 deletions(-) diff --git a/src/Cipher/ARC2.py b/src/Cipher/ARC2.py index 2cbd4a3..a50290c 100644 --- a/src/Cipher/ARC2.py +++ b/src/Cipher/ARC2.py @@ -1,25 +1,16 @@ -import blockcipher +from blockcipher import * import Crypto.Cipher.ARC2 import Crypto from pkg_resources import parse_version -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -#RC2 blocksize is 8bytes, XTS requires 16bytes -#MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,effective_keylen=None): +def new(key,mode=MODE_ECB,IV=None,counter=None,effective_keylen=None): """Create a new cipher object ARC2 using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,effective_keylen=None): + new(key,mode=MODE_ECB,IV=None,counter=None,effective_keylen=None): key = raw string containing the keys - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -40,7 +31,7 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,effective_keylen=None """ return ARC2(key,mode,IV,counter,effective_keylen) -class ARC2(blockcipher.BlockCipher): +class ARC2(BlockCipher): def __init__(self,key,mode,IV,counter,effective_keylen): # pycrypto versions newer than 2.0.1 will have support for "effective_keylen" if parse_version(Crypto.__version__) <= parse_version("2.0.1"): @@ -48,7 +39,7 @@ class ARC2(blockcipher.BlockCipher): else: self.cipher = Crypto.Cipher.ARC2.new(key,effective_keylen=effective_keylen) self.blocksize = Crypto.Cipher.ARC2.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/Blowfish.py b/src/Cipher/Blowfish.py index f272b73..6ffa417 100644 --- a/src/Cipher/Blowfish.py +++ b/src/Cipher/Blowfish.py @@ -1,23 +1,14 @@ -import blockcipher +from blockcipher import * import Crypto.Cipher.Blowfish -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -#XTS only works with blocksizes of 16 bytes; Blowfish -> 8 bytes -#MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object Blowfish using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the key - mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -66,11 +57,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return Blowfish(key,mode,IV,counter) -class Blowfish(blockcipher.BlockCipher): +class Blowfish(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = Crypto.Cipher.Blowfish.new(key) self.blocksize = Crypto.Cipher.Blowfish.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/CAST.py b/src/Cipher/CAST.py index a77d0fd..94175a0 100644 --- a/src/Cipher/CAST.py +++ b/src/Cipher/CAST.py @@ -1,23 +1,14 @@ -import blockcipher +from blockcipher import * import Crypto.Cipher.CAST -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -#CAST blocksize is 8bytes, XTS requires 16bytes -#MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object CAST using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the keys - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -44,11 +35,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return CAST(key,mode,IV,counter) -class CAST(blockcipher.BlockCipher): +class CAST(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = Crypto.Cipher.CAST.new(key) self.blocksize = Crypto.Cipher.CAST.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/DES.py b/src/Cipher/DES.py index 25ba95a..22d4103 100644 --- a/src/Cipher/DES.py +++ b/src/Cipher/DES.py @@ -1,21 +1,14 @@ -import blockcipher +from blockcipher import * import Crypto.Cipher.DES -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object DES using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the keys - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -36,11 +29,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return DES(key,mode,IV,counter) -class DES(blockcipher.BlockCipher): +class DES(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = Crypto.Cipher.DES.new(key) self.blocksize = Crypto.Cipher.DES.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/DES3.py b/src/Cipher/DES3.py index e2adef8..7e9155a 100644 --- a/src/Cipher/DES3.py +++ b/src/Cipher/DES3.py @@ -1,24 +1,16 @@ -import blockcipher +from blockcipher import * import Crypto.Cipher.DES3 -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_CMAC = 8 -#TODO: XTS nog niet mogelijk -> blocksize des = 8, XTS = 16 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object DES using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the 2/3 keys - DES-EDE2: supply 2 keys as 1 single concatenated 16byte key= key1|key2 - DES-EDE3: supply 3 keys as 1 single concatenated 24byte key= key1|key2|key3 - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -62,11 +54,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return DES3(key,mode,IV,counter) -class DES3(blockcipher.BlockCipher): +class DES3(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = Crypto.Cipher.DES3.new(key) self.blocksize = Crypto.Cipher.DES3.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/IDEA.py b/src/Cipher/IDEA.py index 486ab7b..f53e116 100644 --- a/src/Cipher/IDEA.py +++ b/src/Cipher/IDEA.py @@ -1,29 +1,18 @@ -# key size = 16bytes -# blocksize = 8bytes -import blockcipher +from blockcipher import * try: import Crypto.Cipher.IDEA except ImportError: print "Crypto.Cipher.IDEA isn't available. You're probably using the Debian pycrypto version. Install the original pycrypto for IDEA." raise -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -#IDEA blocksize is 8bytes, XTS requires 16bytes -#MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object IDEA using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the keys - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -40,11 +29,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return IDEA(key,mode,IV,counter) -class IDEA(blockcipher.BlockCipher): +class IDEA(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = Crypto.Cipher.IDEA.new(key) self.blocksize = Crypto.Cipher.IDEA.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/RC5.py b/src/Cipher/RC5.py index c4eda5c..d8e761f 100644 --- a/src/Cipher/RC5.py +++ b/src/Cipher/RC5.py @@ -1,26 +1,18 @@ -import blockcipher +from blockcipher import * try: import Crypto.Cipher.RC5 except ImportError: print "Crypto.Cipher.RC5 isn't available. You're probably using the Debian pycrypto version. Install the original pycrypto for RC5." raise -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,rounds=12,word_size=32): +def new(key,mode=MODE_ECB,IV=None,counter=None,rounds=12,word_size=32): """Create a new cipher object RC5 using pycrypto for algo and pycryptoplus for ciphermode - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the keys - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -38,7 +30,7 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,rounds=12,word_size=3 """ return RC5(key,mode,IV,counter,rounds,word_size) -class RC5(blockcipher.BlockCipher): +class RC5(BlockCipher): def __init__(self,key,mode,IV,counter,rounds,word_size): if mode == MODE_XTS: #XTS implementation only works with blocksizes of 16 bytes @@ -53,7 +45,7 @@ class RC5(blockcipher.BlockCipher): else: self.cipher = Crypto.Cipher.RC5.new(key,rounds=rounds,word_size=word_size) self.blocksize = self.cipher.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/blockcipher.py b/src/Cipher/blockcipher.py index a3ab7c6..772c764 100644 --- a/src/Cipher/blockcipher.py +++ b/src/Cipher/blockcipher.py @@ -41,6 +41,7 @@ class BlockCipher(): assert self.blocksize == 16 self.chain = XTS(self.cipher, self.cipher2) elif mode == MODE_CMAC: + assert self.blocksize in (8,16) self.chain = CMAC(self.cipher,self.blocksize) def encrypt(self,plaintext,n=''): diff --git a/src/Cipher/python_AES.py b/src/Cipher/python_AES.py index 878f15a..2d1d951 100644 --- a/src/Cipher/python_AES.py +++ b/src/Cipher/python_AES.py @@ -1,29 +1,15 @@ -# wrapper for rijndael.py. rijndael.py can be found here: -# http://bitconjurer.org/rijndael.py -# other possible python AES implementations: -# http://psionicist.online.fr/code/rijndael.py.txt -# http://jclement.ca/software/pyrijndael/ - -import blockcipher +from blockcipher import * from rijndael import rijndael -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object Wrapper for pure python implementation rijndael.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the key, AES-128..256 will be selected according to the key length -> when using XTS mode: the key should be a tuple containing the 2 keys needed - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption IV = IV as a raw string -> needed for CBC, CFB and OFB mode @@ -273,7 +259,7 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return python_AES(key,mode,IV,counter) -class python_AES(blockcipher.BlockCipher): +class python_AES(BlockCipher): def __init__(self,key,mode,IV,counter): if mode == MODE_XTS: assert type(key) is tuple @@ -282,7 +268,7 @@ class python_AES(blockcipher.BlockCipher): else: self.cipher = rijndael(key, 16) self.blocksize = 16 - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_Blowfish.py b/src/Cipher/python_Blowfish.py index ba4a7f8..6c13fd9 100644 --- a/src/Cipher/python_Blowfish.py +++ b/src/Cipher/python_Blowfish.py @@ -1,27 +1,19 @@ -# source of the used python implementation of blowfish -# http://www.michaelgilfix.com/files/blowfish.py -# other possibility: -# http://www.4dsolutions.net/cgi-bin/py2html.cgi?script=/ocn/python/blowfish.py -# => difficulties: doesn't define a class, only functions -# - -import blockcipher +from blockcipher import * from pyblowfish import Blowfish -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -#XTS only works with blocksizes of 16 bytes; Blowfish -> 8 bytes -#MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object Wrapper for pure python implementation pyblowfish.py + new(key,mode=MODE_ECB,IV=None,counter=None): + key = raw string containing the key + mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB + IV = IV as a raw string + -> only needed for CBC mode + counter = counter object (CryptoPlus.Util.util.Counter) + -> only needed for CTR mode + EXAMPLE: (http://www.schneier.com/code/vectors.txt) ---------- >>> import python_Blowfish @@ -64,11 +56,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): 'E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA'""" return python_Blowfish(key,mode,IV,counter) -class python_Blowfish(blockcipher.BlockCipher): +class python_Blowfish(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = Blowfish(key) self.blocksize = 8 - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_DES.py b/src/Cipher/python_DES.py index 3b0589c..004c67b 100644 --- a/src/Cipher/python_DES.py +++ b/src/Cipher/python_DES.py @@ -1,21 +1,14 @@ -import blockcipher +from blockcipher import * import pyDes -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object wrapper for pure python implementation pyDes.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the key - mode = python_DES.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = python_DES.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption IV = IV as a raw string -> needed for CBC, CFB and OFB mode @@ -39,11 +32,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return python_DES(key,mode,IV,counter) -class python_DES(blockcipher.BlockCipher): +class python_DES(BlockCipher): def __init__(self,key,mode,IV,counter): self.cipher = pyDes.des(key) self.blocksize = self.cipher.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_DES3.py b/src/Cipher/python_DES3.py index 1519858..1f38bbe 100644 --- a/src/Cipher/python_DES3.py +++ b/src/Cipher/python_DES3.py @@ -1,23 +1,16 @@ -import blockcipher +from blockcipher import * import pyDes -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a DES-EDE3 or DES-EDE2 cipher object wrapper for pure python 3DES implementation pyDes.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the 2/3 keys - DES-EDE2: supply 2 keys as 1 single concatenated 16byte key= key1|key2 - DES-EDE3: supply 3 keys as 1 single concatenated 24byte key= key1|key2|key3 - mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB IV = IV as a raw string -> only needed for CBC mode counter = counter object (CryptoPlus.Util.util.Counter) @@ -61,12 +54,12 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): '32e7758f3f614dbf'""" return python_DES3(key,mode,IV,counter) -class python_DES3(blockcipher.BlockCipher): +class python_DES3(BlockCipher): def __init__(self,key,mode,IV,counter): assert len(key) in (16,24) self.cipher = pyDes.triple_des(key) self.blocksize = self.cipher.block_size - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_PRESENT.py b/src/Cipher/python_PRESENT.py index a077662..b400d6f 100644 --- a/src/Cipher/python_PRESENT.py +++ b/src/Cipher/python_PRESENT.py @@ -1,24 +1,15 @@ -import blockcipher +from blockcipher import * from pypresent import Present -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -#Present blocksize is 8bytes, XTS requires 16bytes -#MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,rounds=32): +def new(key,mode=MODE_ECB,IV=None,counter=None,rounds=32): """Create a new cipher object Wrapper for pure python implementation rijndael.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,rounds=32): + new(key,mode=MODE_ECB,IV=None,counter=None,rounds=32): key = raw string containing the key, AES-128..256 will be selected according to the key length -> when using XTS mode: the key should be a tuple containing the 2 keys needed - mode = python_PRESENT.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = python_PRESENT.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption IV = IV as a raw string -> needed for CBC, CFB and OFB mode @@ -77,11 +68,11 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,rounds=32): """ return python_PRESENT(key,mode,IV,counter,rounds) -class python_PRESENT(blockcipher.BlockCipher): +class python_PRESENT(BlockCipher): def __init__(self,key,mode,IV,counter,rounds): self.cipher = Present(key,rounds) self.blocksize = 8 - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_Rijndael.py b/src/Cipher/python_Rijndael.py index b78c88b..49f625c 100644 --- a/src/Cipher/python_Rijndael.py +++ b/src/Cipher/python_Rijndael.py @@ -1,29 +1,15 @@ -# wrapper for rijndael.py. rijndael.py can be found here: -# http://bitconjurer.org/rijndael.py -# other possible python AES implementations: -# http://psionicist.online.fr/code/rijndael.py.txt -# http://jclement.ca/software/pyrijndael/ - -import blockcipher +from blockcipher import * from rijndael import rijndael -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,blocksize=None): +def new(key,mode=MODE_ECB,IV=None,counter=None,blocksize=None): """Create a new cipher object Wrapper for pure python implementation rijndael.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,blocksize=None): + new(key,mode=MODE_ECB,IV=None,counter=None,blocksize=None): key = raw string containing the key -> supported key size are 16, 24 and 32 bytes - mode = python_Rijndael.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = python_Rijndael.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption IV = IV as a raw string -> needed for CBC, CFB and OFB mode @@ -93,7 +79,7 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None,blocksize=None): """ return python_Rijndael(key,mode,IV,counter,blocksize) -class python_Rijndael(blockcipher.BlockCipher): +class python_Rijndael(BlockCipher): def __init__(self,key,mode,IV,counter,blocksize): #Limitations on key and block size: # the wrapped rijndael implementation doesn't support all 32bit multiples between 128 and 256bits @@ -114,7 +100,7 @@ class python_Rijndael(blockcipher.BlockCipher): assert blocksize in (16, 24, 32) self.cipher = rijndael(key, blocksize) self.blocksize = blocksize - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_Serpent.py b/src/Cipher/python_Serpent.py index 285e648..cab3786 100644 --- a/src/Cipher/python_Serpent.py +++ b/src/Cipher/python_Serpent.py @@ -1,23 +1,15 @@ -import blockcipher +from blockcipher import * from pyserpent import Serpent -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): #key length can be any multiple of 4 bytes between 0 and 32 bytes (=256bits) """Create a new cipher object Wrapper for pure python implementation pyserpent.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the key - mode = python_Serpent.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = python_Serpent.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption IV = IV as a raw string -> needed for CBC, CFB and OFB mode @@ -61,7 +53,7 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return python_Serpent(key,mode,IV,counter) -class python_Serpent(blockcipher.BlockCipher): +class python_Serpent(BlockCipher): def __init__(self,key,mode,IV,counter): if mode == MODE_XTS: assert type(key) is tuple @@ -70,7 +62,7 @@ class python_Serpent(blockcipher.BlockCipher): else: self.cipher = Serpent(key) self.blocksize = self.cipher.get_block_size() - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest diff --git a/src/Cipher/python_Twofish.py b/src/Cipher/python_Twofish.py index 27e46ad..d6bc5ed 100644 --- a/src/Cipher/python_Twofish.py +++ b/src/Cipher/python_Twofish.py @@ -1,26 +1,14 @@ -# blocksize = 128 bits -# key = up to 256 bits -# algo supports 16, 24 and 32*8? maakt er een 32 van telkens? - -import blockcipher +from blockcipher import * from pytwofish import Twofish -MODE_ECB = 1 -MODE_CBC = 2 -MODE_CFB = 3 -MODE_OFB = 5 -MODE_CTR = 6 -MODE_XTS = 7 -MODE_CMAC = 8 - -def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): +def new(key,mode=MODE_ECB,IV=None,counter=None): """Create a new cipher object Wrapper for pure python implementation pytwofish.py - new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + new(key,mode=MODE_ECB,IV=None,counter=None): key = raw string containing the key - mode = python_Twofish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC + mode = python_Twofish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption IV = IV as a raw string -> needed for CBC, CFB and OFB mode @@ -43,7 +31,7 @@ def new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): """ return python_Twofish(key,mode,IV,counter) -class python_Twofish(blockcipher.BlockCipher): +class python_Twofish(BlockCipher): def __init__(self,key,mode,IV,counter): if mode == MODE_XTS: assert type(key) is tuple @@ -52,7 +40,7 @@ class python_Twofish(blockcipher.BlockCipher): else: self.cipher = Twofish(key) self.blocksize = self.cipher.get_block_size() - blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + BlockCipher.__init__(self,key,mode,IV,counter) def _test(): import doctest -- 2.11.4.GIT