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