]> git.cryptolib.org Git - avr-crypto-lib.git/blob - xtea/xtea-asm.S
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / xtea / xtea-asm.S
1 /* xtea-asm.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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 V00 = 12
33 V01 = 13
34 V02 = 14
35 V03 = 15
36
37 V10 = 16
38 V11 = 17
39 V12 = 18
40 V13 = 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 20:
128         rcall sum_plus_k
129         ret
130
131 main2:
132         rcall xchg_V0V1
133         rcall g_func
134         mov r24, S1
135         lsr r24
136         rcall xchg_V0V1
137         rjmp 20b
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 /*
188         st X+, V00
189         st X+, V01
190         st X+, V02
191         st X+, V03
192         st X+, V10
193         st X+, V11
194         st X+, V12
195         st X+, V13
196 */
197         clr r31
198         ldi r30, V00
199         ldi r24, 8
200 10:
201         ld r0, Z+
202         st X+, r0
203         dec r24
204         brne 10b
205
206 ;       clr r31
207         ldi r30, 18
208         ldi r24, 14
209 10:
210         pop r0
211         st -Z, r0
212         dec r24
213         brne 10b
214         ret
215
216
217 /******************************************************************************/
218 /******************************************************************************/
219 /******************************************************************************/
220 /******************************************************************************/
221
222 .global xtea_dec
223 xtea_dec:
224         clt
225         rjmp xtea_intro
226 xtea_dec_start:
227         ldi S0, 0x20 /* sum = 0xC6EF3720 */
228         ldi S1, 0x37
229         ldi S2, 0xEF
230         ldi S3, 0xC6
231
232 10:
233         rcall main2
234         subi S0, 0xB9
235         sbci S1, 0x79
236         sbci S2, 0x37
237         sbci S3, 0x9E
238         rcall main1
239
240         dec r0
241         brne 10b
242 /* store back */
243         rjmp xtea_enc_exit
244
245
246