-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColumnMixing.inc
257 lines (236 loc) · 5.9 KB
/
ColumnMixing.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
251
252
253
254
255
256
257
section .data
galiosField db 2, 3, 1, 1,
db 1, 2, 3, 1,
db 1, 1, 2, 3,
db 3, 1, 1, 2
galiosFieldInverse db 0x0E, 0x0B, 0x0D, 0x09,
db 0x09, 0x0E, 0x0B, 0x0D,
db 0x0D, 0x09, 0x0E, 0x0B,
db 0x0B, 0x0D, 0x09, 0x0E
galiosIndexI dd 0
galiosIndexJ dd 0
messageIndexI dd 0
messageIndexJ dd 0
section .text
; Takes:
; eax: vertical index (i)
; ebx: horizontal index (j)
; Returns:
; dl: value of the byte
GetGaliosNumber:
push eax
push ebx
mov edx, 4
mul edx
xor edx, edx
mov dl, [galiosField+ebx+eax]
pop ebx
pop eax
ret
; Takes:
; dl: value
; Returns:
; dl: result multiplied by 2
AdvancedMultiplyByTwo:
shl dl, 1
jnc .done
xor dl, 0x1B
.done: ret
; Takes:
; dl: value
; Returns:
; dl: result multiplied by 3
AdvancedMultiplyByThree:
mov dh, dl
call AdvancedMultiplyByTwo
xor dl, dh
ret
; Takes:
; dl: value
; Returns:
; dl: result multiplied by 9
AdvancedMultiplyByNine:
mov dh, dl ;x0
call AdvancedMultiplyByTwo
; dl now contains x1=x0*2
call AdvancedMultiplyByTwo
; dl now contains x2=x1*2
call AdvancedMultiplyByTwo
; dl now contains x3=x2*2
xor dl, dh
ret
; Takes:
; dl: value
; Returns:
; dl: result multiplied by 11
AdvancedMultiplyBy0B:
push eax
mov dh, dl ;x0
call AdvancedMultiplyByTwo
;dl now contains x1=x0*2
mov al, dl
call AdvancedMultiplyByTwo
;dl now contains x2=x1*2
call AdvancedMultiplyByTwo
;dl now contains x3=x2*2
xor dl, dh
xor dl, al
pop eax
ret
; Takes:
; dl: value
; Returns:
; dl: result multiplied by 13
AdvancedMultiplyBy0D:
push eax
mov dh, dl ;save x0 in dh
call AdvancedMultiplyByTwo
;dl now contains x1=x0*2
mov al, dl ;save x1 in al
call AdvancedMultiplyByTwo
;dl now contains x2=x1*2
mov ah, dl ;save x2 in ah
call AdvancedMultiplyByTwo
;dl now contains x3=x2*2
xor dl, dh
xor dl, ah
pop eax
ret
; Takes:
; dl: value
; Returns:
; dl: result multiplied by 11
AdvancedMultiplyBy0E:
push eax
call AdvancedMultiplyByTwo
;dl now contains x1=x0*2
mov al, dl ;save x1 in al
call AdvancedMultiplyByTwo
;dl now contains x2=x1*2
mov ah, dl ;save x2 in ah
call AdvancedMultiplyByTwo
;dl now contains x3=x2*2
xor dl, al
xor dl, ah
pop eax
ret
; Takes:
; eax: Row
; ebx: Column
; esi: Address of the source message
; edi: Address of the destination message
; Multiplies the row from galio's matrix by the column from the message
MultiplyRowByColumn:
mov [galiosIndexI], eax
mov [messageIndexJ], ebx
mov DWORD [galiosIndexJ], 0
mov DWORD [messageIndexI], 0
push eax
push ebx
mov ecx, 0
.rows:
mov eax, [galiosIndexI]
mov ebx, [galiosIndexJ]
call GetGaliosNumber
inc DWORD [galiosIndexJ]
mov eax, [messageIndexJ]
mov ebx, [messageIndexI]
shl ebx, 2
add eax, ebx
cmp dl, 2
jne .three
mov dl, [esi + eax]
call AdvancedMultiplyByTwo
movzx edx, dl
xor ecx, edx
jmp .done
.three:
cmp dl, 3
jne .nine
mov dl, [esi + eax]
call AdvancedMultiplyByThree
movzx edx, dl
xor ecx, edx
jmp .done
.nine:
cmp dl,9
jne .zeroB
mov dl, [esi + eax]
call AdvancedMultiplyByNine
movzx edx,dl
xor ecx,edx
jmp .done
.zeroB:
cmp dl,0x0B
jne .zeroD
mov dl, [esi + eax]
call AdvancedMultiplyBy0B
movzx edx,dl
xor ecx,edx
jmp .done
.zeroD:
cmp dl,0x0D
jne .zeroE
mov dl, [esi + eax]
call AdvancedMultiplyBy0D
movzx edx,dl
xor ecx,edx
jmp .done
.zeroE:
cmp dl,0x0E
jne .one
mov dl, [esi + eax]
call AdvancedMultiplyBy0E
movzx edx,dl
xor ecx,edx
jmp .done
.one:
mov dl, [esi + eax]
movzx edx, dl
xor ecx, edx
.done:
inc DWORD [messageIndexI]
cmp DWORD [galiosIndexJ], 4
jl .rows
pop ebx
pop eax
xchg eax, ebx
shl ebx, 2
add eax, ebx
mov [edi+eax], cl
ret
; Takes:
; esi: Address of the source message
; edi: Address of the destination message
MixColumns:
mov eax, 0
mov ebx, 0
.nextColumn:
mov eax, 0
.nextRow:
push eax
push ebx
call MultiplyRowByColumn
pop ebx
pop eax
inc eax
cmp eax, 4
jl .nextRow
inc ebx
cmp ebx, 4
jl .nextColumn
ret
;Prepares the galios field during the decryption phase
GaliosDecryption:
push eax
mov ecx, 16
cld
mov esi, galiosFieldInverse
mov edi, galiosField
rep movsb
pop eax
mov DWORD [galiosIndexI],0
mov DWORD [galiosIndexJ],0
mov DWORD [messageIndexI],0
mov DWORD [messageIndexJ],0
ret