1
2
3
4 """OpenSSL/M2Crypto 3DES implementation."""
5
6 from .cryptomath import *
7 from .tripledes import *
8
9 if m2cryptoLoaded:
10
11 - def new(key, mode, IV):
13
15
17 TripleDES.__init__(self, key, mode, IV, "openssl")
18 self.key = key
19 self.IV = IV
20
21 - def _createContext(self, encrypt):
22 context = m2.cipher_ctx_new()
23 cipherType = m2.des_ede3_cbc()
24 m2.cipher_init(context, cipherType, self.key, self.IV, encrypt)
25 return context
26
28 TripleDES.encrypt(self, plaintext)
29 context = self._createContext(1)
30 ciphertext = m2.cipher_update(context, plaintext)
31 m2.cipher_ctx_free(context)
32 self.IV = ciphertext[-self.block_size:]
33 return bytearray(ciphertext)
34
36 TripleDES.decrypt(self, ciphertext)
37 context = self._createContext(0)
38
39
40 plaintext = m2.cipher_update(context, ciphertext+('\0'*16))
41
42
43
44 plaintext = plaintext[:len(ciphertext)]
45 m2.cipher_ctx_free(context)
46 self.IV = ciphertext[-self.block_size:]
47 return bytearray(plaintext)
48