]> git.cryptolib.org Git - avr-crypto-lib.git/blob - serpent/serpent-sboxes-fast.S
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / serpent / serpent-sboxes-fast.S
1 /* serpent-sboxes-fast.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 /* 
21  * File:        serpent-sboxes-fast.S
22  * Author:      Daniel Otte
23  * Date:        2008-08-07
24  * License:     GPLv3 or later
25  * Description: Implementation of the serpent sbox function.
26  * 
27  */
28  
29 #include <avr/io.h>
30 #include "avr-asm-macros.S"
31
32
33 serpent_sbox_fast:
34  .byte 0x33, 0x88, 0xFF, 0x11, 0xAA, 0x66, 0x55, 0xBB 
35  .byte 0xEE, 0xDD, 0x44, 0x22, 0x77, 0x00, 0x99, 0xCC
36  .byte 0xFF, 0xCC, 0x22, 0x77, 0x99, 0x00, 0x55, 0xAA
37  .byte 0x11, 0xBB, 0xEE, 0x88, 0x66, 0xDD, 0x33, 0x44 
38  .byte 0x88, 0x66, 0x77, 0x99, 0x33, 0xCC, 0xAA, 0xFF 
39  .byte 0xDD, 0x11, 0xEE, 0x44, 0x00, 0xBB, 0x55, 0x22
40  .byte 0x00, 0xFF, 0xBB, 0x88, 0xCC, 0x99, 0x66, 0x33 
41  .byte 0xDD, 0x11, 0x22, 0x44, 0xAA, 0x77, 0x55, 0xEE
42  .byte 0x11, 0xFF, 0x88, 0x33, 0xCC, 0x00, 0xBB, 0x66 
43  .byte 0x22, 0x55, 0x44, 0xAA, 0x99, 0xEE, 0x77, 0xDD
44  .byte 0xFF, 0x55, 0x22, 0xBB, 0x44, 0xAA, 0x99, 0xCC 
45  .byte 0x00, 0x33, 0xEE, 0x88, 0xDD, 0x66, 0x77, 0x11
46  .byte 0x77, 0x22, 0xCC, 0x55, 0x88, 0x44, 0x66, 0xBB 
47  .byte 0xEE, 0x99, 0x11, 0xFF, 0xDD, 0x33, 0xAA, 0x00
48  .byte 0x11, 0xDD, 0xFF, 0x00, 0xEE, 0x88, 0x22, 0xBB 
49  .byte 0x77, 0x44, 0xCC, 0xAA, 0x99, 0x33, 0x55, 0x66
50
51 serpent_sbox_inv_fast:
52  .byte 0xDD, 0x33, 0xBB, 0x00, 0xAA, 0x66, 0x55, 0xCC 
53  .byte 0x11, 0xEE, 0x44, 0x77, 0xFF, 0x99, 0x88, 0x22
54  .byte 0x55, 0x88, 0x22, 0xEE, 0xFF, 0x66, 0xCC, 0x33 
55  .byte 0xBB, 0x44, 0x77, 0x99, 0x11, 0xDD, 0xAA, 0x00
56  .byte 0xCC, 0x99, 0xFF, 0x44, 0xBB, 0xEE, 0x11, 0x22 
57  .byte 0x00, 0x33, 0x66, 0xDD, 0x55, 0x88, 0xAA, 0x77
58  .byte 0x00, 0x99, 0xAA, 0x77, 0xBB, 0xEE, 0x66, 0xDD 
59  .byte 0x33, 0x55, 0xCC, 0x22, 0x44, 0x88, 0xFF, 0x11
60  .byte 0x55, 0x00, 0x88, 0x33, 0xAA, 0x99, 0x77, 0xEE 
61  .byte 0x22, 0xCC, 0xBB, 0x66, 0x44, 0xFF, 0xDD, 0x11
62  .byte 0x88, 0xFF, 0x22, 0x99, 0x44, 0x11, 0xDD, 0xEE 
63  .byte 0xBB, 0x66, 0x55, 0x33, 0x77, 0xCC, 0xAA, 0x00
64  .byte 0xFF, 0xAA, 0x11, 0xDD, 0x55, 0x33, 0x66, 0x00 
65  .byte 0x44, 0x99, 0xEE, 0x77, 0x22, 0xCC, 0x88, 0xBB
66  .byte 0x33, 0x00, 0x66, 0xDD, 0x99, 0xEE, 0xFF, 0x88 
67  .byte 0x55, 0xCC, 0xBB, 0x77, 0xAA, 0x11, 0x44, 0x22
68                   
69  
70 /*
71  * void ip(uint32_t *i, uint8_t *o){
72  */
73 /* 
74  * param i is given in r24:r25
75  * parma o is given in r22:r23
76  */
77 .global serpent_ip
78 serpent_ip:
79         push_range 2, 17        
80         movw r26, r24
81         ldi r24, 16
82         clr r31
83         ldi r30, 2
84 1:
85         ld r25, X+
86         st Z+, r25
87         dec r24
88         brne 1b
89         /* now the whole input is loaded in r2-r18 */
90         movw r26, r22
91         ldi r21, 4
92 4:
93         ldi r20, 8      
94 2:      
95         lsr r2
96         ror r19
97         lsr r6
98         ror 19
99         lsr r10
100         ror r19
101         lsr r14
102         ror 19
103         sbrc r20, 0
104         st X+, r19
105         dec r20
106         brne 2b
107         
108         ldi r20, 15
109         ldi r30, 2
110 3:
111         ldd r19, Z+1
112         st Z+, r19
113         dec r20
114         brne 3b
115         
116         dec r21
117         brne 4b
118         pop_range 2, 17 
119         ret
120
121 /*
122  * void serpent_fp(uint32_t *i, uint8_t *o){
123  */
124 /* 
125  * param i is given in r24:r25
126  * parma o is given in r22:r23
127  */
128 .global serpent_fp
129 serpent_fp:
130         movw r26, r24
131         movw r30, r22
132         ldi r18, 4
133 1:      
134         ldi r19, 8 
135 2:
136         sbrs r19, 0
137         ld r24, X+
138 3:
139         lsr r24
140         ror r20
141         lsr r24
142         ror r21
143         lsr r24
144         ror r22
145         lsr r24
146         ror r23
147         dec r19
148         brne 2b
149         
150         st Z+, r20
151         std Z+3, r21
152         std Z+7, r22
153         std Z+11, r23
154         
155         dec r18
156         brne 1b         
157         ret
158         
159
160 /*
161  * void inv_sbox128(void * w, uint8_t box)
162  */
163 .global inv_sbox128 
164 inv_sbox128:
165         andi r22, 0x07
166         ori  r22, 0x08
167         rjmp sbox128x_fast
168  
169 /*      
170  * void sbox128(void * w, uint8_t box);
171  */
172 /*
173  * param w   is passed in r24:r25
174  * param box is passed in r22
175  */
176 .global sbox128 
177 sbox128:
178         andi r22, 0x07
179  
180 sbox128x_fast:
181         stack_alloc 16
182         adiw r30, 1
183         push_ r24, r25, r22, r30, r31
184         movw r22, r30   /* Z points to the stack buffer */
185         rcall serpent_ip
186         pop_ r27, r26, r22
187         ldi r25, hi8(serpent_sbox_fast)
188         ldi r24, lo8(serpent_sbox_fast)
189         swap r22 /* r22 *= 16 */
190         add r24, r22
191         adc r25, r1
192         /* now we have X pointing to the buffer and (r24:r25) pointing to the SBox */
193         ldi r22, 16
194 1:      
195         movw r30, r24
196         ld r18, X
197         mov r20, r18
198         andi r18, 0x0f
199         add r30, r18
200         adc r31, r1
201         lpm r19, Z
202 2:      
203         swap r20
204         andi r20, 0x0f
205         movw r30, r24
206         add r30, r20
207         adc r31, r1
208         lpm r21, Z
209 3:      
210         andi r19, 0x0F
211         andi r21, 0xF0
212         or r19, r21
213         st X+, r19
214         dec r22
215         brne 1b
216         
217         pop_ r23, r22
218         movw r24, r26
219         sbiw r24, 16
220
221         rcall serpent_fp
222         
223         stack_free 16   
224         ret
225
226
227
228
229
230
231  
232
233