Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / tlslite / tlslite / utils / Cryptlib_TripleDES.py
1 """Cryptlib 3DES implementation."""
2
3 from cryptomath import *
4
5 from TripleDES import *
6
7 if cryptlibpyLoaded:
8
9     def new(key, mode, IV):
10         return Cryptlib_TripleDES(key, mode, IV)
11
12     class Cryptlib_TripleDES(TripleDES):
13
14         def __init__(self, key, mode, IV):
15             TripleDES.__init__(self, key, mode, IV, "cryptlib")
16             self.context = cryptlib_py.cryptCreateContext(cryptlib_py.CRYPT_UNUSED, cryptlib_py.CRYPT_ALGO_3DES)
17             cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINFO_MODE, cryptlib_py.CRYPT_MODE_CBC)
18             cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINFO_KEYSIZE, len(key))
19             cryptlib_py.cryptSetAttributeString(self.context, cryptlib_py.CRYPT_CTXINFO_KEY, key)
20             cryptlib_py.cryptSetAttributeString(self.context, cryptlib_py.CRYPT_CTXINFO_IV, IV)
21
22         def __del__(self):
23              cryptlib_py.cryptDestroyContext(self.context)
24
25         def encrypt(self, plaintext):
26             TripleDES.encrypt(self, plaintext)
27             bytes = stringToBytes(plaintext)
28             cryptlib_py.cryptEncrypt(self.context, bytes)
29             return bytesToString(bytes)
30
31         def decrypt(self, ciphertext):
32             TripleDES.decrypt(self, ciphertext)
33             bytes = stringToBytes(ciphertext)
34             cryptlib_py.cryptDecrypt(self.context, bytes)
35             return bytesToString(bytes)