]> git.cryptolib.org Git - avr-crypto-lib.git/blob - skein/threefish512_dec_asm.S
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / skein / threefish512_dec_asm.S
1 /* threefish512_enc_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-24
23  * \license GPLv3 or later
24  */
25
26 #include "avr-asm-macros.S"
27
28 /******************************************************************************/
29 /*
30 #define X(a) (((uint64_t*)data)[(a)])
31
32
33 static
34 void permute_inv8(void *data){
35         uint64_t t;
36         t = X(6);
37         X(6) = X(4);
38         X(4) = X(2);
39         X(2) = X(0);
40         X(0) = t;
41         t = X(7);
42         X(7) = X(3);
43         X(3) = t;
44 }
45
46 static
47 void add_key_8(void *data, const threefish512_ctx_t *ctx, uint8_t s){
48         uint8_t i;
49         for(i=0; i<5; ++i){
50                 X(i) -= ctx->k[(s+i)%9];
51         }
52         X(5) -= ctx->k[(s+5)%9] + ctx->t[s%3];
53         X(6) -= ctx->k[(s+6)%9] + ctx->t[(s+1)%3];
54         X(7) -= ctx->k[(s+7)%9] + s;
55 }
56
57 void threefish512_dec(void *data, const threefish512_ctx_t *ctx){
58         uint8_t i=0,s=18;
59         uint8_t r0[8] = {0x41, 0x4b, 0x59, 0x41, 0x32, 0x42, 0x60, 0x5a};
60         uint8_t r1[8] = {0x63, 0x32, 0x33, 0x61, 0x14, 0x2a, 0x24, 0x4a};
61         uint8_t r2[8] = {0x59, 0x13, 0x51, 0x10, 0x72, 0x29, 0x53, 0x62};
62         uint8_t r3[8] = {0x43, 0x11, 0x2a, 0x52, 0x19, 0x33, 0x49, 0x7b};
63         do{
64                 if(i%4==0){
65                         add_key_8(data, ctx, s);
66                         --s;
67                 }
68                 permute_inv8(data);
69                 threefish_invmix((uint8_t*)data +  0, r0[i%8]);
70                 threefish_invmix((uint8_t*)data + 16, r1[i%8]);
71                 threefish_invmix((uint8_t*)data + 32, r2[i%8]);
72                 threefish_invmix((uint8_t*)data + 48, r3[i%8]);
73                 ++i;
74         }while(i!=72);
75         add_key_8(data, ctx, s);
76 }
77 */
78 I     =  2
79 S     =  3
80 DATA0 =  4
81 DATA1 =  5
82 CTX0  =  6
83 CTX1  =  7
84 IDX0  =  8
85 IDX1  =  9
86 IDX2  = 10
87 IDX3  = 11
88 IDX4  = 12
89 IDX5  = 13
90 IDX6  = 14
91 IDX7  = 15
92 /*
93  * param data:  r24:r25
94  * param ctx:   r22:r23
95  */
96 .global threefish512_dec
97 threefish512_dec:
98         push r28
99         push r29
100         push_range 2, 17
101         movw DATA0, r24
102         movw CTX0, r22
103         clr I
104         ldi r26, 18
105         mov S, r26
106 1:
107         mov r30,  I
108         andi r30, 0x03
109         breq 2f
110         rjmp 4f
111 2:
112         ldi r30, lo8(threefish512_slut9)
113         ldi r31, hi8(threefish512_slut9)
114         add r30, S
115         adc r31, r1
116         lpm IDX0, Z+
117         lpm IDX1, Z+
118         lpm IDX2, Z+
119         lpm IDX3, Z+
120         lpm IDX4, Z+
121         lpm IDX5, Z+
122         lpm IDX6, Z+
123         lpm IDX7, Z
124         movw r30, CTX0
125         movw r26, DATA0
126         add r30, IDX0
127         adc r31, r1
128         rcall sub_z_from_x8
129         movw r30, CTX0
130         add r30, IDX1
131         adc r31, r1
132         rcall sub_z_from_x8
133         movw r30, CTX0
134         add r30, IDX2
135         adc r31, r1
136         rcall sub_z_from_x8
137         movw r30, CTX0
138         add r30, IDX3
139         adc r31, r1
140         rcall sub_z_from_x8
141         movw r30, CTX0
142         add r30, IDX4
143         adc r31, r1
144         rcall sub_z_from_x8
145         movw r30, CTX0
146         add r30, IDX5
147         adc r31, r1
148         rcall sub_z_from_x8
149         movw r30, CTX0
150         add r30, IDX6
151         adc r31, r1
152         rcall sub_z_from_x8
153         movw r30, CTX0
154         add r30, IDX7
155         adc r31, r1
156         rcall sub_z_from_x8
157
158         /* now the remaining key */
159         sbiw r26, 3*8
160         ldi r30, lo8(threefish512_slut3)
161         ldi r31, hi8(threefish512_slut3)
162         add r30, S
163         adc r31, r1
164         lpm IDX0, Z+
165         lpm IDX1, Z
166         movw r30, CTX0
167         adiw r30, 7*8 /* make Z pointing to (extended tweak) */
168         adiw r30, 2*8
169         movw IDX2, r30
170         add r30, IDX0
171         adc r31, r1
172         rcall sub_z_from_x8
173         movw r30, IDX2
174         add r30, IDX1
175         adc r31, r1
176         rcall sub_z_from_x8
177         ld r0, X
178         sub r0, S
179         st X+, r0
180         ld r0, X
181         sbc r0, r1
182         st X+, r0
183         ld r0, X
184         sbc r0, r1
185         st X+, r0
186         ld r0, X
187         sbc r0, r1
188         st X+, r0
189         ld r0, X
190         sbc r0, r1
191         st X+, r0
192         ld r0, X
193         sbc r0, r1
194         st X+, r0
195         ld r0, X
196         sbc r0, r1
197         st X+, r0
198         ld r0, X
199         sbc r0, r1
200         st X+, r0
201         tst S
202         brne 3f
203 exit:
204         pop_range 2, 17
205         pop r29
206         pop r28
207         ret
208 3:
209         dec S
210 4:
211         /* now the permutation */
212         movw r26, DATA0
213         movw r30, DATA0
214         adiw r30, 6*8
215         rcall xchg_zx8
216         movw r26, DATA0
217         adiw r26, 6*8
218         movw r30, DATA0
219         adiw r30, 4*8
220         rcall xchg_zx8
221         movw r26, DATA0
222         adiw r26, 2*8
223         movw r30, DATA0
224         adiw r30, 4*8
225         rcall xchg_zx8
226         movw r26, DATA0
227         adiw r26, 3*8
228         movw r30, DATA0
229         adiw r30, 7*8
230         rcall xchg_zx8
231         /* call mix */
232         ldi r30, lo8(threefish512_rc0)
233         ldi r31, hi8(threefish512_rc0)
234         mov r26, I
235         andi r26, 0x07
236         add r30, r26
237         adc r31, r1
238         lpm r22, Z
239         adiw r30, 8
240         lpm IDX0, Z
241         adiw r30, 8
242         lpm IDX1, Z
243         push IDX1
244         adiw r30, 8
245         lpm IDX1, Z
246
247         movw r24, DATA0
248         call threefish_invmix_asm /* no rcall? */
249         movw r24, DATA0
250         adiw r24, 16
251         mov r22, IDX0
252         call threefish_invmix_asm /* no rcall? */
253         movw r24, DATA0
254         adiw r24, 32
255         pop r22
256         ;mov r22, IDX0
257         call threefish_invmix_asm /* no rcall? */
258         movw r24, DATA0
259         adiw r24, 48
260         mov r22, IDX1
261         call threefish_invmix_asm /* no rcall? */
262         inc I
263         rjmp 1b
264
265 threefish512_slut9:
266         .byte 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38
267         .byte 0x40, 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30
268         .byte 0x38, 0x40, 0x00, 0x08, 0x10, 0x18, 0x20, 0x28
269         .byte 0x30, 0x38, 0x40
270 threefish512_slut3:
271         .byte 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08
272         .byte 0x10, 0x00, 0x08, 0x10, 0x00, 0x08, 0x10, 0x00
273         .byte 0x08, 0x10, 0x00, 0x08, 0x10, 0x00, 0x08
274
275 /* old round constants
276 threefish512_rc0: .byte 0x41, 0x4b, 0x59, 0x41, 0x32, 0x42, 0x60, 0x5a
277 threefish512_rc1: .byte 0x63, 0x32, 0x33, 0x61, 0x14, 0x2a, 0x24, 0x4a
278 threefish512_rc2: .byte 0x59, 0x13, 0x51, 0x10, 0x72, 0x29, 0x53, 0x62
279 threefish512_rc3: .byte 0x43, 0x11, 0x2a, 0x52, 0x19, 0x33, 0x49, 0x7b
280 */
281 threefish512_rc0:  .byte 0x10, 0x31, 0x2b, 0x59, 0x54, 0x21, 0x41, 0x6a
282 threefish512_rc1:  .byte 0x43, 0x4b, 0x62, 0x4a, 0x11, 0x61, 0x33, 0x44
283 threefish512_rc2:  .byte 0x70, 0x59, 0x12, 0x42, 0x7a, 0x44, 0x2a, 0x23
284 threefish512_rc3:  .byte 0x3a, 0x53, 0x21, 0x30, 0x70, 0x59, 0x52, 0x5b
285
286 sub_z_from_x8:
287         ld r0, Z+
288         ld r1, X
289         sub r1, r0
290         st X+, r1
291         ld r0, Z+
292         ld r1, X
293         sbc r1, r0
294         st X+, r1
295         ld r0, Z+
296         ld r1, X
297         sbc r1, r0
298         st X+, r1
299         ld r0, Z+
300         ld r1, X
301         sbc r1, r0
302         st X+, r1
303         ld r0, Z+
304         ld r1, X
305         sbc r1, r0
306         st X+, r1
307         ld r0, Z+
308         ld r1, X
309         sbc r1, r0
310         st X+, r1
311         ld r0, Z+
312         ld r1, X
313         sbc r1, r0
314         st X+, r1
315         ld r0, Z+
316         ld r1, X
317         sbc r1, r0
318         st X+, r1
319         clr r1
320         ret
321
322 T0 = IDX0
323 T1 = 0
324 CNT = 24
325 xchg_zx8:
326         ldi CNT, 8
327 1:      ld T0, X
328         ld T1, Z
329         st X+, T1
330         st Z+, T0
331         dec CNT
332         brne 1b
333         ret
334
335
336