]> git.cryptolib.org Git - avr-crypto-lib.git/blob - aes/aes_keyschedule-asm.S
new and more compact aes
[avr-crypto-lib.git] / aes / aes_keyschedule-asm.S
1 /* aes_keyschedule-asm */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008, 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  * \file     aes_keyschedule-asm.S
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2009-01-09
24  * \license  GPLv3 or later
25  * 
26  */
27
28 #include "avr-asm-macros.S"
29
30 .global aes256_init
31 aes256_init:
32         movw r20, r22
33         ldi r23, hi8(256)
34         ldi r22, lo8(256)
35         rjmp aes_init
36         
37 .global aes192_init
38 aes192_init:
39         movw r20, r22
40         ldi r23, hi8(192)
41         ldi r22, lo8(192)
42         rjmp aes_init
43         
44 .global aes128_init
45 aes128_init:
46         movw r20, r22
47         clr r23
48         ldi r22, 128
49
50 /* 
51 void aes_init(const void *key, uint16_t keysize_b, aes_genctx_t *ctx){
52         uint8_t hi,i,nk, next_nk;
53         uint8_t rc=1;
54         uint8_t tmp[4];
55         nk=keysize_b>>5; / * 4, 6, 8 * /
56         hi=4*(nk+6+1);
57         memcpy(ctx, key, keysize_b/8);
58         next_nk = nk;
59         for(i=nk;i<hi;++i){
60                 *((uint32_t*)tmp) = ((uint32_t*)(ctx->key[0].ks))[i-1];
61                 if(i!=next_nk){
62                         if(nk==8 && i%8==4){
63                                 tmp[0] = pgm_read_byte(aes_sbox+tmp[0]);
64                                 tmp[1] = pgm_read_byte(aes_sbox+tmp[1]);
65                                 tmp[2] = pgm_read_byte(aes_sbox+tmp[2]);
66                                 tmp[3] = pgm_read_byte(aes_sbox+tmp[3]);
67                         }
68                 } else {
69                         next_nk += nk;
70                         aes_rotword(tmp);
71                         tmp[0] = pgm_read_byte(aes_sbox+tmp[0]);
72                         tmp[1] = pgm_read_byte(aes_sbox+tmp[1]);
73                         tmp[2] = pgm_read_byte(aes_sbox+tmp[2]);
74                         tmp[3] = pgm_read_byte(aes_sbox+tmp[3]);
75                         tmp[0] ^= rc;
76                         rc<<=1;
77                 }
78                 ((uint32_t*)(ctx->key[0].ks))[i] = ((uint32_t*)(ctx->key[0].ks))[i-nk]
79                                                    ^ *((uint32_t*)tmp);
80         }
81 }
82 */
83
84 SBOX_SAVE0 = 14
85 SBOX_SAVE1 = 15
86 XRC = 17
87 NK = 22
88 C1 = 18
89 NEXT_NK = 19
90 HI = 23
91 T0 = 20
92 T1 = 21
93 T2 = 24
94 T3 = 25
95 /*
96  * param key:       r24:r25
97  * param keysize_b: r22:r23
98  * param ctx:       r20:r21
99  */
100 .global aes_init
101 aes_init:
102         push_range 14, 17
103         push r28
104         push r29
105         movw r30, r20
106         movw r28, r20
107         movw r26, r24
108         lsr r23
109         ror r22
110         lsr r22
111         lsr r22 /* r22 contains keysize_b/8 */
112         mov C1, r22
113
114 1:      /* copy key to ctx */ 
115         ld r0, X+
116         st Z+, r0
117         dec C1
118         brne 1b
119         
120         lsr NK
121         lsr NK
122         bst NK,3 /* set T if NK==8 */
123         mov NEXT_NK, NK
124         mov HI, NK
125         subi HI, -7
126         lsl HI
127         lsl HI
128         movw r26, r30
129         sbiw r26, 4
130         mov C1, NK
131         ldi r30, lo8(aes_sbox)
132         ldi r31, hi8(aes_sbox)
133         movw SBOX_SAVE0, r30
134         ldi XRC, 1
135 1:      
136         ld T0, X+
137         ld T1, X+
138         ld T2, X+
139         ld T3, X+
140         cp NEXT_NK, C1
141         breq 2f 
142         brtc 5f
143         mov r16, C1
144         andi r16, 0x07
145         cpi r16, 0x04
146         brne 5f
147         movw r30, SBOX_SAVE0
148         add r30, T0
149         adc r31, r1
150         lpm T0, Z
151         movw r30, SBOX_SAVE0
152         add r30, T1
153         adc r31, r1
154         lpm T1, Z
155         movw r30, SBOX_SAVE0
156         add r30, T2
157         adc r31, r1
158         lpm T2, Z
159         movw r30, SBOX_SAVE0
160         add r30, T3
161         adc r31, r1
162         lpm T3, Z
163         rjmp 5f
164 2:
165         add NEXT_NK, NK
166         movw r30, SBOX_SAVE0
167         add r30, T0
168         adc r31, r1
169         lpm r16, Z
170         movw r30, SBOX_SAVE0
171         add r30, T1
172         adc r31, r1
173         lpm T0, Z
174         movw r30, SBOX_SAVE0
175         add r30, T2
176         adc r31, r1
177         lpm T1, Z
178         movw r30, SBOX_SAVE0
179         add r30, T3
180         adc r31, r1
181         lpm T2, Z
182         mov T3, r16
183         eor T0, XRC
184         lsl XRC
185         brcc 3f
186         ldi XRC, 0x1b
187 3:
188 5:      
189         movw r30, r26
190
191         ld r0, Y+
192         eor r0, T0
193         st Z+, r0 
194         ld r0, Y+
195         eor r0 ,T1
196         st Z+, r0
197         ld r0, Y+
198         eor r0, T2
199         st Z+, r0
200         ld r0, Y+
201         eor r0, T3
202         st Z+, r0
203         
204 /*
205         st Z+, T0
206         st Z+, T1
207         st Z+, T2
208         st Z+, T3
209 */              
210         
211         inc C1
212         cp C1, HI
213         breq 6f
214         rjmp 1b
215 6:      
216         
217         clt
218         pop r29
219         pop r28
220         pop_range 14, 17
221         ret
222         
223         
224         
225