Skip to content

Commit d26b370

Browse files
committed
Move to pep272-encryption
1 parent 28d11c8 commit d26b370

1 file changed

Lines changed: 31 additions & 146 deletions

File tree

src/camellia/__init__.py

Lines changed: 31 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import sys
99

10+
from pep272_encryption import PEP272Cipher
11+
1012
#: ECB mode of operation
1113
MODE_ECB = 1
1214
#: CBC mode of operation
@@ -82,7 +84,6 @@ def Camellia_Decrypt(keyLength, keytable, cipherText):
8284

8385
def new(key, mode=MODE_ECB, **kwargs):
8486
"""Create an "CamelliaCipher" object.
85-
It's not fully PEP-272 comliant (yet).
8687
The default mode is ECB.
8788
8889
:param key: The key for encrytion/decryption. Must be 16/24/32 in length.
@@ -95,179 +96,63 @@ def new(key, mode=MODE_ECB, **kwargs):
9596
:type IV: bytestring
9697
9798
:param counter: Counter for CTR.
98-
:type counter: callable, must return bytestrings
99+
:type counter: callable, must return bytestrings 16 in length
99100
100101
:returns: CamelliaCipher
101102
:raises: ValueError, NotImplementedError
102103
"""
103-
return CamelliaCipher(key, mode=mode, **kwargs)
104+
return CamelliaCipher(key, mode, **kwargs)
104105

105106
key_size = None
106107
block_size = 16
107108

108-
class CamelliaCipher(object):
109+
class CamelliaCipher(PEP272Cipher):
109110
"""
110111
The CamelliaCipher object.
111112
"""
112113

113114
#: block size of the camellia cipher
114115
block_size = 16
115116

116-
def __init__(self, key, **kwargs):
117+
def __init__(self, key, mode, **kwargs):
117118
"""
118119
Constructer of Cipher class. See :func:`camellia.new`.
119-
120-
*mode* and *IV* must be passed as keyword arguments.
121-
"""
122-
self.__key_length = len(key) * 8
123-
self.__key = Camellia_Ekeygen(key)
124-
125-
if len(key) not in (16,24,32):
126-
raise ValueError("Key must be 128, 192 or 256 bits long")
127-
128-
keys = kwargs.keys()
129-
if "mode" in keys:
130-
self.mode = kwargs["mode"]
131-
if self.mode not in SUPPORTED_MODES:
132-
raise NotImplementedError("This mode is not supported!")
133-
else:
134-
self.mode = MODE_ECB
135-
136-
if "IV" in keys:
137-
self.IV = kwargs["IV"]
138-
if len(self.IV) != self.block_size/8:
139-
raise ValueError("IV must be 16 bytes long")
140-
141-
# self.IV = IV # self.IV can be changed, but has no effect!
142-
143-
if "counter" in keys:
144-
self.counter = kwargs["counter"]
145-
elif self.mode == MODE_CTR:
146-
raise ValueError("CTR needs a counter!")
147-
148-
def encrypt(self, data):
149-
"""
150-
Encrypt data.
151-
152-
:param data:
153-
The data to encrypt.
154-
For the most modes of operation is must be a multiple
155-
of 16 in length.
156-
:type data: bytestring
157-
158120
"""
159-
blocks = self._block(data)
160-
161-
if self.mode == MODE_ECB:
162-
if len(data) % (self.block_size/8):
163-
raise ValueError("Input string must be a multiple "
164-
"of blocksize in length")
165121

166-
out = []
167-
for block in blocks:
168-
out.append(Camellia_Encrypt(self.__key_length, self.__key,
169-
block))
122+
keytable = Camellia_Ekeygen(key)
123+
self.key_length = len(key)*8
124+
170125

171-
return b''.join(out)
126+
PEP272Cipher.__init__(self, keytable, mode, **kwargs)
172127

173-
elif self.mode == MODE_CBC:
174-
if len(data) % (self.block_size/8):
175-
raise ValueError("Input string must be a multiple "
176-
"of blocksize in length")
128+
def encrypt_block(self, key, block, **kwargs):
129+
return Camellia_Encrypt(self.key_length, key, block)
177130

