├── .gitignore ├── LICENSE ├── README ├── setup.py ├── src └── CryptoPlus │ ├── Cipher │ ├── AES.py │ ├── ARC2.py │ ├── Blowfish.py │ ├── CAST.py │ ├── DES.py │ ├── DES3.py │ ├── IDEA.py │ ├── RC5.py │ ├── __init__.py │ ├── blockcipher.py │ ├── pyDes.py │ ├── pyblowfish.py │ ├── pypresent.py │ ├── pyserpent.py │ ├── python_AES.py │ ├── python_Blowfish.py │ ├── python_DES.py │ ├── python_DES3.py │ ├── python_PRESENT.py │ ├── python_Rijndael.py │ ├── python_Serpent.py │ ├── python_Twofish.py │ ├── pytwofish.py │ └── rijndael.py │ ├── Hash │ ├── RIPEMD.py │ ├── __init__.py │ ├── pymd5.py │ ├── pypbkdf2.py │ ├── pyradiogatun.py │ ├── pysha.py │ ├── pysha224.py │ ├── pysha256.py │ ├── pysha384.py │ ├── pysha512.py │ ├── python_MD5.py │ ├── python_PBKDF2.py │ ├── python_RadioGatun.py │ ├── python_SHA.py │ ├── python_SHA224.py │ ├── python_SHA256.py │ ├── python_SHA384.py │ ├── python_SHA512.py │ ├── python_whirlpool.py │ └── pywhirlpool.py │ ├── Protocol.py │ ├── PublicKey.py │ ├── Random │ ├── Fortuna.py │ ├── OSRNG.py │ └── __init__.py │ ├── SelfTest │ ├── ..new │ ├── Cipher │ │ ├── __init__.py │ │ ├── common.py │ │ ├── test_AES.py │ │ ├── test_ARC2.py │ │ ├── test_ARC4.py │ │ ├── test_Blowfish.py │ │ ├── test_CAST.py │ │ ├── test_DES.py │ │ ├── test_DES3.py │ │ ├── test_IDEA.py │ │ ├── test_RC5.py │ │ ├── test_XOR.py │ │ └── test_python_AES.py │ ├── Hash │ │ ├── __init__.py │ │ ├── common.py │ │ ├── test_HMAC.py │ │ ├── test_MD2.py │ │ ├── test_MD4.py │ │ ├── test_MD5.py │ │ ├── test_RIPEMD.py │ │ ├── test_SHA.py │ │ └── test_SHA256.py │ ├── PublicKey │ │ ├── __init__.py │ │ └── test_RSA.py │ ├── Random │ │ ├── Fortuna.new │ │ ├── Fortuna │ │ │ ├── __init__.py │ │ │ ├── test_FortunaAccumulator.py │ │ │ ├── test_FortunaGenerator.py │ │ │ └── test_SHAd256.py │ │ ├── OSRNG.new │ │ ├── OSRNG │ │ │ ├── __init__.py │ │ │ ├── test_fallback.py │ │ │ ├── test_generic.py │ │ │ ├── test_nt.py │ │ │ ├── test_posix.py │ │ │ └── test_winrandom.py │ │ ├── __init__.py │ │ ├── test_random.py │ │ └── test_rpoolcompat.py │ ├── Util │ │ ├── __init__.py │ │ ├── test_number.py │ │ └── test_winrandom.py │ ├── __init__.py │ └── st_common.py │ ├── Util │ ├── RFC1751.py │ ├── __init__.py │ ├── number.py │ ├── padding.py │ ├── python_compat.py │ ├── randpool.py │ └── util.py │ ├── __init__.py │ └── testvectors.py └── test ├── test.py └── test_doctests.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *~ 3 | dist/ 4 | build/ 5 | MANIFEST 6 | CryptoPlus.egg-info/ 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CryptoPlus original code is distributed under the MIT License: 2 | 3 | # ============================================================================= 4 | # Copyright (c) 2008 Christophe Oosterlynck (christophe.oosterlynck_AT_gmail.com) 5 | # Philippe Teuwen (philippe.teuwen_AT_nxp.com) 6 | # 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy 8 | # of this software and associated documentation files (the "Software"), to deal 9 | # in the Software without restriction, including without limitation the rights 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | # copies of the Software, and to permit persons to whom the Software is 12 | # furnished to do so, subject to the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be included in 15 | # all copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | # THE SOFTWARE. 24 | # ============================================================================= 25 | 26 | Third party implementations are distributed under their respective license 27 | as specified in their source file(s): 28 | 29 | Ciphers: 30 | pyblowfish.py, pyDes.py, pyserpent.py, pytwofish.py, rijndael.py 31 | - pyblowfish.py is being redistributed under the Artistic License 32 | 33 | Hash functions: 34 | pypbkdf2.py, pywhirlpool.py 35 | using pypy license: pysha.py, pysha224.py, pysha256.py, pysha384.py, 36 | pysha512.py, pymd5.py 37 | 38 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | PYCRYPTOPLUS 2 | ============= 3 | 4 | TABLE OF CONTENTS 5 | ================== 6 | 7 | 1. WHAT IS CRYPTOPLUS 8 | 2. INSTALLING 9 | 3. GETTING STARTED 10 | 4. LIMITATIONS 11 | 12 | 1. WHAT IS CRYPTOPLUS 13 | ====================== 14 | 15 | PyCryptoPlus is an extension to the Python Crypto module (www.pycrypto.org). 16 | PyCryptoPlus provides same ciphers as included in pycrypto but also new ones, 17 | all being written 100% in Python. Some additional chaining modes have been 18 | added, also in pure Python, while the ones already available in pycrypto are 19 | provided in pure python in this package. 20 | The reasoning is that Python code has the advantage to be more readable and 21 | so easier to be adapted to your needs or experiments. 22 | All other functions of pycrypto are still available via the interface 23 | of CryptoPlus. The new cipher implementations can be accessed via 24 | CryptoPlus.Cipher.python_* while the original ones from pycrypto are 25 | still available under their original name via CryptoPlus.Cipher.*. 26 | When using the original ciphers, the original pycrypto code written in C is 27 | used but the chaining modes being used are the new ones in Python. 28 | 29 | New functions: 30 | Ciphers: 31 | Rijndael 32 | Serpent 33 | Twofish 34 | Chaining Modes: 35 | XTS 36 | CMAC 37 | 38 | Note: for the cipher algorithms, code has been reused from third parties. 39 | Corresponding copyright notices are available in their source code. 40 | 41 | 2. INSTALLING 42 | ============== 43 | 44 | required packages before installing: 45 | - python-setuptools 46 | - python-pkg-resources 47 | 48 | python setup.py install 49 | 50 | 3. GETTING STARTED 51 | =================== 52 | 53 | Same API from PyCrypto can be used. See: 54 | http://www.dlitz.net/software/pycrypto/doc/ 55 | 56 | Biggest changes are the addition of some chain modes and block ciphers. 57 | A lot of examples are provided as docstrings. 58 | Have a look at them in '../CryptoPlus/Cipher/*.py' or via an interactive 59 | python session by using 'CryptoPlus.Cipher.python_AES.new?'. 60 | Once a cipher object is constructed with 61 | 'cipher = CryptoPlus.Cipher.python_AES.new(...)' 62 | you can get more info about encrypting and decrypting by reading 63 | the apprioprate docstring ('cipher.encrypt?','cipher.decrypt?'). 64 | 65 | Some test functions are provided in the docstrings and in the 'test' 66 | folder. Run all the doctests in the new Cipher function by using 67 | the '../test/test_doctest.py' script. '../test/test.py' provides 68 | some test function for the testvectors available from the module via 69 | 'CryptoPlus.Cipher.testvectors'. Have a look at the test.py sourcecode 70 | to have an idea of how to use those test vectors. 71 | 72 | 4. LIMITATIONS 73 | =============== 74 | 75 | CMAC can only be used with ciphers of 64 or 128 bits blocksizes 76 | XTS can only be used with ciphers of 128 bits blocksize 77 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from setuptools import setup, find_packages 4 | 5 | setup(name='CryptoPlus', 6 | version='1.0', 7 | description='PyCrypto Cipher extension', 8 | author='Christophe Oosterlynck', 9 | author_email='tiftof@gmail.com', 10 | packages = find_packages('src'), 11 | install_requires = ['pycrypto'], 12 | package_dir={'': 'src'} 13 | ) 14 | 15 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/ARC2.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import Crypto.Cipher.ARC2 3 | import Crypto 4 | from pkg_resources import parse_version 5 | 6 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None,effective_keylen=None): 7 | """Create a new cipher object 8 | 9 | ARC2 using pycrypto for algo and pycryptoplus for ciphermode 10 | 11 | key = raw string containing the keys 12 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 13 | IV = IV as a raw string, default is "all zero" IV 14 | -> only needed for CBC mode 15 | counter = counter object (CryptoPlus.Util.util.Counter) 16 | -> only needed for CTR mode 17 | segment_size = amount of bits to use from the keystream in each chain part 18 | -> supported values: multiple of 8 between 8 and the blocksize 19 | of the cipher (only per byte access possible), default is 8 20 | -> only needed for CFB mode 21 | effective_keylen = how much bits to effectively use from the supplied key 22 | -> will only be used when the pycrypto version on your system is >2.0.1 23 | 24 | EXAMPLES: 25 | ********** 26 | IMPORTING: 27 | ----------- 28 | >>> from CryptoPlus.Cipher import ARC2 29 | 30 | http://www.ietf.org/rfc/rfc2268.txt 31 | Doctest will fail when using pycrypto 2.0.1 and older 32 | ------------------------------------ 33 | >>> key = "0000000000000000".decode('hex') 34 | >>> plaintext = "0000000000000000".decode('hex') 35 | >>> ek = 63 36 | >>> cipher = ARC2.new(key,ARC2.MODE_ECB,effective_keylen=ek) 37 | >>> cipher.encrypt(plaintext).encode('hex') 38 | 'ebb773f993278eff' 39 | """ 40 | return ARC2(key,mode,IV,counter,effective_keylen,segment_size) 41 | 42 | class ARC2(BlockCipher): 43 | def __init__(self,key,mode,IV,counter,effective_keylen,segment_size): 44 | # pycrypto versions newer than 2.0.1 will have support for "effective_keylen" 45 | if parse_version(Crypto.__version__) <= parse_version("2.0.1"): 46 | cipher_module = Crypto.Cipher.ARC2.new 47 | args = {} 48 | else: 49 | cipher_module = Crypto.Cipher.ARC2.new 50 | args = {'effective_keylen':effective_keylen} 51 | self.blocksize = 8 52 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args) 53 | 54 | def _test(): 55 | import doctest 56 | doctest.testmod() 57 | 58 | if __name__ == "__main__": 59 | _test() 60 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/Blowfish.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import Crypto.Cipher.Blowfish 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | Blowfish using pycrypto for algo and pycryptoplus for ciphermode 8 | 9 | key = raw string containing the key 10 | mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 11 | IV = IV as a raw string, default is "all zero" IV 12 | -> only needed for CBC mode 13 | counter = counter object (CryptoPlus.Util.util.Counter) 14 | -> only needed for CTR mode 15 | segment_size = amount of bits to use from the keystream in each chain part 16 | -> supported values: multiple of 8 between 8 and the blocksize 17 | of the cipher (only per byte access possible), default is 8 18 | -> only needed for CFB mode 19 | 20 | EXAMPLES: 21 | ********** 22 | IMPORTING: 23 | ----------- 24 | >>> from CryptoPlus.Cipher import Blowfish 25 | 26 | ECB EXAMPLE: http://www.schneier.com/code/vectors.txt 27 | ------------- 28 | >>> cipher = Blowfish.new(('0131D9619DC1376E').decode('hex')) 29 | >>> ( cipher.encrypt(('5CD54CA83DEF57DA').decode('hex')) ).encode('hex') 30 | 'b1b8cc0b250f09a0' 31 | >>> ( cipher.decrypt((_).decode('hex')) ).encode('hex') 32 | '5cd54ca83def57da' 33 | 34 | CBC, CFB, OFB EXAMPLE: http://www.schneier.com/code/vectors.txt 35 | ---------------------- 36 | >>> key = ('0123456789ABCDEFF0E1D2C3B4A59687').decode('hex') 37 | >>> IV = ('FEDCBA9876543210').decode('hex') 38 | >>> plaintext = ('37363534333231204E6F77206973207468652074696D6520').decode('hex') 39 | >>> cipher = Blowfish.new(key,Blowfish.MODE_CBC,IV) 40 | >>> ciphertext = cipher.encrypt(plaintext) 41 | >>> (ciphertext).encode('hex').upper() 42 | '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9' 43 | 44 | 45 | >>> key = '0123456789ABCDEFF0E1D2C3B4A59687'.decode('hex') 46 | >>> iv = 'FEDCBA9876543210'.decode('hex') 47 | >>> plaintext = '37363534333231204E6F77206973207468652074696D6520666F722000'.decode('hex') 48 | 49 | >>> cipher = Blowfish.new(key,Blowfish.MODE_CBC,iv) 50 | >>> ciphertext = cipher.encrypt(plaintext) 51 | >>> (ciphertext).encode('hex').upper() 52 | '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9' 53 | 54 | >>> cipher = Blowfish.new(key,Blowfish.MODE_CFB,iv,segment_size=64) 55 | >>> ciphertext = cipher.encrypt(plaintext) 56 | >>> (ciphertext).encode('hex').upper() 57 | 'E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3' 58 | 59 | >>> cipher = Blowfish.new(key,Blowfish.MODE_OFB,iv) 60 | >>> ciphertext = cipher.encrypt(plaintext) 61 | >>> (ciphertext).encode('hex').upper() 62 | 'E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA' 63 | """ 64 | return Blowfish(key,mode,IV,counter,segment_size) 65 | 66 | class Blowfish(BlockCipher): 67 | def __init__(self,key,mode,IV,counter,segment_size): 68 | cipher_module = Crypto.Cipher.Blowfish.new 69 | self.blocksize = 8 70 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 71 | 72 | def _test(): 73 | import doctest 74 | doctest.testmod() 75 | 76 | if __name__ == "__main__": 77 | _test() 78 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/CAST.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import Crypto.Cipher.CAST 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | CAST using pycrypto for algo and pycryptoplus for ciphermode 8 | 9 | key = raw string containing the keys 10 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 11 | IV = IV as a raw string, default is "all zero" IV 12 | -> only needed for CBC mode 13 | counter = counter object (CryptoPlus.Util.util.Counter) 14 | -> only needed for CTR mode 15 | segment_size = amount of bits to use from the keystream in each chain part 16 | -> supported values: multiple of 8 between 8 and the blocksize 17 | of the cipher (only per byte access possible), default is 8 18 | -> only needed for CFB mode 19 | 20 | EXAMPLES: 21 | ********** 22 | IMPORTING: 23 | ----------- 24 | >>> from CryptoPlus.Cipher import CAST 25 | 26 | ECB example: http://www.rfc-editor.org/rfc/rfc2144.txt 27 | ------------- 28 | 128 bit key 29 | 30 | >>> key = "0123456712345678234567893456789A".decode('hex') 31 | >>> plaintext = "0123456789ABCDEF".decode('hex') 32 | >>> cipher = CAST.new(key,CAST.MODE_ECB,) 33 | >>> cipher.encrypt(plaintext).encode('hex') 34 | '238b4fe5847e44b2' 35 | 36 | 40 bit key 37 | >>> from CryptoPlus.Cipher import CAST 38 | >>> key = "0123456712".decode('hex') 39 | >>> plaintext = "0123456789ABCDEF".decode('hex') 40 | >>> cipher = CAST.new(key,CAST.MODE_ECB,) 41 | >>> cipher.encrypt(plaintext).encode('hex').upper() 42 | '7AC816D16E9B302E' 43 | """ 44 | return CAST(key,mode,IV,counter,segment_size) 45 | 46 | class CAST(BlockCipher): 47 | def __init__(self,key,mode,IV,counter,segment_size): 48 | cipher_module = Crypto.Cipher.CAST.new 49 | self.blocksize = 8 50 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 51 | 52 | def _test(): 53 | import doctest 54 | doctest.testmod() 55 | 56 | if __name__ == "__main__": 57 | _test() 58 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/DES.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import Crypto.Cipher.DES 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | DES using pycrypto for algo and pycryptoplus for ciphermode 8 | 9 | key = raw string containing the keys 10 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 11 | IV = IV as a raw string, default is "all zero" IV 12 | -> only needed for CBC mode 13 | counter = counter object (CryptoPlus.Util.util.Counter) 14 | -> only needed for CTR mode 15 | segment_size = amount of bits to use from the keystream in each chain part 16 | -> supported values: multiple of 8 between 8 and the blocksize 17 | of the cipher (only per byte access possible), default is 8 18 | -> only needed for CFB mode 19 | 20 | EXAMPLES: 21 | ********** 22 | IMPORTING: 23 | ----------- 24 | >>> from CryptoPlus.Cipher import DES 25 | 26 | EXAMPLE (test vectors from NESSIE): 27 | ----------------------------------- 28 | 29 | >>> cipher = DES.new(('7CA110454A1A6E57').decode('hex')) 30 | >>> ciphertext = cipher.encrypt(('01A1D6D039776742').decode('hex')) 31 | >>> (ciphertext).encode('hex') 32 | '690f5b0d9a26939b' 33 | >>> plaintext = cipher.decrypt(ciphertext) 34 | >>> (plaintext).encode('hex') 35 | '01a1d6d039776742' 36 | 37 | """ 38 | return DES(key,mode,IV,counter,segment_size) 39 | 40 | class DES(BlockCipher): 41 | def __init__(self,key,mode,IV,counter,segment_size): 42 | cipher_module = Crypto.Cipher.DES.new 43 | self.blocksize = 8 44 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 45 | 46 | def _test(): 47 | import doctest 48 | doctest.testmod() 49 | 50 | if __name__ == "__main__": 51 | _test() 52 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/DES3.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import Crypto.Cipher.DES3 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | DES using pycrypto for algo and pycryptoplus for ciphermode 8 | 9 | key = raw string containing the 2/3 keys 10 | - DES-EDE2: supply 2 keys as 1 single concatenated 16byte key= key1|key2 11 | - DES-EDE3: supply 3 keys as 1 single concatenated 24byte key= key1|key2|key3 12 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 13 | IV = IV as a raw string, default is "all zero" IV 14 | -> only needed for CBC mode 15 | counter = counter object (CryptoPlus.Util.util.Counter) 16 | -> only needed for CTR mode 17 | segment_size = amount of bits to use from the keystream in each chain part 18 | -> supported values: multiple of 8 between 8 and the blocksize 19 | of the cipher (only per byte access possible), default is 8 20 | -> only needed for CFB mode 21 | 22 | EXAMPLES: 23 | ********** 24 | IMPORTING: 25 | ----------- 26 | >>> from CryptoPlus.Cipher import DES3 27 | 28 | CBC TDES-EDE3 EXAMPLE: (using test vectors from http://csrc.nist.gov/groups/STM/cavp/documents/des/DESMMT.pdf) 29 | ------------ 30 | >>> key = ('37ae5ebf46dff2dc0754b94f31cbb3855e7fd36dc870bfae').decode('hex') 31 | >>> IV = ('3d1de3cc132e3b65').decode('hex') 32 | >>> cipher = DES3.new(key, DES3.MODE_CBC, IV) 33 | >>> ciphertext = cipher.encrypt(('84401f78fe6c10876d8ea23094ea5309').decode('hex')) 34 | >>> (ciphertext).encode('hex') 35 | '7b1f7c7e3b1c948ebd04a75ffba7d2f5' 36 | >>> decipher = DES3.new(key, DES3.MODE_CBC, IV) 37 | >>> plaintext = decipher.decrypt(ciphertext) 38 | >>> (plaintext).encode('hex') 39 | '84401f78fe6c10876d8ea23094ea5309' 40 | 41 | CMAC TDES-EDE3 EXAMPLE: (http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf) 42 | ------------- 43 | >>> key = '8aa83bf8cbda10620bc1bf19fbb6cd58bc313d4a371ca8b5'.decode('hex') 44 | >>> plaintext = '6bc1bee22e409f96e93d7e117393172aae2d8a57'.decode('hex') 45 | >>> cipher = DES3.new(key, DES3.MODE_CMAC) 46 | >>> cipher.encrypt(plaintext).encode('hex') 47 | '743ddbe0ce2dc2ed' 48 | 49 | CMAC TDES-EDE2 EXAMPLE: 50 | ----------------------- 51 | testvector: http://csrc.nist.gov/groups/STM/cavp/documents/mac/cmactestvectors.zip 52 | 53 | >>> key1 = "5104f2c76180c1d3".decode('hex') 54 | >>> key2 = "b9df763e31ada716".decode('hex') 55 | >>> key = key1 + key2 56 | >>> plaintext = 'a6866be2fa6678f264a19c4474968e3f4eec24f5086d'.decode('hex') 57 | >>> cipher = DES3.new(key, DES3.MODE_CMAC) 58 | >>> cipher.encrypt(plaintext).encode('hex') 59 | '32e7758f3f614dbf' 60 | """ 61 | return DES3(key,mode,IV,counter,segment_size) 62 | 63 | class DES3(BlockCipher): 64 | def __init__(self,key,mode,IV,counter,segment_size): 65 | cipher_module = Crypto.Cipher.DES3.new 66 | self.blocksize = 8 67 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 68 | 69 | def _test(): 70 | import doctest 71 | doctest.testmod() 72 | 73 | if __name__ == "__main__": 74 | _test() 75 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/IDEA.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | try: 3 | import Crypto.Cipher.IDEA 4 | except ImportError: 5 | print "Crypto.Cipher.IDEA isn't available. You're probably using the Debian pycrypto version. Install the original pycrypto for IDEA." 6 | raise 7 | 8 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 9 | """Create a new cipher object 10 | 11 | IDEA using pycrypto for algo and pycryptoplus for ciphermode 12 | 13 | key = raw string containing the keys 14 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 15 | IV = IV as a raw string, default is "all zero" IV 16 | -> only needed for CBC mode 17 | counter = counter object (CryptoPlus.Util.util.Counter) 18 | -> only needed for CTR mode 19 | segment_size = amount of bits to use from the keystream in each chain part 20 | -> supported values: multiple of 8 between 8 and the blocksize 21 | of the cipher (only per byte access possible), default is 8 22 | -> only needed for CFB mode 23 | 24 | EXAMPLES: 25 | ********** 26 | IMPORTING: 27 | ----------- 28 | >>> from CryptoPlus.Cipher import IDEA 29 | 30 | https://www.cosic.esat.kuleuven.be/nessie/testvectors/ 31 | ----------------------------------------- 32 | >>> from CryptoPlus.Cipher import IDEA 33 | >>> key = "2BD6459F82C5B300952C49104881FF48".decode('hex') 34 | >>> plaintext = "F129A6601EF62A47".decode('hex') 35 | >>> cipher = IDEA.new(key,IDEA.MODE_ECB,) 36 | >>> cipher.encrypt(plaintext).encode('hex').upper() 37 | 'EA024714AD5C4D84' 38 | """ 39 | return IDEA(key,mode,IV,counter,segment_size) 40 | 41 | class IDEA(BlockCipher): 42 | def __init__(self,key,mode,IV,counter,segment_size): 43 | cipher_module = Crypto.Cipher.IDEA.new 44 | self.blocksize = 8 45 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 46 | 47 | def _test(): 48 | import doctest 49 | doctest.testmod() 50 | 51 | if __name__ == "__main__": 52 | _test() 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/RC5.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | try: 3 | import Crypto.Cipher.RC5 4 | except ImportError: 5 | print "Crypto.Cipher.RC5 isn't available. You're probably using the Debian pycrypto version. Install the original pycrypto for RC5." 6 | raise 7 | 8 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None,rounds=12,word_size=32): 9 | """Create a new cipher object 10 | 11 | RC5 using pycrypto for algo and pycryptoplus for ciphermode 12 | 13 | key = raw string containing the keys 14 | multiple of 8 bits between 0 <-> 2040 bits 15 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 16 | IV = IV as a raw string, default is "all zero" IV 17 | -> only needed for CBC mode 18 | counter = counter object (CryptoPlus.Util.util.Counter) 19 | -> only needed for CTR mode 20 | segment_size = amount of bits to use from the keystream in each chain part 21 | -> supported values: multiple of 8 between 8 and the blocksize 22 | of the cipher (only per byte access possible), default is 8 23 | -> only needed for CFB mode 24 | rounds = amount of rounds, default = 12 25 | minimum 12 and multiple of 2 26 | word_size = RC5 word size (bits), supported = 16 and 32, default = 32 27 | RC5 encrypts blocks of size 2*word_size 28 | 29 | EXAMPLES: 30 | ********** 31 | IMPORTING: 32 | ----------- 33 | >>> from CryptoPlus.Cipher import RC5 34 | 35 | https://www.cosic.esat.kuleuven.be/nessie/testvectors/ 36 | ----------------------------------------- 37 | >>> key = "00000000000000000000000000000000".decode('hex') 38 | >>> plaintext = "0000000000000000".decode('hex') 39 | >>> rounds = 12 40 | >>> cipher = RC5.new(key,RC5.MODE_ECB,rounds=rounds) 41 | >>> cipher.encrypt(plaintext).encode('hex') 42 | '21a5dbee154b8f6d' 43 | """ 44 | return RC5(key,mode,IV,counter,rounds,word_size,segment_size) 45 | 46 | class RC5(BlockCipher): 47 | def __init__(self,key,mode,IV,counter,rounds,word_size,segment_size): 48 | cipher_module = Crypto.Cipher.RC5.new 49 | args = {'rounds':rounds,'word_size':word_size} 50 | self.blocksize = (2*word_size)/8 51 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args) 52 | 53 | def _test(): 54 | import doctest 55 | doctest.testmod() 56 | 57 | if __name__ == "__main__": 58 | _test() 59 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/__init__.py: -------------------------------------------------------------------------------- 1 | from Crypto.Cipher import ARC4, XOR 2 | 3 | __all__ = ["AES","python_AES","python_DES","python_DES3","DES","DES3","Blowfish","python_Blowfish","python_Twofish","python_Serpent","python_Rijndael","ARC4","ARC2","CAST","XOR","python_PRESENT"] 4 | 5 | try: 6 | import Crypto.Cipher.IDEA 7 | __all__.append("IDEA") 8 | __all__.append("RC5") 9 | except ImportError: 10 | pass 11 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/pypresent.py: -------------------------------------------------------------------------------- 1 | # Python PRESENT implementation 2 | # Version: 1.0 3 | # Date: 13/10/2008 4 | # 5 | # ============================================================================= 6 | # Copyright (c) 2008 Christophe Oosterlynck 7 | # & NXP ( Philippe Teuwen ) 8 | # 9 | # Permission is hereby granted, free of charge, to any person obtaining a copy 10 | # of this software and associated documentation files (the "Software"), to deal 11 | # in the Software without restriction, including without limitation the rights 12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | # copies of the Software, and to permit persons to whom the Software is 14 | # furnished to do so, subject to the following conditions: 15 | # 16 | # The above copyright notice and this permission notice shall be included in 17 | # all copies or substantial portions of the Software. 18 | # 19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | # THE SOFTWARE. 26 | # ============================================================================= 27 | 28 | """ PRESENT block cipher implementation 29 | 30 | USAGE EXAMPLE: 31 | --------------- 32 | Importing: 33 | ----------- 34 | >>> from pypresent import Present 35 | 36 | Encrypting with a 80-bit key: 37 | ------------------------------ 38 | >>> key = "00000000000000000000".decode('hex') 39 | >>> plain = "0000000000000000".decode('hex') 40 | >>> cipher = Present(key) 41 | >>> encrypted = cipher.encrypt(plain) 42 | >>> encrypted.encode('hex') 43 | '5579c1387b228445' 44 | >>> decrypted = cipher.decrypt(encrypted) 45 | >>> decrypted.encode('hex') 46 | '0000000000000000' 47 | 48 | Encrypting with a 128-bit key: 49 | ------------------------------- 50 | >>> key = "0123456789abcdef0123456789abcdef".decode('hex') 51 | >>> plain = "0123456789abcdef".decode('hex') 52 | >>> cipher = Present(key) 53 | >>> encrypted = cipher.encrypt(plain) 54 | >>> encrypted.encode('hex') 55 | '0e9d28685e671dd6' 56 | >>> decrypted = cipher.decrypt(encrypted) 57 | >>> decrypted.encode('hex') 58 | '0123456789abcdef' 59 | 60 | fully based on standard specifications: http://www.crypto.ruhr-uni-bochum.de/imperia/md/content/texte/publications/conferences/present_ches2007.pdf 61 | test vectors: http://www.crypto.ruhr-uni-bochum.de/imperia/md/content/texte/publications/conferences/slides/present_testvectors.zip 62 | """ 63 | class Present: 64 | 65 | def __init__(self,key,rounds=32): 66 | """Create a PRESENT cipher object 67 | 68 | key: the key as a 128-bit or 80-bit rawstring 69 | rounds: the number of rounds as an integer, 32 by default 70 | """ 71 | self.rounds = rounds 72 | if len(key) * 8 == 80: 73 | self.roundkeys = generateRoundkeys80(string2number(key),self.rounds) 74 | elif len(key) * 8 == 128: 75 | self.roundkeys = generateRoundkeys128(string2number(key),self.rounds) 76 | else: 77 | raise ValueError, "Key must be a 128-bit or 80-bit rawstring" 78 | 79 | def encrypt(self,block): 80 | """Encrypt 1 block (8 bytes) 81 | 82 | Input: plaintext block as raw string 83 | Output: ciphertext block as raw string 84 | """ 85 | state = string2number(block) 86 | for i in xrange (self.rounds-1): 87 | state = addRoundKey(state,self.roundkeys[i]) 88 | state = sBoxLayer(state) 89 | state = pLayer(state) 90 | cipher = addRoundKey(state,self.roundkeys[-1]) 91 | return number2string_N(cipher,8) 92 | 93 | def decrypt(self,block): 94 | """Decrypt 1 block (8 bytes) 95 | 96 | Input: ciphertext block as raw string 97 | Output: plaintext block as raw string 98 | """ 99 | state = string2number(block) 100 | for i in xrange (self.rounds-1): 101 | state = addRoundKey(state,self.roundkeys[-i-1]) 102 | state = pLayer_dec(state) 103 | state = sBoxLayer_dec(state) 104 | decipher = addRoundKey(state,self.roundkeys[0]) 105 | return number2string_N(decipher,8) 106 | 107 | def get_block_size(self): 108 | return 8 109 | 110 | # 0 1 2 3 4 5 6 7 8 9 a b c d e f 111 | Sbox= [0xc,0x5,0x6,0xb,0x9,0x0,0xa,0xd,0x3,0xe,0xf,0x8,0x4,0x7,0x1,0x2] 112 | Sbox_inv = [Sbox.index(x) for x in xrange(16)] 113 | PBox = [0,16,32,48,1,17,33,49,2,18,34,50,3,19,35,51, 114 | 4,20,36,52,5,21,37,53,6,22,38,54,7,23,39,55, 115 | 8,24,40,56,9,25,41,57,10,26,42,58,11,27,43,59, 116 | 12,28,44,60,13,29,45,61,14,30,46,62,15,31,47,63] 117 | PBox_inv = [PBox.index(x) for x in xrange(64)] 118 | 119 | def generateRoundkeys80(key,rounds): 120 | """Generate the roundkeys for a 80-bit key 121 | 122 | Input: 123 | key: the key as a 80-bit integer 124 | rounds: the number of rounds as an integer 125 | Output: list of 64-bit roundkeys as integers""" 126 | roundkeys = [] 127 | for i in xrange(1,rounds+1): # (K1 ... K32) 128 | # rawkey: used in comments to show what happens at bitlevel 129 | # rawKey[0:64] 130 | roundkeys.append(key >>16) 131 | #1. Shift 132 | #rawKey[19:len(rawKey)]+rawKey[0:19] 133 | key = ((key & (2**19-1)) << 61) + (key >> 19) 134 | #2. SBox 135 | #rawKey[76:80] = S(rawKey[76:80]) 136 | key = (Sbox[key >> 76] << 76)+(key & (2**76-1)) 137 | #3. Salt 138 | #rawKey[15:20] ^ i 139 | key ^= i << 15 140 | return roundkeys 141 | 142 | def generateRoundkeys128(key,rounds): 143 | """Generate the roundkeys for a 128-bit key 144 | 145 | Input: 146 | key: the key as a 128-bit integer 147 | rounds: the number of rounds as an integer 148 | Output: list of 64-bit roundkeys as integers""" 149 | roundkeys = [] 150 | for i in xrange(1,rounds+1): # (K1 ... K32) 151 | # rawkey: used in comments to show what happens at bitlevel 152 | roundkeys.append(key >>64) 153 | #1. Shift 154 | key = ((key & (2**67-1)) << 61) + (key >> 67) 155 | #2. SBox 156 | key = (Sbox[key >> 124] << 124)+(Sbox[(key >> 120) & 0xF] << 120)+(key & (2**120-1)) 157 | #3. Salt 158 | #rawKey[62:67] ^ i 159 | key ^= i << 62 160 | return roundkeys 161 | 162 | def addRoundKey(state,roundkey): 163 | return state ^ roundkey 164 | 165 | def sBoxLayer(state): 166 | """SBox function for encryption 167 | 168 | Input: 64-bit integer 169 | Output: 64-bit integer""" 170 | 171 | output = 0 172 | for i in xrange(16): 173 | output += Sbox[( state >> (i*4)) & 0xF] << (i*4) 174 | return output 175 | 176 | def sBoxLayer_dec(state): 177 | """Inverse SBox function for decryption 178 | 179 | Input: 64-bit integer 180 | Output: 64-bit integer""" 181 | output = 0 182 | for i in xrange(16): 183 | output += Sbox_inv[( state >> (i*4)) & 0xF] << (i*4) 184 | return output 185 | 186 | def pLayer(state): 187 | """Permutation layer for encryption 188 | 189 | Input: 64-bit integer 190 | Output: 64-bit integer""" 191 | output = 0 192 | for i in xrange(64): 193 | output += ((state >> i) & 0x01) << PBox[i] 194 | return output 195 | 196 | def pLayer_dec(state): 197 | """Permutation layer for decryption 198 | 199 | Input: 64-bit integer 200 | Output: 64-bit integer""" 201 | output = 0 202 | for i in xrange(64): 203 | output += ((state >> i) & 0x01) << PBox_inv[i] 204 | return output 205 | 206 | def string2number(i): 207 | """ Convert a string to a number 208 | 209 | Input: string (big-endian) 210 | Output: long or integer 211 | """ 212 | return int(i.encode('hex'),16) 213 | 214 | def number2string_N(i, N): 215 | """Convert a number to a string of fixed size 216 | 217 | i: long or integer 218 | N: length of string 219 | Output: string (big-endian) 220 | """ 221 | s = '%0*x' % (N*2, i) 222 | return s.decode('hex') 223 | 224 | def _test(): 225 | import doctest 226 | doctest.testmod() 227 | 228 | if __name__ == "__main__": 229 | _test() 230 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_Blowfish.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | from pyblowfish import Blowfish 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | Wrapper for pure python implementation pyblowfish.py 8 | 9 | key = raw string containing the key 10 | mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 11 | IV = IV as a raw string, default is "all zero" IV 12 | -> only needed for CBC mode 13 | counter = counter object (CryptoPlus.Util.util.Counter) 14 | -> only needed for CTR mode 15 | segment_size = amount of bits to use from the keystream in each chain part 16 | -> supported values: multiple of 8 between 8 and the blocksize 17 | of the cipher (only per byte access possible), default is 8 18 | -> only needed for CFB mode 19 | 20 | EXAMPLES: 21 | ********** 22 | IMPORTING: 23 | ----------- 24 | >>> from CryptoPlus.Cipher import python_Blowfish 25 | 26 | EXAMPLE: (http://www.schneier.com/code/vectors.txt) 27 | ---------- 28 | 29 | >>> cipher = python_Blowfish.new(('0131D9619DC1376E').decode('hex')) 30 | >>> ( cipher.encrypt(('5CD54CA83DEF57DA').decode('hex')) ).encode('hex') 31 | 'b1b8cc0b250f09a0' 32 | >>> ( cipher.decrypt((_).decode('hex')) ).encode('hex') 33 | '5cd54ca83def57da' 34 | 35 | CBC, CFB, OFB EXAMPLE: http://www.schneier.com/code/vectors.txt 36 | ---------------------- 37 | >>> key = ('0123456789ABCDEFF0E1D2C3B4A59687').decode('hex') 38 | >>> IV = ('FEDCBA9876543210').decode('hex') 39 | >>> plaintext = ('37363534333231204E6F77206973207468652074696D6520').decode('hex') 40 | >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CBC,IV) 41 | >>> ciphertext = cipher.encrypt(plaintext) 42 | >>> (ciphertext).encode('hex').upper() 43 | '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9' 44 | 45 | 46 | >>> key = '0123456789ABCDEFF0E1D2C3B4A59687'.decode('hex') 47 | >>> iv = 'FEDCBA9876543210'.decode('hex') 48 | >>> plaintext = '37363534333231204E6F77206973207468652074696D6520666F722000'.decode('hex') 49 | 50 | >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CBC,iv) 51 | >>> ciphertext = cipher.encrypt(plaintext) 52 | >>> (ciphertext).encode('hex').upper() 53 | '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9' 54 | 55 | >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CFB,iv,segment_size=64) 56 | >>> ciphertext = cipher.encrypt(plaintext) 57 | >>> (ciphertext).encode('hex').upper() 58 | 'E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3' 59 | 60 | >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_OFB,iv) 61 | >>> ciphertext = cipher.encrypt(plaintext) 62 | >>> (ciphertext).encode('hex').upper() 63 | 'E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA'""" 64 | return python_Blowfish(key,mode,IV,counter,segment_size) 65 | 66 | class python_Blowfish(BlockCipher): 67 | key_error_message = "Key should be between 8 and 56 bytes (64 <-> 448 bits)" 68 | 69 | def __init__(self,key,mode,IV,counter,segment_size): 70 | cipher_module = Blowfish 71 | self.blocksize = 8 72 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 73 | 74 | def keylen_valid(self,key): 75 | return 8 <= len(key) <= 56 76 | 77 | def _test(): 78 | import doctest 79 | doctest.testmod() 80 | 81 | if __name__ == "__main__": 82 | _test() 83 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_DES.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import pyDes 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | wrapper for pure python implementation pyDes.py 8 | 9 | key = raw string containing the key 10 | mode = python_DES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 11 | -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption 12 | IV = IV as a raw string, default is "all zero" IV 13 | -> needed for CBC, CFB and OFB mode 14 | counter = counter object (CryptoPlus.Util.util.Counter) 15 | -> only needed for CTR mode 16 | -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy 17 | see CTR example further on in the docstring 18 | segment_size = amount of bits to use from the keystream in each chain part 19 | -> supported values: multiple of 8 between 8 and the blocksize 20 | of the cipher (only per byte access possible), default is 8 21 | -> only needed for CFB mode 22 | 23 | EXAMPLES: 24 | ********** 25 | IMPORTING: 26 | ----------- 27 | >>> from CryptoPlus.Cipher import python_DES 28 | 29 | EXAMPLE (test vectors from NESSIE): 30 | ----------------------------------- 31 | >>> cipher = python_DES.new(('7CA110454A1A6E57').decode('hex')) 32 | >>> ciphertext = cipher.encrypt(('01A1D6D039776742').decode('hex')) 33 | >>> (ciphertext).encode('hex') 34 | '690f5b0d9a26939b' 35 | >>> plaintext = cipher.decrypt(ciphertext) 36 | >>> (plaintext).encode('hex') 37 | '01a1d6d039776742' 38 | """ 39 | return python_DES(key,mode,IV,counter,segment_size) 40 | 41 | class python_DES(BlockCipher): 42 | key_error_message = ("Key should be 64 bits") 43 | 44 | def __init__(self,key,mode,IV,counter,segment_size): 45 | cipher_module = pyDes.des 46 | self.blocksize = 8 47 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 48 | 49 | def keylen_valid(self,key): 50 | return len(key) == 8 51 | 52 | def _test(): 53 | import doctest 54 | doctest.testmod() 55 | 56 | if __name__ == "__main__": 57 | _test() 58 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_DES3.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | import pyDes 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a DES-EDE3 or DES-EDE2 cipher object 6 | 7 | wrapper for pure python 3DES implementation pyDes.py 8 | 9 | key = raw string containing the 2/3 keys 10 | - DES-EDE2: supply 2 keys as 1 single concatenated 16byte key= key1|key2 11 | - DES-EDE3: supply 3 keys as 1 single concatenated 24byte key= key1|key2|key3 12 | mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 13 | IV = IV as a raw string, default is "all zero" IV 14 | -> only needed for CBC mode 15 | counter = counter object (CryptoPlus.Util.util.Counter) 16 | -> only needed for CTR mode 17 | segment_size = amount of bits to use from the keystream in each chain part 18 | -> supported values: multiple of 8 between 8 and the blocksize 19 | of the cipher (only per byte access possible), default is 8 20 | -> only needed for CFB mode 21 | 22 | EXAMPLES: 23 | ********** 24 | IMPORTING: 25 | ----------- 26 | >>> from CryptoPlus.Cipher import python_DES3 27 | 28 | CBC TDES-EDE3 EXAMPLE: (using test vectors from http://csrc.nist.gov/groups/STM/cavp/documents/des/DESMMT.pdf) 29 | ------------ 30 | >>> key = ('37ae5ebf46dff2dc0754b94f31cbb3855e7fd36dc870bfae').decode('hex') 31 | >>> IV = ('3d1de3cc132e3b65').decode('hex') 32 | >>> cipher = python_DES3.new(key, python_DES3.MODE_CBC, IV) 33 | >>> ciphertext = cipher.encrypt(('84401f78fe6c10876d8ea23094ea5309').decode('hex')) 34 | >>> (ciphertext).encode('hex') 35 | '7b1f7c7e3b1c948ebd04a75ffba7d2f5' 36 | >>> decipher = python_DES3.new(key, python_DES3.MODE_CBC, IV) 37 | >>> plaintext = decipher.decrypt(ciphertext) 38 | >>> (plaintext).encode('hex') 39 | '84401f78fe6c10876d8ea23094ea5309' 40 | 41 | CMAC TDES-EDE3 EXAMPLE: 42 | ------------- 43 | testvector: http://csrc.nist.gov/publications/nistpubs/800-38B/Updated_CMAC_Examples.pdf 44 | 45 | >>> key = '8aa83bf8cbda10620bc1bf19fbb6cd58bc313d4a371ca8b5'.decode('hex') 46 | >>> plaintext = '6bc1bee22e409f96e93d7e117393172aae2d8a57'.decode('hex') 47 | >>> cipher = python_DES3.new(key, python_DES3.MODE_CMAC) 48 | >>> cipher.encrypt(plaintext).encode('hex') 49 | '743ddbe0ce2dc2ed' 50 | 51 | CMAC TDES-EDE2 EXAMPLE: 52 | ----------------------- 53 | testvector: http://csrc.nist.gov/groups/STM/cavp/documents/mac/cmactestvectors.zip 54 | 55 | >>> key1 = "5104f2c76180c1d3".decode('hex') 56 | >>> key2 = "b9df763e31ada716".decode('hex') 57 | >>> key = key1 + key2 58 | >>> plaintext = 'a6866be2fa6678f264a19c4474968e3f4eec24f5086d'.decode('hex') 59 | >>> cipher = python_DES3.new(key, python_DES3.MODE_CMAC) 60 | >>> cipher.encrypt(plaintext).encode('hex') 61 | '32e7758f3f614dbf'""" 62 | return python_DES3(key,mode,IV,counter,segment_size) 63 | 64 | class python_DES3(BlockCipher): 65 | key_error_message = "Key should be 128 or 192 bits" 66 | 67 | def __init__(self,key,mode,IV,counter,segment_size): 68 | cipher_module = pyDes.triple_des 69 | self.blocksize = 8 70 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 71 | 72 | def keylen_valid(self,key): 73 | return len(key) in (16,24) 74 | 75 | def _test(): 76 | import doctest 77 | doctest.testmod() 78 | 79 | if __name__ == "__main__": 80 | _test() 81 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_PRESENT.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | from pypresent import Present 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None,rounds=32): 5 | """Create a new cipher object 6 | 7 | Wrapper for pure python implementation rijndael.py 8 | 9 | key = raw string containing the key, AES-128..256 will be selected according to the key length 10 | mode = python_PRESENT.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB 11 | -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption 12 | IV = IV as a raw string, default is "all zero" IV 13 | -> needed for CBC, CFB and OFB mode 14 | counter = counter object (CryptoPlus.Util.util.Counter) 15 | -> only needed for CTR mode 16 | -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy 17 | see CTR example further on in the docstring 18 | rounds = amount of rounds 19 | segment_size = amount of bits to use from the keystream in each chain part 20 | -> supported values: multiple of 8 between 8 and the blocksize 21 | of the cipher (only per byte access possible), default is 8 22 | -> only needed for CFB mode 23 | rounds = amount of rounds, default = 32 24 | 25 | Notes: 26 | - Always construct a seperate cipher object for encryption and decryption. Once a cipher object has been used for encryption, 27 | it can't be used for decryption because it keeps a state (if necessary) for the IV. 28 | 29 | EXAMPLES: 30 | ********** 31 | IMPORTING: 32 | ----------- 33 | >>> from CryptoPlus.Cipher import python_PRESENT 34 | 35 | ECB Test Vectors: 36 | ------------------ 37 | >>> key = "00000000000000000000".decode('hex') 38 | >>> plain = "0000000000000000".decode('hex') 39 | >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB) 40 | >>> cipher.encrypt(plain).encode('hex') 41 | '5579c1387b228445' 42 | 43 | >>> key = "00000000000000000000000000000000".decode('hex') 44 | >>> plain = "0000000000000000".decode('hex') 45 | >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64) 46 | >>> cipher.encrypt(plain).encode('hex') 47 | '59a27d01607ebf05' 48 | 49 | >>> key = "00000000000000000000".decode('hex') 50 | >>> plain = "0000000000000000".decode('hex') 51 | >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=64) 52 | >>> cipher.encrypt(plain).encode('hex') 53 | '13991dd588bc1288' 54 | 55 | Test Vectors for maximum rounds supported by PRESENT reference C code: 56 | ----------------------------------------------------------------------- 57 | >>> key = "0123456789abcdef0123".decode('hex') 58 | >>> plain = "0123456789abcdef".decode('hex') 59 | >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534) 60 | >>> ciphertext = cipher.encrypt(plain) 61 | >>> ciphertext.encode('hex') 62 | 'a140dc5d7175ca20' 63 | >>> cipher.decrypt(ciphertext).encode('hex') 64 | '0123456789abcdef' 65 | 66 | >>> key = "0123456789abcdef0123456789abcdef".decode('hex') 67 | >>> plain = "0123456789abcdef".decode('hex') 68 | >>> cipher = python_PRESENT.new(key,python_PRESENT.MODE_ECB,rounds=65534) 69 | >>> ciphertext = cipher.encrypt(plain) 70 | >>> ciphertext.encode('hex') 71 | '21007772e5d4ef14' 72 | >>> cipher.decrypt(ciphertext).encode('hex') 73 | '0123456789abcdef' 74 | """ 75 | return python_PRESENT(key,mode,IV,counter,rounds,segment_size) 76 | 77 | class python_PRESENT(BlockCipher): 78 | key_error_message = "Key should be 80 or 128 bits" 79 | 80 | def __init__(self,key,mode,IV,counter,rounds,segment_size): 81 | cipher_module = Present 82 | args = {'rounds':rounds} 83 | self.blocksize = 8 84 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args) 85 | 86 | def keylen_valid(self,key): 87 | return len(key) in (10,16) 88 | 89 | def _test(): 90 | import doctest 91 | doctest.testmod() 92 | 93 | if __name__ == "__main__": 94 | _test() 95 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_Rijndael.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | from rijndael import rijndael 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None,blocksize=None): 5 | """Create a new cipher object 6 | 7 | Wrapper for pure python implementation rijndael.py 8 | 9 | key = raw string containing the key 10 | -> supported key size are 16, 24 and 32 bytes 11 | mode = python_Rijndael.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB 12 | -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption 13 | IV = IV as a raw string, default is "all zero" IV 14 | -> needed for CBC, CFB and OFB mode 15 | counter = counter object (CryptoPlus.Util.util.Counter) 16 | -> only needed for CTR mode 17 | -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy 18 | see CTR example further on in the docstring 19 | segment_size = amount of bits to use from the keystream in each chain part 20 | -> supported values: multiple of 8 between 8 and the blocksize 21 | of the cipher (only per byte access possible), default is 8 22 | -> only needed for CFB mode 23 | blocksize = blocksize in bytes 24 | -> supported blocksizes are 16, 24 and 32 bytes, must be 16 if XTS mode. 25 | 26 | EXAMPLES: 27 | ********** 28 | IMPORTING: 29 | ----------- 30 | >>> from CryptoPlus.Cipher import python_Rijndael 31 | 32 | EXAMPLE: 33 | -------- 34 | 24 byte block, 32 byte key (http://fp.gladman.plus.com/cryptography_technology/rijndael/) 35 | >>> key = '2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe'.decode('hex') 36 | >>> plaintext ='3243f6a8885a308d313198a2e03707344a4093822299f31d'.decode('hex') 37 | >>> cipher = python_Rijndael.new(key,python_Rijndael.MODE_ECB,blocksize=24) 38 | >>> cipher.encrypt(plaintext).encode('hex') 39 | '0ebacf199e3315c2e34b24fcc7c46ef4388aa475d66c194c' 40 | 41 | CBC EXAMPLE (plaintext = 3 blocksizes) (AES): 42 | ----------------------------------------- 43 | >>> key = ('2b7e151628aed2a6abf7158809cf4f3c').decode('hex') 44 | >>> IV = ('000102030405060708090a0b0c0d0e0f').decode('hex') 45 | >>> plaintext1 = ('6bc1bee22e409f96e93d7e117393172a').decode('hex') 46 | >>> plaintext2 = ('ae2d8a571e03ac9c9eb76fac45af8e51').decode('hex') 47 | >>> plaintext3 = ('30c81c46a35ce411e5fbc1191a0a52ef').decode('hex') 48 | >>> cipher = python_Rijndael.new(key,python_Rijndael.MODE_CBC,IV,blocksize=16) 49 | >>> ciphertext = cipher.encrypt(plaintext1 + plaintext2 + plaintext3) 50 | >>> (ciphertext).encode('hex') 51 | '7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e22229516' 52 | >>> decipher = python_Rijndael.new(key,python_Rijndael.MODE_CBC,IV,blocksize=16) 53 | >>> plaintext = decipher.decrypt(ciphertext) 54 | >>> (plaintext).encode('hex') 55 | '6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef' 56 | 57 | XTS EXAMPLE: 58 | ------------ 59 | (Examples for XTS-AES) 60 | XTS-AES-128 applied for a data unit of 512 bytes 61 | testvector: http://grouper.ieee.org/groups/1619/email/pdf00086.pdf 62 | 63 | >>> key = ('27182818284590452353602874713526'.decode('hex'),'31415926535897932384626433832795'.decode('hex')) 64 | >>> plaintext = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'.decode('hex') 65 | >>> cipher = python_Rijndael.new(key,python_Rijndael.MODE_XTS,blocksize=16) 66 | >>> ciphertext = cipher.encrypt(plaintext) 67 | >>> ciphertext.encode('hex') 68 | '27a7479befa1d476489f308cd4cfa6e2a96e4bbe3208ff25287dd3819616e89cc78cf7f5e543445f8333d8fa7f56000005279fa5d8b5e4ad40e736ddb4d35412328063fd2aab53e5ea1e0a9f332500a5df9487d07a5c92cc512c8866c7e860ce93fdf166a24912b422976146ae20ce846bb7dc9ba94a767aaef20c0d61ad02655ea92dc4c4e41a8952c651d33174be51a10c421110e6d81588ede82103a252d8a750e8768defffed9122810aaeb99f9172af82b604dc4b8e51bcb08235a6f4341332e4ca60482a4ba1a03b3e65008fc5da76b70bf1690db4eae29c5f1badd03c5ccf2a55d705ddcd86d449511ceb7ec30bf12b1fa35b913f9f747a8afd1b130e94bff94effd01a91735ca1726acd0b197c4e5b03393697e126826fb6bbde8ecc1e08298516e2c9ed03ff3c1b7860f6de76d4cecd94c8119855ef5297ca67e9f3e7ff72b1e99785ca0a7e7720c5b36dc6d72cac9574c8cbbc2f801e23e56fd344b07f22154beba0f08ce8891e643ed995c94d9a69c9f1b5f499027a78572aeebd74d20cc39881c213ee770b1010e4bea718846977ae119f7a023ab58cca0ad752afe656bb3c17256a9f6e9bf19fdd5a38fc82bbe872c5539edb609ef4f79c203ebb140f2e583cb2ad15b4aa5b655016a8449277dbd477ef2c8d6c017db738b18deb4a427d1923ce3ff262735779a418f20a282df920147beabe421ee5319d0568' 69 | >>> decipher = python_Rijndael.new(key,python_Rijndael.MODE_XTS,blocksize=16) 70 | >>> decipher.decrypt(ciphertext).encode('hex') 71 | '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff' 72 | 73 | using data sequence number n 74 | 75 | >>> key = ('fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0'.decode('hex'),'22222222222222222222222222222222'.decode('hex')) 76 | >>> plain ='4444444444444444444444444444444444444444444444444444444444444444'.decode('hex') 77 | >>> n = '3333333333'.decode('hex') 78 | >>> cipher = python_Rijndael.new(key,python_Rijndael.MODE_XTS,blocksize=16) 79 | >>> ciphertext = cipher.encrypt(plain,n) 80 | >>> ciphertext.encode('hex') 81 | 'af85336b597afc1a900b2eb21ec949d292df4c047e0b21532186a5971a227a89' 82 | >>> decipher = python_Rijndael.new(key,python_Rijndael.MODE_XTS,blocksize=16) 83 | >>> decipher.decrypt(ciphertext,n).encode('hex') 84 | '4444444444444444444444444444444444444444444444444444444444444444' 85 | """ 86 | return python_Rijndael(key,mode,IV,counter,blocksize,segment_size) 87 | 88 | class python_Rijndael(BlockCipher): 89 | key_error_message = ("Key should be 128, 192 or 256 bits") 90 | 91 | def __init__(self,key,mode,IV,counter,blocksize,segment_size): 92 | if blocksize not in (16,24,32): 93 | raise ValueError("Blocksize should be 16, 24 or 32") 94 | cipher_module = rijndael 95 | args = {'block_size':blocksize} 96 | self.blocksize = blocksize 97 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size,args) 98 | 99 | def keylen_valid(self,key): 100 | return len(key) in (16,24,32) 101 | 102 | def _test(): 103 | import doctest 104 | doctest.testmod() 105 | 106 | if __name__ == "__main__": 107 | _test() 108 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_Serpent.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | from pyserpent import Serpent 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | Wrapper for pure python implementation pyserpent.py 8 | 9 | key = raw string containing the key 10 | -> when using XTS mode: the key should be a tuple containing the 2 keys needed 11 | mode = python_Serpent.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB 12 | -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption 13 | IV = IV as a raw string, default is "all zero" IV 14 | -> needed for CBC, CFB and OFB mode 15 | counter = counter object (CryptoPlus.Util.util.Counter) 16 | -> only needed for CTR mode 17 | -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy 18 | see CTR example further on in the docstring 19 | 20 | EXAMPLES: 21 | ********** 22 | IMPORTING: 23 | ----------- 24 | >>> from CryptoPlus.Cipher import python_Serpent 25 | 26 | EXAMPLE: 27 | --------- 28 | NESSIE Test Vectors: http://www.cs.technion.ac.il/~biham/Reports/Serpent/Serpent-128-128.verified.test-vectors 29 | 30 | >>> cipher = python_Serpent.new(('000102030405060708090A0B0C0D0E0F').decode('hex')) 31 | >>> (cipher.encrypt(('33B3DC87EDDD9B0F6A1F407D14919365').decode('hex'))).encode('hex').upper() 32 | '00112233445566778899AABBCCDDEEFF' 33 | >>> ( cipher.decrypt((_).decode('hex')) ).encode('hex').upper() 34 | '33B3DC87EDDD9B0F6A1F407D14919365' 35 | 36 | >>> cipher = python_Serpent.new(('FDFDFDFDFDFDFDFDFDFDFDFDFDFDFDFD').decode('hex')) 37 | >>> (cipher.encrypt(('FDFDFDFDFDFDFDFDFDFDFDFDFDFDFDFD').decode('hex'))).encode('hex').upper() 38 | '81F9163BDF39B5BB2932AB91DF2A5FFC' 39 | >>> ( cipher.decrypt((_).decode('hex')) ).encode('hex').upper() 40 | 'FDFDFDFDFDFDFDFDFDFDFDFDFDFDFDFD' 41 | 42 | CBC EXAMPLE: 43 | ------------- 44 | >>> key = ('000102030405060708090A0B0C0D0E0F').decode('hex') 45 | >>> IV = ('00000000000000000000000000000000').decode('hex') 46 | >>> plaintext = ('33B3DC87EDDD9B0F6A1F407D14919365'*3).decode('hex') 47 | >>> cipher = python_Serpent.new(key,python_Serpent.MODE_CBC,IV) 48 | >>> ciphertext = cipher.encrypt(plaintext) 49 | >>> decipher = python_Serpent.new(key,python_Serpent.MODE_CBC,IV) 50 | >>> ( decipher.decrypt(ciphertext)).encode('hex').upper() 51 | '33B3DC87EDDD9B0F6A1F407D1491936533B3DC87EDDD9B0F6A1F407D1491936533B3DC87EDDD9B0F6A1F407D14919365' 52 | """ 53 | return python_Serpent(key,mode,IV,counter,segment_size) 54 | 55 | class python_Serpent(BlockCipher): 56 | def __init__(self,key,mode,IV,counter,segment_size): 57 | if len(key) not in (16,24,32) and type(key) is not tuple: 58 | raise ValueError("Key should be 128, 192 or 256 bits") 59 | cipher_module = Serpent 60 | self.blocksize = 16 61 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 62 | 63 | def _test(): 64 | import doctest 65 | doctest.testmod() 66 | 67 | if __name__ == "__main__": 68 | _test() 69 | -------------------------------------------------------------------------------- /src/CryptoPlus/Cipher/python_Twofish.py: -------------------------------------------------------------------------------- 1 | from blockcipher import * 2 | from pytwofish import Twofish 3 | 4 | def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None): 5 | """Create a new cipher object 6 | 7 | Wrapper for pure python implementation pytwofish.py 8 | 9 | key = raw string containing the key 10 | -> when using XTS mode: the key should be a tuple containing the 2 keys needed 11 | mode = python_Twofish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB 12 | -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption 13 | IV = IV as a raw string, default is "all zero" IV 14 | -> needed for CBC, CFB and OFB mode 15 | counter = counter object (CryptoPlus.Util.util.Counter) 16 | -> only needed for CTR mode 17 | -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy 18 | see CTR example further on in the docstring 19 | segment_size = amount of bits to use from the keystream in each chain part 20 | -> supported values: multiple of 8 between 8 and the blocksize 21 | of the cipher (only per byte access possible), default is 8 22 | -> only needed for CFB mode 23 | 24 | EXAMPLES: 25 | ********** 26 | IMPORTING: 27 | ----------- 28 | >>> from CryptoPlus.Cipher import python_Twofish 29 | 30 | EXAMPLE: 31 | ---------- 32 | http://www.schneier.com/code/ecb_ival.txt -> test vector I=5 33 | 34 | >>> cipher = python_Twofish.new(('019F9809DE1711858FAAC3A3BA20FBC3').decode('hex')) 35 | >>> (cipher.encrypt(('6363977DE839486297E661C6C9D668EB').decode('hex'))).encode('hex').upper() 36 | '816D5BD0FAE35342BF2A7412C246F752' 37 | >>> ( cipher.decrypt((_).decode('hex')) ).encode('hex').upper() 38 | '6363977DE839486297E661C6C9D668EB' 39 | """ 40 | return python_Twofish(key,mode,IV,counter,segment_size) 41 | 42 | class python_Twofish(BlockCipher): 43 | def __init__(self,key,mode,IV,counter,segment_size): 44 | if len(key) not in (16,24,32) and type(key) is not tuple: 45 | raise ValueError("Key should be 128, 192 or 256 bits") 46 | cipher_module = Twofish 47 | self.blocksize = 16 48 | BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size) 49 | 50 | def _test(): 51 | import doctest 52 | doctest.testmod() 53 | 54 | if __name__ == "__main__": 55 | _test() 56 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/RIPEMD.py: -------------------------------------------------------------------------------- 1 | from Crypto.Hash import RIPEMD 2 | 3 | def new(data=None): 4 | """Create a new RIPEMD-160 hash object 5 | 6 | data = initial input (raw string) to the hashing object 7 | if present, the method call update(arg) is made 8 | 9 | EXAMPLE: 10 | ========= 11 | 12 | >>> from CryptoPlus.Hash import RIPEMD 13 | 14 | >>> message = "abc" 15 | >>> hasher = RIPEMD.new() 16 | >>> hasher.update(message) 17 | >>> hasher.hexdigest() 18 | '8eb208f7e05d987a9b044a8e98c6b087f15a0bfc' 19 | 20 | >>> message = "message digest" 21 | >>> hasher = RIPEMD.new() 22 | >>> hasher.update(message) 23 | >>> hasher.hexdigest() 24 | '5d0689ef49d2fae572b881b123a85ffa21595f36' 25 | """ 26 | return RIPEMD.new(data) 27 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/__init__.py: -------------------------------------------------------------------------------- 1 | # hash functions of pycrypto can be just imported 2 | # wrapping might be a better idea if docstrings need to be expanded 3 | # wrapping in Cipher was needed to make the new chaining modes available 4 | from Crypto.Hash import SHA, SHA256, MD5, MD2, MD4, HMAC 5 | 6 | __all__ = ["SHA","SHA256","MD5","MD2","MD4","HMAC","RIPEMD"] 7 | 8 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/pymd5.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/doegox/python-cryptoplus/a5a1f8aecce4ddf476b2d80b586822d9e91eeb7d/src/CryptoPlus/Hash/pymd5.py -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/pysha224.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' from pysha256 import sha256 def new(m=None): return sha224(m) class sha224(sha256): _h = (0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4) _output_size = 7 digest_size = 28 -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/pysha256.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' import copy, struct, sys def new(m=None): return sha256(m) class sha256(object): _k = (0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2) _h = (0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19) _output_size = 8 2 | blocksize = 1 block_size = 64 digest_size = 32 def __init__(self, m=None): self._buffer = '' self._counter = 0 if m is not None: if type(m) is not str: raise TypeError, '%s() argument 1 must be string, not %s' % (self.__class__.__name__, type(m).__name__) self.update(m) def _rotr(self, x, y): return ((x >> y) | (x << (32-y))) & 0xFFFFFFFF def _sha256_process(self, c): w = [0]*64 w[0:15] = struct.unpack('!16L', c) for i in range(16, 64): s0 = self._rotr(w[i-15], 7) ^ self._rotr(w[i-15], 18) ^ (w[i-15] >> 3) s1 = self._rotr(w[i-2], 17) ^ self._rotr(w[i-2], 19) ^ (w[i-2] >> 10) w[i] = (w[i-16] + s0 + w[i-7] + s1) & 0xFFFFFFFF a,b,c,d,e,f,g,h = self._h for i in range(64): s0 = self._rotr(a, 2) ^ self._rotr(a, 13) ^ self._rotr(a, 22) maj = (a & b) ^ (a & c) ^ (b & c) t2 = s0 + maj s1 = self._rotr(e, 6) ^ self._rotr(e, 11) ^ self._rotr(e, 25) ch = (e & f) ^ ((~e) & g) t1 = h + s1 + ch + self._k[i] + w[i] h = g g = f f = e e = (d + t1) & 0xFFFFFFFF d = c c = b b = a a = (t1 + t2) & 0xFFFFFFFF self._h = [(x+y) & 0xFFFFFFFF for x,y in zip(self._h, [a,b,c,d,e,f,g,h])] def update(self, m): if not m: return if type(m) is not str: raise TypeError, '%s() argument 1 must be string, not %s' % (sys._getframe().f_code.co_name, type(m).__name__) self._buffer += m self._counter += len(m) while len(self._buffer) >= 64: self._sha256_process(self._buffer[:64]) self._buffer = self._buffer[64:] def digest(self): mdi = self._counter & 0x3F length = struct.pack('!Q', self._counter<<3) if mdi < 56: padlen = 55-mdi else: padlen = 119-mdi r = self.copy() r.update('\x80'+('\x00'*padlen)+length) return ''.join([struct.pack('!L', i) for i in r._h[:self._output_size]]) def hexdigest(self): return self.digest().encode('hex') def copy(self): return copy.deepcopy(self) 3 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/pysha384.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' from pysha512 import sha512 def new(m=None): return sha384(m) class sha384(sha512): _h = (0xcbbb9d5dc1059ed8L, 0x629a292a367cd507L, 0x9159015a3070dd17L, 0x152fecd8f70e5939L, 0x67332667ffc00b31L, 0x8eb44a8768581511L, 0xdb0c2e0d64f98fa7L, 0x47b5481dbefa4fa4L) _output_size = 6 digest_size = 48 -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/pysha512.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python __author__ = 'Thomas Dixon' __license__ = 'MIT' import copy, struct, sys def new(m=None): return sha512(m) class sha512(object): _k = (0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L, 0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L, 0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L, 0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L, 0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L, 0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L, 0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL, 0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L, 0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL, 0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL, 0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L, 0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L, 0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L, 0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L, 0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL, 0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL, 0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL, 0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L, 0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L, 0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL, 0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL, 0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L) _h = (0x6a09e667f3bcc908L, 0xbb67ae8584caa73bL, 0x3c6ef372fe94f82bL, 0xa54ff53a5f1d36f1L, 0x510e527fade682d1L, 0x9b05688c2b3e6c1fL, 0x1f83d9abfb41bd6bL, 0x5be0cd19137e2179L) _output_size = 8 2 | blocksize = 1 block_size = 128 digest_size = 64 def __init__(self, m=None): self._buffer = '' self._counter = 0 if m is not None: if type(m) is not str: raise TypeError, '%s() argument 1 must be string, not %s' % (self.__class__.__name__, type(m).__name__) self.update(m) def _rotr(self, x, y): return ((x >> y) | (x << (64-y))) & 0xFFFFFFFFFFFFFFFF def _sha512_process(self, chunk): w = [0]*80 w[0:15] = struct.unpack('!16Q', chunk) for i in range(16, 80): s0 = self._rotr(w[i-15], 1) ^ self._rotr(w[i-15], 8) ^ (w[i-15] >> 7) s1 = self._rotr(w[i-2], 19) ^ self._rotr(w[i-2], 61) ^ (w[i-2] >> 6) w[i] = (w[i-16] + s0 + w[i-7] + s1) & 0xFFFFFFFFFFFFFFFF a,b,c,d,e,f,g,h = self._h for i in range(80): s0 = self._rotr(a, 28) ^ self._rotr(a, 34) ^ self._rotr(a, 39) maj = (a & b) ^ (a & c) ^ (b & c) t2 = s0 + maj s1 = self._rotr(e, 14) ^ self._rotr(e, 18) ^ self._rotr(e, 41) ch = (e & f) ^ ((~e) & g) t1 = h + s1 + ch + self._k[i] + w[i] h = g g = f f = e e = (d + t1) & 0xFFFFFFFFFFFFFFFF d = c c = b b = a a = (t1 + t2) & 0xFFFFFFFFFFFFFFFF self._h = [(x+y) & 0xFFFFFFFFFFFFFFFF for x,y in zip(self._h, [a,b,c,d,e,f,g,h])] def update(self, m): if not m: return if type(m) is not str: raise TypeError, '%s() argument 1 must be string, not %s' % (sys._getframe().f_code.co_name, type(m).__name__) self._buffer += m self._counter += len(m) while len(self._buffer) >= 128: self._sha512_process(self._buffer[:128]) self._buffer = self._buffer[128:] def digest(self): mdi = self._counter & 0x7F length = struct.pack('!Q', self._counter<<3) if mdi < 112: padlen = 111-mdi else: padlen = 239-mdi r = self.copy() r.update('\x80'+('\x00'*(padlen+8))+length) return ''.join([struct.pack('!Q', i) for i in r._h[:self._output_size]]) def hexdigest(self): return self.digest().encode('hex') def copy(self): return copy.deepcopy(self) 3 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_MD5.py: -------------------------------------------------------------------------------- 1 | import pymd5 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python MD5 hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: (http://www.rfc-editor.org/rfc/rfc1321.txt) 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import MD5 15 | 16 | >>> message = "abc" 17 | >>> hasher = MD5.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest() 20 | '900150983cd24fb0d6963f7d28e17f72' 21 | 22 | >>> message = "message digest" 23 | >>> hasher = MD5.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest() 26 | 'f96b697d7cb7938d525a2f31aaf161d0' 27 | """ 28 | return pymd5.new(data) 29 | 30 | digest_size = pymd5.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_PBKDF2.py: -------------------------------------------------------------------------------- 1 | import pypbkdf2 2 | from CryptoPlus.Hash import SHA as SHA1, HMAC 3 | 4 | __all__ = ['new'] 5 | 6 | def new(passphrase, salt, iterations=1000, digestmodule=SHA1, macmodule=HMAC): 7 | """PKCS#5 v2.0 Password-Based Key Derivation 8 | 9 | passphrase = the passphrase, supplied as a raw string, to make a key from 10 | salt = salt as raw string 11 | iterations = amount of iterations (default = 1000) 12 | digestmodule = digest function to use, supply as module 13 | example: python_SHA from CryptoPlus.Hash 14 | default: SHA1 15 | macmodule = mac function to use, supply as module 16 | example: HMAC from CryptoPlus.Hash 17 | default: HMAC 18 | 19 | => macmodule & digestmodule construct the pseudorandom function 20 | => by default: HMAC-SHA1 21 | 22 | Examples: (from: http://www.ietf.org/rfc/rfc3962.txt) 23 | ========== 24 | 25 | >>> from CryptoPlus.Hash import python_PBKDF2 26 | 27 | >>> passphrase = "password" 28 | >>> salt = "ATHENA.MIT.EDUraeburn" 29 | >>> iterations = 1 30 | >>> hasher = python_PBKDF2.new(passphrase,salt,iterations) 31 | >>> hasher.hexread(16) 32 | 'cdedb5281bb2f801565a1122b2563515' 33 | 34 | >>> passphrase = "password" 35 | >>> salt = "ATHENA.MIT.EDUraeburn" 36 | >>> iterations = 1200 37 | >>> hasher = python_PBKDF2.new(passphrase,salt,iterations) 38 | >>> hasher.hexread(32) 39 | '5c08eb61fdf71e4e4ec3cf6ba1f5512ba7e52ddbc5e5142f708a31e2e62b1e13' 40 | 41 | >>> passphrase = "X"*64 42 | >>> salt = "pass phrase equals block size" 43 | >>> iterations = 1200 44 | >>> hasher = python_PBKDF2.new(passphrase,salt,iterations) 45 | >>> hasher.hexread(32) 46 | '139c30c0966bc32ba55fdbf212530ac9c5ec59f1a452f5cc9ad940fea0598ed1' 47 | 48 | >>> passphrase = "X"*65 49 | >>> salt = "pass phrase exceeds block size" 50 | >>> iterations = 1200 51 | >>> hasher = python_PBKDF2.new(passphrase,salt,iterations) 52 | >>> hasher.hexread(32) 53 | '9ccad6d468770cd51b10e6a68721be611a8b4d282601db3b36be9246915ec82a' 54 | """ 55 | return pypbkdf2.PBKDF2(passphrase, salt, iterations, digestmodule, macmodule) 56 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_RadioGatun.py: -------------------------------------------------------------------------------- 1 | from pyradiogatun import RadioGatunType 2 | 3 | __all__ = ['new'] 4 | 5 | def new(data=None,wl=64): 6 | """Create a new pure python RadioGatun hash object 7 | 8 | wl = wordlength (in bits) of the RadioGatun hash method 9 | between 1 and 64 (default = 64) 10 | data = if present, the method call update(arg) is made 11 | 12 | EXAMPLES: (testvectors from: http://radiogatun.noekeon.org/) 13 | ========== 14 | >>> import python_RadioGatun 15 | 16 | radiogatun[64] 17 | --------------- 18 | >>> hasher = python_RadioGatun.new() 19 | >>> hasher.update('1234567890123456') 20 | >>> hasher.hexdigest() 21 | 'caaec14b5b4a7960d6854709770e3071d635d60224f58aa385867e549ef4cc42' 22 | 23 | >>> hasher = python_RadioGatun.new() 24 | >>> hasher.update('Santa Barbara, California') 25 | >>> hasher.hexdigest() 26 | '0d08daf2354fa95aaa5b6a50f514384ecdd35940252e0631002e600e13cd285f' 27 | 28 | radiogatun[32] 29 | --------------- 30 | >>> hasher = python_RadioGatun.new(wl=32) 31 | >>> hasher.update('1234567890123456') 32 | >>> hasher.hexdigest() 33 | '59612324f3f42d3096e69125d2733b86143ae668ae9ed561ad785e0eac8dba25' 34 | 35 | >>> hasher = python_RadioGatun.new(wl=32) 36 | >>> hasher.update('Santa Barbara, California') 37 | >>> hasher.hexdigest() 38 | '041666388ef9655d48996a66dada1193d6646012a7b25a24fb10e6075cf0fc54' 39 | """ 40 | 41 | crypto = RadioGatunType(wl) 42 | if data: 43 | crypto.update(data) 44 | 45 | return crypto 46 | 47 | def _test(): 48 | import doctest 49 | doctest.testmod() 50 | 51 | if __name__ == "__main__": 52 | print "DOCTEST running... no messages = all good" 53 | _test() 54 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_SHA.py: -------------------------------------------------------------------------------- 1 | import pysha 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python SHA hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: FIPS 180-2 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import python_SHA 15 | 16 | >>> message = "abc" 17 | >>> hasher = python_SHA.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest() 20 | 'a9993e364706816aba3e25717850c26c9cd0d89d' 21 | 22 | >>> message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 23 | >>> hasher = python_SHA.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest() 26 | '84983e441c3bd26ebaae4aa1f95129e5e54670f1' 27 | """ 28 | return pysha.new(data) 29 | 30 | digest_size = pysha.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_SHA224.py: -------------------------------------------------------------------------------- 1 | from pysha224 import sha224 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python SHA-224 hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: FIPS 180-2 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import python_SHA224 15 | 16 | >>> message = "abc" 17 | >>> hasher = python_SHA224.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest() 20 | '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7' 21 | 22 | >>> message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 23 | >>> hasher = python_SHA224.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest() 26 | '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525' 27 | """ 28 | return sha224(data) 29 | 30 | digest_size = sha224.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_SHA256.py: -------------------------------------------------------------------------------- 1 | from pysha256 import sha256 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python SHA-256 hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: FIPS 180-2 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import python_SHA256 15 | 16 | >>> message = "abc" 17 | >>> hasher = python_SHA256.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest() 20 | 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' 21 | 22 | >>> message = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" 23 | >>> hasher = python_SHA256.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest() 26 | '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1' 27 | """ 28 | return sha256(data) 29 | 30 | digest_size = sha256.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_SHA384.py: -------------------------------------------------------------------------------- 1 | from pysha384 import sha384 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python SHA-384 hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: FIPS 180-2 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import python_SHA384 15 | 16 | >>> message = "abc" 17 | >>> hasher = python_SHA384.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest() 20 | 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7' 21 | 22 | >>> message = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" 23 | >>> hasher = python_SHA384.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest() 26 | '09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039' 27 | """ 28 | return sha384(data) 29 | 30 | digest_size = sha384.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_SHA512.py: -------------------------------------------------------------------------------- 1 | from pysha512 import sha512 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python SHA-512 hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: FIPS 180-2 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import python_SHA512 15 | 16 | >>> message = "abc" 17 | >>> hasher = python_SHA512.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest() 20 | 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f' 21 | 22 | >>> message = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" 23 | >>> hasher = python_SHA512.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest() 26 | '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909' 27 | """ 28 | return sha512(data) 29 | 30 | digest_size = sha512.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Hash/python_whirlpool.py: -------------------------------------------------------------------------------- 1 | import pywhirlpool 2 | 3 | __all__ = ['new','digest_size'] 4 | 5 | def new(data=None): 6 | """Create a new pure python Whirlpool hash object 7 | 8 | data = initial input (raw string) to the hashing object 9 | if present, the method call update(arg) is made 10 | 11 | EXAMPLE: (http://paginas.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html) 12 | ========= 13 | 14 | >>> from CryptoPlus.Hash import python_whirlpool 15 | 16 | >>> message = "abc" 17 | >>> hasher = python_whirlpool.new() 18 | >>> hasher.update(message) 19 | >>> hasher.hexdigest().upper() 20 | '4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5' 21 | 22 | >>> message = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 23 | >>> hasher = python_whirlpool.new() 24 | >>> hasher.update(message) 25 | >>> hasher.hexdigest().upper() 26 | 'DC37E008CF9EE69BF11F00ED9ABA26901DD7C28CDEC066CC6AF42E40F82F3A1E08EBA26629129D8FB7CB57211B9281A65517CC879D7B962142C65F5A7AF01467' 27 | """ 28 | return pywhirlpool.new(data) 29 | 30 | digest_size = pywhirlpool.digest_size 31 | -------------------------------------------------------------------------------- /src/CryptoPlus/Protocol.py: -------------------------------------------------------------------------------- 1 | """Imports Crypto Protocol 2 | 3 | Now you can do: 4 | >>> from CryptoPlus.Protocol import * 5 | OR: 6 | >>> from CryptoPlus.Protocol import XXX 7 | but not: 8 | >>> import CryptoPlus.Protocol.XXX 9 | """ 10 | from Crypto.Protocol import * 11 | -------------------------------------------------------------------------------- /src/CryptoPlus/PublicKey.py: -------------------------------------------------------------------------------- 1 | """Imports Crypto PublicKey 2 | 3 | Now you can do: 4 | >>> from CryptoPlus.PublicKey import * 5 | OR: 6 | >>> from CryptoPlus.PublicKey import RSA 7 | but not: 8 | >>> import CryptoPlus.PublicKey.RSA 9 | """ 10 | 11 | from Crypto.PublicKey import * 12 | -------------------------------------------------------------------------------- /src/CryptoPlus/Random/Fortuna.py: -------------------------------------------------------------------------------- 1 | from Crypto.Random.Fortuna import * 2 | -------------------------------------------------------------------------------- /src/CryptoPlus/Random/OSRNG.py: -------------------------------------------------------------------------------- 1 | from Crypto.Random.OSRNG import * 2 | -------------------------------------------------------------------------------- /src/CryptoPlus/Random/__init__.py: -------------------------------------------------------------------------------- 1 | from Crypto.Random import * 2 | from Crypto.Random import _UserFriendlyRNG, atfork, random 3 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/..new: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/doegox/python-cryptoplus/a5a1f8aecce4ddf476b2d80b586822d9e91eeb7d/src/CryptoPlus/SelfTest/..new -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/__init__.py: Self-test for cipher modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for cipher modules""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | def get_tests(): 34 | tests = [] 35 | import test_python_AES; tests += test_python_AES.get_tests() 36 | import test_AES; tests += test_AES.get_tests() 37 | import test_ARC2; tests += test_ARC2.get_tests() 38 | import test_ARC4; tests += test_ARC4.get_tests() 39 | import test_Blowfish; tests += test_Blowfish.get_tests() 40 | import test_CAST; tests += test_CAST.get_tests() 41 | import test_DES3; tests += test_DES3.get_tests() 42 | import test_DES; tests += test_DES.get_tests() 43 | import test_IDEA; tests += test_IDEA.get_tests() 44 | import test_RC5; tests += test_RC5.get_tests() 45 | import test_XOR; tests += test_XOR.get_tests() 46 | return tests 47 | 48 | if __name__ == '__main__': 49 | import unittest 50 | suite = lambda: unittest.TestSuite(get_tests()) 51 | unittest.main(defaultTest='suite') 52 | 53 | # vim:set ts=4 sw=4 sts=4 expandtab: 54 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/common.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/common.py: Common code for CryptoPlus.SelfTest.Hash 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-testing for PyCryptoPlus hash modules""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import sys 34 | import unittest 35 | from binascii import a2b_hex, b2a_hex 36 | 37 | # For compatibility with Python 2.1 and Python 2.2 38 | if sys.hexversion < 0x02030000: 39 | # Python 2.1 doesn't have a dict() function 40 | # Python 2.2 dict() function raises TypeError if you do dict(MD5='blah') 41 | def dict(**kwargs): 42 | return kwargs.copy() 43 | else: 44 | dict = __builtins__['dict'] 45 | 46 | class _NoDefault: pass # sentinel object 47 | def _extract(d, k, default=_NoDefault): 48 | """Get an item from a dictionary, and remove it from the dictionary.""" 49 | try: 50 | retval = d[k] 51 | except KeyError: 52 | if default is _NoDefault: 53 | raise 54 | return default 55 | del d[k] 56 | return retval 57 | 58 | # Generic cipher test case 59 | class CipherSelfTest(unittest.TestCase): 60 | 61 | def __init__(self, module, params): 62 | unittest.TestCase.__init__(self) 63 | self.module = module 64 | 65 | # Extract the parameters 66 | params = params.copy() 67 | self.description = _extract(params, 'description') 68 | self.key = _extract(params, 'key') 69 | self.plaintext = _extract(params, 'plaintext') 70 | self.ciphertext = _extract(params, 'ciphertext') 71 | 72 | mode = _extract(params, 'mode', None) 73 | if mode is not None: 74 | # Block cipher 75 | self.mode = getattr(self.module, "MODE_" + mode) 76 | self.iv = _extract(params, 'iv', None) 77 | else: 78 | # Stream cipher 79 | self.mode = None 80 | self.iv = None 81 | 82 | self.extra_params = params 83 | 84 | def shortDescription(self): 85 | return self.description 86 | 87 | def _new(self): 88 | if self.mode is None: 89 | # Stream cipher 90 | return self.module.new(a2b_hex(self.key), **self.extra_params) 91 | elif self.iv is None: 92 | # Block cipher without iv 93 | return self.module.new(a2b_hex(self.key), self.mode, **self.extra_params) 94 | else: 95 | # Block cipher with iv 96 | return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.iv), **self.extra_params) 97 | 98 | def runTest(self): 99 | plaintext = a2b_hex(self.plaintext) 100 | ciphertext = a2b_hex(self.ciphertext) 101 | 102 | ct1 = b2a_hex(self._new().encrypt(plaintext)) 103 | pt1 = b2a_hex(self._new().decrypt(ciphertext)) 104 | ct2 = b2a_hex(self._new().encrypt(plaintext)) 105 | pt2 = b2a_hex(self._new().decrypt(ciphertext)) 106 | 107 | self.assertEqual(self.ciphertext, ct1) 108 | self.assertEqual(self.ciphertext, ct2) 109 | self.assertEqual(self.plaintext, pt1) 110 | self.assertEqual(self.plaintext, pt2) 111 | 112 | def make_block_tests(module, module_name, test_data): 113 | tests = [] 114 | for i in range(len(test_data)): 115 | row = test_data[i] 116 | 117 | # Build the "params" dictionary 118 | params = {'mode': 'ECB'} 119 | if len(row) == 3: 120 | (params['plaintext'], params['ciphertext'], params['key']) = row 121 | elif len(row) == 4: 122 | (params['plaintext'], params['ciphertext'], params['key'], params['description']) = row 123 | elif len(row) == 5: 124 | (params['plaintext'], params['ciphertext'], params['key'], params['description'], extra_params) = row 125 | params.update(extra_params) 126 | else: 127 | raise AssertionError("Unsupported tuple size %d" % (len(row),)) 128 | 129 | # Build the display-name for the test 130 | p2 = params.copy() 131 | p_key = _extract(p2, 'key') 132 | p_plaintext = _extract(p2, 'plaintext') 133 | p_ciphertext = _extract(p2, 'ciphertext') 134 | p_description = _extract(p2, 'description', None) 135 | p_mode = p2.get('mode', 'ECB') 136 | if p_mode == 'ECB': 137 | _extract(p2, 'mode', 'ECB') 138 | 139 | if p_description is not None: 140 | description = p_description 141 | elif p_mode == 'ECB' and not p2: 142 | description = "p=%s, k=%s" % (p_plaintext, p_key) 143 | else: 144 | description = "p=%s, k=%s, %r" % (p_plaintext, p_key, p2) 145 | name = "%s #%d: %s" % (module_name, i+1, description) 146 | params['description'] = name 147 | 148 | # Add the test to the test suite 149 | tests.append(CipherSelfTest(module, params)) 150 | return tests 151 | 152 | def make_stream_tests(module, module_name, test_data): 153 | tests = [] 154 | for i in range(len(test_data)): 155 | row = test_data[i] 156 | 157 | # Build the "params" dictionary 158 | params = {} 159 | if len(row) == 3: 160 | (params['plaintext'], params['ciphertext'], params['key']) = row 161 | elif len(row) == 4: 162 | (params['plaintext'], params['ciphertext'], params['key'], params['description']) = row 163 | elif len(row) == 5: 164 | (params['plaintext'], params['ciphertext'], params['key'], params['description'], extra_params) = row 165 | params.update(extra_params) 166 | else: 167 | raise AssertionError("Unsupported tuple size %d" % (len(row),)) 168 | 169 | # Build the display-name for the test 170 | p2 = params.copy() 171 | p_key = _extract(p2, 'key') 172 | p_plaintext = _extract(p2, 'plaintext') 173 | p_ciphertext = _extract(p2, 'ciphertext') 174 | p_description = _extract(p2, 'description', None) 175 | 176 | if p_description is not None: 177 | description = p_description 178 | elif not p2: 179 | description = "p=%s, k=%s" % (p_plaintext, p_key) 180 | else: 181 | description = "p=%s, k=%s, %r" % (p_plaintext, p_key, p2) 182 | name = "%s #%d: %s" % (module_name, i+1, description) 183 | params['description'] = name 184 | 185 | # Add the test to the test suite 186 | tests.append(CipherSelfTest(module, params)) 187 | return tests 188 | 189 | # vim:set ts=4 sw=4 sts=4 expandtab: 190 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_ARC2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/ARC2.py: Self-test for the Alleged-RC2 cipher 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.ARC2""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | from common import dict # For compatibility with Python 2.1 and 2.2 34 | 35 | # This is a list of (plaintext, ciphertext, key[, description[, extra_params]]) tuples. 36 | test_data = [ 37 | # Test vectors from RFC 2268 38 | 39 | # 63-bit effective key length 40 | ('0000000000000000', 'ebb773f993278eff', '0000000000000000', 41 | 'RFC2268-1', dict(effective_keylen=63)), 42 | 43 | # 64-bit effective key length 44 | ('ffffffffffffffff', '278b27e42e2f0d49', 'ffffffffffffffff', 45 | 'RFC2268-2', dict(effective_keylen=64)), 46 | ('1000000000000001', '30649edf9be7d2c2', '3000000000000000', 47 | 'RFC2268-3', dict(effective_keylen=64)), 48 | ('0000000000000000', '61a8a244adacccf0', '88', 49 | 'RFC2268-4', dict(effective_keylen=64)), 50 | ('0000000000000000', '6ccf4308974c267f', '88bca90e90875a', 51 | 'RFC2268-5', dict(effective_keylen=64)), 52 | ('0000000000000000', '1a807d272bbe5db1', '88bca90e90875a7f0f79c384627bafb2', 53 | 'RFC2268-6', dict(effective_keylen=64)), 54 | 55 | # 128-bit effective key length 56 | ('0000000000000000', '2269552ab0f85ca6', '88bca90e90875a7f0f79c384627bafb2', 57 | "RFC2268-7", dict(effective_keylen=128)), 58 | ('0000000000000000', '5b78d3a43dfff1f1', 59 | '88bca90e90875a7f0f79c384627bafb216f80a6f85920584c42fceb0be255daf1e', 60 | "RFC2268-8", dict(effective_keylen=129)), 61 | 62 | # Test vectors from PyCryptoPlus 2.0.1's testdata.py 63 | # 1024-bit effective key length 64 | ('0000000000000000', '624fb3e887419e48', '5068696c6970476c617373', 65 | 'PCTv201-0',dict(effective_keylen=1024)), 66 | ('ffffffffffffffff', '79cadef44c4a5a85', '5068696c6970476c617373', 67 | 'PCTv201-1',dict(effective_keylen=1024)), 68 | ('0001020304050607', '90411525b34e4c2c', '5068696c6970476c617373', 69 | 'PCTv201-2',dict(effective_keylen=1024)), 70 | ('0011223344556677', '078656aaba61cbfb', '5068696c6970476c617373', 71 | 'PCTv201-3',dict(effective_keylen=1024)), 72 | ('0000000000000000', 'd7bcc5dbb4d6e56a', 'ffffffffffffffff', 'PCTv201-4',dict(effective_keylen=1024)), 73 | ('ffffffffffffffff', '7259018ec557b357', 'ffffffffffffffff', 'PCTv201-5',dict(effective_keylen=1024)), 74 | ('0001020304050607', '93d20a497f2ccb62', 'ffffffffffffffff', 'PCTv201-6',dict(effective_keylen=1024)), 75 | ('0011223344556677', 'cb15a7f819c0014d', 'ffffffffffffffff', 'PCTv201-7',dict(effective_keylen=1024)), 76 | ('0000000000000000', '63ac98cdf3843a7a', 77 | 'ffffffffffffffff5065746572477265656e6177617953e5ffe553', 78 | 'PCTv201-8',dict(effective_keylen=1024)), 79 | ('ffffffffffffffff', '3fb49e2fa12371dd', 80 | 'ffffffffffffffff5065746572477265656e6177617953e5ffe553', 81 | 'PCTv201-9',dict(effective_keylen=1024)), 82 | ('0001020304050607', '46414781ab387d5f', 83 | 'ffffffffffffffff5065746572477265656e6177617953e5ffe553', 84 | 'PCTv201-10',dict(effective_keylen=1024)), 85 | ('0011223344556677', 'be09dc81feaca271', 86 | 'ffffffffffffffff5065746572477265656e6177617953e5ffe553', 87 | 'PCTv201-11',dict(effective_keylen=1024)), 88 | ('0000000000000000', 'e64221e608be30ab', '53e5ffe553', 'PCTv201-12',dict(effective_keylen=1024)), 89 | ('ffffffffffffffff', '862bc60fdcd4d9a9', '53e5ffe553', 'PCTv201-13',dict(effective_keylen=1024)), 90 | ('0001020304050607', '6a34da50fa5e47de', '53e5ffe553', 'PCTv201-14',dict(effective_keylen=1024)), 91 | ('0011223344556677', '584644c34503122c', '53e5ffe553', 'PCTv201-15',dict(effective_keylen=1024)), 92 | ] 93 | 94 | def get_tests(): 95 | from CryptoPlus.Cipher import ARC2 96 | from common import make_block_tests 97 | return make_block_tests(ARC2, "ARC2", test_data) 98 | 99 | if __name__ == '__main__': 100 | import unittest 101 | suite = lambda: unittest.TestSuite(get_tests()) 102 | unittest.main(defaultTest='suite') 103 | 104 | # vim:set ts=4 sw=4 sts=4 expandtab: 105 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_ARC4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/ARC4.py: Self-test for the Alleged-RC4 cipher 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.ARC4""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (plaintext, ciphertext, key[, description]) tuples. 34 | test_data = [ 35 | # Test vectors from Eric Rescorla's message with the subject 36 | # "RC4 compatibility testing", sent to the cipherpunks mailing list on 37 | # September 13, 1994. 38 | # http://cypherpunks.venona.com/date/1994/09/msg00420.html 39 | 40 | ('0123456789abcdef', '75b7878099e0c596', '0123456789abcdef', 41 | 'Test vector 0'), 42 | 43 | ('0000000000000000', '7494c2e7104b0879', '0123456789abcdef', 44 | 'Test vector 1'), 45 | 46 | ('0000000000000000', 'de188941a3375d3a', '0000000000000000', 47 | 'Test vector 2'), 48 | 49 | ('00000000000000000000', 'd6a141a7ec3c38dfbd61', 'ef012345', 50 | 'Test vector 3'), 51 | 52 | ('01' * 512, 53 | '7595c3e6114a09780c4ad452338e1ffd9a1be9498f813d76533449b6778dcad8' 54 | + 'c78a8d2ba9ac66085d0e53d59c26c2d1c490c1ebbe0ce66d1b6b1b13b6b919b8' 55 | + '47c25a91447a95e75e4ef16779cde8bf0a95850e32af9689444fd377108f98fd' 56 | + 'cbd4e726567500990bcc7e0ca3c4aaa304a387d20f3b8fbbcd42a1bd311d7a43' 57 | + '03dda5ab078896ae80c18b0af66dff319616eb784e495ad2ce90d7f772a81747' 58 | + 'b65f62093b1e0db9e5ba532fafec47508323e671327df9444432cb7367cec82f' 59 | + '5d44c0d00b67d650a075cd4b70dedd77eb9b10231b6b5b741347396d62897421' 60 | + 'd43df9b42e446e358e9c11a9b2184ecbef0cd8e7a877ef968f1390ec9b3d35a5' 61 | + '585cb009290e2fcde7b5ec66d9084be44055a619d9dd7fc3166f9487f7cb2729' 62 | + '12426445998514c15d53a18c864ce3a2b7555793988126520eacf2e3066e230c' 63 | + '91bee4dd5304f5fd0405b35bd99c73135d3d9bc335ee049ef69b3867bf2d7bd1' 64 | + 'eaa595d8bfc0066ff8d31509eb0c6caa006c807a623ef84c3d33c195d23ee320' 65 | + 'c40de0558157c822d4b8c569d849aed59d4e0fd7f379586b4b7ff684ed6a189f' 66 | + '7486d49b9c4bad9ba24b96abf924372c8a8fffb10d55354900a77a3db5f205e1' 67 | + 'b99fcd8660863a159ad4abe40fa48934163ddde542a6585540fd683cbfd8c00f' 68 | + '12129a284deacc4cdefe58be7137541c047126c8d49e2755ab181ab7e940b0c0', 69 | '0123456789abcdef', 70 | "Test vector 4"), 71 | ] 72 | 73 | def get_tests(): 74 | from CryptoPlus.Cipher import ARC4 75 | from common import make_stream_tests 76 | return make_stream_tests(ARC4, "ARC4", test_data) 77 | 78 | if __name__ == '__main__': 79 | import unittest 80 | suite = lambda: unittest.TestSuite(get_tests()) 81 | unittest.main(defaultTest='suite') 82 | 83 | # vim:set ts=4 sw=4 sts=4 expandtab: 84 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_Blowfish.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/test_Blowfish.py: Self-test for the Blowfish cipher 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.Blowfish""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (plaintext, ciphertext, key) tuples. 34 | test_data = [ 35 | # Test vectors from http://www.schneier.com/code/vectors.txt 36 | ('0000000000000000', '4ef997456198dd78', '0000000000000000'), 37 | ('ffffffffffffffff', '51866fd5b85ecb8a', 'ffffffffffffffff'), 38 | ('1000000000000001', '7d856f9a613063f2', '3000000000000000'), 39 | ('1111111111111111', '2466dd878b963c9d', '1111111111111111'), 40 | ('1111111111111111', '61f9c3802281b096', '0123456789abcdef'), 41 | ('0123456789abcdef', '7d0cc630afda1ec7', '1111111111111111'), 42 | ('0000000000000000', '4ef997456198dd78', '0000000000000000'), 43 | ('0123456789abcdef', '0aceab0fc6a0a28d', 'fedcba9876543210'), 44 | ('01a1d6d039776742', '59c68245eb05282b', '7ca110454a1a6e57'), 45 | ('5cd54ca83def57da', 'b1b8cc0b250f09a0', '0131d9619dc1376e'), 46 | ('0248d43806f67172', '1730e5778bea1da4', '07a1133e4a0b2686'), 47 | ('51454b582ddf440a', 'a25e7856cf2651eb', '3849674c2602319e'), 48 | ('42fd443059577fa2', '353882b109ce8f1a', '04b915ba43feb5b6'), 49 | ('059b5e0851cf143a', '48f4d0884c379918', '0113b970fd34f2ce'), 50 | ('0756d8e0774761d2', '432193b78951fc98', '0170f175468fb5e6'), 51 | ('762514b829bf486a', '13f04154d69d1ae5', '43297fad38e373fe'), 52 | ('3bdd119049372802', '2eedda93ffd39c79', '07a7137045da2a16'), 53 | ('26955f6835af609a', 'd887e0393c2da6e3', '04689104c2fd3b2f'), 54 | ('164d5e404f275232', '5f99d04f5b163969', '37d06bb516cb7546'), 55 | ('6b056e18759f5cca', '4a057a3b24d3977b', '1f08260d1ac2465e'), 56 | ('004bd6ef09176062', '452031c1e4fada8e', '584023641aba6176'), 57 | ('480d39006ee762f2', '7555ae39f59b87bd', '025816164629b007'), 58 | ('437540c8698f3cfa', '53c55f9cb49fc019', '49793ebc79b3258f'), 59 | ('072d43a077075292', '7a8e7bfa937e89a3', '4fb05e1515ab73a7'), 60 | ('02fe55778117f12a', 'cf9c5d7a4986adb5', '49e95d6d4ca229bf'), 61 | ('1d9d5c5018f728c2', 'd1abb290658bc778', '018310dc409b26d6'), 62 | ('305532286d6f295a', '55cb3774d13ef201', '1c587f1c13924fef'), 63 | ('0123456789abcdef', 'fa34ec4847b268b2', '0101010101010101'), 64 | ('0123456789abcdef', 'a790795108ea3cae', '1f1f1f1f0e0e0e0e'), 65 | ('0123456789abcdef', 'c39e072d9fac631d', 'e0fee0fef1fef1fe'), 66 | ('ffffffffffffffff', '014933e0cdaff6e4', '0000000000000000'), 67 | ('0000000000000000', 'f21e9a77b71c49bc', 'ffffffffffffffff'), 68 | ('0000000000000000', '245946885754369a', '0123456789abcdef'), 69 | ('ffffffffffffffff', '6b5c5a9c5d9e0a5a', 'fedcba9876543210'), 70 | 71 | ('fedcba9876543210', 'f9ad597c49db005e', 'f0'), 72 | ('fedcba9876543210', 'e91d21c1d961a6d6', 'f0e1'), 73 | ('fedcba9876543210', 'e9c2b70a1bc65cf3', 'f0e1d2'), 74 | ('fedcba9876543210', 'be1e639408640f05', 'f0e1d2c3'), 75 | ('fedcba9876543210', 'b39e44481bdb1e6e', 'f0e1d2c3b4'), 76 | ('fedcba9876543210', '9457aa83b1928c0d', 'f0e1d2c3b4a5'), 77 | ('fedcba9876543210', '8bb77032f960629d', 'f0e1d2c3b4a596'), 78 | ('fedcba9876543210', 'e87a244e2cc85e82', 'f0e1d2c3b4a59687'), 79 | ('fedcba9876543210', '15750e7a4f4ec577', 'f0e1d2c3b4a5968778'), 80 | ('fedcba9876543210', '122ba70b3ab64ae0', 'f0e1d2c3b4a596877869'), 81 | ('fedcba9876543210', '3a833c9affc537f6', 'f0e1d2c3b4a5968778695a'), 82 | ('fedcba9876543210', '9409da87a90f6bf2', 'f0e1d2c3b4a5968778695a4b'), 83 | ('fedcba9876543210', '884f80625060b8b4', 'f0e1d2c3b4a5968778695a4b3c'), 84 | ('fedcba9876543210', '1f85031c19e11968', 'f0e1d2c3b4a5968778695a4b3c2d'), 85 | ('fedcba9876543210', '79d9373a714ca34f', 'f0e1d2c3b4a5968778695a4b3c2d1e'), 86 | ('fedcba9876543210', '93142887ee3be15c', 87 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f'), 88 | ('fedcba9876543210', '03429e838ce2d14b', 89 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f00'), 90 | ('fedcba9876543210', 'a4299e27469ff67b', 91 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f0011'), 92 | ('fedcba9876543210', 'afd5aed1c1bc96a8', 93 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f001122'), 94 | ('fedcba9876543210', '10851c0e3858da9f', 95 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f00112233'), 96 | ('fedcba9876543210', 'e6f51ed79b9db21f', 97 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f0011223344'), 98 | ('fedcba9876543210', '64a6e14afd36b46f', 99 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f001122334455'), 100 | ('fedcba9876543210', '80c7d7d45a5479ad', 101 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f00112233445566'), 102 | ('fedcba9876543210', '05044b62fa52d080', 103 | 'f0e1d2c3b4a5968778695a4b3c2d1e0f0011223344556677'), 104 | ] 105 | 106 | def get_tests(): 107 | from CryptoPlus.Cipher import Blowfish 108 | from common import make_block_tests 109 | return make_block_tests(Blowfish, "Blowfish", test_data) 110 | 111 | if __name__ == '__main__': 112 | import unittest 113 | suite = lambda: unittest.TestSuite(get_tests()) 114 | unittest.main(defaultTest='suite') 115 | 116 | # vim:set ts=4 sw=4 sts=4 expandtab: 117 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_CAST.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/CAST.py: Self-test for the CAST-128 (CAST5) cipher 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.CAST""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (plaintext, ciphertext, key) tuples. 34 | test_data = [ 35 | # Test vectors from RFC 2144, B.1 36 | ('0123456789abcdef', '238b4fe5847e44b2', 37 | '0123456712345678234567893456789a', 38 | '128-bit key'), 39 | 40 | ('0123456789abcdef', 'eb6a711a2c02271b', 41 | '01234567123456782345', 42 | '80-bit key'), 43 | 44 | ('0123456789abcdef', '7ac816d16e9b302e', 45 | '0123456712', 46 | '40-bit key'), 47 | ] 48 | 49 | def get_tests(): 50 | from CryptoPlus.Cipher import CAST 51 | from common import make_block_tests 52 | return make_block_tests(CAST, "CAST", test_data) 53 | 54 | if __name__ == '__main__': 55 | import unittest 56 | suite = lambda: unittest.TestSuite(get_tests()) 57 | unittest.main(defaultTest='suite') 58 | 59 | # vim:set ts=4 sw=4 sts=4 expandtab: 60 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_IDEA.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/IDEA.py: Self-test for the IDEA cipher 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.IDEA""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (plaintext, ciphertext, key) tuples. 34 | test_data = [ 35 | # Test vectors from 36 | # http://web.archive.org/web/20001006183113/http://www.it-sec.com/pdffiles/testdata.zip 37 | 38 | # Test_Cases_IDEA.txt 39 | ('d53fabbf94ff8b5f', '1d0cb2af1654820a', '729a27ed8f5c3e8baf16560d14c90b43'), 40 | ('848f836780938169', 'd7e0468226d0fc56', '729a27ed8f5c3e8baf16560d14c90b43'), 41 | ('819440ca2065d112', '264a8bba66959075', '729a27ed8f5c3e8baf16560d14c90b43'), 42 | ('6889f5647ab23d59', 'f963468b52f45d4d', '729a27ed8f5c3e8baf16560d14c90b43'), 43 | ('df8c6fc637e3dad1', '29358cc6c83828ae', '729a27ed8f5c3e8baf16560d14c90b43'), 44 | ('ac4856242b121589', '95cd92f44bacb72d', '729a27ed8f5c3e8baf16560d14c90b43'), 45 | ('cbe465f232f9d85c', 'bce24dc8d0961c44', '729a27ed8f5c3e8baf16560d14c90b43'), 46 | ('6c2e3617da2bac35', '1569e0627007b12e', '729a27ed8f5c3e8baf16560d14c90b43'), 47 | 48 | # NewTestCases.txt 49 | ('d53fabbf94ff8b5f', '1320f99bfe052804', '000027ed8f5c3e8baf16560d14c90b43'), 50 | ('848f836780938169', '4821b99f61acebb7', '000027ed8f5c3e8baf16560d14c90b43'), 51 | ('819440ca2065d112', 'c88600093b348575', '000027ed8f5c3e8baf16560d14c90b43'), 52 | ('6889f5647ab23d59', '61d5397046f99637', '000027ed8f5c3e8baf16560d14c90b43'), 53 | ('df8c6fc637e3dad1', 'ef4899b48de5907c', '000027ed8f5c3e8baf16560d14c90b43'), 54 | ('ac4856242b121589', '85c6b232294c2f27', '000027ed8f5c3e8baf16560d14c90b43'), 55 | ('cbe465f232f9d85c', 'b67ac767c0c06a55', '000027ed8f5c3e8baf16560d14c90b43'), 56 | ('6c2e3617da2bac35', 'b2229067630f7045', '000027ed8f5c3e8baf16560d14c90b43'), 57 | 58 | ('0000abbf94ff8b5f', '65861be574e1eab6', '729a27ed8f5c3e8baf16560d14c90b43'), 59 | ('848f836780938169', 'd7e0468226d0fc56', '729a27ed8f5c3e8baf16560d14c90b43'), 60 | ('819440ca2065d112', '264a8bba66959075', '729a27ed8f5c3e8baf16560d14c90b43'), 61 | ('6889f5647ab23d59', 'f963468b52f45d4d', '729a27ed8f5c3e8baf16560d14c90b43'), 62 | ('df8c6fc637e3dad1', '29358cc6c83828ae', '729a27ed8f5c3e8baf16560d14c90b43'), 63 | ('ac4856242b121589', '95cd92f44bacb72d', '729a27ed8f5c3e8baf16560d14c90b43'), 64 | ('cbe465f232f9d85c', 'bce24dc8d0961c44', '729a27ed8f5c3e8baf16560d14c90b43'), 65 | ('6c2e3617da2bac35', '1569e0627007b12e', '729a27ed8f5c3e8baf16560d14c90b43'), 66 | 67 | ('0000abbf94ff8b5f', 'cbbb2e6c05ee8c89', '000027ed8f5c3e8baf16560d14c90b43'), 68 | ('848f836780938169', '4821b99f61acebb7', '000027ed8f5c3e8baf16560d14c90b43'), 69 | ('819440ca2065d112', 'c88600093b348575', '000027ed8f5c3e8baf16560d14c90b43'), 70 | ('6889f5647ab23d59', '61d5397046f99637', '000027ed8f5c3e8baf16560d14c90b43'), 71 | ('df8c6fc637e3dad1', 'ef4899b48de5907c', '000027ed8f5c3e8baf16560d14c90b43'), 72 | ('ac4856242b121589', '85c6b232294c2f27', '000027ed8f5c3e8baf16560d14c90b43'), 73 | ('cbe465f232f9d85c', 'b67ac767c0c06a55', '000027ed8f5c3e8baf16560d14c90b43'), 74 | ('6c2e3617da2bac35', 'b2229067630f7045', '000027ed8f5c3e8baf16560d14c90b43'), 75 | ] 76 | 77 | def get_tests(): 78 | from CryptoPlus.Cipher import IDEA 79 | from common import make_block_tests 80 | return make_block_tests(IDEA, "IDEA", test_data) 81 | 82 | if __name__ == '__main__': 83 | import unittest 84 | suite = lambda: unittest.TestSuite(get_tests()) 85 | unittest.main(defaultTest='suite') 86 | 87 | # vim:set ts=4 sw=4 sts=4 expandtab: 88 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_RC5.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/RC5.py: Self-test for the RC5 cipher 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.RC5""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | from common import dict # For compatibility with Python 2.1 and 2.2 34 | 35 | # This is a list of (plaintext, ciphertext, key, description or None, extra_params) tuples. 36 | test_data = [ 37 | # Test vectors from http://theory.lcs.mit.edu/~rivest/Rivest-rc5rev.pdf 38 | # Rivest, R. L. (1994). "The RC5 Encryption Algorithm" (pdf). Proceedings 39 | # of the Second International Workshop on Fast Software Encryption (FSE) 40 | # 1994e: 86–96. 41 | ('0000000000000000', '21a5dbee154b8f6d', '00000000000000000000000000000000', 42 | "Rivest94-1", dict(word_size=32, rounds=12)), 43 | ('21a5dbee154b8f6d', 'f7c013ac5b2b8952', '915f4619be41b2516355a50110a9ce91', 44 | "Rivest94-2", dict(word_size=32, rounds=12)), 45 | ('f7c013ac5b2b8952', '2f42b3b70369fc92', '783348e75aeb0f2fd7b169bb8dc16787', 46 | "Rivest94-3", dict(word_size=32, rounds=12)), 47 | ('2f42b3b70369fc92', '65c178b284d197cc', 'dc49db1375a5584f6485b413b5f12baf', 48 | "Rivest94-4", dict(word_size=32, rounds=12)), 49 | ('65c178b284d197cc', 'eb44e415da319824', '5269f149d41ba0152497574d7f153125', 50 | "Rivest94-5", dict(word_size=32, rounds=12)), 51 | 52 | # Test vectors from RFC 2040 53 | ('0000000000000000', '7a7bba4d79111d1e', '00', 'RFC2040-1', dict(rounds=0, mode='CBC', iv='0000000000000000')), 54 | ('ffffffffffffffff', '797bba4d78111d1e', '00', 'RFC2040-2', dict(rounds=0, mode='CBC', iv='0000000000000000')), 55 | ('0000000000000000', '7a7bba4d79111d1f', '00', 'RFC2040-3', dict(rounds=0, mode='CBC', iv='0000000000000001')), 56 | ('0000000000000001', '7a7bba4d79111d1f', '00', 'RFC2040-4', dict(rounds=0, mode='CBC', iv='0000000000000000')), 57 | ('1020304050607080', '8b9ded91ce7794a6', '00', 'RFC2040-5', dict(rounds=0, mode='CBC', iv='0102030405060708')), 58 | ('0000000000000000', '2f759fe7ad86a378', '11', 'RFC2040-6', dict(rounds=1, mode='CBC', iv='0000000000000000')), 59 | ('0000000000000000', 'dca2694bf40e0788', '00', 'RFC2040-7', dict(rounds=2, mode='CBC', iv='0000000000000000')), 60 | ('0000000000000000', 'dca2694bf40e0788', '00000000', 'RFC2040-8', dict(rounds=2, mode='CBC', iv='0000000000000000')), 61 | ('0000000000000000', 'dcfe098577eca5ff', '00', 'RFC2040-9', dict(rounds=8, mode='CBC', iv='0000000000000000')), 62 | ('1020304050607080', '9646fb77638f9ca8', '00', 'RFC2040-10', dict(rounds=8, mode='CBC', iv='0102030405060708')), 63 | ('1020304050607080', 'b2b3209db6594da4', '00', 'RFC2040-11', dict(rounds=12, mode='CBC', iv='0102030405060708')), 64 | ('1020304050607080', '545f7f32a5fc3836', '00', 'RFC2040-12', dict(rounds=16, mode='CBC', iv='0102030405060708')), 65 | ('ffffffffffffffff', '8285e7c1b5bc7402', '01020304', 'RFC2040-13', dict(rounds=8, mode='CBC', iv='0000000000000000')), 66 | ('ffffffffffffffff', 'fc586f92f7080934', '01020304', 'RFC2040-14', dict(rounds=12, mode='CBC', iv='0000000000000000')), 67 | ('ffffffffffffffff', 'cf270ef9717ff7c4', '01020304', 'RFC2040-15', dict(rounds=16, mode='CBC', iv='0000000000000000')), 68 | ('ffffffffffffffff', 'e493f1c1bb4d6e8c', '0102030405060708', 'RFC2040-16', dict(rounds=12, mode='CBC', iv='0000000000000000')), 69 | ('1020304050607080', '5c4c041e0f217ac3', '0102030405060708', 'RFC2040-17', dict(rounds=8, mode='CBC', iv='0102030405060708')), 70 | ('1020304050607080', '921f12485373b4f7', '0102030405060708', 'RFC2040-18', dict(rounds=12, mode='CBC', iv='0102030405060708')), 71 | ('1020304050607080', '5ba0ca6bbe7f5fad', '0102030405060708', 'RFC2040-19', dict(rounds=16, mode='CBC', iv='0102030405060708')), 72 | ('1020304050607080', 'c533771cd0110e63', '01020304050607081020304050607080', 'RFC2040-20', dict(rounds=8, mode='CBC', iv='0102030405060708')), 73 | ('1020304050607080', '294ddb46b3278d60', '01020304050607081020304050607080', 'RFC2040-21', dict(rounds=12, mode='CBC', iv='0102030405060708')), 74 | ('1020304050607080', 'dad6bda9dfe8f7e8', '01020304050607081020304050607080', 'RFC2040-22', dict(rounds=16, mode='CBC', iv='0102030405060708')), 75 | ('ffffffffffffffff', '97e0787837ed317f', '0102030405', 'RFC2040-23', dict(rounds=12, mode='CBC', iv='0000000000000000')), 76 | ('ffffffffffffffff', '7875dbf6738c6478', '0102030405', 'RFC2040-24', dict(rounds=8, mode='CBC', iv='0000000000000000')), 77 | ('0808080808080808', '8f34c3c681c99695', '0102030405', 'RFC2040-25', dict(rounds=8, mode='CBC', iv='7875dbf6738c6478')), 78 | # ('ffffffffffffffff', '7875dbf6738c64788f34c3c681c99695', '0102030405', 'RFC2040-26', dict(rounds=8, mode='CBC-Pad', iv='0000000000000000')), 79 | ('0000000000000000', '7cb3f1df34f94811', '0102030405', 'RFC2040-27', dict(rounds=8, mode='CBC', iv='0000000000000000')), 80 | ('1122334455667701', '7fd1a023a5bba217', '0102030405', 'RFC2040-28', dict(rounds=8, mode='CBC', iv='7cb3f1df34f94811')), 81 | # ('ffffffffffffffff7875dbf6738c647811223344556677', '7875dbf6738c64787cb3f1df34f948117fd1a023a5bba217', '0102030405', 'RFC2040-29', dict(rounds=8, mode='CBC-Pad', iv='0000000000000000')), 82 | ] 83 | 84 | def get_tests(): 85 | from CryptoPlus.Cipher import RC5 86 | from common import make_block_tests 87 | return make_block_tests(RC5, "RC5", test_data) 88 | 89 | if __name__ == '__main__': 90 | import unittest 91 | suite = lambda: unittest.TestSuite(get_tests()) 92 | unittest.main(defaultTest='suite') 93 | 94 | # vim:set ts=4 sw=4 sts=4 expandtab: 95 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_XOR.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Cipher/XOR.py: Self-test for the XOR "cipher" 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Cipher.XOR""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (plaintext, ciphertext, key) tuples. 34 | test_data = [ 35 | # Test vectors written from scratch. (Nobody posts XOR test vectors on the web? How disappointing.) 36 | ('01', '01', 37 | '00', 38 | 'zero key'), 39 | 40 | ('0102040810204080', '0003050911214181', 41 | '01', 42 | '1-byte key'), 43 | 44 | ('0102040810204080', 'cda8c8a2dc8a8c2a', 45 | 'ccaa', 46 | '2-byte key'), 47 | 48 | ('ff'*64, 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0'*2, 49 | '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', 50 | '32-byte key'), 51 | 52 | # XOR truncates at 32 bytes. 53 | ('ff'*64, 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0'*2, 54 | '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f55', 55 | '33-byte key (truncated to 32 bytes)'), 56 | ] 57 | 58 | def get_tests(): 59 | from CryptoPlus.Cipher import XOR 60 | from common import make_stream_tests 61 | return make_stream_tests(XOR, "XOR", test_data) 62 | 63 | if __name__ == '__main__': 64 | import unittest 65 | suite = lambda: unittest.TestSuite(get_tests()) 66 | unittest.main(defaultTest='suite') 67 | 68 | # vim:set ts=4 sw=4 sts=4 expandtab: 69 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Cipher/test_python_AES.py: -------------------------------------------------------------------------------- 1 | """Self-test suite for CryptoPlus.Cipher.python_AES""" 2 | 3 | __revision__ = "$Id$" 4 | 5 | # This is a list of (plaintext, ciphertext, key) tuples. 6 | # TODO: add CTR test vectors 7 | test_data = [ 8 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 9 | '7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7', 10 | '2b7e151628aed2a6abf7158809cf4f3c', 11 | 'CBC 1', 12 | {'mode':'CBC','iv': '000102030405060708090a0b0c0d0e0f'}), 13 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 14 | '4f021db243bc633d7178183a9fa071e8b4d9ada9ad7dedf4e5e738763f69145a571b242012fb7ae07fa9baac3df102e008b0e27988598881d920a9e64f5615cd', 15 | '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b', 16 | 'CBC 2', 17 | {'mode':'CBC','iv': '000102030405060708090a0b0c0d0e0f'}), 18 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 19 | 'f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461b2eb05e2c39be9fcda6c19078c6a9d1b', 20 | '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', 21 | 'CBC 3', 22 | {'mode':'CBC','iv': '000102030405060708090a0b0c0d0e0f'}), 23 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 24 | '3b3fd92eb72dad20333449f8e83cfb4ac8a64537a0b3a93fcde3cdad9f1ce58b26751f67a3cbb140b1808cf187a4f4dfc04b05357c5d1c0eeac4c66f9ff7f2e6', 25 | '2b7e151628aed2a6abf7158809cf4f3c', 26 | 'CFB 1', 27 | {'iv': '000102030405060708090a0b0c0d0e0f', 'mode': 'CFB'}), 28 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 29 | 'cdc80d6fddf18cab34c25909c99a417467ce7f7f81173621961a2b70171d3d7a2e1e8a1dd59b88b1c8e60fed1efac4c9c05f9f9ca9834fa042ae8fba584b09ff', 30 | '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b', 31 | 'CFB 2', 32 | {'iv': '000102030405060708090a0b0c0d0e0f', 'mode': 'CFB'}), 33 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 34 | 'dc7e84bfda79164b7ecd8486985d386039ffed143b28b1c832113c6331e5407bdf10132415e54b92a13ed0a8267ae2f975a385741ab9cef82031623d55b1e471', 35 | '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', 36 | 'CFB 3', 37 | {'iv': '000102030405060708090a0b0c0d0e0f', 'mode': 'CFB'}), 38 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 39 | '3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e', 40 | '2b7e151628aed2a6abf7158809cf4f3c', 41 | 'OFB 1', 42 | {'iv': '000102030405060708090a0b0c0d0e0f', 'mode': 'OFB'}), 43 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 44 | 'cdc80d6fddf18cab34c25909c99a4174fcc28b8d4c63837c09e81700c11004018d9a9aeac0f6596f559c6d4daf59a5f26d9f200857ca6c3e9cac524bd9acc92a', 45 | '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b', 46 | 'OFB 2', 47 | {'iv': '000102030405060708090a0b0c0d0e0f', 'mode': 'OFB'}), 48 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 49 | 'dc7e84bfda79164b7ecd8486985d38604febdc6740d20b3ac88f6ad82a4fb08d71ab47a086e86eedf39d1c5bba97c4080126141d67f37be8538f5a8be740e484', 50 | '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', 51 | 'OFB 3', 52 | {'iv': '000102030405060708090a0b0c0d0e0f', 'mode': 'OFB'}), 53 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 54 | '874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee', 55 | '2b7e151628aed2a6abf7158809cf4f3c', 56 | 'CTR 1', 57 | {'counter': "Crypto.Util.util.Counter('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff')", 58 | 'mode': 'CTR'}), 59 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 60 | '1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e941e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050', 61 | '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b', 62 | 'CTR 2', 63 | {'counter': "Crypto.Util.util.Counter('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff')", 64 | 'mode': 'CTR'}), 65 | ('6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710', 66 | '601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6', 67 | '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4', 68 | 'CTR 3', 69 | {'counter': "Crypto.Util.util.Counter('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff')", 70 | 'mode': 'CTR'}) 71 | ] 72 | 73 | def get_tests(): 74 | from CryptoPlus.Cipher import python_AES 75 | from common import make_block_tests 76 | return make_block_tests(python_AES, "python_AES", test_data) 77 | 78 | if __name__ == '__main__': 79 | import unittest 80 | suite = lambda: unittest.TestSuite(get_tests()) 81 | unittest.main(defaultTest='suite') 82 | 83 | #CONVERSION OLD TEST VECTORS: 84 | #CFB example: 85 | #for i in range(1,len(dict_cbc_aes)/4 + 1): 86 | # test.append((dict_cfb_aes['msg%i'%i],dict_cfb_aes['cip%i'%i],dict_cfb_aes['key%i'%i],"CFB %i"%i,dict(mode='CFB',iv=dict_cfb_aes['iv%i'%i]))) 87 | 88 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/__init__.py: Self-test for hash modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for hash modules""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | def get_tests(): 34 | tests = [] 35 | import test_HMAC; tests += test_HMAC.get_tests() 36 | import test_MD2; tests += test_MD2.get_tests() 37 | import test_MD4; tests += test_MD4.get_tests() 38 | import test_MD5; tests += test_MD5.get_tests() 39 | import test_RIPEMD; tests += test_RIPEMD.get_tests() 40 | import test_SHA; tests += test_SHA.get_tests() 41 | import test_SHA256; tests += test_SHA256.get_tests() 42 | return tests 43 | 44 | if __name__ == '__main__': 45 | import unittest 46 | suite = lambda: unittest.TestSuite(get_tests()) 47 | unittest.main(defaultTest='suite') 48 | 49 | # vim:set ts=4 sw=4 sts=4 expandtab: 50 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/common.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/common.py: Common code for CryptoPlus.SelfTest.Hash 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-testing for PyCryptoPlus hash modules""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import sys 34 | import unittest 35 | import binascii 36 | import string 37 | 38 | # For compatibility with Python 2.1 and Python 2.2 39 | if sys.hexversion < 0x02030000: 40 | # Python 2.1 doesn't have a dict() function 41 | # Python 2.2 dict() function raises TypeError if you do dict(MD5='blah') 42 | def dict(**kwargs): 43 | return kwargs.copy() 44 | else: 45 | dict = __builtins__['dict'] 46 | 47 | 48 | class HashSelfTest(unittest.TestCase): 49 | 50 | def __init__(self, hashmod, description, expected, input): 51 | unittest.TestCase.__init__(self) 52 | self.hashmod = hashmod 53 | self.expected = expected 54 | self.input = input 55 | self.description = description 56 | 57 | def shortDescription(self): 58 | return self.description 59 | 60 | def runTest(self): 61 | h = self.hashmod.new() 62 | h.update(self.input) 63 | 64 | out1 = binascii.b2a_hex(h.digest()) 65 | out2 = h.hexdigest() 66 | 67 | h = self.hashmod.new(self.input) 68 | 69 | out3 = h.hexdigest() 70 | out4 = binascii.b2a_hex(h.digest()) 71 | 72 | self.assertEqual(self.expected, out1) 73 | self.assertEqual(self.expected, out2) 74 | self.assertEqual(self.expected, out3) 75 | self.assertEqual(self.expected, out4) 76 | 77 | class MACSelfTest(unittest.TestCase): 78 | 79 | def __init__(self, hashmod, description, expected_dict, input, key, hashmods): 80 | unittest.TestCase.__init__(self) 81 | self.hashmod = hashmod 82 | self.expected_dict = expected_dict 83 | self.input = input 84 | self.key = key 85 | self.hashmods = hashmods 86 | self.description = description 87 | 88 | def shortDescription(self): 89 | return self.description 90 | 91 | def runTest(self): 92 | for hashname in self.expected_dict.keys(): 93 | hashmod = self.hashmods[hashname] 94 | key = binascii.a2b_hex(self.key) 95 | data = binascii.a2b_hex(self.input) 96 | 97 | # Strip whitespace from the expected string (which should be in lowercase-hex) 98 | expected = self.expected_dict[hashname] 99 | for ch in string.whitespace: 100 | expected = expected.replace(ch, "") 101 | 102 | h = self.hashmod.new(key, digestmod=hashmod) 103 | h.update(data) 104 | out1 = binascii.b2a_hex(h.digest()) 105 | out2 = h.hexdigest() 106 | 107 | h = self.hashmod.new(key, data, hashmod) 108 | 109 | out3 = h.hexdigest() 110 | out4 = binascii.b2a_hex(h.digest()) 111 | 112 | # Test .copy() 113 | h2 = h.copy() 114 | h.update("blah blah blah") # Corrupt the original hash object 115 | out5 = binascii.b2a_hex(h2.digest()) # The copied hash object should return the correct result 116 | 117 | self.assertEqual(expected, out1) 118 | self.assertEqual(expected, out2) 119 | self.assertEqual(expected, out3) 120 | self.assertEqual(expected, out4) 121 | self.assertEqual(expected, out5) 122 | 123 | def make_hash_tests(module, module_name, test_data): 124 | tests = [] 125 | for i in range(len(test_data)): 126 | row = test_data[i] 127 | if len(row) < 3: 128 | (expected, input) = row 129 | description = repr(input) 130 | else: 131 | (expected, input, description) = row 132 | name = "%s #%d: %s" % (module_name, i+1, description) 133 | tests.append(HashSelfTest(module, name, expected, input)) 134 | return tests 135 | 136 | def make_mac_tests(module, module_name, test_data, hashmods): 137 | tests = [] 138 | for i in range(len(test_data)): 139 | row = test_data[i] 140 | (key, data, results, description) = row 141 | name = "%s #%d: %s" % (module_name, i+1, description) 142 | tests.append(MACSelfTest(module, name, results, data, key, hashmods)) 143 | return tests 144 | 145 | # vim:set ts=4 sw=4 sts=4 expandtab: 146 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_HMAC.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/HMAC.py: Self-test for the HMAC module 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Hash.HMAC""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | from common import dict # For compatibility with Python 2.1 and 2.2 34 | 35 | # This is a list of (key, data, results, description) tuples. 36 | test_data = [ 37 | ## Test vectors from RFC 2202 ## 38 | # Test that the default hashmod is MD5 39 | ('0b' * 16, 40 | '4869205468657265', 41 | dict(default='9294727a3638bb1c13f48ef8158bfc9d'), 42 | 'default-is-MD5'), 43 | 44 | # Test case 1 (MD5) 45 | ('0b' * 16, 46 | '4869205468657265', 47 | dict(MD5='9294727a3638bb1c13f48ef8158bfc9d'), 48 | 'RFC 2202 #1-MD5 (HMAC-MD5)'), 49 | 50 | # Test case 1 (SHA1) 51 | ('0b' * 20, 52 | '4869205468657265', 53 | dict(SHA1='b617318655057264e28bc0b6fb378c8ef146be00'), 54 | 'RFC 2202 #1-SHA1 (HMAC-SHA1)'), 55 | 56 | # Test case 2 57 | ('4a656665', 58 | '7768617420646f2079612077616e7420666f72206e6f7468696e673f', 59 | dict(MD5='750c783e6ab0b503eaa86e310a5db738', 60 | SHA1='effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'), 61 | 'RFC 2202 #2 (HMAC-MD5/SHA1)'), 62 | 63 | # Test case 3 (MD5) 64 | ('aa' * 16, 65 | 'dd' * 50, 66 | dict(MD5='56be34521d144c88dbb8c733f0e8b3f6'), 67 | 'RFC 2202 #3-MD5 (HMAC-MD5)'), 68 | 69 | # Test case 3 (SHA1) 70 | ('aa' * 20, 71 | 'dd' * 50, 72 | dict(SHA1='125d7342b9ac11cd91a39af48aa17b4f63f175d3'), 73 | 'RFC 2202 #3-SHA1 (HMAC-SHA1)'), 74 | 75 | # Test case 4 76 | ('0102030405060708090a0b0c0d0e0f10111213141516171819', 77 | 'cd' * 50, 78 | dict(MD5='697eaf0aca3a3aea3a75164746ffaa79', 79 | SHA1='4c9007f4026250c6bc8414f9bf50c86c2d7235da'), 80 | 'RFC 2202 #4 (HMAC-MD5/SHA1)'), 81 | 82 | # Test case 5 (MD5) 83 | ('0c' * 16, 84 | '546573742057697468205472756e636174696f6e', 85 | dict(MD5='56461ef2342edc00f9bab995690efd4c'), 86 | 'RFC 2202 #5-MD5 (HMAC-MD5)'), 87 | 88 | # Test case 5 (SHA1) 89 | # NB: We do not implement hash truncation, so we only test the full hash here. 90 | ('0c' * 20, 91 | '546573742057697468205472756e636174696f6e', 92 | dict(SHA1='4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'), 93 | 'RFC 2202 #5-SHA1 (HMAC-SHA1)'), 94 | 95 | # Test case 6 96 | ('aa' * 80, 97 | '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a' 98 | + '65204b6579202d2048617368204b6579204669727374', 99 | dict(MD5='6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', 100 | SHA1='aa4ae5e15272d00e95705637ce8a3b55ed402112'), 101 | 'RFC 2202 #6 (HMAC-MD5/SHA1)'), 102 | 103 | # Test case 7 104 | ('aa' * 80, 105 | '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a' 106 | + '65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d' 107 | + '53697a652044617461', 108 | dict(MD5='6f630fad67cda0ee1fb1f562db3aa53e', 109 | SHA1='e8e99d0f45237d786d6bbaa7965c7808bbff1a91'), 110 | 'RFC 2202 #7 (HMAC-MD5/SHA1)'), 111 | 112 | ## Test vectors from RFC 4231 ## 113 | # 4.2. Test Case 1 114 | ('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 115 | '4869205468657265', 116 | dict(SHA256=''' 117 | b0344c61d8db38535ca8afceaf0bf12b 118 | 881dc200c9833da726e9376c2e32cff7 119 | '''), 120 | 'RFC 4231 #1 (HMAC-SHA256)'), 121 | 122 | # 4.3. Test Case 2 - Test with a key shorter than the length of the HMAC 123 | # output. 124 | ('4a656665', 125 | '7768617420646f2079612077616e7420666f72206e6f7468696e673f', 126 | dict(SHA256=''' 127 | 5bdcc146bf60754e6a042426089575c7 128 | 5a003f089d2739839dec58b964ec3843 129 | '''), 130 | 'RFC 4231 #2 (HMAC-SHA256)'), 131 | 132 | # 4.4. Test Case 3 - Test with a combined length of key and data that is 133 | # larger than 64 bytes (= block-size of SHA-224 and SHA-256). 134 | ('aa' * 20, 135 | 'dd' * 50, 136 | dict(SHA256=''' 137 | 773ea91e36800e46854db8ebd09181a7 138 | 2959098b3ef8c122d9635514ced565fe 139 | '''), 140 | 'RFC 4231 #3 (HMAC-SHA256)'), 141 | 142 | # 4.5. Test Case 4 - Test with a combined length of key and data that is 143 | # larger than 64 bytes (= block-size of SHA-224 and SHA-256). 144 | ('0102030405060708090a0b0c0d0e0f10111213141516171819', 145 | 'cd' * 50, 146 | dict(SHA256=''' 147 | 82558a389a443c0ea4cc819899f2083a 148 | 85f0faa3e578f8077a2e3ff46729665b 149 | '''), 150 | 'RFC 4231 #4 (HMAC-SHA256)'), 151 | 152 | # 4.6. Test Case 5 - Test with a truncation of output to 128 bits. 153 | # 154 | # Not included because we do not implement hash truncation. 155 | # 156 | 157 | # 4.7. Test Case 6 - Test with a key larger than 128 bytes (= block-size of 158 | # SHA-384 and SHA-512). 159 | ('aa' * 131, 160 | '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a' 161 | + '65204b6579202d2048617368204b6579204669727374', 162 | dict(SHA256=''' 163 | 60e431591ee0b67f0d8a26aacbf5b77f 164 | 8e0bc6213728c5140546040f0ee37f54 165 | '''), 166 | 'RFC 4231 #6 (HMAC-SHA256)'), 167 | 168 | # 4.8. Test Case 7 - Test with a key and data that is larger than 128 bytes 169 | # (= block-size of SHA-384 and SHA-512). 170 | ('aa' * 131, 171 | '5468697320697320612074657374207573696e672061206c6172676572207468' 172 | + '616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074' 173 | + '68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565' 174 | + '647320746f20626520686173686564206265666f7265206265696e6720757365' 175 | + '642062792074686520484d414320616c676f726974686d2e', 176 | dict(SHA256=''' 177 | 9b09ffa71b942fcb27635fbcd5b0e944 178 | bfdc63644f0713938a7f51535c3a35e2 179 | '''), 180 | 'RFC 4231 #7 (HMAC-SHA256)'), 181 | ] 182 | 183 | def get_tests(): 184 | from CryptoPlus.Hash import HMAC, MD5, SHA as SHA1, SHA256 185 | from common import make_mac_tests 186 | hashmods = dict(MD5=MD5, SHA1=SHA1, SHA256=SHA256, default=None) 187 | return make_mac_tests(HMAC, "HMAC", test_data, hashmods) 188 | 189 | if __name__ == '__main__': 190 | import unittest 191 | suite = lambda: unittest.TestSuite(get_tests()) 192 | unittest.main(defaultTest='suite') 193 | 194 | # vim:set ts=4 sw=4 sts=4 expandtab: 195 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_MD2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/MD2.py: Self-test for the MD2 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Hash.MD2""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (expected_result, input[, description]) tuples. 34 | test_data = [ 35 | # Test vectors from RFC 1319 36 | ('8350e5a3e24c153df2275c9f80692773', '', "'' (empty string)"), 37 | ('32ec01ec4a6dac72c0ab96fb34c0b5d1', 'a'), 38 | ('da853b0d3f88d99b30283a69e6ded6bb', 'abc'), 39 | ('ab4f496bfb2a530b219ff33031fe06b0', 'message digest'), 40 | 41 | ('4e8ddff3650292ab5a4108c3aa47940b', 'abcdefghijklmnopqrstuvwxyz', 42 | 'a-z'), 43 | 44 | ('da33def2a42df13975352846c30338cd', 45 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 46 | 'A-Z, a-z, 0-9'), 47 | 48 | ('d5976f79d83d3a0dc9806c3c66f3efd8', 49 | '1234567890123456789012345678901234567890123456' 50 | + '7890123456789012345678901234567890', 51 | "'1234567890' * 8"), 52 | ] 53 | 54 | def get_tests(): 55 | from CryptoPlus.Hash import MD2 56 | from common import make_hash_tests 57 | return make_hash_tests(MD2, "MD2", test_data) 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_MD4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/MD4.py: Self-test for the MD4 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Hash.MD4""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (expected_result, input[, description]) tuples. 34 | test_data = [ 35 | # Test vectors from RFC 1320 36 | ('31d6cfe0d16ae931b73c59d7e0c089c0', '', "'' (empty string)"), 37 | ('bde52cb31de33e46245e05fbdbd6fb24', 'a'), 38 | ('a448017aaf21d8525fc10ae87aa6729d', 'abc'), 39 | ('d9130a8164549fe818874806e1c7014b', 'message digest'), 40 | 41 | ('d79e1c308aa5bbcdeea8ed63df412da9', 'abcdefghijklmnopqrstuvwxyz', 42 | 'a-z'), 43 | 44 | ('043f8582f241db351ce627e153e7f0e4', 45 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 46 | 'A-Z, a-z, 0-9'), 47 | 48 | ('e33b4ddc9c38f2199c3e7b164fcc0536', 49 | '1234567890123456789012345678901234567890123456' 50 | + '7890123456789012345678901234567890', 51 | "'1234567890' * 8"), 52 | ] 53 | 54 | def get_tests(): 55 | from CryptoPlus.Hash import MD4 56 | from common import make_hash_tests 57 | return make_hash_tests(MD4, "MD4", test_data) 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_MD5.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/MD5.py: Self-test for the MD5 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Hash.MD5""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (expected_result, input[, description]) tuples. 34 | test_data = [ 35 | # Test vectors from RFC 1321 36 | ('d41d8cd98f00b204e9800998ecf8427e', '', "'' (empty string)"), 37 | ('0cc175b9c0f1b6a831c399e269772661', 'a'), 38 | ('900150983cd24fb0d6963f7d28e17f72', 'abc'), 39 | ('f96b697d7cb7938d525a2f31aaf161d0', 'message digest'), 40 | 41 | ('c3fcd3d76192e4007dfb496cca67e13b', 'abcdefghijklmnopqrstuvwxyz', 42 | 'a-z'), 43 | 44 | ('d174ab98d277d9f5a5611c2c9f419d9f', 45 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 46 | 'A-Z, a-z, 0-9'), 47 | 48 | ('57edf4a22be3c955ac49da2e2107b67a', 49 | '1234567890123456789012345678901234567890123456' 50 | + '7890123456789012345678901234567890', 51 | "'1234567890' * 8"), 52 | ] 53 | 54 | def get_tests(): 55 | from CryptoPlus.Hash import MD5 56 | from common import make_hash_tests 57 | return make_hash_tests(MD5, "MD5", test_data) 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_RIPEMD.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/test_RIPEMD.py: Self-test for the RIPEMD-160 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | #"""Self-test suite for CryptoPlus.Hash.RIPEMD""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (expected_result, input[, description]) tuples. 34 | test_data = [ 35 | # Test vectors downloaded 2008-09-12 from 36 | # http://homes.esat.kuleuven.be/~bosselae/ripemd160.html 37 | ('9c1185a5c5e9fc54612808977ee8f548b2258d31', '', "'' (empty string)"), 38 | ('0bdc9d2d256b3ee9daae347be6f4dc835a467ffe', 'a'), 39 | ('8eb208f7e05d987a9b044a8e98c6b087f15a0bfc', 'abc'), 40 | ('5d0689ef49d2fae572b881b123a85ffa21595f36', 'message digest'), 41 | 42 | ('f71c27109c692c1b56bbdceb5b9d2865b3708dbc', 43 | 'abcdefghijklmnopqrstuvwxyz', 44 | 'a-z'), 45 | 46 | ('12a053384a9c0c88e405a06c27dcf49ada62eb2b', 47 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', 48 | 'abcdbcd...pnopq'), 49 | 50 | ('b0e20b6e3116640286ed3a87a5713079b21f5189', 51 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 52 | 'A-Z, a-z, 0-9'), 53 | 54 | ('9b752e45573d4b39f4dbd3323cab82bf63326bfb', 55 | '1234567890' * 8, 56 | "'1234567890' * 8"), 57 | 58 | ('52783243c1697bdbe16d37f97f68f08325dc1528', 59 | 'a' * 10**6, 60 | '"a" * 10**6'), 61 | ] 62 | 63 | def get_tests(): 64 | from CryptoPlus.Hash import RIPEMD 65 | from common import make_hash_tests 66 | return make_hash_tests(RIPEMD, "RIPEMD", test_data) 67 | 68 | if __name__ == '__main__': 69 | import unittest 70 | suite = lambda: unittest.TestSuite(get_tests()) 71 | unittest.main(defaultTest='suite') 72 | 73 | # vim:set ts=4 sw=4 sts=4 expandtab: 74 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_SHA.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/SHA.py: Self-test for the SHA-1 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Hash.SHA""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # Test vectors from various sources 34 | # This is a list of (expected_result, input[, description]) tuples. 35 | test_data = [ 36 | # FIPS PUB 180-2, A.1 - "One-Block Message" 37 | ('a9993e364706816aba3e25717850c26c9cd0d89d', 'abc'), 38 | 39 | # FIPS PUB 180-2, A.2 - "Multi-Block Message" 40 | ('84983e441c3bd26ebaae4aa1f95129e5e54670f1', 41 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), 42 | 43 | # FIPS PUB 180-2, A.3 - "Long Message" 44 | # ('34aa973cd4c4daa4f61eeb2bdbad27316534016f', 45 | # 'a' * 10**6, 46 | # '"a" * 10**6'), 47 | 48 | # RFC 3174: Section 7.3, "TEST4" (multiple of 512 bits) 49 | ('dea356a2cddd90c7a7ecedc5ebb563934f460452', 50 | "01234567" * 80, 51 | '"01234567" * 80'), 52 | ] 53 | 54 | def get_tests(): 55 | from CryptoPlus.Hash import SHA 56 | from common import make_hash_tests 57 | return make_hash_tests(SHA, "SHA", test_data) 58 | 59 | if __name__ == '__main__': 60 | import unittest 61 | suite = lambda: unittest.TestSuite(get_tests()) 62 | unittest.main(defaultTest='suite') 63 | 64 | # vim:set ts=4 sw=4 sts=4 expandtab: 65 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Hash/test_SHA256.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Hash/SHA256.py: Self-test for the SHA-256 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHA256LL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Hash.SHA256""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # Test vectors from FIPS PUB 180-2 34 | # This is a list of (expected_result, input[, description]) tuples. 35 | test_data = [ 36 | # FIPS PUB 180-2, B.1 - "One-Block Message" 37 | ('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 38 | 'abc'), 39 | 40 | # FIPS PUB 180-2, B.2 - "Multi-Block Message" 41 | ('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1', 42 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'), 43 | 44 | # FIPS PUB 180-2, B.3 - "Long Message" 45 | # ('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0', 46 | # 'a' * 10**6, 47 | # '"a" * 10**6'), 48 | 49 | # Test for an old PyCryptoPlus bug. 50 | ('f7fd017a3c721ce7ff03f3552c0813adcc48b7f33f07e5e2ba71e23ea393d103', 51 | 'This message is precisely 55 bytes long, to test a bug.', 52 | 'Length = 55 (mod 64)'), 53 | ] 54 | 55 | def get_tests(): 56 | from CryptoPlus.Hash import SHA256 57 | from common import make_hash_tests 58 | return make_hash_tests(SHA256, "SHA256", test_data) 59 | 60 | if __name__ == '__main__': 61 | import unittest 62 | suite = lambda: unittest.TestSuite(get_tests()) 63 | unittest.main(defaultTest='suite') 64 | 65 | # vim:set ts=4 sw=4 sts=4 expandtab: 66 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/PublicKey/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/PublicKey/__init__.py: Self-test for public key crypto 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for public-key crypto""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import os 34 | 35 | def get_tests(): 36 | tests = [] 37 | import test_RSA; tests += test_RSA.get_tests() 38 | return tests 39 | 40 | if __name__ == '__main__': 41 | import unittest 42 | suite = lambda: unittest.TestSuite(get_tests()) 43 | unittest.main(defaultTest='suite') 44 | 45 | # vim:set ts=4 sw=4 sts=4 expandtab: 46 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/Fortuna.new: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/doegox/python-cryptoplus/a5a1f8aecce4ddf476b2d80b586822d9e91eeb7d/src/CryptoPlus/SelfTest/Random/Fortuna.new -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/Fortuna/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/Fortuna/__init__.py: Self-test for Fortuna modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for the CryptoPlus.Random.Fortuna package""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import os 34 | 35 | def get_tests(): 36 | tests = [] 37 | import test_FortunaAccumulator; tests += test_FortunaAccumulator.get_tests() 38 | import test_FortunaGenerator; tests += test_FortunaGenerator.get_tests() 39 | import test_SHAd256; tests += test_SHAd256.get_tests() 40 | return tests 41 | 42 | if __name__ == '__main__': 43 | import unittest 44 | suite = lambda: unittest.TestSuite(get_tests()) 45 | unittest.main(defaultTest='suite') 46 | 47 | 48 | # vim:set ts=4 sw=4 sts=4 expandtab: 49 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/Fortuna/test_FortunaGenerator.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/Fortuna/test_FortunaGenerator.py: Self-test for the FortunaGenerator module 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-tests for CryptoPlus.Random.Fortuna.FortunaGenerator""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | from CryptoPlus.Util.python_compat import * 34 | 35 | import unittest 36 | from binascii import b2a_hex 37 | 38 | class FortunaGeneratorTests(unittest.TestCase): 39 | def setUp(self): 40 | global FortunaGenerator 41 | from CryptoPlus.Random.Fortuna import FortunaGenerator 42 | 43 | def test_generator(self): 44 | """FortunaGenerator.AESGenerator""" 45 | fg = FortunaGenerator.AESGenerator() 46 | 47 | # We shouldn't be able to read data until we've seeded the generator 48 | self.assertRaises(Exception, fg.pseudo_random_data, 1) 49 | self.assertEqual(0, fg.counter.get_value()) 50 | 51 | # Seed the generator, which should set the key and increment the counter. 52 | fg.reseed("Hello") 53 | self.assertEqual("0ea6919d4361551364242a4ba890f8f073676e82cf1a52bb880f7e496648b565", b2a_hex(fg.key)) 54 | self.assertEqual(1, fg.counter.get_value()) 55 | 56 | # Read 2 full blocks from the generator 57 | self.assertEqual("7cbe2c17684ac223d08969ee8b565616" + # counter=1 58 | "717661c0d2f4758bd6ba140bf3791abd", # counter=2 59 | b2a_hex(fg.pseudo_random_data(32))) 60 | 61 | # Meanwhile, the generator will have re-keyed itself and incremented its counter 62 | self.assertEqual("33a1bb21987859caf2bbfc5615bef56d" + # counter=3 63 | "e6b71ff9f37112d0c193a135160862b7", # counter=4 64 | b2a_hex(fg.key)) 65 | self.assertEqual(5, fg.counter.get_value()) 66 | 67 | # Read another 2 blocks from the generator 68 | self.assertEqual("fd6648ba3086e919cee34904ef09a7ff" + # counter=5 69 | "021f77580558b8c3e9248275f23042bf", # counter=6 70 | b2a_hex(fg.pseudo_random_data(32))) 71 | 72 | 73 | # Try to read more than 2**20 bytes using the internal function. This should fail. 74 | self.assertRaises(AssertionError, fg._pseudo_random_data, 2**20+1) 75 | 76 | def get_tests(): 77 | from CryptoPlus.SelfTest.st_common import list_test_cases 78 | return list_test_cases(FortunaGeneratorTests) 79 | 80 | if __name__ == '__main__': 81 | suite = lambda: unittest.TestSuite(get_tests()) 82 | unittest.main(defaultTest='suite') 83 | 84 | # vim:set ts=4 sw=4 sts=4 expandtab: 85 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/Fortuna/test_SHAd256.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/Fortuna/test_SHAd256.py: Self-test for the SHAd256 hash function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.Fortuna.SHAd256""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | # This is a list of (expected_result, input[, description]) tuples. 34 | test_data = [ 35 | # I could not find any test vectors for SHAd256, so I made these vectors by 36 | # feeding some sample data into several plain SHA256 implementations 37 | # (including OpenSSL, the "sha256sum" tool, and this implementation). 38 | # This is a subset of the resulting test vectors. The complete list can be 39 | # found at: http://www.dlitz.net/crypto/shad256-test-vectors/ 40 | ('5df6e0e2761359d30a8275058e299fcc0381534545f55cf43e41983f5d4c9456', 41 | '', "'' (empty string)"), 42 | ('4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358', 43 | 'abc'), 44 | ('0cffe17f68954dac3a84fb1458bd5ec99209449749b2b308b7cb55812f9563af', 45 | 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') 46 | ] 47 | 48 | def get_tests(): 49 | from CryptoPlus.Random.Fortuna import SHAd256 50 | from CryptoPlus.SelfTest.Hash.common import make_hash_tests 51 | return make_hash_tests(SHAd256, "SHAd256", test_data) 52 | 53 | if __name__ == '__main__': 54 | import unittest 55 | suite = lambda: unittest.TestSuite(get_tests()) 56 | unittest.main(defaultTest='suite') 57 | 58 | # vim:set ts=4 sw=4 sts=4 expandtab: 59 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG.new: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/doegox/python-cryptoplus/a5a1f8aecce4ddf476b2d80b586822d9e91eeb7d/src/CryptoPlus/SelfTest/Random/OSRNG.new -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/OSRNG/__init__.py: Self-test for OSRNG modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for CryptoPlus.Random.OSRNG package""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import os 34 | 35 | def get_tests(): 36 | tests = [] 37 | if os.name == 'nt': 38 | import test_nt; tests += test_nt.get_tests() 39 | import test_winrandom; tests += test_winrandom.get_tests() 40 | elif os.name == 'posix': 41 | import test_posix; tests += test_posix.get_tests() 42 | if hasattr(os, 'urandom'): 43 | import test_fallback; tests += test_fallback.get_tests() 44 | import test_generic; tests += test_generic.get_tests() 45 | return tests 46 | 47 | if __name__ == '__main__': 48 | import unittest 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | 53 | # vim:set ts=4 sw=4 sts=4 expandtab: 54 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG/test_fallback.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_fallback.py: Self-test for the OSRNG.fallback.new() function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.OSRNG.fallback""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.OSRNG.fallback.new()""" 38 | # Import the OSRNG.nt module and try to use it 39 | import CryptoPlus.Random.OSRNG.fallback 40 | randobj = CryptoPlus.Random.OSRNG.fallback.new() 41 | x = randobj.read(16) 42 | y = randobj.read(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [SimpleTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG/test_generic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_generic.py: Self-test for the OSRNG.new() function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.OSRNG""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.OSRNG.new()""" 38 | # Import the OSRNG module and try to use it 39 | import CryptoPlus.Random.OSRNG 40 | randobj = CryptoPlus.Random.OSRNG.new() 41 | x = randobj.read(16) 42 | y = randobj.read(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [SimpleTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG/test_nt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_generic.py: Self-test for the OSRNG.nt.new() function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.OSRNG.nt""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.OSRNG.nt.new()""" 38 | # Import the OSRNG.nt module and try to use it 39 | import CryptoPlus.Random.OSRNG.nt 40 | randobj = CryptoPlus.Random.OSRNG.nt.new() 41 | x = randobj.read(16) 42 | y = randobj.read(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [SimpleTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG/test_posix.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_posix.py: Self-test for the OSRNG.posix.new() function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.OSRNG.posix""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.OSRNG.posix.new()""" 38 | # Import the OSRNG.nt module and try to use it 39 | import CryptoPlus.Random.OSRNG.posix 40 | randobj = CryptoPlus.Random.OSRNG.posix.new() 41 | x = randobj.read(16) 42 | y = randobj.read(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [SimpleTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/OSRNG/test_winrandom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_winrandom.py: Self-test for the winrandom module 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.OSRNG.winrandom""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.OSRNG.winrandom""" 38 | # Import the winrandom module and try to use it 39 | from CryptoPlus.Random.OSRNG import winrandom 40 | randobj = winrandom.new() 41 | x = randobj.get_bytes(16) 42 | y = randobj.get_bytes(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [SimpleTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Random/__init__.py: Self-test for random number generation modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for random number generators""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | def get_tests(): 34 | tests = [] 35 | import Fortuna; tests += Fortuna.get_tests() 36 | import OSRNG; tests += OSRNG.get_tests() 37 | import test_random; tests += test_random.get_tests() 38 | import test_rpoolcompat; tests += test_rpoolcompat.get_tests() 39 | return tests 40 | 41 | if __name__ == '__main__': 42 | import unittest 43 | suite = lambda: unittest.TestSuite(get_tests()) 44 | unittest.main(defaultTest='suite') 45 | 46 | # vim:set ts=4 sw=4 sts=4 expandtab: 47 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/test_random.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_generic.py: Self-test for the CryptoPlus.Random.new() function 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Random.new()""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.new()""" 38 | # Import the OSRNG module and try to use it 39 | from CryptoPlus import Random 40 | randobj = Random.new() 41 | x = randobj.read(16) 42 | y = randobj.read(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [SimpleTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Random/test_rpoolcompat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_winrandom.py: Self-test for the winrandom module 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for the CryptoPlus.Random.RandomPoolCompat class""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class SimpleTest(unittest.TestCase): 36 | def runTest(self): 37 | """CryptoPlus.Random.RandomPoolCompat""" 38 | # Import the winrandom module and try to use it 39 | from CryptoPlus.Random import RandomPoolCompat 40 | rpool = RandomPoolCompat() 41 | x = rpool.get_bytes(16) 42 | y = rpool.get_bytes(16) 43 | self.assertNotEqual(x, y) 44 | self.assertNotEqual(rpool.entropy, 0) 45 | 46 | rpool.randomize() 47 | rpool.stir('foo') 48 | rpool.add_event('foo') 49 | 50 | def get_tests(): 51 | return [SimpleTest()] 52 | 53 | if __name__ == '__main__': 54 | suite = lambda: unittest.TestSuite(get_tests()) 55 | unittest.main(defaultTest='suite') 56 | 57 | # vim:set ts=4 sw=4 sts=4 expandtab: 58 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Util/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/__init__.py: Self-test for utility modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test for utility modules""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import os 34 | 35 | def get_tests(): 36 | tests = [] 37 | if os.name == 'nt': 38 | import test_winrandom; tests += test_winrandom.get_tests() 39 | import test_number; tests += test_number.get_tests() 40 | return tests 41 | 42 | if __name__ == '__main__': 43 | import unittest 44 | suite = lambda: unittest.TestSuite(get_tests()) 45 | unittest.main(defaultTest='suite') 46 | 47 | # vim:set ts=4 sw=4 sts=4 expandtab: 48 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/Util/test_winrandom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/Util/test_winrandom.py: Self-test for the winrandom module 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self-test suite for CryptoPlus.Util.winrandom""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | 35 | class WinRandomImportTest(unittest.TestCase): 36 | def runTest(self): 37 | """winrandom: simple test""" 38 | # Import the winrandom module and try to use it 39 | from CryptoPlus.Util import winrandom 40 | randobj = winrandom.new() 41 | x = randobj.get_bytes(16) 42 | y = randobj.get_bytes(16) 43 | self.assertNotEqual(x, y) 44 | 45 | def get_tests(): 46 | return [WinRandomImportTest()] 47 | 48 | if __name__ == '__main__': 49 | suite = lambda: unittest.TestSuite(get_tests()) 50 | unittest.main(defaultTest='suite') 51 | 52 | # vim:set ts=4 sw=4 sts=4 expandtab: 53 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/__init__.py: Self-test for PyCryptoPlus 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Self tests 30 | 31 | These tests should perform quickly and can ideally be used every time an 32 | application runs. 33 | """ 34 | __revision__ = "$Id$" 35 | 36 | #import st_common 37 | 38 | #__all__ = ["st_common"] 39 | 40 | import sys 41 | import unittest 42 | import StringIO 43 | 44 | class SelfTestError(Exception): 45 | def __init__(self, message, result): 46 | Exception.__init__(self, message, result) 47 | self.message = message 48 | self.result = result 49 | 50 | def run(module=None, verbosity=0, stream=None, **kwargs): 51 | """Execute self-tests. 52 | 53 | This raises SelfTestError if any test is unsuccessful. 54 | 55 | You may optionally pass in a sub-module of SelfTest if you only want to 56 | perform some of the tests. For example, the following would test only the 57 | hash modules: 58 | 59 | CryptoPlus.SelfTest.run(CryptoPlus.SelfTest.Hash) 60 | 61 | """ 62 | suite = unittest.TestSuite() 63 | if module is None: 64 | suite.addTests(get_tests()) 65 | else: 66 | suite.addTests(module.get_tests()) 67 | if stream is None: 68 | kwargs['stream'] = StringIO.StringIO() 69 | runner = unittest.TextTestRunner(verbosity=verbosity, **kwargs) 70 | result = runner.run(suite) 71 | if not result.wasSuccessful(): 72 | if stream is None: 73 | sys.stderr.write(stream.getvalue()) 74 | raise SelfTestError("Self-test failed", result) 75 | return result 76 | 77 | def get_tests(): 78 | tests = [] 79 | import Cipher; tests += Cipher.get_tests() 80 | import Hash; tests += Hash.get_tests() 81 | import PublicKey; tests += PublicKey.get_tests() 82 | # import Random; tests += Random.get_tests() 83 | # import Util; tests += Util.get_tests() 84 | return tests 85 | 86 | if __name__ == '__main__': 87 | suite = lambda: unittest.TestSuite(get_tests()) 88 | unittest.main(defaultTest='suite') 89 | 90 | # vim:set ts=4 sw=4 sts=4 expandtab: 91 | -------------------------------------------------------------------------------- /src/CryptoPlus/SelfTest/st_common.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # SelfTest/st_common.py: Common functions for SelfTest modules 4 | # 5 | # ======================================================================= 6 | # Copyright (C) 2008 Dwayne C. Litzenberger 7 | # 8 | # Permission is hereby granted, free of charge, to any person obtaining 9 | # a copy of this software and associated documentation files (the 10 | # "Software"), to deal in the Software without restriction, including 11 | # without limitation the rights to use, copy, modify, merge, publish, 12 | # distribute, sublicense, and/or sell copies of the Software, and to 13 | # permit persons to whom the Software is furnished to do so. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | # ======================================================================= 27 | # 28 | 29 | """Common functions for SelfTest modules""" 30 | 31 | __revision__ = "$Id$" 32 | 33 | import unittest 34 | import string 35 | import binascii 36 | 37 | class _list_testloader(unittest.TestLoader): 38 | suiteClass = list 39 | 40 | def list_test_cases(class_): 41 | """Return a list of TestCase instances given a TestCase class 42 | 43 | This is useful when you have defined test* methods on your TestCase class. 44 | """ 45 | return _list_testloader().loadTestsFromTestCase(class_) 46 | 47 | def strip_whitespace(s): 48 | """Remove whitespace from a string""" 49 | table = string.maketrans(string.whitespace, " " * len(string.whitespace)) 50 | s = s.translate(table).replace(" ", "") 51 | return s 52 | 53 | def a2b_hex(s): 54 | """Convert hexadecimal to binary, ignoring whitespace""" 55 | return binascii.a2b_hex(strip_whitespace(s)) 56 | 57 | def b2a_hex(s): 58 | """Convert binary to hexadecimal""" 59 | # For completeness 60 | return binascii.b2a_hex(s) 61 | 62 | # vim:set ts=4 sw=4 sts=4 expandtab: 63 | -------------------------------------------------------------------------------- /src/CryptoPlus/Util/RFC1751.py: -------------------------------------------------------------------------------- 1 | from Crypto.Util.RFC1751 import * 2 | -------------------------------------------------------------------------------- /src/CryptoPlus/Util/__init__.py: -------------------------------------------------------------------------------- 1 | """Util initialization 2 | 3 | makes the Util modules from Crypto AND CryptoPlus available here 4 | """ 5 | #import Crypto 6 | #from Crypto.Util import number, randpool, RFC1751 7 | import padding, util, python_compat, number, randpool, RFC1751 8 | 9 | from pkg_resources import parse_version 10 | 11 | __all__ = ["padding","util","number","randpool","RFC1751","python_compat"] 12 | 13 | #if parse_version(Crypto.__version__) > parse_version("2.0.1"): 14 | # from Crypto.Util import python_compat 15 | # __all__.append("python_compat") 16 | 17 | #del Crypto 18 | -------------------------------------------------------------------------------- /src/CryptoPlus/Util/number.py: -------------------------------------------------------------------------------- 1 | from Crypto.Util.number import * 2 | -------------------------------------------------------------------------------- /src/CryptoPlus/Util/python_compat.py: -------------------------------------------------------------------------------- 1 | from pkg_resources import parse_version 2 | import Crypto 3 | 4 | if parse_version(Crypto.__version__) > parse_version("2.0.1"): 5 | del Crypto 6 | try: 7 | from Crypto.Util.python_compat import * 8 | except: 9 | from Crypto.Util.py21compat import * 10 | -------------------------------------------------------------------------------- /src/CryptoPlus/Util/randpool.py: -------------------------------------------------------------------------------- 1 | from Crypto.Util.randpool import * 2 | -------------------------------------------------------------------------------- /src/CryptoPlus/Util/util.py: -------------------------------------------------------------------------------- 1 | def number2string(i): 2 | """Convert a number to a string 3 | 4 | Input: long or integer 5 | Output: string (big-endian) 6 | """ 7 | s=hex(i)[2:].rstrip('L') 8 | if len(s) % 2: 9 | s = '0' + s 10 | return s.decode('hex') 11 | 12 | def number2string_N(i, N): 13 | """Convert a number to a string of fixed size 14 | 15 | i: long or integer 16 | N: length of string 17 | Output: string (big-endian) 18 | """ 19 | s = '%0*x' % (N*2, i) 20 | return s.decode('hex') 21 | 22 | def string2number(i): 23 | """ Convert a string to a number 24 | 25 | Input: string (big-endian) 26 | Output: long or integer 27 | """ 28 | return int(i.encode('hex'),16) 29 | 30 | def xorstring(a,b): 31 | """XOR two strings of same length 32 | 33 | For more complex cases, see CryptoPlus.Cipher.XOR""" 34 | assert len(a) == len(b) 35 | return number2string_N(string2number(a)^string2number(b), len(a)) 36 | 37 | class Counter(str): 38 | #found here: http://www.lag.net/pipermail/paramiko/2008-February.txt 39 | """Necessary for CTR chaining mode 40 | 41 | Initializing a counter object (ctr = Counter('xxx'), gives a value to the counter object. 42 | Everytime the object is called ( ctr() ) it returns the current value and increments it by 1. 43 | Input/output is a raw string. 44 | 45 | Counter value is big endian""" 46 | def __init__(self, initial_ctr): 47 | if not isinstance(initial_ctr, str): 48 | raise TypeError("nonce must be str") 49 | self.c = int(initial_ctr.encode('hex'), 16) 50 | def __call__(self): 51 | # This might be slow, but it works as a demonstration 52 | ctr = ("%032x" % (self.c,)).decode('hex') 53 | self.c += 1 54 | return ctr 55 | 56 | -------------------------------------------------------------------------------- /src/CryptoPlus/__init__.py: -------------------------------------------------------------------------------- 1 | from pkg_resources import parse_version 2 | import Crypto 3 | __all__ = ["Cipher","PublicKey","Util","Protocol","Hash","testvectors","SelfTest"] 4 | 5 | if parse_version(Crypto.__version__) > parse_version("2.0.1"): 6 | __all__.append("Random") 7 | 8 | #del parse_version 9 | #del Crypto 10 | -------------------------------------------------------------------------------- /test/test_doctests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import unittest 4 | import doctest 5 | from pkg_resources import require 6 | require("CryptoPlus>=1.0") 7 | #import CryptoPlus.Cipher.python_AES 8 | from CryptoPlus.Cipher import python_AES, AES, python_DES, DES, python_DES3,\ 9 | DES3, python_Blowfish, Blowfish, python_Twofish, python_Serpent,\ 10 | python_Rijndael, CAST, ARC2, python_PRESENT 11 | from CryptoPlus.Util import padding 12 | from CryptoPlus.Hash import python_RadioGatun, python_PBKDF2, RIPEMD, python_MD5,\ 13 | python_SHA,python_SHA256,python_SHA224,python_SHA384,python_SHA512,\ 14 | python_whirlpool 15 | try: 16 | from CryptoPlus.Cipher import IDEA 17 | from CryptoPlus.Cipher import RC5 18 | import_error = 0 19 | except ImportError: 20 | import_error = 1 21 | 22 | suite = unittest.TestSuite() 23 | #for mod in (CryptoPlus.Cipher.python_AES,CryptoPlus.Cipher.python_AES): 24 | for mod in python_AES, AES, python_DES, DES, python_DES3, DES3, python_Blowfish,\ 25 | Blowfish, python_Twofish, python_Serpent, python_Rijndael, CAST, ARC2,\ 26 | python_PRESENT, padding, python_RadioGatun, python_PBKDF2, RIPEMD,\ 27 | python_MD5, python_SHA,python_SHA256,python_SHA224,python_SHA384,python_SHA512,\ 28 | python_whirlpool: 29 | suite.addTest(doctest.DocTestSuite(mod)) 30 | if not import_error: 31 | suite.addTest(doctest.DocTestSuite(IDEA)) 32 | suite.addTest(doctest.DocTestSuite(RC5)) 33 | runner = unittest.TextTestRunner(verbosity=2) 34 | runner.run(suite) 35 | 36 | --------------------------------------------------------------------------------