/
rijndael_cipher.py
654 lines (583 loc) · 19.7 KB
/
rijndael_cipher.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#!/usr/bin/python
#coding=utf-8
import sys
import string
import random
import hashlib
import numpy as np
import numpy.core.numeric as N
def bits2int(bits):
"""
convert bit list to int
"""
num = 0
for b in bits:
num <<= 1
num += int(b)
return num
def bytes2int(bytes):
"""
convert byte list to int
"""
num = 0
for byte in bytes:
num <<= 8
num ^= byte
return num
def int2bytes(num):
"""
convert int to byte list
"""
bytes = []
while num:
byte = num & 0xFF
num >>= 8
bytes.append(byte)
return list(bytes.__reversed__())
def hexs2bytes(hextext):
"""
convert hex string to byte list
"""
bytes = []
for i in xrange(0, len(hextext), 2):
byte = int(hextext[i:i+2], 16)
bytes.append(byte)
return bytes
def unicode2bytes(text):
"""
convert unicode to byte list.
1 unicode character takes 2 bytes even is a single letter
"""
hextext = ''.join([format(ord(c), 'X').zfill(4) for c in text])
return hexs2bytes(hextext)
def bytes2unicode(bytes):
"""
convert byte list to unicode.
take every 2 bytes as 1 unicode character
"""
hextext = u''
for i in xrange(0, len(bytes), 2):
chrcode = (bytes[i]<<8) + bytes[i+1]
hextext += unichr(chrcode)
return hextext
def phex(num):
"""
convert int to 2 characters hex string
which not contain '0x' in begin or 'L' in end
"""
num = hex(int(num))[2:].upper()
if num[-1].lower() == 'L':
num = num[:-1]
return num.zfill(2)
def pbin(num):
"""
convert int to 8 characters binary string
which not contain '0b' in begin
"""
return bin(int(num))[2:].zfill(8)
def pmatrix(m, base='hex'):
"""
print matrix by given base
"""
if base in ('hex', 16, 'x'):
turnto = phex
elif base in ('bin', 2, 'b'):
turnto = pbin
elif base in ('dec', 10, 'd'):
turnto = lambda x: x
else:
raise Exception('Unknow base')
out = ''
if isinstance(m, MyMatrix):
m = m.getA()
for row in m:
for col in row:
out += '%2s ' % turnto(col)
out += '\n'
print(out[:-1])
class MyPolynomial(object):
"""
special polynomial arithmetic. All add figure replace by xor.
every operation will modulo `self.modulo`
and another argument can be `int`, `long` or `float`
"""
def __init__(self, num, modulo=sys.maxint):
self.num = num
self.modulo = modulo
def __mod__(self, other):
if isinstance(other, MyPolynomial):
other = other.num
times = len(bin(self.num)) - len(bin(other))
residue = self.num
for i in xrange(times):
div = other << (times - i)
if len(bin(residue)) == len(bin(div)):
residue ^= div
if len(bin(residue)) == len(bin(other)):
residue ^= other
return MyPolynomial(residue, self.modulo)
def __rmod__(self, other):
return MyPolynomial(other, self.modulo) % self
def __mul__(self, other):
if isinstance(other, MyPolynomial):
other = other.num
res = 0
for bit in bin(other)[2:]:
res <<= 1
if bit == '1':
res ^= self.num
return MyPolynomial(res, self.modulo) % self.modulo
def __rmul__(self, other):
return self.__mul__(other)
def __lshift__(self, other):
if isinstance(other, MyPolynomial):
other = other.num
return MyPolynomial(self.num << 1, self.modulo) % self.modulo
def __eq__(self, other):
if isinstance(other, MyPolynomial):
other = other.num
return self.num == other
def __int__(self):
return int(self.num)
def __long__(self):
return long(self.num)
def __float__(self):
return float(self.num)
class MyMatrix(np.matrix):
"""
same as `numpy.matrix` except do every calculate by `MyPolynomial`
"""
def __new__(subtype, data, dtype=int, copy=True, modulo=sys.maxint):
instance = np.matrix.__new__(subtype, data, dtype, copy)
instance.modulo = modulo
return instance
def dot(self, b, out=None):
res = []
a = self.getA()
b = b.getA()
for arow in a:
rcol = []
for y in xrange(len(b[0])):
bit = 0
x = 0
for acol in arow:
bit ^= int(MyPolynomial(acol, self.modulo) * b[x][y])
x += 1
rcol.append(bit)
res.append(rcol)
return MyMatrix(res)
def __mul__(self, other):
if isinstance(other, MyMatrix):
return self.dot(other)
if isinstance(other, (N.ndarray, list, tuple)):
return self.dot(MyMatrix.list2matrix(other))
raise Exception('Invalid operation')
def __add__(self, other):
a = self.getA()
b = other.getA()
res = []
if len(a) == len(b) and len(a[0]) == len(b[0]):
for x in xrange(len(a)):
rcol = []
for y in xrange(len(a[0])):
rcol.append(a[x][y] ^ b[x][y])
res.append(rcol)
return MyMatrix(res)
raise Exception('Invalid operation')
@classmethod
def list2matrix(cls, array, row=None, column=None):
"""
convert list to `MyMatrix` by given row length or column length
"""
marray = []
length = len(array)
if row and column:
pass
elif row:
column = length/row
elif column:
row = length/column
else:
row = 1
column = length
if length != row * column:
raise Exception('array length not equals row * column')
for i in xrange(row):
marray.append(array[column*i:column*(i+1)])
return MyMatrix(marray)
class Monoalphabetic(object):
"""
Substitution cipher is a improve Caesar cipher.
>>> # generate random letter pairs
>>> letters = string.ascii_letters
>>> randletters = list(letters)
>>> random.shuffle(randletters)
>>> letter_map = zip(letters, randletters)
>>>
>>> cipher = Monoalphabetic(letter_map)
>>> text = u'''
... mail -s "Hello, world." bob@b12
... Bob, could you please write me a program that prints "Hello, world."?
... I need it by tomorrow.
... ^D
... '''
>>> ctext = cipher.encrypt(text)
>>> assert text == cipher.decrypt(ctext)
"""
def __init__(self, letter_map):
self.lmap = {k:v for k, v in letter_map}
self.rlmap = {v:k for k, v in letter_map}
def encrypt(self, text):
return ''.join([self.lmap.get(c, c) for c in text])
def decrypt(self, text):
return ''.join([self.rlmap.get(c, c) for c in text])
class Rijndael(object):
"""
AES implementation with Rijndael algorithm, for more detail please read
'Federal Information Processing Standards Publication 197'.
NOTICE: all string must be unicode
>>> text = u'''
... Nb: Number of columns (32-bit words) comprising the State
... Nk: Number of 32-bit words comprising the Cipher Key
... Nr: Number of rounds, which is a function of Nk and Nb (which is fixed)
... The algorithm using cipher keys with lengths of 128, 192, and 256 bits
...
... Nk Nb Nr
... AES-128 4 4 10
... AES-192 6 4 12
... AES-256 8 4 14
... '''
>>> cipher = Rijndael(u'I am secret key')
>>> ctext = cipher.encrypt(text)
>>> assert text == cipher.decrypt(ctext)
"""
def __init__(self, key=u''):
self.key = key
# x^8+x^4+x^3+x+1
self.modulo = 0b100011011
def encrypt(self, text, key=None):
"""
wrap of `_cipher`, can deal with any unicode kind of `text` or `key`
"""
blocks, key = self._init_blocks_and_key(text, key)
self._pad(blocks)
for i in xrange(len(blocks)):
blocks[i] = MyMatrix.list2matrix(blocks[i], 4, 4)
w = self._keyexpansion(key)
for state in blocks:
self._cipher(state, w)
texts = [bytes2unicode(block.getA1()) for block in blocks]
return u''.join(texts)
def decrypt(self, text, key=None):
blocks, key = self._init_blocks_and_key(text, key)
for i in xrange(len(blocks)):
blocks[i] = MyMatrix.list2matrix(blocks[i], 4, 4)
w = self._keyexpansion(key)
for state in blocks:
self._invcipher(state, w)
for i in xrange(len(blocks)):
blocks[i] = blocks[i].getA1().tolist()
if not self._invpad(blocks):
raise Exception(u'Invalid blocks')
texts = [bytes2unicode(blocks[i]) for i in xrange(len(blocks)-1)]
texts = u''.join(texts) + bytes2unicode(blocks[-1])
return texts
def _init_blocks_and_key(self, text, key):
"""
convert string type `key` to byte type `key`, `text` same as `key`
"""
key = key or self.key
if not (type(text) == type(key) == unicode):
raise Exception('Key or text must unicode')
# string length not bit length
textlen = len(key)
if 0 <= textlen <= 128:
key = hashlib.md5(key).hexdigest()
elif 128 < textlen <= 192:
key = hashlib.md5(key).hexdigest()
key += hashlib.md5(key).hexdigest()[:16]
else:
key = hashlib.sha256(key).hexdigest()
key = hexs2bytes(key)
self.Nk = len(key) * 8 / 32
self.Nr = self.Nk + 6
self.Nb = 4
return self._getblocks(text), key
def _cipher(self, state, w):
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
self._addroundkey(state, w, 0)
for rnd in xrange(1, self.Nr):
self._subbytes(state)
self._shiftrows(state)
self._mixcolumns(state)
self._addroundkey(state, w, rnd)
self._subbytes(state)
self._shiftrows(state)
self._addroundkey(state, w, self.Nr)
def _invcipher(self, state, w):
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
self._addroundkey(state, w, self.Nr)
for rnd in xrange(self.Nr-1, 0, -1):
self._invshiftrows(state)
self._invsubbytes(state)
self._addroundkey(state, w, rnd)
self._invmixcolumns(state)
self._invshiftrows(state)
self._invsubbytes(state)
self._addroundkey(state, w, 0)
def _pad(self, blocks):
"""
The pad way using PKCS7 algorithm, if last block is not a
4 * 4 byte list then padding it, otherwise pad a new block
"""
length = len(blocks[-1])
if length == 16:
blocks.append([16 for i in xrange(16)])
else:
num = 16 - length
blocks[-1] = blocks[-1] + [num] * num
def _invpad(self, blocks):
"""
The inverse of `_pad`
"""
padlen = blocks[-1][-1]
# if not a valid blocks, then pad it back
padding = []
for i in xrange(padlen):
padding.insert(len(padding), blocks[-1].pop())
for byte in padding:
if byte != padding[0]:
blocks[-1] += padding
return False
if padlen == 16:
blocks.pop()
return True
def _getblocks(self, text):
"""
Sequence of binary bits that comprise the input, output, State,
and Round Key. Blocks are interpreted as arrays of bytes.
Every block is a 16 bytes (128 bits) list, except the last one
"""
bytes = unicode2bytes(text)
bytelen = len(bytes)
# 8 bit * 16 = 128 bit
blocks = [bytes[i*16:i*16+16] for i in xrange(bytelen/16)]
# lack of 128 bit
last_block = bytes[bytelen/16*16:]
if last_block:
blocks.append(last_block)
return blocks
def _mulinverse(self, num):
"""
multiplicative inverse in GF(2^8)
"""
if num == 0:
return num
num = MyPolynomial(num, modulo=self.modulo)
for rnum in xrange(256):
if num * rnum == 1:
return rnum
def _subbyte(self, byte, sbox):
# get high 4 bits
x = (byte & 0xF0) >> 4
# get low 4 bits
y = byte & 0xF
return sbox[x][y]
def _do_subbytes(self, state, sbox):
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
state = state.getA()
for i in xrange(len(state)):
for j in xrange(len(state[i])):
state[i][j] = self._subbyte(state[i][j], sbox)
def _gensbox(self, mulmatrix, addmatrix):
"""
Generate a S-box by given matrixs
"""
def affineTransform(num, mulmatrix, addmatrix):
num = bin(num)[2:].zfill(8)
num = MyMatrix([int(i) for i in num]).T
res = mulmatrix * num + addmatrix
res = bits2int(res.T.getA1())
return res
num = affineTransform(1, mulmatrix, addmatrix)
sbox = []
for x in xrange(16):
scol = []
for y in xrange(16):
num = (x << 4) + y
num = self._mulinverse(num)
num = affineTransform(num, mulmatrix, addmatrix)
scol.append(num)
sbox.append(scol)
return sbox
@property
def sbox(self):
if not hasattr(self, '_sbox'):
mulmatrix = MyMatrix([
[1, 1, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1],
])
addmatrix = MyMatrix([0, 1, 1, 0, 0, 0, 1, 1]).T
self._sbox = self._gensbox(mulmatrix, addmatrix)
return self._sbox
def _subbytes(self, state):
"""
Transformation in the Cipher that processes the State
using a non linear byte substitution table (S-box)
that operates on each of the State bytes independently.
"""
self._do_subbytes(state, self._sbox)
@property
def invsbox(self):
# inverse S-box
if not hasattr(self, '_invsbox'):
self._invsbox = [[0] * 16 for row in xrange(16)]
for x in xrange(16):
for y in xrange(16):
byte = self._sbox[x][y]
r = (byte & 0xF0) >> 4
c = byte & 0xF
self._invsbox[r][c] = (x << 4) + y
return self._invsbox
def _invsubbytes(self, state):
self._do_subbytes(state, self.invsbox)
def _shiftrows(self, state):
"""
Transformation in the Cipher that processes the State
by cyclically shifting the last three rows of the State
by different offsets
"""
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
array2d = state.getA()
array2d[0] = self._rotword(array2d[0], 0)
array2d[1] = self._rotword(array2d[1], 1)
array2d[2] = self._rotword(array2d[2], 2)
array2d[3] = self._rotword(array2d[3], 3)
return MyMatrix(array2d)
def _invshiftrows(self, state):
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
array2d = state.getA()
array2d[0] = self._rotword(array2d[0], 4)
array2d[1] = self._rotword(array2d[1], 3)
array2d[2] = self._rotword(array2d[2], 2)
array2d[3] = self._rotword(array2d[3], 1)
return MyMatrix(array2d)
def _do_mixcolumns(self, state, mulmatrix):
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
def mixcolumn(column):
column = MyMatrix.list2matrix(column, column=1)
column = mulmatrix * column
return column.getA1()
array = state.getA1()
columns = []
for i in xrange(4):
column = array[i::4]
column = mixcolumn(column)
columns.append(column)
columns = MyMatrix(columns).T
return columns
def _mixcolumns(self, state):
"""
Transformation in the Cipher that takes all of the columns
of the State and mixes their data (independently of one another)
to produce new columns
"""
mulmatrix = MyMatrix([
[2, 3, 1, 1],
[1, 2, 3, 1],
[1, 1, 2, 3],
[3, 1, 1, 2],
], modulo=0b10001)
return self._do_mixcolumns(state, mulmatrix)
def _invmixcolumns(self, state):
mulmatrix = MyMatrix([
[0xE, 0xB, 0xD, 0x9],
[0x9, 0xE, 0xB, 0xD],
[0xD, 0x9, 0xE, 0xB],
[0xB, 0xD, 0x9, 0xE],
], modulo=0b10001)
return self._do_mixcolumns(state, mulmatrix)
def _rotword(self, word, times=1):
"""
Function used in the Key Expansion routine that
takes a four-byte word and performs a cyclic permutation
"""
for i in xrange(times):
word = list(word[1:]) + list(word[:1])
return list(word)
def _subword(self, word):
"""
Function used in the Key Expansion routine that
takes a four-byte input word and applies an S-box to each of
the four bytes to produce an output word
"""
return [self._subbyte(byte, self.sbox) for byte in word]
@property
def rcon(self):
"""
The round constant word array of 4 bytes.
"""
if not hasattr(self, '_rcon'):
self._rcon = [0]
num = MyPolynomial(0x01, self.modulo)
for i in xrange(1, 11):
# doubling in GF(2**8)
x = [int(num)] + [0] * 3
num <<= 1
self._rcon.append(x)
return self._rcon
def _addroundkey(self, state, w, rnd):
"""
Transformation in the Cipher and Inverse Cipher
in which a Round Key is added to the State using an XOR operation.
The length of a Round Key equals the size of the State
"""
if not isinstance(state, MyMatrix):
raise Exception('Not a `MyMatrix` instance')
state = state.getA()
for c in xrange(self.Nb):
for r in xrange(self.Nb):
state[r][c] ^= w[rnd*self.Nb+c][r]
def _keyexpansion(self, key):
"""
Routine used to generate a series of Round Keys from the Cipher Key
"""
w = [key[4*i:4*i+4] for i in xrange(self.Nk)]
def xor(*args):
"""
calculate (list xor list) or (list xor int)
return 4 byte list
"""
args = list(args)
if isinstance(args[0], list):
args[0] = bytes2int(args[0])
num = args[0]
for i in xrange(1, len(args)):
if isinstance(args[i], list):
args[i] = bytes2int(args[i])
num ^= args[i]
num = phex(num).zfill(8)
return hexs2bytes(num)
for i in xrange(self.Nk, self.Nb*(self.Nr+1)):
tmp = w[i-1]
if i % self.Nk == 0:
tmp = xor(self._subword(self._rotword(tmp)), self.rcon[i/self.Nk])
elif self.Nk > 6 and i % self.Nk == 4:
tmp = self._subword(tmp)
w.append(xor(w[i-self.Nk], tmp))
return w
if __name__ == '__main__':
import doctest
doctest.testmod()