1
2
3
4 """Factory functions for symmetric cryptography."""
5
6 import os
7
8 from tlslite.utils import python_aes
9 from tlslite.utils import python_aesgcm
10 from tlslite.utils import python_chacha20_poly1305
11 from tlslite.utils import python_rc4
12
13 from tlslite.utils import cryptomath
14
15 tripleDESPresent = False
16
17 if cryptomath.m2cryptoLoaded:
18 from tlslite.utils import openssl_aes
19 from tlslite.utils import openssl_rc4
20 from tlslite.utils import openssl_tripledes
21 tripleDESPresent = True
22
23 if cryptomath.pycryptoLoaded:
24 from tlslite.utils import pycrypto_aes
25 from tlslite.utils import pycrypto_aesgcm
26 from tlslite.utils import pycrypto_rc4
27 from tlslite.utils import pycrypto_tripledes
28 tripleDESPresent = True
29
30
31
32
33
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 is None:
47 implList = ["openssl", "pycrypto", "python"]
48
49 for impl in implList:
50 if impl == "openssl" and cryptomath.m2cryptoLoaded:
51 return openssl_aes.new(key, 2, IV)
52 elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
53 return pycrypto_aes.new(key, 2, IV)
54 elif impl == "python":
55 return python_aes.new(key, 2, IV)
56 raise NotImplementedError()
57
59 """Create a new AESGCM object.
60
61 @type key: bytearray
62 @param key: A 16 or 32 byte byte array.
63
64 @rtype: L{tlslite.utils.AESGCM}
65 @return: An AESGCM object.
66 """
67 if implList is None:
68 implList = ["pycrypto", "python"]
69
70 for impl in implList:
71 if impl == "pycrypto" and cryptomath.pycryptoLoaded:
72 return pycrypto_aesgcm.new(key)
73 if impl == "python":
74 return python_aesgcm.new(key)
75 raise NotImplementedError()
76
78 """Create a new CHACHA20_POLY1305 object.
79
80 @type key: bytearray
81 @param key: a 32 byte array to serve as key
82
83 @rtype: L{tlslite.utils.CHACHA20_POLY1305}
84 @return: A ChaCha20/Poly1305 object
85 """
86 if implList is None:
87 implList = ["python"]
88
89 for impl in implList:
90 if impl == "python":
91 return python_chacha20_poly1305.new(key)
92 raise NotImplementedError()
93
95 """Create a new RC4 object.
96
97 @type key: str
98 @param key: A 16 to 32 byte string.
99
100 @type IV: object
101 @param IV: Ignored, whatever it is.
102
103 @rtype: L{tlslite.utils.RC4}
104 @return: An RC4 object.
105 """
106 if implList is None:
107 implList = ["openssl", "pycrypto", "python"]
108
109 if len(IV) != 0:
110 raise AssertionError()
111 for impl in implList:
112 if impl == "openssl" and cryptomath.m2cryptoLoaded:
113 return openssl_rc4.new(key)
114 elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
115 return pycrypto_rc4.new(key)
116 elif impl == "python":
117 return python_rc4.new(key)
118 raise NotImplementedError()
119
120
122 """Create a new 3DES object.
123
124 @type key: str
125 @param key: A 24 byte string.
126
127 @type IV: str
128 @param IV: An 8 byte string
129
130 @rtype: L{tlslite.utils.TripleDES}
131 @return: A 3DES object.
132 """
133 if implList is None:
134 implList = ["openssl", "pycrypto"]
135
136 for impl in implList:
137 if impl == "openssl" and cryptomath.m2cryptoLoaded:
138 return openssl_tripledes.new(key, 2, IV)
139 elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
140 return pycrypto_tripledes.new(key, 2, IV)
141 raise NotImplementedError()
142