]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/ubi1024_asm.S
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / skein / ubi1024_asm.S
1 /* ubi1024_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  * \author  Daniel Otte
21  * \email   bg@nerilex.org
22  * \date    2009-03-16
23  * \license GPLv3 or later
24  */ 
25
26 #include "avr-asm-macros.S"
27
28 /******************************************************************************/
29 /*
30 void ubi1024_init(ubi1024_ctx_t *ctx, const void *g, uint8_t type){
31         memset(ctx->tweak, 0, 15);
32         ctx->tweak[15] = 0x40+type;
33         memcpy(ctx->g, g, UBI1024_BLOCKSIZE_B);
34 }
35 */
36 /*
37  * param ctx:  r24:r25
38  * param g:    r22:r23
39  * param type: r20
40  */
41 .global ubi1024_init
42 ubi1024_init:
43         movw r26, r24
44         ldi r21, 15
45 1:      st X+, r1
46         dec r21
47         brne 1b
48         ori r20, 0x40
49         st X+, r20
50         ldi r21, 128
51         movw r30, r22
52 2:  ld r20, Z+
53         st X+, r20
54         dec r21
55         brne 2b
56         ret     
57
58 /******************************************************************************/
59 /*
60 void ubi1024_ctx2hash(void *dest, const ubi1024_ctx_t *ctx){
61         memcpy(dest, ctx->g, UBI1024_BLOCKSIZE_B);
62 }
63 */
64 /*
65  * param dest: r24:r24
66  * param ctx:  r22:r23
67  */
68 .global ubi1024_ctx2hash
69 ubi1024_ctx2hash:
70         movw r26, r24
71         movw r30, r22
72         adiw r30, 16
73         ldi r22, 128
74 1:      ld r23, Z+
75         st X+, r23
76         dec r22
77         brne 1b
78         ret
79
80 /******************************************************************************/
81 /*
82 void ubi1024_nextBlock(ubi1024_ctx_t *ctx, const void *block){
83         threefish1024_ctx_t tfctx;
84         ((uint64_t*)(ctx->tweak))[0] += UBI1024_BLOCKSIZE_B;
85         threefish1024_init(ctx->g, ctx->tweak, &tfctx);
86         memcpy(ctx->g, block, UBI1024_BLOCKSIZE_B);
87         threefish1024_enc(ctx->g, &tfctx);
88         memxor(ctx->g, block, UBI1024_BLOCKSIZE_B);
89         ctx->tweak[15] &= (uint8_t)~0x40;
90 }
91 */
92 /*
93  * param ctx:   r24:r25
94  * param block: r22:r23
95  */
96 CTX0   = 2
97 CTX1   = 3
98 BLOCK0 = 4
99 BLOCK1 = 5
100 TFCTX0 = 6
101 TFCTX1 = 7 
102 .global ubi1024_nextBlock
103 ubi1024_nextBlock:
104         stack_alloc_large 20*8
105         push_range 2, 7
106         adiw r30, 1 /* Z points to tfctx */
107         movw TFCTX0, r30
108         movw CTX0, r24
109         movw BLOCK0, r22
110         movw r26, r24
111 /* add BLOCKSIZE_B (128) to tweak */
112         ldi r25, 128
113         ld r24, X
114         add r24, r25
115         st X+, r24
116         ldi r25, 11
117 1:      ld r24, X 
118         adc r24, r1
119         st X+, r24
120         dec r25
121         brne 1b
122 /* call threefish1024_init */   
123         movw r24, CTX0
124         adiw r24, 16
125         movw r22, CTX0
126         movw CTX0, r24  /* CTX points to ctx->g */
127         movw r20, TFCTX0
128         rcall threefish1024_init
129         /* copy block to ctx->g */      
130         movw r26, CTX0
131         movw r30, BLOCK0
132         ldi r25, 128
133 1:      ld r24, Z+
134         st X+, r24
135         dec r25
136         brne 1b
137 /* call threefish1024_enc */    
138         movw r24, CTX0
139         movw r22, TFCTX0
140         rcall threefish1024_enc
141 /* xor block into ctx->g */     
142         movw r26, BLOCK0
143         movw r30, CTX0
144         ldi r25, 128
145 1:      ld r24, X+
146         ld r23, Z
147         eor r23, r24
148         st Z+, r23      
149         dec r25
150         brne 1b
151 /* clear 'first' bit in tweak */
152         sbiw r30, 1+2   
153         sbiw r30, 63
154         sbiw r30, 63
155         ld r24, Z
156         andi r24, ~0x40
157         st Z, r24
158 exit:
159         pop_range 2, 7
160         stack_free_large2 20*8
161         ret
162
163 /******************************************************************************/
164 /*
165 void ubi1024_lastBlock(ubi1024_ctx_t *ctx, const void *block, uint16_t length_b){
166         threefish1024_ctx_t tfctx;
167         while(length_b>UBI1024_BLOCKSIZE){
168                 ubi1024_nextBlock(ctx, block);
169                 block = (uint8_t*)block + UBI1024_BLOCKSIZE_B;
170                 length_b -= UBI1024_BLOCKSIZE;
171         }
172         ctx->tweak[15] |= 0x80;
173         ((uint64_t*)(ctx->tweak))[0] += (length_b+7)/8;
174         if(length_b & 0x07)
175                 ctx->tweak[14] |= 0x80;
176         threefish1024_init(ctx->g, ctx->tweak, &tfctx);
177         memset(ctx->g, 0, UBI1024_BLOCKSIZE_B);
178         memcpy(ctx->g, block, (length_b+7)/8);
179         if(length_b & 0x07)
180                 ctx->g[(length_b+7)/8-1] |= 0x80>>(length_b&7);
181         threefish1024_enc(ctx->g, &tfctx);
182         memxor(ctx->g, block, (length_b+7)/8);
183         if(length_b & 0x07){
184                 ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
185         }
186 }  
187 */
188 /*
189  * param ctx:     r24:r25
190  * param block:   r22:r23
191  * param ength_b: r20:r21
192  */
193 MASK_B  =  8 
194 LEN_B   =  9
195 TFCTX0  = 10
196 TFCTX1  = 11
197 CTX0    = 12
198 CTX1    = 13
199 BLOCK0  = 14
200 BLOCK1  = 15
201 LENGTH0 = 16
202 LENGTH1 = 17
203 .global ubi1024_lastBlock
204 ubi1024_lastBlock:
205 /* run nextBlock for preceding blocks*/
206         push_range 8, 17
207         movw CTX0, r24
208         movw BLOCK0, r22
209         movw LENGTH0, r20
210 1:      cpi LENGTH1, 5
211         brlo 2f
212         movw r24, CTX0
213         movw r22, BLOCK0
214         rcall ubi1024_nextBlock
215         ldi r25, 128
216         add BLOCK0, r25
217         adc BLOCK1, r1
218         subi LENGTH1, 4
219         rjmp 1b
220 2:      cpi LENGTH1, 4
221         brlo 3f
222         tst LENGTH0
223         breq 3f
224         movw r24, CTX0
225         movw r22, BLOCK0
226         rcall ubi1024_nextBlock
227         ldi r25, 128
228         add BLOCK0, r25
229         adc BLOCK1, r1
230         subi LENGTH1, 4
231 3:      /* now the real fun */
232     stack_alloc_large 20*8
233         adiw r30, 1
234         movw TFCTX0, r30
235         /* calculate LEN_B */
236         movw r24, LENGTH0
237         adiw r24, 7
238         lsr r25
239         ror r24
240         lsr r25
241         ror r24
242         lsr r25
243         ror r24
244         mov LEN_B, r24
245         /* add length to tweak */
246         movw r30, CTX0
247         ld r24, Z
248         add r24, LEN_B
249         st Z+, r24
250         ldi r25, 11
251 1:      ld r24, Z
252         adc r24, r1
253         st Z+, r24
254         dec r25
255         brne 1b
256         /* set 'final' bit*/
257         movw r30, CTX0
258         ldd r24, Z+15
259         ori r24, 0x80
260         std Z+15, r24
261         /* store in MASK_B if we do bit processing and set 'BitPad' bit*/
262         clr MASK_B
263         mov r24, LENGTH0
264         andi r24, 0x07
265         tst r24
266         breq 4f
267         ldd r25, Z+14
268         ori r25, 0x80
269         std Z+14, r25
270         ldi r25, 0x80
271         mov MASK_B, r25
272 1:      lsr MASK_B
273         dec r24
274         brne 1b
275 4:  /* call threefish1024_init*/
276         movw r24, CTX0
277         adiw r24, 16
278         movw r22, CTX0
279         movw CTX0, r24 /* CTX points at ctx->g */
280         movw r20, TFCTX0
281         rcall threefish1024_init
282         /* copy block to ctx->g */
283         movw r26, BLOCK0
284         movw r30, CTX0
285         mov r24, LEN_B
286         ldi r25, 128
287         sub r25, LEN_B
288         tst r24
289 1:      breq 2f
290         ld r22, X+
291         st Z+, r22
292         dec r24 
293         rjmp 1b
294 2:      tst MASK_B
295         breq 29f
296         or r22, MASK_B
297         st -Z, r22
298         adiw r30, 1
299 29:     tst r25
300 3:      breq 4f
301         st Z+, r1
302         dec r25
303         rjmp 3b
304 4: /* call threefish1024_enc */
305         movw r24, CTX0
306         movw r22, TFCTX0
307         rcall threefish1024_enc
308    /* xor block into ctx->g */
309         movw r30, CTX0
310         movw r26, BLOCK0
311         tst LEN_B
312 5:      breq 6f
313         ld r22, X+
314         ld r23, Z
315         eor r23, r22
316         st Z+, r23
317         dec LEN_B
318         rjmp 5b         
319 6:      tst MASK_B
320         breq 7f
321         eor r23, MASK_B
322         st -Z, r23
323         
324 7:      stack_free_large2 20*8
325         pop_range 8, 17
326         ret
327
328