]> git.cryptolib.org Git - avr-crypto-lib.git/blob - serpent/serpent-sboxes-small.S
fixing style, typos and uart
[avr-crypto-lib.git] / serpent / serpent-sboxes-small.S
1 /* serpent_sboxes.S */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  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 /* 
21  * File:        serpent_sboxes.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:
34  .byte 0x83, 0x1F, 0x6A, 0xB5, 0xDE, 0x24, 0x07, 0xC9
35  .byte 0xCF, 0x72, 0x09, 0xA5, 0xB1, 0x8E, 0xD6, 0x43 
36  .byte 0x68, 0x97, 0xC3, 0xFA, 0x1D, 0x4E, 0xB0, 0x25
37  .byte 0xF0, 0x8B, 0x9C, 0x36, 0x1D, 0x42, 0x7A, 0xE5
38  .byte 0xF1, 0x38, 0x0C, 0x6B, 0x52, 0xA4, 0xE9, 0xD7
39  .byte 0x5F, 0xB2, 0xA4, 0xC9, 0x30, 0x8E, 0x6D, 0x17
40  .byte 0x27, 0x5C, 0x48, 0xB6, 0x9E, 0xF1, 0x3D, 0x0A
41  .byte 0xD1, 0x0F, 0x8E, 0xB2, 0x47, 0xAC, 0x39, 0x65
42
43 serpent_sbox_inv:
44  .byte 0x3D, 0x0B, 0x6A, 0xC5, 0xE1, 0x74, 0x9F, 0x28
45  .byte 0x85, 0xE2, 0x6F, 0x3C, 0x4B, 0x97, 0xD1, 0x0A
46  .byte 0x9C, 0x4F, 0xEB, 0x21, 0x30, 0xD6, 0x85, 0x7A
47  .byte 0x90, 0x7A, 0xEB, 0xD6, 0x53, 0x2C, 0x84, 0x1F
48  .byte 0x05, 0x38, 0x9A, 0xE7, 0xC2, 0x6B, 0xF4, 0x1D
49  .byte 0xF8, 0x92, 0x14, 0xED, 0x6B, 0x35, 0xC7, 0x0A
50  .byte 0xAF, 0xD1, 0x35, 0x06, 0x94, 0x7E, 0xC2, 0xB8
51  .byte 0x03, 0xD6, 0xE9, 0x8F, 0xC5, 0x7B, 0x1A, 0x24                  
52  
53 /*
54  * void ip(uint32_t *i, uint8_t *o){
55  */
56 /* 
57  * param i is given in r24:r25
58  * parma o is given in r22:r23
59  */
60 .global serpent_ip
61 serpent_ip:
62         push_range 2, 17        
63         movw r26, r24
64         ldi r24, 16
65         clr r31
66         ldi r30, 2
67 1:
68         ld r25, X+
69         st Z+, r25
70         dec r24
71         brne 1b
72         /* now the whole input is loaded in r2-r18 */
73         movw r26, r22
74         ldi r21, 4
75 4:
76         ldi r20, 8      
77 2:      
78         lsr r2
79         ror r19
80         lsr r6
81         ror 19
82         lsr r10
83         ror r19
84         lsr r14
85         ror 19
86         sbrc r20, 0
87         st X+, r19
88         dec r20
89         brne 2b
90         
91         ldi r20, 15
92         ldi r30, 2
93 3:
94         ldd r19, Z+1
95         st Z+, r19
96         dec r20
97         brne 3b
98         
99         dec r21
100         brne 4b
101         pop_range 2, 17 
102         ret
103
104 /*
105  * void serpent_fp(uint32_t *i, uint8_t *o){
106  */
107 /* 
108  * param i is given in r24:r25
109  * parma o is given in r22:r23
110  */
111 .global serpent_fp
112 serpent_fp:
113         movw r26, r24
114         movw r30, r22
115         ldi r18, 4
116 1:      
117         ldi r19, 8 
118 2:
119         sbrs r19, 0
120         ld r24, X+
121 3:
122         lsr r24
123         ror r20
124         lsr r24
125         ror r21
126         lsr r24
127         ror r22
128         lsr r24
129         ror r23
130         dec r19
131         brne 2b
132         
133         st Z+, r20
134         std Z+3, r21
135         std Z+7, r22
136         std Z+11, r23
137         
138         dec r18
139         brne 1b 
140         
141         ret
142 /*
143  * void inv_sbox128(void * w, uint8_t box)
144  */
145 .global inv_sbox128 
146 inv_sbox128:
147         andi r22, 0x07
148         ori  r22, 0x08
149         rjmp sbox128x
150  
151 /*      
152  * void sbox128(void * w, uint8_t box);
153  */
154 /*
155  * param w   is passed in r24:r25
156  * param box is passed in r22
157  */
158 .global sbox128 
159 sbox128:
160         andi r22, 0x07
161
162 /*      
163  * void sbox128x(void * w, uint8_t box);
164  */
165 /*
166  * param w   is passed in r24:r25
167  * param box is passed in r22
168  */
169  
170 .global sbox128x
171 sbox128x:
172         stack_alloc 16
173         adiw r30, 1
174         push_ r24, r25, r22, r30, r31
175         movw r22, r30   /* Z points to the stack buffer */
176         rcall serpent_ip
177         pop_ r27, r26, r22
178         ldi r25, hi8(serpent_sbox)
179         ldi r24, lo8(serpent_sbox)
180         swap r22
181         lsr  r22 /* r22 *= 8 */
182         add r24, r22
183         adc r25, r1
184         /* now we have X pointing to the buffer and (r24:r25) pointing to the SBox */
185         ldi r22, 16
186 1:      
187         movw r30, r24
188         ld r18, X
189         mov r20, r18
190         andi r18, 0x0f
191         bst r18, 0
192         lsr r18
193         add r30, r18
194         adc r31, r1
195         lpm r19, Z
196         brtc 2f
197         swap r19
198 2:      
199         swap r20
200         andi r20, 0x0f
201         bst r20, 0
202         lsr r20
203         movw r30, r24
204         add r30, r20
205         adc r31, r1
206         lpm r21, Z
207         brts 3f
208         swap r21
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