178-
out = []
179-
for block in blocks:
180-
xored = xor(block, self.IV)
181-
self.IV = (Camellia_Encrypt(self.__key_length, self.__key,
182-
xored))
131+
def decrypt_block(self, key, block, **kwargs):
132+
return Camellia_Decrypt(self.key_length, key, block)
183133

184-
out.append(self.IV)
185134

186-
return b''.join(out)
187135

188-
elif self.mode == MODE_CTR:
189-
out = []
136+
CamelliaCipher.encrypt.__doc__ = """\
137+
Encrypt string.
190138
191-
for block in blocks:
192-
ctr = self.counter()
193-
if len(ctr) != self.blocksize:
194-
raise ValueError("The counter function must return "
195-
"a bytestring of blocksize in length")
139+
:param string:
140+
The data to encrypt.
141+
For the most modes of operation it must be a multiple
142+
of 16 in length.
143+
:type string: bytestring
144+
"""
196145

197-
encrypted_counter = Camellia_Encrypt(self.__key_length,
198-
self.__key,
199-
self.counter())
200-
encrypted_block = xor(block, encrypted_counter)
201-
out.append(encrypted_block)
202146

203-
return b''.join(out)
204-
205-
else:
206-
raise Exception("???")
147+
CamelliaCipher.decrypt.__doc__ = """\
148+
Decrypt string.
207149
208-
def decrypt(self, data):
209-
"""
210-
Decrypt data.
211-
212-
:param data:
213-
The data to decrypt.
214-
For the most modes of operation is must be a multiple
215-
of 16 in length.
216-
:type data: bytestring
217-
218-
"""
219-
blocks = self._block(data)
220-
221-
if self.mode == MODE_ECB:
222-
if len(data) % (self.block_size/8):
223-
raise ValueError("Input string must be a multiple "
224-
"of blocksize in length")
225-
226-
out = []
227-
for block in blocks:
228-
out.append(Camellia_Decrypt(self.__key_length, self.__key,
229-
block))
230-
231-
return b''.join(out)
232-
233-
elif self.mode == MODE_CBC:
234-
if len(data) % (self.block_size/8):
235-
raise ValueError("Input string must be a multiple "
236-
"of blocksize in length")
237-
238-
out = []
239-
blocks = [self.IV] + blocks
240-
for i in range(1, len(blocks)):
241-
temp = Camellia_Decrypt(self.__key_length, self.__key,
242-
blocks[i])
243-
out.append(xor(temp, blocks[i-1]))
244-
245-
self.IV = blocks[-1]
246-
247-
return b''.join(out)
248-
249-
elif self.mode == MODE_CTR:
250-
return self.encrypt(data)
251-
252-
else:
253-
raise Exception("???")
254-
255-
def _block(self, s):
256-
l = []
257-
rest_size = int(len(s) % (self.block_size))
258-
for i in range(int(len(s)/(self.block_size))):
259-
l.append(s[i*(self.block_size):((i+1)*(self.block_size))])
260-
if rest_size:
261-
# raise ValueError()
262-
l.append(s[-rest_size:])
263-
return l
264-
265-
if int(sys.version[0]) < 3:
266-
def xor(a, b):
267-
return "".join([chr(ord(c) ^ ord(d)) for c, d in zip(a, b)])
268-
else:
269-
def xor(a, b):
270-
return bytes([c ^ d for c, d in zip(a, b)])
150+
:param string:
151+
The data to decrypt.
152+
For the most modes of operation it must be a multiple
153+
of 16 in length.
154+
:type string: bytestring
155+
"""
271156

272157

273158
def test(v=True):
@@ -277,7 +162,7 @@ def test(v=True):
277162

278163
import binascii
279164

280-
c = CamelliaCipher(binascii.unhexlify(key))
165+
c = CamelliaCipher(binascii.unhexlify(key), mode=MODE_ECB)
281166

282167
ec = c.encrypt(binascii.unhexlify(plain))
283168

0 commit comments

Comments
 (0)