Package tlslite :: Package utils :: Module cipherfactory
[hide private]
[frames] | no frames]

Source Code for Module tlslite.utils.cipherfactory

  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   
  6  import os 
  7   
  8  from tlslite.utils import python_aes 
  9  from tlslite.utils import python_aesgcm 
 10  from tlslite.utils import python_rc4 
 11   
 12  from tlslite.utils import cryptomath 
 13   
 14  tripleDESPresent = False 
 15   
 16  if cryptomath.m2cryptoLoaded: 
 17      from tlslite.utils import openssl_aes 
 18      from tlslite.utils import openssl_rc4 
 19      from tlslite.utils import openssl_tripledes 
 20      tripleDESPresent = True 
 21   
 22  if cryptomath.pycryptoLoaded: 
 23      from tlslite.utils import pycrypto_aes 
 24      from tlslite.utils import pycrypto_aesgcm 
 25      from tlslite.utils import pycrypto_rc4 
 26      from tlslite.utils import pycrypto_tripledes 
 27      tripleDESPresent = True 
 28   
 29  # ************************************************************************** 
 30  # Factory Functions for AES 
 31  # ************************************************************************** 
 32   
33 -def createAES(key, IV, implList=None):
34 """Create a new AES object. 35 36 @type key: str 37 @param key: A 16, 24, or 32 byte string. 38 39 @type IV: str 40 @param IV: A 16 byte string 41 42 @rtype: L{tlslite.utils.AES} 43 @return: An AES object. 44 """ 45 if implList is None: 46 implList = ["openssl", "pycrypto", "python"] 47 48 for impl in implList: 49 if impl == "openssl" and cryptomath.m2cryptoLoaded: 50 return openssl_aes.new(key, 2, IV) 51 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 52 return pycrypto_aes.new(key, 2, IV) 53 elif impl == "python": 54 return python_aes.new(key, 2, IV) 55 raise NotImplementedError()
56
57 -def createAESGCM(key, implList=None):
58 """Create a new AESGCM object. 59 60 @type key: bytearray 61 @param key: A 16 or 32 byte byte array. 62 63 @rtype: L{tlslite.utils.AESGCM} 64 @return: An AESGCM object. 65 """ 66 if implList is None: 67 implList = ["pycrypto", "python"] 68 69 for impl in implList: 70 if impl == "pycrypto" and cryptomath.pycryptoLoaded: 71 return pycrypto_aesgcm.new(key) 72 if impl == "python": 73 return python_aesgcm.new(key) 74 raise NotImplementedError()
75
76 -def createRC4(key, IV, implList=None):
77 """Create a new RC4 object. 78 79 @type key: str 80 @param key: A 16 to 32 byte string. 81 82 @type IV: object 83 @param IV: Ignored, whatever it is. 84 85 @rtype: L{tlslite.utils.RC4} 86 @return: An RC4 object. 87 """ 88 if implList is None: 89 implList = ["openssl", "pycrypto", "python"] 90 91 if len(IV) != 0: 92 raise AssertionError() 93 for impl in implList: 94 if impl == "openssl" and cryptomath.m2cryptoLoaded: 95 return openssl_rc4.new(key) 96 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 97 return pycrypto_rc4.new(key) 98 elif impl == "python": 99 return python_rc4.new(key) 100 raise NotImplementedError()
101 102 #Create a new TripleDES instance
103 -def createTripleDES(key, IV, implList=None):
104 """Create a new 3DES object. 105 106 @type key: str 107 @param key: A 24 byte string. 108 109 @type IV: str 110 @param IV: An 8 byte string 111 112 @rtype: L{tlslite.utils.TripleDES} 113 @return: A 3DES object. 114 """ 115 if implList is None: 116 implList = ["openssl", "pycrypto"] 117 118 for impl in implList: 119 if impl == "openssl" and cryptomath.m2cryptoLoaded: 120 return openssl_tripledes.new(key, 2, IV) 121 elif impl == "pycrypto" and cryptomath.pycryptoLoaded: 122 return pycrypto_tripledes.new(key, 2, IV) 123 raise NotImplementedError()
124