-
Notifications
You must be signed in to change notification settings - Fork 2
/
AES_ExpandKey128_R.S
220 lines (215 loc) · 5.05 KB
/
AES_ExpandKey128_R.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
#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
# #
# AVR-AES-Faster Library #
# (c) 2020 Rados³aw Gancarz <radgan99@gmail.com> #
# #
# This Source Code Form is subject to the terms of the Mozilla Public #
# License, v. 2.0. If a copy of the MPL was not distributed with this #
# file, You can obtain one at http://mozilla.org/MPL/2.0/. #
# #
#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#
#include "AVR-AES-Faster-devel.h"
#ifdef AES_BENCHMARK
#include <avr/io.h>
#endif
#============================================================================
#
# AES_ExpandKey128_R
#
# AES Key Expansion - 128 bits, S-box in RAM
#
# Input : STxx(key), XKEYPTR(&expanded_key[0][0])
# Output : (memory)
# Modified: Z, RCON, STxx, KEYPTR(temp)
#
#define ST00 r0
#define ST10 r1
#define ST20 r2
#define ST30 r3
#define ST01 r4
#define ST11 r5
#define ST21 r6
#define ST31 r7
#define ST02 r8
#define ST12 r9
#define ST22 r10
#define ST32 r11
#define ST03 r12
#define ST13 r13
#define ST23 r14
#define ST33 r15
#define RCON r25
#define KEYPTR_L r26
#define KEYPTR_H r27
#define XKEYPTR_L r28
#define XKEYPTR_H r29
#define KEYPTR X
#define XKEYPTR Y
.section .text.AES_ExpandKey128_R,"ax",@progbits
.global AES_ExpandKey128_R
AES_ExpandKey128_R:
#ifdef AES_BENCHMARK
lds r30,_SFR_MEM_ADDR(TCNT1L)
sts benchmark_data+0,r30
lds r30,_SFR_MEM_ADDR(TCNT1H)
sts benchmark_data+1,r30
#endif
push ST20
push ST30
push ST01
push ST11
push ST21
push ST31
push ST02
push ST12
push ST22
push ST32
push ST03
push ST13
push ST23
push ST33
push XKEYPTR_L
push XKEYPTR_H
movw KEYPTR_L,r24
movw XKEYPTR_L,r22
#Load_State
ld ST00,KEYPTR+
ld ST01,KEYPTR+
ld ST02,KEYPTR+
ld ST03,KEYPTR+
ld ST10,KEYPTR+
ld ST11,KEYPTR+
ld ST12,KEYPTR+
ld ST13,KEYPTR+
ld ST20,KEYPTR+
ld ST21,KEYPTR+
ld ST22,KEYPTR+
ld ST23,KEYPTR+
ld ST30,KEYPTR+
ld ST31,KEYPTR+
ld ST32,KEYPTR+
ld ST33,KEYPTR+
#ifdef AES_BENCHMARK
lds r30,_SFR_MEM_ADDR(TCNT1L)
sts benchmark_data+2,r30
lds r30,_SFR_MEM_ADDR(TCNT1H)
sts benchmark_data+3,r30
AES_ExpandKey128_R_start1:
.global AES_ExpandKey128_R_start1
#endif
#------------- Real function starts here -------------
ldi zh,hi8(AES_SBox_R)
#nr=10 nk=4 4nr/nk=10 max_rcon=0x6c
ldi RCON,1
rjmp 101f
100:
mov zl,ST30
ld KEYPTR_L,Z
mov zl,ST31
ld KEYPTR_H,Z
eor ST00,KEYPTR_H
mov zl,ST32
ld KEYPTR_H,Z
eor ST01,KEYPTR_H
mov zl,ST33
ld KEYPTR_H,Z
eor ST02,KEYPTR_H
eor ST03,KEYPTR_L
eor ST00,RCON
# xtime2_rcon
lsl RCON
brcc 1f
ldi RCON,0x1b
1:
eor ST10,ST00
eor ST11,ST01
eor ST12,ST02
eor ST13,ST03
eor ST20,ST10
eor ST21,ST11
eor ST22,ST12
eor ST23,ST13
eor ST30,ST20
eor ST31,ST21
eor ST32,ST22
eor ST33,ST23
101:
st XKEYPTR+,ST00
st XKEYPTR+,ST01
st XKEYPTR+,ST02
st XKEYPTR+,ST03
st XKEYPTR+,ST10
st XKEYPTR+,ST11
st XKEYPTR+,ST12
st XKEYPTR+,ST13
st XKEYPTR+,ST20
st XKEYPTR+,ST21
st XKEYPTR+,ST22
st XKEYPTR+,ST23
st XKEYPTR+,ST30
st XKEYPTR+,ST31
st XKEYPTR+,ST32
st XKEYPTR+,ST33
cpi RCON,0x6c
brne 100b
#-------------- Real function ends here --------------
#ifdef AES_BENCHMARK
AES_ExpandKey128_R_end1:
.global AES_ExpandKey128_R_end1
lds r30,_SFR_MEM_ADDR(TCNT1L)
sts benchmark_data+4,r30
lds r30,_SFR_MEM_ADDR(TCNT1H)
sts benchmark_data+5,r30
#endif
pop XKEYPTR_H
pop XKEYPTR_L
pop ST33
pop ST23
pop ST13
pop ST03
pop ST32
pop ST22
pop ST12
pop ST02
pop ST31
pop ST21
pop ST11
pop ST01
pop ST30
pop ST20
eor r1, r1
#ifdef AES_BENCHMARK
lds r30,_SFR_MEM_ADDR(TCNT1L)
sts benchmark_data+6,r30
lds r30,_SFR_MEM_ADDR(TCNT1H)
sts benchmark_data+7,r30
#endif
ret
#ifdef AES_BENCHMARK
AES_ExpandKey128_R_end:
.global AES_ExpandKey128_R_end
#endif
.size AES_ExpandKey128_R,.-AES_ExpandKey128_R
#undef ST00
#undef ST10
#undef ST20
#undef ST30
#undef ST01
#undef ST11
#undef ST21
#undef ST31
#undef ST02
#undef ST12
#undef ST22
#undef ST32
#undef ST03
#undef ST13
#undef ST23
#undef ST33
#undef RCON
#undef KEYPTR_L
#undef KEYPTR_H
#undef XKEYPTR_L
#undef XKEYPTR_H
#undef KEYPTR
#undef XKEYPTR