1# Author: Trevor Perrin 2# See the LICENSE file for legal information regarding use of this file. 3 4"""Factory functions for symmetric cryptography.""" 5 6import os 7 8from tlslite.utils import python_aes 9from tlslite.utils import python_rc4 10 11from tlslite.utils import cryptomath 12 13tripleDESPresent = False 14 15if cryptomath.m2cryptoLoaded: 16 from tlslite.utils import openssl_aes 17 from tlslite.utils import openssl_rc4 18 from tlslite.utils import openssl_tripledes 19 tripleDESPresent = True 20 21if cryptomath.pycryptoLoaded: 22 from tlslite.utils import pycrypto_aes 23 from tlslite.utils import pycrypto_rc4 24 from tlslite.utils import pycrypto_tripledes 25 tripleDESPresent = True 26 27# ************************************************************************** 28# Factory Functions for AES 29# ************************************************************************** 30 31def createAES(key, IV, implList=None): 32 """Create a new AES object. 33 34 @type key: str 35 @param key: A 16, 24, or 32 byte string. 36 37 @type IV: str 38 @param IV: A 16 byte string 39 40 @rtype: L{tlslite.utils.AES} 41 @return: An AES object. 42 """ 43 if implList == None: 44 implList = ["openssl", "pycrypto", "python"] 45 46 for impl in implList: 47 if impl == "openssl" and cryptomath.m2cryptoLoaded: 48 return openssl_aes.new(key, 2, IV) 49 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 50 return pycrypto_aes.new(key, 2, IV) 51 elif impl == "python": 52 return python_aes.new(key, 2, IV) 53 raise NotImplementedError() 54 55def createRC4(key, IV, implList=None): 56 """Create a new RC4 object. 57 58 @type key: str 59 @param key: A 16 to 32 byte string. 60 61 @type IV: object 62 @param IV: Ignored, whatever it is. 63 64 @rtype: L{tlslite.utils.RC4} 65 @return: An RC4 object. 66 """ 67 if implList == None: 68 implList = ["openssl", "pycrypto", "python"] 69 70 if len(IV) != 0: 71 raise AssertionError() 72 for impl in implList: 73 if impl == "openssl" and cryptomath.m2cryptoLoaded: 74 return openssl_rc4.new(key) 75 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 76 return pycrypto_rc4.new(key) 77 elif impl == "python": 78 return python_rc4.new(key) 79 raise NotImplementedError() 80 81#Create a new TripleDES instance 82def createTripleDES(key, IV, implList=None): 83 """Create a new 3DES object. 84 85 @type key: str 86 @param key: A 24 byte string. 87 88 @type IV: str 89 @param IV: An 8 byte string 90 91 @rtype: L{tlslite.utils.TripleDES} 92 @return: A 3DES object. 93 """ 94 if implList == None: 95 implList = ["openssl", "pycrypto"] 96 97 for impl in implList: 98 if impl == "openssl" and cryptomath.m2cryptoLoaded: 99 return openssl_tripledes.new(key, 2, IV) 100 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 101 return pycrypto_tripledes.new(key, 2, IV) 102 raise NotImplementedError()