]> git.cryptolib.org Git - avr-crypto-lib.git/blob - xtea/xtea-asm.S
optimized xtea asm implementation
[avr-crypto-lib.git] / xtea / xtea-asm.S
1 /* xtea-enc.S */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "avr-asm-macros.S"
21
22 B0 =  4
23 B1 =  5
24 B2 =  6
25 B3 =  7
26
27 A0 =  8
28 A1 =  9
29 A2 = 10
30 A3 = 11
31
32 V10 = 12
33 V11 = 13
34 V12 = 14
35 V13 = 15
36
37 V00 = 16
38 V01 = 17
39 V02 = 18
40 V03 = 19
41
42 S0 = 20
43 S1 = 21
44 S2 = 22
45 S3 = 23
46
47 xchg_V0V1:
48         movw r26, V10
49         movw V10, V00
50         movw V00, r26
51         movw r26, V12
52         movw V12, V02
53         movw V02, r26
54         ret
55
56 eor_AB:
57         eor A0, B0
58         eor A1, B1
59         eor A2, B2
60         eor A3, B3
61         ret
62
63 g_func:
64         movw A0, V10
65         movw A2, V12
66         movw B0, V10
67         movw B2, V12
68
69         ldi r24, 4
70 10:
71         lsl A0
72         rol A1
73         rol A2
74         rol A3
75         dec r24
76         brne 10b
77
78         ldi r24, 5
79 10:
80         lsr B3
81         ror B2
82         ror B1
83         ror B0
84         dec r24
85         brne 10b
86
87         rcall eor_AB
88
89         add A0, V10
90         adc A1, V11
91         adc A2, V12
92         adc A3, V13
93
94         ret
95
96 sum_plus_k:
97         andi r24, (3<<2)
98         movw r26, r30
99         add r26, r24
100         adc r27, r1
101         ld B0, X+
102         ld B1, X+
103         ld B2, X+
104         ld B3, X+
105         add B0, S0
106         adc B1, S1
107         adc B2, S2
108         adc B3, S3
109         rcall eor_AB
110         brtc 20f
111         add V00, A0
112         adc V01, A1
113         adc V02, A2
114         adc V03, A3
115         ret
116 20:     sub V00, A0
117         sbc V01, A1
118         sbc V02, A2
119         sbc V03, A3
120         ret
121
122 main1:
123         rcall g_func
124         mov r24, S0
125         lsl r24
126         lsl r24
127         rcall sum_plus_k
128         ret
129
130 main2:
131         rcall xchg_V0V1
132         rcall g_func
133         mov r24, S1
134         lsr r24
135         rcall sum_plus_k
136         rcall xchg_V0V1
137         ret
138
139 .global xtea_enc
140 xtea_enc:
141         set
142 xtea_intro:
143         clr r27
144         ldi r26, 4
145         ldi r30, 14
146 10:
147         ld r0, X+
148         push r0
149         dec r30
150         brne 10b
151
152         push r24
153         push r25
154         movw r30, r20
155 /* load block */
156         movw r26, r22
157         ld V00, X+
158         ld V01, X+
159         ld V02, X+
160         ld V03, X+
161         ld V10, X+
162         ld V11, X+
163         ld V12, X+
164         ld V13, X+
165         ldi r24, 32
166         mov r0, r24
167         brtc xtea_dec_start
168         clr S0
169         clr S1
170         movw S2, S0
171
172 10:
173         rcall main1
174         subi S0, 0x47
175         sbci S1, 0x86
176         sbci S2, 0xC8
177         sbci S3, 0x61
178         rcall main2
179
180         dec r0
181         brne 10b
182
183 /* store back */
184 xtea_enc_exit:
185         pop r27
186         pop r26
187         st X+, V00
188         st X+, V01
189         st X+, V02
190         st X+, V03
191         st X+, V10
192         st X+, V11
193         st X+, V12
194         st X+, V13
195
196         clr r27
197         ldi r26, 18
198         ldi r24, 14
199 10:
200         pop r0
201         st -X, r0
202         dec r24
203         brne 10b
204         ret
205
206
207 /******************************************************************************/
208 /******************************************************************************/
209 /******************************************************************************/
210 /******************************************************************************/
211
212 .global xtea_dec
213 xtea_dec:
214         clt
215         rjmp xtea_intro
216 xtea_dec_start:
217         ldi S0, 0x20 /* sum = 0xC6EF3720 */
218         ldi S1, 0x37
219         ldi S2, 0xEF
220         ldi S3, 0xC6
221
222 10:
223         rcall main2
224         subi S0, 0xB9
225         sbci S1, 0x79
226         sbci S2, 0x37
227         sbci S3, 0x9E
228         rcall main1
229
230         dec r0
231         brne 10b
232 /* store back */
233         rjmp xtea_enc_exit
234
235
236