-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsquare.py
More file actions
90 lines (83 loc) · 2.85 KB
/
square.py
File metadata and controls
90 lines (83 loc) · 2.85 KB
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
import random
import numpy as np
import binascii
from boxes import *
from KeySchedule import *
from aes import *
####
# Square-Attack
####
def encryptWithRounds(plaintext, key, rounds):
pt = print_state(plaintext)
#KeyExpansion
key_state = KeyExpansion(key) #, rounds=rounds + 1)
round_key = key_state[0:4]
round_key = b''.join(round_key)
round_key = print_state(round_key)
# pre withenting
state = AddRoundKey(pt, round_key)
#print("AddRoundKey: \n",state)
for round in range(1,rounds):
#LOOP(rounds-1)
state = SubBytes(state)
state = ShiftRows(state)
state = MixColumns(state)
#get round key
round_key = key_state[4 * round : 4 * (round+1)]
round_key = b''.join(round_key)
round_key = print_state(round_key)
#print("RoundKey: \n", round_key)
#AddRoundKey
state = AddRoundKey(state, round_key)
#print("Runde: ",round)
#print(state)
state = SubBytes(state)
state = ShiftRows(state)
#get round key
round_key = key_state[4 * rounds : 4 * (rounds+1)]
round_key = b''.join(round_key)
round_key = print_state(round_key)
state = AddRoundKey(state, round_key)
return state
def setup(key, rounds):
#create delta set
delta_set = []
r = random.randint(0x00, 0xFF)
for i in range(256):
matrix = [r] * 16
matrix[0] = i
byteArrayObject = bytearray(matrix)
by = (bytes(byteArrayObject))
delta_set.append(by)
#print(print_state(delta_set[i]))
# encrypt the delta set with AES-3Round
result_set = []
for i in range(256):
#print("Plaintext: \n",print_state(delta_set[i]))
result_set.append(encryptWithRounds( delta_set[i], key, rounds ))
#print("Ciphertext: \n",print_state(result_set[i]))
return result_set
def reverse_state(key_guess, position, enc_ds):
position_in_state = (position % 4, position // 4)
r = []
i, j = position_in_state
for s in enc_ds:
#before_add_round_key = s[i, j] ^ key_guess
# not sure if ShiftRows is needed for the attack, because it will
#print(s)
#before_shift_rows = InvShiftRows(s)
before_add_round_key = s[i, j] ^ key_guess
before_sub_byte = sbox_en.index(before_add_round_key)
r.append(before_sub_byte)
#print(r, len(r))
#print("statepos: ",position_in_state)
return r
def checkKeyGuess(key_guess, position, reverse_states):
#position_in_state = (position % 4, position // 4)
xored = 0x00
for x in range(len(reverse_states)):
#print (reverse_states[x])
#print (hex(reverse_states[x][0][0]))
xored ^= reverse_states[x]
#print ('XORED ', xored)
return xored == 0