]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister/twister-large-asm.S
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / twister / twister-large-asm.S
1 /* twister-large-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  * \file     twister-large-asm.S
21  * \email    bg@nerilex.org
22  * \author   Daniel Otte 
23  * \date     2008-12-27
24  * \license  GPLv3 or later
25  * 
26  */
27  
28  #include "avr-asm-macros.S"
29
30 /* void checksum_update(twister_large_ctx_t *ctx, uint8_t col) */
31 /*
32  * param ctx: r24:r25
33  * param col: r22
34  */
35 checksum_update:
36         push r16
37         push r28
38         push r29
39         lsl r22
40         lsl r22
41         lsl r22
42         mov r16, r22
43         movw r30, r24 /* X points to ctx->state.s */
44         
45         ldd r18, Z+7*8
46         ldd r19, Z+6*8
47         ldd r20, Z+5*8
48         ldd r21, Z+4*8
49         ldd r22, Z+3*8
50         ldd r23, Z+2*8
51         ldd r24, Z+1*8
52         ldd r25, Z+0*8
53         
54         adiw r30, 63
55         adiw r30, 1+3*8 /* Z points at ctx->checksum[0][8] */
56         movw r28, r30   /* Y points at ctx->checksum[0][8] */
57         andi r16, 63
58         add r30, r16   /* Z points at ctx->checksum[col][8]*/
59         adc r31, r1
60         ldi r26, 8
61         add r16, r26
62         andi r16, 63
63         add r28, r16
64         adc r29, r1    /* Y points at ctx->checksum[(col+1)%8][8]*/
65         
66         ld r0, -Y
67         add r18, r0
68         ld r0, -Z
69         eor r0, r18
70         st Z, r0
71         
72         ld r0, -Y
73         adc r19, r0
74         ld r0, -Z
75         eor r0, r19
76         st Z, r0
77         
78         ld r0, -Y
79         adc r20, r0
80         ld r0, -Z
81         eor r0, r20
82         st Z, r0
83         
84         ld r0, -Y
85         adc r21, r0
86         ld r0, -Z
87         eor r0, r21
88         st Z, r0
89         
90         ld r0, -Y
91         adc r22, r0
92         ld r0, -Z
93         eor r0, r22
94         st Z, r0
95         
96         ld r0, -Y
97         adc r23, r0
98         ld r0, -Z
99         eor r0, r23
100         st Z, r0
101         
102         ld r0, -Y
103         adc r24, r0
104         ld r0, -Z
105         eor r0, r24
106         st Z, r0
107         
108         ld r0, -Y
109         adc r25, r0
110         ld r0, -Z
111         eor r0, r25
112         st Z, r0
113         
114         pop r29
115         pop r28
116         pop r16
117         ret
118
119 /*********************************************************************/
120 /* void twister_large_init(twister_large_ctx_t *ctx, uint16_t hashsize_b)*/
121 /* 
122  * param ctx:        r24:r25
123  * param hashsize_b: r22:r23
124  */
125 .global twister384_init
126 twister384_init:
127         ldi r22, lo8(384)
128         ldi r23, hi8(384)
129         rjmp twister_large_init
130         
131 .global twister512_init
132 twister512_init:
133         ldi r22, lo8(512)
134         ldi r23, hi8(512)
135         
136 .global twister_large_init
137 twister_large_init:
138         movw r30, r24
139         ldi r24, 64
140 1:
141         st Z+, r1
142         dec r24
143         brne 1b
144
145         dec r1          
146         ldi r24, 8
147 1:
148         st Z+, r1
149         dec r24
150         brne 1b
151
152         inc r1          
153         ldi r24, 8+64
154 1:
155         st Z+, r1
156         dec r24
157         brne 1b
158
159         subi r30, lo8(1+8+8+8*7+64)
160         sbci r31, hi8(1+8+8+8*7+64)
161         st Z, r23
162         std Z+8, r22
163         ret
164
165 /*********************************************************************/
166 /* void twister_large_nextBlock(twister_state_t *ctx, void *msg) */   
167 /*
168  * param ctx: r24:r25
169  * param msg: r22:r23
170  */
171 CTX_SAVE0 = 14
172 CTX_SAVE1 = 15
173 TMP_SAVE0 = 12
174 TMP_SAVE1 = 13
175 MSG_SAVE0 = 28
176 MSG_SAVE1 = 29
177 .global twister_large_nextBlock
178 .global twister384_nextBlock
179 .global twister512_nextBlock
180
181 twister384_nextBlock:
182 twister512_nextBlock:
183 twister_large_nextBlock:
184         push_range 12, 15
185         push r28
186         push r29
187         stack_alloc_large 64
188         adiw r30, 1
189         movw TMP_SAVE0, r30
190         movw CTX_SAVE0, r24
191         movw MSG_SAVE0, r22
192         movw r26, CTX_SAVE0
193         ldi r18, 64/8
194 1:
195         ld r0, X+
196         st Z+, r0
197         ld r0, X+
198         st Z+, r0
199         ld r0, X+
200         st Z+, r0
201         ld r0, X+
202         st Z+, r0
203         ld r0, X+
204         st Z+, r0
205         ld r0, X+
206         st Z+, r0
207         ld r0, X+
208         st Z+, r0
209         ld r0, X+
210         st Z+, r0
211         dec r18
212         brne 1b
213         /* maxi round 1 */
214         movw r24, CTX_SAVE0 
215         ldi r22, 0
216         rcall checksum_update
217         movw r22, MSG_SAVE0
218         movw r24, CTX_SAVE0 
219         rcall twister_mini_round
220         
221         movw r24, CTX_SAVE0 
222         ldi r22, 1
223         rcall checksum_update
224         adiw MSG_SAVE0, 8
225         movw r22, MSG_SAVE0
226         movw r24, CTX_SAVE0 
227         rcall twister_mini_round
228         
229         movw r24, CTX_SAVE0 
230         ldi r22, 2
231         rcall checksum_update
232         adiw MSG_SAVE0, 8
233         movw r22, MSG_SAVE0
234         movw r24, CTX_SAVE0 
235         rcall twister_mini_round
236         
237         movw r30, TMP_SAVE0
238         movw r26, CTX_SAVE0
239         ldi r18, 64
240 1:
241         ld r0, X
242         ld r23, Z
243         eor r0, r23
244         st X+, r0
245         st Z+, r0
246         dec r18
247         brne 1b
248         /* maxi round 2 */
249         movw r24, CTX_SAVE0 
250         ldi r22, 3
251         rcall checksum_update
252         adiw MSG_SAVE0, 8
253         movw r22, MSG_SAVE0
254         movw r24, CTX_SAVE0 
255         rcall twister_mini_round
256         
257         movw r24, CTX_SAVE0 
258         rcall twister_blank_round
259         
260         movw r24, CTX_SAVE0 
261         ldi r22, 4
262         rcall checksum_update
263         adiw MSG_SAVE0, 8
264         movw r22, MSG_SAVE0
265         movw r24, CTX_SAVE0 
266         rcall twister_mini_round
267
268         movw r30, TMP_SAVE0
269         movw r26, CTX_SAVE0
270         ldi r18, 64
271 1:
272         ld r0, X
273         ld r23, Z
274         eor r0, r23
275         st X+, r0
276         st Z+, r0
277         dec r18
278         brne 1b
279         /* maxi round 3 */
280         movw r24, CTX_SAVE0 
281         ldi r22, 5
282         rcall checksum_update
283         adiw MSG_SAVE0, 8
284         movw r22, MSG_SAVE0
285         movw r24, CTX_SAVE0 
286         rcall twister_mini_round
287         
288         movw r24, CTX_SAVE0 
289         ldi r22, 6
290         rcall checksum_update
291         adiw MSG_SAVE0, 8
292         movw r22, MSG_SAVE0
293         movw r24, CTX_SAVE0 
294         rcall twister_mini_round
295         
296         movw r24, CTX_SAVE0 
297         ldi r22, 7
298         rcall checksum_update
299         adiw MSG_SAVE0, 8
300         movw r22, MSG_SAVE0
301         movw r24, CTX_SAVE0 
302         rcall twister_mini_round
303         
304         movw r24, CTX_SAVE0 
305         rcall twister_blank_round
306
307         movw r30, TMP_SAVE0
308         movw r26, CTX_SAVE0
309         ldi r18, 64
310 1:
311         ld r0, X
312         ld r23, Z+
313         eor r0, r23
314         st X+, r0
315         dec r18
316         brne 1b
317         
318         adiw r26, 9
319         ldi r19, 2
320         ld r0, X
321         add r0, r19
322         st X+, r0
323                 
324         ld r0, X
325         adc r0, r1
326         st X+, r0
327         ld r0, X
328         adc r0, r1
329         st X+, r0
330         ld r0, X
331         adc r0, r1
332         st X+, r0
333         ld r0, X
334         adc r0, r1
335         st X+, r0
336         ld r0, X
337         adc r0, r1
338         st X+, r0
339         ld r0, X
340         adc r0, r1
341         st X+, r0
342         ld r0, X
343         adc r0, r1
344         st X+, r0
345         
346         stack_free_large 64
347         pop r29
348         pop r28
349         pop_range 12, 15
350         ret
351         
352 /*********************************************************************/
353 /* void twister_large_lastBlock(twister_state_t *ctx, void *msg, uint16_t length_b) */   
354 /*
355  * param ctx:      r24:r25
356  * param msg:      r22:r23
357  * param length_b: r20:r21
358  */
359 TMP_SAVE0 = 12
360 TMP_SAVE1 = 13
361 CTX_SAVE0 = 14
362 CTX_SAVE1 = 15
363 LEN_SAVE0 = 16
364 LEN_SAVE1 = 17
365 MSG_SAVE0 = 28
366 MSG_SAVE1 = 29
367 .global twister_large_lastBlock
368 .global twister384_lastBlock
369 .global twister512_lastBlock
370
371 twister384_lastBlock:
372 twister512_lastBlock:
373 twister_large_lastBlock:
374         push_range 12, 17
375         push r28
376         push r29
377         stack_alloc_large 64
378         adiw r30, 1
379         movw TMP_SAVE0, r30
380         movw CTX_SAVE0, r24
381         movw MSG_SAVE0, r22
382         movw LEN_SAVE0, r20
383 1:      
384         cpi LEN_SAVE1, 2
385         brmi 2f
386         movw r24, CTX_SAVE0
387         movw r22, MSG_SAVE0
388         rcall twister_large_nextBlock
389         adiw MSG_SAVE0, 8
390         subi LEN_SAVE1, 2
391         rjmp 1b
392 2:
393         movw r18, LEN_SAVE0
394         lsr r19
395         ror r18
396         lsr r18
397         lsr r18
398         ldi r19, 63
399         movw r26, MSG_SAVE0
400         movw r30, TMP_SAVE0     
401         ldi r20, 0x80
402         sub r19, r18 /* r18: bytes to copy, r19: bytes to clear */
403         
404         ld r0, X+       
405 3:
406         tst r18
407         breq 4f
408 31:
409         st Z+, r0
410         ld r0, X+
411         dec r18
412         brne 31b
413 4:      
414         mov r18, LEN_SAVE0
415         andi r18, 0x07
416         ldi r20, 0x80
417         breq 5f
418 4:
419         lsr r20
420         dec r18
421         brne 4b
422         or r20, r0
423         rjmp 5f
424
425 5:
426         st Z+, r20      
427         tst r19
428         breq 7f
429 6:      
430         st Z+, r1
431         dec r19
432         brne 6b
433 7:      
434         movw r24, CTX_SAVE0
435         movw r22, TMP_SAVE0
436         rcall twister_large_nextBlock
437         
438         ldi r19, 2
439         clr r18
440         
441         sub r18, LEN_SAVE0
442         sbc r19, LEN_SAVE1
443         movw r26, CTX_SAVE0
444         adiw r26, 63
445         adiw r26, 1+8
446         
447         ld r0, X
448         sub r0, r18
449         st X+, r0
450         ld r0, X
451         sbc r0, r19
452         st X+, r0
453         ld r0, X
454         sbc r0, r1
455         st X+, r0
456         ld r0, X
457         sbc r0, r1
458         st X+, r0
459         ld r0, X
460         sbc r0, r1
461         st X+, r0
462         ld r0, X
463         sbc r0, r1
464         st X+, r0
465         ld r0, X
466         sbc r0, r1
467         st X+, r0
468         ld r0, X
469         sbc r0, r1
470         st X+, r0
471         
472         sbiw r26, 8
473         movw r24, CTX_SAVE0
474         movw r22, r26
475         rcall twister_mini_round        
476
477         movw r24, CTX_SAVE0
478         movw r22, CTX_SAVE0
479         ldi r16, 64+8+8
480         add r22, r16
481         adc r23, r1
482         movw r30, r22
483         ldi r26, 8
484 1:      
485         ld r12, Z+
486         ld r13, Z+
487         ld r16, Z+
488         ld r17, Z+
489         ld r18, Z+
490         ld r19, Z+
491         ld r20, Z+
492         ld r21, Z+
493         st -Z, r12
494         st -Z, r13
495         st -Z, r16
496         st -Z, r17
497         st -Z, r18
498         st -Z, r19
499         st -Z, r20
500         st -Z, r21
501         adiw r30, 8
502         dec r26
503         brne 1b
504         
505         movw r24, CTX_SAVE0
506         movw r22, CTX_SAVE0
507         ldi r26, 64+2*8
508         add r22, r26
509         adc r23, r1
510         rcall twister_small_nextBlock
511         
512         stack_free_large 64
513         pop r29
514         pop r28
515         pop_range 12, 17
516         ret