-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoundKey.inc
250 lines (230 loc) · 4.54 KB
/
RoundKey.inc
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
section .data
RConstantMatrix db 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
TIMES 30 db 0
RoundKeys TIMES 176 db 0
; 4*4*11 Matrix for all of the round keys.
column1 db 1,1,1,1
column2 db 2,2,2,2
section .text
; Takes:
; esi: matrix
; edi: destination
; eax: column
; edx: size of matrix row
GetColumn:
push edi
push eax
push ebx
mov ecx, 4
.next:
mov bl, BYTE [esi + eax]
mov BYTE [edi], bl
inc edi
add eax, edx
loop .next
pop ebx
pop eax
pop edi
ret
; Takes:
; esi: column
; edi: matrix
; eax: column to be replaced
; edx: size of the matrix row
PlaceColumn:
push esi
push eax
mov ecx, 4
.next:
mov bl, BYTE [esi]
mov BYTE [edi+eax], bl
inc esi
add eax, edx
loop .next
pop eax
pop esi
ret
; Takes:
; ecx: value of eax before the division
CreateFirstColumn:
push ecx
push ecx
mov esi, RConstantMatrix
mov edi, column1
mov edx, 10
sub eax, 1
call GetColumn
pop eax
mov esi, RoundKeys
mov edi, column2
mov edx, 44
sub eax, 1
call GetColumn
add eax, 1
push eax
; Shift the column by one byte
mov ebx, [column2]
ror ebx, 8
mov [column2], ebx
; Substitute the bytes
mov ecx, 4
mov esi, column2
mov edi, column2
call SubstituteMessage
; XOR W[i] and RCON
mov ebx, [column2]
mov eax, [column1]
xor eax, ebx
mov [column1], eax
; Get W[i-4]
pop eax
sub eax, 4
mov esi, RoundKeys
mov edi, column2
mov edx, 44
call GetColumn
add eax, 4
push eax
; XOR W[i] XOR RCON XOR W[i-4]
mov ebx, [column2]
mov eax, [column1]
xor eax, ebx
; Save it
mov [column1], eax
; Place the column at W[i]
pop eax
mov esi, column1
mov edi, RoundKeys
mov edx, 44
call PlaceColumn
pop ecx
ret
; Takes:
; esi: key
CreateRoundKeys:
; First copy the cipher key for Round 0
push esi ;save esi
xor eax, eax
.nextColumn:
mov edi, column1
mov esi, [esp]
mov edx, 4
call GetColumn
mov esi, column1
mov edi, RoundKeys
mov edx, 44
call PlaceColumn
inc eax
cmp eax, 4
jne .nextColumn
pop esi ;stack cleanup
; Calculate the rest of the Round keys
mov eax, 4 ; This is W
mov ecx, 40
.schedule:
push ecx
mov ebx, 4
mov edx, 0
mov ecx, eax
div ebx
cmp edx, 0
jne .NotAFirstColumn
call CreateFirstColumn
jmp .next
.NotAFirstColumn:
mov eax, ecx
mov ebx, 0
mov ecx, 2
.GetTheNextColumns:
push ecx
cmp ecx, 2
je .sub1
sub eax, 3
jmp .done
.sub1:
sub eax, 1
.done:
mov esi, RoundKeys
mov edi, column1
mov edx, 44
call GetColumn
xor ebx, [column1]
pop ecx
loop .GetTheNextColumns
mov [column1], ebx
add eax, 4
; Place the column at W[i]
mov esi, column1
mov edi, RoundKeys
mov edx, 44
call PlaceColumn
.next:
inc eax
pop ecx
loop .schedule
ret
PrintRoundKeys:
mov bl, 0
mov dl, 0
mov ecx, 176
.print:
xor eax, eax
mov al, [esi]
call WriteHex
inc esi
inc bl
inc dl
cmp bl, 44
jne .next
mov al, 0xA
call WriteChar
mov bl, 0
mov dl, 0
jmp .done
.next:
cmp dl, 4
je .tab
mov al, ' '
call WriteChar
jmp .done
.tab:
mov al, ' '
call WriteChar
mov al, ' '
call WriteChar
mov dl, 0
.done:
loop .print
ret
; Takes:
; edi: variable to store the round in
; eax: the number of the round
GetRoundKey:
push edi
mov ebx, 4
mul ebx
mov edx, eax
mov ecx, 4
.GetNextColumn:
push ecx
mov esi, RoundKeys
mov edi, column1
push edx
mov edx, 44
call GetColumn
pop edx
push eax
sub eax, edx
mov esi, column1
mov edi, [esp+0x8]
mov ebx, 4
push edx
mov edx, 4
call PlaceColumn
pop edx
pop eax
inc eax
pop ecx
loop .GetNextColumn
pop edi
ret