/gdata/tlslite/utils/cipherfactory.py
Python | 111 lines | 59 code | 14 blank | 38 comment | 25 complexity | 690be0bd657f6979b0b87ad8e4c96d0a MD5 | raw file
1"""Factory functions for symmetric cryptography.""" 2 3import os 4 5import Python_AES 6import Python_RC4 7 8import cryptomath 9 10tripleDESPresent = False 11 12if cryptomath.m2cryptoLoaded: 13 import OpenSSL_AES 14 import OpenSSL_RC4 15 import OpenSSL_TripleDES 16 tripleDESPresent = True 17 18if cryptomath.cryptlibpyLoaded: 19 import Cryptlib_AES 20 import Cryptlib_RC4 21 import Cryptlib_TripleDES 22 tripleDESPresent = True 23 24if cryptomath.pycryptoLoaded: 25 import PyCrypto_AES 26 import PyCrypto_RC4 27 import PyCrypto_TripleDES 28 tripleDESPresent = True 29 30# ************************************************************************** 31# Factory Functions for AES 32# ************************************************************************** 33 34def createAES(key, IV, implList=None): 35 """Create a new AES object. 36 37 @type key: str 38 @param key: A 16, 24, or 32 byte string. 39 40 @type IV: str 41 @param IV: A 16 byte string 42 43 @rtype: L{tlslite.utils.AES} 44 @return: An AES object. 45 """ 46 if implList == None: 47 implList = ["cryptlib", "openssl", "pycrypto", "python"] 48 49 for impl in implList: 50 if impl == "cryptlib" and cryptomath.cryptlibpyLoaded: 51 return Cryptlib_AES.new(key, 2, IV) 52 elif impl == "openssl" and cryptomath.m2cryptoLoaded: 53 return OpenSSL_AES.new(key, 2, IV) 54 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 55 return PyCrypto_AES.new(key, 2, IV) 56 elif impl == "python": 57 return Python_AES.new(key, 2, IV) 58 raise NotImplementedError() 59 60def createRC4(key, IV, implList=None): 61 """Create a new RC4 object. 62 63 @type key: str 64 @param key: A 16 to 32 byte string. 65 66 @type IV: object 67 @param IV: Ignored, whatever it is. 68 69 @rtype: L{tlslite.utils.RC4} 70 @return: An RC4 object. 71 """ 72 if implList == None: 73 implList = ["cryptlib", "openssl", "pycrypto", "python"] 74 75 if len(IV) != 0: 76 raise AssertionError() 77 for impl in implList: 78 if impl == "cryptlib" and cryptomath.cryptlibpyLoaded: 79 return Cryptlib_RC4.new(key) 80 elif impl == "openssl" and cryptomath.m2cryptoLoaded: 81 return OpenSSL_RC4.new(key) 82 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 83 return PyCrypto_RC4.new(key) 84 elif impl == "python": 85 return Python_RC4.new(key) 86 raise NotImplementedError() 87 88#Create a new TripleDES instance 89def createTripleDES(key, IV, implList=None): 90 """Create a new 3DES object. 91 92 @type key: str 93 @param key: A 24 byte string. 94 95 @type IV: str 96 @param IV: An 8 byte string 97 98 @rtype: L{tlslite.utils.TripleDES} 99 @return: A 3DES object. 100 """ 101 if implList == None: 102 implList = ["cryptlib", "openssl", "pycrypto"] 103 104 for impl in implList: 105 if impl == "cryptlib" and cryptomath.cryptlibpyLoaded: 106 return Cryptlib_TripleDES.new(key, 2, IV) 107 elif impl == "openssl" and cryptomath.m2cryptoLoaded: 108 return OpenSSL_TripleDES.new(key, 2, IV) 109 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 110 return PyCrypto_TripleDES.new(key, 2, IV) 111 raise NotImplementedError()