summaryrefslogtreecommitdiffstats
path: root/src/lib/tlslite/utils/cipherfactory.py
blob: ccbb6b5ff9b7f71c7850dc6d650df4a4bc49bc90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Factory functions for symmetric cryptography."""

import os

import Python_AES
import Python_RC4

import cryptomath

tripleDESPresent = False

if cryptomath.m2cryptoLoaded:
    import OpenSSL_AES
    import OpenSSL_RC4
    import OpenSSL_TripleDES
    tripleDESPresent = True

if cryptomath.cryptlibpyLoaded:
    import Cryptlib_AES
    import Cryptlib_RC4
    import Cryptlib_TripleDES
    tripleDESPresent = True

if cryptomath.pycryptoLoaded:
    import PyCrypto_AES
    import PyCrypto_RC4
    import PyCrypto_TripleDES
    tripleDESPresent = True

# **************************************************************************
# Factory Functions for AES
# **************************************************************************

def createAES(key, IV, implList=None):
    """Create a new AES object.

    @type key: str
    @param key: A 16, 24, or 32 byte string.

    @type IV: str
    @param IV: A 16 byte string

    @rtype: L{tlslite.utils.AES}
    @return: An AES object.
    """
    if implList == None:
        implList = ["cryptlib", "openssl", "pycrypto", "python"]

    for impl in implList:
        if impl == "cryptlib" and cryptomath.cryptlibpyLoaded:
            return Cryptlib_AES.new(key, 2, IV)
        elif impl == "openssl" and cryptomath.m2cryptoLoaded:
            return OpenSSL_AES.new(key, 2, IV)
        elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
            return PyCrypto_AES.new(key, 2, IV)
        elif impl == "python":
            return Python_AES.new(key, 2, IV)
    raise NotImplementedError()

def createRC4(key, IV, implList=None):
    """Create a new RC4 object.

    @type key: str
    @param key: A 16 to 32 byte string.

    @type IV: object
    @param IV: Ignored, whatever it is.

    @rtype: L{tlslite.utils.RC4}
    @return: An RC4 object.
    """
    if implList == None:
        implList = ["cryptlib", "openssl", "pycrypto", "python"]

    if len(IV) != 0:
        raise AssertionError()
    for impl in implList:
        if impl == "cryptlib" and cryptomath.cryptlibpyLoaded:
            return Cryptlib_RC4.new(key)
        elif impl == "openssl" and cryptomath.m2cryptoLoaded:
            return OpenSSL_RC4.new(key)
        elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
            return PyCrypto_RC4.new(key)
        elif impl == "python":
            return Python_RC4.new(key)
    raise NotImplementedError()

#Create a new TripleDES instance
def createTripleDES(key, IV, implList=None):
    """Create a new 3DES object.

    @type key: str
    @param key: A 24 byte string.

    @type IV: str
    @param IV: An 8 byte string

    @rtype: L{tlslite.utils.TripleDES}
    @return: A 3DES object.
    """
    if implList == None:
        implList = ["cryptlib", "openssl", "pycrypto"]

    for impl in implList:
        if impl == "cryptlib" and cryptomath.cryptlibpyLoaded:
           return Cryptlib_TripleDES.new(key, 2, IV)
        elif impl == "openssl" and cryptomath.m2cryptoLoaded:
            return OpenSSL_TripleDES.new(key, 2, IV)
        elif impl == "pycrypto" and cryptomath.pycryptoLoaded:
            return PyCrypto_TripleDES.new(key, 2, IV)
    raise NotImplementedError()