]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/skein1024_asm.S
global style change (now * is attached to identifier not to type)
[avr-crypto-lib.git] / skein / skein1024_asm.S
1 /* skein1024_asm.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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  * \author  Daniel Otte
21  * \email   daniel.otte@rub.de
22  * \date    2009-03-25
23  * \license GPLv3 or later
24  */ 
25
26 #include "avr-asm-macros.S"
27
28 /******************************************************************************/
29 /*
30 void skein1024_init(skein1024_ctx_t *ctx, uint16_t outsize_b){
31         skein_config_t conf;
32         uint8_t null[UBI1024_BLOCKSIZE_B];
33         memset(null, 0, UBI1024_BLOCKSIZE_B);
34         memset(&conf, 0, sizeof(skein_config_t));
35         conf.schema[0] = 'S';
36         conf.schema[1] = 'H';
37         conf.schema[2] = 'A';
38         conf.schema[3] = '3';
39         conf.version = 1;
40         conf.out_length = outsize_b;
41         ctx->outsize_b = outsize_b;
42         ubi1024_init(&(ctx->ubictx), null, UBI_TYPE_CFG);
43         ubi1024_lastBlock(&(ctx->ubictx), &conf, 256);
44         ubi1024_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_MSG);
45 }
46 */
47 /*
48  * param ctx:       r24:r25
49  * param outsize_b: r22:r23
50  */
51 UBICTX0 = 2
52 UBICTX1 = 3
53 CONF0   = 4
54 CONF1   = 5
55 .global skein1024_init
56 skein1024_init:
57         push_range 2, 5
58         stack_alloc_large 32+128-22   ;     |<- 22 ->|
59         adiw r30, 1                   ; | CONF (32)  | 
60         movw CONF0, r30               ;     | null (128)           |   
61         movw r26, r24
62         st X+, r22
63         st X+, r23
64         movw UBICTX0, r26
65         ldi r24, 'S'
66         st Z+, r24
67         ldi r24, 'H'
68         st Z+, r24
69         ldi r24, 'A'
70         st Z+, r24
71         ldi r24, '3'
72         st Z+, r24
73         ldi r24, 1
74         st Z+, r24
75         st Z+, r1
76         st Z+, r1
77         st Z+, r1
78         st Z+, r22
79         st Z+, r23
80         ldi 24, 128
81 1:  st Z+, r1
82         dec r24
83         brne 1b
84         /* call ubi1024_init*/
85         subi r30, lo8(128)
86         sbci r31, hi8(128)
87         movw r24, UBICTX0
88         movw r22, r30
89         ldi r20, 4
90         rcall ubi1024_init      
91         /* call ubi1024_lastBlock*/
92         movw r24, UBICTX0
93         movw r22, CONF0
94         ldi r21, 1
95         clr r20
96         rcall ubi1024_lastBlock
97         /* call ubi1024_init*/
98         movw r24, UBICTX0
99         adiw r24, 16
100         movw r22, r24
101         movw r24, UBICTX0
102         ldi r20, 48
103         rcall ubi1024_init
104         stack_free_large2 32+128-22
105         pop_range 2, 5
106         ret
107         
108 /******************************************************************************/
109 .global skein1024_nextBlock
110 skein1024_nextBlock:
111         adiw r24, 2
112         rjmp ubi1024_nextBlock
113
114 /******************************************************************************/
115 .global skein1024_lastBlock
116 skein1024_lastBlock:
117         adiw r24, 2
118         rjmp ubi1024_lastBlock
119
120 /******************************************************************************/
121 /*
122 void skein1024_ctx2hash(void *dest, skein1024_ctx_t *ctx){
123         ubi1024_ctx_t uctx;
124         uint16_t outsize_b;
125         
126         uint64_t counter=0;
127         uint8_t outbuffer[UBI1024_BLOCKSIZE_B];
128         ubi1024_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_OUT);
129         
130         outsize_b = ctx->outsize_b;
131         while(1){
132                 memcpy(&uctx, &(ctx->ubictx), sizeof(ubi1024_ctx_t));
133                 ubi1024_lastBlock(&uctx, &counter, 64);
134                 ubi1024_ctx2hash(outbuffer, &uctx);
135                 if(outsize_b<=UBI1024_BLOCKSIZE){
136                         memcpy(dest, outbuffer, (ctx->outsize_b+7)/8);
137                         break;
138                 }else{
139                         memcpy(dest, outbuffer, UBI1024_BLOCKSIZE_B);
140                         dest = (uint8_t*)dest + UBI1024_BLOCKSIZE_B;
141                         outsize_b -= UBI1024_BLOCKSIZE;
142                         counter++;
143                 }
144         }
145 }
146 */
147 /*
148  * param dest: r24:r25
149  * param ctx:  r22:r23
150  */
151  OUTSIZE_B0 = 16
152  OUTSIZE_B1 = 17
153  UCTX0      = 14
154  UCTX1      = 15
155  UBICTX0    = 12
156  UBICTX1    = 13
157  DEST0      = 10
158  DEST1      = 11
159 .global skein1024_ctx2hash
160 skein1024_ctx2hash:
161         push_range 10, 17
162                                      /* 144  ||  8      ||  128      */
163         stack_alloc_large 144+8+128  /* uctx || counter || outbuffer */
164         movw DEST0, r24
165         adiw r30, 1
166         movw UCTX0, r30
167         ldi r16, 144
168         add r30, r16
169         adc r31, r1
170         st Z+, r1
171         st Z+, r1
172         st Z+, r1
173         st Z+, r1
174         st Z+, r1
175         st Z+, r1
176         st Z+, r1
177         st Z+, r1
178         movw r26, 22
179         ld OUTSIZE_B0, X+
180         ld OUTSIZE_B1, X+
181         movw UBICTX0, r26
182    /* call ubi1024_init */
183         movw r24, UBICTX0
184         adiw r24, 16
185         movw r22, r24
186         movw r24, UBICTX0
187         ldi r20, 63
188         rcall ubi1024_init
189         
190    /* main loop */
191    /* copy ubictx in uctx*/
192 1:      movw r30, UCTX0
193         movw r26, UBICTX0
194         ldi r24, 144
195 2:      ld r25, X+
196         st Z+, r25
197         dec r24
198         brne 2b
199   /* call ubi1024_lastBlock */
200     movw r24, UCTX0
201         adiw r24, 63
202         adiw r24, 63
203         adiw r24, 18
204         movw r22, r24
205         movw r24, UCTX0
206         clr r21
207         ldi r20, 64
208         rcall ubi1024_lastBlock
209   /* copy uctx->g to outbuffer */       
210         movw r26, UCTX0
211         adiw r26, 16
212         movw r30, UCTX0
213         adiw r30, 63
214         adiw r30, 63
215         adiw r30, 18+8
216         ldi r24, 128
217 2:      ld r25, X+
218     st Z+, r25
219         dec r24
220         brne 2b
221    /* compare outsize_b with 1024*/      
222         cpi OUTSIZE_B1, 5
223         brge 5f
224         cpi OUTSIZE_B1, 4
225         brlo 3f
226         tst OUTSIZE_B0
227         breq 3f 
228 5:      /* copy outbuffer to dest */
229         movw r30, DEST0
230         movw r26, UCTX0
231         adiw r26, 63
232         adiw r26, 63
233         adiw r26, 18+8
234         ldi r24, 128
235 6:      ld r25, X+
236         st Z+, r25
237         dec r24
238         brne 6b
239         /* store new dest */
240         movw DEST0, r30
241         /* adjust counter and outsize_b*/
242         subi OUTSIZE_B1, 2
243         movw r30, UCTX0
244         adiw r30, 63
245         adiw r30, 63
246         adiw r30, 18
247         ldi r24, 1
248         ld r25, Z
249         add r25, r24
250         st Z+, r25
251         ldi r24, 7
252 6:      ld r25, Z
253         adc r25, r1
254         st Z+, r25
255         dec r24
256         brne 6b
257         rjmp 1b
258 3:      /* last iteraton */
259         movw r24, OUTSIZE_B0
260         adiw r24, 7
261         lsr r25
262         ror r24
263         lsr r25
264         ror r24
265         lsr r25
266         ror r24
267         movw r30, DEST0
268         movw r26, UCTX0
269         adiw r26, 63
270         adiw r26, 63
271         adiw r26, 18+8
272         tst r24
273         breq 8f
274 7:      ld r25, X+
275         st Z+, r25
276         dec r24
277         brne 7b
278 8:      
279         stack_free_large3 144+8+128
280         pop_range 10, 17
281         ret
282
283 /******************************************************************************/
284 /*
285 void skein1024(void *dest, uint16_t outlength_b, const void *msg, uint32_t length_b){
286         skein1024_ctx_t ctx;
287         skein1024_init(&ctx, outlength_b);
288         while(length_b>SKEIN1024_BLOCKSIZE){
289                 skein1024_nextBlock(&ctx, msg);
290                 msg = (uint8_t*)msg + SKEIN1024_BLOCKSIZE_B;
291                 length_b -= SKEIN1024_BLOCKSIZE;
292         }
293         skein1024_lastBlock(&ctx, msg, length_b);
294         skein1024_ctx2hash(dest, &ctx);
295 }
296 */
297 /*
298  * param dest:         r24:r25
299  * param outlength_b:  r22:r23
300  * param msg:          r20:r21
301  * param length_b:     r16:r19
302  */
303 LENGTH_B0 =  2
304 LENGTH_B1 =  3
305 LENGTH_B2 =  4
306 LENGTH_B3 =  5
307 DEST0     =  6
308 DEST1     =  7
309 MSG0      =  8
310 MSG1      =  9
311 CTX0      = 10
312 CTX1      = 11
313 .global skein1024
314 skein1024:
315         push_range 2, 11
316         stack_alloc_large 146
317         adiw r30, 1
318         movw CTX0, r30
319         movw DEST0, r24
320         movw MSG0, r20
321         movw LENGTH_B0, r16
322         movw LENGTH_B2, r18
323         /* call skein1024_init */
324         movw r24, r30
325         rcall skein1024_init
326 1:      tst LENGTH_B2
327         brne 4f
328         tst LENGTH_B3
329         brne 4f
330    /* call skein1024_lastBlock */
331         movw r24, CTX0
332         movw r22, MSG0
333         movw r20, LENGTH_B0
334         rcall skein1024_lastBlock
335    /* call skein1024_ctx2hash */
336         movw r24, DEST0
337         movw r22, CTX0
338         rcall skein1024_ctx2hash
339    /* return */ 
340         stack_free_large2 146
341         pop_range 2, 11
342         ret
343                 
344 4: /* process preceeding blocks */      
345         movw r24, CTX0
346         movw r22, MSG0
347         rcall skein1024_nextBlock
348         ldi r24, 128
349         add MSG0, r24
350         adc MSG0, r1
351         mov r24, LENGTH_B1
352         mov r25, LENGTH_B2
353         sbiw r24, 4
354         sbc LENGTH_B3, r1
355         mov LENGTH_B1, r24
356         mov LENGTH_B2, r25
357         rjmp 1b
358