-
Notifications
You must be signed in to change notification settings - Fork 6
/
dec_ca16.inc
397 lines (313 loc) · 11.2 KB
/
dec_ca16.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
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
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for BASM16/Compressed table
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed table
0.11 10.07.06 we Removed bx in TCd[bx+si+?]
0.12 13.07.06 we Uses TCd box byte instead of InvSBox
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{16 bit BASM used for TP6, BP7, Delphi1}
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
s,t: TAESBlock;
r: integer;
pK: pointer;
begin
r := ctx.Rounds-1;
pK := @ctx.RK[ctx.Rounds];
asm
{AES_XorBlock(BI, ctx.RK[ctx.Rounds], s);}
db $66; pusha
les si,[BI]
db $66; mov ax,es:[si]
db $66; mov bx,es:[si+4]
db $66; mov cx,es:[si+8]
db $66; mov dx,es:[si+12]
les di,[pK]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
db $66; mov word ptr [s],ax
db $66; mov word ptr [s+4],bx
db $66; mov word ptr [s+8],cx
db $66; mov word ptr [s+12],dx
sub di,16 {di -> ctx.RK[r]}
mov cx,[r]
{ *Note* in the following round loop }
{ op eax, mem[8*ebx] is calculated as }
{ lea esi, [edx+8*ebx] $66,$67,$8D,$34,$DA }
{ op eax, mem[esi] }
db $66; sub bx,bx {clear ebx}
db $66; sub dx,dx {clear edx}
@@1:
{TWA4(t)[3] := Td0[s[3*4+0]] xor Td1[s[2*4+1]] xor Td2[s[1*4+2]] xor Td3[s[0*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr s[3*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[2*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[1*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[0*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di+12]
db $66; mov word ptr t[12],ax
{TWA4(t)[2] := Td0[s[2*4+0]] xor Td1[s[1*4+1]] xor Td2[s[0*4+2]] xor Td3[s[3*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr s[2*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[1*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[0*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[3*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di+8]
db $66; mov word ptr t[8],ax
{TWA4(t)[1] := Td0[s[1*4+0]] xor Td1[s[0*4+1]] xor Td2[s[3*4+2]] xor Td3[s[2*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr s[1*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[0*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[3*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[2*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di+4]
db $66; mov word ptr t[4],ax
{TWA4(t)[0] := Td0[s[0*4+0]] xor Td1[s[3*4+1]] xor Td2[s[2*4+2]] xor Td3[s[1*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr s[0*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[3*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[2*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[1*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di]
db $66; mov word ptr t[0],ax
{ dec(r); if r<1 then break;}
sub cx,1
jle @@2
{TWA4(s)[3] := Td0[t[3*4+0]] xor Td1[t[2*4+1]] xor Td2[t[1*4+2]] xor Td3[t[0*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr t[3*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[2*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[1*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[0*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-4]
db $66; mov word ptr s[12],ax
{TWA4(s)[2] := Td0[t[2*4+0]] xor Td1[t[1*4+1]] xor Td2[t[0*4+2]] xor Td3[t[3*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr t[2*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[1*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[0*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[3*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-8]
db $66; mov word ptr s[8],ax
{TWA4(s)[1] := Td0[t[1*4+0]] xor Td1[t[0*4+1]] xor Td2[t[3*4+2]] xor Td3[t[2*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr t[1*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[0*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[3*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[2*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-12]
db $66; mov word ptr s[4],ax
{TWA4(s)[0] := Td0[t[0*4+0]] xor Td1[t[3*4+1]] xor Td2[t[2*4+2]] xor Td3[t[1*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr t[0*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[3*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[2*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[1*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-16]
db $66; mov word ptr s[0],ax
sub di,32
dec cx
jmp @@1
@@2: sub di,16 {di -> ctx.RK[0]}
sub bx,bx
mov bl, byte ptr t[0*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[0],al
mov bl, byte ptr t[3*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[1],al
mov bl, byte ptr t[2*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[2],al
mov bl, byte ptr t[1*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[3],al
mov bl, byte ptr t[1*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[4],al
mov bl, byte ptr t[0*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[5],al
mov bl, byte ptr t[3*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[6],al
mov bl, byte ptr t[2*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[7],al
mov bl, byte ptr t[2*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[8],al
mov bl, byte ptr t[1*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[9],al
mov bl, byte ptr t[0*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[10],al
mov bl, byte ptr t[3*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[11],al
mov bl, byte ptr t[3*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[12],al
mov bl, byte ptr t[2*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[13],al
mov bl, byte ptr t[1*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[14],al
mov bl, byte ptr t[0*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[15],al
{AES_XorBlock(s, ctx.RK[0], BO);}
db $66; mov ax,word ptr [s]
db $66; mov bx,word ptr [s+4]
db $66; mov cx,word ptr [s+8]
db $66; mov dx,word ptr [s+12]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
les si,[BO]
db $66; mov es:[si],ax
db $66; mov es:[si+4],bx
db $66; mov es:[si+8],cx
db $66; mov es:[si+12],dx
db $66; popa
end;
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
n: integer;
p: PLong;
begin
p := Plong(@ctx.RK[1]);
n := 4*(ctx.Rounds-1);
{BASM version of 16 bit code, no need for local x/t}
{implicit endian conversion compared with [2]}
asm
les si,[p]
mov cx,[n]
@@1: mov dx,es:[si]
sub bh,bh
mov bl,dl
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; mov ax,word ptr TCd[bx+3]
sub bh,bh
mov bl,dh
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; xor ax,word ptr TCd[bx+2]
mov dx,es:[si+2]
sub bh,bh
mov bl,dl
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; xor ax,word ptr TCd[bx+1]
sub bh,bh
mov bl,dh
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; xor ax,word ptr TCd[bx]
db $66; mov es:[si],ax
add si,4
dec cx
jnz @@1
end;
end;