coding utf-8 SubBytes 99 124 119 123 242 107 111 197 48 103 43 254 215

  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
#coding=utf-8
SubBytes = [99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21,
4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117,
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132,
83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207,
208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210,
205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115,
96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219,
224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121,
231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8,
186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138,
112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22]
InvSubBytes = [82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251,
124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203,
84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78,
8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37,
114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146,
108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132,
144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6,
208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107,
58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115,
150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110,
71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27,
252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244,
31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95,
96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239,
160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125]
null_char = 0
block_size = 16
keys = [35, 54, 46, 49, 58, 45, 34, 0, 52, 0, 38, 41, 39, 47, 49, 47]
enc_data = [13, 84, 192, 126, 130, 148, 115, 248, 109, 253, 42, 181, 128, 21, 115, 30,
96, 90, 109, 242, 36, 113, 33, 63, 247, 21, 202, 79, 97, 202, 67, 189]
list2hexline = lambda data: " ".join([hex(ch)[2:].zfill(2) for ch in data])
def dec2poly(num):
res = []
num = bin(num)[2:]
for offset, ch in enumerate(num):
if ch == '1':
res.append(len(num)-offset-1)
return res
#Cyclic shift function| direction: 1 - right, -1 - left
def cyclic_shift(data, shift, direction=1):
shift %= len(data)
return data[direction*-shift:] + data[:direction*-shift]
#XOR elements
def xor(first, second):
result_size = max(len(first), len(second))
first, second = map(lambda n: [0, ]*(result_size - len(n)) + n, [first, second])
return map(lambda n: (n[0] ^ n[1]), zip(first, second))
#Addition to the block size | len(keyword) = 0 (mod key_block_len)
def check_key(key):
return key + [null_char, ] * ((block_size - len(key)) % block_size)
#Generate round keys
def gen_round_keys():
global keys
keys = check_key(keys)
keys = [keys[offset:offset+4] for offset in xrange(0, len(keys), 4)]
while len(keys) < 16:
curr_key_index = len(keys)
prev_key = keys[curr_key_index-1]
if curr_key_index % 4 == 0:
prev_key = xor(substitute(cyclic_shift(prev_key, 1, -1), SubBytes), [2**(curr_key_index/4-1), ])
keys.append(xor(prev_key, keys[curr_key_index-4]))
#Multiplication number and const
def poly_mul(number, poly_const):
result = 0
for pw in poly_const:
result ^= number << pw
print "POLY_MUL: (%s == %s) * %s = %s" % (number, dec2poly(number), poly_const, dec2poly(result))
return result
def poly_mod(number, mod=283):
while number > 255:
lst = number
xor_num = mod << len(bin(number)[2:])-len(bin(mod)[2:])
number ^= xor_num
print "...POLY_MOD: %s ^ %s = %s" % (dec2poly(lst), dec2poly(xor_num), dec2poly(number))
print "POLY_MOD_RES: %s = %s\n" % (dec2poly(number), number)
return number
#SybBytes
def substitute(index, table):
return [table[num] for num in index]
def step_InvMixColumn(block):
poly_const = [[3, 2, 1], [3, 1, 0], [3, 2, 0], [3, 0]]
res_block = []
for index, line in enumerate(block):
tmp = cyclic_shift(poly_const, index)
res = []
for j, ch in enumerate(line):
temptemp = map(lambda (bl, const): poly_mod(poly_mul(bl, const)), [(block[s][j], tmp[s]) for s in range(4)])
value_res = reduce(lambda prev_a, prev_b: prev_a ^ prev_b, temptemp)
print "XOR_ELEMS(%s) = %s" % (temptemp, value_res)
print "=====\n\n"
res.append(value_res)
res_block.append(res)
return res_block
def step_InvSubBytes(block):
return [substitute(line, InvSubBytes) for index, line in enumerate(block)]
def step_InvShiftRow(block):
return [cyclic_shift(line, index) for index, line in enumerate(block)]
def step_AddRoundKey(block, shift_key):
return [xor(line, keys[shift_key+index]) for index, line in enumerate(block)]
def main():
global keys
gen_round_keys()
print ">>>ROUND KEYS:"
for ind, r_key in enumerate(keys):
print "w[%s]: %s" % (ind, list2hexline(r_key))
keys = keys[:8] + step_InvMixColumn(keys[8:12]) + keys[12:]
keys = keys[:4] + step_InvMixColumn(keys[4:8]) + keys[8:]
print "\n>>>\?ROUND KEYS AFTER InvMixColumn | replacement on the direct order"
for ind, r_key in enumerate(keys):
print "w[%s]: %s" % (ind, list2hexline(r_key))
for block_num in range(0, len(enc_data), 16):
print "\n>>>BLOCK:\n"
block = [enc_data[sbi:sbi+4] for sbi in xrange(block_num, block_num+block_size, 4)]
block = step_AddRoundKey(block, 12)
print "Step1:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvSubBytes(block)
print "Step2:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvShiftRow(block)
print "Step3:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvMixColumn(block)
print "Step4:\n%s" % "\n".join(map(list2hexline, block))
block = step_AddRoundKey(block, 8)
print "Step5:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvSubBytes(block)
print "Step6:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvShiftRow(block)
print "Step7:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvMixColumn(block)
print "Step8:\n%s" % "\n".join(map(list2hexline, block))
block = step_AddRoundKey(block, 4)
print "Step9:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvSubBytes(block)
print "Step10:\n%s" % "\n".join(map(list2hexline, block))
block = step_InvShiftRow(block)
print "Step11:\n%s" % "\n".join(map(list2hexline, block))
block = step_AddRoundKey(block, 0)
print "Step12:\n%s" % "\n".join(map(list2hexline, block))
if __name__ == "__main__":
main()