-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaes.asm
223 lines (195 loc) · 5.88 KB
/
aes.asm
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
10 ORG 100H
20 JP MAIN
ABLEN EQU 16 ; AES_BLOCKLEN
AKLEN EQU 32 ; AES_KEYLEN
AKESIZE EQU 240 ; AES_keyExpSize
NK EQU 8
NB EQU 4
NR EQU 14
4000GETSB: ; getSBoxValue
4010 PUSH IX ; IN BC-->OFFSET
4020 LD IX, SBOX ; OUT A --> VALUE
4030 ADD IX, BC
4040 LD A,(IX)
4050 POP IX
4060 RET
4070GETRSB: ; getSBoxInvert
4080 PUSH IX ; IN BC-->OFFSET
4090 LD IX, RSBOX ; OUT A --> VALUE
4100 ADD IX, BC
4110 LD A,(HL)
4120 POP IX
4130 RET
4070GETRCON:
4080 PUSH IX ; IN BC-->OFFSET
4090 LD IX, RCON ; OUT A --> VALUE
4100 ADD IX, BC
4110 LD A,(HL)
4120 POP IX
4130 RET
VARI: DB 0
VARK: DB 0
4200KEXPAN: LD HL, KEY
LD DE, RNDKEY
LD BC, 32
LDIR; On va faire simple, hein?
; for (i = 0; i < Nk; ++i) {
; RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
; RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
; RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
; RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
; }
; RoundKey[0] = Key[0]
; RoundKey[1] = Key[1]
; ...
; RoundKey[31] = Key[31]
; for (i = Nk; i < Nb * (Nr + 1); ++i) {
; 8 --> 60 (4 x (14+1))
; 28 --> 235
LD (VARI),8
LD (VARK),28
LD HL RNDKEY
LD BC, 28
ADD HL, BC
LD DE,TEMPA
LD BC, 4
LDIR
; {
; k = (i - 1) * 4;
; tempa[0] = RoundKey[k + 0];
; tempa[1] = RoundKey[k + 1];
; tempa[2] = RoundKey[k + 2];
; tempa[3] = RoundKey[k + 3];
; }
LD B,(VARI)
CALL MOD8
JP NZ, KXPAN0
; if (i % Nk == 0) {
LD B,(TEMPA)
; uint8_t u8tmp = tempa[0];
LD DE,TEMPA
LD HL,TEMPA
INC HL
LD BC, 3
LDIR
; tempa[0] = tempa[1];
; tempa[1] = tempa[2];
; tempa[2] = tempa[3];
DEC HL
LD (HL),B
; tempa[3] = u8tmp;
LD DE,TEMPA
LD B,(DE)
LD C,0
CALL GETSB
LD (DE), A
; tempa[0] = getSBoxValue(tempa[0]);
INC DE
LD B,(DE)
LD C,0
CALL GETSB
LD (DE), A
; tempa[1] = getSBoxValue(tempa[1]);
INC DE
LD B,(DE)
LD C,0
CALL GETSB
LD (DE), A
; tempa[2] = getSBoxValue(tempa[2]);
INC DE
LD B,(DE)
LD C,0
CALL GETSB
LD (DE), A
; tempa[3] = getSBoxValue(tempa[3]);
; tempa[0] = tempa[0] ^ Rcon[i / Nk];
LD A,(VARI)
RRCA
RRCA
RRCA ; DIVIDE BY 8 (NK)
LD DE,TEMPA
LD B,(DE)
XOR B
LD B,A
LD C,0
CALL GETRCON
LD (DE),A
KXPAN0:
4000MOD8: LD A, B ; IN B = VALUE TO TEST
4010 RRCA
4020 RRCA
4030 RRCA ; DIVIDE BY 8
4040 RLCA
4050 RLCA
4060 RLCA ; MULTIPLY BY 8
4070 CP B ; SAME? --> Z
4080 RET ; OUT Z/NZ
5000SBOX: DB 063H, 07CH, 077H, 07BH, 0F2H, 06BH, 06FH, 0C5H,
5010 DB 030H, 001H, 067H, 02BH, 0FEH, 0D7H, 0ABH, 076H,
5020 DB 0CAH, 082H, 0C9H, 07DH, 0FAH, 059H, 047H, 0F0H,
5030 DB 0ADH, 0D4H, 0A2H, 0AFH, 09CH, 0A4H, 072H, 0C0H,
5040 DB 0B7H, 0FDH, 093H, 026H, 036H, 03FH, 0F7H, 0CCH,
5050 DB 034H, 0A5H, 0E5H, 0F1H, 071H, 0D8H, 031H, 015H,
5060 DB 004H, 0C7H, 023H, 0C3H, 018H, 096H, 005H, 09AH,
5070 DB 007H, 012H, 080H, 0E2H, 0EBH, 027H, 0B2H, 075H,
5080 DB 009H, 083H, 02CH, 01AH, 01BH, 06EH, 05AH, 0A0H,
5090 DB 052H, 03BH, 0D6H, 0B3H, 029H, 0E3H, 02FH, 084H,
5100 DB 053H, 0D1H, 000H, 0EDH, 020H, 0FCH, 0B1H, 05BH,
5110 DB 06AH, 0CBH, 0BEH, 039H, 04AH, 04CH, 058H, 0CFH,
5120 DB 0D0H, 0EFH, 0AAH, 0FBH, 043H, 04DH, 033H, 085H,
5130 DB 045H, 0F9H, 002H, 07FH, 050H, 03CH, 09FH, 0A8H,
5140 DB 051H, 0A3H, 040H, 08FH, 092H, 09DH, 038H, 0F5H,
5150 DB 0BCH, 0B6H, 0DAH, 021H, 010H, 0FFH, 0F3H, 0D2H,
5160 DB 0CDH, 00CH, 013H, 0ECH, 05FH, 097H, 044H, 017H,
5170 DB 0C4H, 0A7H, 07EH, 03DH, 064H, 05DH, 019H, 073H,
5180 DB 060H, 081H, 04FH, 0DCH, 022H, 02AH, 090H, 088H,
5190 DB 046H, 0EEH, 0B8H, 014H, 0DEH, 05EH, 00BH, 0DBH,
5200 DB 0E0H, 032H, 03AH, 00AH, 049H, 006H, 024H, 05CH,
5210 DB 0C2H, 0D3H, 0ACH, 062H, 091H, 095H, 0E4H, 079H,
5220 DB 0E7H, 0C8H, 037H, 06DH, 08DH, 0D5H, 04EH, 0A9H,
5230 DB 06CH, 056H, 0F4H, 0EAH, 065H, 07AH, 0AEH, 008H,
5240 DB 0BAH, 078H, 025H, 02EH, 01CH, 0A6H, 0B4H, 0C6H,
5250 DB 0E8H, 0DDH, 074H, 01FH, 04BH, 0BDH, 08BH, 08AH,
5260 DB 070H, 03EH, 0B5H, 066H, 048H, 003H, 0F6H, 00EH,
5270 DB 061H, 035H, 057H, 0B9H, 086H, 0C1H, 01DH, 09EH,
5280 DB 0E1H, 0F8H, 098H, 011H, 069H, 0D9H, 08EH, 094H,
5290 DB 09BH, 01EH, 087H, 0E9H, 0CEH, 055H, 028H, 0DFH,
5300 DB 08CH, 0A1H, 089H, 00DH, 0BFH, 0E6H, 042H, 068H,
5310 DB 041H, 099H, 02DH, 00FH, 0B0H, 054H, 0BBH, 016H
5320RSBOX: DB 052H, 009H, 06AH, 0D5H, 030H, 036H, 0A5H, 038H,
5330 DB 0BFH, 040H, 0A3H, 09EH, 081H, 0F3H, 0D7H, 0FBH,
5340 DB 07CH, 0E3H, 039H, 082H, 09BH, 02FH, 0FFH, 087H,
5350 DB 034H, 08EH, 043H, 044H, 0C4H, 0DEH, 0E9H, 0CBH,
5360 DB 054H, 07BH, 094H, 032H, 0A6H, 0C2H, 023H, 03DH,
5370 DB 0EEH, 04CH, 095H, 00BH, 042H, 0FAH, 0C3H, 04EH,
5380 DB 008H, 02EH, 0A1H, 066H, 028H, 0D9H, 024H, 0B2H,
5390 DB 076H, 05BH, 0A2H, 049H, 06DH, 08BH, 0D1H, 025H,
5400 DB 072H, 0F8H, 0F6H, 064H, 086H, 068H, 098H, 016H,
5410 DB 0D4H, 0A4H, 05CH, 0CCH, 05DH, 065H, 0B6H, 092H,
5420 DB 06CH, 070H, 048H, 050H, 0FDH, 0EDH, 0B9H, 0DAH,
5430 DB 05EH, 015H, 046H, 057H, 0A7H, 08DH, 09DH, 084H,
5440 DB 090H, 0D8H, 0ABH, 000H, 08CH, 0BCH, 0D3H, 00AH,
5450 DB 0F7H, 0E4H, 058H, 005H, 0B8H, 0B3H, 045H, 006H,
5460 DB 0D0H, 02CH, 01EH, 08FH, 0CAH, 03FH, 00FH, 002H,
5470 DB 0C1H, 0AFH, 0BDH, 003H, 001H, 013H, 08AH, 06BH,
5480 DB 03AH, 091H, 011H, 041H, 04FH, 067H, 0DCH, 0EAH,
5490 DB 097H, 0F2H, 0CFH, 0CEH, 0F0H, 0B4H, 0E6H, 073H,
5500 DB 096H, 0ACH, 074H, 022H, 0E7H, 0ADH, 035H, 085H,
5510 DB 0E2H, 0F9H, 037H, 0E8H, 01CH, 075H, 0DFH, 06EH,
5520 DB 047H, 0F1H, 01AH, 071H, 01DH, 029H, 0C5H, 089H,
5530 DB 06FH, 0B7H, 062H, 00EH, 0AAH, 018H, 0BEH, 01BH,
5540 DB 0FCH, 056H, 03EH, 04BH, 0C6H, 0D2H, 079H, 020H,
5550 DB 09AH, 0DBH, 0C0H, 0FEH, 078H, 0CDH, 05AH, 0F4H,
5560 DB 01FH, 0DDH, 0A8H, 033H, 088H, 007H, 0C7H, 031H,
5570 DB 0B1H, 012H, 010H, 059H, 027H, 080H, 0ECH, 05FH,
5580 DB 060H, 051H, 07FH, 0A9H, 019H, 0B5H, 04AH, 00DH,
5590 DB 02DH, 0E5H, 07AH, 09FH, 093H, 0C9H, 09CH, 0EFH,
5600 DB 0A0H, 0E0H, 03BH, 04DH, 0AEH, 02AH, 0F5H, 0B0H,
5610 DB 0C8H, 0EBH, 0BBH, 03CH, 083H, 053H, 099H, 061H,
5620 DB 017H, 02BH, 004H, 07EH, 0BAH, 077H, 0D6H, 026H,
5630 DB 0E1H, 069H, 014H, 063H, 055H, 021H, 00CH, 07DH
5330RNDKEY: DEFS AKESIZE ; AES_ctx / RoundKey
5340IV: DEFS ABLEN ; AES_ctx / Iv
5350RCON: DB 08DH, 001H, 002H, 004H, 008H, 010H, 020H, 040H, 080H, 01BH, 036H
5360KEY: DEFS AKLEN
5370TEMPA: DB 0,0,0,0