]> git.cryptolib.org Git - avr-crypto-lib.git/blob - xtea-asm.S
insereated GPLv3 stub
[avr-crypto-lib.git] / xtea-asm.S
1 /* xtea-asm.S */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  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 /* xtea-asm.S 
20  * Author:      Daniel Otte
21  * Date:                06.06.2006
22  * License: GPL
23  *  Implementation of XTEA for AVR
24  *  include xtea.h in your C-Project to use this functions.
25 */
26
27 V01 = 2
28 V02 = 3
29 V03 = 4
30 V04 = 5
31 V11 = 6
32 V12 = 7
33 V13 = 8
34 V14 = 9
35 Accu1 = 14
36 Accu2 = 15
37 Accu3 = 16
38 Accu4 = 17
39 Sum1 = 18
40 Sum2 = 19
41 Sum3 = 20
42 Sum4 = 21
43 Func1 = 22
44 Func2 = 23
45 Func3 = 24
46 Func4 = 25
47 C = 28 /* der kleine Zaehler fuer zwischendurch */
48
49 .global xtea_enc
50 ; == xtea_enc ==
51 ; xtea encrytion function
52 ; param1: 16-bit pointer to destination for encrypted block 
53 ;  given in r25,r24
54 ; param2: 16-bit pointer to the block (64-bit) which is to encrypt 
55 ;  given in r23,r22
56 ; param3: 16-bit pointer to the key (128-bit) 
57 ;  given in r21,r20
58 ;
59 xtea_enc:
60  /* prolog */
61         push r2
62         push r3
63         push r4
64         push r5
65         push r6
66         push r7
67         push r8
68         push r9
69         push r14
70         push r15
71         push r16
72         push r17
73         push r28
74         
75  /* load the block */
76         movw r26, r22 /* X points to block */
77         movw r30, r20 /* Z points to key   */
78         ld V01, X+
79         ld V02, X+
80         ld V03, X+
81         ld V04, X+
82         ld V11, X+
83         ld V12, X+
84         ld V13, X+
85         ld V14, X+
86 ;       push r25
87 ;       push r24
88         movw r26, r24 /* X points to destination */
89  
90         ldi Func1, 32
91         mov r0, Func1 /* r1 is cycle-counter */
92         clr Sum1
93         clr Sum2
94         movw Sum3, Sum1
95         clt
96
97 1:
98         movw Accu1, V11
99         movw Accu3, V13
100         ldi C, 4
101 2:      lsl Accu1
102         rol Accu2
103         rol Accu3
104         rol Accu4
105         dec C
106         brne 2b                 /* Accu == V1 << 4 */
107
108         movw Func1, V11
109         movw Func3, V13
110         ldi C, 5
111 3:      lsr Func4
112         ror Func3
113         ror Func2
114         ror Func1
115         dec C
116         brne 3b                 /* Func == V1 >> 5 */
117         
118         eor Accu1, Func1
119         eor Accu2, Func2
120         eor Accu3, Func3
121         eor Accu4, Func4
122         add Accu1, V11
123         adc Accu2, V12
124         adc Accu3, V13
125         adc Accu4, V14  /* Accu == ( (V1<<4)^(V1>>5) ) + V1 */
126         
127         brtc 4f
128         mov C, Sum2
129         lsr C
130         andi C,(0x03 <<2)
131         clt
132         rjmp 5f
133 4:      
134         mov C, Sum1     /* calc key offset */
135         andi C, 0x03
136         lsl C
137         lsl C
138         set
139         
140 5:      
141         add r30, C
142         adc r31, r1
143         ld  Func1, Z
144         ldd Func2, Z+1
145         ldd Func3, Z+2
146         ldd Func4, Z+3 /* Func = key[sum & 3] */
147         sub r30, C
148         sbci r31, 0
149         add Func1, Sum1
150         adc Func2, Sum2
151         adc Func3, Sum3
152         adc Func4, Sum4 
153         eor Accu1, Func1
154         eor Accu2, Func2
155         eor Accu3, Func3
156         eor Accu4, Func4 /* Accu = ((V1<<4 ^ V1>>5) + V1) ^ (sum + key[sum&3])  */
157         add Accu1, V01
158         adc Accu2, V02
159         adc Accu3, V03
160         adc Accu4, V04
161         
162         movw V01, V11
163         movw V03, V13
164         movw V11, Accu1
165         movw V13, Accu3
166         
167         /* sum += delta */ /* delta == 0x9E3779B9 */
168         brtc 6f
169         ldi C, 0xB9
170         add Sum1, C
171         ldi C, 0x79
172         adc Sum2, C
173         ldi C, 0x37
174         adc Sum3, C
175         ldi C, 0x9E
176         adc Sum4, C
177         rjmp 1b
178         
179 6:      
180         dec r0
181         breq 7f
182         rjmp 1b 
183  
184  7:
185  /* write block back */
186  ;      pop r26
187  ;      pop r27
188         st X+, V01
189         st X+, V02
190         st X+, V03
191         st X+, V04
192         st X+, V11
193         st X+, V12
194         st X+, V13
195         st X+, V14
196  
197  /* epilog */
198         pop r28
199         pop r17
200         pop r16
201         pop r15
202         pop r14
203         pop r9
204         pop r8
205         pop r7
206         pop r6
207         pop r5
208         pop r4
209         pop r3
210         pop r2
211         ret
212
213 ;####################################################################
214  
215  /* #endif TWO_IN_ONE */        
216  
217  /* #ifdef TWO_IN_ONE */
218  /* now we use the same base-structure for enc- and decryption
219         to indicate operation mode we use the highest bit of param3 (16 bit pointer to key),
220         this is ok, since even the larges atmel today has "only" 8k of ram,
221         but you shouldn't use this feature while using external ram. 
222  */
223 .global xtea_enc
224         ori r21, 0x80
225         
226 .global xtea_dec
227 ; == xtea_dec ==
228 ; xtea decrytion function
229 ; param1: 16-bit pointer to destination for decrypted block 
230 ;  given in r25,r24
231 ; param2: 16-bit pointer to the block (64-bit) which is to derypt 
232 ;  given in r23,r22
233 ; param3: 16-bit pointer to the key (128-bit) 
234 ;  given in r21,r20
235 ;
236 /*
237 void xtea_dec(uint32_t* dest, uint32_t* v, uint32_t* k) {
238     uint32_t v0=v[0], v1=v[1], i;
239     uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
240     for(i=0; i<32; i++) {
241         v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
242         sum -= delta;
243         v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
244     }
245     dest[0]=v0; dest[1]=v1;
246 }
247 */
248
249 xtea_dec:
250  /* prolog */
251         push r2
252         push r3
253         push r4
254         push r5
255         push r6
256         push r7
257         push r8
258         push r9
259         push r14
260         push r15
261         push r16
262         push r17
263         push r28 
264  /* load the block */
265         movw r26, r22 /* Z points to block */
266         movw r30, r20 /* X points to key   */
267         ld V01, X+
268         ld V02, X+
269         ld V03, X+
270         ld V04, X+
271         ld V11, X+
272         ld V12, X+
273         ld V13, X+
274         ld V14, X+
275         movw r26, r24 /* Z points to destination */
276  
277         ldi Sum1, 32
278         mov r0, Sum1 /* r1 is cycle-counter */
279         ldi Sum1, 0x20 /* sum = 0xC6EF3720 */
280         ldi Sum2, 0x37
281         ldi Sum3, 0xEF
282         ldi Sum4, 0xC6
283         clt
284
285 1:
286         movw Accu1, V01
287         movw Accu3, V03
288         ldi C, 4
289 2:      lsl Accu1
290         rol Accu2
291         rol Accu3
292         rol Accu4
293         dec C
294         brne 2b                 /* Accu == V0 << 4 */
295
296         movw Func1, V01
297         movw Func3, V03
298         ldi C, 5
299 3:      lsr Func4
300         ror Func3
301         ror Func2
302         ror Func1
303         dec C
304         brne 3b                 /* Func == V0 >> 5 */
305         
306         eor Accu1, Func1
307         eor Accu2, Func2
308         eor Accu3, Func3
309         eor Accu4, Func4
310         add Accu1, V01
311         adc Accu2, V02
312         adc Accu3, V03
313         adc Accu4, V04  /* Accu == ( (V0<<4)^(V0>>5) ) + V0 */
314         
315         brts 4f
316         mov C, Sum2
317         lsr C
318         andi C,(0x03 <<2)
319         set
320         rjmp 5f
321 4:      
322         mov C, Sum1     /* calc key offset */
323         andi C, 0x03
324         lsl C
325         lsl C
326         clt
327         
328 5:      
329         add r30, C
330         adc r31, r1
331         ld  Func1, Z
332         ldd Func2, Z+1
333         ldd Func3, Z+2
334         ldd Func4, Z+3 /* Func = key[sum & 3] */
335         sub r30, C
336         sbci r31, 0
337         add Func1, Sum1
338         adc Func2, Sum2
339         adc Func3, Sum3
340         adc Func4, Sum4 
341         eor Accu1, Func1
342         eor Accu2, Func2
343         eor Accu3, Func3
344         eor Accu4, Func4 /* Accu = ((V0<<4 ^ V0>>5) + V0) ^ (sum + key[sum&3])  */
345         sub V11, Accu1
346         sbc V12, Accu2
347         sbc V13, Accu3
348         sbc V14, Accu4
349         
350         movw Accu1, V01
351         movw Accu3, V03
352         movw V01, V11
353         movw V03, V13
354         movw V11, Accu1
355         movw V13, Accu3
356         
357         /* sum += delta */ /* delta == 0x9E3779B9 */
358         brtc 6f
359         subi Sum1, 0xB9
360         sbci Sum2, 0x79
361         sbci Sum3, 0x37
362         sbci Sum4, 0x9E
363         rjmp 1b
364         
365 6:      
366         dec r0
367         breq 7f
368         rjmp 1b 
369  
370 7:
371  /* write block back */
372         st X+, V01
373         st X+, V02
374         st X+, V03
375         st X+, V04
376         st X+, V11
377         st X+, V12
378         st X+, V13
379         st X+, V14
380  
381  /* epilog */
382         pop r28
383         pop r17
384         pop r16
385         pop r15
386         pop r14
387         pop r9
388         pop r8
389         pop r7
390         pop r6
391         pop r5
392         pop r4
393         pop r3
394         pop r2
395         ret
396         
397  /* #endif */
398
399 ;####################################################################
400  
401  #ifdef TWO_IN_ONE
402  /* now we use the same base-structure for enc- and decryption
403         to indicate operation mode we use the highest bit of param3 (16 bit pointer to key),
404         this is ok, since even the larges atmel today has "only" 8k of ram,
405         but you shouldn't use this feature while using external ram. 
406  */
407 .global xtea_enc
408         ori r21, 0x80
409         
410 .global xtea_dec
411 ; == xtea_dec ==
412 ; xtea decrytion function
413 ; param1: 16-bit pointer to destination for decrypted block 
414 ;  given in r25,r24
415 ; param2: 16-bit pointer to the block (64-bit) which is to derypt 
416 ;  given in r23,r22
417 ; param3: 16-bit pointer to the key (128-bit) 
418 ;  given in r21,r20
419 ;
420 /*
421 void xtea_dec(uint32_t* dest, uint32_t* v, uint32_t* k) {
422     uint32_t v0=v[0], v1=v[1], i;
423     uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
424     for(i=0; i<32; i++) {
425         v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
426         sum -= delta;
427         v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
428     }
429     dest[0]=v0; dest[1]=v1;
430 }
431 */
432
433 xtea_dec:
434  /* prolog */
435         push r2
436         push r3
437         push r4
438         push r5
439         push r6
440         push r7
441         push r8
442         push r9
443         push r14
444         push r15
445         push r16
446         push r17
447         push r28 
448  /* set T-bit if we are going to encrypt, clear otherwise */
449         bst r21, 7
450         andi r21, 0x7f /* fix r21:r22 to a real addr */
451  /* load the block */
452         movw r26, r22 /* Z points to block */
453         movw r30, r20 /* X points to key   */
454         ld V01, X+
455         ld V02, X+
456         ld V03, X+
457         ld V04, X+
458         ld V11, X+
459         ld V12, X+
460         ld V13, X+
461         ld V14, X+
462         movw r26, r24 /* Z points to destination */
463  
464         ldi Sum1, 32
465         mov r0, Sum1 /* r1 is cycle-counter */
466         ldi Sum1, 0x20 /* sum = 0xC6EF3720 */
467         ldi Sum2, 0x37
468         ldi Sum3, 0xEF
469         ldi Sum4, 0xC6
470         clt
471
472 1:
473         movw Accu1, V01
474         movw Accu3, V03
475         ldi C, 4
476 2:      lsl Accu1
477         rol Accu2
478         rol Accu3
479         rol Accu4
480         dec C
481         brne 2b                 /* Accu == V0 << 4 */
482
483         movw Func1, V01
484         movw Func3, V03
485         ldi C, 5
486 3:      lsr Func4
487         ror Func3
488         ror Func2
489         ror Func1
490         dec C
491         brne 3b                 /* Func == V0 >> 5 */
492         
493         eor Accu1, Func1
494         eor Accu2, Func2
495         eor Accu3, Func3
496         eor Accu4, Func4
497         add Accu1, V01
498         adc Accu2, V02
499         adc Accu3, V03
500         adc Accu4, V04  /* Accu == ( (V0<<4)^(V0>>5) ) + V0 */
501         
502         brts 4f
503         mov C, Sum2
504         lsr C
505         andi C,(0x03 <<2)
506         set
507         rjmp 5f
508 4:      
509         mov C, Sum1     /* calc key offset */
510         andi C, 0x03
511         lsl C
512         lsl C
513         clt
514         
515 5:      
516         add r30, C
517         adc r31, r1
518         ld  Func1, Z
519         ldd Func2, Z+1
520         ldd Func3, Z+2
521         ldd Func4, Z+3 /* Func = key[sum & 3] */
522         sub r30, C
523         sbci r31, 0
524         add Func1, Sum1
525         adc Func2, Sum2
526         adc Func3, Sum3
527         adc Func4, Sum4 
528         eor Accu1, Func1
529         eor Accu2, Func2
530         eor Accu3, Func3
531         eor Accu4, Func4 /* Accu = ((V0<<4 ^ V0>>5) + V0) ^ (sum + key[sum&3])  */
532         sub V11, Accu1
533         sbc V12, Accu2
534         sbc V13, Accu3
535         sbc V14, Accu4
536         
537         movw Accu1, V01
538         movw Accu3, V03
539         movw V01, V11
540         movw V03, V13
541         movw V11, Accu1
542         movw V13, Accu3
543         
544         /* sum += delta */ /* delta == 0x9E3779B9 */
545         brtc 6f
546         subi Sum1, 0xB9
547         sbci Sum2, 0x79
548         sbci Sum3, 0x37
549         sbci Sum4, 0x9E
550         rjmp 1b
551         
552 6:      
553         dec r0
554         breq 7f
555         rjmp 1b 
556  
557 7:
558  /* write block back */
559         st X+, V01
560         st X+, V02
561         st X+, V03
562         st X+, V04
563         st X+, V11
564         st X+, V12
565         st X+, V13
566         st X+, V14
567  
568  /* epilog */
569         pop r28
570         pop r17
571         pop r16
572         pop r15
573         pop r14
574         pop r9
575         pop r8
576         pop r7
577         pop r6
578         pop r5
579         pop r4
580         pop r3
581         pop r2
582         ret
583         
584  #endif
585