forked from DavyLandman/AESLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes_enc-asm.S
341 lines (290 loc) · 4.7 KB
/
aes_enc-asm.S
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
/* aes_enc-asm.S */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008, 2009 Daniel Otte ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file aes_enc-asm.S
* \email [email protected]
* \author Daniel Otte
* \date 2009-01-10
* \license GPLv3 or later
*
*/
#include "avr-asm-macros.S"
/*
* param a: r24
* param b: r22
* param reducer: r0
*/
A = 28
B = 29
P = 0
xREDUCER = 25
.global aes256_enc
aes256_enc:
ldi r20, 14
rjmp aes_encrypt_core
.global aes192_enc
aes192_enc:
ldi r20, 12
rjmp aes_encrypt_core
.global aes128_enc
aes128_enc:
ldi r20, 10
/*
void aes_encrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds)
*/
T0= 2
T1= 3
T2= 4
T3= 5
SBOX_SAVE0 = 6
SBOX_SAVE1 = 7
ST00 = 8
ST01 = 9
ST02 = 10
ST03 = 11
ST10 = 12
ST11 = 13
ST12 = 14
ST13 = 15
ST20 = 16
ST21 = 17
ST22 = 18
ST23 = 19
ST30 = 20
ST31 = 21
ST32 = 22
ST33 = 23
CTR = 24
/*
* param state: r24:r25
* param ks: r22:r23
* param rounds: r20
*/
.global aes_encrypt_core
aes_encrypt_core:
push_range 2, 17
push r28
push r29
push r24
push r25
movw r26, r22
movw r30, r24
mov CTR, r20
clt
.irp param,ST00, ST01, ST02, ST03, ST10, ST11, ST12, ST13, ST20, ST21, ST22, ST23, ST30, ST31, ST32, ST33
ld \param, Z+
.endr
ldi xREDUCER, 0x1b /* load reducer */
ldi r31, hi8(aes_sbox)
/* key whitening */
1:
.irp param,ST00, ST01, ST02, ST03, ST10, ST11, ST12, ST13, ST20, ST21, ST22, ST23, ST30, ST31, ST32, ST33
ld r0, X+
eor \param, r0
.endr
brtc 2f
exit:
pop r31
pop r30
st Z+, ST00
st Z+, ST01
st Z+, ST02
st Z+, ST03
st Z+, ST10
st Z+, ST11
st Z+, ST12
st Z+, ST13
st Z+, ST20
st Z+, ST21
st Z+, ST22
st Z+, ST23
st Z+, ST30
st Z+, ST31
st Z+, ST32
st Z+, ST33
pop r29
pop r28
pop_range 2, 17
ret
2: dec CTR
brne 3f
set
3:
/* encryption loop */
/* SBOX substitution and shifting */
mov r30, ST00
lpm ST00, Z
mov r30, ST10
lpm ST10, Z
mov r30, ST20
lpm ST20, Z
mov r30, ST30
lpm ST30, Z
mov r30, ST01
lpm T0, Z
mov r30, ST11
lpm ST01, Z
mov r30, ST21
lpm ST11, Z
mov r30, ST31
lpm ST21, Z
mov ST31, T0
mov r30, ST02
lpm T0, Z
mov r30, ST12
lpm T1, Z
mov r30, ST22
lpm ST02, Z
mov r30, ST32
lpm ST12, Z
mov ST22, T0
mov ST32, T1
mov r30, ST03
lpm T0, Z
mov r30, ST33
lpm ST03, Z
mov r30, ST23
lpm ST33, Z
mov r30, ST13
lpm ST23, Z
mov ST13, T0
/* mixcols (or rows in our case) */
brtc 2f
rjmp 1b
2:
/* mixrow 1 */
mov r0, ST02
eor r0, ST03
mov T2, r0
mov T0, ST00
eor ST00, ST01
eor r0, ST00
lsl ST00
brcc 3f
eor ST00, xREDUCER
3: eor ST00, r0
eor ST00, T0
mov T1, ST01
eor T1, ST02
lsl T1
brcc 3f
eor T1, xREDUCER
3: eor T1, r0
eor ST01, T1
lsl T2
brcc 3f
eor T2, xREDUCER
3: eor T2, r0
eor ST02, T2
eor T0, ST03
lsl T0
brcc 3f
eor T0, xREDUCER
3: eor T0, r0
eor ST03, T0
/* mixrow 2 */
mov r0, ST12
eor r0, ST13
mov T2, r0
mov T0, ST10
eor ST10, ST11
eor r0, ST10
lsl ST10
brcc 3f
eor ST10, xREDUCER
3: eor ST10, r0
eor ST10, T0
mov T1, ST11
eor T1, ST12
lsl T1
brcc 3f
eor T1, xREDUCER
3: eor T1, r0
eor ST11, T1
lsl T2
brcc 3f
eor T2, xREDUCER
3: eor T2, r0
eor ST12, T2
eor T0, ST13
lsl T0
brcc 3f
eor T0, xREDUCER
3: eor T0, r0
eor ST13, T0
/* mixrow 3 */
mov r0, ST22
eor r0, ST23
mov T2, r0
mov T0, ST20
eor ST20, ST21
eor r0, ST20
lsl ST20
brcc 3f
eor ST20, xREDUCER
3: eor ST20, r0
eor ST20, T0
mov T1, ST21
eor T1, ST22
lsl T1
brcc 3f
eor T1, xREDUCER
3: eor T1, r0
eor ST21, T1
lsl T2
brcc 3f
eor T2, xREDUCER
3: eor T2, r0
eor ST22, T2
eor T0, ST23
lsl T0
brcc 3f
eor T0, xREDUCER
3: eor T0, r0
eor ST23, T0
/* mixrow 4 */
mov r0, ST32
eor r0, ST33
mov T2, r0
mov T0, ST30
eor ST30, ST31
eor r0, ST30
lsl ST30
brcc 3f
eor ST30, xREDUCER
3: eor ST30, r0
eor ST30, T0
mov T1, ST31
eor T1, ST32
lsl T1
brcc 3f
eor T1, xREDUCER
3: eor T1, r0
eor ST31, T1
lsl T2
brcc 3f
eor T2, xREDUCER
3: eor T2, r0
eor ST32, T2
eor T0, ST33
lsl T0
brcc 3f
eor T0, xREDUCER
3: eor T0, r0
eor ST33, T0
/* mix colums (rows) done */
/* add key*/
rjmp 1b