/
blockcipher.py
592 lines (511 loc) · 24.5 KB
/
blockcipher.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# =============================================================================
# Copyright (c) 2008 Christophe Oosterlynck <christophe.oosterlynck_AT_gmail.com>
# & NXP ( Philippe Teuwen <philippe.teuwen_AT_nxp.com> )
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# =============================================================================
from ..Util import util
from ..Util import padding
MODE_ECB = 1
MODE_CBC = 2
MODE_CFB = 3
MODE_OFB = 5
MODE_CTR = 6
MODE_XTS = 7
MODE_CMAC = 8
class BlockCipher():
""" Base class for all blockciphers
"""
key_error_message = "Wrong key size" #should be overwritten in child classes
def __init__(self,key,mode,IV,counter,cipher_module,segment_size,args={}):
# Cipher classes inheriting from this one take care of:
# self.blocksize
# self.cipher
self.key = key
self.mode = mode
self.cache = ''
self.ed = None
if 'keylen_valid' in dir(self): #wrappers for pycrypto functions don't have this function
if not self.keylen_valid(key) and type(key) is not tuple:
raise ValueError(self.key_error_message)
if IV == None:
self.IV = '\x00'*self.blocksize
else:
self.IV = IV
if mode <> MODE_XTS:
self.cipher = cipher_module(self.key,**args)
if mode == MODE_ECB:
self.chain = ECB(self.cipher, self.blocksize)
elif mode == MODE_CBC:
if len(self.IV) <> self.blocksize:
raise Exception,"the IV length should be %i bytes"%self.blocksize
self.chain = CBC(self.cipher, self.blocksize,self.IV)
elif mode == MODE_CFB:
if len(self.IV) <> self.blocksize:
raise Exception,"the IV length should be %i bytes"%self.blocksize
if segment_size == None:
raise ValueError,"segment size must be defined explicitely for CFB mode"
if segment_size > self.blocksize*8 or segment_size%8 <> 0:
# current CFB implementation doesn't support bit level acces => segment_size should be multiple of bytes
raise ValueError,"segment size should be a multiple of 8 bits between 8 and %i"%(self.blocksize*8)
self.chain = CFB(self.cipher, self.blocksize,self.IV,segment_size)
elif mode == MODE_OFB:
if len(self.IV) <> self.blocksize:
raise ValueError("the IV length should be %i bytes"%self.blocksize)
self.chain = OFB(self.cipher, self.blocksize,self.IV)
elif mode == MODE_CTR:
if (counter == None) or not callable(counter):
raise Exception,"Supply a valid counter object for the CTR mode"
self.chain = CTR(self.cipher,self.blocksize,counter)
elif mode == MODE_XTS:
if self.blocksize <> 16:
raise Exception,'XTS only works with blockcipher that have a 128-bit blocksize'
if not(type(key) == tuple and len(key) == 2):
raise Exception,'Supply two keys as a tuple when using XTS'
if 'keylen_valid' in dir(self): #wrappers for pycrypto functions don't have this function
if not self.keylen_valid(key[0]) or not self.keylen_valid(key[1]):
raise ValueError(self.key_error_message)
self.cipher = cipher_module(self.key[0],**args)
self.cipher2 = cipher_module(self.key[1],**args)
self.chain = XTS(self.cipher, self.cipher2)
elif mode == MODE_CMAC:
if self.blocksize not in (8,16):
raise Exception,'CMAC only works with blockcipher that have a 64 or 128-bit blocksize'
self.chain = CMAC(self.cipher,self.blocksize,self.IV)
else:
raise Exception,"Unknown chaining mode!"
def encrypt(self,plaintext,n=''):
"""Encrypt some plaintext
plaintext = a string of binary data
n = the 'tweak' value when the chaining mode is XTS
The encrypt function will encrypt the supplied plaintext.
The behavior varies slightly depending on the chaining mode.
ECB, CBC:
---------
When the supplied plaintext is not a multiple of the blocksize
of the cipher, then the remaining plaintext will be cached.
The next time the encrypt function is called with some plaintext,
the new plaintext will be concatenated to the cache and then
cache+plaintext will be encrypted.
CFB, OFB, CTR:
--------------
When the chaining mode allows the cipher to act as a stream cipher,
the encrypt function will always encrypt all of the supplied
plaintext immediately. No cache will be kept.
XTS:
----
Because the handling of the last two blocks is linked,
it needs the whole block of plaintext to be supplied at once.
Every encrypt function called on a XTS cipher will output
an encrypted block based on the current supplied plaintext block.
CMAC:
-----
Everytime the function is called, the hash from the input data is calculated.
No finalizing needed.
The hashlength is equal to block size of the used block cipher.
"""
#self.ed = 'e' if chain is encrypting, 'd' if decrypting,
# None if nothing happened with the chain yet
#assert self.ed in ('e',None)
# makes sure you don't encrypt with a cipher that has started decrypting
self.ed = 'e'
if self.mode == MODE_XTS:
# data sequence number (or 'tweak') has to be provided when in XTS mode
return self.chain.update(plaintext,'e',n)
else:
return self.chain.update(plaintext,'e')
def decrypt(self,ciphertext,n=''):
"""Decrypt some ciphertext
ciphertext = a string of binary data
n = the 'tweak' value when the chaining mode is XTS
The decrypt function will decrypt the supplied ciphertext.
The behavior varies slightly depending on the chaining mode.
ECB, CBC:
---------
When the supplied ciphertext is not a multiple of the blocksize
of the cipher, then the remaining ciphertext will be cached.
The next time the decrypt function is called with some ciphertext,
the new ciphertext will be concatenated to the cache and then
cache+ciphertext will be decrypted.
CFB, OFB, CTR:
--------------
When the chaining mode allows the cipher to act as a stream cipher,
the decrypt function will always decrypt all of the supplied
ciphertext immediately. No cache will be kept.
XTS:
----
Because the handling of the last two blocks is linked,
it needs the whole block of ciphertext to be supplied at once.
Every decrypt function called on a XTS cipher will output
a decrypted block based on the current supplied ciphertext block.
CMAC:
-----
Mode not supported for decryption as this does not make sense.
"""
#self.ed = 'e' if chain is encrypting, 'd' if decrypting,
# None if nothing happened with the chain yet
#assert self.ed in ('d',None)
# makes sure you don't decrypt with a cipher that has started encrypting
self.ed = 'd'
if self.mode == MODE_XTS:
# data sequence number (or 'tweak') has to be provided when in XTS mode
return self.chain.update(ciphertext,'d',n)
else:
return self.chain.update(ciphertext,'d')
def final(self,padfct=padding.PKCS7):
# TODO: after calling final, reset the IV? so the cipher is as good as new?
"""Finalizes the encryption by padding the cache
padfct = padding function
import from CryptoPlus.Util.padding
For ECB, CBC: the remaining bytes in the cache will be padded and
encrypted.
For OFB,CFB, CTR: an encrypted padding will be returned, making the
total outputed bytes since construction of the cipher
a multiple of the blocksize of that cipher.
If the cipher has been used for decryption, the final function won't do
anything. You have to manually unpad if necessary.
After finalization, the chain can still be used but the IV, counter etc
aren't reset but just continue as they were after the last step (finalization step).
"""
assert self.mode not in (MODE_XTS, MODE_CMAC) # finalizing (=padding) doesn't make sense when in XTS or CMAC mode
if self.ed == 'e':
# when the chain is in encryption mode, finalizing will pad the cache and encrypt this last block
if self.mode in (MODE_OFB,MODE_CFB,MODE_CTR):
dummy = '0'*(self.chain.totalbytes%self.blocksize) # a dummy string that will be used to get a valid padding
else: #ECB, CBC
dummy = self.chain.cache
pad = padfct(dummy,padding.PAD,self.blocksize)[len(dummy):] # construct the padding necessary
return self.chain.update(pad,'e') # supply the padding to the update function => chain cache will be "cache+padding"
else:
# final function doesn't make sense when decrypting => padding should be removed manually
pass
class ECB:
"""ECB chaining mode
"""
def __init__(self, codebook, blocksize):
self.cache = ''
self.codebook = codebook
self.blocksize = blocksize
def update(self, data, ed):
"""Processes the given ciphertext/plaintext
Inputs:
data: raw string of any length
ed: 'e' for encryption, 'd' for decryption
Output:
processed raw string block(s), if any
When the supplied data is not a multiple of the blocksize
of the cipher, then the remaining input data will be cached.
The next time the update function is called with some data,
the new data will be concatenated to the cache and then
cache+data will be processed and full blocks will be outputted.
"""
output_blocks = []
self.cache += data
if len(self.cache) < self.blocksize:
return ''
for i in xrange(0, len(self.cache)-self.blocksize+1, self.blocksize):
#the only difference between encryption/decryption in the chain is the cipher block
if ed == 'e':
output_blocks.append(self.codebook.encrypt( self.cache[i:i + self.blocksize] ))
else:
output_blocks.append(self.codebook.decrypt( self.cache[i:i + self.blocksize] ))
self.cache = self.cache[i+self.blocksize:]
return ''.join(output_blocks)
class CBC:
"""CBC chaining mode
"""
def __init__(self, codebook, blocksize, IV):
self.IV = IV
self.cache = ''
self.codebook = codebook
self.blocksize = blocksize
def update(self, data, ed):
"""Processes the given ciphertext/plaintext
Inputs:
data: raw string of any length
ed: 'e' for encryption, 'd' for decryption
Output:
processed raw string block(s), if any
When the supplied data is not a multiple of the blocksize
of the cipher, then the remaining input data will be cached.
The next time the update function is called with some data,
the new data will be concatenated to the cache and then
cache+data will be processed and full blocks will be outputted.
"""
if ed == 'e':
encrypted_blocks = ''
self.cache += data
if len(self.cache) < self.blocksize:
return ''
for i in xrange(0, len(self.cache)-self.blocksize+1, self.blocksize):
self.IV = self.codebook.encrypt(util.xorstring(self.cache[i:i+self.blocksize],self.IV))
encrypted_blocks += self.IV
self.cache = self.cache[i+self.blocksize:]
return encrypted_blocks
else:
decrypted_blocks = ''
self.cache += data
if len(self.cache) < self.blocksize:
return ''
for i in xrange(0, len(self.cache)-self.blocksize+1, self.blocksize):
plaintext = util.xorstring(self.IV,self.codebook.decrypt(self.cache[i:i + self.blocksize]))
self.IV = self.cache[i:i + self.blocksize]
decrypted_blocks+=plaintext
self.cache = self.cache[i+self.blocksize:]
return decrypted_blocks
class CFB:
# TODO: bit access instead of only byte level access
"""CFB Chaining Mode
Can be accessed as a stream cipher.
"""
def __init__(self, codebook, blocksize, IV,segment_size):
self.codebook = codebook
self.IV = IV
self.blocksize = blocksize
self.segment_size = segment_size/8
self.keystream = []
self.totalbytes = 0
def update(self, data, ed):
"""Processes the given ciphertext/plaintext
Inputs:
data: raw string of any multiple of bytes
ed: 'e' for encryption, 'd' for decryption
Output:
processed raw string
The encrypt/decrypt functions will always process all of the supplied
input data immediately. No cache will be kept.
"""
output = list(data)
for i in xrange(len(data)):
if ed =='e':
if len(self.keystream) == 0:
block = self.codebook.encrypt(self.IV)
self.keystream = list(block)[:self.segment_size] # keystream consists of the s MSB's
self.IV = self.IV[self.segment_size:] # keeping (b-s) LSB's
output[i] = chr(ord(output[i]) ^ ord(self.keystream.pop(0)))
self.IV += output[i] # the IV for the next block in the chain is being built byte per byte as the ciphertext flows in
else:
if len(self.keystream) == 0:
block = self.codebook.encrypt(self.IV)
self.keystream = list(block)[:self.segment_size]
self.IV = self.IV[self.segment_size:]
self.IV += output[i]
output[i] = chr(ord(output[i]) ^ ord(self.keystream.pop(0)))
self.totalbytes += len(output)
return ''.join(output)
class OFB:
"""OFB Chaining Mode
Can be accessed as a stream cipher.
"""
def __init__(self, codebook, blocksize, IV):
self.codebook = codebook
self.IV = IV
self.blocksize = blocksize
self.keystream = []
self.totalbytes = 0
def update(self, data, ed):
"""Processes the given ciphertext/plaintext
Inputs:
data: raw string of any multiple of bytes
ed: 'e' for encryption, 'd' for decryption
Output:
processed raw string
The encrypt/decrypt functions will always process all of the supplied
input data immediately. No cache will be kept.
"""
#no difference between encryption and decryption mode
n = len(data)
blocksize = self.blocksize
output = list(data)
for i in xrange(n):
if len(self.keystream) == 0: #encrypt a new counter block when the current keystream is fully used
self.IV = self.codebook.encrypt(self.IV)
self.keystream = list(self.IV)
output[i] = chr(ord(output[i]) ^ ord(self.keystream.pop(0))) #as long as an encrypted counter value is available, the output is just "input XOR keystream"
self.totalbytes += len(output)
return ''.join(output)
class CTR:
"""CTR Chaining Mode
Can be accessed as a stream cipher.
"""
# initial counter value can be choosen, decryption always starts from beginning
# -> you can start from anywhere yourself: just feed the cipher encoded blocks and feed a counter with the corresponding value
def __init__(self, codebook, blocksize, counter):
self.codebook = codebook
self.counter = counter
self.blocksize = blocksize
self.keystream = [] #holds the output of the current encrypted counter value
self.totalbytes = 0
def update(self, data, ed):
"""Processes the given ciphertext/plaintext
Inputs:
data: raw string of any multiple of bytes
ed: 'e' for encryption, 'd' for decryption
Output:
processed raw string
The encrypt/decrypt functions will always process all of the supplied
input data immediately. No cache will be kept.
"""
# no need for the encryption/decryption distinction: both are the same
n = len(data)
blocksize = self.blocksize
output = list(data)
for i in xrange(n):
if len(self.keystream) == 0: #encrypt a new counter block when the current keystream is fully used
block = self.codebook.encrypt(self.counter())
self.keystream = list(block)
output[i] = chr(ord(output[i])^ord(self.keystream.pop(0))) #as long as an encrypted counter value is available, the output is just "input XOR keystream"
self.totalbytes += len(output)
return ''.join(output)
class XTS:
"""XTS Chaining Mode
Usable with blockciphers with a 16-byte blocksize
"""
# TODO: allow other blocksizes besides 16bytes?
def __init__(self,codebook1, codebook2):
self.cache = ''
self.codebook1 = codebook1
self.codebook2 = codebook2
def update(self, data, ed,tweak=''):
# supply n as a raw string
# tweak = data sequence number
"""Perform a XTS encrypt/decrypt operation.
Because the handling of the last two blocks is linked,
it needs the whole block of ciphertext to be supplied at once.
Every decrypt function called on a XTS cipher will output
a decrypted block based on the current supplied ciphertext block.
"""
output = ''
assert len(data) > 15, "At least one block of 128 bits needs to be supplied"
assert len(data) < 128*pow(2,20)
# initializing T
# e_k2_n = E_K2(tweak)
e_k2_n = self.codebook2.encrypt(tweak+ '\x00' * (16-len(tweak)))[::-1]
self.T = util.string2number(e_k2_n)
i=0
while i < ((len(data) // 16)-1): #Decrypt all the blocks but one last full block and opt one last partial block
# C = E_K1(P xor T) xor T
output += self.__xts_step(ed,data[i*16:(i+1)*16],self.T)
# T = E_K2(n) mul (a pow i)
self.__T_update()
i+=1
# Check if the data supplied is a multiple of 16 bytes -> one last full block and we're done
if len(data[i*16:]) == 16:
# C = E_K1(P xor T) xor T
output += self.__xts_step(ed,data[i*16:(i+1)*16],self.T)
# T = E_K2(n) mul (a pow i)
self.__T_update()
else:
T_temp = [self.T]
self.__T_update()
T_temp.append(self.T)
if ed=='d':
# Permutation of the last two indexes
T_temp.reverse()
# Decrypt/Encrypt the last two blocks when data is not a multiple of 16 bytes
Cm1 = data[i*16:(i+1)*16]
Cm = data[(i+1)*16:]
PP = self.__xts_step(ed,Cm1,T_temp[0])
Cp = PP[len(Cm):]
Pm = PP[:len(Cm)]
CC = Cm+Cp
Pm1 = self.__xts_step(ed,CC,T_temp[1])
output += Pm1 + Pm
return output
def __xts_step(self,ed,tocrypt,T):
T_string = util.number2string_N(T,16)[::-1]
# C = E_K1(P xor T) xor T
if ed == 'd':
return util.xorstring(T_string, self.codebook1.decrypt(util.xorstring(T_string, tocrypt)))
else:
return util.xorstring(T_string, self.codebook1.encrypt(util.xorstring(T_string, tocrypt)))
def __T_update(self):
# Used for calculating T for a certain step using the T value from the previous step
self.T = self.T << 1
# if (Cout)
if self.T >> (8*16):
#T[0] ^= GF_128_FDBK;
self.T = self.T ^ 0x100000000000000000000000000000087L
class CMAC:
"""CMAC chaining mode
Supports every cipher with a blocksize available
in the list CMAC.supported_blocksizes.
The hashlength is equal to block size of the used block cipher.
Usable with blockciphers with a 8 or 16-byte blocksize
"""
# TODO: move to hash module?
# TODO: change update behaviour to .update() and .digest() as for all hash modules?
# -> other hash functions in pycrypto: calling update, concatenates current input with previous input and hashes everything
__Rb_dictionary = {64:0x000000000000001b,128:0x00000000000000000000000000000087}
supported_blocksizes = __Rb_dictionary.keys()
def __init__(self,codebook,blocksize,IV):
# Purpose of init: calculate Lu & Lu2
#blocksize (in bytes): to select the Rb constant in the dictionary
#Rb as a dictionary: adding support for other blocksizes is easy
self.cache=''
self.blocksize = blocksize
self.codebook = codebook
self.IV = IV
#Rb_dictionary: holds values for Rb for different blocksizes
# values for 64 and 128 bits found here: http://www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/omac.html
# explanation from: http://csrc.nist.gov/publications/nistpubs/800-38B/SP_800-38B.pdf
# Rb is a representation of a certain irreducible binary polynomial of degree b, namely,
# the lexicographically first among all such polynomials with the minimum possible number of
# nonzero terms. If this polynomial is expressed as ub+cb-1ub-1+...+c2u2+c1u+c0, where the
# coefficients cb-1, cb-2, ..., c2, c1, c0 are either 0 or 1, then Rb is the bit string cb-1cb-2...c2c1c0.
self.Rb = self.__Rb_dictionary[blocksize*8]
mask1 = int(('\xff'*blocksize).encode('hex'),16)
mask2 = int(('\x80' + '\x00'*(blocksize-1) ).encode('hex'),16)
L = int(self.codebook.encrypt('\x00'*blocksize).encode('hex'),16)
if L & mask2:
Lu = ((L << 1) & mask1) ^ self.Rb
else:
Lu = L << 1
Lu = Lu & mask1
if Lu & mask2:
Lu2 = ((Lu << 1) & mask1)^ self.Rb
else:
Lu2 = Lu << 1
Lu2 = Lu2 & mask1
self.Lu =util.number2string_N(Lu,self.blocksize)
self.Lu2=util.number2string_N(Lu2,self.blocksize)
def update(self, data, ed):
"""Processes the given ciphertext/plaintext
Inputs:
data: raw string of any length
ed: 'e' for encryption, 'd' for decryption
Output:
hashed data as raw string
This is not really an update function:
Everytime the function is called, the hash from the input data is calculated.
No finalizing needed.
"""
assert ed == 'e'
blocksize = self.blocksize
m = (len(data)+blocksize-1)/blocksize #m = amount of datablocks
i=0
for i in range(1,m):
self.IV = self.codebook.encrypt( util.xorstring(data[(i-1)*blocksize:(i)*blocksize],self.IV) )
if len(data[(i)*blocksize:])==blocksize:
X = util.xorstring(util.xorstring(data[(i)*blocksize:],self.IV),self.Lu)
else:
tmp = data[(i)*blocksize:] + '\x80' + '\x00'*(blocksize - len(data[(i)*blocksize:])-1)
X = util.xorstring(util.xorstring(tmp,self.IV),self.Lu2)
T = self.codebook.encrypt(X)
return T