OLD | NEW |
| 1 # Author: Trevor Perrin |
| 2 # See the LICENSE file for legal information regarding use of this file. |
| 3 |
1 """OpenSSL/M2Crypto 3DES implementation.""" | 4 """OpenSSL/M2Crypto 3DES implementation.""" |
2 | 5 |
3 from cryptomath import * | 6 from .cryptomath import * |
4 from tripledes import * | 7 from .tripledes import * |
5 | 8 |
6 if m2cryptoLoaded: | 9 if m2cryptoLoaded: |
7 | 10 |
8 def new(key, mode, IV): | 11 def new(key, mode, IV): |
9 return OpenSSL_TripleDES(key, mode, IV) | 12 return OpenSSL_TripleDES(key, mode, IV) |
10 | 13 |
11 class OpenSSL_TripleDES(TripleDES): | 14 class OpenSSL_TripleDES(TripleDES): |
12 | 15 |
13 def __init__(self, key, mode, IV): | 16 def __init__(self, key, mode, IV): |
14 TripleDES.__init__(self, key, mode, IV, "openssl") | 17 TripleDES.__init__(self, key, mode, IV, "openssl") |
15 self.key = key | 18 self.key = key |
16 self.IV = IV | 19 self.IV = IV |
17 | 20 |
18 def _createContext(self, encrypt): | 21 def _createContext(self, encrypt): |
19 context = m2.cipher_ctx_new() | 22 context = m2.cipher_ctx_new() |
20 cipherType = m2.des_ede3_cbc() | 23 cipherType = m2.des_ede3_cbc() |
21 m2.cipher_init(context, cipherType, self.key, self.IV, encrypt) | 24 m2.cipher_init(context, cipherType, self.key, self.IV, encrypt) |
22 return context | 25 return context |
23 | 26 |
24 def encrypt(self, plaintext): | 27 def encrypt(self, plaintext): |
25 TripleDES.encrypt(self, plaintext) | 28 TripleDES.encrypt(self, plaintext) |
26 context = self._createContext(1) | 29 context = self._createContext(1) |
27 ciphertext = m2.cipher_update(context, plaintext) | 30 ciphertext = m2.cipher_update(context, plaintext) |
28 m2.cipher_ctx_free(context) | 31 m2.cipher_ctx_free(context) |
29 self.IV = ciphertext[-self.block_size:] | 32 self.IV = ciphertext[-self.block_size:] |
30 return ciphertext | 33 return bytearray(ciphertext) |
31 | 34 |
32 def decrypt(self, ciphertext): | 35 def decrypt(self, ciphertext): |
33 TripleDES.decrypt(self, ciphertext) | 36 TripleDES.decrypt(self, ciphertext) |
34 context = self._createContext(0) | 37 context = self._createContext(0) |
35 #I think M2Crypto has a bug - it fails to decrypt and return the las
t block passed in. | 38 #I think M2Crypto has a bug - it fails to decrypt and return the las
t block passed in. |
36 #To work around this, we append sixteen zeros to the string, below: | 39 #To work around this, we append sixteen zeros to the string, below: |
37 plaintext = m2.cipher_update(context, ciphertext+('\0'*16)) | 40 plaintext = m2.cipher_update(context, ciphertext+('\0'*16)) |
38 | 41 |
39 #If this bug is ever fixed, then plaintext will end up having a garb
age | 42 #If this bug is ever fixed, then plaintext will end up having a garb
age |
40 #plaintext block on the end. That's okay - the below code will igno
re it. | 43 #plaintext block on the end. That's okay - the below code will igno
re it. |
41 plaintext = plaintext[:len(ciphertext)] | 44 plaintext = plaintext[:len(ciphertext)] |
42 m2.cipher_ctx_free(context) | 45 m2.cipher_ctx_free(context) |
43 self.IV = ciphertext[-self.block_size:] | 46 self.IV = ciphertext[-self.block_size:] |
44 return plaintext | 47 return bytearray(plaintext) |
OLD | NEW |