/gdata/tlslite/utils/cipherfactory.py

http://radioappz.googlecode.com/ · Python · 111 lines · 59 code · 14 blank · 38 comment · 30 complexity · 690be0bd657f6979b0b87ad8e4c96d0a MD5 · raw file

  1. """Factory functions for symmetric cryptography."""
  2. import os
  3. import Python_AES
  4. import Python_RC4
  5. import cryptomath
  6. tripleDESPresent = False
  7. if cryptomath.m2cryptoLoaded:
  8. import OpenSSL_AES
  9. import OpenSSL_RC4
  10. import OpenSSL_TripleDES
  11. tripleDESPresent = True
  12. if cryptomath.cryptlibpyLoaded:
  13. import Cryptlib_AES
  14. import Cryptlib_RC4
  15. import Cryptlib_TripleDES
  16. tripleDESPresent = True
  17. if cryptomath.pycryptoLoaded:
  18. import PyCrypto_AES
  19. import PyCrypto_RC4
  20. import PyCrypto_TripleDES
  21. tripleDESPresent = True
  22. # **************************************************************************
  23. # Factory Functions for AES
  24. # **************************************************************************
  25. def createAES(key, IV, implList=None):
  26. """Create a new AES object.
  27. @type key: str
  28. @param key: A 16, 24, or 32 byte string.
  29. @type IV: str
  30. @param IV: A 16 byte string
  31. @rtype: L{tlslite.utils.AES}
  32. @return: An AES object.
  33. """
  34. if implList == None:
  35. implList = ["cryptlib", "openssl", "pycrypto", "python"]
  36. for impl in implList:
  37. if impl == "cryptlib" and cryptomath.cryptlibpyLoaded:
  38. return Cryptlib_AES.new(key, 2, IV)
  39. elif impl == "openssl" and cryptomath.m2cryptoLoaded:
  40. return OpenSSL_AES.new(key, 2, IV)
  41. elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
  42. return PyCrypto_AES.new(key, 2, IV)
  43. elif impl == "python":
  44. return Python_AES.new(key, 2, IV)
  45. raise NotImplementedError()
  46. def createRC4(key, IV, implList=None):
  47. """Create a new RC4 object.
  48. @type key: str
  49. @param key: A 16 to 32 byte string.
  50. @type IV: object
  51. @param IV: Ignored, whatever it is.
  52. @rtype: L{tlslite.utils.RC4}
  53. @return: An RC4 object.
  54. """
  55. if implList == None:
  56. implList = ["cryptlib", "openssl", "pycrypto", "python"]
  57. if len(IV) != 0:
  58. raise AssertionError()
  59. for impl in implList:
  60. if impl == "cryptlib" and cryptomath.cryptlibpyLoaded:
  61. return Cryptlib_RC4.new(key)
  62. elif impl == "openssl" and cryptomath.m2cryptoLoaded:
  63. return OpenSSL_RC4.new(key)
  64. elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
  65. return PyCrypto_RC4.new(key)
  66. elif impl == "python":
  67. return Python_RC4.new(key)
  68. raise NotImplementedError()
  69. #Create a new TripleDES instance
  70. def createTripleDES(key, IV, implList=None):
  71. """Create a new 3DES object.
  72. @type key: str
  73. @param key: A 24 byte string.
  74. @type IV: str
  75. @param IV: An 8 byte string
  76. @rtype: L{tlslite.utils.TripleDES}
  77. @return: A 3DES object.
  78. """
  79. if implList == None:
  80. implList = ["cryptlib", "openssl", "pycrypto"]
  81. for impl in implList:
  82. if impl == "cryptlib" and cryptomath.cryptlibpyLoaded:
  83. return Cryptlib_TripleDES.new(key, 2, IV)
  84. elif impl == "openssl" and cryptomath.m2cryptoLoaded:
  85. return OpenSSL_TripleDES.new(key, 2, IV)
  86. elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
  87. return PyCrypto_TripleDES.new(key, 2, IV)
  88. raise NotImplementedError()