1 # Author: Trevor Perrin
2 # See the LICENSE file for legal information regarding use of this file.
4 """Factory functions for symmetric cryptography."""
8 from tlslite.utils import python_aes
9 from tlslite.utils import python_rc4
11 from tlslite.utils import cryptomath
13 tripleDESPresent = False
15 if 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
21 if 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
27 # **************************************************************************
28 # Factory Functions for AES
29 # **************************************************************************
31 def createAES(key, IV, implList=None):
32 """Create a new AES object.
35 @param key: A 16, 24, or 32 byte string.
38 @param IV: A 16 byte string
40 @rtype: L{tlslite.utils.AES}
41 @return: An AES object.
44 implList = ["openssl", "pycrypto", "python"]
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()
55 def createRC4(key, IV, implList=None):
56 """Create a new RC4 object.
59 @param key: A 16 to 32 byte string.
62 @param IV: Ignored, whatever it is.
64 @rtype: L{tlslite.utils.RC4}
65 @return: An RC4 object.
68 implList = ["openssl", "pycrypto", "python"]
71 raise AssertionError()
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()
81 #Create a new TripleDES instance
82 def createTripleDES(key, IV, implList=None):
83 """Create a new 3DES object.
86 @param key: A 24 byte string.
89 @param IV: An 8 byte string
91 @rtype: L{tlslite.utils.TripleDES}
92 @return: A 3DES object.
95 implList = ["openssl", "pycrypto"]
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()