From 861718f5043c2275f2f6405cbead3c1dbfe8b22b Mon Sep 17 00:00:00 2001 From: tiftof Date: Tue, 30 Sep 2008 18:27:37 +0000 Subject: [PATCH] Added ARC2, CAST, XOR - XOR is just a plain import from Crypto.Cipher as it is a stream cipher - ARC2, CAST have been made available via a wrapper git-svn-id: svn+ssh://devel.yobi.be/home/svn/CryptoPlus/trunk@30 49921240-e1b6-4d8d-af3b-5ae6c3d9e7c1 --- src/Cipher/ARC2.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Cipher/CAST.py | 50 +++++++++++++++++++++++++++++++++++++++++++ src/Cipher/__init__.py | 4 ++-- test/test_doctests.py | 4 ++-- 4 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/Cipher/ARC2.py create mode 100644 src/Cipher/CAST.py diff --git a/src/Cipher/ARC2.py b/src/Cipher/ARC2.py new file mode 100644 index 0000000..79f5ad5 --- /dev/null +++ b/src/Cipher/ARC2.py @@ -0,0 +1,58 @@ +import blockcipher +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): + """Create a new cipher object + + ARC2 using pycrypto for algo en pycryptoplus for ciphermode + + new(key,mode=blockcipher.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 + IV = IV as a raw string + -> only needed for CBC mode + counter = counter object (Cipher/util.py:Counter) + -> only needed for CTR mode + effective_keylen = how much bits to effectively use from the supplied key + -> will only be used when the pycrypto version on your system is >2.0.1 + + http://www.ietf.org/rfc/rfc2268.txt + Doctest will fail when using pycrypto 2.0.1 and older + ------------------------------------ + >>> from CryptoPlus.Cipher import ARC2 + >>> key = "0000000000000000".decode('hex') + >>> plaintext = "0000000000000000".decode('hex') + >>> ek = 63 + >>> cipher = ARC2.new(key,ARC2.MODE_ECB,effective_keylen=ek) + >>> cipher.encrypt(plaintext) + 'ebb773f993278eff' + """ + return ARC2(key,mode,IV,counter,effective_keylen) + +class ARC2(blockcipher.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"): + self.cipher = Crypto.Cipher.ARC2.new(key) + else: + self.cipher = Crypto.Cipher.ARC2.new(key,effective_keylen) + self.blocksize = Crypto.Cipher.ARC2.block_size + blockcipher.BlockCipher.__init__(self,key,mode,IV,counter) + +def _test(): + import doctest + doctest.testmod() + +if __name__ == "__main__": + _test() diff --git a/src/Cipher/CAST.py b/src/Cipher/CAST.py new file mode 100644 index 0000000..56d0e4d --- /dev/null +++ b/src/Cipher/CAST.py @@ -0,0 +1,50 @@ +# variable key size +# blocksize = 8bytes +import blockcipher +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): + """Create a new cipher object + + CAST using pycrypto for algo en pycryptoplus for ciphermode + + new(key,mode=blockcipher.MODE_ECB,IV=None,counter=None): + key = raw string containing the keys + mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC + IV = IV as a raw string + -> only needed for CBC mode + counter = counter object (Cipher/util.py:Counter) + -> only needed for CTR mode + + http://www.rfc-editor.org/rfc/rfc2144.txt + ----------------------------------------- + >>> from CryptoPlus.Cipher import CAST + >>> key = "0123456712345678234567893456789A".decode('hex') + >>> plaintext = "0123456789ABCDEF".decode('hex') + >>> cipher = CAST.new(key,CAST.MODE_ECB,) + >>> cipher.encrypt(plaintext).encode('hex') + '238b4fe5847e44b2' + """ + return CAST(key,mode,IV,counter) + +class CAST(blockcipher.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) + +def _test(): + import doctest + doctest.testmod() + +if __name__ == "__main__": + _test() diff --git a/src/Cipher/__init__.py b/src/Cipher/__init__.py index 3de6d02..f89b41d 100644 --- a/src/Cipher/__init__.py +++ b/src/Cipher/__init__.py @@ -1,2 +1,2 @@ -from Crypto.Cipher import ARC4 -__all__ = ["AES","python_AES","python_DES","python_DES3","DES","DES3","Blowfish","python_Blowfish","python_Twofish","python_Serpent","python_Rijndael","ARC4"] +from Crypto.Cipher import ARC4, XOR +__all__ = ["AES","python_AES","python_DES","python_DES3","DES","DES3","Blowfish","python_Blowfish","python_Twofish","python_Serpent","python_Rijndael","ARC4","ARC2","CAST","XOR"] diff --git a/test/test_doctests.py b/test/test_doctests.py index 9b52ef4..87e26a5 100644 --- a/test/test_doctests.py +++ b/test/test_doctests.py @@ -3,11 +3,11 @@ import unittest import doctest #import CryptoPlus.Cipher.python_AES -from CryptoPlus.Cipher import python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent, python_Rijndael +from CryptoPlus.Cipher import python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent, python_Rijndael, CAST, ARC2 suite = unittest.TestSuite() #for mod in (CryptoPlus.Cipher.python_AES,CryptoPlus.Cipher.python_AES): -for mod in python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent, python_Rijndael: +for mod in python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent, python_Rijndael, CAST, ARC2: suite.addTest(doctest.DocTestSuite(mod)) runner = unittest.TextTestRunner() runner.run(suite) -- 2.11.4.GIT